Add util functions to copy memory. We should remove dependency to glib later.
Signed-off-by: Jaeyun <jy1210.jung@samsung.com>
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)
* @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.
*/
return NNS_EDGE_ERROR_INVALID_PARAMETER;
}
- *capability = g_strdup (ee->data.data);
+ *capability = nns_edge_strdup (ee->data.data);
return NNS_EDGE_ERROR_NONE;
}
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;
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;
}
return NNS_EDGE_ERROR_INVALID_PARAMETER;
}
- *value = g_strdup (val);
+ *value = nns_edge_strdup (val);
return NNS_EDGE_ERROR_NONE;
}
#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)
#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.
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);
}
/**
}
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 ();
}
/* 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);
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;
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;
* @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);
}