[Util] string util functions
authorJaeyun <jy1210.jung@samsung.com>
Mon, 4 Jul 2022 09:35:45 +0000 (18:35 +0900)
committerjaeyun-jung <39614140+jaeyun-jung@users.noreply.github.com>
Tue, 5 Jul 2022 03:36:30 +0000 (12:36 +0900)
Add util functions to copy memory. We should remove dependency to glib later.

Signed-off-by: Jaeyun <jy1210.jung@samsung.com>
CMakeLists.txt
src/libnnstreamer-edge/nnstreamer-edge-common.c
src/libnnstreamer-edge/nnstreamer-edge-common.h
src/libnnstreamer-edge/nnstreamer-edge-internal.c

index b46fb440cd3ec63a0f15540fcd27b233f51bf8a2..dada8e87a7b114c6ef15be3d5478256b7610e9ac 100644 (file)
@@ -47,13 +47,6 @@ FOREACH(flag ${EDGE_REQUIRE_PKGS_CFLAGS})
     SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}")
 ENDFOREACH(flag)
 
-# TODO FIXME remove glib dependency
-# Check glib version to set proper flag
-PKG_CHECK_MODULES(GLIB-2.0 glib-2.0)
-IF (GLIB-2.0_VERSION VERSION_GREATER_EQUAL "2.68")
-  SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DGLIB_USE_G_MEMDUP2=1")
-ENDIF()
-
 ADD_SUBDIRECTORY(src)
 
 IF (ENABLE_TEST)
index b3eba68bc88070e768cf1b3fecdd3a504006534e..e4a721fe7e3e0aac9a1086ce1bdd3c554f06bd7d 100644 (file)
  * @bug    No known bugs except for NYI items
  */
 
+#define _GNU_SOURCE
+#include <stdio.h>
+
 #include "nnstreamer-edge-common.h"
 
+/**
+ * @brief Allocate new memory and copy bytes.
+ * @note Caller should release newly allocated memory using free().
+ */
+void *
+nns_edge_memdup (const void *data, size_t size)
+{
+  void *mem = NULL;
+
+  if (data && size > 0) {
+    mem = malloc (size);
+
+    if (mem) {
+      memcpy (mem, data, size);
+    } else {
+      nns_edge_loge ("Failed to allocate memory (%zd).", size);
+    }
+  }
+
+  return mem;
+}
+
+/**
+ * @brief Allocate new memory and copy string.
+ * @note Caller should release newly allocated string using free().
+ */
+char *
+nns_edge_strdup (const char *str)
+{
+  char *new_str = NULL;
+  size_t len;
+
+  if (str) {
+    len = strlen (str);
+
+    new_str = (char *) malloc (len + 1);
+    if (new_str) {
+      memcpy (new_str, str, len);
+      new_str[len] = '\0';
+    } else {
+      nns_edge_loge ("Failed to allocate memory (%zd).", len + 1);
+    }
+  }
+
+  return new_str;
+}
+
+/**
+ * @brief Allocate new memory and print formatted string.
+ * @note Caller should release newly allocated string using free().
+ */
+char *
+nns_edge_strdup_printf (const char *format, ...)
+{
+  char *new_str = NULL;
+  va_list args;
+  int len;
+
+  va_start (args, format);
+  len = vasprintf (&new_str, format, args);
+  if (len < 0)
+    new_str = NULL;
+  va_end (args);
+
+  return new_str;
+}
+
 /**
  * @brief Create nnstreamer edge event.
  */
@@ -180,7 +250,7 @@ nns_edge_event_parse_capability (nns_edge_event_h event_h, char **capability)
     return NNS_EDGE_ERROR_INVALID_PARAMETER;
   }
 
-  *capability = g_strdup (ee->data.data);
+  *capability = nns_edge_strdup (ee->data.data);
 
   return NNS_EDGE_ERROR_NONE;
 }
@@ -295,14 +365,16 @@ nns_edge_data_copy (nns_edge_data_h data_h, nns_edge_data_h * new_data_h)
 
   copied->num = ed->num;
   for (i = 0; i < ed->num; i++) {
-    copied->data[i].data = _g_memdup (ed->data[i].data, ed->data[i].data_len);
+    copied->data[i].data = nns_edge_memdup (ed->data[i].data,
+        ed->data[i].data_len);
     copied->data[i].data_len = ed->data[i].data_len;
     copied->data[i].destroy_cb = g_free;
   }
 
   g_hash_table_iter_init (&iter, ed->info_table);
   while (g_hash_table_iter_next (&iter, &key, &value)) {
-    g_hash_table_insert (copied->info_table, g_strdup (key), g_strdup (value));
+    g_hash_table_insert (copied->info_table, nns_edge_strdup (key),
+        nns_edge_strdup (value));
   }
 
   return NNS_EDGE_ERROR_NONE;
@@ -429,7 +501,8 @@ nns_edge_data_set_info (nns_edge_data_h data_h, const char *key,
     return NNS_EDGE_ERROR_INVALID_PARAMETER;
   }
 
-  g_hash_table_insert (ed->info_table, g_strdup (key), g_strdup (value));
+  g_hash_table_insert (ed->info_table, nns_edge_strdup (key),
+      nns_edge_strdup (value));
 
   return NNS_EDGE_ERROR_NONE;
 }
@@ -466,7 +539,7 @@ nns_edge_data_get_info (nns_edge_data_h data_h, const char *key, char **value)
     return NNS_EDGE_ERROR_INVALID_PARAMETER;
   }
 
-  *value = g_strdup (val);
+  *value = nns_edge_strdup (val);
 
   return NNS_EDGE_ERROR_NONE;
 }
index 892ef4a411e21b49cba3d3d06c22561431f7b93c..8eebda4e66d74bf0cbf982257a5958eafe1ea39a 100644 (file)
@@ -29,15 +29,6 @@ extern "C" {
 #define UNUSED(expr) do { (void)(expr); } while (0)
 #endif
 
-/**
- * @brief g_memdup() function replaced by g_memdup2() in glib version >= 2.68
- */
-#if GLIB_USE_G_MEMDUP2
-#define _g_memdup g_memdup2
-#else
-#define _g_memdup g_memdup
-#endif
-
 #define NNS_EDGE_MAGIC 0xfeedfeed
 #define NNS_EDGE_MAGIC_DEAD 0xdeaddead
 #define NNS_EDGE_MAGIC_IS_VALID(h) ((h) && (h)->magic == NNS_EDGE_MAGIC)
@@ -87,6 +78,24 @@ typedef struct {
 #define nns_edge_logd g_debug
 #define nns_edge_logf g_error
 
+/**
+ * @brief Allocate new memory and copy bytes.
+ * @note Caller should release newly allocated memory using free().
+ */
+void *nns_edge_memdup (const void *data, size_t size);
+
+/**
+ * @brief Allocate new memory and copy string.
+ * @note Caller should release newly allocated string using free().
+ */
+char *nns_edge_strdup (const char *str);
+
+/**
+ * @brief Allocate new memory and print formatted string.
+ * @note Caller should release newly allocated string using free().
+ */
+char *nns_edge_strdup_printf (const char *format, ...);
+
 /**
  * @brief Create nnstreamer edge event.
  * @note This is internal function for edge event.
index 24bfa30d13cd8f35cd189a4f737ed6e02502e78a..642c1dae6f6035bdfc3c84d10168baf23db43d05 100644 (file)
@@ -169,7 +169,7 @@ _parse_host_str (const char *host, char **ip, int *port)
 static void
 _get_host_str (const char *ip, const int port, char **host)
 {
-  *host = g_strdup_printf ("%s:%d", ip, port);
+  *host = nns_edge_strdup_printf ("%s:%d", ip, port);
 }
 
 /**
@@ -618,7 +618,7 @@ _nns_edge_connect_to (nns_edge_handle_s * eh, const char *ip, int port)
   }
 
   memset (conn, 0, sizeof (nns_edge_conn_s));
-  conn->ip = g_strdup (ip);
+  conn->ip = nns_edge_strdup (ip);
   conn->port = port;
   conn->cancellable = g_cancellable_new ();
 
@@ -748,7 +748,7 @@ _nns_edge_message_handler (void *thread_data)
     }
 
     /* Set client ID in edge data */
-    val = g_strdup_printf ("%ld", (long int) client_id);
+    val = nns_edge_strdup_printf ("%ld", (long int) client_id);
     nns_edge_data_set_info (data_h, "client_id", val);
     g_free (val);
 
@@ -956,11 +956,11 @@ nns_edge_create_handle (const char *id, const char *topic, nns_edge_h * edge_h)
   memset (eh, 0, sizeof (nns_edge_handle_s));
   nns_edge_lock_init (eh);
   eh->magic = NNS_EDGE_MAGIC;
-  eh->id = g_strdup (id);
-  eh->topic = g_strdup (topic);
+  eh->id = nns_edge_strdup (id);
+  eh->topic = nns_edge_strdup (topic);
   eh->protocol = NNS_EDGE_PROTOCOL_TCP;
   eh->is_server = true;
-  eh->recv_ip = g_strdup ("localhost");
+  eh->recv_ip = nns_edge_strdup ("localhost");
   eh->recv_port = 0;
   eh->caps_str = NULL;
 
@@ -1367,7 +1367,7 @@ nns_edge_get_topic (nns_edge_h edge_h, char **topic)
     return NNS_EDGE_ERROR_INVALID_PARAMETER;
   }
 
-  *topic = g_strdup (eh->topic);
+  *topic = nns_edge_strdup (eh->topic);
 
   nns_edge_unlock (eh);
   return NNS_EDGE_ERROR_NONE;
@@ -1411,17 +1411,17 @@ nns_edge_set_info (nns_edge_h edge_h, const char *key, const char *value)
    * @todo Change key-value set as json or hash table.
    */
   if (0 == g_ascii_strcasecmp (key, "CAPS")) {
-    ret_str = g_strdup_printf ("%s%s", _STR_NULL (eh->caps_str), value);
+    ret_str = nns_edge_strdup_printf ("%s%s", _STR_NULL (eh->caps_str), value);
     g_free (eh->caps_str);
     eh->caps_str = ret_str;
   } else if (0 == g_ascii_strcasecmp (key, "IP")) {
     g_free (eh->recv_ip);
-    eh->recv_ip = g_strdup (value);
+    eh->recv_ip = nns_edge_strdup (value);
   } else if (0 == g_ascii_strcasecmp (key, "PORT")) {
     eh->recv_port = g_ascii_strtoll (value, NULL, 10);
   } else if (0 == g_ascii_strcasecmp (key, "TOPIC")) {
     g_free (eh->topic);
-    eh->topic = g_strdup (value);
+    eh->topic = nns_edge_strdup (value);
   } else {
     nns_edge_logw ("Failed to set edge info. Unknown key: %s", key);
   }