2 * Ethereal memory management and garbage collection functions
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
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.
35 #ifdef HAVE_SYS_TIME_H
44 #include <windows.h> /* VirtualAlloc, VirtualProtect */
45 #include <process.h> /* getpid */
51 #include <wiretap/file_util.h>
55 * Tools like Valgrind and ElectricFence don't work well with memchunks.
56 * Uncomment the defines below to make {ep|se}_alloc() allocate each
57 * object individually.
59 /* #define EP_DEBUG_FREE 1 */
60 /* #define SE_DEBUG_FREE 1 */
62 /* Do we want to use guardpages? if available */
63 #define WANT_GUARD_PAGES 1
65 /* Do we want to use canaries ? */
66 #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 #define USE_GUARD_PAGES 1
79 /* When required, allocate more memory from the OS in this size chunks */
80 #define EMEM_PACKET_CHUNK_SIZE 10485760
82 /* The maximum number of allocations per chunk */
83 #define EMEM_ALLOCS_PER_CHUNK (EMEM_PACKET_CHUNK_SIZE / 512)
86 #ifdef DEBUG_USE_CANARIES
87 #define EMEM_CANARY_SIZE 8
88 #define EMEM_CANARY_DATA_SIZE (EMEM_CANARY_SIZE * 2 - 1)
89 guint8 ep_canary[EMEM_CANARY_DATA_SIZE], se_canary[EMEM_CANARY_DATA_SIZE];
90 #endif /* DEBUG_USE_CANARIES */
92 typedef struct _emem_chunk_t {
93 struct _emem_chunk_t *next;
94 unsigned int amount_free_init;
95 unsigned int amount_free;
96 unsigned int free_offset_init;
97 unsigned int free_offset;
99 #ifdef DEBUG_USE_CANARIES
100 #if ! defined(EP_DEBUG_FREE) && ! defined(SE_DEBUG_FREE)
101 unsigned int c_count;
102 void *canary[EMEM_ALLOCS_PER_CHUNK];
103 guint8 cmp_len[EMEM_ALLOCS_PER_CHUNK];
105 #endif /* DEBUG_USE_CANARIES */
108 typedef struct _emem_header_t {
109 emem_chunk_t *free_list;
110 emem_chunk_t *used_list;
113 static emem_header_t ep_packet_mem;
114 static emem_header_t se_packet_mem;
116 #if !defined(SE_DEBUG_FREE)
118 static SYSTEM_INFO sysinfo;
119 static OSVERSIONINFO versinfo;
121 #elif defined(USE_GUARD_PAGES)
122 static intptr_t pagesize;
123 #endif /* _WIN32 / USE_GUARD_PAGES */
124 #endif /* SE_DEBUG_FREE */
126 #ifdef DEBUG_USE_CANARIES
128 * Set a canary value to be placed between memchunks.
131 emem_canary(guint8 *canary) {
133 #if GLIB_MAJOR_VERSION >= 2
134 static GRand *rand_state = NULL;
138 /* First, use GLib's random function if we have it */
139 #if GLIB_MAJOR_VERSION >= 2
140 if (rand_state == NULL) {
141 rand_state = g_rand_new();
143 for (i = 0; i < EMEM_CANARY_DATA_SIZE; i ++) {
144 canary[i] = (guint8) g_rand_int(rand_state);
150 /* Try /dev/urandom */
151 if ((fp = eth_fopen("/dev/urandom", "r")) != NULL) {
152 sz = fread(canary, EMEM_CANARY_DATA_SIZE, 1, fp);
154 if (sz == EMEM_CANARY_SIZE) {
159 /* Our last resort */
160 srandom(time(NULL) | getpid());
161 for (i = 0; i < EMEM_CANARY_DATA_SIZE; i ++) {
162 canary[i] = (guint8) random();
165 #endif /* GLIB_MAJOR_VERSION >= 2 */
168 #if !defined(SE_DEBUG_FREE)
170 * Given an allocation size, return the amount of padding needed for
174 emem_canary_pad (size_t allocation) {
177 pad = EMEM_CANARY_SIZE - (allocation % EMEM_CANARY_SIZE);
178 if (pad < EMEM_CANARY_SIZE)
179 pad += EMEM_CANARY_SIZE;
184 #endif /* DEBUG_USE_CANARIES */
187 /* Initialize the packet-lifetime memory allocation pool.
188 * This function should be called only once when Ethereal or Tethereal starts
194 ep_packet_mem.free_list=NULL;
195 ep_packet_mem.used_list=NULL;
197 #ifdef DEBUG_USE_CANARIES
198 emem_canary(ep_canary);
199 #endif /* DEBUG_USE_CANARIES */
201 #if !defined(SE_DEBUG_FREE)
203 /* Set up our guard page info for Win32 */
204 GetSystemInfo(&sysinfo);
205 pagesize = sysinfo.dwPageSize;
207 versinfo.dwOSVersionInfoSize = sizeof(versinfo);
208 GetVersionEx(&versinfo);
209 #elif defined(USE_GUARD_PAGES)
210 pagesize = sysconf(_SC_PAGESIZE);
211 #endif /* _WIN32 / USE_GUARD_PAGES */
212 #endif /* SE_DEBUG_FREE */
216 /* Initialize the capture-lifetime memory allocation pool.
217 * This function should be called only once when Ethereal or Tethereal starts
223 se_packet_mem.free_list=NULL;
224 se_packet_mem.used_list=NULL;
226 #ifdef DEBUG_USE_CANARIES
227 emem_canary(se_canary);
228 #endif /* DEBUG_USE_CANARIES */
231 #if !defined(SE_DEBUG_FREE)
233 emem_create_chunk(emem_chunk_t **free_list) {
236 char *buf_end, *prot1, *prot2;
238 #elif defined(USE_GUARD_PAGES)
240 char *buf_end, *prot1, *prot2;
241 #endif /* _WIN32 / USE_GUARD_PAGES */
242 /* we dont have any free data, so we must allocate a new one */
245 npc = g_malloc(sizeof(emem_chunk_t));
247 #ifdef DEBUG_USE_CANARIES
248 #if ! defined(EP_DEBUG_FREE) && ! defined(SE_DEBUG_FREE)
251 #endif /* DEBUG_USE_CANARIES */
256 * MSDN documents VirtualAlloc/VirtualProtect at
257 * http://msdn.microsoft.com/library/en-us/memory/base/creating_guard_pages.asp
260 /* XXX - is MEM_COMMIT|MEM_RESERVE correct? */
261 npc->buf = VirtualAlloc(NULL, EMEM_PACKET_CHUNK_SIZE,
262 MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
263 g_assert(npc->buf != NULL);
264 buf_end = npc->buf + EMEM_PACKET_CHUNK_SIZE;
266 /* Align our guard pages on page-sized boundaries */
267 prot1 = (char *) ((((int) npc->buf + pagesize - 1) / pagesize) * pagesize);
268 prot2 = (char *) ((((int) buf_end - (1 * pagesize)) / pagesize) * pagesize);
270 ret = VirtualProtect(prot1, pagesize, PAGE_NOACCESS, &oldprot);
271 g_assert(ret != 0 || versinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
272 ret = VirtualProtect(prot2, pagesize, PAGE_NOACCESS, &oldprot);
273 g_assert(ret != 0 || versinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
275 npc->amount_free_init = prot2 - prot1 - pagesize;
276 npc->amount_free = npc->amount_free_init;
277 npc->free_offset_init = (prot1 - npc->buf) + pagesize;
278 npc->free_offset = npc->free_offset_init;
280 #elif defined(USE_GUARD_PAGES)
281 npc->buf = mmap(NULL, EMEM_PACKET_CHUNK_SIZE,
282 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
283 g_assert(npc->buf != MAP_FAILED);
284 buf_end = npc->buf + EMEM_PACKET_CHUNK_SIZE;
286 /* Align our guard pages on page-sized boundaries */
287 prot1 = (char *) ((((intptr_t) npc->buf + pagesize - 1) / pagesize) * pagesize);
288 prot2 = (char *) ((((intptr_t) buf_end - (1 * pagesize)) / pagesize) * pagesize);
289 ret = mprotect(prot1, pagesize, PROT_NONE);
291 ret = mprotect(prot2, pagesize, PROT_NONE);
294 npc->amount_free_init = prot2 - prot1 - pagesize;
295 npc->amount_free = npc->amount_free_init;
296 npc->free_offset_init = (prot1 - npc->buf) + pagesize;
297 npc->free_offset = npc->free_offset_init;
299 #else /* Is there a draft in here? */
300 npc->amount_free_init = EMEM_PACKET_CHUNK_SIZE;
301 npc->amount_free = npc->amount_free_init;
302 npc->free_offset_init = 0;
303 npc->free_offset = npc->free_offset_init;
304 npc->buf = g_malloc(EMEM_PACKET_CHUNK_SIZE);
305 #endif /* USE_GUARD_PAGES */
310 /* allocate 'size' amount of memory with an allocation lifetime until the
314 ep_alloc(size_t size)
317 #ifndef EP_DEBUG_FREE
318 #ifdef DEBUG_USE_CANARIES
320 guint8 pad = emem_canary_pad(size);
323 #endif /* DEBUG_USE_CANARIES */
324 emem_chunk_t *free_list;
327 #ifndef EP_DEBUG_FREE
328 /* Round up to an 8 byte boundary. Make sure we have at least
329 * 8 pad bytes for our canary.
333 /* make sure we dont try to allocate too much (arbitrary limit) */
334 DISSECTOR_ASSERT(size<(EMEM_PACKET_CHUNK_SIZE>>2));
336 emem_create_chunk(&ep_packet_mem.free_list);
338 /* oops, we need to allocate more memory to serve this request
339 * than we have free. move this node to the used list and try again
341 if(size>ep_packet_mem.free_list->amount_free
342 #ifdef DEBUG_USE_CANARIES
343 || ep_packet_mem.free_list->c_count >= EMEM_ALLOCS_PER_CHUNK
344 #endif /* DEBUG_USE_CANARIES */
347 npc=ep_packet_mem.free_list;
348 ep_packet_mem.free_list=ep_packet_mem.free_list->next;
349 npc->next=ep_packet_mem.used_list;
350 ep_packet_mem.used_list=npc;
353 emem_create_chunk(&ep_packet_mem.free_list);
355 free_list = ep_packet_mem.free_list;
357 buf = free_list->buf + free_list->free_offset;
359 free_list->amount_free -= size;
360 free_list->free_offset += size;
362 #ifdef DEBUG_USE_CANARIES
363 cptr = (char *)buf + size - pad;
364 memcpy(cptr, &ep_canary, pad);
365 free_list->canary[free_list->c_count] = cptr;
366 free_list->cmp_len[free_list->c_count] = pad;
367 free_list->c_count++;
368 #endif /* DEBUG_USE_CANARIES */
370 #else /* EP_DEBUG_FREE */
373 npc=g_malloc(sizeof(emem_chunk_t));
374 npc->next=ep_packet_mem.used_list;
375 npc->amount_free=size;
377 npc->buf=g_malloc(size);
379 ep_packet_mem.used_list=npc;
380 #endif /* EP_DEBUG_FREE */
384 /* allocate 'size' amount of memory with an allocation lifetime until the
388 se_alloc(size_t size)
391 #ifndef SE_DEBUG_FREE
392 #ifdef DEBUG_USE_CANARIES
394 guint8 pad = emem_canary_pad(size);
397 #endif /* DEBUG_USE_CANARIES */
398 emem_chunk_t *free_list;
401 #ifndef SE_DEBUG_FREE
402 /* Round up to an 8 byte boundary. Make sure we have at least
403 * 8 pad bytes for our canary.
407 /* make sure we dont try to allocate too much (arbitrary limit) */
408 DISSECTOR_ASSERT(size<(EMEM_PACKET_CHUNK_SIZE>>2));
410 emem_create_chunk(&se_packet_mem.free_list);
412 /* oops, we need to allocate more memory to serve this request
413 * than we have free. move this node to the used list and try again
415 if(size>se_packet_mem.free_list->amount_free
416 #ifdef DEBUG_USE_CANARIES
417 || se_packet_mem.free_list->c_count >= EMEM_ALLOCS_PER_CHUNK
418 #endif /* DEBUG_USE_CANARIES */
421 npc=se_packet_mem.free_list;
422 se_packet_mem.free_list=se_packet_mem.free_list->next;
423 npc->next=se_packet_mem.used_list;
424 se_packet_mem.used_list=npc;
427 emem_create_chunk(&se_packet_mem.free_list);
429 free_list = se_packet_mem.free_list;
431 buf = free_list->buf + free_list->free_offset;
433 free_list->amount_free -= size;
434 free_list->free_offset += size;
436 #ifdef DEBUG_USE_CANARIES
437 cptr = (char *)buf + size - pad;
438 memcpy(cptr, &se_canary, pad);
439 free_list->canary[free_list->c_count] = cptr;
440 free_list->cmp_len[free_list->c_count] = pad;
441 free_list->c_count++;
442 #endif /* DEBUG_USE_CANARIES */
444 #else /* SE_DEBUG_FREE */
447 npc=g_malloc(sizeof(emem_chunk_t));
448 npc->next=se_packet_mem.used_list;
449 npc->amount_free=size;
451 npc->buf=g_malloc(size);
453 se_packet_mem.used_list=npc;
454 #endif /* SE_DEBUG_FREE */
460 void* ep_alloc0(size_t size) {
461 return memset(ep_alloc(size),'\0',size);
464 gchar* ep_strdup(const gchar* src) {
465 guint len = strlen(src);
468 dst = strncpy(ep_alloc(len+1), src, len);
475 gchar* ep_strndup(const gchar* src, size_t len) {
476 gchar* dst = ep_alloc(len+1);
479 for (i = 0; src[i] && i < len; i++)
487 void* ep_memdup(const void* src, size_t len) {
488 return memcpy(ep_alloc(len), src, len);
491 gchar* ep_strdup_vprintf(const gchar* fmt, va_list ap) {
498 len = g_printf_string_upper_bound(fmt, ap);
500 dst = ep_alloc(len+1);
501 g_vsnprintf (dst, len, fmt, ap2);
507 gchar* ep_strdup_printf(const gchar* fmt, ...) {
512 dst = ep_strdup_vprintf(fmt, ap);
517 gchar** ep_strsplit(const gchar* string, const gchar* sep, int max_tokens) {
525 enum { AT_START, IN_PAD, IN_TOKEN } state;
533 s = splitted = ep_strdup(string);
534 str_len = strlen(splitted);
535 sep_len = strlen(sep);
537 if (max_tokens < 1) max_tokens = INT_MAX;
542 while (tokens <= (guint)max_tokens && ( s = strstr(s,sep) )) {
545 for(i=0; i < sep_len; i++ )
552 vec = ep_alloc_array(gchar*,tokens+1);
555 for (i=0; i< str_len; i++) {
558 switch(splitted[i]) {
563 vec[curr_tok] = &(splitted[i]);
569 switch(splitted[i]) {
576 switch(splitted[i]) {
578 vec[curr_tok] = &(splitted[i]);
587 vec[curr_tok] = NULL;
594 void* se_alloc0(size_t size) {
595 return memset(se_alloc(size),'\0',size);
598 /* If str is NULL, just return the string "<NULL>" so that the callers dont
599 * have to bother checking it.
601 gchar* se_strdup(const gchar* src) {
610 dst = strncpy(se_alloc(len+1), src, len);
617 gchar* se_strndup(const gchar* src, size_t len) {
618 gchar* dst = se_alloc(len+1);
621 for (i = 0; src[i] && i < len; i++)
629 void* se_memdup(const void* src, size_t len) {
630 return memcpy(se_alloc(len), src, len);
633 gchar* se_strdup_vprintf(const gchar* fmt, va_list ap) {
640 len = g_printf_string_upper_bound(fmt, ap);
642 dst = se_alloc(len+1);
643 g_vsnprintf (dst, len, fmt, ap2);
649 gchar* se_strdup_printf(const gchar* fmt, ...) {
654 dst = se_strdup_vprintf(fmt, ap);
659 /* release all allocated memory back to the pool.
665 #ifndef EP_DEBUG_FREE
666 #ifdef DEBUG_USE_CANARIES
668 #endif /* DEBUG_USE_CANARIES */
671 /* move all used chunks over to the free list */
672 while(ep_packet_mem.used_list){
673 npc=ep_packet_mem.used_list;
674 ep_packet_mem.used_list=ep_packet_mem.used_list->next;
675 npc->next=ep_packet_mem.free_list;
676 ep_packet_mem.free_list=npc;
679 /* clear them all out */
680 npc = ep_packet_mem.free_list;
681 while (npc != NULL) {
682 #ifndef EP_DEBUG_FREE
683 #ifdef DEBUG_USE_CANARIES
684 for (i = 0; i < npc->c_count; i++) {
685 if (memcmp(npc->canary[i], &ep_canary, npc->cmp_len[i]) != 0)
686 g_error("Per-packet memory corrupted.");
689 #endif /* DEBUG_USE_CANARIES */
690 npc->amount_free = npc->amount_free_init;
691 npc->free_offset = npc->free_offset_init;
693 #else /* EP_DEBUG_FREE */
694 emem_chunk_t *next = npc->next;
699 #endif /* EP_DEBUG_FREE */
706 /* release all allocated memory back to the pool.
712 se_tree_t *se_tree_list;
713 #ifndef SE_DEBUG_FREE
714 #ifdef DEBUG_USE_CANARIES
716 #endif /* DEBUG_USE_CANARIES */
720 /* move all used chunks over to the free list */
721 while(se_packet_mem.used_list){
722 npc=se_packet_mem.used_list;
723 se_packet_mem.used_list=se_packet_mem.used_list->next;
724 npc->next=se_packet_mem.free_list;
725 se_packet_mem.free_list=npc;
728 /* clear them all out */
729 npc = se_packet_mem.free_list;
730 while (npc != NULL) {
731 #ifndef SE_DEBUG_FREE
732 #ifdef DEBUG_USE_CANARIES
733 for (i = 0; i < npc->c_count; i++) {
734 if (memcmp(npc->canary[i], &se_canary, npc->cmp_len[i]) != 0)
735 g_error("Per-session memory corrupted.");
738 #endif /* DEBUG_USE_CANARIES */
739 npc->amount_free = npc->amount_free_init;
740 npc->free_offset = npc->free_offset_init;
742 #else /* SE_DEBUG_FREE */
743 emem_chunk_t *next = npc->next;
748 #endif /* SE_DEBUG_FREE */
755 /* release/reset all se allocated trees */
756 for(se_tree_list=se_trees;se_tree_list;se_tree_list=se_tree_list->next){
757 se_tree_list->tree=NULL;
762 ep_stack_t ep_stack_new(void) {
763 ep_stack_t s = ep_new(struct _ep_stack_frame_t*);
764 *s = ep_new0(struct _ep_stack_frame_t);
768 /* for ep_stack_t we'll keep the popped frames so we reuse them instead
769 of allocating new ones.
773 void* ep_stack_push(ep_stack_t stack, void* data) {
774 struct _ep_stack_frame_t* frame;
775 struct _ep_stack_frame_t* head = (*stack);
780 frame = ep_new(struct _ep_stack_frame_t);
786 frame->payload = data;
792 void* ep_stack_pop(ep_stack_t stack) {
794 if ((*stack)->below) {
795 (*stack) = (*stack)->below;
796 return (*stack)->above->payload;
805 void print_tree_item(se_tree_node_t *node, int level){
807 for(i=0;i<level;i++){
810 printf("%s KEY:0x%08x node:0x%08x parent:0x%08x left:0x%08x right:0x%08x\n",node->u.rb_color==SE_TREE_RB_COLOR_BLACK?"BLACK":"RED",node->key32,(int)node,(int)node->parent,(int)node->left,(int)node->right);
812 print_tree_item(node->left,level+1);
814 print_tree_item(node->right,level+1);
817 void print_tree(se_tree_node_t *node){
824 print_tree_item(node,0);
830 /* routines to manage se allocated red-black trees */
831 se_tree_t *se_trees=NULL;
834 se_tree_create(int type, char *name)
836 se_tree_t *tree_list;
838 tree_list=malloc(sizeof(se_tree_t));
839 tree_list->next=se_trees;
840 tree_list->type=type;
841 tree_list->tree=NULL;
842 tree_list->name=name;
851 se_tree_lookup32(se_tree_t *se_tree, guint32 key)
853 se_tree_node_t *node;
858 if(key==node->key32){
874 se_tree_lookup32_le(se_tree_t *se_tree, guint32 key)
876 se_tree_node_t *node;
886 if(key==node->key32){
908 /* If we are still at the root of the tree this means that this node
909 * is either smaller thant the search key and then we return this
910 * node or else there is no smaller key availabel and then
921 if(node->parent->left==node){
925 /* if this is a left child and its key is smaller than
926 * the search key, then this is the node we want.
930 /* if this is a left child and its key is bigger than
931 * the search key, we have to check if any
932 * of our ancestors are smaller than the search key.
946 /* if this is the right child and its key is smaller
947 * than the search key then this is the one we want.
951 /* if this is the right child and its key is larger
952 * than the search key then our parent is the one we
955 return node->parent->data;
962 static inline se_tree_node_t *
963 se_tree_parent(se_tree_node_t *node)
968 static inline se_tree_node_t *
969 se_tree_grandparent(se_tree_node_t *node)
971 se_tree_node_t *parent;
973 parent=se_tree_parent(node);
975 return parent->parent;
979 static inline se_tree_node_t *
980 se_tree_uncle(se_tree_node_t *node)
982 se_tree_node_t *parent, *grandparent;
984 parent=se_tree_parent(node);
988 grandparent=se_tree_parent(parent);
992 if(parent==grandparent->left){
993 return grandparent->right;
995 return grandparent->left;
998 static inline void rb_insert_case1(se_tree_t *se_tree, se_tree_node_t *node);
999 static inline void rb_insert_case2(se_tree_t *se_tree, se_tree_node_t *node);
1002 rotate_left(se_tree_t *se_tree, se_tree_node_t *node)
1005 if(node->parent->left==node){
1006 node->parent->left=node->right;
1008 node->parent->right=node->right;
1011 se_tree->tree=node->right;
1013 node->right->parent=node->parent;
1014 node->parent=node->right;
1015 node->right=node->right->left;
1017 node->right->parent=node;
1019 node->parent->left=node;
1023 rotate_right(se_tree_t *se_tree, se_tree_node_t *node)
1026 if(node->parent->left==node){
1027 node->parent->left=node->left;
1029 node->parent->right=node->left;
1032 se_tree->tree=node->left;
1034 node->left->parent=node->parent;
1035 node->parent=node->left;
1036 node->left=node->left->right;
1038 node->left->parent=node;
1040 node->parent->right=node;
1044 rb_insert_case5(se_tree_t *se_tree, se_tree_node_t *node)
1046 se_tree_node_t *grandparent;
1047 se_tree_node_t *parent;
1049 parent=se_tree_parent(node);
1050 grandparent=se_tree_parent(parent);
1051 parent->u.rb_color=SE_TREE_RB_COLOR_BLACK;
1052 grandparent->u.rb_color=SE_TREE_RB_COLOR_RED;
1053 if( (node==parent->left) && (parent==grandparent->left) ){
1054 rotate_right(se_tree, grandparent);
1056 rotate_left(se_tree, grandparent);
1061 rb_insert_case4(se_tree_t *se_tree, se_tree_node_t *node)
1063 se_tree_node_t *grandparent;
1064 se_tree_node_t *parent;
1066 parent=se_tree_parent(node);
1067 grandparent=se_tree_parent(parent);
1071 if( (node==parent->right) && (parent==grandparent->left) ){
1072 rotate_left(se_tree, parent);
1074 } else if( (node==parent->left) && (parent==grandparent->right) ){
1075 rotate_right(se_tree, parent);
1078 rb_insert_case5(se_tree, node);
1082 rb_insert_case3(se_tree_t *se_tree, se_tree_node_t *node)
1084 se_tree_node_t *grandparent;
1085 se_tree_node_t *parent;
1086 se_tree_node_t *uncle;
1088 uncle=se_tree_uncle(node);
1089 if(uncle && (uncle->u.rb_color==SE_TREE_RB_COLOR_RED)){
1090 parent=se_tree_parent(node);
1091 parent->u.rb_color=SE_TREE_RB_COLOR_BLACK;
1092 uncle->u.rb_color=SE_TREE_RB_COLOR_BLACK;
1093 grandparent=se_tree_grandparent(node);
1094 grandparent->u.rb_color=SE_TREE_RB_COLOR_RED;
1095 rb_insert_case1(se_tree, grandparent);
1097 rb_insert_case4(se_tree, node);
1102 rb_insert_case2(se_tree_t *se_tree, se_tree_node_t *node)
1104 se_tree_node_t *parent;
1106 parent=se_tree_parent(node);
1107 /* parent is always non-NULL here */
1108 if(parent->u.rb_color==SE_TREE_RB_COLOR_BLACK){
1111 rb_insert_case3(se_tree, node);
1115 rb_insert_case1(se_tree_t *se_tree, se_tree_node_t *node)
1117 se_tree_node_t *parent;
1119 parent=se_tree_parent(node);
1121 node->u.rb_color=SE_TREE_RB_COLOR_BLACK;
1124 rb_insert_case2(se_tree, node);
1127 /* insert a new node in the tree. if this node matches an already existing node
1128 * then just replace the data for that node */
1130 se_tree_insert32(se_tree_t *se_tree, guint32 key, void *data)
1132 se_tree_node_t *node;
1136 /* is this the first node ?*/
1138 node=se_alloc(sizeof(se_tree_node_t));
1139 switch(se_tree->type){
1140 case SE_TREE_TYPE_RED_BLACK:
1141 node->u.rb_color=SE_TREE_RB_COLOR_BLACK;
1153 /* it was not the new root so walk the tree until we find where to
1154 * insert this new leaf.
1157 /* this node already exists, so just replace the data pointer*/
1158 if(key==node->key32){
1162 if(key<node->key32) {
1164 /* new node to the left */
1165 se_tree_node_t *new_node;
1166 new_node=se_alloc(sizeof(se_tree_node_t));
1167 node->left=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;
1179 if(key>node->key32) {
1181 /* new node to the right */
1182 se_tree_node_t *new_node;
1183 new_node=se_alloc(sizeof(se_tree_node_t));
1184 node->right=new_node;
1185 new_node->parent=node;
1186 new_node->left=NULL;
1187 new_node->right=NULL;
1188 new_node->key32=key;
1189 new_node->data=data;
1198 /* node will now point to the newly created node */
1199 switch(se_tree->type){
1200 case SE_TREE_TYPE_RED_BLACK:
1201 node->u.rb_color=SE_TREE_RB_COLOR_RED;
1202 rb_insert_case1(se_tree, node);
1207 static void* lookup_or_insert32(se_tree_t *se_tree, guint32 key, void*(*func)(void*),void* ud) {
1208 se_tree_node_t *node;
1212 /* is this the first node ?*/
1214 node=se_alloc(sizeof(se_tree_node_t));
1215 switch(se_tree->type){
1216 case SE_TREE_TYPE_RED_BLACK:
1217 node->u.rb_color=SE_TREE_RB_COLOR_BLACK;
1224 node->data= func(ud);
1229 /* it was not the new root so walk the tree until we find where to
1230 * insert this new leaf.
1233 /* this node already exists, so just return the data pointer*/
1234 if(key==node->key32){
1237 if(key<node->key32) {
1239 /* new node to the left */
1240 se_tree_node_t *new_node;
1241 new_node=se_alloc(sizeof(se_tree_node_t));
1242 node->left=new_node;
1243 new_node->parent=node;
1244 new_node->left=NULL;
1245 new_node->right=NULL;
1246 new_node->key32=key;
1247 new_node->data= func(ud);
1254 if(key>node->key32) {
1256 /* new node to the right */
1257 se_tree_node_t *new_node;
1258 new_node=se_alloc(sizeof(se_tree_node_t));
1259 node->right=new_node;
1260 new_node->parent=node;
1261 new_node->left=NULL;
1262 new_node->right=NULL;
1263 new_node->key32=key;
1264 new_node->data= func(ud);
1273 /* node will now point to the newly created node */
1274 switch(se_tree->type){
1275 case SE_TREE_TYPE_RED_BLACK:
1276 node->u.rb_color=SE_TREE_RB_COLOR_RED;
1277 rb_insert_case1(se_tree, node);
1284 /* When the se data is released, this entire tree will dissapear as if it
1285 * never existed including all metadata associated with the tree.
1288 se_tree_create_non_persistent(int type, char *name)
1290 se_tree_t *tree_list;
1292 tree_list=se_alloc(sizeof(se_tree_t));
1293 tree_list->next=NULL;
1294 tree_list->type=type;
1295 tree_list->tree=NULL;
1296 tree_list->name=name;
1301 static void* create_sub_tree(void* d) {
1302 se_tree_t *se_tree = d;
1303 return se_tree_create_non_persistent(se_tree->type, "subtree");
1306 /* insert a new node in the tree. if this node matches an already existing node
1307 * then just replace the data for that node */
1310 se_tree_insert32_array(se_tree_t *se_tree, se_tree_key_t *key, void *data)
1312 se_tree_t *next_tree;
1314 if((key[0].length<1)||(key[0].length>100)){
1315 DISSECTOR_ASSERT_NOT_REACHED();
1317 if((key[0].length==1)&&(key[1].length==0)){
1318 se_tree_insert32(se_tree, *key[0].key, data);
1322 next_tree=lookup_or_insert32(se_tree, *key[0].key, create_sub_tree, se_tree);
1324 if(key[0].length==1){
1330 se_tree_insert32_array(next_tree, key, data);
1334 se_tree_lookup32_array(se_tree_t *se_tree, se_tree_key_t *key)
1336 se_tree_t *next_tree;
1338 if((key[0].length<1)||(key[0].length>100)){
1339 DISSECTOR_ASSERT_NOT_REACHED();
1341 if((key[0].length==1)&&(key[1].length==0)){
1342 return se_tree_lookup32(se_tree, *key[0].key);
1344 next_tree=se_tree_lookup32(se_tree, *key[0].key);
1348 if(key[0].length==1){
1354 return se_tree_lookup32_array(next_tree, key);
1358 void se_tree_insert_string(se_string_hash_t* se_tree, const gchar* k, void* v) {
1359 guint32 len = strlen(k);
1360 guint32 div = (len-1)/4;
1361 guint32 residual = 0;
1362 se_tree_key_t key[] = {
1364 {div,(guint32*)(&k[0])},
1370 key[1].length = key[2].length;
1371 key[1].key = key[2].key;
1380 residual |= ( k[div+3] << 24 );
1382 residual |= ( k[div+2] << 16 );
1384 residual |= ( k[div+1] << 8 );
1390 se_tree_insert32_array(se_tree,key,v);
1393 void* se_tree_lookup_string(se_string_hash_t* se_tree, const gchar* k) {
1394 guint32 len = strlen(k);
1395 guint32 div = (len-1)/4;
1396 guint32 residual = 0;
1397 se_tree_key_t key[] = {
1399 {div,(guint32*)(&k[0])},
1405 key[1].length = key[2].length;
1406 key[1].key = key[2].key;
1415 residual |= k[div+3] << 24;
1417 residual |= k[div+2] << 16;
1419 residual |= k[div+1] << 8;
1425 return se_tree_lookup32_array(se_tree, key);