diff options
author | Luis Ontanon <luis.ontanon@gmail.com> | 2005-07-24 00:29:57 +0000 |
---|---|---|
committer | Luis Ontanon <luis.ontanon@gmail.com> | 2005-07-24 00:29:57 +0000 |
commit | d9c22732626fd6a54589abb83e5c9b4203ad8582 (patch) | |
tree | 506e17c43dfc10483b26cd6736863874c3d6f31b /epan/emem.c | |
parent | 38f827806af6f82573e4ef8a97b5d2805c341aad (diff) | |
download | wireshark-d9c22732626fd6a54589abb83e5c9b4203ad8582.tar.gz wireshark-d9c22732626fd6a54589abb83e5c9b4203ad8582.tar.bz2 wireshark-d9c22732626fd6a54589abb83e5c9b4203ad8582.zip |
add some utility functions that use ep_alloc instead of g_malloc.
ep_memdup
ep_strdup
ep_strndup
ep_strdup_printf
svn path=/trunk/; revision=15018
Diffstat (limited to 'epan/emem.c')
-rw-r--r-- | epan/emem.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/epan/emem.c b/epan/emem.c index 451fe706e2..d618883894 100644 --- a/epan/emem.c +++ b/epan/emem.c @@ -25,6 +25,8 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> +#include <stdarg.h> #include <glib.h> #include "emem.h" @@ -114,6 +116,61 @@ ep_alloc(size_t size) return buf; } + + +gchar* ep_strdup(const gchar* src) { + guint len = strlen(src); + gchar* dst; + + dst = strncpy(ep_alloc(len+1), src, len); + + dst[len] = '\0'; + + return dst; +} + +gchar* ep_strndup(const gchar* src, size_t len) { + guint actual_len = strlen(src); + gchar* dst; + + if (len > actual_len) + len = actual_len; + + dst = strncpy(ep_alloc(len+1), src, len); + + dst[len] = '\0'; + + return dst; +} + +guint8* ep_memdup(const guint8* src, size_t len) { + return memcpy(ep_alloc(len), src, len); +} + +gchar* ep_strdup_printf(const gchar* fmt, ...) { + va_list ap; + guint len; + gchar* dst; + + va_start(ap,fmt); + len = g_printf_string_upper_bound (fmt, ap); + + if ( len < (EMEM_PACKET_CHUNK_SIZE>>2) ) { + dst = ep_alloc(len); + g_vsnprintf (dst, len, fmt, ap); + } else { + len = (EMEM_PACKET_CHUNK_SIZE>>2) - 4; + + dst = ep_alloc(len); + g_vsnprintf (dst, len, fmt, ap); + memcpy(dst+len,"...",4); + } + + va_end(ap); + return dst; +} + + /* release all allocated memory back to the pool. */ void |