Browse Source

Merge commit '2be921d88376e78f84d79aafa6db2714da804e59'

tags/json-c-0.13-20171207
Eric Haszlakiewicz 9 years ago
parent
commit
980cdd61be
4 changed files with 73 additions and 19 deletions
  1. +8
    -3
      json_object.c
  2. +4
    -1
      json_object.h
  3. +28
    -11
      linkhash.c
  4. +33
    -4
      linkhash.h

+ 8
- 3
json_object.c View File

@@ -444,7 +444,7 @@ void json_object_object_add_ex(struct json_object* jso,
existing_entry->v = val; existing_entry->v = val;
} }


void json_object_object_add(struct json_object* jso, const char *key,
int json_object_object_add(struct json_object* jso, const char *key,
struct json_object *val) struct json_object *val)
{ {
// We lookup the entry and replace the value, rather than just deleting // We lookup the entry and replace the value, rather than just deleting
@@ -455,13 +455,18 @@ void json_object_object_add(struct json_object* jso, const char *key,
existing_entry = lh_table_lookup_entry_w_hash(jso->o.c_object, (void*)key, hash); existing_entry = lh_table_lookup_entry_w_hash(jso->o.c_object, (void*)key, hash);
if (!existing_entry) if (!existing_entry)
{ {
lh_table_insert_w_hash(jso->o.c_object, strdup(key), val, hash, 0);
return;
char *keydup = strdup(key);
if (keydup == NULL)
return -1;

return lh_table_insert_w_hash(jso->o.c_object, keydup, val, hash, 0);
} }
existing_value = (json_object *)existing_entry->v; existing_value = (json_object *)existing_entry->v;
if (existing_value) if (existing_value)
json_object_put(existing_value); json_object_put(existing_value);
existing_entry->v = val; existing_entry->v = val;

return 0;
} }






+ 4
- 1
json_object.h View File

@@ -314,8 +314,11 @@ extern int json_object_object_length(struct json_object* obj);
* @param obj the json_object instance * @param obj the json_object instance
* @param key the object field name (a private copy will be duplicated) * @param key the object field name (a private copy will be duplicated)
* @param val a json_object or NULL member to associate with the given field * @param val a json_object or NULL member to associate with the given field
*
* @return On success, <code>0</code> is returned.
* On error, a negative value is returned.
*/ */
extern void json_object_object_add(struct json_object* obj, const char *key,
extern int json_object_object_add(struct json_object* obj, const char *key,
struct json_object *val); struct json_object *val);


/** Add an object field to a json_object of type json_type_object /** Add an object field to a json_object of type json_type_object


+ 28
- 11
linkhash.c View File

@@ -474,11 +474,17 @@ struct lh_table* lh_table_new(int size,
struct lh_table *t; struct lh_table *t;


t = (struct lh_table*)calloc(1, sizeof(struct lh_table)); t = (struct lh_table*)calloc(1, sizeof(struct lh_table));
if(!t) lh_abort("lh_table_new: calloc failed\n");
if (!t)
return NULL;

t->count = 0; t->count = 0;
t->size = size; t->size = size;
t->table = (struct lh_entry*)calloc(size, sizeof(struct lh_entry)); t->table = (struct lh_entry*)calloc(size, sizeof(struct lh_entry));
if(!t->table) lh_abort("lh_table_new: calloc failed\n");
if (!t->table)
{
free(t);
return NULL;
}
t->free_fn = free_fn; t->free_fn = free_fn;
t->hash_fn = hash_fn; t->hash_fn = hash_fn;
t->equal_fn = equal_fn; t->equal_fn = equal_fn;
@@ -498,18 +504,25 @@ struct lh_table* lh_kptr_table_new(int size,
return lh_table_new(size, free_fn, lh_ptr_hash, lh_ptr_equal); return lh_table_new(size, free_fn, lh_ptr_hash, lh_ptr_equal);
} }


void lh_table_resize(struct lh_table *t, int new_size)
int lh_table_resize(struct lh_table *t, int new_size)
{ {
struct lh_table *new_t; struct lh_table *new_t;
struct lh_entry *ent;


new_t = lh_table_new(new_size, NULL, t->hash_fn, t->equal_fn); new_t = lh_table_new(new_size, NULL, t->hash_fn, t->equal_fn);
ent = t->head;
while(ent) {
lh_table_insert_w_hash(new_t, ent->k, ent->v,
lh_get_hash(new_t, ent->k),
(ent->k_is_constant) ? JSON_C_OBJECT_KEY_IS_CONSTANT : 0 );
ent = ent->next;
if (new_t == NULL)
return -1;

for (struct lh_entry *ent = t->head; ent != NULL; ent = ent->next)
{
unsigned long h = lh_get_hash(new_t, ent->k);
unsigned int opts = 0;
if (ent->k_is_constant)
opts = JSON_C_OBJECT_KEY_IS_CONSTANT;
if (lh_table_insert_w_hash(new_t, ent->k, ent->v, h, opts) != 0)
{
lh_table_free(new_t);
return -1;
}
} }
free(t->table); free(t->table);
t->table = new_t->table; t->table = new_t->table;
@@ -517,6 +530,8 @@ void lh_table_resize(struct lh_table *t, int new_size)
t->head = new_t->head; t->head = new_t->head;
t->tail = new_t->tail; t->tail = new_t->tail;
free(new_t); free(new_t);

return 0;
} }


void lh_table_free(struct lh_table *t) void lh_table_free(struct lh_table *t)
@@ -536,7 +551,9 @@ int lh_table_insert_w_hash(struct lh_table *t, void *k, const void *v, const uns
{ {
unsigned long n; unsigned long n;


if(t->count >= t->size * LH_LOAD_FACTOR) lh_table_resize(t, t->size * 2);
if (t->count >= t->size * LH_LOAD_FACTOR)
if (lh_table_resize(t, t->size * 2) != 0)
return -1;


n = h % t->size; n = h % t->size;




+ 33
- 4
linkhash.h View File

@@ -159,7 +159,8 @@ for(entry = table->head; entry && ((tmp = entry->next) || 1); entry = tmp)
* @param equal_fn comparison function to compare keys. 2 standard ones defined: * @param equal_fn comparison function to compare keys. 2 standard ones defined:
* lh_ptr_hash and lh_char_hash for comparing pointer values * lh_ptr_hash and lh_char_hash for comparing pointer values
* and C strings respectively. * and C strings respectively.
* @return a pointer onto the linkhash table.
* @return On success, a pointer to the new linkhash table is returned.
* On error, a null pointer is returned.
*/ */
extern struct lh_table* lh_table_new(int size, extern struct lh_table* lh_table_new(int size,
lh_entry_free_fn *free_fn, lh_entry_free_fn *free_fn,
@@ -172,7 +173,8 @@ extern struct lh_table* lh_table_new(int size,
* @param size initial table size. * @param size initial table size.
* @param name table name. * @param name table name.
* @param free_fn callback function used to free memory for entries. * @param free_fn callback function used to free memory for entries.
* @return a pointer onto the linkhash table.
* @return On success, a pointer to the new linkhash table is returned.
* On error, a null pointer is returned.
*/ */
extern struct lh_table* lh_kchar_table_new(int size, extern struct lh_table* lh_kchar_table_new(int size,
lh_entry_free_fn *free_fn); lh_entry_free_fn *free_fn);
@@ -184,7 +186,8 @@ extern struct lh_table* lh_kchar_table_new(int size,
* @param size initial table size. * @param size initial table size.
* @param name table name. * @param name table name.
* @param free_fn callback function used to free memory for entries. * @param free_fn callback function used to free memory for entries.
* @return a pointer onto the linkhash table.
* @return On success, a pointer to the new linkhash table is returned.
* On error, a null pointer is returned.
*/ */
extern struct lh_table* lh_kptr_table_new(int size, extern struct lh_table* lh_kptr_table_new(int size,
lh_entry_free_fn *free_fn); lh_entry_free_fn *free_fn);
@@ -204,6 +207,9 @@ extern void lh_table_free(struct lh_table *t);
* @param t the table to insert into. * @param t the table to insert into.
* @param k a pointer to the key to insert. * @param k a pointer to the key to insert.
* @param v a pointer to the value to insert. * @param v a pointer to the value to insert.
*
* @return On success, <code>0</code> is returned.
* On error, a negative value is returned.
*/ */
extern int lh_table_insert(struct lh_table *t, void *k, const void *v); extern int lh_table_insert(struct lh_table *t, void *k, const void *v);


@@ -287,8 +293,31 @@ extern int lh_table_delete(struct lh_table *t, const void *k);


extern int lh_table_length(struct lh_table *t); extern int lh_table_length(struct lh_table *t);


/**
* Prints a message to <code>stdout</code>,
* then exits the program with an exit code of <code>1</code>.
*
* @param msg Message format string, like for <code>printf</code>.
* @param ... Format args.
*
* @deprecated Since it is not a good idea to exit the entire program
* because of an internal library failure, json-c will no longer
* use this function internally.
* However, because its interface is public, it will remain part of
* the API on the off chance of legacy software using it externally.
*/
void lh_abort(const char *msg, ...); void lh_abort(const char *msg, ...);
void lh_table_resize(struct lh_table *t, int new_size);

/**
* Resizes the specified table.
*
* @param t Pointer to table to resize.
* @param new_size New table size. Must be positive.
*
* @return On success, <code>0</code> is returned.
* On error, a negative value is returned.
*/
int lh_table_resize(struct lh_table *t, int new_size);




/** /**


Loading…
Cancel
Save