Fix some warnings
[metze/wireshark/wip.git] / epan / emem.c
1 /* emem.c
2  * Wireshark memory management and garbage collection functions
3  * Ronnie Sahlberg 2005
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
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.
15  *
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.
20  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 #include "config.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <ctype.h>
32
33 #include <time.h>
34 #ifdef HAVE_SYS_TIME_H
35 #include <sys/time.h>
36 #endif
37
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41
42 #include <glib.h>
43
44 #include "app_mem_usage.h"
45 #include "proto.h"
46 #include "exceptions.h"
47 #include "emem.h"
48 #include "wmem/wmem.h"
49
50 #ifdef _WIN32
51 #include <windows.h>    /* VirtualAlloc, VirtualProtect */
52 #include <process.h>    /* getpid */
53 #endif
54
55 /* Print out statistics about our memory allocations? */
56 /*#define SHOW_EMEM_STATS*/
57
58 /* Do we want to use guardpages? if available */
59 #define WANT_GUARD_PAGES 1
60
61 #ifdef WANT_GUARD_PAGES
62 /* Add guard pages at each end of our allocated memory */
63
64 #if defined(HAVE_SYSCONF) && defined(HAVE_MMAP) && defined(HAVE_MPROTECT) && defined(HAVE_STDINT_H)
65 #include <stdint.h>
66
67 #ifdef HAVE_SYS_TYPES_H
68 #include <sys/types.h>
69 #endif /* HAVE_SYS_TYPES_H */
70
71 #include <sys/mman.h>
72
73 #if defined(MAP_ANONYMOUS)
74 #define ANON_PAGE_MODE  (MAP_ANONYMOUS|MAP_PRIVATE)
75 #elif defined(MAP_ANON)
76 #define ANON_PAGE_MODE  (MAP_ANON|MAP_PRIVATE)
77 #else
78 #define ANON_PAGE_MODE  (MAP_PRIVATE)   /* have to map /dev/zero */
79 #define NEED_DEV_ZERO
80 #endif /* defined(MAP_ANONYMOUS) */
81
82 #ifdef NEED_DEV_ZERO
83 #include <fcntl.h>
84 static int dev_zero_fd;
85 #define ANON_FD dev_zero_fd
86 #else
87 #define ANON_FD -1
88 #endif /* NEED_DEV_ZERO */
89
90 #define USE_GUARD_PAGES 1
91 #endif /* defined(HAVE_SYSCONF) && defined(HAVE_MMAP) && defined(HAVE_MPROTECT) && defined(HAVE_STDINT_H) */
92 #endif /* WANT_GUARD_PAGES */
93
94 /* When required, allocate more memory from the OS in this size chunks */
95 #define EMEM_PACKET_CHUNK_SIZE (10 * 1024 * 1024)
96
97 /* The canary between allocations is at least 8 bytes and up to 16 bytes to
98  * allow future allocations to be 4- or 8-byte aligned.
99  * All but the last byte of the canary are randomly generated; the last byte is
100  * NULL to separate the canary and the pointer to the next canary.
101  *
102  * For example, if the allocation is a multiple of 8 bytes, the canary and
103  * pointer would look like:
104  *   |0|1|2|3|4|5|6|7||0|1|2|3|4|5|6|7|
105  *   |c|c|c|c|c|c|c|0||p|p|p|p|p|p|p|p| (64-bit), or:
106  *   |c|c|c|c|c|c|c|0||p|p|p|p|         (32-bit)
107  *
108  * If the allocation was, for example, 12 bytes, the canary would look like:
109  *        |0|1|2|3|4|5|6|7||0|1|2|3|4|5|6|7|
110  *   [...]|a|a|a|a|c|c|c|c||c|c|c|c|c|c|c|0| (followed by the pointer)
111  */
112 #define EMEM_CANARY_SIZE 8
113 #define EMEM_CANARY_DATA_SIZE (EMEM_CANARY_SIZE * 2 - 1)
114
115 typedef struct _emem_chunk_t {
116         struct _emem_chunk_t *next;
117         char            *buf;
118         size_t           size;
119         unsigned int    amount_free_init;
120         unsigned int    amount_free;
121         unsigned int    free_offset_init;
122         unsigned int    free_offset;
123         void            *canary_last;
124 } emem_chunk_t;
125
126 typedef struct _emem_pool_t {
127         emem_chunk_t *free_list;
128         emem_chunk_t *used_list;
129
130         emem_tree_t *trees;             /* only used by se_mem allocator */
131
132         guint8 canary[EMEM_CANARY_DATA_SIZE];
133         void *(*memory_alloc)(size_t size, struct _emem_pool_t *);
134
135         /*
136          * Tools like Valgrind and ElectricFence don't work well with memchunks.
137          * Export the following environment variables to make {ep|se}_alloc() allocate each
138          * object individually.
139          *
140          * WIRESHARK_DEBUG_EP_NO_CHUNKS
141          * WIRESHARK_DEBUG_SE_NO_CHUNKS
142          */
143         gboolean debug_use_chunks;
144
145         /* Do we want to use canaries?
146          * Export the following environment variables to disable/enable canaries
147          *
148          * WIRESHARK_DEBUG_EP_NO_CANARY
149          * For SE memory use of canary is default off as the memory overhead
150          * is considerable.
151          * WIRESHARK_DEBUG_SE_USE_CANARY
152          */
153         gboolean debug_use_canary;
154
155         /*  Do we want to verify no one is using a pointer to an ep_ or se_
156          *  allocated thing where they shouldn't be?
157          *
158          * Export WIRESHARK_EP_VERIFY_POINTERS or WIRESHARK_SE_VERIFY_POINTERS
159          * to turn this on.
160          */
161         gboolean debug_verify_pointers;
162
163 } emem_pool_t;
164
165 static emem_pool_t ep_packet_mem;
166 static emem_pool_t se_packet_mem;
167
168 /*
169  *  Memory scrubbing is expensive but can be useful to ensure we don't:
170  *    - use memory before initializing it
171  *    - use memory after freeing it
172  *  Export WIRESHARK_DEBUG_SCRUB_MEMORY to turn it on.
173  */
174 static gboolean debug_use_memory_scrubber = FALSE;
175
176 #if defined (_WIN32)
177 static SYSTEM_INFO sysinfo;
178 static gboolean iswindowsplatform;
179 static int pagesize;
180 #elif defined(USE_GUARD_PAGES)
181 static intptr_t pagesize;
182 #endif /* _WIN32 / USE_GUARD_PAGES */
183
184 static void *emem_alloc_chunk(size_t size, emem_pool_t *mem);
185 static void *emem_alloc_glib(size_t size, emem_pool_t *mem);
186
187 /*
188  * Set a canary value to be placed between memchunks.
189  */
190 static void
191 emem_canary_init(guint8 *canary)
192 {
193         int i;
194         static GRand *rand_state = NULL;
195
196         if (rand_state == NULL) {
197                 rand_state = g_rand_new();
198         }
199         for (i = 0; i < EMEM_CANARY_DATA_SIZE; i ++) {
200                 canary[i] = (guint8) g_rand_int_range(rand_state, 1, 0x100);
201         }
202         return;
203 }
204
205 static void *
206 emem_canary_next(guint8 *mem_canary, guint8 *canary, int *len)
207 {
208         void *ptr;
209         int i;
210
211         for (i = 0; i < EMEM_CANARY_SIZE-1; i++)
212                 if (mem_canary[i] != canary[i])
213                         return (void *) -1;
214
215         for (; i < EMEM_CANARY_DATA_SIZE; i++) {
216                 if (canary[i] == '\0') {
217                         memcpy(&ptr, &canary[i+1], sizeof(void *));
218
219                         if (len)
220                                 *len = i + 1 + (int)sizeof(void *);
221                         return ptr;
222                 }
223
224                 if (mem_canary[i] != canary[i])
225                         return (void *) -1;
226         }
227
228         return (void *) -1;
229 }
230
231 /*
232  * Given an allocation size, return the amount of room needed for the canary
233  * (with a minimum of 8 bytes) while using the canary to pad to an 8-byte
234  * boundary.
235  */
236 static guint8
237 emem_canary_pad (size_t allocation)
238 {
239         guint8 pad;
240
241         pad = EMEM_CANARY_SIZE - (allocation % EMEM_CANARY_SIZE);
242         if (pad < EMEM_CANARY_SIZE)
243                 pad += EMEM_CANARY_SIZE;
244
245         return pad;
246 }
247
248 /* used for debugging canaries, will block */
249 #ifdef DEBUG_INTENSE_CANARY_CHECKS
250 gboolean intense_canary_checking = FALSE;
251
252 /*  used to intensivelly check ep canaries
253  */
254 void
255 ep_check_canary_integrity(const char* fmt, ...)
256 {
257         va_list ap;
258         static gchar there[128] = {
259                 '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,
260                 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,
261                 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,
262                 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 };
263         gchar here[128];
264         emem_chunk_t* npc = NULL;
265
266         if (! intense_canary_checking ) return;
267
268         va_start(ap,fmt);
269         g_vsnprintf(here, sizeof(here), fmt, ap);
270         va_end(ap);
271
272         for (npc = ep_packet_mem.free_list; npc != NULL; npc = npc->next) {
273                 void *canary_next = npc->canary_last;
274
275                 while (canary_next != NULL) {
276                         canary_next = emem_canary_next(ep_packet_mem.canary, canary_next, NULL);
277                         /* XXX, check if canary_next is inside allocated memory? */
278
279                         if (canary_next == (void *) -1)
280                                 g_error("Per-packet memory corrupted\nbetween: %s\nand: %s", there, here);
281                 }
282         }
283
284         g_strlcpy(there, here, sizeof(there));
285 }
286 #endif
287
288 static void
289 emem_init_chunk(emem_pool_t *mem)
290 {
291         if (mem->debug_use_canary)
292                 emem_canary_init(mem->canary);
293
294         if (mem->debug_use_chunks)
295                 mem->memory_alloc = emem_alloc_chunk;
296         else
297                 mem->memory_alloc = emem_alloc_glib;
298 }
299
300 static gsize
301 emem_memory_usage(const emem_pool_t *pool)
302 {
303         gsize total_used = 0;
304         emem_chunk_t *chunk;
305
306         for (chunk = pool->used_list; chunk; chunk = chunk->next)
307                 total_used += (chunk->amount_free_init - chunk->amount_free);
308
309         for (chunk = pool->free_list; chunk; chunk = chunk->next)
310                 total_used += (chunk->amount_free_init - chunk->amount_free);
311
312         return total_used;
313 }
314
315 static gsize
316 ep_memory_usage(void)
317 {
318         return emem_memory_usage(&ep_packet_mem);
319 }
320
321 /* Initialize the packet-lifetime memory allocation pool.
322  * This function should be called only once when Wireshark or TShark starts
323  * up.
324  */
325 static void
326 ep_init_chunk(void)
327 {
328         static const ws_mem_usage_t ep_stats = { "EP", ep_memory_usage, NULL };
329
330         ep_packet_mem.free_list=NULL;
331         ep_packet_mem.used_list=NULL;
332         ep_packet_mem.trees=NULL;       /* not used by this allocator */
333
334         ep_packet_mem.debug_use_chunks = (getenv("WIRESHARK_DEBUG_EP_NO_CHUNKS") == NULL);
335         ep_packet_mem.debug_use_canary = ep_packet_mem.debug_use_chunks && (getenv("WIRESHARK_DEBUG_EP_NO_CANARY") == NULL);
336         ep_packet_mem.debug_verify_pointers = (getenv("WIRESHARK_EP_VERIFY_POINTERS") != NULL);
337
338 #ifdef DEBUG_INTENSE_CANARY_CHECKS
339         intense_canary_checking = (getenv("WIRESHARK_DEBUG_EP_INTENSE_CANARY") != NULL);
340 #endif
341
342         emem_init_chunk(&ep_packet_mem);
343
344         memory_usage_component_register(&ep_stats);
345 }
346
347 static gsize
348 se_memory_usage(void)
349 {
350         return emem_memory_usage(&se_packet_mem);
351 }
352
353 /* Initialize the capture-lifetime memory allocation pool.
354  * This function should be called only once when Wireshark or TShark starts
355  * up.
356  */
357 static void
358 se_init_chunk(void)
359 {
360         static const ws_mem_usage_t se_stats = { "SE", se_memory_usage, NULL };
361
362         se_packet_mem.free_list = NULL;
363         se_packet_mem.used_list = NULL;
364         se_packet_mem.trees = NULL;
365
366         se_packet_mem.debug_use_chunks = (getenv("WIRESHARK_DEBUG_SE_NO_CHUNKS") == NULL);
367         se_packet_mem.debug_use_canary = se_packet_mem.debug_use_chunks && (getenv("WIRESHARK_DEBUG_SE_USE_CANARY") != NULL);
368         se_packet_mem.debug_verify_pointers = (getenv("WIRESHARK_SE_VERIFY_POINTERS") != NULL);
369
370         emem_init_chunk(&se_packet_mem);
371
372         memory_usage_component_register(&se_stats);
373 }
374
375 /*  Initialize all the allocators here.
376  *  This function should be called only once when Wireshark or TShark starts
377  *  up.
378  */
379 void
380 emem_init(void)
381 {
382         ep_init_chunk();
383         se_init_chunk();
384
385         if (getenv("WIRESHARK_DEBUG_SCRUB_MEMORY"))
386                 debug_use_memory_scrubber  = TRUE;
387
388 #if defined (_WIN32)
389         /* Set up our guard page info for Win32 */
390         GetSystemInfo(&sysinfo);
391         pagesize = sysinfo.dwPageSize;
392
393 #if (_MSC_VER >= 1800)
394         /*
395          * On VS2103, GetVersionEx is deprecated. Microsoft recommend to
396          * use VerifyVersionInfo instead
397          */
398         {
399                 OSVERSIONINFOEX osvi;
400                 DWORDLONG dwlConditionMask = 0;
401                 int op = VER_EQUAL;
402
403                 SecureZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
404                 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
405                 osvi.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS;
406                 VER_SET_CONDITION(dwlConditionMask, VER_PLATFORMID, op);
407                 iswindowsplatform = VerifyVersionInfo(&osvi, VER_PLATFORMID, dwlConditionMask);
408         }
409 #else
410         /* calling GetVersionEx using the OSVERSIONINFO structure.
411          * OSVERSIONINFOEX requires Win NT4 with SP6 or newer NT Versions.
412          * OSVERSIONINFOEX will fail on Win9x and older NT Versions.
413          * See also:
414          * http://msdn.microsoft.com/library/en-us/sysinfo/base/getversionex.asp
415          * http://msdn.microsoft.com/library/en-us/sysinfo/base/osversioninfo_str.asp
416          * http://msdn.microsoft.com/library/en-us/sysinfo/base/osversioninfoex_str.asp
417          */
418         {
419                 OSVERSIONINFO versinfo;
420
421                 SecureZeroMemory(&versinfo, sizeof(OSVERSIONINFO));
422                 versinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
423                 GetVersionEx(&versinfo);
424                 iswindowsplatform = (versinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
425         }
426 #endif
427
428 #elif defined(USE_GUARD_PAGES)
429         pagesize = sysconf(_SC_PAGESIZE);
430         if (pagesize == -1)
431                 fprintf(stderr, "Warning: call to sysconf() for _SC_PAGESIZE has failed...\n");
432 #ifdef NEED_DEV_ZERO
433         dev_zero_fd = ws_open("/dev/zero", O_RDWR);
434         g_assert(dev_zero_fd != -1);
435 #endif
436 #endif /* _WIN32 / USE_GUARD_PAGES */
437 }
438
439 #ifdef SHOW_EMEM_STATS
440 #define NUM_ALLOC_DIST 10
441 static guint allocations[NUM_ALLOC_DIST] = { 0 };
442 static guint total_no_chunks = 0;
443
444 static void
445 print_alloc_stats(void)
446 {
447         guint num_chunks = 0;
448         guint num_allocs = 0;
449         guint total_used = 0;
450         guint total_allocation = 0;
451         guint used_for_canaries = 0;
452         guint total_headers;
453         guint i;
454         emem_chunk_t *chunk;
455         guint total_space_allocated_from_os, total_space_wasted;
456         gboolean ep_stat=TRUE;
457
458         fprintf(stderr, "\n-------- EP allocator statistics --------\n");
459         fprintf(stderr, "%s chunks, %s canaries, %s memory scrubber\n",
460                ep_packet_mem.debug_use_chunks ? "Using" : "Not using",
461                ep_packet_mem.debug_use_canary ? "using" : "not using",
462                debug_use_memory_scrubber ? "using" : "not using");
463
464         if (! (ep_packet_mem.free_list || !ep_packet_mem.used_list)) {
465                 fprintf(stderr, "No memory allocated\n");
466                 ep_stat = FALSE;
467         }
468         if (ep_packet_mem.debug_use_chunks && ep_stat) {
469                 /* Nothing interesting without chunks */
470                 /*  Only look at the used_list since those chunks are fully
471                  *  used.  Looking at the free list would skew our view of what
472                  *  we have wasted.
473                  */
474                 for (chunk = ep_packet_mem.used_list; chunk; chunk = chunk->next) {
475                         num_chunks++;
476                         total_used += (chunk->amount_free_init - chunk->amount_free);
477                         total_allocation += chunk->amount_free_init;
478                 }
479                 if (num_chunks > 0) {
480                         fprintf (stderr, "\n");
481                         fprintf (stderr, "\n---- Buffer space ----\n");
482                         fprintf (stderr, "\tChunk allocation size: %10u\n", EMEM_PACKET_CHUNK_SIZE);
483                         fprintf (stderr, "\t*    Number of chunks: %10u\n", num_chunks);
484                         fprintf (stderr, "\t-------------------------------------------\n");
485                         fprintf (stderr, "\t= %u (%u including guard pages) total space used for buffers\n",
486                         total_allocation, EMEM_PACKET_CHUNK_SIZE * num_chunks);
487                         fprintf (stderr, "\t-------------------------------------------\n");
488                         total_space_allocated_from_os = total_allocation
489                                 + sizeof(emem_chunk_t) * num_chunks;
490                         fprintf (stderr, "Total allocated from OS: %u\n\n",
491                                 total_space_allocated_from_os);
492                 }else{
493                         fprintf (stderr, "No fully used chunks, nothing to do\n");
494                 }
495                 /* Reset stats */
496                 num_chunks = 0;
497                 num_allocs = 0;
498                 total_used = 0;
499                 total_allocation = 0;
500                 used_for_canaries = 0;
501         }
502
503
504         fprintf(stderr, "\n-------- SE allocator statistics --------\n");
505         fprintf(stderr, "Total number of chunk allocations %u\n",
506                 total_no_chunks);
507         fprintf(stderr, "%s chunks, %s canaries\n",
508                se_packet_mem.debug_use_chunks ? "Using" : "Not using",
509                se_packet_mem.debug_use_canary ? "using" : "not using");
510
511         if (! (se_packet_mem.free_list || !se_packet_mem.used_list)) {
512                 fprintf(stderr, "No memory allocated\n");
513                 return;
514         }
515
516         if (!se_packet_mem.debug_use_chunks )
517                 return; /* Nothing interesting without chunks?? */
518
519         /*  Only look at the used_list since those chunks are fully used.
520          *  Looking at the free list would skew our view of what we have wasted.
521          */
522         for (chunk = se_packet_mem.used_list; chunk; chunk = chunk->next) {
523                 num_chunks++;
524                 total_used += (chunk->amount_free_init - chunk->amount_free);
525                 total_allocation += chunk->amount_free_init;
526
527                 if (se_packet_mem.debug_use_canary){
528                         void *ptr = chunk->canary_last;
529                         int len;
530
531                         while (ptr != NULL) {
532                                 ptr = emem_canary_next(se_packet_mem.canary, (guint8*)ptr, &len);
533
534                                 if (ptr == (void *) -1)
535                                         g_error("Memory corrupted");
536                                 used_for_canaries += len;
537                         }
538                 }
539         }
540
541         if (num_chunks == 0) {
542
543                 fprintf (stderr, "No fully used chunks, nothing to do\n");
544                 return;
545         }
546
547         fprintf (stderr, "\n");
548         fprintf (stderr, "---------- Allocations from the OS ----------\n");
549         fprintf (stderr, "---- Headers ----\n");
550         fprintf (stderr, "\t(    Chunk header size: %10lu\n",
551                  sizeof(emem_chunk_t));
552         fprintf (stderr, "\t*     Number of chunks: %10u\n", num_chunks);
553         fprintf (stderr, "\t-------------------------------------------\n");
554
555         total_headers = sizeof(emem_chunk_t) * num_chunks;
556         fprintf (stderr, "\t= %u bytes used for headers\n", total_headers);
557         fprintf (stderr, "\n---- Buffer space ----\n");
558         fprintf (stderr, "\tChunk allocation size: %10u\n",
559                  EMEM_PACKET_CHUNK_SIZE);
560         fprintf (stderr, "\t*    Number of chunks: %10u\n", num_chunks);
561         fprintf (stderr, "\t-------------------------------------------\n");
562         fprintf (stderr, "\t= %u (%u including guard pages) bytes used for buffers\n",
563                 total_allocation, EMEM_PACKET_CHUNK_SIZE * num_chunks);
564         fprintf (stderr, "\t-------------------------------------------\n");
565         total_space_allocated_from_os = (EMEM_PACKET_CHUNK_SIZE * num_chunks)
566                                         + total_headers;
567         fprintf (stderr, "Total bytes allocated from the OS: %u\n\n",
568                 total_space_allocated_from_os);
569
570         for (i = 0; i < NUM_ALLOC_DIST; i++)
571                 num_allocs += allocations[i];
572
573         fprintf (stderr, "---------- Allocations from the SE pool ----------\n");
574         fprintf (stderr, "                Number of SE allocations: %10u\n",
575                  num_allocs);
576         fprintf (stderr, "             Bytes used (incl. canaries): %10u\n",
577                  total_used);
578         fprintf (stderr, "                 Bytes used for canaries: %10u\n",
579                  used_for_canaries);
580         fprintf (stderr, "Bytes unused (wasted, excl. guard pages): %10u\n",
581                  total_allocation - total_used);
582         fprintf (stderr, "Bytes unused (wasted, incl. guard pages): %10u\n\n",
583                  total_space_allocated_from_os - total_used);
584
585         fprintf (stderr, "---------- Statistics ----------\n");
586         fprintf (stderr, "Average SE allocation size (incl. canaries): %6.2f\n",
587                 (float)total_used/(float)num_allocs);
588         fprintf (stderr, "Average SE allocation size (excl. canaries): %6.2f\n",
589                 (float)(total_used - used_for_canaries)/(float)num_allocs);
590         fprintf (stderr, "        Average wasted bytes per allocation: %6.2f\n",
591                 (total_allocation - total_used)/(float)num_allocs);
592         total_space_wasted = (total_allocation - total_used)
593                 + (sizeof(emem_chunk_t));
594         fprintf (stderr, " Space used for headers + unused allocation: %8u\n",
595                 total_space_wasted);
596         fprintf (stderr, "--> %% overhead/waste: %4.2f\n",
597                 100 * (float)total_space_wasted/(float)total_space_allocated_from_os);
598
599         fprintf (stderr, "\nAllocation distribution (sizes include canaries):\n");
600         for (i = 0; i < (NUM_ALLOC_DIST-1); i++)
601                 fprintf (stderr, "size < %5d: %8u\n", 32<<i, allocations[i]);
602         fprintf (stderr, "size > %5d: %8u\n", 32<<i, allocations[i]);
603 }
604 #endif
605
606 static gboolean
607 emem_verify_pointer_list(const emem_chunk_t *chunk_list, const void *ptr)
608 {
609         const gchar *cptr = (const gchar *)ptr;
610         const emem_chunk_t *chunk;
611
612         for (chunk = chunk_list; chunk; chunk = chunk->next) {
613                 if (cptr >= (chunk->buf + chunk->free_offset_init) && cptr < (chunk->buf + chunk->free_offset))
614                         return TRUE;
615         }
616         return FALSE;
617 }
618
619 static gboolean
620 emem_verify_pointer(const emem_pool_t *hdr, const void *ptr)
621 {
622         return emem_verify_pointer_list(hdr->free_list, ptr) || emem_verify_pointer_list(hdr->used_list, ptr);
623 }
624
625 gboolean
626 ep_verify_pointer(const void *ptr)
627 {
628         if (ep_packet_mem.debug_verify_pointers)
629                 return emem_verify_pointer(&ep_packet_mem, ptr);
630         else
631                 return FALSE;
632 }
633
634 gboolean
635 se_verify_pointer(const void *ptr)
636 {
637         if (se_packet_mem.debug_verify_pointers)
638                 return emem_verify_pointer(&se_packet_mem, ptr);
639         else
640                 return FALSE;
641 }
642
643 static void
644 emem_scrub_memory(char *buf, size_t size, gboolean alloc)
645 {
646         guint scrubbed_value;
647         size_t offset;
648
649         if (!debug_use_memory_scrubber)
650                 return;
651
652         if (alloc) /* this memory is being allocated */
653                 scrubbed_value = 0xBADDCAFE;
654         else /* this memory is being freed */
655                 scrubbed_value = 0xDEADBEEF;
656
657         /*  We shouldn't need to check the alignment of the starting address
658          *  since this is malloc'd memory (or 'pagesize' bytes into malloc'd
659          *  memory).
660          */
661
662         /* XXX - if the above is *NOT* true, we should use memcpy here,
663          * in order to avoid problems on alignment-sensitive platforms, e.g.
664          * http://stackoverflow.com/questions/108866/is-there-memset-that-accepts-integers-larger-than-char
665          */
666
667         for (offset = 0; offset + sizeof(guint) <= size; offset += sizeof(guint))
668                 *(guint*)(void*)(buf+offset) = scrubbed_value;
669
670         /* Initialize the last bytes, if any */
671         if (offset < size) {
672                 *(guint8*)(buf+offset) = scrubbed_value >> 24;
673                 offset++;
674                 if (offset < size) {
675                         *(guint8*)(buf+offset) = (scrubbed_value >> 16) & 0xFF;
676                         offset++;
677                         if (offset < size) {
678                                 *(guint8*)(buf+offset) = (scrubbed_value >> 8) & 0xFF;
679                         }
680                 }
681         }
682
683
684 }
685
686 static emem_chunk_t *
687 emem_create_chunk(size_t size)
688 {
689         emem_chunk_t *npc;
690
691         npc = g_new(emem_chunk_t, 1);
692         npc->next = NULL;
693         npc->canary_last = NULL;
694
695 #if defined (_WIN32)
696         /*
697          * MSDN documents VirtualAlloc/VirtualProtect at
698          * http://msdn.microsoft.com/library/en-us/memory/base/creating_guard_pages.asp
699          */
700
701         /* XXX - is MEM_COMMIT|MEM_RESERVE correct? */
702         npc->buf = (char *)VirtualAlloc(NULL, size,
703                 MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
704
705         if (npc->buf == NULL) {
706                 g_free(npc);
707                 if (getenv("WIRESHARK_ABORT_ON_OUT_OF_MEMORY"))
708                         abort();
709                 else
710                         THROW(OutOfMemoryError);
711         }
712
713 #elif defined(USE_GUARD_PAGES)
714         npc->buf = (char *)mmap(NULL, size,
715                 PROT_READ|PROT_WRITE, ANON_PAGE_MODE, ANON_FD, 0);
716
717         if (npc->buf == MAP_FAILED) {
718                 g_free(npc);
719                 if (getenv("WIRESHARK_ABORT_ON_OUT_OF_MEMORY"))
720                         abort();
721                 else
722                         THROW(OutOfMemoryError);
723         }
724
725 #else /* Is there a draft in here? */
726         npc->buf = g_malloc(size);
727         /* g_malloc() can't fail */
728 #endif
729
730 #ifdef SHOW_EMEM_STATS
731         total_no_chunks++;
732 #endif
733
734         npc->amount_free = npc->amount_free_init = (unsigned int) size;
735         npc->free_offset = npc->free_offset_init = 0;
736         return npc;
737 }
738
739 static emem_chunk_t *
740 emem_create_chunk_gp(size_t size)
741 {
742 #if defined (_WIN32)
743         BOOL ret;
744         char *buf_end, *prot1, *prot2;
745         DWORD oldprot;
746 #elif defined(USE_GUARD_PAGES)
747         int ret;
748         char *buf_end, *prot1, *prot2;
749 #endif /* _WIN32 / USE_GUARD_PAGES */
750         emem_chunk_t *npc;
751
752         npc = emem_create_chunk(size);
753
754 #if defined (_WIN32)
755         buf_end = npc->buf + size;
756
757         /* Align our guard pages on page-sized boundaries */
758         prot1 = (char *) ((((intptr_t) npc->buf + pagesize - 1) / pagesize) * pagesize);
759         prot2 = (char *) ((((intptr_t) buf_end - (1 * pagesize)) / pagesize) * pagesize);
760
761         ret = VirtualProtect(prot1, pagesize, PAGE_NOACCESS, &oldprot);
762         g_assert(ret != 0 || iswindowsplatform);
763         ret = VirtualProtect(prot2, pagesize, PAGE_NOACCESS, &oldprot);
764         g_assert(ret != 0 || iswindowsplatform);
765
766         npc->amount_free_init = (unsigned int) (prot2 - prot1 - pagesize);
767         npc->free_offset_init = (unsigned int) (prot1 - npc->buf) + pagesize;
768 #elif defined(USE_GUARD_PAGES)
769         buf_end = npc->buf + size;
770
771         /* Align our guard pages on page-sized boundaries */
772         prot1 = (char *) ((((intptr_t) npc->buf + pagesize - 1) / pagesize) * pagesize);
773         prot2 = (char *) ((((intptr_t) buf_end - (1 * pagesize)) / pagesize) * pagesize);
774
775         ret = mprotect(prot1, pagesize, PROT_NONE);
776         g_assert(ret != -1);
777         ret = mprotect(prot2, pagesize, PROT_NONE);
778         g_assert(ret != -1);
779
780         npc->amount_free_init = (unsigned int)(prot2 - prot1 - pagesize);
781         npc->free_offset_init = (unsigned int)((prot1 - npc->buf) + pagesize);
782 #else
783         npc->amount_free_init = size;
784         npc->free_offset_init = 0;
785 #endif /* USE_GUARD_PAGES */
786
787         npc->amount_free = npc->amount_free_init;
788         npc->free_offset = npc->free_offset_init;
789         return npc;
790 }
791
792 static void *
793 emem_alloc_chunk(size_t size, emem_pool_t *mem)
794 {
795         void *buf;
796
797         size_t asize = size;
798         gboolean use_canary = mem->debug_use_canary;
799         guint8 pad;
800         emem_chunk_t *free_list;
801
802         /* Allocate room for at least 8 bytes of canary plus some padding
803          * so the canary ends on an 8-byte boundary.
804          * But first add the room needed for the pointer to the next canary
805          * (so the entire allocation will end on an 8-byte boundary).
806          */
807          if (use_canary) {
808                 asize += sizeof(void *);
809                 pad = emem_canary_pad(asize);
810         } else
811                 pad = (WS_MEM_ALIGN - (asize & (WS_MEM_ALIGN-1))) & (WS_MEM_ALIGN-1);
812
813         asize += pad;
814
815 #ifdef SHOW_EMEM_STATS
816         /* Do this check here so we can include the canary size */
817         if (mem == &se_packet_mem) {
818                 if (asize < 32)
819                         allocations[0]++;
820                 else if (asize < 64)
821                         allocations[1]++;
822                 else if (asize < 128)
823                         allocations[2]++;
824                 else if (asize < 256)
825                         allocations[3]++;
826                 else if (asize < 512)
827                         allocations[4]++;
828                 else if (asize < 1024)
829                         allocations[5]++;
830                 else if (asize < 2048)
831                         allocations[6]++;
832                 else if (asize < 4096)
833                         allocations[7]++;
834                 else if (asize < 8192)
835                         allocations[8]++;
836                 else if (asize < 16384)
837                         allocations[8]++;
838                 else
839                         allocations[(NUM_ALLOC_DIST-1)]++;
840         }
841 #endif
842
843         /* make sure we dont try to allocate too much (arbitrary limit) */
844         DISSECTOR_ASSERT(size<(EMEM_PACKET_CHUNK_SIZE>>2));
845
846         if (!mem->free_list)
847                 mem->free_list = emem_create_chunk_gp(EMEM_PACKET_CHUNK_SIZE);
848
849         /* oops, we need to allocate more memory to serve this request
850          * than we have free. move this node to the used list and try again
851          */
852         if(asize > mem->free_list->amount_free) {
853                 emem_chunk_t *npc;
854                 npc=mem->free_list;
855                 mem->free_list=mem->free_list->next;
856                 npc->next=mem->used_list;
857                 mem->used_list=npc;
858
859                 if (!mem->free_list)
860                         mem->free_list = emem_create_chunk_gp(EMEM_PACKET_CHUNK_SIZE);
861         }
862
863         free_list = mem->free_list;
864
865         buf = free_list->buf + free_list->free_offset;
866
867         free_list->amount_free -= (unsigned int) asize;
868         free_list->free_offset += (unsigned int) asize;
869
870         if (use_canary) {
871                 char *cptr = (char *)buf + size;
872
873                 memcpy(cptr, mem->canary, pad-1);
874                 cptr[pad-1] = '\0';
875                 memcpy(cptr + pad, &free_list->canary_last, sizeof(void *));
876
877                 free_list->canary_last = cptr;
878         }
879
880         return buf;
881 }
882
883 static void *
884 emem_alloc_glib(size_t size, emem_pool_t *mem)
885 {
886         emem_chunk_t *npc;
887
888         npc=g_new(emem_chunk_t, 1);
889         npc->next=mem->used_list;
890         npc->buf=(char *)g_malloc(size);
891         npc->canary_last = NULL;
892         mem->used_list=npc;
893         /* There's no padding/alignment involved (from our point of view) when
894          * we fetch the memory directly from the system pool, so WYSIWYG */
895         npc->amount_free = npc->free_offset_init = 0;
896         npc->free_offset = npc->amount_free_init = (unsigned int) size;
897
898         return npc->buf;
899 }
900
901 /* allocate 'size' amount of memory. */
902 static void *
903 emem_alloc(size_t size, emem_pool_t *mem)
904 {
905         void *buf;
906
907 #if 0
908         /* For testing wmem, effectively redirects most emem memory to wmem.
909          * You will also have to comment out several assertions in wmem_core.c,
910          * specifically anything g_assert(allocator->in_scope), since it is much
911          * stricter about when it is permitted to be called. */
912         if (mem == &ep_packet_mem) {
913                 return wmem_alloc(wmem_packet_scope(), size);
914         }
915         else if (mem == &se_packet_mem) {
916                 return wmem_alloc(wmem_file_scope(), size);
917         }
918 #endif
919
920         buf = mem->memory_alloc(size, mem);
921
922         /*  XXX - this is a waste of time if the allocator function is going to
923          *  memset this straight back to 0.
924          */
925         emem_scrub_memory((char *)buf, size, TRUE);
926
927         return buf;
928 }
929
930 /* allocate 'size' amount of memory with an allocation lifetime until the
931  * next packet.
932  */
933 void *
934 ep_alloc(size_t size)
935 {
936         return emem_alloc(size, &ep_packet_mem);
937 }
938
939 /* allocate 'size' amount of memory with an allocation lifetime until the
940  * next capture.
941  */
942 void *
943 se_alloc(size_t size)
944 {
945         return emem_alloc(size, &se_packet_mem);
946 }
947
948 void *
949 ep_alloc0(size_t size)
950 {
951         return memset(ep_alloc(size),'\0',size);
952 }
953
954 void *
955 se_alloc0(size_t size)
956 {
957         return memset(se_alloc(size),'\0',size);
958 }
959
960 static gchar *
961 emem_strdup(const gchar *src, void *allocator(size_t))
962 {
963         guint len;
964         gchar *dst;
965
966         /* If str is NULL, just return the string "<NULL>" so that the callers don't
967          * have to bother checking it.
968          */
969         if(!src)
970                 src = "<NULL>";
971
972         len = (guint) strlen(src);
973         dst = (gchar *)memcpy(allocator(len+1), src, len+1);
974
975         return dst;
976 }
977
978 gchar *
979 ep_strdup(const gchar *src)
980 {
981         return emem_strdup(src, ep_alloc);
982 }
983
984 gchar *
985 se_strdup(const gchar *src)
986 {
987         return emem_strdup(src, se_alloc);
988 }
989
990 static gchar *
991 emem_strndup(const gchar *src, size_t len, void *allocator(size_t))
992 {
993         gchar *dst = (gchar *)allocator(len+1);
994         guint i;
995
996         for (i = 0; (i < len) && src[i]; i++)
997                 dst[i] = src[i];
998
999         dst[i] = '\0';
1000
1001         return dst;
1002 }
1003
1004 gchar *
1005 ep_strndup(const gchar *src, size_t len)
1006 {
1007         return emem_strndup(src, len, ep_alloc);
1008 }
1009
1010 gchar *
1011 se_strndup(const gchar *src, size_t len)
1012 {
1013         return emem_strndup(src, len, se_alloc);
1014 }
1015
1016
1017
1018 void *
1019 ep_memdup(const void* src, size_t len)
1020 {
1021         return memcpy(ep_alloc(len), src, len);
1022 }
1023
1024 void *
1025 se_memdup(const void* src, size_t len)
1026 {
1027         return memcpy(se_alloc(len), src, len);
1028 }
1029
1030 static gchar *
1031 emem_strdup_vprintf(const gchar *fmt, va_list ap, void *allocator(size_t))
1032 {
1033         va_list ap2;
1034         gsize len;
1035         gchar* dst;
1036
1037         G_VA_COPY(ap2, ap);
1038
1039         len = g_printf_string_upper_bound(fmt, ap);
1040
1041         dst = (gchar *)allocator(len+1);
1042         g_vsnprintf (dst, (gulong) len, fmt, ap2);
1043         va_end(ap2);
1044
1045         return dst;
1046 }
1047
1048 gchar *
1049 ep_strdup_vprintf(const gchar *fmt, va_list ap)
1050 {
1051         return emem_strdup_vprintf(fmt, ap, ep_alloc);
1052 }
1053
1054 gchar *
1055 se_strdup_vprintf(const gchar* fmt, va_list ap)
1056 {
1057         return emem_strdup_vprintf(fmt, ap, se_alloc);
1058 }
1059
1060 gchar *
1061 ep_strdup_printf(const gchar *fmt, ...)
1062 {
1063         va_list ap;
1064         gchar *dst;
1065
1066         va_start(ap, fmt);
1067         dst = ep_strdup_vprintf(fmt, ap);
1068         va_end(ap);
1069         return dst;
1070 }
1071
1072 gchar *
1073 se_strdup_printf(const gchar *fmt, ...)
1074 {
1075         va_list ap;
1076         gchar *dst;
1077
1078         va_start(ap, fmt);
1079         dst = se_strdup_vprintf(fmt, ap);
1080         va_end(ap);
1081         return dst;
1082 }
1083
1084 gchar **
1085 ep_strsplit(const gchar* string, const gchar* sep, int max_tokens)
1086 {
1087         gchar* splitted;
1088         gchar* s;
1089         guint tokens;
1090         guint str_len;
1091         guint sep_len;
1092         guint i;
1093         gchar** vec;
1094         enum { AT_START, IN_PAD, IN_TOKEN } state;
1095         guint curr_tok = 0;
1096
1097         if (    ! string
1098              || ! sep
1099              || ! sep[0])
1100                 return NULL;
1101
1102         s = splitted = ep_strdup(string);
1103         str_len = (guint) strlen(splitted);
1104         sep_len = (guint) strlen(sep);
1105
1106         if (max_tokens < 1) max_tokens = INT_MAX;
1107
1108         tokens = 1;
1109
1110
1111         while (tokens <= (guint)max_tokens && ( s = strstr(s,sep) )) {
1112                 tokens++;
1113
1114                 for(i=0; i < sep_len; i++ )
1115                         s[i] = '\0';
1116
1117                 s += sep_len;
1118
1119         }
1120
1121         vec = ep_alloc_array(gchar*,tokens+1);
1122         state = AT_START;
1123
1124         for (i=0; i< str_len; i++) {
1125                 switch(state) {
1126                         case AT_START:
1127                                 switch(splitted[i]) {
1128                                         case '\0':
1129                                                 state  = IN_PAD;
1130                                                 continue;
1131                                         default:
1132                                                 vec[curr_tok] = &(splitted[i]);
1133                                                 curr_tok++;
1134                                                 state = IN_TOKEN;
1135                                                 continue;
1136                                 }
1137                         case IN_TOKEN:
1138                                 switch(splitted[i]) {
1139                                         case '\0':
1140                                                 state = IN_PAD;
1141                                         default:
1142                                                 continue;
1143                                 }
1144                         case IN_PAD:
1145                                 switch(splitted[i]) {
1146                                         default:
1147                                                 vec[curr_tok] = &(splitted[i]);
1148                                                 curr_tok++;
1149                                                 state = IN_TOKEN;
1150                                         case '\0':
1151                                                 continue;
1152                                 }
1153                 }
1154         }
1155
1156         vec[curr_tok] = NULL;
1157
1158         return vec;
1159 }
1160
1161 gchar *
1162 ep_strconcat(const gchar *string1, ...)
1163 {
1164         gsize   l;
1165         va_list args;
1166         gchar   *s;
1167         gchar   *concat;
1168         gchar   *ptr;
1169
1170         if (!string1)
1171                 return NULL;
1172
1173         l = 1 + strlen(string1);
1174         va_start(args, string1);
1175         s = va_arg(args, gchar*);
1176         while (s) {
1177                 l += strlen(s);
1178                 s = va_arg(args, gchar*);
1179         }
1180         va_end(args);
1181
1182         concat = (gchar *)ep_alloc(l);
1183         ptr = concat;
1184
1185         ptr = g_stpcpy(ptr, string1);
1186         va_start(args, string1);
1187         s = va_arg(args, gchar*);
1188         while (s) {
1189                 ptr = g_stpcpy(ptr, s);
1190                 s = va_arg(args, gchar*);
1191         }
1192         va_end(args);
1193
1194         return concat;
1195 }
1196
1197
1198
1199 /* release all allocated memory back to the pool. */
1200 static void
1201 emem_free_all(emem_pool_t *mem)
1202 {
1203         gboolean use_chunks = mem->debug_use_chunks;
1204
1205         emem_chunk_t *npc;
1206         emem_tree_t *tree_list;
1207
1208         /* move all used chunks over to the free list */
1209         while(mem->used_list){
1210                 npc=mem->used_list;
1211                 mem->used_list=mem->used_list->next;
1212                 npc->next=mem->free_list;
1213                 mem->free_list=npc;
1214         }
1215
1216         /* clear them all out */
1217         npc = mem->free_list;
1218         while (npc != NULL) {
1219                 if (use_chunks) {
1220                         while (npc->canary_last != NULL) {
1221                                 npc->canary_last = emem_canary_next(mem->canary, (guint8 *)npc->canary_last, NULL);
1222                                 /* XXX, check if canary_last is inside allocated memory? */
1223
1224                                 if (npc->canary_last == (void *) -1)
1225                                         g_error("Memory corrupted");
1226                         }
1227
1228                         emem_scrub_memory((npc->buf + npc->free_offset_init),
1229                                           (npc->free_offset - npc->free_offset_init),
1230                                           FALSE);
1231
1232                         npc->amount_free = npc->amount_free_init;
1233                         npc->free_offset = npc->free_offset_init;
1234                         npc = npc->next;
1235                 } else {
1236                         emem_chunk_t *next = npc->next;
1237
1238                         emem_scrub_memory(npc->buf, npc->amount_free_init, FALSE);
1239
1240                         g_free(npc->buf);
1241                         g_free(npc);
1242                         npc = next;
1243                 }
1244         }
1245
1246         if (!use_chunks) {
1247                 /* We've freed all this memory already */
1248                 mem->free_list = NULL;
1249         }
1250
1251         /* release/reset all allocated trees */
1252         for(tree_list=mem->trees;tree_list;tree_list=tree_list->next){
1253                 tree_list->tree=NULL;
1254         }
1255 }
1256
1257 /* release all allocated memory back to the pool. */
1258 void
1259 ep_free_all(void)
1260 {
1261         emem_free_all(&ep_packet_mem);
1262 }
1263
1264 /* release all allocated memory back to the pool. */
1265 void
1266 se_free_all(void)
1267 {
1268 #ifdef SHOW_EMEM_STATS
1269         print_alloc_stats();
1270 #endif
1271
1272         emem_free_all(&se_packet_mem);
1273 }
1274
1275 ep_stack_t
1276 ep_stack_new(void) {
1277         ep_stack_t s = ep_new(struct _ep_stack_frame_t*);
1278         *s = ep_new0(struct _ep_stack_frame_t);
1279         return s;
1280 }
1281
1282 /*  for ep_stack_t we'll keep the popped frames so we reuse them instead
1283 of allocating new ones.
1284 */
1285
1286 void *
1287 ep_stack_push(ep_stack_t stack, void* data)
1288 {
1289         struct _ep_stack_frame_t* frame;
1290         struct _ep_stack_frame_t* head = (*stack);
1291
1292         if (head->above) {
1293                 frame = head->above;
1294         } else {
1295                 frame = ep_new(struct _ep_stack_frame_t);
1296                 head->above = frame;
1297                 frame->below = head;
1298                 frame->above = NULL;
1299         }
1300
1301         frame->payload = data;
1302         (*stack) = frame;
1303
1304         return data;
1305 }
1306
1307 void *
1308 ep_stack_pop(ep_stack_t stack)
1309 {
1310
1311         if ((*stack)->below) {
1312                 (*stack) = (*stack)->below;
1313                 return (*stack)->above->payload;
1314         } else {
1315                 return NULL;
1316         }
1317 }
1318
1319 emem_tree_t *
1320 se_tree_create(int type, const char *name)
1321 {
1322         emem_tree_t *tree_list;
1323
1324         tree_list=(emem_tree_t *)g_malloc(sizeof(emem_tree_t));
1325         tree_list->next=se_packet_mem.trees;
1326         tree_list->type=type;
1327         tree_list->tree=NULL;
1328         tree_list->name=name;
1329         tree_list->malloc=se_alloc;
1330         se_packet_mem.trees=tree_list;
1331
1332         return tree_list;
1333 }
1334
1335 void *
1336 emem_tree_lookup32(emem_tree_t *se_tree, guint32 key)
1337 {
1338         emem_tree_node_t *node;
1339
1340         node=se_tree->tree;
1341
1342         while(node){
1343                 if(key==node->key32){
1344                         return node->data;
1345                 }
1346                 if(key<node->key32){
1347                         node=node->left;
1348                         continue;
1349                 }
1350                 if(key>node->key32){
1351                         node=node->right;
1352                         continue;
1353                 }
1354         }
1355         return NULL;
1356 }
1357
1358 static inline emem_tree_node_t *
1359 emem_tree_parent(emem_tree_node_t *node)
1360 {
1361         return node->parent;
1362 }
1363
1364 static inline emem_tree_node_t *
1365 emem_tree_grandparent(emem_tree_node_t *node)
1366 {
1367         emem_tree_node_t *parent;
1368
1369         parent=emem_tree_parent(node);
1370         if(parent){
1371                 return parent->parent;
1372         }
1373         return NULL;
1374 }
1375
1376 static inline emem_tree_node_t *
1377 emem_tree_uncle(emem_tree_node_t *node)
1378 {
1379         emem_tree_node_t *parent, *grandparent;
1380
1381         parent=emem_tree_parent(node);
1382         if(!parent){
1383                 return NULL;
1384         }
1385         grandparent=emem_tree_parent(parent);
1386         if(!grandparent){
1387                 return NULL;
1388         }
1389         if(parent==grandparent->left){
1390                 return grandparent->right;
1391         }
1392         return grandparent->left;
1393 }
1394
1395 static inline void rb_insert_case1(emem_tree_t *se_tree, emem_tree_node_t *node);
1396 static inline void rb_insert_case2(emem_tree_t *se_tree, emem_tree_node_t *node);
1397
1398 static inline void
1399 rotate_left(emem_tree_t *se_tree, emem_tree_node_t *node)
1400 {
1401         if(node->parent){
1402                 if(node->parent->left==node){
1403                         node->parent->left=node->right;
1404                 } else {
1405                         node->parent->right=node->right;
1406                 }
1407         } else {
1408                 se_tree->tree=node->right;
1409         }
1410         node->right->parent=node->parent;
1411         node->parent=node->right;
1412         node->right=node->right->left;
1413         if(node->right){
1414                 node->right->parent=node;
1415         }
1416         node->parent->left=node;
1417 }
1418
1419 static inline void
1420 rotate_right(emem_tree_t *se_tree, emem_tree_node_t *node)
1421 {
1422         if(node->parent){
1423                 if(node->parent->left==node){
1424                         node->parent->left=node->left;
1425                 } else {
1426                         node->parent->right=node->left;
1427                 }
1428         } else {
1429                 se_tree->tree=node->left;
1430         }
1431         node->left->parent=node->parent;
1432         node->parent=node->left;
1433         node->left=node->left->right;
1434         if(node->left){
1435                 node->left->parent=node;
1436         }
1437         node->parent->right=node;
1438 }
1439
1440 static inline void
1441 rb_insert_case5(emem_tree_t *se_tree, emem_tree_node_t *node)
1442 {
1443         emem_tree_node_t *grandparent;
1444         emem_tree_node_t *parent;
1445
1446         parent=emem_tree_parent(node);
1447         grandparent=emem_tree_parent(parent);
1448         parent->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1449         grandparent->u.rb_color=EMEM_TREE_RB_COLOR_RED;
1450         if( (node==parent->left) && (parent==grandparent->left) ){
1451                 rotate_right(se_tree, grandparent);
1452         } else {
1453                 rotate_left(se_tree, grandparent);
1454         }
1455 }
1456
1457 static inline void
1458 rb_insert_case4(emem_tree_t *se_tree, emem_tree_node_t *node)
1459 {
1460         emem_tree_node_t *grandparent;
1461         emem_tree_node_t *parent;
1462
1463         parent=emem_tree_parent(node);
1464         grandparent=emem_tree_parent(parent);
1465         if(!grandparent){
1466                 return;
1467         }
1468         if( (node==parent->right) && (parent==grandparent->left) ){
1469                 rotate_left(se_tree, parent);
1470                 node=node->left;
1471         } else if( (node==parent->left) && (parent==grandparent->right) ){
1472                 rotate_right(se_tree, parent);
1473                 node=node->right;
1474         }
1475         rb_insert_case5(se_tree, node);
1476 }
1477
1478 static inline void
1479 rb_insert_case3(emem_tree_t *se_tree, emem_tree_node_t *node)
1480 {
1481         emem_tree_node_t *grandparent;
1482         emem_tree_node_t *parent;
1483         emem_tree_node_t *uncle;
1484
1485         uncle=emem_tree_uncle(node);
1486         if(uncle && (uncle->u.rb_color==EMEM_TREE_RB_COLOR_RED)){
1487                 parent=emem_tree_parent(node);
1488                 parent->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1489                 uncle->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1490                 grandparent=emem_tree_grandparent(node);
1491                 grandparent->u.rb_color=EMEM_TREE_RB_COLOR_RED;
1492                 rb_insert_case1(se_tree, grandparent);
1493         } else {
1494                 rb_insert_case4(se_tree, node);
1495         }
1496 }
1497
1498 static inline void
1499 rb_insert_case2(emem_tree_t *se_tree, emem_tree_node_t *node)
1500 {
1501         emem_tree_node_t *parent;
1502
1503         parent=emem_tree_parent(node);
1504         /* parent is always non-NULL here */
1505         if(parent->u.rb_color==EMEM_TREE_RB_COLOR_BLACK){
1506                 return;
1507         }
1508         rb_insert_case3(se_tree, node);
1509 }
1510
1511 static inline void
1512 rb_insert_case1(emem_tree_t *se_tree, emem_tree_node_t *node)
1513 {
1514         emem_tree_node_t *parent;
1515
1516         parent=emem_tree_parent(node);
1517         if(!parent){
1518                 node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1519                 return;
1520         }
1521         rb_insert_case2(se_tree, node);
1522 }
1523
1524 /* insert a new node in the tree. if this node matches an already existing node
1525  * then just replace the data for that node */
1526 void
1527 emem_tree_insert32(emem_tree_t *se_tree, guint32 key, void *data)
1528 {
1529         emem_tree_node_t *node;
1530
1531         node=se_tree->tree;
1532
1533         /* is this the first node ?*/
1534         if(!node){
1535                 node=(emem_tree_node_t *)se_tree->malloc(sizeof(emem_tree_node_t));
1536                 switch(se_tree->type){
1537                 case EMEM_TREE_TYPE_RED_BLACK:
1538                         node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
1539                         break;
1540                 }
1541                 node->parent=NULL;
1542                 node->left=NULL;
1543                 node->right=NULL;
1544                 node->key32=key;
1545                 node->data=data;
1546                 node->u.is_subtree = EMEM_TREE_NODE_IS_DATA;
1547                 se_tree->tree=node;
1548                 return;
1549         }
1550
1551         /* it was not the new root so walk the tree until we find where to
1552          * insert this new leaf.
1553          */
1554         while(1){
1555                 /* this node already exists, so just replace the data pointer*/
1556                 if(key==node->key32){
1557                         node->data=data;
1558                         return;
1559                 }
1560                 if(key<node->key32) {
1561                         if(!node->left){
1562                                 /* new node to the left */
1563                                 emem_tree_node_t *new_node;
1564                                 new_node=(emem_tree_node_t *)se_tree->malloc(sizeof(emem_tree_node_t));
1565                                 node->left=new_node;
1566                                 new_node->parent=node;
1567                                 new_node->left=NULL;
1568                                 new_node->right=NULL;
1569                                 new_node->key32=key;
1570                                 new_node->data=data;
1571                                 new_node->u.is_subtree=EMEM_TREE_NODE_IS_DATA;
1572                                 node=new_node;
1573                                 break;
1574                         }
1575                         node=node->left;
1576                         continue;
1577                 }
1578                 if(key>node->key32) {
1579                         if(!node->right){
1580                                 /* new node to the right */
1581                                 emem_tree_node_t *new_node;
1582                                 new_node=(emem_tree_node_t *)se_tree->malloc(sizeof(emem_tree_node_t));
1583                                 node->right=new_node;
1584                                 new_node->parent=node;
1585                                 new_node->left=NULL;
1586                                 new_node->right=NULL;
1587                                 new_node->key32=key;
1588                                 new_node->data=data;
1589                                 new_node->u.is_subtree=EMEM_TREE_NODE_IS_DATA;
1590                                 node=new_node;
1591                                 break;
1592                         }
1593                         node=node->right;
1594                         continue;
1595                 }
1596         }
1597
1598         /* node will now point to the newly created node */
1599         switch(se_tree->type){
1600         case EMEM_TREE_TYPE_RED_BLACK:
1601                 node->u.rb_color=EMEM_TREE_RB_COLOR_RED;
1602                 rb_insert_case1(se_tree, node);
1603                 break;
1604         }
1605 }
1606
1607 static gboolean
1608 emem_tree_foreach_nodes(emem_tree_node_t* node, tree_foreach_func callback, void *user_data)
1609 {
1610         gboolean stop_traverse = FALSE;
1611
1612         if (!node)
1613                 return FALSE;
1614
1615         if(node->left) {
1616                 stop_traverse = emem_tree_foreach_nodes(node->left, callback, user_data);
1617                 if (stop_traverse) {
1618                         return TRUE;
1619                 }
1620         }
1621
1622         if (node->u.is_subtree == EMEM_TREE_NODE_IS_SUBTREE) {
1623                 stop_traverse = emem_tree_foreach((emem_tree_t *)node->data, callback, user_data);
1624         } else {
1625                 stop_traverse = callback(node->data, user_data);
1626         }
1627
1628         if (stop_traverse) {
1629                 return TRUE;
1630         }
1631
1632         if(node->right) {
1633                 stop_traverse = emem_tree_foreach_nodes(node->right, callback, user_data);
1634                 if (stop_traverse) {
1635                         return TRUE;
1636                 }
1637         }
1638
1639         return FALSE;
1640 }
1641
1642 gboolean
1643 emem_tree_foreach(emem_tree_t* emem_tree, tree_foreach_func callback, void *user_data)
1644 {
1645         if (!emem_tree)
1646                 return FALSE;
1647
1648         if(!emem_tree->tree)
1649                 return FALSE;
1650
1651         return emem_tree_foreach_nodes(emem_tree->tree, callback, user_data);
1652 }
1653
1654 static void emem_print_subtree(emem_tree_t* emem_tree, guint32 level);
1655
1656 static void
1657 emem_tree_print_nodes(const char *prefix, emem_tree_node_t* node, guint32 level)
1658 {
1659         guint32 i;
1660
1661         if (!node)
1662                 return;
1663
1664         for(i=0;i<level;i++){
1665                 printf("    ");
1666         }
1667
1668         printf("%sNODE:%p parent:%p left:%p right:%p colour:%s key:%u %s:%p\n", prefix,
1669                 (void *)node,(void *)(node->parent),(void *)(node->left),(void *)(node->right),
1670                 (node->u.rb_color)?"Black":"Red",(node->key32),(node->u.is_subtree)?"tree":"data",node->data);
1671         if(node->left)
1672                 emem_tree_print_nodes("L-", node->left, level+1);
1673         if(node->right)
1674                 emem_tree_print_nodes("R-", node->right, level+1);
1675
1676         if (node->u.is_subtree)
1677                 emem_print_subtree((emem_tree_t *)node->data, level+1);
1678 }
1679
1680 static void
1681 emem_print_subtree(emem_tree_t* emem_tree, guint32 level)
1682 {
1683         guint32 i;
1684
1685         if (!emem_tree)
1686                 return;
1687
1688         for(i=0;i<level;i++){
1689                 printf("    ");
1690         }
1691
1692         printf("EMEM tree:%p type:%s name:%s root:%p\n",(void *)emem_tree,(emem_tree->type==1)?"RedBlack":"unknown",emem_tree->name,(void *)(emem_tree->tree));
1693         if(emem_tree->tree)
1694                 emem_tree_print_nodes("Root-", emem_tree->tree, level);
1695 }
1696
1697 void
1698 emem_print_tree(emem_tree_t* emem_tree)
1699 {
1700         emem_print_subtree(emem_tree, 0);
1701 }
1702
1703 /*
1704  * String buffers
1705  */
1706
1707 /*
1708  * Presumably we're using these routines for building strings for the tree.
1709  * Use ITEM_LABEL_LENGTH as the basis for our default lengths.
1710  */
1711
1712 #define DEFAULT_STRBUF_LEN (ITEM_LABEL_LENGTH / 10)
1713 #define MAX_STRBUF_LEN 65536
1714
1715 static gsize
1716 next_size(gsize cur_alloc_len, gsize wanted_alloc_len, gsize max_alloc_len)
1717 {
1718         if (max_alloc_len < 1 || max_alloc_len > MAX_STRBUF_LEN) {
1719                 max_alloc_len = MAX_STRBUF_LEN;
1720         }
1721
1722         if (cur_alloc_len < 1) {
1723                 cur_alloc_len = DEFAULT_STRBUF_LEN;
1724         }
1725
1726         while (cur_alloc_len < wanted_alloc_len) {
1727                 cur_alloc_len *= 2;
1728         }
1729
1730         return cur_alloc_len < max_alloc_len ? cur_alloc_len : max_alloc_len;
1731 }
1732
1733 static void
1734 ep_strbuf_grow(emem_strbuf_t *strbuf, gsize wanted_alloc_len)
1735 {
1736         gsize new_alloc_len;
1737         gchar *new_str;
1738
1739         if (!strbuf || (wanted_alloc_len <= strbuf->alloc_len) || (strbuf->alloc_len >= strbuf->max_alloc_len)) {
1740                 return;
1741         }
1742
1743         new_alloc_len = next_size(strbuf->alloc_len, wanted_alloc_len, strbuf->max_alloc_len);
1744         new_str = (gchar *)ep_alloc(new_alloc_len);
1745         g_strlcpy(new_str, strbuf->str, new_alloc_len);
1746
1747         strbuf->alloc_len = new_alloc_len;
1748         strbuf->str = new_str;
1749 }
1750
1751 emem_strbuf_t *
1752 ep_strbuf_sized_new(gsize alloc_len, gsize max_alloc_len)
1753 {
1754         emem_strbuf_t *strbuf;
1755
1756         strbuf = ep_new(emem_strbuf_t);
1757
1758         if ((max_alloc_len == 0) || (max_alloc_len > MAX_STRBUF_LEN))
1759                 max_alloc_len = MAX_STRBUF_LEN;
1760         if (alloc_len == 0)
1761                 alloc_len = 1;
1762         else if (alloc_len > max_alloc_len)
1763                 alloc_len = max_alloc_len;
1764
1765         strbuf->str = (char *)ep_alloc(alloc_len);
1766         strbuf->str[0] = '\0';
1767
1768         strbuf->len = 0;
1769         strbuf->alloc_len = alloc_len;
1770         strbuf->max_alloc_len = max_alloc_len;
1771
1772         return strbuf;
1773 }
1774
1775 emem_strbuf_t *
1776 ep_strbuf_new(const gchar *init)
1777 {
1778         emem_strbuf_t *strbuf;
1779
1780         strbuf = ep_strbuf_sized_new(next_size(0, init?strlen(init)+1:0, 0), 0);  /* +1 for NULL terminator */
1781         if (init) {
1782                 gsize full_len;
1783                 full_len = g_strlcpy(strbuf->str, init, strbuf->alloc_len);
1784                 strbuf->len = MIN(full_len, strbuf->alloc_len-1);
1785         }
1786
1787         return strbuf;
1788 }
1789
1790 emem_strbuf_t *
1791 ep_strbuf_new_label(const gchar *init)
1792 {
1793         emem_strbuf_t *strbuf;
1794         gsize full_len;
1795
1796         /* Be optimistic: Allocate default size strbuf string and only      */
1797         /*  request an increase if needed.                                  */
1798         /* XXX: Is it reasonable to assume that much of the usage of        */
1799         /*  ep_strbuf_new_label will have  init==NULL or                    */
1800         /*   strlen(init) < DEFAULT_STRBUF_LEN) ???                         */
1801         strbuf = ep_strbuf_sized_new(DEFAULT_STRBUF_LEN, ITEM_LABEL_LENGTH);
1802
1803         if (!init)
1804                 return strbuf;
1805
1806         /* full_len does not count the trailing '\0'.                       */
1807         full_len = g_strlcpy(strbuf->str, init, strbuf->alloc_len);
1808         if (full_len < strbuf->alloc_len) {
1809                 strbuf->len += full_len;
1810         } else {
1811                 strbuf = ep_strbuf_sized_new(full_len+1, ITEM_LABEL_LENGTH);
1812                 full_len = g_strlcpy(strbuf->str, init, strbuf->alloc_len);
1813                 strbuf->len = MIN(full_len, strbuf->alloc_len-1);
1814         }
1815
1816         return strbuf;
1817 }
1818
1819 emem_strbuf_t *
1820 ep_strbuf_append(emem_strbuf_t *strbuf, const gchar *str)
1821 {
1822         gsize add_len, full_len;
1823
1824         if (!strbuf || !str || str[0] == '\0') {
1825                 return strbuf;
1826         }
1827
1828         /* Be optimistic; try the g_strlcpy first & see if enough room.                 */
1829         /* Note: full_len doesn't count the trailing '\0'; add_len does allow for same  */
1830         add_len = strbuf->alloc_len - strbuf->len;
1831         full_len = g_strlcpy(&strbuf->str[strbuf->len], str, add_len);
1832         if (full_len < add_len) {
1833                 strbuf->len += full_len;
1834         } else {
1835                 strbuf->str[strbuf->len] = '\0'; /* end string at original length again */
1836                 ep_strbuf_grow(strbuf, strbuf->len + full_len + 1);
1837                 add_len = strbuf->alloc_len - strbuf->len;
1838                 full_len = g_strlcpy(&strbuf->str[strbuf->len], str, add_len);
1839                 strbuf->len += MIN(add_len-1, full_len);
1840         }
1841
1842         return strbuf;
1843 }
1844
1845 void
1846 ep_strbuf_append_vprintf(emem_strbuf_t *strbuf, const gchar *format, va_list ap)
1847 {
1848         va_list ap2;
1849         gsize add_len, full_len;
1850
1851         G_VA_COPY(ap2, ap);
1852
1853         /* Be optimistic; try the g_vsnprintf first & see if enough room.               */
1854         /* Note: full_len doesn't count the trailing '\0'; add_len does allow for same. */
1855         add_len = strbuf->alloc_len - strbuf->len;
1856         full_len = g_vsnprintf(&strbuf->str[strbuf->len], (gulong) add_len, format, ap);
1857         if (full_len < add_len) {
1858                 strbuf->len += full_len;
1859         } else {
1860                 strbuf->str[strbuf->len] = '\0'; /* end string at original length again */
1861                 ep_strbuf_grow(strbuf, strbuf->len + full_len + 1);
1862                 add_len = strbuf->alloc_len - strbuf->len;
1863                 full_len = g_vsnprintf(&strbuf->str[strbuf->len], (gulong) add_len, format, ap2);
1864                 strbuf->len += MIN(add_len-1, full_len);
1865         }
1866
1867         va_end(ap2);
1868 }
1869
1870 void
1871 ep_strbuf_append_printf(emem_strbuf_t *strbuf, const gchar *format, ...)
1872 {
1873         va_list ap;
1874
1875         va_start(ap, format);
1876         ep_strbuf_append_vprintf(strbuf, format, ap);
1877         va_end(ap);
1878 }
1879
1880 void
1881 ep_strbuf_printf(emem_strbuf_t *strbuf, const gchar *format, ...)
1882 {
1883         va_list ap;
1884         if (!strbuf) {
1885                 return;
1886         }
1887
1888         strbuf->len = 0;
1889
1890         va_start(ap, format);
1891         ep_strbuf_append_vprintf(strbuf, format, ap);
1892         va_end(ap);
1893 }
1894
1895 emem_strbuf_t *
1896 ep_strbuf_append_c(emem_strbuf_t *strbuf, const gchar c)
1897 {
1898         if (!strbuf) {
1899                 return strbuf;
1900         }
1901
1902         /* +1 for the new character & +1 for the trailing '\0'. */
1903         if (strbuf->alloc_len < strbuf->len + 1 + 1) {
1904                 ep_strbuf_grow(strbuf, strbuf->len + 1 + 1);
1905         }
1906         if (strbuf->alloc_len >= strbuf->len + 1 + 1) {
1907                 strbuf->str[strbuf->len] = c;
1908                 strbuf->len++;
1909                 strbuf->str[strbuf->len] = '\0';
1910         }
1911
1912         return strbuf;
1913 }
1914
1915 emem_strbuf_t *
1916 ep_strbuf_append_unichar(emem_strbuf_t *strbuf, const gunichar c)
1917 {
1918         gchar buf[6];
1919         gint charlen;
1920
1921         if (!strbuf) {
1922                 return strbuf;
1923         }
1924
1925         charlen = g_unichar_to_utf8(c, buf);
1926
1927         /* +charlen for the new character & +1 for the trailing '\0'. */
1928         if (strbuf->alloc_len < strbuf->len + charlen + 1) {
1929                 ep_strbuf_grow(strbuf, strbuf->len + charlen + 1);
1930         }
1931         if (strbuf->alloc_len >= strbuf->len + charlen + 1) {
1932                 memcpy(&strbuf->str[strbuf->len], buf, charlen);
1933                 strbuf->len += charlen;
1934                 strbuf->str[strbuf->len] = '\0';
1935         }
1936
1937         return strbuf;
1938 }
1939
1940 emem_strbuf_t *
1941 ep_strbuf_truncate(emem_strbuf_t *strbuf, gsize len)
1942 {
1943         if (!strbuf || len >= strbuf->len) {
1944                 return strbuf;
1945         }
1946
1947         strbuf->str[len] = '\0';
1948         strbuf->len = len;
1949
1950         return strbuf;
1951 }
1952
1953 /*
1954  * Editor modelines
1955  *
1956  * Local Variables:
1957  * c-basic-offset: 8
1958  * tab-width: 8
1959  * indent-tabs-mode: t
1960  * End:
1961  *
1962  * ex: set shiftwidth=8 tabstop=8 noexpandtab:
1963  * :indentSize=8:tabSize=8:noTabs=false:
1964  */