2 * Wireshark memory management and garbage collection functions
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 #ifdef HAVE_SYS_TIME_H
50 #include <windows.h> /* VirtualAlloc, VirtualProtect */
51 #include <process.h> /* getpid */
56 * Tools like Valgrind and ElectricFence don't work well with memchunks.
57 * Uncomment the defines below to make {ep|se}_alloc() allocate each
58 * object individually.
60 /* #define EP_DEBUG_FREE 1 */
61 /* #define SE_DEBUG_FREE 1 */
63 /* Do we want to use guardpages? if available */
64 #define WANT_GUARD_PAGES 1
66 /* Do we want to use canaries ? */
67 #define DEBUG_USE_CANARIES 1
69 #ifdef WANT_GUARD_PAGES
70 /* Add guard pages at each end of our allocated memory */
71 #if defined(HAVE_SYSCONF) && defined(HAVE_MMAP) && defined(HAVE_MPROTECT) && defined(HAVE_STDINT_H)
73 #include <sys/types.h>
75 #if defined(MAP_ANONYMOUS)
76 #define ANON_PAGE_MODE (MAP_ANONYMOUS|MAP_PRIVATE)
77 #elif defined(MAP_ANON)
78 #define ANON_PAGE_MODE (MAP_ANON|MAP_PRIVATE)
80 #define ANON_PAGE_MODE (MAP_PRIVATE) /* have to map /dev/zero */
85 static int dev_zero_fd;
86 #define ANON_FD dev_zero_fd
90 #define USE_GUARD_PAGES 1
94 /* When required, allocate more memory from the OS in this size chunks */
95 #define EMEM_PACKET_CHUNK_SIZE 10485760
97 /* The maximum number of allocations per chunk */
98 #define EMEM_ALLOCS_PER_CHUNK (EMEM_PACKET_CHUNK_SIZE / 512)
101 #ifdef DEBUG_USE_CANARIES
102 #define EMEM_CANARY_SIZE 8
103 #define EMEM_CANARY_DATA_SIZE (EMEM_CANARY_SIZE * 2 - 1)
105 /* this should be static, but if it were gdb would had problems finding it */
106 guint8 ep_canary[EMEM_CANARY_DATA_SIZE], se_canary[EMEM_CANARY_DATA_SIZE];
107 #endif /* DEBUG_USE_CANARIES */
109 typedef struct _emem_chunk_t {
110 struct _emem_chunk_t *next;
111 unsigned int amount_free_init;
112 unsigned int amount_free;
113 unsigned int free_offset_init;
114 unsigned int free_offset;
116 #ifdef DEBUG_USE_CANARIES
117 #if ! defined(EP_DEBUG_FREE) && ! defined(SE_DEBUG_FREE)
118 unsigned int c_count;
119 void *canary[EMEM_ALLOCS_PER_CHUNK];
120 guint8 cmp_len[EMEM_ALLOCS_PER_CHUNK];
122 #endif /* DEBUG_USE_CANARIES */
125 typedef struct _emem_header_t {
126 emem_chunk_t *free_list;
127 emem_chunk_t *used_list;
130 static emem_header_t ep_packet_mem;
131 static emem_header_t se_packet_mem;
133 #if !defined(SE_DEBUG_FREE)
135 static SYSTEM_INFO sysinfo;
136 static OSVERSIONINFO versinfo;
138 #elif defined(USE_GUARD_PAGES)
139 static intptr_t pagesize;
140 #endif /* _WIN32 / USE_GUARD_PAGES */
141 #endif /* SE_DEBUG_FREE */
143 #ifdef DEBUG_USE_CANARIES
145 * Set a canary value to be placed between memchunks.
148 emem_canary(guint8 *canary) {
150 static GRand *rand_state = NULL;
152 if (rand_state == NULL) {
153 rand_state = g_rand_new();
155 for (i = 0; i < EMEM_CANARY_DATA_SIZE; i ++) {
156 canary[i] = (guint8) g_rand_int(rand_state);
161 #if !defined(SE_DEBUG_FREE)
163 * Given an allocation size, return the amount of padding needed for
167 emem_canary_pad (size_t allocation) {
170 pad = EMEM_CANARY_SIZE - (allocation % EMEM_CANARY_SIZE);
171 if (pad < EMEM_CANARY_SIZE)
172 pad += EMEM_CANARY_SIZE;
177 #endif /* DEBUG_USE_CANARIES */
179 /* used for debugging canaries, will block */
180 #ifdef DEBUG_INTENSE_CANARY_CHECKS
181 gboolean intense_canary_checking = FALSE;
183 /* used to intensivelly check ep canaries
185 void ep_check_canary_integrity(const char* fmt, ...) {
187 static gchar there[128] = {
188 'L','a','u','n','c','h',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
189 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
190 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
191 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
193 emem_chunk_t* npc = NULL;
195 if (! intense_canary_checking ) return;
201 g_vsnprintf(here, 126,fmt, ap);
204 for (npc = ep_packet_mem.free_list; npc != NULL; npc = npc->next) {
205 static unsigned i_ctr;
207 if (npc->c_count > 0x00ffffff) {
208 g_error("ep_packet_mem.free_list was corrupted\nbetween: %s\nand: %s",there, here);
211 for (i_ctr = 0; i_ctr < npc->c_count; i_ctr++) {
212 if (memcmp(npc->canary[i_ctr], &ep_canary, npc->cmp_len[i_ctr]) != 0) {
213 g_error("Per-packet memory corrupted\nbetween: %s\nand: %s",there, here);
218 strncpy(there,here,126);
224 /* Initialize the packet-lifetime memory allocation pool.
225 * This function should be called only once when Wireshark or TShark starts
231 ep_packet_mem.free_list=NULL;
232 ep_packet_mem.used_list=NULL;
234 #ifdef DEBUG_INTENSE_CANARY_CHECKS
235 intense_canary_checking = (gboolean)getenv("WIRESHARK_DEBUG_EP_CANARY");
238 #ifdef DEBUG_USE_CANARIES
239 emem_canary(ep_canary);
240 #endif /* DEBUG_USE_CANARIES */
242 #if !defined(SE_DEBUG_FREE)
244 /* Set up our guard page info for Win32 */
245 GetSystemInfo(&sysinfo);
246 pagesize = sysinfo.dwPageSize;
248 /* calling GetVersionEx using the OSVERSIONINFO structure.
249 * OSVERSIONINFOEX requires Win NT4 with SP6 or newer NT Versions.
250 * OSVERSIONINFOEX will fail on Win9x and older NT Versions.
252 * http://msdn.microsoft.com/library/en-us/sysinfo/base/getversionex.asp
253 * http://msdn.microsoft.com/library/en-us/sysinfo/base/osversioninfo_str.asp
254 * http://msdn.microsoft.com/library/en-us/sysinfo/base/osversioninfoex_str.asp
256 versinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
257 GetVersionEx(&versinfo);
259 #elif defined(USE_GUARD_PAGES)
260 pagesize = sysconf(_SC_PAGESIZE);
262 dev_zero_fd = ws_open("/dev/zero", O_RDWR);
263 g_assert(dev_zero_fd != -1);
265 #endif /* _WIN32 / USE_GUARD_PAGES */
266 #endif /* SE_DEBUG_FREE */
270 /* Initialize the capture-lifetime memory allocation pool.
271 * This function should be called only once when Wireshark or TShark starts
277 se_packet_mem.free_list=NULL;
278 se_packet_mem.used_list=NULL;
280 #ifdef DEBUG_USE_CANARIES
281 emem_canary(se_canary);
282 #endif /* DEBUG_USE_CANARIES */
285 #if !defined(SE_DEBUG_FREE)
287 emem_create_chunk(emem_chunk_t **free_list) {
290 char *buf_end, *prot1, *prot2;
292 #elif defined(USE_GUARD_PAGES)
294 char *buf_end, *prot1, *prot2;
295 #endif /* _WIN32 / USE_GUARD_PAGES */
296 /* we dont have any free data, so we must allocate a new one */
299 npc = g_malloc(sizeof(emem_chunk_t));
301 #ifdef DEBUG_USE_CANARIES
302 #if ! defined(EP_DEBUG_FREE) && ! defined(SE_DEBUG_FREE)
305 #endif /* DEBUG_USE_CANARIES */
310 * MSDN documents VirtualAlloc/VirtualProtect at
311 * http://msdn.microsoft.com/library/en-us/memory/base/creating_guard_pages.asp
314 /* XXX - is MEM_COMMIT|MEM_RESERVE correct? */
315 npc->buf = VirtualAlloc(NULL, EMEM_PACKET_CHUNK_SIZE,
316 MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
317 if(npc->buf == NULL) {
318 THROW(OutOfMemoryError);
320 buf_end = npc->buf + EMEM_PACKET_CHUNK_SIZE;
322 /* Align our guard pages on page-sized boundaries */
323 prot1 = (char *) ((((int) npc->buf + pagesize - 1) / pagesize) * pagesize);
324 prot2 = (char *) ((((int) buf_end - (1 * pagesize)) / pagesize) * pagesize);
326 ret = VirtualProtect(prot1, pagesize, PAGE_NOACCESS, &oldprot);
327 g_assert(ret != 0 || versinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
328 ret = VirtualProtect(prot2, pagesize, PAGE_NOACCESS, &oldprot);
329 g_assert(ret != 0 || versinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
331 npc->amount_free_init = prot2 - prot1 - pagesize;
332 npc->amount_free = npc->amount_free_init;
333 npc->free_offset_init = (prot1 - npc->buf) + pagesize;
334 npc->free_offset = npc->free_offset_init;
336 #elif defined(USE_GUARD_PAGES)
337 npc->buf = mmap(NULL, EMEM_PACKET_CHUNK_SIZE,
338 PROT_READ|PROT_WRITE, ANON_PAGE_MODE, ANON_FD, 0);
339 if(npc->buf == MAP_FAILED) {
340 /* XXX - what do we have to cleanup here? */
341 THROW(OutOfMemoryError);
343 buf_end = npc->buf + EMEM_PACKET_CHUNK_SIZE;
345 /* Align our guard pages on page-sized boundaries */
346 prot1 = (char *) ((((intptr_t) npc->buf + pagesize - 1) / pagesize) * pagesize);
347 prot2 = (char *) ((((intptr_t) buf_end - (1 * pagesize)) / pagesize) * pagesize);
348 ret = mprotect(prot1, pagesize, PROT_NONE);
350 ret = mprotect(prot2, pagesize, PROT_NONE);
353 npc->amount_free_init = prot2 - prot1 - pagesize;
354 npc->amount_free = npc->amount_free_init;
355 npc->free_offset_init = (prot1 - npc->buf) + pagesize;
356 npc->free_offset = npc->free_offset_init;
358 #else /* Is there a draft in here? */
359 npc->buf = malloc(EMEM_PACKET_CHUNK_SIZE);
360 if(npc->buf == NULL) {
361 THROW(OutOfMemoryError);
363 npc->amount_free_init = EMEM_PACKET_CHUNK_SIZE;
364 npc->amount_free = npc->amount_free_init;
365 npc->free_offset_init = 0;
366 npc->free_offset = npc->free_offset_init;
367 #endif /* USE_GUARD_PAGES */
372 /* allocate 'size' amount of memory with an allocation lifetime until the
376 ep_alloc(size_t size)
379 #ifndef EP_DEBUG_FREE
380 #ifdef DEBUG_USE_CANARIES
382 guint8 pad = emem_canary_pad(size);
385 #endif /* DEBUG_USE_CANARIES */
386 emem_chunk_t *free_list;
389 #ifndef EP_DEBUG_FREE
390 /* Round up to an 8 byte boundary. Make sure we have at least
391 * 8 pad bytes for our canary.
395 /* make sure we dont try to allocate too much (arbitrary limit) */
396 DISSECTOR_ASSERT(size<(EMEM_PACKET_CHUNK_SIZE>>2));
398 emem_create_chunk(&ep_packet_mem.free_list);
400 /* oops, we need to allocate more memory to serve this request
401 * than we have free. move this node to the used list and try again
403 if(size>ep_packet_mem.free_list->amount_free
404 #ifdef DEBUG_USE_CANARIES
405 || ep_packet_mem.free_list->c_count >= EMEM_ALLOCS_PER_CHUNK
406 #endif /* DEBUG_USE_CANARIES */
409 npc=ep_packet_mem.free_list;
410 ep_packet_mem.free_list=ep_packet_mem.free_list->next;
411 npc->next=ep_packet_mem.used_list;
412 ep_packet_mem.used_list=npc;
415 emem_create_chunk(&ep_packet_mem.free_list);
417 free_list = ep_packet_mem.free_list;
419 buf = free_list->buf + free_list->free_offset;
421 free_list->amount_free -= size;
422 free_list->free_offset += size;
424 #ifdef DEBUG_USE_CANARIES
425 cptr = (char *)buf + size - pad;
426 memcpy(cptr, &ep_canary, pad);
427 free_list->canary[free_list->c_count] = cptr;
428 free_list->cmp_len[free_list->c_count] = pad;
429 free_list->c_count++;
430 #endif /* DEBUG_USE_CANARIES */
432 #else /* EP_DEBUG_FREE */
435 npc=g_malloc(sizeof(emem_chunk_t));
436 npc->next=ep_packet_mem.used_list;
437 npc->amount_free=size;
439 npc->buf=g_malloc(size);
441 ep_packet_mem.used_list=npc;
442 #endif /* EP_DEBUG_FREE */
446 /* allocate 'size' amount of memory with an allocation lifetime until the
450 se_alloc(size_t size)
453 #ifndef SE_DEBUG_FREE
454 #ifdef DEBUG_USE_CANARIES
456 guint8 pad = emem_canary_pad(size);
459 #endif /* DEBUG_USE_CANARIES */
460 emem_chunk_t *free_list;
463 #ifndef SE_DEBUG_FREE
464 /* Round up to an 8 byte boundary. Make sure we have at least
465 * 8 pad bytes for our canary.
469 /* make sure we dont try to allocate too much (arbitrary limit) */
470 DISSECTOR_ASSERT(size<(EMEM_PACKET_CHUNK_SIZE>>2));
472 emem_create_chunk(&se_packet_mem.free_list);
474 /* oops, we need to allocate more memory to serve this request
475 * than we have free. move this node to the used list and try again
477 if(size>se_packet_mem.free_list->amount_free
478 #ifdef DEBUG_USE_CANARIES
479 || se_packet_mem.free_list->c_count >= EMEM_ALLOCS_PER_CHUNK
480 #endif /* DEBUG_USE_CANARIES */
483 npc=se_packet_mem.free_list;
484 se_packet_mem.free_list=se_packet_mem.free_list->next;
485 npc->next=se_packet_mem.used_list;
486 se_packet_mem.used_list=npc;
489 emem_create_chunk(&se_packet_mem.free_list);
491 free_list = se_packet_mem.free_list;
493 buf = free_list->buf + free_list->free_offset;
495 free_list->amount_free -= size;
496 free_list->free_offset += size;
498 #ifdef DEBUG_USE_CANARIES
499 cptr = (char *)buf + size - pad;
500 memcpy(cptr, &se_canary, pad);
501 free_list->canary[free_list->c_count] = cptr;
502 free_list->cmp_len[free_list->c_count] = pad;
503 free_list->c_count++;
504 #endif /* DEBUG_USE_CANARIES */
506 #else /* SE_DEBUG_FREE */
509 npc=g_malloc(sizeof(emem_chunk_t));
510 npc->next=se_packet_mem.used_list;
511 npc->amount_free=size;
513 npc->buf=g_malloc(size);
515 se_packet_mem.used_list=npc;
516 #endif /* SE_DEBUG_FREE */
522 void* ep_alloc0(size_t size) {
523 return memset(ep_alloc(size),'\0',size);
526 gchar* ep_strdup(const gchar* src) {
527 guint len = strlen(src);
530 dst = strncpy(ep_alloc(len+1), src, len);
537 gchar* ep_strndup(const gchar* src, size_t len) {
538 gchar* dst = ep_alloc(len+1);
541 for (i = 0; (i < len) && src[i]; i++)
549 void* ep_memdup(const void* src, size_t len) {
550 return memcpy(ep_alloc(len), src, len);
553 gchar* ep_strdup_vprintf(const gchar* fmt, va_list ap) {
560 len = g_printf_string_upper_bound(fmt, ap);
562 dst = ep_alloc(len+1);
563 g_vsnprintf (dst, len, fmt, ap2);
569 gchar* ep_strdup_printf(const gchar* fmt, ...) {
574 dst = ep_strdup_vprintf(fmt, ap);
579 gchar** ep_strsplit(const gchar* string, const gchar* sep, int max_tokens) {
587 enum { AT_START, IN_PAD, IN_TOKEN } state;
595 s = splitted = ep_strdup(string);
596 str_len = strlen(splitted);
597 sep_len = strlen(sep);
599 if (max_tokens < 1) max_tokens = INT_MAX;
604 while (tokens <= (guint)max_tokens && ( s = strstr(s,sep) )) {
607 for(i=0; i < sep_len; i++ )
614 vec = ep_alloc_array(gchar*,tokens+1);
617 for (i=0; i< str_len; i++) {
620 switch(splitted[i]) {
625 vec[curr_tok] = &(splitted[i]);
631 switch(splitted[i]) {
638 switch(splitted[i]) {
640 vec[curr_tok] = &(splitted[i]);
649 vec[curr_tok] = NULL;
656 void* se_alloc0(size_t size) {
657 return memset(se_alloc(size),'\0',size);
660 /* If str is NULL, just return the string "<NULL>" so that the callers dont
661 * have to bother checking it.
663 gchar* se_strdup(const gchar* src) {
672 dst = strncpy(se_alloc(len+1), src, len);
679 gchar* se_strndup(const gchar* src, size_t len) {
680 gchar* dst = se_alloc(len+1);
683 for (i = 0; (i < len) && src[i]; i++)
691 void* se_memdup(const void* src, size_t len) {
692 return memcpy(se_alloc(len), src, len);
695 gchar* se_strdup_vprintf(const gchar* fmt, va_list ap) {
702 len = g_printf_string_upper_bound(fmt, ap);
704 dst = se_alloc(len+1);
705 g_vsnprintf (dst, len, fmt, ap2);
711 gchar* se_strdup_printf(const gchar* fmt, ...) {
716 dst = se_strdup_vprintf(fmt, ap);
722 /* release all allocated memory back to the pool.
728 #ifndef EP_DEBUG_FREE
729 #ifdef DEBUG_USE_CANARIES
731 #endif /* DEBUG_USE_CANARIES */
734 /* move all used chunks over to the free list */
735 while(ep_packet_mem.used_list){
736 npc=ep_packet_mem.used_list;
737 ep_packet_mem.used_list=ep_packet_mem.used_list->next;
738 npc->next=ep_packet_mem.free_list;
739 ep_packet_mem.free_list=npc;
742 /* clear them all out */
743 npc = ep_packet_mem.free_list;
744 while (npc != NULL) {
745 #ifndef EP_DEBUG_FREE
746 #ifdef DEBUG_USE_CANARIES
747 for (i = 0; i < npc->c_count; i++) {
748 if (memcmp(npc->canary[i], &ep_canary, npc->cmp_len[i]) != 0)
749 g_error("Per-packet memory corrupted.");
752 #endif /* DEBUG_USE_CANARIES */
753 npc->amount_free = npc->amount_free_init;
754 npc->free_offset = npc->free_offset_init;
756 #else /* EP_DEBUG_FREE */
757 emem_chunk_t *next = npc->next;
762 #endif /* EP_DEBUG_FREE */
769 /* release all allocated memory back to the pool.
775 emem_tree_t *se_tree_list;
776 #ifndef SE_DEBUG_FREE
777 #ifdef DEBUG_USE_CANARIES
779 #endif /* DEBUG_USE_CANARIES */
782 /* move all used chunks over to the free list */
783 while(se_packet_mem.used_list){
784 npc=se_packet_mem.used_list;
785 se_packet_mem.used_list=se_packet_mem.used_list->next;
786 npc->next=se_packet_mem.free_list;
787 se_packet_mem.free_list=npc;
790 /* clear them all out */
791 npc = se_packet_mem.free_list;
792 while (npc != NULL) {
793 #ifndef SE_DEBUG_FREE
794 #ifdef DEBUG_USE_CANARIES
795 for (i = 0; i < npc->c_count; i++) {
796 if (memcmp(npc->canary[i], &se_canary, npc->cmp_len[i]) != 0)
797 g_error("Per-session memory corrupted.");
800 #endif /* DEBUG_USE_CANARIES */
801 npc->amount_free = npc->amount_free_init;
802 npc->free_offset = npc->free_offset_init;
804 #else /* SE_DEBUG_FREE */
805 emem_chunk_t *next = npc->next;
810 #endif /* SE_DEBUG_FREE */
817 /* release/reset all se allocated trees */
818 for(se_tree_list=se_trees;se_tree_list;se_tree_list=se_tree_list->next){
819 se_tree_list->tree=NULL;
824 ep_stack_t ep_stack_new(void) {
825 ep_stack_t s = ep_new(struct _ep_stack_frame_t*);
826 *s = ep_new0(struct _ep_stack_frame_t);
830 /* for ep_stack_t we'll keep the popped frames so we reuse them instead
831 of allocating new ones.
835 void* ep_stack_push(ep_stack_t stack, void* data) {
836 struct _ep_stack_frame_t* frame;
837 struct _ep_stack_frame_t* head = (*stack);
842 frame = ep_new(struct _ep_stack_frame_t);
848 frame->payload = data;
854 void* ep_stack_pop(ep_stack_t stack) {
856 if ((*stack)->below) {
857 (*stack) = (*stack)->below;
858 return (*stack)->above->payload;
867 void print_tree_item(emem_tree_node_t *node, int level){
869 for(i=0;i<level;i++){
872 printf("%s KEY:0x%08x node:0x%08x parent:0x%08x left:0x%08x right:0x%08x\n",node->u.rb_color==EMEM_TREE_RB_COLOR_BLACK?"BLACK":"RED",node->key32,(int)node,(int)node->parent,(int)node->left,(int)node->right);
874 print_tree_item(node->left,level+1);
876 print_tree_item(node->right,level+1);
879 void print_tree(emem_tree_node_t *node){
886 print_tree_item(node,0);
892 /* routines to manage se allocated red-black trees */
893 emem_tree_t *se_trees=NULL;
896 se_tree_create(int type, const char *name)
898 emem_tree_t *tree_list;
900 tree_list=malloc(sizeof(emem_tree_t));
901 tree_list->next=se_trees;
902 tree_list->type=type;
903 tree_list->tree=NULL;
904 tree_list->name=name;
905 tree_list->malloc=se_alloc;
914 emem_tree_lookup32(emem_tree_t *se_tree, guint32 key)
916 emem_tree_node_t *node;
921 if(key==node->key32){
937 emem_tree_lookup32_le(emem_tree_t *se_tree, guint32 key)
939 emem_tree_node_t *node;
949 if(key==node->key32){
971 /* If we are still at the root of the tree this means that this node
972 * is either smaller thant the search key and then we return this
973 * node or else there is no smaller key availabel and then
984 if(node->parent->left==node){
988 /* if this is a left child and its key is smaller than
989 * the search key, then this is the node we want.
993 /* if this is a left child and its key is bigger than
994 * the search key, we have to check if any
995 * of our ancestors are smaller than the search key.
1008 if(node->key32<key){
1009 /* if this is the right child and its key is smaller
1010 * than the search key then this is the one we want.
1014 /* if this is the right child and its key is larger
1015 * than the search key then our parent is the one we
1018 return node->parent->data;
1025 static inline emem_tree_node_t *
1026 emem_tree_parent(emem_tree_node_t *node)
1028 return node->parent;
1031 static inline emem_tree_node_t *
1032 emem_tree_grandparent(emem_tree_node_t *node)
1034 emem_tree_node_t *parent;
1036 parent=emem_tree_parent(node);
1038 return parent->parent;
1042 static inline emem_tree_node_t *
1043 emem_tree_uncle(emem_tree_node_t *node)
1045 emem_tree_node_t *parent, *grandparent;
1047 parent=emem_tree_parent(node);
1051 grandparent=emem_tree_parent(parent);
1055 if(parent==grandparent->left){
1056 return grandparent->right;
1058 return grandparent->left;
1061 static inline void rb_insert_case1(emem_tree_t *se_tree, emem_tree_node_t *node);
1062 static inline void rb_insert_case2(emem_tree_t *se_tree, emem_tree_node_t *node);
1065 rotate_left(emem_tree_t *se_tree, emem_tree_node_t *node)
1068 if(node->parent->left==node){
1069 node->parent->left=node->right;
1071 node->parent->right=node->right;
1074 se_tree->tree=node->right;
1076 node->right->parent=node->parent;
1077 node->parent=node->right;
1078 node->right=node->right->left;
1080 node->right->parent=node;
1082 node->parent->left=node;
1086 rotate_right(emem_tree_t *se_tree, emem_tree_node_t *node)
1089 if(node->parent->left==node){
1090 node->parent->left=node->left;
1092 node->parent->right=node->left;
1095 se_tree->tree=node->left;
1097 node->left->parent=node->parent;
1098 node->parent=node->left;
1099 node->left=node->left->right;
1101 node->left->parent=node;
1103 node->parent->right=node;
1107 rb_insert_case5(emem_tree_t *se_tree, emem_tree_node_t *node)
1109 emem_tree_node_t *grandparent;
1110 emem_tree_node_t *parent;
1112 parent=emem_tree_parent(node);
1113 grandparent=emem_tree_parent(parent);
1114 parent->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1115 grandparent->u.rb_color=EMEM_TREE_RB_COLOR_RED;
1116 if( (node==parent->left) && (parent==grandparent->left) ){
1117 rotate_right(se_tree, grandparent);
1119 rotate_left(se_tree, grandparent);
1124 rb_insert_case4(emem_tree_t *se_tree, emem_tree_node_t *node)
1126 emem_tree_node_t *grandparent;
1127 emem_tree_node_t *parent;
1129 parent=emem_tree_parent(node);
1130 grandparent=emem_tree_parent(parent);
1134 if( (node==parent->right) && (parent==grandparent->left) ){
1135 rotate_left(se_tree, parent);
1137 } else if( (node==parent->left) && (parent==grandparent->right) ){
1138 rotate_right(se_tree, parent);
1141 rb_insert_case5(se_tree, node);
1145 rb_insert_case3(emem_tree_t *se_tree, emem_tree_node_t *node)
1147 emem_tree_node_t *grandparent;
1148 emem_tree_node_t *parent;
1149 emem_tree_node_t *uncle;
1151 uncle=emem_tree_uncle(node);
1152 if(uncle && (uncle->u.rb_color==EMEM_TREE_RB_COLOR_RED)){
1153 parent=emem_tree_parent(node);
1154 parent->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1155 uncle->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1156 grandparent=emem_tree_grandparent(node);
1157 grandparent->u.rb_color=EMEM_TREE_RB_COLOR_RED;
1158 rb_insert_case1(se_tree, grandparent);
1160 rb_insert_case4(se_tree, node);
1165 rb_insert_case2(emem_tree_t *se_tree, emem_tree_node_t *node)
1167 emem_tree_node_t *parent;
1169 parent=emem_tree_parent(node);
1170 /* parent is always non-NULL here */
1171 if(parent->u.rb_color==EMEM_TREE_RB_COLOR_BLACK){
1174 rb_insert_case3(se_tree, node);
1178 rb_insert_case1(emem_tree_t *se_tree, emem_tree_node_t *node)
1180 emem_tree_node_t *parent;
1182 parent=emem_tree_parent(node);
1184 node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1187 rb_insert_case2(se_tree, node);
1190 /* insert a new node in the tree. if this node matches an already existing node
1191 * then just replace the data for that node */
1193 emem_tree_insert32(emem_tree_t *se_tree, guint32 key, void *data)
1195 emem_tree_node_t *node;
1199 /* is this the first node ?*/
1201 node=se_tree->malloc(sizeof(emem_tree_node_t));
1202 switch(se_tree->type){
1203 case EMEM_TREE_TYPE_RED_BLACK:
1204 node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1212 node->u.is_subtree = EMEM_TREE_NODE_IS_DATA;
1217 /* it was not the new root so walk the tree until we find where to
1218 * insert this new leaf.
1221 /* this node already exists, so just replace the data pointer*/
1222 if(key==node->key32){
1226 if(key<node->key32) {
1228 /* new node to the left */
1229 emem_tree_node_t *new_node;
1230 new_node=se_tree->malloc(sizeof(emem_tree_node_t));
1231 node->left=new_node;
1232 new_node->parent=node;
1233 new_node->left=NULL;
1234 new_node->right=NULL;
1235 new_node->key32=key;
1236 new_node->data=data;
1237 new_node->u.is_subtree=EMEM_TREE_NODE_IS_DATA;
1244 if(key>node->key32) {
1246 /* new node to the right */
1247 emem_tree_node_t *new_node;
1248 new_node=se_tree->malloc(sizeof(emem_tree_node_t));
1249 node->right=new_node;
1250 new_node->parent=node;
1251 new_node->left=NULL;
1252 new_node->right=NULL;
1253 new_node->key32=key;
1254 new_node->data=data;
1255 new_node->u.is_subtree=EMEM_TREE_NODE_IS_DATA;
1264 /* node will now point to the newly created node */
1265 switch(se_tree->type){
1266 case EMEM_TREE_TYPE_RED_BLACK:
1267 node->u.rb_color=EMEM_TREE_RB_COLOR_RED;
1268 rb_insert_case1(se_tree, node);
1273 static void* lookup_or_insert32(emem_tree_t *se_tree, guint32 key, void*(*func)(void*),void* ud, int is_subtree) {
1274 emem_tree_node_t *node;
1278 /* is this the first node ?*/
1280 node=se_tree->malloc(sizeof(emem_tree_node_t));
1281 switch(se_tree->type){
1282 case EMEM_TREE_TYPE_RED_BLACK:
1283 node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1290 node->data= func(ud);
1291 node->u.is_subtree = is_subtree;
1296 /* it was not the new root so walk the tree until we find where to
1297 * insert this new leaf.
1300 /* this node already exists, so just return the data pointer*/
1301 if(key==node->key32){
1304 if(key<node->key32) {
1306 /* new node to the left */
1307 emem_tree_node_t *new_node;
1308 new_node=se_tree->malloc(sizeof(emem_tree_node_t));
1309 node->left=new_node;
1310 new_node->parent=node;
1311 new_node->left=NULL;
1312 new_node->right=NULL;
1313 new_node->key32=key;
1314 new_node->data= func(ud);
1315 new_node->u.is_subtree = is_subtree;
1322 if(key>node->key32) {
1324 /* new node to the right */
1325 emem_tree_node_t *new_node;
1326 new_node=se_tree->malloc(sizeof(emem_tree_node_t));
1327 node->right=new_node;
1328 new_node->parent=node;
1329 new_node->left=NULL;
1330 new_node->right=NULL;
1331 new_node->key32=key;
1332 new_node->data= func(ud);
1333 new_node->u.is_subtree = is_subtree;
1342 /* node will now point to the newly created node */
1343 switch(se_tree->type){
1344 case EMEM_TREE_TYPE_RED_BLACK:
1345 node->u.rb_color=EMEM_TREE_RB_COLOR_RED;
1346 rb_insert_case1(se_tree, node);
1353 /* When the se data is released, this entire tree will dissapear as if it
1354 * never existed including all metadata associated with the tree.
1357 se_tree_create_non_persistent(int type, const char *name)
1359 emem_tree_t *tree_list;
1361 tree_list=se_alloc(sizeof(emem_tree_t));
1362 tree_list->next=NULL;
1363 tree_list->type=type;
1364 tree_list->tree=NULL;
1365 tree_list->name=name;
1366 tree_list->malloc=se_alloc;
1371 /* This tree is PErmanent and will never be released
1374 pe_tree_create(int type, const char *name)
1376 emem_tree_t *tree_list;
1378 tree_list=g_malloc(sizeof(emem_tree_t));
1379 tree_list->next=NULL;
1380 tree_list->type=type;
1381 tree_list->tree=NULL;
1382 tree_list->name=name;
1383 tree_list->malloc=(void *(*)(size_t)) g_malloc;
1388 /* create another (sub)tree using the same memory allocation scope
1389 * as the parent tree.
1391 static emem_tree_t *
1392 emem_tree_create_subtree(emem_tree_t *parent_tree, const char *name)
1394 emem_tree_t *tree_list;
1396 tree_list=parent_tree->malloc(sizeof(emem_tree_t));
1397 tree_list->next=NULL;
1398 tree_list->type=parent_tree->type;
1399 tree_list->tree=NULL;
1400 tree_list->name=name;
1401 tree_list->malloc=parent_tree->malloc;
1406 static void* create_sub_tree(void* d) {
1407 emem_tree_t *se_tree = d;
1408 return emem_tree_create_subtree(se_tree, "subtree");
1411 /* insert a new node in the tree. if this node matches an already existing node
1412 * then just replace the data for that node */
1415 emem_tree_insert32_array(emem_tree_t *se_tree, emem_tree_key_t *key, void *data)
1417 emem_tree_t *next_tree;
1419 if((key[0].length<1)||(key[0].length>100)){
1420 DISSECTOR_ASSERT_NOT_REACHED();
1422 if((key[0].length==1)&&(key[1].length==0)){
1423 emem_tree_insert32(se_tree, *key[0].key, data);
1427 next_tree=lookup_or_insert32(se_tree, *key[0].key, create_sub_tree, se_tree, EMEM_TREE_NODE_IS_SUBTREE);
1429 if(key[0].length==1){
1435 emem_tree_insert32_array(next_tree, key, data);
1439 emem_tree_lookup32_array(emem_tree_t *se_tree, emem_tree_key_t *key)
1441 emem_tree_t *next_tree;
1443 if((key[0].length<1)||(key[0].length>100)){
1444 DISSECTOR_ASSERT_NOT_REACHED();
1446 if((key[0].length==1)&&(key[1].length==0)){
1447 return emem_tree_lookup32(se_tree, *key[0].key);
1449 next_tree=emem_tree_lookup32(se_tree, *key[0].key);
1453 if(key[0].length==1){
1459 return emem_tree_lookup32_array(next_tree, key);
1463 /* Strings are stored as an array of uint32 containing the string characters
1464 with 4 characters in each uint32.
1465 The first byte of the string is stored as the most significant byte.
1466 If the string is not a multiple of 4 characters in length the last
1467 uint32 containing the string bytes are padded with 0 bytes.
1468 After the uint32's containing the string, there is one final terminator
1469 uint32 with the value 0x00000001
1472 emem_tree_insert_string(emem_tree_t* se_tree, const gchar* k, void* v, guint32 flags)
1474 emem_tree_key_t key[2];
1475 guint32 *aligned=NULL;
1476 guint32 len = strlen(k);
1477 guint32 div = (len+3)/4+1;
1481 aligned = malloc(div * sizeof (guint32));
1483 /* pack the bytes one one by one into guint32s */
1485 for (i = 0;i < len;i++) {
1488 ch = (unsigned char)k[i];
1489 if (flags & EMEM_TREE_STRING_NOCASE) {
1501 /* add required padding to the last uint32 */
1507 aligned[i/4-1] = tmp;
1510 /* add the terminator */
1511 aligned[div-1] = 0x00000001;
1513 key[0].length = div;
1514 key[0].key = aligned;
1519 emem_tree_insert32_array(se_tree, key, v);
1524 emem_tree_lookup_string(emem_tree_t* se_tree, const gchar* k, guint32 flags)
1526 emem_tree_key_t key[2];
1527 guint32 *aligned=NULL;
1528 guint32 len = strlen(k);
1529 guint32 div = (len+3)/4+1;
1534 aligned = malloc(div * sizeof (guint32));
1536 /* pack the bytes one one by one into guint32s */
1538 for (i = 0;i < len;i++) {
1541 ch = (unsigned char)k[i];
1542 if (flags & EMEM_TREE_STRING_NOCASE) {
1554 /* add required padding to the last uint32 */
1560 aligned[i/4-1] = tmp;
1563 /* add the terminator */
1564 aligned[div-1] = 0x00000001;
1566 key[0].length = div;
1567 key[0].key = aligned;
1572 ret = emem_tree_lookup32_array(se_tree, key);
1578 emem_tree_foreach_nodes(emem_tree_node_t* node, tree_foreach_func callback, void *user_data)
1580 gboolean stop_traverse = FALSE;
1586 stop_traverse = emem_tree_foreach_nodes(node->left, callback, user_data);
1587 if (stop_traverse) {
1592 if (node->u.is_subtree == EMEM_TREE_NODE_IS_SUBTREE) {
1593 stop_traverse = emem_tree_foreach(node->data, callback, user_data);
1595 stop_traverse = callback(node->data, user_data);
1598 if (stop_traverse) {
1603 stop_traverse = emem_tree_foreach_nodes(node->right, callback, user_data);
1604 if (stop_traverse) {
1613 emem_tree_foreach(emem_tree_t* emem_tree, tree_foreach_func callback, void *user_data)
1618 if(!emem_tree->tree)
1621 return emem_tree_foreach_nodes(emem_tree->tree, callback, user_data);
1626 emem_tree_print_nodes(emem_tree_node_t* node, int level)
1633 for(i=0;i<level;i++){
1637 printf("NODE:%p parent:%p left:0x%p right:%px key:%d data:%p\n",
1638 (void *)node,(void *)(node->parent),(void *)(node->left),(void *)(node->right),
1639 (node->key32),node->data);
1641 emem_tree_print_nodes(node->left, level+1);
1643 emem_tree_print_nodes(node->right, level+1);
1646 emem_print_tree(emem_tree_t* emem_tree)
1651 printf("EMEM tree type:%d name:%s tree:%p\n",emem_tree->type,emem_tree->name,(void *)(emem_tree->tree));
1653 emem_tree_print_nodes(emem_tree->tree, 0);
1661 * Presumably we're using these routines for building strings for the tree.
1662 * Use ITEM_LABEL_LENGTH as the basis for our default lengths.
1665 #define DEFAULT_STRBUF_LEN (ITEM_LABEL_LENGTH / 10)
1666 #define MAX_STRBUF_LEN 65536
1669 next_size(gsize cur_len, gsize wanted_len, gsize max_len) {
1670 if (max_len < 1 || max_len > MAX_STRBUF_LEN) {
1671 max_len = MAX_STRBUF_LEN;
1675 cur_len = DEFAULT_STRBUF_LEN;
1678 while (cur_len < wanted_len) {
1682 return cur_len < max_len ? cur_len : max_len;
1686 ep_strbuf_grow(emem_strbuf_t *strbuf, gsize wanted_len) {
1687 gsize new_alloc_len;
1690 if (!strbuf || strbuf->alloc_len >= strbuf->max_len) {
1694 new_alloc_len = next_size(strbuf->len, wanted_len, strbuf->max_len);
1695 new_str = ep_alloc(new_alloc_len);
1696 g_strlcpy(new_str, strbuf->str, new_alloc_len);
1698 strbuf->alloc_len = new_alloc_len;
1699 strbuf->str = new_str;
1703 ep_strbuf_sized_new(gsize len, gsize max_len) {
1704 emem_strbuf_t *strbuf;
1706 strbuf = ep_alloc(sizeof(emem_strbuf_t));
1709 strbuf->str = ep_alloc(len);
1710 strbuf->str[0] = '\0';
1712 strbuf->str = ep_strdup("");
1716 strbuf->alloc_len = len;
1717 strbuf->max_len = max_len;
1722 ep_strbuf_new(const gchar *init) {
1723 emem_strbuf_t *strbuf;
1725 strbuf = ep_strbuf_sized_new(next_size(0, strlen(init), 0), 0);
1727 g_strlcpy(strbuf->str, init, strbuf->alloc_len);
1732 ep_strbuf_new_label(const gchar *init) {
1733 emem_strbuf_t *strbuf;
1740 init_size = strlen(init);
1741 strbuf = ep_strbuf_sized_new(init_size > DEFAULT_STRBUF_LEN ? init_size : DEFAULT_STRBUF_LEN,
1744 g_strlcpy(strbuf->str, init, strbuf->alloc_len);
1745 strbuf->len = MIN(init_size, strbuf->alloc_len);
1750 ep_strbuf_append(emem_strbuf_t *strbuf, const gchar *str) {
1753 if (!strbuf || !str) {
1757 add_len = strlen(str);
1759 if (strbuf->len + add_len > strbuf->alloc_len) {
1760 ep_strbuf_grow(strbuf, strbuf->len + add_len);
1763 if (strbuf->len + add_len > strbuf->alloc_len) {
1764 add_len = strbuf->alloc_len - strbuf->len;
1767 g_strlcpy(&strbuf->str[strbuf->len], str, add_len);
1768 strbuf->len += add_len;
1772 ep_strbuf_append_vprintf(emem_strbuf_t *strbuf, const gchar *format, va_list ap) {
1774 gsize add_len, full_len;
1778 add_len = g_printf_string_upper_bound(format, ap);
1780 if (strbuf->len + add_len > strbuf->alloc_len) {
1781 ep_strbuf_grow(strbuf, strbuf->len + add_len);
1784 if (strbuf->len + add_len > strbuf->alloc_len) {
1785 add_len = strbuf->alloc_len - strbuf->len;
1788 full_len = g_vsnprintf(&strbuf->str[strbuf->len], add_len, format, ap2);
1789 strbuf->len += MIN(add_len, full_len);
1795 ep_strbuf_append_printf(emem_strbuf_t *strbuf, const gchar *format, ...) {
1798 va_start(ap, format);
1799 ep_strbuf_append_vprintf(strbuf, format, ap);
1804 ep_strbuf_printf(emem_strbuf_t *strbuf, const gchar *format, ...) {
1812 va_start(ap, format);
1813 ep_strbuf_append_vprintf(strbuf, format, ap);
1818 ep_strbuf_append_c(emem_strbuf_t *strbuf, const gchar c) {
1823 ep_strbuf_grow(strbuf, strbuf->len + 1);
1825 if (strbuf->alloc_len > strbuf->len + 1) {
1826 strbuf->str[strbuf->len] = c;
1828 strbuf->str[strbuf->len] = '\0';
1833 ep_strbuf_truncate(emem_strbuf_t *strbuf, gsize len) {
1834 if (!strbuf || len >= strbuf->len) {
1838 strbuf->str[len] = '\0';