Add back the fix from revision 54693.
[metze/wireshark/wip.git] / epan / emem.c
index fb38d7553225228132808228b706c65cc4f9a7ee..c4868bad20d1f594abc84227dc9d5510b46099bf 100644 (file)
 
 #include <glib.h>
 
+#include "app_mem_usage.h"
 #include "proto.h"
+#include "exceptions.h"
 #include "emem.h"
+#include "wmem/wmem.h"
 
 #ifdef _WIN32
 #include <windows.h>   /* VirtualAlloc, VirtualProtect */
 
 #ifdef WANT_GUARD_PAGES
 /* Add guard pages at each end of our allocated memory */
+
 #if defined(HAVE_SYSCONF) && defined(HAVE_MMAP) && defined(HAVE_MPROTECT) && defined(HAVE_STDINT_H)
 #include <stdint.h>
+
 #ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
-#endif
+#endif /* HAVE_SYS_TYPES_H */
+
 #include <sys/mman.h>
+
 #if defined(MAP_ANONYMOUS)
 #define ANON_PAGE_MODE (MAP_ANONYMOUS|MAP_PRIVATE)
 #elif defined(MAP_ANON)
 #else
 #define ANON_PAGE_MODE (MAP_PRIVATE)   /* have to map /dev/zero */
 #define NEED_DEV_ZERO
-#endif
+#endif /* defined(MAP_ANONYMOUS) */
+
 #ifdef NEED_DEV_ZERO
 #include <fcntl.h>
 static int dev_zero_fd;
 #define ANON_FD        dev_zero_fd
 #else
 #define ANON_FD        -1
-#endif
+#endif /* NEED_DEV_ZERO */
+
 #define USE_GUARD_PAGES 1
-#endif
-#endif
+#endif /* defined(HAVE_SYSCONF) && defined(HAVE_MMAP) && defined(HAVE_MPROTECT) && defined(HAVE_STDINT_H) */
+#endif /* WANT_GUARD_PAGES */
 
 /* When required, allocate more memory from the OS in this size chunks */
 #define EMEM_PACKET_CHUNK_SIZE (10 * 1024 * 1024)
@@ -106,6 +115,7 @@ static int dev_zero_fd;
 typedef struct _emem_chunk_t {
        struct _emem_chunk_t *next;
        char            *buf;
+       size_t           size;
        unsigned int    amount_free_init;
        unsigned int    amount_free;
        unsigned int    free_offset_init;
@@ -113,14 +123,14 @@ typedef struct _emem_chunk_t {
        void            *canary_last;
 } emem_chunk_t;
 
-typedef struct _emem_header_t {
+typedef struct _emem_pool_t {
        emem_chunk_t *free_list;
        emem_chunk_t *used_list;
 
        emem_tree_t *trees;             /* only used by se_mem allocator */
 
        guint8 canary[EMEM_CANARY_DATA_SIZE];
-       void *(*memory_alloc)(size_t size, struct _emem_header_t *);
+       void *(*memory_alloc)(size_t size, struct _emem_pool_t *);
 
        /*
         * Tools like Valgrind and ElectricFence don't work well with memchunks.
@@ -150,15 +160,10 @@ typedef struct _emem_header_t {
         */
        gboolean debug_verify_pointers;
 
-} emem_header_t;
+} emem_pool_t;
 
-static GSList *ep_pool_stack = NULL;
-/* Some functions use ep_ calls when there isn't actually a packet in scope.
- * These should perhaps be fixed, but in the meantime, ep_fake_pool is used
- * to handle those cases. */
-static emem_header_t ep_fake_pool;
-static emem_header_t *ep_packet_mem = &ep_fake_pool;
-static emem_header_t se_packet_mem;
+static emem_pool_t ep_packet_mem;
+static emem_pool_t se_packet_mem;
 
 /*
  *  Memory scrubbing is expensive but can be useful to ensure we don't:
@@ -170,14 +175,14 @@ static gboolean debug_use_memory_scrubber = FALSE;
 
 #if defined (_WIN32)
 static SYSTEM_INFO sysinfo;
-static OSVERSIONINFO versinfo;
+static gboolean iswindowsplatform;
 static int pagesize;
 #elif defined(USE_GUARD_PAGES)
 static intptr_t pagesize;
 #endif /* _WIN32 / USE_GUARD_PAGES */
 
-static void *emem_alloc_chunk(size_t size, emem_header_t *mem);
-static void *emem_alloc_glib(size_t size, emem_header_t *mem);
+static void *emem_alloc_chunk(size_t size, emem_pool_t *mem);
+static void *emem_alloc_glib(size_t size, emem_pool_t *mem);
 
 /*
  * Set a canary value to be placed between memchunks.
@@ -212,7 +217,7 @@ emem_canary_next(guint8 *mem_canary, guint8 *canary, int *len)
                        memcpy(&ptr, &canary[i+1], sizeof(void *));
 
                        if (len)
-                               *len = i + 1 + sizeof(void *);
+                               *len = i + 1 + (int)sizeof(void *);
                        return ptr;
                }
 
@@ -264,11 +269,11 @@ ep_check_canary_integrity(const char* fmt, ...)
        g_vsnprintf(here, sizeof(here), fmt, ap);
        va_end(ap);
 
-       for (npc = ep_packet_mem->free_list; npc != NULL; npc = npc->next) {
+       for (npc = ep_packet_mem.free_list; npc != NULL; npc = npc->next) {
                void *canary_next = npc->canary_last;
 
                while (canary_next != NULL) {
-                       canary_next = emem_canary_next(ep_packet_mem->canary, canary_next, NULL);
+                       canary_next = emem_canary_next(ep_packet_mem.canary, canary_next, NULL);
                        /* XXX, check if canary_next is inside allocated memory? */
 
                        if (canary_next == (void *) -1)
@@ -281,7 +286,7 @@ ep_check_canary_integrity(const char* fmt, ...)
 #endif
 
 static void
-emem_init_chunk(emem_header_t *mem)
+emem_init_chunk(emem_pool_t *mem)
 {
        if (mem->debug_use_canary)
                emem_canary_init(mem->canary);
@@ -292,27 +297,57 @@ emem_init_chunk(emem_header_t *mem)
                mem->memory_alloc = emem_alloc_glib;
 }
 
+static gsize
+emem_memory_usage(const emem_pool_t *pool)
+{
+       gsize total_used = 0;
+       emem_chunk_t *chunk;
+
+       for (chunk = pool->used_list; chunk; chunk = chunk->next)
+               total_used += (chunk->amount_free_init - chunk->amount_free);
+
+       for (chunk = pool->free_list; chunk; chunk = chunk->next)
+               total_used += (chunk->amount_free_init - chunk->amount_free);
+
+       return total_used;
+}
+
+static gsize
+ep_memory_usage(void)
+{
+       return emem_memory_usage(&ep_packet_mem);
+}
 
 /* Initialize the packet-lifetime memory allocation pool.
  * This function should be called only once when Wireshark or TShark starts
  * up.
  */
 static void
-ep_init_chunk(emem_header_t *mem)
+ep_init_chunk(void)
 {
-       mem->free_list=NULL;
-       mem->used_list=NULL;
-       mem->trees=NULL;        /* not used by this allocator */
+       static const ws_mem_usage_t ep_stats = { "EP", ep_memory_usage, NULL };
 
-       mem->debug_use_chunks = (getenv("WIRESHARK_DEBUG_EP_NO_CHUNKS") == NULL);
-       mem->debug_use_canary = mem->debug_use_chunks && (getenv("WIRESHARK_DEBUG_EP_NO_CANARY") == NULL);
-       mem->debug_verify_pointers = (getenv("WIRESHARK_EP_VERIFY_POINTERS") != NULL);
+       ep_packet_mem.free_list=NULL;
+       ep_packet_mem.used_list=NULL;
+       ep_packet_mem.trees=NULL;       /* not used by this allocator */
+
+       ep_packet_mem.debug_use_chunks = (getenv("WIRESHARK_DEBUG_EP_NO_CHUNKS") == NULL);
+       ep_packet_mem.debug_use_canary = ep_packet_mem.debug_use_chunks && (getenv("WIRESHARK_DEBUG_EP_NO_CANARY") == NULL);
+       ep_packet_mem.debug_verify_pointers = (getenv("WIRESHARK_EP_VERIFY_POINTERS") != NULL);
 
 #ifdef DEBUG_INTENSE_CANARY_CHECKS
        intense_canary_checking = (getenv("WIRESHARK_DEBUG_EP_INTENSE_CANARY") != NULL);
 #endif
 
-       emem_init_chunk(mem);
+       emem_init_chunk(&ep_packet_mem);
+
+       memory_usage_component_register(&ep_stats);
+}
+
+static gsize
+se_memory_usage(void)
+{
+       return emem_memory_usage(&se_packet_mem);
 }
 
 /* Initialize the capture-lifetime memory allocation pool.
@@ -322,6 +357,8 @@ ep_init_chunk(emem_header_t *mem)
 static void
 se_init_chunk(void)
 {
+       static const ws_mem_usage_t se_stats = { "SE", se_memory_usage, NULL };
+
        se_packet_mem.free_list = NULL;
        se_packet_mem.used_list = NULL;
        se_packet_mem.trees = NULL;
@@ -331,6 +368,8 @@ se_init_chunk(void)
        se_packet_mem.debug_verify_pointers = (getenv("WIRESHARK_SE_VERIFY_POINTERS") != NULL);
 
        emem_init_chunk(&se_packet_mem);
+
+       memory_usage_component_register(&se_stats);
 }
 
 /*  Initialize all the allocators here.
@@ -340,8 +379,8 @@ se_init_chunk(void)
 void
 emem_init(void)
 {
+       ep_init_chunk();
        se_init_chunk();
-       ep_init_chunk(&ep_fake_pool);
 
        if (getenv("WIRESHARK_DEBUG_SCRUB_MEMORY"))
                debug_use_memory_scrubber  = TRUE;
@@ -351,6 +390,23 @@ emem_init(void)
        GetSystemInfo(&sysinfo);
        pagesize = sysinfo.dwPageSize;
 
+#if (_MSC_VER >= 1800)
+       /*
+        * On VS2103, GetVersionEx is deprecated. Microsoft recommend to
+        * use VerifyVersionInfo instead
+        */
+       {
+               OSVERSIONINFOEX osvi;
+               DWORDLONG dwlConditionMask = 0;
+               int op = VER_EQUAL;
+
+               SecureZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
+               osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
+               osvi.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS;
+               VER_SET_CONDITION(dwlConditionMask, VER_PLATFORMID, op);
+               iswindowsplatform = VerifyVersionInfo(&osvi, VER_PLATFORMID, dwlConditionMask);
+       }
+#else
        /* calling GetVersionEx using the OSVERSIONINFO structure.
         * OSVERSIONINFOEX requires Win NT4 with SP6 or newer NT Versions.
         * OSVERSIONINFOEX will fail on Win9x and older NT Versions.
@@ -359,11 +415,20 @@ emem_init(void)
         * http://msdn.microsoft.com/library/en-us/sysinfo/base/osversioninfo_str.asp
         * http://msdn.microsoft.com/library/en-us/sysinfo/base/osversioninfoex_str.asp
         */
-       versinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
-       GetVersionEx(&versinfo);
+       {
+               OSVERSIONINFO versinfo;
+
+               SecureZeroMemory(&versinfo, sizeof(OSVERSIONINFO));
+               versinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+               GetVersionEx(&versinfo);
+               iswindowsplatform = (versinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
+       }
+#endif
 
 #elif defined(USE_GUARD_PAGES)
        pagesize = sysconf(_SC_PAGESIZE);
+       if (pagesize == -1)
+               fprintf(stderr, "Warning: call to sysconf() for _SC_PAGESIZE has failed...\n");
 #ifdef NEED_DEV_ZERO
        dev_zero_fd = ws_open("/dev/zero", O_RDWR);
        g_assert(dev_zero_fd != -1);
@@ -377,7 +442,7 @@ static guint allocations[NUM_ALLOC_DIST] = { 0 };
 static guint total_no_chunks = 0;
 
 static void
-print_alloc_stats()
+print_alloc_stats(void)
 {
        guint num_chunks = 0;
        guint num_allocs = 0;
@@ -392,21 +457,21 @@ print_alloc_stats()
 
        fprintf(stderr, "\n-------- EP allocator statistics --------\n");
        fprintf(stderr, "%s chunks, %s canaries, %s memory scrubber\n",
-              ep_packet_mem->debug_use_chunks ? "Using" : "Not using",
-              ep_packet_mem->debug_use_canary ? "using" : "not using",
+              ep_packet_mem.debug_use_chunks ? "Using" : "Not using",
+              ep_packet_mem.debug_use_canary ? "using" : "not using",
               debug_use_memory_scrubber ? "using" : "not using");
 
-       if (! (ep_packet_mem->free_list || !ep_packet_mem->used_list)) {
+       if (! (ep_packet_mem.free_list || !ep_packet_mem.used_list)) {
                fprintf(stderr, "No memory allocated\n");
                ep_stat = FALSE;
        }
-       if (ep_packet_mem->debug_use_chunks && ep_stat) {
+       if (ep_packet_mem.debug_use_chunks && ep_stat) {
                /* Nothing interesting without chunks */
                /*  Only look at the used_list since those chunks are fully
                 *  used.  Looking at the free list would skew our view of what
                 *  we have wasted.
                 */
-               for (chunk = ep_packet_mem->used_list; chunk; chunk = chunk->next) {
+               for (chunk = ep_packet_mem.used_list; chunk; chunk = chunk->next) {
                        num_chunks++;
                        total_used += (chunk->amount_free_init - chunk->amount_free);
                        total_allocation += chunk->amount_free_init;
@@ -464,7 +529,7 @@ print_alloc_stats()
                        int len;
 
                        while (ptr != NULL) {
-                               ptr = emem_canary_next(se_packet_mem.canary, ptr, &len);
+                               ptr = emem_canary_next(se_packet_mem.canary, (guint8*)ptr, &len);
 
                                if (ptr == (void *) -1)
                                        g_error("Memory corrupted");
@@ -541,7 +606,7 @@ print_alloc_stats()
 static gboolean
 emem_verify_pointer_list(const emem_chunk_t *chunk_list, const void *ptr)
 {
-       const gchar *cptr = ptr;
+       const gchar *cptr = (const gchar *)ptr;
        const emem_chunk_t *chunk;
 
        for (chunk = chunk_list; chunk; chunk = chunk->next) {
@@ -552,7 +617,7 @@ emem_verify_pointer_list(const emem_chunk_t *chunk_list, const void *ptr)
 }
 
 static gboolean
-emem_verify_pointer(const emem_header_t *hdr, const void *ptr)
+emem_verify_pointer(const emem_pool_t *hdr, const void *ptr)
 {
        return emem_verify_pointer_list(hdr->free_list, ptr) || emem_verify_pointer_list(hdr->used_list, ptr);
 }
@@ -560,8 +625,8 @@ emem_verify_pointer(const emem_header_t *hdr, const void *ptr)
 gboolean
 ep_verify_pointer(const void *ptr)
 {
-       if (ep_packet_mem->debug_verify_pointers)
-               return emem_verify_pointer(ep_packet_mem, ptr);
+       if (ep_packet_mem.debug_verify_pointers)
+               return emem_verify_pointer(&ep_packet_mem, ptr);
        else
                return FALSE;
 }
@@ -579,7 +644,7 @@ static void
 emem_scrub_memory(char *buf, size_t size, gboolean alloc)
 {
        guint scrubbed_value;
-       guint offset;
+       size_t offset;
 
        if (!debug_use_memory_scrubber)
                return;
@@ -634,7 +699,7 @@ emem_create_chunk(size_t size)
         */
 
        /* XXX - is MEM_COMMIT|MEM_RESERVE correct? */
-       npc->buf = VirtualAlloc(NULL, size,
+       npc->buf = (char *)VirtualAlloc(NULL, size,
                MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
 
        if (npc->buf == NULL) {
@@ -646,7 +711,7 @@ emem_create_chunk(size_t size)
        }
 
 #elif defined(USE_GUARD_PAGES)
-       npc->buf = mmap(NULL, size,
+       npc->buf = (char *)mmap(NULL, size,
                PROT_READ|PROT_WRITE, ANON_PAGE_MODE, ANON_FD, 0);
 
        if (npc->buf == MAP_FAILED) {
@@ -671,22 +736,6 @@ emem_create_chunk(size_t size)
        return npc;
 }
 
-static void
-emem_destroy_chunk(emem_chunk_t *npc)
-{
-#if defined (_WIN32)
-       VirtualFree(npc->buf, 0, MEM_RELEASE);
-#elif defined(USE_GUARD_PAGES)
-       munmap(npc->buf, npc->amount_free_init);
-#else
-       g_free(npc->buf);
-#endif
-#ifdef SHOW_EMEM_STATS
-       total_no_chunks--;
-#endif
-       g_free(npc);
-}
-
 static emem_chunk_t *
 emem_create_chunk_gp(size_t size)
 {
@@ -710,9 +759,9 @@ emem_create_chunk_gp(size_t size)
        prot2 = (char *) ((((intptr_t) buf_end - (1 * pagesize)) / pagesize) * pagesize);
 
        ret = VirtualProtect(prot1, pagesize, PAGE_NOACCESS, &oldprot);
-       g_assert(ret != 0 || versinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
+       g_assert(ret != 0 || iswindowsplatform);
        ret = VirtualProtect(prot2, pagesize, PAGE_NOACCESS, &oldprot);
-       g_assert(ret != 0 || versinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
+       g_assert(ret != 0 || iswindowsplatform);
 
        npc->amount_free_init = (unsigned int) (prot2 - prot1 - pagesize);
        npc->free_offset_init = (unsigned int) (prot1 - npc->buf) + pagesize;
@@ -728,8 +777,8 @@ emem_create_chunk_gp(size_t size)
        ret = mprotect(prot2, pagesize, PROT_NONE);
        g_assert(ret != -1);
 
-       npc->amount_free_init = prot2 - prot1 - pagesize;
-       npc->free_offset_init = (prot1 - npc->buf) + pagesize;
+       npc->amount_free_init = (unsigned int)(prot2 - prot1 - pagesize);
+       npc->free_offset_init = (unsigned int)((prot1 - npc->buf) + pagesize);
 #else
        npc->amount_free_init = size;
        npc->free_offset_init = 0;
@@ -741,7 +790,7 @@ emem_create_chunk_gp(size_t size)
 }
 
 static void *
-emem_alloc_chunk(size_t size, emem_header_t *mem)
+emem_alloc_chunk(size_t size, emem_pool_t *mem)
 {
        void *buf;
 
@@ -832,33 +881,48 @@ emem_alloc_chunk(size_t size, emem_header_t *mem)
 }
 
 static void *
-emem_alloc_glib(size_t size, emem_header_t *mem)
+emem_alloc_glib(size_t size, emem_pool_t *mem)
 {
        emem_chunk_t *npc;
 
        npc=g_new(emem_chunk_t, 1);
        npc->next=mem->used_list;
-       npc->buf=g_malloc(size);
+       npc->buf=(char *)g_malloc(size);
        npc->canary_last = NULL;
        mem->used_list=npc;
        /* There's no padding/alignment involved (from our point of view) when
         * we fetch the memory directly from the system pool, so WYSIWYG */
-       npc->free_offset = npc->free_offset_init = 0;
-       npc->amount_free = npc->amount_free_init = (unsigned int) size;
+       npc->amount_free = npc->free_offset_init = 0;
+       npc->free_offset = npc->amount_free_init = (unsigned int) size;
 
        return npc->buf;
 }
 
 /* allocate 'size' amount of memory. */
 static void *
-emem_alloc(size_t size, emem_header_t *mem)
+emem_alloc(size_t size, emem_pool_t *mem)
 {
-       void *buf = mem->memory_alloc(size, mem);
+       void *buf;
+
+#if 0
+       /* For testing wmem, effectively redirects most emem memory to wmem.
+        * You will also have to comment out several assertions in wmem_core.c,
+        * specifically anything g_assert(allocator->in_scope), since it is much
+        * stricter about when it is permitted to be called. */
+       if (mem == &ep_packet_mem) {
+               return wmem_alloc(wmem_packet_scope(), size);
+       }
+       else if (mem == &se_packet_mem) {
+               return wmem_alloc(wmem_file_scope(), size);
+       }
+#endif
+
+       buf = mem->memory_alloc(size, mem);
 
        /*  XXX - this is a waste of time if the allocator function is going to
         *  memset this straight back to 0.
         */
-       emem_scrub_memory(buf, size, TRUE);
+       emem_scrub_memory((char *)buf, size, TRUE);
 
        return buf;
 }
@@ -869,7 +933,7 @@ emem_alloc(size_t size, emem_header_t *mem)
 void *
 ep_alloc(size_t size)
 {
-       return emem_alloc(size, ep_packet_mem);
+       return emem_alloc(size, &ep_packet_mem);
 }
 
 /* allocate 'size' amount of memory with an allocation lifetime until the
@@ -881,52 +945,6 @@ se_alloc(size_t size)
        return emem_alloc(size, &se_packet_mem);
 }
 
-void *
-sl_alloc(struct ws_memory_slab *mem_chunk)
-{
-       emem_chunk_t *chunk;
-       void *ptr;
-
-       /* XXX, debug_use_slices -> fallback to g_slice_alloc0 */
-
-       if ((mem_chunk->freed != NULL)) {
-               ptr = mem_chunk->freed;
-               memcpy(&mem_chunk->freed, ptr, sizeof(void *));
-               return ptr;
-       }
-
-       if (!(chunk = mem_chunk->chunk_list) || chunk->amount_free < (guint) mem_chunk->item_size) {
-               size_t alloc_size = mem_chunk->item_size * mem_chunk->count;
-
-               /* align to page-size */
-#if defined (_WIN32) || defined(USE_GUARD_PAGES)
-               alloc_size = (alloc_size + (pagesize - 1)) & ~(pagesize - 1);
-#endif
-
-               chunk = emem_create_chunk(alloc_size);  /* NOTE: using version without guard pages! */
-               chunk->next = mem_chunk->chunk_list;
-               mem_chunk->chunk_list = chunk;
-       }
-
-       ptr = chunk->buf + chunk->free_offset;
-       chunk->free_offset += mem_chunk->item_size;
-       chunk->amount_free -= mem_chunk->item_size;
-
-       return ptr;
-}
-
-void
-sl_free(struct ws_memory_slab *mem_chunk, gpointer ptr)
-{
-       /* XXX, debug_use_slices -> fallback to g_slice_free1 */
-
-       /* XXX, abort if ptr not found in emem_verify_pointer_list()? */
-       if (ptr != NULL /* && emem_verify_pointer_list(mem_chunk->chunk_list, ptr) */) {
-               memcpy(ptr, &(mem_chunk->freed), sizeof(void *));
-               mem_chunk->freed = ptr;
-       }
-}
-
 void *
 ep_alloc0(size_t size)
 {
@@ -939,12 +957,6 @@ se_alloc0(size_t size)
        return memset(se_alloc(size),'\0',size);
 }
 
-void *
-sl_alloc0(struct ws_memory_slab *mem_chunk)
-{
-       return memset(sl_alloc(mem_chunk), '\0', mem_chunk->item_size);
-}
-
 static gchar *
 emem_strdup(const gchar *src, void *allocator(size_t))
 {
@@ -955,10 +967,10 @@ emem_strdup(const gchar *src, void *allocator(size_t))
         * have to bother checking it.
         */
        if(!src)
-               return "<NULL>";
+               src = "<NULL>";
 
        len = (guint) strlen(src);
-       dst = memcpy(allocator(len+1), src, len+1);
+       dst = (gchar *)memcpy(allocator(len+1), src, len+1);
 
        return dst;
 }
@@ -978,7 +990,7 @@ se_strdup(const gchar *src)
 static gchar *
 emem_strndup(const gchar *src, size_t len, void *allocator(size_t))
 {
-       gchar *dst = allocator(len+1);
+       gchar *dst = (gchar *)allocator(len+1);
        guint i;
 
        for (i = 0; (i < len) && src[i]; i++)
@@ -1026,7 +1038,7 @@ emem_strdup_vprintf(const gchar *fmt, va_list ap, void *allocator(size_t))
 
        len = g_printf_string_upper_bound(fmt, ap);
 
-       dst = allocator(len+1);
+       dst = (gchar *)allocator(len+1);
        g_vsnprintf (dst, (gulong) len, fmt, ap2);
        va_end(ap2);
 
@@ -1167,7 +1179,7 @@ ep_strconcat(const gchar *string1, ...)
        }
        va_end(args);
 
-       concat = ep_alloc(l);
+       concat = (gchar *)ep_alloc(l);
        ptr = concat;
 
        ptr = g_stpcpy(ptr, string1);
@@ -1186,7 +1198,7 @@ ep_strconcat(const gchar *string1, ...)
 
 /* release all allocated memory back to the pool. */
 static void
-emem_free_all(emem_header_t *mem)
+emem_free_all(emem_pool_t *mem)
 {
        gboolean use_chunks = mem->debug_use_chunks;
 
@@ -1206,7 +1218,7 @@ emem_free_all(emem_header_t *mem)
        while (npc != NULL) {
                if (use_chunks) {
                        while (npc->canary_last != NULL) {
-                               npc->canary_last = emem_canary_next(mem->canary, npc->canary_last, NULL);
+                               npc->canary_last = emem_canary_next(mem->canary, (guint8 *)npc->canary_last, NULL);
                                /* XXX, check if canary_last is inside allocated memory? */
 
                                if (npc->canary_last == (void *) -1)
@@ -1242,39 +1254,11 @@ emem_free_all(emem_header_t *mem)
        }
 }
 
-emem_header_t *ep_create_pool(void)
-{
-       emem_header_t *mem;
-       
-       mem = g_malloc(sizeof(emem_header_t));
-
-       ep_init_chunk(mem);
-
-       if (ep_pool_stack == NULL) {
-               emem_free_all(&ep_fake_pool);
-       }
-
-       ep_pool_stack = g_slist_prepend(ep_pool_stack, mem);
-
-       ep_packet_mem = mem;
-
-       return mem;
-}
-
-void ep_free_pool(emem_header_t *mem)
+/* release all allocated memory back to the pool. */
+void
+ep_free_all(void)
 {
-       ep_pool_stack = g_slist_remove(ep_pool_stack, mem);
-
-       emem_free_all(mem);
-
-       g_free(mem);
-
-       if (ep_pool_stack == NULL) {
-               ep_packet_mem = &ep_fake_pool;
-       }
-       else {
-               ep_packet_mem = ep_pool_stack->data;
-       }
+       emem_free_all(&ep_packet_mem);
 }
 
 /* release all allocated memory back to the pool. */
@@ -1288,71 +1272,12 @@ se_free_all(void)
        emem_free_all(&se_packet_mem);
 }
 
-void
-sl_free_all(struct ws_memory_slab *mem_chunk)
-{
-       emem_chunk_t *chunk_list = mem_chunk->chunk_list;
-
-       mem_chunk->chunk_list = NULL;
-       mem_chunk->freed = NULL;
-       while (chunk_list) {
-               emem_chunk_t *chunk = chunk_list;
-
-               chunk_list = chunk_list->next;
-               emem_destroy_chunk(chunk);
-       }
-}
-
-ep_stack_t
-ep_stack_new(void) {
-       ep_stack_t s = ep_new(struct _ep_stack_frame_t*);
-       *s = ep_new0(struct _ep_stack_frame_t);
-       return s;
-}
-
-/*  for ep_stack_t we'll keep the popped frames so we reuse them instead
-of allocating new ones.
-*/
-
-void *
-ep_stack_push(ep_stack_t stack, void* data)
-{
-       struct _ep_stack_frame_t* frame;
-       struct _ep_stack_frame_t* head = (*stack);
-
-       if (head->above) {
-               frame = head->above;
-       } else {
-               frame = ep_new(struct _ep_stack_frame_t);
-               head->above = frame;
-               frame->below = head;
-               frame->above = NULL;
-       }
-
-       frame->payload = data;
-       (*stack) = frame;
-
-       return data;
-}
-
-void *
-ep_stack_pop(ep_stack_t stack)
-{
-
-       if ((*stack)->below) {
-               (*stack) = (*stack)->below;
-               return (*stack)->above->payload;
-       } else {
-               return NULL;
-       }
-}
-
 emem_tree_t *
 se_tree_create(int type, const char *name)
 {
        emem_tree_t *tree_list;
 
-       tree_list=g_malloc(sizeof(emem_tree_t));
+       tree_list=(emem_tree_t *)g_malloc(sizeof(emem_tree_t));
        tree_list->next=se_packet_mem.trees;
        tree_list->type=type;
        tree_list->tree=NULL;
@@ -1386,99 +1311,6 @@ emem_tree_lookup32(emem_tree_t *se_tree, guint32 key)
        return NULL;
 }
 
-void *
-emem_tree_lookup32_le(emem_tree_t *se_tree, guint32 key)
-{
-       emem_tree_node_t *node;
-
-       node=se_tree->tree;
-
-       if(!node){
-               return NULL;
-       }
-
-
-       while(node){
-               if(key==node->key32){
-                       return node->data;
-               }
-               if(key<node->key32){
-                       if(node->left){
-                               node=node->left;
-                               continue;
-                       } else {
-                               break;
-                       }
-               }
-               if(key>node->key32){
-                       if(node->right){
-                               node=node->right;
-                               continue;
-                       } else {
-                               break;
-                       }
-               }
-       }
-
-
-       if(!node){
-               return NULL;
-       }
-
-       /* If we are still at the root of the tree this means that this node
-        * is either smaller than the search key and then we return this
-        * node or else there is no smaller key available and then
-        * we return NULL.
-        */
-       if(!node->parent){
-               if(key>node->key32){
-                       return node->data;
-               } else {
-                       return NULL;
-               }
-       }
-
-       if(node->parent->left==node){
-               /* left child */
-
-               if(key>node->key32){
-                       /* if this is a left child and its key is smaller than
-                        * the search key, then this is the node we want.
-                        */
-                       return node->data;
-               } else {
-                       /* if this is a left child and its key is bigger than
-                        * the search key, we have to check if any
-                        * of our ancestors are smaller than the search key.
-                        */
-                       while(node){
-                               if(key>node->key32){
-                                       return node->data;
-                               }
-                               node=node->parent;
-                       }
-                       return NULL;
-               }
-       } else {
-               /* right child */
-
-               if(node->key32<key){
-                       /* if this is the right child and its key is smaller
-                        * than the search key then this is the one we want.
-                        */
-                       return node->data;
-               } else {
-                       /* if this is the right child and its key is larger
-                        * than the search key then our parent is the one we
-                        * want.
-                        */
-                       return node->parent->data;
-               }
-       }
-
-}
-
-
 static inline emem_tree_node_t *
 emem_tree_parent(emem_tree_node_t *node)
 {
@@ -1656,7 +1488,7 @@ emem_tree_insert32(emem_tree_t *se_tree, guint32 key, void *data)
 
        /* is this the first node ?*/
        if(!node){
-               node=se_tree->malloc(sizeof(emem_tree_node_t));
+               node=(emem_tree_node_t *)se_tree->malloc(sizeof(emem_tree_node_t));
                switch(se_tree->type){
                case EMEM_TREE_TYPE_RED_BLACK:
                        node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
@@ -1685,7 +1517,7 @@ emem_tree_insert32(emem_tree_t *se_tree, guint32 key, void *data)
                        if(!node->left){
                                /* new node to the left */
                                emem_tree_node_t *new_node;
-                               new_node=se_tree->malloc(sizeof(emem_tree_node_t));
+                               new_node=(emem_tree_node_t *)se_tree->malloc(sizeof(emem_tree_node_t));
                                node->left=new_node;
                                new_node->parent=node;
                                new_node->left=NULL;
@@ -1703,7 +1535,7 @@ emem_tree_insert32(emem_tree_t *se_tree, guint32 key, void *data)
                        if(!node->right){
                                /* new node to the right */
                                emem_tree_node_t *new_node;
-                               new_node=se_tree->malloc(sizeof(emem_tree_node_t));
+                               new_node=(emem_tree_node_t *)se_tree->malloc(sizeof(emem_tree_node_t));
                                node->right=new_node;
                                new_node->parent=node;
                                new_node->left=NULL;
@@ -1728,372 +1560,6 @@ emem_tree_insert32(emem_tree_t *se_tree, guint32 key, void *data)
        }
 }
 
-static void *
-lookup_or_insert32(emem_tree_t *se_tree, guint32 key, void*(*func)(void*),void* ud, int is_subtree)
-{
-       emem_tree_node_t *node;
-
-       node=se_tree->tree;
-
-       /* is this the first node ?*/
-       if(!node){
-               node=se_tree->malloc(sizeof(emem_tree_node_t));
-               switch(se_tree->type){
-                       case EMEM_TREE_TYPE_RED_BLACK:
-                               node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
-                               break;
-               }
-               node->parent=NULL;
-               node->left=NULL;
-               node->right=NULL;
-               node->key32=key;
-               node->data= func(ud);
-               node->u.is_subtree = is_subtree;
-               se_tree->tree=node;
-               return node->data;
-       }
-
-       /* it was not the new root so walk the tree until we find where to
-               * insert this new leaf.
-               */
-       while(1){
-               /* this node already exists, so just return the data pointer*/
-               if(key==node->key32){
-                       return node->data;
-               }
-               if(key<node->key32) {
-                       if(!node->left){
-                               /* new node to the left */
-                               emem_tree_node_t *new_node;
-                               new_node=se_tree->malloc(sizeof(emem_tree_node_t));
-                               node->left=new_node;
-                               new_node->parent=node;
-                               new_node->left=NULL;
-                               new_node->right=NULL;
-                               new_node->key32=key;
-                               new_node->data= func(ud);
-                               new_node->u.is_subtree = is_subtree;
-                               node=new_node;
-                               break;
-                       }
-                       node=node->left;
-                       continue;
-               }
-               if(key>node->key32) {
-                       if(!node->right){
-                               /* new node to the right */
-                               emem_tree_node_t *new_node;
-                               new_node=se_tree->malloc(sizeof(emem_tree_node_t));
-                               node->right=new_node;
-                               new_node->parent=node;
-                               new_node->left=NULL;
-                               new_node->right=NULL;
-                               new_node->key32=key;
-                               new_node->data= func(ud);
-                               new_node->u.is_subtree = is_subtree;
-                               node=new_node;
-                               break;
-                       }
-                       node=node->right;
-                       continue;
-               }
-       }
-
-       /* node will now point to the newly created node */
-       switch(se_tree->type){
-               case EMEM_TREE_TYPE_RED_BLACK:
-                       node->u.rb_color=EMEM_TREE_RB_COLOR_RED;
-                       rb_insert_case1(se_tree, node);
-                       break;
-       }
-
-       return node->data;
-}
-
-/* When the se data is released, this entire tree will dissapear as if it
- * never existed including all metadata associated with the tree.
- */
-emem_tree_t *
-se_tree_create_non_persistent(int type, const char *name)
-{
-       emem_tree_t *tree_list;
-
-       tree_list=se_alloc(sizeof(emem_tree_t));
-       tree_list->next=NULL;
-       tree_list->type=type;
-       tree_list->tree=NULL;
-       tree_list->name=name;
-       tree_list->malloc=se_alloc;
-
-       return tree_list;
-}
-
-/* This tree is PErmanent and will never be released
- */
-emem_tree_t *
-pe_tree_create(int type, const char *name)
-{
-       emem_tree_t *tree_list;
-
-       tree_list=g_new(emem_tree_t, 1);
-       tree_list->next=NULL;
-       tree_list->type=type;
-       tree_list->tree=NULL;
-       tree_list->name=name;
-       tree_list->malloc=(void *(*)(size_t)) g_malloc;
-
-       return tree_list;
-}
-
-/* create another (sub)tree using the same memory allocation scope
- * as the parent tree.
- */
-static emem_tree_t *
-emem_tree_create_subtree(emem_tree_t *parent_tree, const char *name)
-{
-       emem_tree_t *tree_list;
-
-       tree_list=parent_tree->malloc(sizeof(emem_tree_t));
-       tree_list->next=NULL;
-       tree_list->type=parent_tree->type;
-       tree_list->tree=NULL;
-       tree_list->name=name;
-       tree_list->malloc=parent_tree->malloc;
-
-       return tree_list;
-}
-
-static void *
-create_sub_tree(void* d)
-{
-       emem_tree_t *se_tree = d;
-       return emem_tree_create_subtree(se_tree, "subtree");
-}
-
-/* insert a new node in the tree. if this node matches an already existing node
- * then just replace the data for that node */
-
-void
-emem_tree_insert32_array(emem_tree_t *se_tree, emem_tree_key_t *key, void *data)
-{
-       emem_tree_t *insert_tree = NULL;
-       emem_tree_key_t *cur_key;
-       guint32 i, insert_key32 = 0;
-
-       if(!se_tree || !key) return;
-
-       for (cur_key = key; cur_key->length > 0; cur_key++) {
-               if(cur_key->length > 100) {
-                       DISSECTOR_ASSERT_NOT_REACHED();
-               }
-
-               for (i = 0; i < cur_key->length; i++) {
-                       /* Insert using the previous key32 */
-                       if (!insert_tree) {
-                               insert_tree = se_tree;
-                       } else {
-                               insert_tree = lookup_or_insert32(insert_tree, insert_key32, create_sub_tree, se_tree, EMEM_TREE_NODE_IS_SUBTREE);
-                       }
-                       insert_key32 = cur_key->key[i];
-               }
-       }
-
-       if(!insert_tree) {
-               /* We didn't get a valid key. Should we return NULL instead? */
-               DISSECTOR_ASSERT_NOT_REACHED();
-       }
-
-       emem_tree_insert32(insert_tree, insert_key32, data);
-
-}
-
-void *
-emem_tree_lookup32_array(emem_tree_t *se_tree, emem_tree_key_t *key)
-{
-       emem_tree_t *lookup_tree = NULL;
-       emem_tree_key_t *cur_key;
-       guint32 i, lookup_key32 = 0;
-
-       if(!se_tree || !key) return NULL; /* prevent searching on NULL pointer */
-
-       for (cur_key = key; cur_key->length > 0; cur_key++) {
-               if(cur_key->length > 100) {
-                       DISSECTOR_ASSERT_NOT_REACHED();
-               }
-
-               for (i = 0; i < cur_key->length; i++) {
-                       /* Lookup using the previous key32 */
-                       if (!lookup_tree) {
-                               lookup_tree = se_tree;
-                       } else {
-                               lookup_tree = emem_tree_lookup32(lookup_tree, lookup_key32);
-                               if (!lookup_tree) {
-                                       return NULL;
-                               }
-                       }
-                       lookup_key32 = cur_key->key[i];
-               }
-       }
-
-       if(!lookup_tree) {
-               /* We didn't get a valid key. Should we return NULL instead? */
-               DISSECTOR_ASSERT_NOT_REACHED();
-       }
-
-       return emem_tree_lookup32(lookup_tree, lookup_key32);
-}
-
-void *
-emem_tree_lookup32_array_le(emem_tree_t *se_tree, emem_tree_key_t *key)
-{
-       emem_tree_t *lookup_tree = NULL;
-       emem_tree_key_t *cur_key;
-       guint32 i, lookup_key32 = 0;
-
-       if(!se_tree || !key) return NULL; /* prevent searching on NULL pointer */
-
-       for (cur_key = key; cur_key->length > 0; cur_key++) {
-               if(cur_key->length > 100) {
-                       DISSECTOR_ASSERT_NOT_REACHED();
-               }
-
-               for (i = 0; i < cur_key->length; i++) {
-                       /* Lookup using the previous key32 */
-                       if (!lookup_tree) {
-                               lookup_tree = se_tree;
-                       } else {
-                               lookup_tree = emem_tree_lookup32_le(lookup_tree, lookup_key32);
-                               if (!lookup_tree) {
-                                       return NULL;
-                               }
-                       }
-                       lookup_key32 = cur_key->key[i];
-               }
-       }
-
-       if(!lookup_tree) {
-               /* We didn't get a valid key. Should we return NULL instead? */
-               DISSECTOR_ASSERT_NOT_REACHED();
-       }
-
-       return emem_tree_lookup32_le(lookup_tree, lookup_key32);
-
-}
-
-/* Strings are stored as an array of uint32 containing the string characters
-   with 4 characters in each uint32.
-   The first byte of the string is stored as the most significant byte.
-   If the string is not a multiple of 4 characters in length the last
-   uint32 containing the string bytes are padded with 0 bytes.
-   After the uint32's containing the string, there is one final terminator
-   uint32 with the value 0x00000001
-*/
-void
-emem_tree_insert_string(emem_tree_t* se_tree, const gchar* k, void* v, guint32 flags)
-{
-       emem_tree_key_t key[2];
-       guint32 *aligned=NULL;
-       guint32 len = (guint32) strlen(k);
-       guint32 divx = (len+3)/4+1;
-       guint32 i;
-       guint32 tmp;
-
-       aligned = g_malloc(divx * sizeof (guint32));
-
-       /* pack the bytes one one by one into guint32s */
-       tmp = 0;
-       for (i = 0;i < len;i++) {
-               unsigned char ch;
-
-               ch = (unsigned char)k[i];
-               if (flags & EMEM_TREE_STRING_NOCASE) {
-                       if(isupper(ch)) {
-                               ch = tolower(ch);
-                       }
-               }
-               tmp <<= 8;
-               tmp |= ch;
-               if (i%4 == 3) {
-                       aligned[i/4] = tmp;
-                       tmp = 0;
-               }
-       }
-       /* add required padding to the last uint32 */
-       if (i%4 != 0) {
-               while (i%4 != 0) {
-                       i++;
-                       tmp <<= 8;
-               }
-               aligned[i/4-1] = tmp;
-       }
-
-       /* add the terminator */
-       aligned[divx-1] = 0x00000001;
-
-       key[0].length = divx;
-       key[0].key = aligned;
-       key[1].length = 0;
-       key[1].key = NULL;
-
-
-       emem_tree_insert32_array(se_tree, key, v);
-       g_free(aligned);
-}
-
-void *
-emem_tree_lookup_string(emem_tree_t* se_tree, const gchar* k, guint32 flags)
-{
-       emem_tree_key_t key[2];
-       guint32 *aligned=NULL;
-       guint32 len = (guint) strlen(k);
-       guint32 divx = (len+3)/4+1;
-       guint32 i;
-       guint32 tmp;
-       void *ret;
-
-       aligned = g_malloc(divx * sizeof (guint32));
-
-       /* pack the bytes one one by one into guint32s */
-       tmp = 0;
-       for (i = 0;i < len;i++) {
-               unsigned char ch;
-
-               ch = (unsigned char)k[i];
-               if (flags & EMEM_TREE_STRING_NOCASE) {
-                       if(isupper(ch)) {
-                               ch = tolower(ch);
-                       }
-               }
-               tmp <<= 8;
-               tmp |= ch;
-               if (i%4 == 3) {
-                       aligned[i/4] = tmp;
-                       tmp = 0;
-               }
-       }
-       /* add required padding to the last uint32 */
-       if (i%4 != 0) {
-               while (i%4 != 0) {
-                       i++;
-                       tmp <<= 8;
-               }
-               aligned[i/4-1] = tmp;
-       }
-
-       /* add the terminator */
-       aligned[divx-1] = 0x00000001;
-
-       key[0].length = divx;
-       key[0].key = aligned;
-       key[1].length = 0;
-       key[1].key = NULL;
-
-
-       ret = emem_tree_lookup32_array(se_tree, key);
-       g_free(aligned);
-       return ret;
-}
-
 static gboolean
 emem_tree_foreach_nodes(emem_tree_node_t* node, tree_foreach_func callback, void *user_data)
 {
@@ -2110,7 +1576,7 @@ emem_tree_foreach_nodes(emem_tree_node_t* node, tree_foreach_func callback, void
        }
 
        if (node->u.is_subtree == EMEM_TREE_NODE_IS_SUBTREE) {
-               stop_traverse = emem_tree_foreach(node->data, callback, user_data);
+               stop_traverse = emem_tree_foreach((emem_tree_t *)node->data, callback, user_data);
        } else {
                stop_traverse = callback(node->data, user_data);
        }
@@ -2164,14 +1630,14 @@ emem_tree_print_nodes(const char *prefix, emem_tree_node_t* node, guint32 level)
                emem_tree_print_nodes("R-", node->right, level+1);
 
        if (node->u.is_subtree)
-               emem_print_subtree(node->data, level+1);
+               emem_print_subtree((emem_tree_t *)node->data, level+1);
 }
 
 static void
 emem_print_subtree(emem_tree_t* emem_tree, guint32 level)
 {
        guint32 i;
-        
+
        if (!emem_tree)
                return;
 
@@ -2179,7 +1645,7 @@ emem_print_subtree(emem_tree_t* emem_tree, guint32 level)
                printf("    ");
        }
 
-       printf("EMEM tree:%p type:%s name:%s root:%p\n",emem_tree,(emem_tree->type==1)?"RedBlack":"unknown",emem_tree->name,(void *)(emem_tree->tree));
+       printf("EMEM tree:%p type:%s name:%s root:%p\n",(void *)emem_tree,(emem_tree->type==1)?"RedBlack":"unknown",emem_tree->name,(void *)(emem_tree->tree));
        if(emem_tree->tree)
                emem_tree_print_nodes("Root-", emem_tree->tree, level);
 }
@@ -2231,7 +1697,7 @@ ep_strbuf_grow(emem_strbuf_t *strbuf, gsize wanted_alloc_len)
        }
 
        new_alloc_len = next_size(strbuf->alloc_len, wanted_alloc_len, strbuf->max_alloc_len);
-       new_str = ep_alloc(new_alloc_len);
+       new_str = (gchar *)ep_alloc(new_alloc_len);
        g_strlcpy(new_str, strbuf->str, new_alloc_len);
 
        strbuf->alloc_len = new_alloc_len;
@@ -2243,7 +1709,7 @@ ep_strbuf_sized_new(gsize alloc_len, gsize max_alloc_len)
 {
        emem_strbuf_t *strbuf;
 
-       strbuf = ep_alloc(sizeof(emem_strbuf_t));
+       strbuf = ep_new(emem_strbuf_t);
 
        if ((max_alloc_len == 0) || (max_alloc_len > MAX_STRBUF_LEN))
                max_alloc_len = MAX_STRBUF_LEN;
@@ -2252,7 +1718,7 @@ ep_strbuf_sized_new(gsize alloc_len, gsize max_alloc_len)
        else if (alloc_len > max_alloc_len)
                alloc_len = max_alloc_len;
 
-       strbuf->str = ep_alloc(alloc_len);
+       strbuf->str = (char *)ep_alloc(alloc_len);
        strbuf->str[0] = '\0';
 
        strbuf->len = 0;