1.make it can been compiled with Visual Studio 2010

2.replace json_object_get/put API with json_object_retain/release, as they operate the reference counter, and confused with array_list_get/put_idx.
3.replace array_list_get/put_idx API with array_list_get/insert to make them more clear to use.
This commit is contained in:
Haffon
2017-08-22 13:53:47 +08:00
parent af87944585
commit 3141c3976b
10 changed files with 41 additions and 32 deletions

View File

@@ -52,6 +52,8 @@ set(JSON_C_HEADERS
./linkhash.h ./linkhash.h
./math_compat.h ./math_compat.h
./strdup_compat.h ./strdup_compat.h
./strerror_override.h
./strerror_override_private
./vasprintf_compat.h ./vasprintf_compat.h
./printbuf.h ./printbuf.h
./random_seed.h ./random_seed.h
@@ -66,6 +68,7 @@ set(JSON_C_SOURCES
./json_util.c ./json_util.c
./linkhash.c ./linkhash.c
./printbuf.c ./printbuf.c
./strerror_override.c
./random_seed.c ./random_seed.c
) )

View File

@@ -62,7 +62,7 @@ array_list_free(struct array_list *arr)
} }
void* void*
array_list_get_idx(struct array_list *arr, size_t i) array_list_get(struct array_list *arr, size_t i)
{ {
if(i >= arr->length) return NULL; if(i >= arr->length) return NULL;
return arr->array[i]; return arr->array[i];
@@ -92,7 +92,7 @@ static int array_list_expand_internal(struct array_list *arr, size_t max)
} }
int int
array_list_put_idx(struct array_list *arr, size_t idx, void *data) array_list_insert(struct array_list *arr, size_t idx, void *data)
{ {
if (idx > SIZE_T_MAX - 1 ) return -1; if (idx > SIZE_T_MAX - 1 ) return -1;
if(array_list_expand_internal(arr, idx+1)) return -1; if(array_list_expand_internal(arr, idx+1)) return -1;
@@ -106,7 +106,7 @@ array_list_put_idx(struct array_list *arr, size_t idx, void *data)
int int
array_list_add(struct array_list *arr, void *data) array_list_add(struct array_list *arr, void *data)
{ {
return array_list_put_idx(arr, arr->length, data); return array_list_insert(arr, arr->length, data);
} }
void void

View File

@@ -35,10 +35,10 @@ extern void
array_list_free(struct array_list *al); array_list_free(struct array_list *al);
extern void* extern void*
array_list_get_idx(struct array_list *al, size_t i); array_list_get(struct array_list *al, size_t i);
extern int extern int
array_list_put_idx(struct array_list *al, size_t i, void *data); array_list_insert(struct array_list *al, size_t i, void *data);
extern int extern int
array_list_add(struct array_list *al, void *data); array_list_add(struct array_list *al, void *data);

View File

@@ -167,14 +167,14 @@ static int json_escape_str(struct printbuf *pb, const char *str, int len, int fl
/* reference counting */ /* reference counting */
extern struct json_object* json_object_get(struct json_object *jso) extern struct json_object* json_object_retain(struct json_object *jso)
{ {
if (jso) if (jso)
jso->_ref_count++; jso->_ref_count++;
return jso; return jso;
} }
int json_object_put(struct json_object *jso) int json_object_release(struct json_object *jso)
{ {
if(jso) if(jso)
{ {
@@ -408,7 +408,7 @@ static void json_object_lh_entry_free(struct lh_entry *ent)
{ {
if (!ent->k_is_constant) if (!ent->k_is_constant)
free(lh_entry_k(ent)); free(lh_entry_k(ent));
json_object_put((struct json_object*)lh_entry_v(ent)); json_object_release((struct json_object*)lh_entry_v(ent));
} }
static void json_object_object_delete(struct json_object* jso) static void json_object_object_delete(struct json_object* jso)
@@ -453,13 +453,15 @@ int json_object_object_add_ex(struct json_object* jso,
struct json_object *const val, struct json_object *const val,
const unsigned opts) const unsigned opts)
{ {
struct json_object *existing_value = NULL;
struct lh_entry *existing_entry;
unsigned long hash;
assert(json_object_get_type(jso) == json_type_object); assert(json_object_get_type(jso) == json_type_object);
// We lookup the entry and replace the value, rather than just deleting // We lookup the entry and replace the value, rather than just deleting
// and re-adding it, so the existing key remains valid. // and re-adding it, so the existing key remains valid.
json_object *existing_value = NULL; hash = lh_get_hash(jso->o.c_object, (const void *)key);
struct lh_entry *existing_entry;
const unsigned long hash = lh_get_hash(jso->o.c_object, (const void *)key);
existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL : existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL :
lh_table_lookup_entry_w_hash(jso->o.c_object, lh_table_lookup_entry_w_hash(jso->o.c_object,
(const void *)key, hash); (const void *)key, hash);
@@ -479,7 +481,7 @@ int json_object_object_add_ex(struct json_object* jso,
} }
existing_value = (json_object *) lh_entry_v(existing_entry); existing_value = (json_object *) lh_entry_v(existing_entry);
if (existing_value) if (existing_value)
json_object_put(existing_value); json_object_release(existing_value);
existing_entry->v = val; existing_entry->v = val;
return 0; return 0;
} }
@@ -841,11 +843,12 @@ struct json_object* json_object_new_double(double d)
struct json_object* json_object_new_double_s(double d, const char *ds) struct json_object* json_object_new_double_s(double d, const char *ds)
{ {
char *new_ds;
struct json_object *jso = json_object_new_double(d); struct json_object *jso = json_object_new_double(d);
if (!jso) if (!jso)
return NULL; return NULL;
char *new_ds = strdup(ds); new_ds = strdup(ds);
if (!new_ds) if (!new_ds)
{ {
json_object_generic_delete(jso); json_object_generic_delete(jso);
@@ -1025,8 +1028,8 @@ int json_object_set_string(json_object* jso, const char* s) {
} }
int json_object_set_string_len(json_object* jso, const char* s, int len){ int json_object_set_string_len(json_object* jso, const char* s, int len){
if (jso==NULL || jso->o_type!=json_type_string) return 0;
char *dstbuf; char *dstbuf;
if (jso==NULL || jso->o_type!=json_type_string) return 0;
if (len<LEN_DIRECT_STRING_DATA) { if (len<LEN_DIRECT_STRING_DATA) {
dstbuf=jso->o.c_string.str.data; dstbuf=jso->o.c_string.str.data;
if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr); if (jso->o.c_string.len>=LEN_DIRECT_STRING_DATA) free(jso->o.c_string.str.ptr);
@@ -1089,7 +1092,7 @@ static int json_object_array_to_json_string(struct json_object* jso,
static void json_object_array_entry_free(void *data) static void json_object_array_entry_free(void *data)
{ {
json_object_put((struct json_object*)data); json_object_release((struct json_object*)data);
} }
static void json_object_array_delete(struct json_object* jso) static void json_object_array_delete(struct json_object* jso)
@@ -1166,7 +1169,7 @@ int json_object_array_put_idx(struct json_object *jso, size_t idx,
struct json_object *val) struct json_object *val)
{ {
assert(json_object_get_type(jso) == json_type_array); assert(json_object_get_type(jso) == json_type_array);
return array_list_put_idx(jso->o.c_array, idx, val); return array_list_insert(jso->o.c_array, idx, val);
} }
int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count) int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
@@ -1179,7 +1182,7 @@ struct json_object* json_object_array_get_idx(const struct json_object *jso,
size_t idx) size_t idx)
{ {
assert(json_object_get_type(jso) == json_type_array); assert(json_object_get_type(jso) == json_type_array);
return (struct json_object*)array_list_get_idx(jso->o.c_array, idx); return (struct json_object*)array_list_get(jso->o.c_array, idx);
} }
static int json_array_equal(struct json_object* jso1, static int json_array_equal(struct json_object* jso1,

View File

@@ -182,7 +182,7 @@ typedef enum json_type {
* *
* @param obj the json_object instance * @param obj the json_object instance
*/ */
JSON_EXPORT struct json_object* json_object_get(struct json_object *obj); JSON_EXPORT struct json_object* json_object_retain(struct json_object *obj);
/** /**
* Decrement the reference count of json_object and free if it reaches zero. * Decrement the reference count of json_object and free if it reaches zero.
@@ -192,7 +192,7 @@ JSON_EXPORT struct json_object* json_object_get(struct json_object *obj);
* @param obj the json_object instance * @param obj the json_object instance
* @returns 1 if the object was freed. * @returns 1 if the object was freed.
*/ */
JSON_EXPORT int json_object_put(struct json_object *obj); JSON_EXPORT int json_object_release(struct json_object *obj);
/** /**
* Check if the json_object is of a given type * Check if the json_object is of a given type

View File

@@ -240,7 +240,7 @@ int json_pointer_set(struct json_object **obj, const char *path, struct json_obj
} }
if (path[0] == '\0') { if (path[0] == '\0') {
json_object_put(*obj); json_object_release(*obj);
*obj = value; *obj = value;
return 0; return 0;
} }
@@ -294,7 +294,7 @@ int json_pointer_setf(struct json_object **obj, struct json_object *value, const
return rc; return rc;
if (path_copy[0] == '\0') { if (path_copy[0] == '\0') {
json_object_put(*obj); json_object_release(*obj);
*obj = value; *obj = value;
goto out; goto out;
} }

View File

@@ -139,7 +139,7 @@ static void json_tokener_reset_level(struct json_tokener *tok, int depth)
{ {
tok->stack[depth].state = json_tokener_state_eatws; tok->stack[depth].state = json_tokener_state_eatws;
tok->stack[depth].saved_state = json_tokener_state_start; tok->stack[depth].saved_state = json_tokener_state_start;
json_object_put(tok->stack[depth].current); json_object_release(tok->stack[depth].current);
tok->stack[depth].current = NULL; tok->stack[depth].current = NULL;
free(tok->stack[depth].obj_field_name); free(tok->stack[depth].obj_field_name);
tok->stack[depth].obj_field_name = NULL; tok->stack[depth].obj_field_name = NULL;
@@ -178,7 +178,7 @@ struct json_object* json_tokener_parse_verbose(const char *str,
*error = tok->err; *error = tok->err;
if(tok->err != json_tokener_success) { if(tok->err != json_tokener_success) {
if (obj != NULL) if (obj != NULL)
json_object_put(obj); json_object_release(obj);
obj = NULL; obj = NULL;
} }
@@ -378,7 +378,7 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
case json_tokener_state_finish: case json_tokener_state_finish:
if(tok->depth == 0) goto out; if(tok->depth == 0) goto out;
obj = json_object_get(current); obj = json_object_retain(current);
json_tokener_reset_level(tok, tok->depth); json_tokener_reset_level(tok, tok->depth);
tok->depth--; tok->depth--;
goto redo_char; goto redo_char;
@@ -387,10 +387,11 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
{ {
size_t size_inf; size_t size_inf;
int is_negative = 0; int is_negative = 0;
char *infbuf;
printbuf_memappend_fast(tok->pb, &c, 1); printbuf_memappend_fast(tok->pb, &c, 1);
size_inf = json_min(tok->st_pos+1, json_inf_str_len); size_inf = json_min(tok->st_pos+1, json_inf_str_len);
char *infbuf = tok->pb->buf; infbuf = tok->pb->buf;
if (*infbuf == '-') if (*infbuf == '-')
{ {
infbuf++; infbuf++;
@@ -958,7 +959,7 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
if (tok->err == json_tokener_success) if (tok->err == json_tokener_success)
{ {
json_object *ret = json_object_get(current); json_object *ret = json_object_retain(current);
int ii; int ii;
/* Partially reset, so we parse additional objects on subsequent calls. */ /* Partially reset, so we parse additional objects on subsequent calls. */

View File

@@ -560,6 +560,11 @@ int lh_table_resize(struct lh_table *t, int new_size)
return 0; return 0;
} }
unsigned long lh_get_hash(const struct lh_table *t, const void *k)
{
return t->hash_fn(k);
}
void lh_table_free(struct lh_table *t) void lh_table_free(struct lh_table *t)
{ {
struct lh_entry *c; struct lh_entry *c;

View File

@@ -332,10 +332,7 @@ int lh_table_resize(struct lh_table *t, int new_size);
* @param k a pointer to the key to lookup * @param k a pointer to the key to lookup
* @return the key's hash * @return the key's hash
*/ */
static inline unsigned long lh_get_hash(const struct lh_table *t, const void *k) unsigned long lh_get_hash(const struct lh_table *t, const void *k);
{
return t->hash_fn(k);
}
/* Don't use this outside of linkhash.h: */ /* Don't use this outside of linkhash.h: */
#ifdef __UNCONST #ifdef __UNCONST

View File

@@ -186,11 +186,11 @@ static int get_dev_random_seed()
static int get_cryptgenrandom_seed() static int get_cryptgenrandom_seed()
{ {
DEBUG_SEED("get_cryptgenrandom_seed");
HCRYPTPROV hProvider = 0; HCRYPTPROV hProvider = 0;
int r; int r;
DEBUG_SEED("get_cryptgenrandom_seed");
if (!CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) { if (!CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
fprintf(stderr, "error CryptAcquireContextW"); fprintf(stderr, "error CryptAcquireContextW");
exit(1); exit(1);