Browse Source

json_object_private: remove _delete field

This field is set based on o_type when the object is created and it is
not changed during the lifetime of the object. Therefore we can check
o_type to choose the proper delete function in json_object_put(), and
save sizeof(void *) bytes in struct json_object_private.
tags/json-c-0.16-20220414
Ramiro Polla 6 years ago
parent
commit
1f46d2f40f
2 changed files with 16 additions and 10 deletions
  1. +16
    -7
      json_object.c
  2. +0
    -3
      json_object_private.h

+ 16
- 7
json_object.c View File

@@ -44,7 +44,6 @@
const char *json_number_chars = "0123456789.+-eE";
const char *json_hex_chars = "0123456789abcdefABCDEF";

static void json_object_generic_delete(struct json_object* jso);
static struct json_object* json_object_new(enum json_type o_type);

static json_object_to_json_string_fn json_object_object_to_json_string;
@@ -54,6 +53,12 @@ static json_object_to_json_string_fn json_object_int_to_json_string;
static json_object_to_json_string_fn json_object_string_to_json_string;
static json_object_to_json_string_fn json_object_array_to_json_string;

typedef void (json_object_private_delete_fn)(struct json_object *o);

static json_object_private_delete_fn json_object_string_delete;
static json_object_private_delete_fn json_object_array_delete;
static json_object_private_delete_fn json_object_object_delete;
static json_object_private_delete_fn json_object_generic_delete;

/* ref count debugging */

@@ -205,7 +210,16 @@ int json_object_put(struct json_object *jso)

if (jso->_user_delete)
jso->_user_delete(jso, jso->_userdata);
jso->_delete(jso);

if (jso->o_type == json_type_string)
json_object_string_delete(jso);
else if (jso->o_type == json_type_array)
json_object_array_delete(jso);
else if (jso->o_type == json_type_object)
json_object_object_delete(jso);
else
json_object_generic_delete(jso);

return 1;
}

@@ -232,7 +246,6 @@ static struct json_object* json_object_new(enum json_type o_type)
return NULL;
jso->o_type = o_type;
jso->_ref_count = 1;
jso->_delete = &json_object_generic_delete;
#ifdef REFCOUNT_DEBUG
lh_table_insert(json_object_table, jso, jso);
MC_DEBUG("json_object_new_%s: %p\n", json_type_to_name(jso->o_type), jso);
@@ -441,7 +454,6 @@ struct json_object* json_object_new_object(void)
struct json_object *jso = json_object_new(json_type_object);
if (!jso)
return NULL;
jso->_delete = &json_object_object_delete;
jso->_to_json_string = &json_object_object_to_json_string;
jso->o.c_object = lh_kchar_table_new(JSON_OBJECT_DEF_HASH_ENTRIES,
&json_object_lh_entry_free);
@@ -1020,7 +1032,6 @@ struct json_object* json_object_new_string(const char *s)
struct json_object *jso = json_object_new(json_type_string);
if (!jso)
return NULL;
jso->_delete = &json_object_string_delete;
jso->_to_json_string = &json_object_string_to_json_string;
jso->o.c_string.len = strlen(s);
if(jso->o.c_string.len < LEN_DIRECT_STRING_DATA) {
@@ -1043,7 +1054,6 @@ struct json_object* json_object_new_string_len(const char *s, const int len)
struct json_object *jso = json_object_new(json_type_string);
if (!jso)
return NULL;
jso->_delete = &json_object_string_delete;
jso->_to_json_string = &json_object_string_to_json_string;
if(len < LEN_DIRECT_STRING_DATA) {
dstbuf = jso->o.c_string.str.data;
@@ -1172,7 +1182,6 @@ struct json_object* json_object_new_array(void)
struct json_object *jso = json_object_new(json_type_array);
if (!jso)
return NULL;
jso->_delete = &json_object_array_delete;
jso->_to_json_string = &json_object_array_to_json_string;
jso->o.c_array = array_list_new(&json_object_array_entry_free);
if(jso->o.c_array == NULL)


+ 0
- 3
json_object_private.h View File

@@ -22,13 +22,10 @@ extern "C" {

#define LEN_DIRECT_STRING_DATA 32 /**< how many bytes are directly stored in json_object for strings? */

typedef void (json_object_private_delete_fn)(struct json_object *o);

struct json_object
{
enum json_type o_type;
uint32_t _ref_count;
json_object_private_delete_fn *_delete;
json_object_to_json_string_fn *_to_json_string;
struct printbuf *_pb;
union data {


Loading…
Cancel
Save