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);
162 * Given an allocation size, return the amount of padding needed for
166 emem_canary_pad (size_t allocation) {
169 pad = EMEM_CANARY_SIZE - (allocation % EMEM_CANARY_SIZE);
170 if (pad < EMEM_CANARY_SIZE)
171 pad += EMEM_CANARY_SIZE;
175 #endif /* DEBUG_USE_CANARIES */
177 /* used for debugging canaries, will block */
178 #ifdef DEBUG_INTENSE_CANARY_CHECKS
179 gboolean intense_canary_checking = FALSE;
181 /* used to intensivelly check ep canaries
183 void ep_check_canary_integrity(const char* fmt, ...) {
185 static gchar there[128] = {
186 '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,
187 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,
188 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,
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 };
191 emem_chunk_t* npc = NULL;
193 if (! intense_canary_checking ) return;
199 g_vsnprintf(here, 126,fmt, ap);
202 for (npc = ep_packet_mem.free_list; npc != NULL; npc = npc->next) {
203 static unsigned i_ctr;
205 if (npc->c_count > 0x00ffffff) {
206 g_error("ep_packet_mem.free_list was corrupted\nbetween: %s\nand: %s",there, here);
209 for (i_ctr = 0; i_ctr < npc->c_count; i_ctr++) {
210 if (memcmp(npc->canary[i_ctr], &ep_canary, npc->cmp_len[i_ctr]) != 0) {
211 g_error("Per-packet memory corrupted\nbetween: %s\nand: %s",there, here);
216 strncpy(there,here,126);
222 /* Initialize the packet-lifetime memory allocation pool.
223 * This function should be called only once when Wireshark or TShark starts
229 ep_packet_mem.free_list=NULL;
230 ep_packet_mem.used_list=NULL;
232 #ifdef DEBUG_INTENSE_CANARY_CHECKS
233 intense_canary_checking = (gboolean)getenv("WIRESHARK_DEBUG_EP_CANARY");
236 #ifdef DEBUG_USE_CANARIES
237 emem_canary(ep_canary);
238 #endif /* DEBUG_USE_CANARIES */
240 #if !defined(SE_DEBUG_FREE)
242 /* Set up our guard page info for Win32 */
243 GetSystemInfo(&sysinfo);
244 pagesize = sysinfo.dwPageSize;
246 /* calling GetVersionEx using the OSVERSIONINFO structure.
247 * OSVERSIONINFOEX requires Win NT4 with SP6 or newer NT Versions.
248 * OSVERSIONINFOEX will fail on Win9x and older NT Versions.
250 * http://msdn.microsoft.com/library/en-us/sysinfo/base/getversionex.asp
251 * http://msdn.microsoft.com/library/en-us/sysinfo/base/osversioninfo_str.asp
252 * http://msdn.microsoft.com/library/en-us/sysinfo/base/osversioninfoex_str.asp
254 versinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
255 GetVersionEx(&versinfo);
257 #elif defined(USE_GUARD_PAGES)
258 pagesize = sysconf(_SC_PAGESIZE);
260 dev_zero_fd = ws_open("/dev/zero", O_RDWR);
261 g_assert(dev_zero_fd != -1);
263 #endif /* _WIN32 / USE_GUARD_PAGES */
264 #endif /* SE_DEBUG_FREE */
268 /* Initialize the capture-lifetime memory allocation pool.
269 * This function should be called only once when Wireshark or TShark starts
275 se_packet_mem.free_list=NULL;
276 se_packet_mem.used_list=NULL;
278 #ifdef DEBUG_USE_CANARIES
279 emem_canary(se_canary);
280 #endif /* DEBUG_USE_CANARIES */
283 #if !defined(SE_DEBUG_FREE)
285 emem_create_chunk(emem_chunk_t **free_list) {
288 char *buf_end, *prot1, *prot2;
290 #elif defined(USE_GUARD_PAGES)
292 char *buf_end, *prot1, *prot2;
293 #endif /* _WIN32 / USE_GUARD_PAGES */
294 /* we dont have any free data, so we must allocate a new one */
297 npc = g_malloc(sizeof(emem_chunk_t));
299 #ifdef DEBUG_USE_CANARIES
300 #if ! defined(EP_DEBUG_FREE) && ! defined(SE_DEBUG_FREE)
303 #endif /* DEBUG_USE_CANARIES */
308 * MSDN documents VirtualAlloc/VirtualProtect at
309 * http://msdn.microsoft.com/library/en-us/memory/base/creating_guard_pages.asp
312 /* XXX - is MEM_COMMIT|MEM_RESERVE correct? */
313 npc->buf = VirtualAlloc(NULL, EMEM_PACKET_CHUNK_SIZE,
314 MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
315 if(npc->buf == NULL) {
316 THROW(OutOfMemoryError);
318 buf_end = npc->buf + EMEM_PACKET_CHUNK_SIZE;
320 /* Align our guard pages on page-sized boundaries */
321 prot1 = (char *) ((((int) npc->buf + pagesize - 1) / pagesize) * pagesize);
322 prot2 = (char *) ((((int) buf_end - (1 * pagesize)) / pagesize) * pagesize);
324 ret = VirtualProtect(prot1, pagesize, PAGE_NOACCESS, &oldprot);
325 g_assert(ret != 0 || versinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
326 ret = VirtualProtect(prot2, pagesize, PAGE_NOACCESS, &oldprot);
327 g_assert(ret != 0 || versinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
329 npc->amount_free_init = (unsigned int) (prot2 - prot1 - pagesize);
330 npc->amount_free = npc->amount_free_init;
331 npc->free_offset_init = (unsigned int) (prot1 - npc->buf) + pagesize;
332 npc->free_offset = npc->free_offset_init;
334 #elif defined(USE_GUARD_PAGES)
335 npc->buf = mmap(NULL, EMEM_PACKET_CHUNK_SIZE,
336 PROT_READ|PROT_WRITE, ANON_PAGE_MODE, ANON_FD, 0);
337 if(npc->buf == MAP_FAILED) {
338 /* XXX - what do we have to cleanup here? */
339 THROW(OutOfMemoryError);
341 buf_end = npc->buf + EMEM_PACKET_CHUNK_SIZE;
343 /* Align our guard pages on page-sized boundaries */
344 prot1 = (char *) ((((intptr_t) npc->buf + pagesize - 1) / pagesize) * pagesize);
345 prot2 = (char *) ((((intptr_t) buf_end - (1 * pagesize)) / pagesize) * pagesize);
346 ret = mprotect(prot1, pagesize, PROT_NONE);
348 ret = mprotect(prot2, pagesize, PROT_NONE);
351 npc->amount_free_init = prot2 - prot1 - pagesize;
352 npc->amount_free = npc->amount_free_init;
353 npc->free_offset_init = (prot1 - npc->buf) + pagesize;
354 npc->free_offset = npc->free_offset_init;
356 #else /* Is there a draft in here? */
357 npc->buf = malloc(EMEM_PACKET_CHUNK_SIZE);
358 if(npc->buf == NULL) {
359 THROW(OutOfMemoryError);
361 npc->amount_free_init = EMEM_PACKET_CHUNK_SIZE;
362 npc->amount_free = npc->amount_free_init;
363 npc->free_offset_init = 0;
364 npc->free_offset = npc->free_offset_init;
365 #endif /* USE_GUARD_PAGES */
370 /* allocate 'size' amount of memory. */
372 emem_alloc(size_t size, gboolean debug_free, emem_header_t *mem, guint8 *canary)
375 #ifdef DEBUG_USE_CANARIES
377 guint8 pad = emem_canary_pad(size);
379 static guint8 pad = 8;
380 #endif /* DEBUG_USE_CANARIES */
381 emem_chunk_t *free_list;
384 /* Round up to an 8 byte boundary. Make sure we have at least
385 * 8 pad bytes for our canary.
389 /* make sure we dont try to allocate too much (arbitrary limit) */
390 DISSECTOR_ASSERT(size<(EMEM_PACKET_CHUNK_SIZE>>2));
392 emem_create_chunk(&mem->free_list);
394 /* oops, we need to allocate more memory to serve this request
395 * than we have free. move this node to the used list and try again
397 if(size>mem->free_list->amount_free
398 #ifdef DEBUG_USE_CANARIES
399 || mem->free_list->c_count >= EMEM_ALLOCS_PER_CHUNK
400 #endif /* DEBUG_USE_CANARIES */
404 mem->free_list=mem->free_list->next;
405 npc->next=mem->used_list;
409 emem_create_chunk(&mem->free_list);
411 free_list = mem->free_list;
413 buf = free_list->buf + free_list->free_offset;
415 free_list->amount_free -= (unsigned int) size;
416 free_list->free_offset += (unsigned int) size;
418 #ifdef DEBUG_USE_CANARIES
419 cptr = (char *)buf + size - pad;
420 memcpy(cptr, canary, pad);
421 free_list->canary[free_list->c_count] = cptr;
422 free_list->cmp_len[free_list->c_count] = pad;
423 free_list->c_count++;
424 #endif /* DEBUG_USE_CANARIES */
428 npc=g_malloc(sizeof(emem_chunk_t));
429 npc->next=mem->used_list;
430 npc->amount_free=size;
432 npc->buf=g_malloc(size);
440 /* allocate 'size' amount of memory with an allocation lifetime until the
444 ep_alloc(size_t size)
447 return emem_alloc(size, TRUE, &ep_packet_mem, ep_canary);
449 return emem_alloc(size, FALSE, &ep_packet_mem, ep_canary);
453 /* allocate 'size' amount of memory with an allocation lifetime until the
457 se_alloc(size_t size)
460 return emem_alloc(size, TRUE, &se_packet_mem, se_canary);
462 return emem_alloc(size, FALSE, &se_packet_mem, se_canary);
466 void* ep_alloc0(size_t size) {
467 return memset(ep_alloc(size),'\0',size);
470 gchar* ep_strdup(const gchar* src) {
471 guint len = (guint) strlen(src);
474 dst = strncpy(ep_alloc(len+1), src, len);
481 gchar* ep_strndup(const gchar* src, size_t len) {
482 gchar* dst = ep_alloc(len+1);
485 for (i = 0; (i < len) && src[i]; i++)
493 void* ep_memdup(const void* src, size_t len) {
494 return memcpy(ep_alloc(len), src, len);
497 gchar* ep_strdup_vprintf(const gchar* fmt, va_list ap) {
504 len = g_printf_string_upper_bound(fmt, ap);
506 dst = ep_alloc(len+1);
507 g_vsnprintf (dst, (gulong) len, fmt, ap2);
513 gchar* ep_strdup_printf(const gchar* fmt, ...) {
518 dst = ep_strdup_vprintf(fmt, ap);
523 gchar** ep_strsplit(const gchar* string, const gchar* sep, int max_tokens) {
531 enum { AT_START, IN_PAD, IN_TOKEN } state;
539 s = splitted = ep_strdup(string);
540 str_len = (guint) strlen(splitted);
541 sep_len = (guint) strlen(sep);
543 if (max_tokens < 1) max_tokens = INT_MAX;
548 while (tokens <= (guint)max_tokens && ( s = strstr(s,sep) )) {
551 for(i=0; i < sep_len; i++ )
558 vec = ep_alloc_array(gchar*,tokens+1);
561 for (i=0; i< str_len; i++) {
564 switch(splitted[i]) {
569 vec[curr_tok] = &(splitted[i]);
575 switch(splitted[i]) {
582 switch(splitted[i]) {
584 vec[curr_tok] = &(splitted[i]);
593 vec[curr_tok] = NULL;
600 void* se_alloc0(size_t size) {
601 return memset(se_alloc(size),'\0',size);
604 /* If str is NULL, just return the string "<NULL>" so that the callers dont
605 * have to bother checking it.
607 gchar* se_strdup(const gchar* src) {
615 len = (guint) strlen(src);
616 dst = strncpy(se_alloc(len+1), src, len);
623 gchar* se_strndup(const gchar* src, size_t len) {
624 gchar* dst = se_alloc(len+1);
627 for (i = 0; (i < len) && src[i]; i++)
635 void* se_memdup(const void* src, size_t len) {
636 return memcpy(se_alloc(len), src, len);
639 gchar* se_strdup_vprintf(const gchar* fmt, va_list ap) {
646 len = g_printf_string_upper_bound(fmt, ap);
648 dst = se_alloc(len+1);
649 g_vsnprintf (dst, (gulong) len, fmt, ap2);
655 gchar* se_strdup_printf(const gchar* fmt, ...) {
660 dst = se_strdup_vprintf(fmt, ap);
665 /* release all allocated memory back to the pool. */
667 emem_free_all(gboolean debug_free, emem_header_t *mem, guint8 *canary, emem_tree_t *trees)
670 emem_tree_t *tree_list;
671 #ifdef DEBUG_USE_CANARIES
673 #endif /* DEBUG_USE_CANARIES */
675 /* move all used chunks over to the free list */
676 while(mem->used_list){
678 mem->used_list=mem->used_list->next;
679 npc->next=mem->free_list;
683 /* clear them all out */
684 npc = mem->free_list;
685 while (npc != NULL) {
687 #ifdef DEBUG_USE_CANARIES
688 for (i = 0; i < npc->c_count; i++) {
689 if (memcmp(npc->canary[i], canary, npc->cmp_len[i]) != 0)
690 g_error("Memory corrupted");
693 #endif /* DEBUG_USE_CANARIES */
694 npc->amount_free = npc->amount_free_init;
695 npc->free_offset = npc->free_offset_init;
698 emem_chunk_t *next = npc->next;
706 /* release/reset all allocated trees */
707 for(tree_list=trees;tree_list;tree_list=tree_list->next){
708 tree_list->tree=NULL;
712 /* release all allocated memory back to the pool. */
717 emem_free_all(TRUE, &ep_packet_mem, ep_canary, NULL);
719 emem_free_all(FALSE, &ep_packet_mem, ep_canary, NULL);
727 /* release all allocated memory back to the pool. */
732 emem_free_all(TRUE, &se_packet_mem, se_canary, se_trees);
734 emem_free_all(FALSE, &se_packet_mem, se_canary, se_trees);
742 ep_stack_t ep_stack_new(void) {
743 ep_stack_t s = ep_new(struct _ep_stack_frame_t*);
744 *s = ep_new0(struct _ep_stack_frame_t);
748 /* for ep_stack_t we'll keep the popped frames so we reuse them instead
749 of allocating new ones.
753 void* ep_stack_push(ep_stack_t stack, void* data) {
754 struct _ep_stack_frame_t* frame;
755 struct _ep_stack_frame_t* head = (*stack);
760 frame = ep_new(struct _ep_stack_frame_t);
766 frame->payload = data;
772 void* ep_stack_pop(ep_stack_t stack) {
774 if ((*stack)->below) {
775 (*stack) = (*stack)->below;
776 return (*stack)->above->payload;
785 void print_tree_item(emem_tree_node_t *node, int level){
787 for(i=0;i<level;i++){
790 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);
792 print_tree_item(node->left,level+1);
794 print_tree_item(node->right,level+1);
797 void print_tree(emem_tree_node_t *node){
804 print_tree_item(node,0);
810 /* routines to manage se allocated red-black trees */
811 emem_tree_t *se_trees=NULL;
814 se_tree_create(int type, const char *name)
816 emem_tree_t *tree_list;
818 tree_list=malloc(sizeof(emem_tree_t));
819 tree_list->next=se_trees;
820 tree_list->type=type;
821 tree_list->tree=NULL;
822 tree_list->name=name;
823 tree_list->malloc=se_alloc;
832 emem_tree_lookup32(emem_tree_t *se_tree, guint32 key)
834 emem_tree_node_t *node;
839 if(key==node->key32){
855 emem_tree_lookup32_le(emem_tree_t *se_tree, guint32 key)
857 emem_tree_node_t *node;
867 if(key==node->key32){
889 /* If we are still at the root of the tree this means that this node
890 * is either smaller than the search key and then we return this
891 * node or else there is no smaller key available and then
902 if(node->parent->left==node){
906 /* if this is a left child and its key is smaller than
907 * the search key, then this is the node we want.
911 /* if this is a left child and its key is bigger than
912 * the search key, we have to check if any
913 * of our ancestors are smaller than the search key.
927 /* if this is the right child and its key is smaller
928 * than the search key then this is the one we want.
932 /* if this is the right child and its key is larger
933 * than the search key then our parent is the one we
936 return node->parent->data;
943 static inline emem_tree_node_t *
944 emem_tree_parent(emem_tree_node_t *node)
949 static inline emem_tree_node_t *
950 emem_tree_grandparent(emem_tree_node_t *node)
952 emem_tree_node_t *parent;
954 parent=emem_tree_parent(node);
956 return parent->parent;
960 static inline emem_tree_node_t *
961 emem_tree_uncle(emem_tree_node_t *node)
963 emem_tree_node_t *parent, *grandparent;
965 parent=emem_tree_parent(node);
969 grandparent=emem_tree_parent(parent);
973 if(parent==grandparent->left){
974 return grandparent->right;
976 return grandparent->left;
979 static inline void rb_insert_case1(emem_tree_t *se_tree, emem_tree_node_t *node);
980 static inline void rb_insert_case2(emem_tree_t *se_tree, emem_tree_node_t *node);
983 rotate_left(emem_tree_t *se_tree, emem_tree_node_t *node)
986 if(node->parent->left==node){
987 node->parent->left=node->right;
989 node->parent->right=node->right;
992 se_tree->tree=node->right;
994 node->right->parent=node->parent;
995 node->parent=node->right;
996 node->right=node->right->left;
998 node->right->parent=node;
1000 node->parent->left=node;
1004 rotate_right(emem_tree_t *se_tree, emem_tree_node_t *node)
1007 if(node->parent->left==node){
1008 node->parent->left=node->left;
1010 node->parent->right=node->left;
1013 se_tree->tree=node->left;
1015 node->left->parent=node->parent;
1016 node->parent=node->left;
1017 node->left=node->left->right;
1019 node->left->parent=node;
1021 node->parent->right=node;
1025 rb_insert_case5(emem_tree_t *se_tree, emem_tree_node_t *node)
1027 emem_tree_node_t *grandparent;
1028 emem_tree_node_t *parent;
1030 parent=emem_tree_parent(node);
1031 grandparent=emem_tree_parent(parent);
1032 parent->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1033 grandparent->u.rb_color=EMEM_TREE_RB_COLOR_RED;
1034 if( (node==parent->left) && (parent==grandparent->left) ){
1035 rotate_right(se_tree, grandparent);
1037 rotate_left(se_tree, grandparent);
1042 rb_insert_case4(emem_tree_t *se_tree, emem_tree_node_t *node)
1044 emem_tree_node_t *grandparent;
1045 emem_tree_node_t *parent;
1047 parent=emem_tree_parent(node);
1048 grandparent=emem_tree_parent(parent);
1052 if( (node==parent->right) && (parent==grandparent->left) ){
1053 rotate_left(se_tree, parent);
1055 } else if( (node==parent->left) && (parent==grandparent->right) ){
1056 rotate_right(se_tree, parent);
1059 rb_insert_case5(se_tree, node);
1063 rb_insert_case3(emem_tree_t *se_tree, emem_tree_node_t *node)
1065 emem_tree_node_t *grandparent;
1066 emem_tree_node_t *parent;
1067 emem_tree_node_t *uncle;
1069 uncle=emem_tree_uncle(node);
1070 if(uncle && (uncle->u.rb_color==EMEM_TREE_RB_COLOR_RED)){
1071 parent=emem_tree_parent(node);
1072 parent->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1073 uncle->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1074 grandparent=emem_tree_grandparent(node);
1075 grandparent->u.rb_color=EMEM_TREE_RB_COLOR_RED;
1076 rb_insert_case1(se_tree, grandparent);
1078 rb_insert_case4(se_tree, node);
1083 rb_insert_case2(emem_tree_t *se_tree, emem_tree_node_t *node)
1085 emem_tree_node_t *parent;
1087 parent=emem_tree_parent(node);
1088 /* parent is always non-NULL here */
1089 if(parent->u.rb_color==EMEM_TREE_RB_COLOR_BLACK){
1092 rb_insert_case3(se_tree, node);
1096 rb_insert_case1(emem_tree_t *se_tree, emem_tree_node_t *node)
1098 emem_tree_node_t *parent;
1100 parent=emem_tree_parent(node);
1102 node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1105 rb_insert_case2(se_tree, node);
1108 /* insert a new node in the tree. if this node matches an already existing node
1109 * then just replace the data for that node */
1111 emem_tree_insert32(emem_tree_t *se_tree, guint32 key, void *data)
1113 emem_tree_node_t *node;
1117 /* is this the first node ?*/
1119 node=se_tree->malloc(sizeof(emem_tree_node_t));
1120 switch(se_tree->type){
1121 case EMEM_TREE_TYPE_RED_BLACK:
1122 node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1130 node->u.is_subtree = EMEM_TREE_NODE_IS_DATA;
1135 /* it was not the new root so walk the tree until we find where to
1136 * insert this new leaf.
1139 /* this node already exists, so just replace the data pointer*/
1140 if(key==node->key32){
1144 if(key<node->key32) {
1146 /* new node to the left */
1147 emem_tree_node_t *new_node;
1148 new_node=se_tree->malloc(sizeof(emem_tree_node_t));
1149 node->left=new_node;
1150 new_node->parent=node;
1151 new_node->left=NULL;
1152 new_node->right=NULL;
1153 new_node->key32=key;
1154 new_node->data=data;
1155 new_node->u.is_subtree=EMEM_TREE_NODE_IS_DATA;
1162 if(key>node->key32) {
1164 /* new node to the right */
1165 emem_tree_node_t *new_node;
1166 new_node=se_tree->malloc(sizeof(emem_tree_node_t));
1167 node->right=new_node;
1168 new_node->parent=node;
1169 new_node->left=NULL;
1170 new_node->right=NULL;
1171 new_node->key32=key;
1172 new_node->data=data;
1173 new_node->u.is_subtree=EMEM_TREE_NODE_IS_DATA;
1182 /* node will now point to the newly created node */
1183 switch(se_tree->type){
1184 case EMEM_TREE_TYPE_RED_BLACK:
1185 node->u.rb_color=EMEM_TREE_RB_COLOR_RED;
1186 rb_insert_case1(se_tree, node);
1191 static void* lookup_or_insert32(emem_tree_t *se_tree, guint32 key, void*(*func)(void*),void* ud, int is_subtree) {
1192 emem_tree_node_t *node;
1196 /* is this the first node ?*/
1198 node=se_tree->malloc(sizeof(emem_tree_node_t));
1199 switch(se_tree->type){
1200 case EMEM_TREE_TYPE_RED_BLACK:
1201 node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1208 node->data= func(ud);
1209 node->u.is_subtree = is_subtree;
1214 /* it was not the new root so walk the tree until we find where to
1215 * insert this new leaf.
1218 /* this node already exists, so just return the data pointer*/
1219 if(key==node->key32){
1222 if(key<node->key32) {
1224 /* new node to the left */
1225 emem_tree_node_t *new_node;
1226 new_node=se_tree->malloc(sizeof(emem_tree_node_t));
1227 node->left=new_node;
1228 new_node->parent=node;
1229 new_node->left=NULL;
1230 new_node->right=NULL;
1231 new_node->key32=key;
1232 new_node->data= func(ud);
1233 new_node->u.is_subtree = is_subtree;
1240 if(key>node->key32) {
1242 /* new node to the right */
1243 emem_tree_node_t *new_node;
1244 new_node=se_tree->malloc(sizeof(emem_tree_node_t));
1245 node->right=new_node;
1246 new_node->parent=node;
1247 new_node->left=NULL;
1248 new_node->right=NULL;
1249 new_node->key32=key;
1250 new_node->data= func(ud);
1251 new_node->u.is_subtree = is_subtree;
1260 /* node will now point to the newly created node */
1261 switch(se_tree->type){
1262 case EMEM_TREE_TYPE_RED_BLACK:
1263 node->u.rb_color=EMEM_TREE_RB_COLOR_RED;
1264 rb_insert_case1(se_tree, node);
1271 /* When the se data is released, this entire tree will dissapear as if it
1272 * never existed including all metadata associated with the tree.
1275 se_tree_create_non_persistent(int type, const char *name)
1277 emem_tree_t *tree_list;
1279 tree_list=se_alloc(sizeof(emem_tree_t));
1280 tree_list->next=NULL;
1281 tree_list->type=type;
1282 tree_list->tree=NULL;
1283 tree_list->name=name;
1284 tree_list->malloc=se_alloc;
1289 /* This tree is PErmanent and will never be released
1292 pe_tree_create(int type, const char *name)
1294 emem_tree_t *tree_list;
1296 tree_list=g_malloc(sizeof(emem_tree_t));
1297 tree_list->next=NULL;
1298 tree_list->type=type;
1299 tree_list->tree=NULL;
1300 tree_list->name=name;
1301 tree_list->malloc=(void *(*)(size_t)) g_malloc;
1306 /* create another (sub)tree using the same memory allocation scope
1307 * as the parent tree.
1309 static emem_tree_t *
1310 emem_tree_create_subtree(emem_tree_t *parent_tree, const char *name)
1312 emem_tree_t *tree_list;
1314 tree_list=parent_tree->malloc(sizeof(emem_tree_t));
1315 tree_list->next=NULL;
1316 tree_list->type=parent_tree->type;
1317 tree_list->tree=NULL;
1318 tree_list->name=name;
1319 tree_list->malloc=parent_tree->malloc;
1324 static void* create_sub_tree(void* d) {
1325 emem_tree_t *se_tree = d;
1326 return emem_tree_create_subtree(se_tree, "subtree");
1329 /* insert a new node in the tree. if this node matches an already existing node
1330 * then just replace the data for that node */
1333 emem_tree_insert32_array(emem_tree_t *se_tree, emem_tree_key_t *key, void *data)
1335 emem_tree_t *next_tree;
1337 if((key[0].length<1)||(key[0].length>100)){
1338 DISSECTOR_ASSERT_NOT_REACHED();
1340 if((key[0].length==1)&&(key[1].length==0)){
1341 emem_tree_insert32(se_tree, *key[0].key, data);
1345 next_tree=lookup_or_insert32(se_tree, *key[0].key, create_sub_tree, se_tree, EMEM_TREE_NODE_IS_SUBTREE);
1347 if(key[0].length==1){
1353 emem_tree_insert32_array(next_tree, key, data);
1357 emem_tree_lookup32_array(emem_tree_t *se_tree, emem_tree_key_t *key)
1359 emem_tree_t *next_tree;
1361 if((key[0].length<1)||(key[0].length>100)){
1362 DISSECTOR_ASSERT_NOT_REACHED();
1364 if((key[0].length==1)&&(key[1].length==0)){
1365 return emem_tree_lookup32(se_tree, *key[0].key);
1367 next_tree=emem_tree_lookup32(se_tree, *key[0].key);
1371 if(key[0].length==1){
1377 return emem_tree_lookup32_array(next_tree, key);
1381 /* Strings are stored as an array of uint32 containing the string characters
1382 with 4 characters in each uint32.
1383 The first byte of the string is stored as the most significant byte.
1384 If the string is not a multiple of 4 characters in length the last
1385 uint32 containing the string bytes are padded with 0 bytes.
1386 After the uint32's containing the string, there is one final terminator
1387 uint32 with the value 0x00000001
1390 emem_tree_insert_string(emem_tree_t* se_tree, const gchar* k, void* v, guint32 flags)
1392 emem_tree_key_t key[2];
1393 guint32 *aligned=NULL;
1394 guint32 len = (guint32) strlen(k);
1395 guint32 div = (len+3)/4+1;
1399 aligned = malloc(div * sizeof (guint32));
1401 /* pack the bytes one one by one into guint32s */
1403 for (i = 0;i < len;i++) {
1406 ch = (unsigned char)k[i];
1407 if (flags & EMEM_TREE_STRING_NOCASE) {
1419 /* add required padding to the last uint32 */
1425 aligned[i/4-1] = tmp;
1428 /* add the terminator */
1429 aligned[div-1] = 0x00000001;
1431 key[0].length = div;
1432 key[0].key = aligned;
1437 emem_tree_insert32_array(se_tree, key, v);
1442 emem_tree_lookup_string(emem_tree_t* se_tree, const gchar* k, guint32 flags)
1444 emem_tree_key_t key[2];
1445 guint32 *aligned=NULL;
1446 guint32 len = (guint) strlen(k);
1447 guint32 div = (len+3)/4+1;
1452 aligned = malloc(div * sizeof (guint32));
1454 /* pack the bytes one one by one into guint32s */
1456 for (i = 0;i < len;i++) {
1459 ch = (unsigned char)k[i];
1460 if (flags & EMEM_TREE_STRING_NOCASE) {
1472 /* add required padding to the last uint32 */
1478 aligned[i/4-1] = tmp;
1481 /* add the terminator */
1482 aligned[div-1] = 0x00000001;
1484 key[0].length = div;
1485 key[0].key = aligned;
1490 ret = emem_tree_lookup32_array(se_tree, key);
1496 emem_tree_foreach_nodes(emem_tree_node_t* node, tree_foreach_func callback, void *user_data)
1498 gboolean stop_traverse = FALSE;
1504 stop_traverse = emem_tree_foreach_nodes(node->left, callback, user_data);
1505 if (stop_traverse) {
1510 if (node->u.is_subtree == EMEM_TREE_NODE_IS_SUBTREE) {
1511 stop_traverse = emem_tree_foreach(node->data, callback, user_data);
1513 stop_traverse = callback(node->data, user_data);
1516 if (stop_traverse) {
1521 stop_traverse = emem_tree_foreach_nodes(node->right, callback, user_data);
1522 if (stop_traverse) {
1531 emem_tree_foreach(emem_tree_t* emem_tree, tree_foreach_func callback, void *user_data)
1536 if(!emem_tree->tree)
1539 return emem_tree_foreach_nodes(emem_tree->tree, callback, user_data);
1544 emem_tree_print_nodes(emem_tree_node_t* node, int level)
1551 for(i=0;i<level;i++){
1555 printf("NODE:%p parent:%p left:0x%p right:%px key:%d data:%p\n",
1556 (void *)node,(void *)(node->parent),(void *)(node->left),(void *)(node->right),
1557 (node->key32),node->data);
1559 emem_tree_print_nodes(node->left, level+1);
1561 emem_tree_print_nodes(node->right, level+1);
1564 emem_print_tree(emem_tree_t* emem_tree)
1569 printf("EMEM tree type:%d name:%s tree:%p\n",emem_tree->type,emem_tree->name,(void *)(emem_tree->tree));
1571 emem_tree_print_nodes(emem_tree->tree, 0);
1579 * Presumably we're using these routines for building strings for the tree.
1580 * Use ITEM_LABEL_LENGTH as the basis for our default lengths.
1583 #define DEFAULT_STRBUF_LEN (ITEM_LABEL_LENGTH / 10)
1584 #define MAX_STRBUF_LEN 65536
1587 next_size(gsize cur_alloc_len, gsize wanted_alloc_len, gsize max_alloc_len) {
1588 if (max_alloc_len < 1 || max_alloc_len > MAX_STRBUF_LEN) {
1589 max_alloc_len = MAX_STRBUF_LEN;
1592 if (cur_alloc_len < 1) {
1593 cur_alloc_len = DEFAULT_STRBUF_LEN;
1596 while (cur_alloc_len < wanted_alloc_len) {
1600 return cur_alloc_len < max_alloc_len ? cur_alloc_len : max_alloc_len;
1604 ep_strbuf_grow(emem_strbuf_t *strbuf, gsize wanted_alloc_len) {
1605 gsize new_alloc_len;
1608 if (!strbuf || (wanted_alloc_len <= strbuf->alloc_len) || (strbuf->alloc_len >= strbuf->max_alloc_len)) {
1612 new_alloc_len = next_size(strbuf->alloc_len, wanted_alloc_len, strbuf->max_alloc_len);
1613 new_str = ep_alloc(new_alloc_len);
1614 g_strlcpy(new_str, strbuf->str, new_alloc_len);
1616 strbuf->alloc_len = new_alloc_len;
1617 strbuf->str = new_str;
1621 ep_strbuf_sized_new(gsize alloc_len, gsize max_alloc_len) {
1622 emem_strbuf_t *strbuf;
1624 strbuf = ep_alloc(sizeof(emem_strbuf_t));
1626 if ((max_alloc_len == 0) || (max_alloc_len > MAX_STRBUF_LEN))
1627 max_alloc_len = MAX_STRBUF_LEN;
1630 else if (alloc_len > max_alloc_len)
1631 alloc_len = max_alloc_len;
1633 strbuf->str = ep_alloc(alloc_len);
1634 strbuf->str[0] = '\0';
1637 strbuf->alloc_len = alloc_len;
1638 strbuf->max_alloc_len = max_alloc_len;
1644 ep_strbuf_new(const gchar *init) {
1645 emem_strbuf_t *strbuf;
1647 strbuf = ep_strbuf_sized_new(next_size(0, init?strlen(init):0, 0), 0);
1650 full_len = g_strlcpy(strbuf->str, init, strbuf->alloc_len);
1651 strbuf->len = MIN(full_len, strbuf->alloc_len-1);
1658 ep_strbuf_new_label(const gchar *init) {
1659 emem_strbuf_t *strbuf;
1662 /* Be optimistic: Allocate default size strbuf string and only */
1663 /* request an increase if needed. */
1664 /* XXX: Is it reasonable to assume that much of the usage of */
1665 /* ep_strbuf_new_label will have init==NULL or */
1666 /* strlen(init) < DEFAULT_STRBUF_LEN) ??? */
1667 strbuf = ep_strbuf_sized_new(DEFAULT_STRBUF_LEN, ITEM_LABEL_LENGTH);
1672 /* full_len does not count the trailing '\0'. */
1673 full_len = g_strlcpy(strbuf->str, init, strbuf->alloc_len);
1674 if (full_len < strbuf->alloc_len) {
1675 strbuf->len += full_len;
1677 strbuf = ep_strbuf_sized_new(full_len+1, ITEM_LABEL_LENGTH);
1678 full_len = g_strlcpy(strbuf->str, init, strbuf->alloc_len);
1679 strbuf->len = MIN(full_len, strbuf->alloc_len-1);
1686 ep_strbuf_append(emem_strbuf_t *strbuf, const gchar *str) {
1687 gsize add_len, full_len;
1689 if (!strbuf || !str || str[0] == '\0') {
1693 /* Be optimistic; try the g_strlcpy first & see if enough room. */
1694 /* Note: full_len doesn't count the trailing '\0'; add_len does allow for same */
1695 add_len = strbuf->alloc_len - strbuf->len;
1696 full_len = g_strlcpy(&strbuf->str[strbuf->len], str, add_len);
1697 if (full_len < add_len) {
1698 strbuf->len += full_len;
1700 strbuf->str[strbuf->len] = '\0'; /* end string at original length again */
1701 ep_strbuf_grow(strbuf, strbuf->len + full_len + 1);
1702 add_len = strbuf->alloc_len - strbuf->len;
1703 full_len = g_strlcpy(&strbuf->str[strbuf->len], str, add_len);
1704 strbuf->len += MIN(add_len-1, full_len);
1711 ep_strbuf_append_vprintf(emem_strbuf_t *strbuf, const gchar *format, va_list ap) {
1713 gsize add_len, full_len;
1717 /* Be optimistic; try the g_vsnprintf first & see if enough room. */
1718 /* Note: full_len doesn't count the trailing '\0'; add_len does allow for same. */
1719 add_len = strbuf->alloc_len - strbuf->len;
1720 full_len = g_vsnprintf(&strbuf->str[strbuf->len], (gulong) add_len, format, ap);
1721 if (full_len < add_len) {
1722 strbuf->len += full_len;
1724 strbuf->str[strbuf->len] = '\0'; /* end string at original length again */
1725 ep_strbuf_grow(strbuf, strbuf->len + full_len + 1);
1726 add_len = strbuf->alloc_len - strbuf->len;
1727 full_len = g_vsnprintf(&strbuf->str[strbuf->len], (gulong) add_len, format, ap2);
1728 strbuf->len += MIN(add_len-1, full_len);
1735 ep_strbuf_append_printf(emem_strbuf_t *strbuf, const gchar *format, ...) {
1738 va_start(ap, format);
1739 ep_strbuf_append_vprintf(strbuf, format, ap);
1744 ep_strbuf_printf(emem_strbuf_t *strbuf, const gchar *format, ...) {
1752 va_start(ap, format);
1753 ep_strbuf_append_vprintf(strbuf, format, ap);
1758 ep_strbuf_append_c(emem_strbuf_t *strbuf, const gchar c) {
1763 /* +1 for the new character & +1 for the trailing '\0'. */
1764 if (strbuf->alloc_len < strbuf->len + 1 + 1) {
1765 ep_strbuf_grow(strbuf, strbuf->len + 1 + 1);
1767 if (strbuf->alloc_len >= strbuf->len + 1 + 1) {
1768 strbuf->str[strbuf->len] = c;
1770 strbuf->str[strbuf->len] = '\0';
1777 ep_strbuf_truncate(emem_strbuf_t *strbuf, gsize len) {
1778 if (!strbuf || len >= strbuf->len) {
1782 strbuf->str[len] = '\0';
1794 * indent-tabs-mode: t
1797 * ex: set shiftwidth=8 tabstop=8 noexpandtab
1798 * :indentSize=8:tabSize=8:noTabs=false: