]> granicus.if.org Git - json-c/commitdiff
json_object_private: remove _delete field
authorRamiro Polla <ramiro.polla@gmail.com>
Sat, 1 Dec 2018 17:57:22 +0000 (18:57 +0100)
committerRamiro Polla <ramiro.polla@gmail.com>
Thu, 20 Dec 2018 21:26:06 +0000 (22:26 +0100)
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.

json_object.c
json_object_private.h

index 344af515ecb664dbd19b24f9e21476170dbc7cb1..1d9a53c1f3d13c0dc1a68e99c93f4cfead30dc9e 100644 (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)
index 4c6681ae2b5105fbd8d85b444b22d1c3e374d039..d964b16272f4f9acbaec436ff1bececedbd2d0e2 100644 (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 {