From 1f76576578d3e52ad4fec8021ab743277477af98 Mon Sep 17 00:00:00 2001 From: Jaeyun Date: Mon, 4 Jul 2022 18:35:45 +0900 Subject: [PATCH] [Util] string util functions Add util functions to copy memory. We should remove dependency to glib later. Signed-off-by: Jaeyun --- CMakeLists.txt | 7 -- .../nnstreamer-edge-common.c | 83 +++++++++++++++++-- .../nnstreamer-edge-common.h | 27 ++++-- .../nnstreamer-edge-internal.c | 20 ++--- 4 files changed, 106 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b46fb44..dada8e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/libnnstreamer-edge/nnstreamer-edge-common.c b/src/libnnstreamer-edge/nnstreamer-edge-common.c index b3eba68..e4a721f 100644 --- a/src/libnnstreamer-edge/nnstreamer-edge-common.c +++ b/src/libnnstreamer-edge/nnstreamer-edge-common.c @@ -10,8 +10,78 @@ * @bug No known bugs except for NYI items */ +#define _GNU_SOURCE +#include + #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; } diff --git a/src/libnnstreamer-edge/nnstreamer-edge-common.h b/src/libnnstreamer-edge/nnstreamer-edge-common.h index 892ef4a..8eebda4 100644 --- a/src/libnnstreamer-edge/nnstreamer-edge-common.h +++ b/src/libnnstreamer-edge/nnstreamer-edge-common.h @@ -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. diff --git a/src/libnnstreamer-edge/nnstreamer-edge-internal.c b/src/libnnstreamer-edge/nnstreamer-edge-internal.c index 24bfa30..642c1da 100644 --- a/src/libnnstreamer-edge/nnstreamer-edge-internal.c +++ b/src/libnnstreamer-edge/nnstreamer-edge-internal.c @@ -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); } -- 2.34.1