This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/celix.git


The following commit(s) were added to refs/heads/develop by this push:
     new e5a7dd8  Performance opts and memory leak fixes (#176)
e5a7dd8 is described below

commit e5a7dd8f45ad6903f401c4b362f2d270b3674d2f
Author: Michael de Lang <kingo...@gmail.com>
AuthorDate: Fri Mar 27 07:49:12 2020 +0000

    Performance opts and memory leak fixes (#176)
    
    * Use jansson nocheck where possible
    
    * Fix some memory leaks
---
 bundles/http_admin/http_admin/src/http_admin.c     | 19 +++++++++------
 .../src/pubsub_websocket_topic_sender.c            | 10 ++++----
 bundles/pubsub/test/test/test_runner.cc            | 18 +++++++-------
 libs/dfi/src/avrobin_serializer.c                  | 28 +++++++++++-----------
 libs/dfi/src/json_rpc.c                            |  8 +++----
 .../private/test/service_registry_test.cpp         |  1 -
 libs/framework/src/service_tracker.c               |  1 +
 7 files changed, 45 insertions(+), 40 deletions(-)

diff --git a/bundles/http_admin/http_admin/src/http_admin.c 
b/bundles/http_admin/http_admin/src/http_admin.c
index f9e5036..ca7c93d 100755
--- a/bundles/http_admin/http_admin/src/http_admin.c
+++ b/bundles/http_admin/http_admin/src/http_admin.c
@@ -102,6 +102,7 @@ http_admin_manager_t 
*httpAdmin_create(celix_bundle_context_t *context, char *ro
         }
         celixThreadMutex_destroy(&admin->admin_lock);
 
+        celix_arrayList_destroy(admin->aliasList);
         free(admin);
         admin = NULL;
     }
@@ -120,9 +121,9 @@ void httpAdmin_destroy(http_admin_manager_t *admin) {
     destroyServiceTree(&admin->http_svc_tree);
 
     //Destroy alias map by removing symbolic links first.
-    unsigned int size = arrayList_size(admin->aliasList);
+    unsigned int size = celix_arrayList_size(admin->aliasList);
     for (unsigned int i = (size - 1); i < size; i--) {
-        http_alias_t *alias = arrayList_get(admin->aliasList, i);
+        http_alias_t *alias = celix_arrayList_get(admin->aliasList, i);
 
         //Delete alias in cache directory
         if (remove(alias->alias_path) < 0)
@@ -133,7 +134,7 @@ void httpAdmin_destroy(http_admin_manager_t *admin) {
         free(alias);
         celix_arrayList_removeAt(admin->aliasList, i);
     }
-    arrayList_destroy(admin->aliasList);
+    celix_arrayList_destroy(admin->aliasList);
 
     celixThreadMutex_unlock(&(admin->admin_lock));
     celixThreadMutex_destroy(&(admin->admin_lock));
@@ -222,7 +223,8 @@ int http_request_handle(struct mg_connection *connection) {
 
                         if (ri->content_length > 0) {
                             int content_size = (ri->content_length > INT_MAX ? 
INT_MAX : (int) ri->content_length);
-                            rcv_buf = malloc((size_t) content_size);
+                            rcv_buf = malloc((size_t) content_size + 1);
+                            rcv_buf[content_size] = '\0';
                             bytes_read = mg_read(connection, rcv_buf, (size_t) 
content_size);
                         } else {
                             no_data = true;
@@ -250,7 +252,8 @@ int http_request_handle(struct mg_connection *connection) {
 
                         if (ri->content_length > 0) {
                             int content_size = (ri->content_length > INT_MAX ? 
INT_MAX : (int) ri->content_length);
-                            rcv_buf = malloc((size_t) content_size);
+                            rcv_buf = malloc((size_t) content_size + 1);
+                            rcv_buf[content_size] = '\0';
                             bytes_read = mg_read(connection, rcv_buf, (size_t) 
content_size);
                         } else {
                             no_data = true;
@@ -284,7 +287,8 @@ int http_request_handle(struct mg_connection *connection) {
 
                         if (ri->content_length > 0) {
                             int content_size = (ri->content_length > INT_MAX ? 
INT_MAX : (int) ri->content_length);
-                            rcv_buf = malloc((size_t) content_size);
+                            rcv_buf = malloc((size_t) content_size + 1);
+                            rcv_buf[content_size] = '\0';
                             bytes_read = mg_read(connection, rcv_buf, (size_t) 
content_size);
                         } else {
                             no_data = true;
@@ -317,7 +321,8 @@ int http_request_handle(struct mg_connection *connection) {
 
                         if (ri->content_length > 0) {
                             int content_size = (ri->content_length > INT_MAX ? 
INT_MAX : (int) ri->content_length);
-                            rcv_buf = malloc((size_t) content_size);
+                            rcv_buf = malloc((size_t) content_size + 1);
+                            rcv_buf[content_size] = '\0';
                             bytes_read = mg_read(connection, rcv_buf, (size_t) 
content_size);
                         } else {
                             no_data = true;
diff --git 
a/bundles/pubsub/pubsub_admin_websocket/src/pubsub_websocket_topic_sender.c 
b/bundles/pubsub/pubsub_admin_websocket/src/pubsub_websocket_topic_sender.c
index af10a1b..5a74457 100644
--- a/bundles/pubsub/pubsub_admin_websocket/src/pubsub_websocket_topic_sender.c
+++ b/bundles/pubsub/pubsub_admin_websocket/src/pubsub_websocket_topic_sender.c
@@ -315,15 +315,15 @@ static int psa_websocket_topicPublicationSend(void* 
handle, unsigned int msgType
             celixThreadMutex_lock(&entry->sendLock);
 
             json_t *jsMsg = json_object();
-            json_object_set_new(jsMsg, "id", json_string(entry->header.id));
-            json_object_set_new(jsMsg, "major", 
json_integer(entry->header.major));
-            json_object_set_new(jsMsg, "minor", 
json_integer(entry->header.minor));
-            json_object_set_new(jsMsg, "seqNr", 
json_integer(entry->header.seqNr++));
+            json_object_set_new_nocheck(jsMsg, "id", 
json_string(entry->header.id));
+            json_object_set_new_nocheck(jsMsg, "major", 
json_integer(entry->header.major));
+            json_object_set_new_nocheck(jsMsg, "minor", 
json_integer(entry->header.minor));
+            json_object_set_new_nocheck(jsMsg, "seqNr", 
json_integer(entry->header.seqNr++));
 
             json_t *jsData;
             jsData = json_loadb((const char *)serializedOutput, 
serializedOutputLen - 1, 0, &jsError);
             if(jsData != NULL) {
-                json_object_set_new(jsMsg, "data", jsData);
+                json_object_set_new_nocheck(jsMsg, "data", jsData);
                 const char *msg = json_dumps(jsMsg, 0);
                 size_t bytes_to_write = strlen(msg);
                 int bytes_written = 
mg_websocket_client_write(sender->sockConnection, MG_WEBSOCKET_OPCODE_TEXT, msg,
diff --git a/bundles/pubsub/test/test/test_runner.cc 
b/bundles/pubsub/test/test/test_runner.cc
index cb1bb1a..855de62 100644
--- a/bundles/pubsub/test/test/test_runner.cc
+++ b/bundles/pubsub/test/test/test_runner.cc
@@ -33,19 +33,19 @@ int main(int argc, char **argv) {
 TEST_GROUP(PUBSUB_INT_GROUP) {
     celix_framework_t *fw = NULL;
     celix_bundle_context_t *ctx = NULL;
-    void setup() {
+    void setup() override {
         celixLauncher_launch("config.properties", &fw);
         ctx = celix_framework_getFrameworkContext(fw);
     }
 
-        void teardown() {
-            celixLauncher_stop(fw);
-            celixLauncher_waitForShutdown(fw);
-            celixLauncher_destroy(fw);
-            ctx = NULL;
-            fw = NULL;
-        }
-        };
+    void teardown() override {
+        celixLauncher_stop(fw);
+        celixLauncher_waitForShutdown(fw);
+        celixLauncher_destroy(fw);
+        ctx = NULL;
+        fw = NULL;
+    }
+};
 
 TEST(PUBSUB_INT_GROUP, recvTest) {
     constexpr int TRIES = 25;
diff --git a/libs/dfi/src/avrobin_serializer.c 
b/libs/dfi/src/avrobin_serializer.c
index a7ed37c..d06456f 100644
--- a/libs/dfi/src/avrobin_serializer.c
+++ b/libs/dfi/src/avrobin_serializer.c
@@ -837,7 +837,7 @@ static int avrobinSerializer_generateComplex(dyn_type 
*type, json_t **output) {
         return ERROR;
     }
 
-    json_t *record_string = json_string("record");
+    json_t *record_string = json_string_nocheck("record");
     if (record_string == NULL) {
         json_decref(record_object);
         return ERROR;
@@ -904,7 +904,7 @@ static int avrobinSerializer_generateComplex(dyn_type 
*type, json_t **output) {
             }
 
             if (status == OK) {
-                if (json_object_set_new(field_object, "name", field_name) == 
0) {
+                if (json_object_set_new_nocheck(field_object, "name", 
field_name) == 0) {
                     field_name = NULL;
                 } else {
                     status = ERROR;
@@ -912,7 +912,7 @@ static int avrobinSerializer_generateComplex(dyn_type 
*type, json_t **output) {
             }
 
             if (status == OK) {
-                if (json_object_set_new(field_object, "type", field_schema) == 
0) {
+                if (json_object_set_new_nocheck(field_object, "type", 
field_schema) == 0) {
                     field_schema = NULL;
                 } else {
                     status = ERROR;
@@ -949,7 +949,7 @@ static int avrobinSerializer_generateComplex(dyn_type 
*type, json_t **output) {
     }
 
     if (status == OK) {
-        if (json_object_set_new(record_object, "type", record_string) == 0) {
+        if (json_object_set_new_nocheck(record_object, "type", record_string) 
== 0) {
             record_string = NULL;
         } else {
             status = ERROR;
@@ -957,7 +957,7 @@ static int avrobinSerializer_generateComplex(dyn_type 
*type, json_t **output) {
     }
 
     if (status == OK) {
-        if (json_object_set_new(record_object, "name", name_string) == 0) {
+        if (json_object_set_new_nocheck(record_object, "name", name_string) == 
0) {
             name_string = NULL;
         } else {
             status = ERROR;
@@ -965,7 +965,7 @@ static int avrobinSerializer_generateComplex(dyn_type 
*type, json_t **output) {
     }
 
     if (status == OK) {
-        if (json_object_set_new(record_object, "fields", record_fields) == 0) {
+        if (json_object_set_new_nocheck(record_object, "fields", 
record_fields) == 0) {
             record_fields = NULL;
         } else {
             status = ERROR;
@@ -1005,7 +1005,7 @@ static int avrobinSerializer_generateSequence(dyn_type 
*type, json_t **output) {
         return ERROR;
     }
 
-    json_t *array_string = json_string("array");
+    json_t *array_string = json_string_nocheck("array");
     if (array_string == NULL) {
         json_decref(array_object);
         return ERROR;
@@ -1018,7 +1018,7 @@ static int avrobinSerializer_generateSequence(dyn_type 
*type, json_t **output) {
         return ERROR;
     }
 
-    if (json_object_set_new(array_object, "type", array_string) == 0) {
+    if (json_object_set_new_nocheck(array_object, "type", array_string) == 0) {
         array_string = NULL;
     } else {
         json_decref(array_object);
@@ -1027,7 +1027,7 @@ static int avrobinSerializer_generateSequence(dyn_type 
*type, json_t **output) {
         return ERROR;
     }
 
-    if (json_object_set_new(array_object, "items", item_schema) == 0) {
+    if (json_object_set_new_nocheck(array_object, "items", item_schema) == 0) {
         item_schema = NULL;
     } else {
         json_decref(array_object);
@@ -1046,7 +1046,7 @@ static int avrobinSerializer_generateEnum(dyn_type *type, 
json_t **output) {
         return ERROR;
     }
 
-    json_t *enum_string = json_string("enum");
+    json_t *enum_string = json_string_nocheck("enum");
     if (enum_string == NULL) {
         json_decref(enum_object);
         return ERROR;
@@ -1097,7 +1097,7 @@ static int avrobinSerializer_generateEnum(dyn_type *type, 
json_t **output) {
         }
     }
 
-    if (json_object_set_new(enum_object, "type", enum_string) != 0) {
+    if (json_object_set_new_nocheck(enum_object, "type", enum_string) != 0) {
         json_decref(enum_object);
         json_decref(enum_string);
         json_decref(name_string);
@@ -1105,14 +1105,14 @@ static int avrobinSerializer_generateEnum(dyn_type 
*type, json_t **output) {
         return ERROR;
     }
 
-    if  (json_object_set_new(enum_object, "name", name_string) != 0) {
+    if  (json_object_set_new_nocheck(enum_object, "name", name_string) != 0) {
         json_decref(enum_object);
         json_decref(name_string);
         json_decref(symbols_array);
         return ERROR;
     }
 
-    if (json_object_set_new(enum_object, "symbols", symbols_array) != 0) {
+    if (json_object_set_new_nocheck(enum_object, "symbols", symbols_array) != 
0) {
         json_decref(enum_object);
         json_decref(symbols_array);
         return ERROR;
@@ -1390,7 +1390,7 @@ static int avrobin_schema_primitive(const char *tname, 
json_t **output) {
         json_decref(jo);
         return ERROR;
     }
-    if (json_object_set_new(jo, "type", js) != 0) {
+    if (json_object_set_new_nocheck(jo, "type", js) != 0) {
         json_decref(jo);
         json_decref(js);
         return ERROR;
diff --git a/libs/dfi/src/json_rpc.c b/libs/dfi/src/json_rpc.c
index 7e62a7c..edd9aa7 100644
--- a/libs/dfi/src/json_rpc.c
+++ b/libs/dfi/src/json_rpc.c
@@ -234,11 +234,11 @@ int jsonRpc_call(dyn_interface_type *intf, void *service, 
const char *request, c
                                //ignore -> no result
                        } else {
                                LOG_DEBUG("Setting result payload");
-                               json_object_set_new(payload, "r", jsonResult);
+                json_object_set_new_nocheck(payload, "r", jsonResult);
                        }
                } else {
                        LOG_DEBUG("Setting error payload");
-                       json_object_set_new(payload, "e", 
json_integer(funcCallStatus));
+            json_object_set_new_nocheck(payload, "e", 
json_integer(funcCallStatus));
                }
                response = json_dumps(payload, JSON_DECODE_ANY);
                json_decref(payload);
@@ -260,10 +260,10 @@ int jsonRpc_prepareInvokeRequest(dyn_function_type *func, 
const char *id, void *
 
        LOG_DEBUG("Calling remote function '%s'\n", id);
        json_t *invoke = json_object();
-       json_object_set_new(invoke, "m", json_string(id));
+    json_object_set_new_nocheck(invoke, "m", json_string(id));
 
        json_t *arguments = json_array();
-       json_object_set_new(invoke, "a", arguments);
+    json_object_set_new_nocheck(invoke, "a", arguments);
 
        int i;
        int nrOfArgs = dynFunction_nrOfArguments(func);
diff --git a/libs/framework/private/test/service_registry_test.cpp 
b/libs/framework/private/test/service_registry_test.cpp
index 67c25f0..9634537 100644
--- a/libs/framework/private/test/service_registry_test.cpp
+++ b/libs/framework/private/test/service_registry_test.cpp
@@ -282,7 +282,6 @@ TEST(service_registry, registerServiceListenerHook) {
        //cleanup
        array_list_pt destroy_this = (array_list_pt) 
hashMap_remove(registry->serviceRegistrations, bundle);
        arrayList_destroy(destroy_this);
-       arrayList_remove(registry->listenerHooks, 0);
        serviceRegistry_destroy(registry);
        free(serviceName);
 }
diff --git a/libs/framework/src/service_tracker.c 
b/libs/framework/src/service_tracker.c
index e421adc..f1a47e5 100644
--- a/libs/framework/src/service_tracker.c
+++ b/libs/framework/src/service_tracker.c
@@ -725,6 +725,7 @@ celix_service_tracker_t* 
celix_serviceTracker_createWithOptions(
                     return NULL;
                 }
                 versionRange = versionRange_createLDAPFilter(range, 
CELIX_FRAMEWORK_SERVICE_VERSION);
+                versionRange_destroy(range);
                 if(versionRange == NULL) {
                     framework_log(logger, OSGI_FRAMEWORK_LOG_ERROR, 
__FUNCTION__, __BASE_FILE__, __LINE__,
                                   "Error creating LDAP filter.");

Reply via email to