From 68f2861e92e08eb5e2af51c026981bc1e990e1eb Mon Sep 17 00:00:00 2001 From: Petri Lehtinen Date: Tue, 15 Jun 2010 15:27:35 +0300 Subject: [PATCH] Unify unsigned integer usage in the API Replace all occurences of unsigned int and unsigned long with size_t. This is a backwards incompatible change, as the signature of many API functions changes. --- doc/apiref.rst | 22 +++++++++--------- doc/github_commits.c | 2 +- doc/tutorial.rst | 2 +- src/dump.c | 13 +++++------ src/hashtable.c | 26 +++++++++++----------- src/hashtable.h | 8 +++---- src/jansson.h.in | 29 ++++++++++++------------ src/jansson_private.h | 8 +++---- src/value.c | 52 +++++++++++++++++++++---------------------- test/bin/json_process.c | 2 +- test/suites/api/test_copy.c | 4 ++-- test/suites/api/test_simple.c | 18 +++++++-------- 12 files changed, 93 insertions(+), 93 deletions(-) diff --git a/doc/apiref.rst b/doc/apiref.rst index bacd756..11a24c1 100644 --- a/doc/apiref.rst +++ b/doc/apiref.rst @@ -345,12 +345,12 @@ A JSON array is an ordered collection of other JSON values. Returns a new JSON array, or *NULL* on error. Initially, the array is empty. -.. cfunction:: unsigned int json_array_size(const json_t *array) +.. cfunction:: size_t json_array_size(const json_t *array) Returns the number of elements in *array*, or 0 if *array* is NULL or not a JSON array. -.. cfunction:: json_t *json_array_get(const json_t *array, unsigned int index) +.. cfunction:: json_t *json_array_get(const json_t *array, size_t index) .. refcounting:: borrow @@ -360,14 +360,14 @@ A JSON array is an ordered collection of other JSON values. if *array* is *NULL*, or if *index* is out of range, *NULL* is returned. -.. cfunction:: int json_array_set(json_t *array, unsigned int index, json_t *value) +.. cfunction:: int json_array_set(json_t *array, size_t index, json_t *value) Replaces the element in *array* at position *index* with *value*. The valid range for *index* is from 0 to the return value of :cfunc:`json_array_size()` minus 1. Returns 0 on success and -1 on error. -.. cfunction:: int json_array_set_new(json_t *array, unsigned int index, json_t *value) +.. cfunction:: int json_array_set_new(json_t *array, size_t index, json_t *value) Like :cfunc:`json_array_set()` but steals the reference to *value*. This is useful when *value* is newly created and not used after @@ -388,7 +388,7 @@ A JSON array is an ordered collection of other JSON values. .. versionadded:: 1.1 -.. cfunction:: int json_array_insert(json_t *array, unsigned int index, json_t *value) +.. cfunction:: int json_array_insert(json_t *array, size_t index, json_t *value) Inserts *value* to *array* at position *index*, shifting the elements at *index* and after it one position towards the end of @@ -396,7 +396,7 @@ A JSON array is an ordered collection of other JSON values. .. versionadded:: 1.1 -.. cfunction:: int json_array_insert_new(json_t *array, unsigned int index, json_t *value) +.. cfunction:: int json_array_insert_new(json_t *array, size_t index, json_t *value) Like :cfunc:`json_array_insert()` but steals the reference to *value*. This is useful when *value* is newly created and not used @@ -404,7 +404,7 @@ A JSON array is an ordered collection of other JSON values. .. versionadded:: 1.1 -.. cfunction:: int json_array_remove(json_t *array, unsigned int index) +.. cfunction:: int json_array_remove(json_t *array, size_t index) Removes the element in *array* at position *index*, shifting the elements after *index* one position towards the start of the array. @@ -440,7 +440,7 @@ Unicode string and the value is any JSON value. Returns a new JSON object, or *NULL* on error. Initially, the object is empty. -.. cfunction:: unsigned int json_object_size(const json_t *object) +.. cfunction:: size_t json_object_size(const json_t *object) Returns the number of elements in *object*, or 0 if *object* is not a JSON object. @@ -628,13 +628,13 @@ can be ORed together to obtain *flags*. The following functions perform the actual JSON encoding. The result is in UTF-8. -.. cfunction:: char *json_dumps(const json_t *root, unsigned long flags) +.. cfunction:: char *json_dumps(const json_t *root, size_t flags) Returns the JSON representation of *root* as a string, or *NULL* on error. *flags* is described above. The return value must be freed by the caller using :cfunc:`free()`. -.. cfunction:: int json_dumpf(const json_t *root, FILE *output, unsigned long flags) +.. cfunction:: int json_dumpf(const json_t *root, FILE *output, size_t flags) Write the JSON representation of *root* to the stream *output*. *flags* is described above. Returns 0 on success and -1 on error. @@ -642,7 +642,7 @@ is in UTF-8. *output*. In this case, the output is undefined and most likely not valid JSON. -.. cfunction:: int json_dump_file(const json_t *json, const char *path, unsigned long flags) +.. cfunction:: int json_dump_file(const json_t *json, const char *path, size_t flags) Write the JSON representation of *root* to the file *path*. If *path* already exists, it is overwritten. *flags* is described diff --git a/doc/github_commits.c b/doc/github_commits.c index 8a362eb..0fc1a1b 100644 --- a/doc/github_commits.c +++ b/doc/github_commits.c @@ -96,7 +96,7 @@ static char *request(const char *url) int main(int argc, char *argv[]) { - unsigned int i; + size_t i; char *text; char url[URL_SIZE]; diff --git a/doc/tutorial.rst b/doc/tutorial.rst index aaeb554..caa11c2 100644 --- a/doc/tutorial.rst +++ b/doc/tutorial.rst @@ -118,7 +118,7 @@ first newline in the commit message:: The main function follows. In the beginning, we first declare a bunch of variables and check the command line parameters:: - unsigned int i; + size_t i; char *text; char url[URL_SIZE]; diff --git a/src/dump.c b/src/dump.c index dc27fbd..3f34919 100644 --- a/src/dump.c +++ b/src/dump.c @@ -44,7 +44,7 @@ static int dump_to_file(const char *buffer, int size, void *data) /* 256 spaces (the maximum indentation size) */ static char whitespace[] = " "; -static int dump_indent(unsigned long flags, int depth, int space, dump_func dump, void *data) +static int dump_indent(size_t flags, int depth, int space, dump_func dump, void *data) { if(JSON_INDENT(flags) > 0) { @@ -165,7 +165,7 @@ static int object_key_compare_serials(const void *key1, const void *key2) (*(const object_key_t **)key2)->serial; } -static int do_dump(const json_t *json, unsigned long flags, int depth, +static int do_dump(const json_t *json, size_t flags, int depth, dump_func dump, void *data) { int ascii = flags & JSON_ENSURE_ASCII ? 1 : 0; @@ -307,8 +307,7 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, if(flags & JSON_SORT_KEYS || flags & JSON_PRESERVE_ORDER) { const object_key_t **keys; - unsigned int size; - unsigned int i; + size_t size, i; int (*cmp_func)(const void *, const void *); size = json_object_size(json); @@ -415,7 +414,7 @@ static int do_dump(const json_t *json, unsigned long flags, int depth, } -char *json_dumps(const json_t *json, unsigned long flags) +char *json_dumps(const json_t *json, size_t flags) { strbuffer_t strbuff; char *result; @@ -437,7 +436,7 @@ char *json_dumps(const json_t *json, unsigned long flags) return result; } -int json_dumpf(const json_t *json, FILE *output, unsigned long flags) +int json_dumpf(const json_t *json, FILE *output, size_t flags) { if(!json_is_array(json) && !json_is_object(json)) return -1; @@ -445,7 +444,7 @@ int json_dumpf(const json_t *json, FILE *output, unsigned long flags) return do_dump(json, flags, 0, dump_to_file, (void *)output); } -int json_dump_file(const json_t *json, const char *path, unsigned long flags) +int json_dump_file(const json_t *json, const char *path, size_t flags) { int result; diff --git a/src/hashtable.c b/src/hashtable.c index a312047..dcba68a 100644 --- a/src/hashtable.c +++ b/src/hashtable.c @@ -59,22 +59,22 @@ static void insert_to_bucket(hashtable_t *hashtable, bucket_t *bucket, } } -static unsigned int primes[] = { +static size_t primes[] = { 5, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469, 12582917, 25165843, 50331653, 100663319, 201326611, 402653189, 805306457, 1610612741 }; -static const unsigned int num_primes = sizeof(primes) / sizeof(unsigned int); +static const size_t num_primes = sizeof(primes) / sizeof(size_t); -static inline unsigned int num_buckets(hashtable_t *hashtable) +static inline size_t num_buckets(hashtable_t *hashtable) { return primes[hashtable->num_buckets]; } static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket, - const void *key, unsigned int hash) + const void *key, size_t hash) { list_t *list; pair_t *pair; @@ -100,11 +100,11 @@ static pair_t *hashtable_find_pair(hashtable_t *hashtable, bucket_t *bucket, /* returns 0 on success, -1 if key was not found */ static int hashtable_do_del(hashtable_t *hashtable, - const void *key, unsigned int hash) + const void *key, size_t hash) { pair_t *pair; bucket_t *bucket; - unsigned int index; + size_t index; index = hash % num_buckets(hashtable); bucket = &hashtable->buckets[index]; @@ -156,7 +156,7 @@ static int hashtable_do_rehash(hashtable_t *hashtable) { list_t *list, *next; pair_t *pair; - unsigned int i, index, new_size; + size_t i, index, new_size; free(hashtable->buckets); @@ -213,7 +213,7 @@ int hashtable_init(hashtable_t *hashtable, key_hash_fn hash_key, key_cmp_fn cmp_keys, free_fn free_key, free_fn free_value) { - unsigned int i; + size_t i; hashtable->size = 0; hashtable->num_buckets = 0; /* index to primes[] */ @@ -247,7 +247,7 @@ int hashtable_set(hashtable_t *hashtable, void *key, void *value) { pair_t *pair; bucket_t *bucket; - unsigned int hash, index; + size_t hash, index; /* rehash if the load ratio exceeds 1 */ if(hashtable->size >= num_buckets(hashtable)) @@ -288,7 +288,7 @@ int hashtable_set(hashtable_t *hashtable, void *key, void *value) void *hashtable_get(hashtable_t *hashtable, const void *key) { pair_t *pair; - unsigned int hash; + size_t hash; bucket_t *bucket; hash = hashtable->hash_key(key); @@ -303,13 +303,13 @@ void *hashtable_get(hashtable_t *hashtable, const void *key) int hashtable_del(hashtable_t *hashtable, const void *key) { - unsigned int hash = hashtable->hash_key(key); + size_t hash = hashtable->hash_key(key); return hashtable_do_del(hashtable, key, hash); } void hashtable_clear(hashtable_t *hashtable) { - unsigned int i; + size_t i; hashtable_do_clear(hashtable); @@ -331,7 +331,7 @@ void *hashtable_iter(hashtable_t *hashtable) void *hashtable_iter_at(hashtable_t *hashtable, const void *key) { pair_t *pair; - unsigned int hash; + size_t hash; bucket_t *bucket; hash = hashtable->hash_key(key); diff --git a/src/hashtable.h b/src/hashtable.h index f03a769..aba5134 100644 --- a/src/hashtable.h +++ b/src/hashtable.h @@ -8,7 +8,7 @@ #ifndef HASHTABLE_H #define HASHTABLE_H -typedef unsigned int (*key_hash_fn)(const void *key); +typedef size_t (*key_hash_fn)(const void *key); typedef int (*key_cmp_fn)(const void *key1, const void *key2); typedef void (*free_fn)(void *key); @@ -20,7 +20,7 @@ struct hashtable_list { struct hashtable_pair { void *key; void *value; - unsigned int hash; + size_t hash; struct hashtable_list list; }; @@ -30,9 +30,9 @@ struct hashtable_bucket { }; typedef struct hashtable { - unsigned int size; + size_t size; struct hashtable_bucket *buckets; - unsigned int num_buckets; /* index to primes[] */ + size_t num_buckets; /* index to primes[] */ struct hashtable_list list; key_hash_fn hash_key; diff --git a/src/jansson.h.in b/src/jansson.h.in index 4980d01..0840c48 100644 --- a/src/jansson.h.in +++ b/src/jansson.h.in @@ -9,6 +9,7 @@ #define JANSSON_H #include +#include /* for size_t */ #ifndef __cplusplus #define JSON_INLINE @json_inline@ @@ -32,7 +33,7 @@ typedef enum { typedef struct { json_type type; - unsigned long refcount; + size_t refcount; } json_t; #define json_typeof(json) ((json)->type) @@ -62,7 +63,7 @@ json_t *json_null(void); static JSON_INLINE json_t *json_incref(json_t *json) { - if(json && json->refcount != (unsigned int)-1) + if(json && json->refcount != (size_t)-1) ++json->refcount; return json; } @@ -73,14 +74,14 @@ void json_delete(json_t *json); static JSON_INLINE void json_decref(json_t *json) { - if(json && json->refcount != (unsigned int)-1 && --json->refcount == 0) + if(json && json->refcount != (size_t)-1 && --json->refcount == 0) json_delete(json); } /* getters, setters, manipulation */ -unsigned int json_object_size(const json_t *object); +size_t json_object_size(const json_t *object); json_t *json_object_get(const json_t *object, const char *key); int json_object_set_new(json_t *object, const char *key, json_t *value); int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value); @@ -112,17 +113,17 @@ int json_object_iter_set(json_t *object, void *iter, json_t *value) return json_object_iter_set_new(object, iter, json_incref(value)); } -unsigned int json_array_size(const json_t *array); -json_t *json_array_get(const json_t *array, unsigned int index); -int json_array_set_new(json_t *array, unsigned int index, json_t *value); +size_t json_array_size(const json_t *array); +json_t *json_array_get(const json_t *array, size_t index); +int json_array_set_new(json_t *array, size_t index, json_t *value); int json_array_append_new(json_t *array, json_t *value); -int json_array_insert_new(json_t *array, unsigned int index, json_t *value); -int json_array_remove(json_t *array, unsigned int index); +int json_array_insert_new(json_t *array, size_t index, json_t *value); +int json_array_remove(json_t *array, size_t index); int json_array_clear(json_t *array); int json_array_extend(json_t *array, json_t *other); static JSON_INLINE -int json_array_set(json_t *array, unsigned int index, json_t *value) +int json_array_set(json_t *array, size_t index, json_t *value) { return json_array_set_new(array, index, json_incref(value)); } @@ -134,7 +135,7 @@ int json_array_append(json_t *array, json_t *value) } static JSON_INLINE -int json_array_insert(json_t *array, unsigned int index, json_t *value) +int json_array_insert(json_t *array, size_t index, json_t *value) { return json_array_insert_new(array, index, json_incref(value)); } @@ -180,9 +181,9 @@ json_t *json_load_file(const char *path, json_error_t *error); #define JSON_SORT_KEYS 0x400 #define JSON_PRESERVE_ORDER 0x800 -char *json_dumps(const json_t *json, unsigned long flags); -int json_dumpf(const json_t *json, FILE *output, unsigned long flags); -int json_dump_file(const json_t *json, const char *path, unsigned long flags); +char *json_dumps(const json_t *json, size_t flags); +int json_dumpf(const json_t *json, FILE *output, size_t flags); +int json_dump_file(const json_t *json, const char *path, size_t flags); #ifdef __cplusplus } diff --git a/src/jansson_private.h b/src/jansson_private.h index 4490702..77a3c6f 100644 --- a/src/jansson_private.h +++ b/src/jansson_private.h @@ -17,14 +17,14 @@ typedef struct { json_t json; hashtable_t hashtable; - unsigned long serial; + size_t serial; int visited; } json_object_t; typedef struct { json_t json; - unsigned int size; - unsigned int entries; + size_t size; + size_t entries; json_t **table; int visited; } json_array_t; @@ -51,7 +51,7 @@ typedef struct { #define json_to_integer(json_) container_of(json_, json_integer_t, json) typedef struct { - unsigned long serial; + size_t serial; char key[]; } object_key_t; diff --git a/src/value.c b/src/value.c index e024fdb..bbc43c8 100644 --- a/src/value.c +++ b/src/value.c @@ -34,14 +34,14 @@ static inline void json_init(json_t *json, json_type type) an object_key_t instance. */ #define string_to_key(string) container_of(string, object_key_t, key) -static unsigned int hash_key(const void *ptr) +static size_t hash_key(const void *ptr) { const char *str = ((const object_key_t *)ptr)->key; - unsigned int hash = 5381; - unsigned int c; + size_t hash = 5381; + size_t c; - while((c = (unsigned int)*str)) + while((c = (size_t)*str)) { hash = ((hash << 5) + hash) + c; str++; @@ -87,7 +87,7 @@ static void json_delete_object(json_object_t *object) free(object); } -unsigned int json_object_size(const json_t *json) +size_t json_object_size(const json_t *json) { json_object_t *object; @@ -371,7 +371,7 @@ json_t *json_array(void) static void json_delete_array(json_array_t *array) { - unsigned int i; + size_t i; for(i = 0; i < array->entries; i++) json_decref(array->table[i]); @@ -380,7 +380,7 @@ static void json_delete_array(json_array_t *array) free(array); } -unsigned int json_array_size(const json_t *json) +size_t json_array_size(const json_t *json) { if(!json_is_array(json)) return 0; @@ -388,7 +388,7 @@ unsigned int json_array_size(const json_t *json) return json_to_array(json)->entries; } -json_t *json_array_get(const json_t *json, unsigned int index) +json_t *json_array_get(const json_t *json, size_t index) { json_array_t *array; if(!json_is_array(json)) @@ -401,7 +401,7 @@ json_t *json_array_get(const json_t *json, unsigned int index) return array->table[index]; } -int json_array_set_new(json_t *json, unsigned int index, json_t *value) +int json_array_set_new(json_t *json, size_t index, json_t *value) { json_array_t *array; @@ -427,24 +427,24 @@ int json_array_set_new(json_t *json, unsigned int index, json_t *value) return 0; } -static void array_move(json_array_t *array, unsigned int dest, - unsigned int src, unsigned int count) +static void array_move(json_array_t *array, size_t dest, + size_t src, size_t count) { memmove(&array->table[dest], &array->table[src], count * sizeof(json_t *)); } -static void array_copy(json_t **dest, unsigned int dpos, - json_t **src, unsigned int spos, - unsigned int count) +static void array_copy(json_t **dest, size_t dpos, + json_t **src, size_t spos, + size_t count) { memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *)); } static json_t **json_array_grow(json_array_t *array, - unsigned int amount, + size_t amount, int copy) { - unsigned int new_size; + size_t new_size; json_t **old_table, **new_table; if(array->entries + amount <= array->size) @@ -494,7 +494,7 @@ int json_array_append_new(json_t *json, json_t *value) return 0; } -int json_array_insert_new(json_t *json, unsigned int index, json_t *value) +int json_array_insert_new(json_t *json, size_t index, json_t *value) { json_array_t *array; json_t **old_table; @@ -534,7 +534,7 @@ int json_array_insert_new(json_t *json, unsigned int index, json_t *value) return 0; } -int json_array_remove(json_t *json, unsigned int index) +int json_array_remove(json_t *json, size_t index) { json_array_t *array; @@ -556,7 +556,7 @@ int json_array_remove(json_t *json, unsigned int index) int json_array_clear(json_t *json) { json_array_t *array; - unsigned int i; + size_t i; if(!json_is_array(json)) return -1; @@ -572,7 +572,7 @@ int json_array_clear(json_t *json) int json_array_extend(json_t *json, json_t *other_json) { json_array_t *array, *other; - unsigned int i; + size_t i; if(!json_is_array(json) || !json_is_array(other_json)) return -1; @@ -593,7 +593,7 @@ int json_array_extend(json_t *json, json_t *other_json) static int json_array_equal(json_t *array1, json_t *array2) { - unsigned int i, size; + size_t i, size; size = json_array_size(array1); if(size != json_array_size(array2)) @@ -616,7 +616,7 @@ static int json_array_equal(json_t *array1, json_t *array2) static json_t *json_array_copy(json_t *array) { json_t *result; - unsigned int i; + size_t i; result = json_array(); if(!result) @@ -631,7 +631,7 @@ static json_t *json_array_copy(json_t *array) static json_t *json_array_deep_copy(json_t *array) { json_t *result; - unsigned int i; + size_t i; result = json_array(); if(!result) @@ -836,7 +836,7 @@ json_t *json_true(void) { static json_t the_true = { .type = JSON_TRUE, - .refcount = (unsigned int)-1 + .refcount = (size_t)-1 }; return &the_true; } @@ -846,7 +846,7 @@ json_t *json_false(void) { static json_t the_false = { .type = JSON_FALSE, - .refcount = (unsigned int)-1 + .refcount = (size_t)-1 }; return &the_false; } @@ -856,7 +856,7 @@ json_t *json_null(void) { static json_t the_null = { .type = JSON_NULL, - .refcount = (unsigned int)-1 + .refcount = (size_t)-1 }; return &the_null; } diff --git a/test/bin/json_process.c b/test/bin/json_process.c index 77407e5..a32f1d0 100644 --- a/test/bin/json_process.c +++ b/test/bin/json_process.c @@ -28,7 +28,7 @@ static int getenv_int(const char *name) int main(int argc, char *argv[]) { int indent = 0; - unsigned int flags = 0; + size_t flags = 0; json_t *json; json_error_t error; diff --git a/test/suites/api/test_copy.c b/test/suites/api/test_copy.c index 54c1d28..e4705f4 100644 --- a/test/suites/api/test_copy.c +++ b/test/suites/api/test_copy.c @@ -174,7 +174,7 @@ static void test_copy_array(void) const char *json_array_text = "[1, \"foo\", 3.141592, {\"foo\": \"bar\"}]"; json_t *array, *copy; - unsigned int i; + size_t i; array = json_loads(json_array_text, NULL); if(!array) @@ -203,7 +203,7 @@ static void test_deep_copy_array(void) const char *json_array_text = "[1, \"foo\", 3.141592, {\"foo\": \"bar\"}]"; json_t *array, *copy; - unsigned int i; + size_t i; array = json_loads(json_array_text, NULL); if(!array) diff --git a/test/suites/api/test_simple.c b/test/suites/api/test_simple.c index 1769c65..9e1dba3 100644 --- a/test/suites/api/test_simple.c +++ b/test/suites/api/test_simple.c @@ -152,33 +152,33 @@ int main() /* Test reference counting on singletons (true, false, null) */ value = json_true(); - if(value->refcount != (unsigned int)-1) + if(value->refcount != (size_t)-1) fail("refcounting true works incorrectly"); json_decref(value); - if(value->refcount != (unsigned int)-1) + if(value->refcount != (size_t)-1) fail("refcounting true works incorrectly"); json_incref(value); - if(value->refcount != (unsigned int)-1) + if(value->refcount != (size_t)-1) fail("refcounting true works incorrectly"); value = json_false(); - if(value->refcount != (unsigned int)-1) + if(value->refcount != (size_t)-1) fail("refcounting false works incorrectly"); json_decref(value); - if(value->refcount != (unsigned int)-1) + if(value->refcount != (size_t)-1) fail("refcounting false works incorrectly"); json_incref(value); - if(value->refcount != (unsigned int)-1) + if(value->refcount != (size_t)-1) fail("refcounting false works incorrectly"); value = json_null(); - if(value->refcount != (unsigned int)-1) + if(value->refcount != (size_t)-1) fail("refcounting null works incorrectly"); json_decref(value); - if(value->refcount != (unsigned int)-1) + if(value->refcount != (size_t)-1) fail("refcounting null works incorrectly"); json_incref(value); - if(value->refcount != (unsigned int)-1) + if(value->refcount != (size_t)-1) fail("refcounting null works incorrectly"); return 0; -- 2.1.4