2005-07-22 07:46:58 +00:00
|
|
|
/* emem.c
|
2006-05-28 19:49:07 +00:00
|
|
|
* Wireshark memory management and garbage collection functions
|
2005-07-22 07:46:58 +00:00
|
|
|
* Ronnie Sahlberg 2005
|
|
|
|
*
|
2005-07-22 17:55:47 +00:00
|
|
|
* $Id$
|
2005-07-22 07:46:58 +00:00
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2005-07-22 07:46:58 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-06-28 22:56:06 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2005-07-22 07:46:58 +00:00
|
|
|
*/
|
2005-07-24 01:21:38 +00:00
|
|
|
#include "config.h"
|
2005-07-22 07:46:58 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2005-07-22 23:38:51 +00:00
|
|
|
#include <stdlib.h>
|
2005-07-24 00:29:57 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
2007-12-04 03:26:50 +00:00
|
|
|
#include <ctype.h>
|
2006-01-09 23:11:40 +00:00
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2005-07-22 07:46:58 +00:00
|
|
|
#include <glib.h>
|
2008-08-05 02:23:35 +00:00
|
|
|
|
2009-09-04 11:48:04 +00:00
|
|
|
#include "proto.h"
|
2005-07-22 07:46:58 +00:00
|
|
|
#include "emem.h"
|
|
|
|
|
2007-04-04 12:18:10 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h> /* VirtualAlloc, VirtualProtect */
|
|
|
|
#include <process.h> /* getpid */
|
|
|
|
#endif
|
|
|
|
|
2009-10-06 16:20:26 +00:00
|
|
|
/* Print out statistics about our memory allocations? */
|
|
|
|
/*#define SHOW_EMEM_STATS*/
|
|
|
|
|
2009-08-10 16:36:44 +00:00
|
|
|
/* Do we want to use guardpages? if available */
|
|
|
|
#define WANT_GUARD_PAGES 1
|
2006-03-10 05:39:57 +00:00
|
|
|
|
2006-03-10 05:15:52 +00:00
|
|
|
#ifdef WANT_GUARD_PAGES
|
2006-02-27 20:51:53 +00:00
|
|
|
/* Add guard pages at each end of our allocated memory */
|
2012-10-08 19:43:03 +00:00
|
|
|
|
2006-03-03 20:44:53 +00:00
|
|
|
#if defined(HAVE_SYSCONF) && defined(HAVE_MMAP) && defined(HAVE_MPROTECT) && defined(HAVE_STDINT_H)
|
|
|
|
#include <stdint.h>
|
2012-10-08 19:43:03 +00:00
|
|
|
|
2009-11-23 18:47:52 +00:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
2006-02-27 20:51:53 +00:00
|
|
|
#include <sys/types.h>
|
2012-10-08 19:43:03 +00:00
|
|
|
#endif /* HAVE_SYS_TYPES_H */
|
|
|
|
|
2006-02-27 20:51:53 +00:00
|
|
|
#include <sys/mman.h>
|
2012-10-08 19:43:03 +00:00
|
|
|
|
2006-06-17 02:31:56 +00:00
|
|
|
#if defined(MAP_ANONYMOUS)
|
|
|
|
#define ANON_PAGE_MODE (MAP_ANONYMOUS|MAP_PRIVATE)
|
|
|
|
#elif defined(MAP_ANON)
|
|
|
|
#define ANON_PAGE_MODE (MAP_ANON|MAP_PRIVATE)
|
|
|
|
#else
|
|
|
|
#define ANON_PAGE_MODE (MAP_PRIVATE) /* have to map /dev/zero */
|
|
|
|
#define NEED_DEV_ZERO
|
2012-10-08 19:43:03 +00:00
|
|
|
#endif /* defined(MAP_ANONYMOUS) */
|
|
|
|
|
2006-06-17 02:31:56 +00:00
|
|
|
#ifdef NEED_DEV_ZERO
|
|
|
|
#include <fcntl.h>
|
|
|
|
static int dev_zero_fd;
|
|
|
|
#define ANON_FD dev_zero_fd
|
|
|
|
#else
|
|
|
|
#define ANON_FD -1
|
2012-10-08 19:43:03 +00:00
|
|
|
#endif /* NEED_DEV_ZERO */
|
|
|
|
|
2006-02-27 20:51:53 +00:00
|
|
|
#define USE_GUARD_PAGES 1
|
2012-10-08 19:43:03 +00:00
|
|
|
#endif /* defined(HAVE_SYSCONF) && defined(HAVE_MMAP) && defined(HAVE_MPROTECT) && defined(HAVE_STDINT_H) */
|
|
|
|
#endif /* WANT_GUARD_PAGES */
|
2006-02-27 20:51:53 +00:00
|
|
|
|
2005-07-22 07:46:58 +00:00
|
|
|
/* When required, allocate more memory from the OS in this size chunks */
|
2009-10-11 06:26:24 +00:00
|
|
|
#define EMEM_PACKET_CHUNK_SIZE (10 * 1024 * 1024)
|
2005-07-22 07:46:58 +00:00
|
|
|
|
2011-02-25 15:49:28 +00:00
|
|
|
/* The canary between allocations is at least 8 bytes and up to 16 bytes to
|
|
|
|
* allow future allocations to be 4- or 8-byte aligned.
|
|
|
|
* All but the last byte of the canary are randomly generated; the last byte is
|
|
|
|
* NULL to separate the canary and the pointer to the next canary.
|
|
|
|
*
|
|
|
|
* For example, if the allocation is a multiple of 8 bytes, the canary and
|
|
|
|
* pointer would look like:
|
2011-02-25 03:18:02 +00:00
|
|
|
* |0|1|2|3|4|5|6|7||0|1|2|3|4|5|6|7|
|
|
|
|
* |c|c|c|c|c|c|c|0||p|p|p|p|p|p|p|p| (64-bit), or:
|
|
|
|
* |c|c|c|c|c|c|c|0||p|p|p|p| (32-bit)
|
|
|
|
*
|
2011-02-25 15:49:28 +00:00
|
|
|
* If the allocation was, for example, 12 bytes, the canary would look like:
|
|
|
|
* |0|1|2|3|4|5|6|7||0|1|2|3|4|5|6|7|
|
|
|
|
* [...]|a|a|a|a|c|c|c|c||c|c|c|c|c|c|c|0| (followed by the pointer)
|
2011-02-25 03:18:02 +00:00
|
|
|
*/
|
2006-01-10 21:12:48 +00:00
|
|
|
#define EMEM_CANARY_SIZE 8
|
|
|
|
#define EMEM_CANARY_DATA_SIZE (EMEM_CANARY_SIZE * 2 - 1)
|
2008-08-05 02:23:35 +00:00
|
|
|
|
2005-07-22 07:46:58 +00:00
|
|
|
typedef struct _emem_chunk_t {
|
|
|
|
struct _emem_chunk_t *next;
|
2009-10-19 22:31:08 +00:00
|
|
|
char *buf;
|
2012-10-10 12:24:56 +00:00
|
|
|
char *org;
|
|
|
|
size_t size;
|
2006-02-27 20:51:53 +00:00
|
|
|
unsigned int amount_free_init;
|
2005-07-22 07:46:58 +00:00
|
|
|
unsigned int amount_free;
|
2006-02-27 20:51:53 +00:00
|
|
|
unsigned int free_offset_init;
|
2005-07-22 07:46:58 +00:00
|
|
|
unsigned int free_offset;
|
2010-01-19 21:28:48 +00:00
|
|
|
void *canary_last;
|
2005-07-22 07:46:58 +00:00
|
|
|
} emem_chunk_t;
|
|
|
|
|
2012-10-10 19:05:30 +00:00
|
|
|
typedef struct _emem_pool_t {
|
2009-04-14 14:08:19 +00:00
|
|
|
emem_chunk_t *free_list;
|
|
|
|
emem_chunk_t *used_list;
|
2009-10-19 22:31:08 +00:00
|
|
|
|
|
|
|
emem_tree_t *trees; /* only used by se_mem allocator */
|
|
|
|
|
|
|
|
guint8 canary[EMEM_CANARY_DATA_SIZE];
|
2012-10-08 18:33:00 +00:00
|
|
|
void *(*memory_alloc)(size_t size, struct _emem_pool_t *);
|
2009-10-19 22:31:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Tools like Valgrind and ElectricFence don't work well with memchunks.
|
|
|
|
* Export the following environment variables to make {ep|se}_alloc() allocate each
|
|
|
|
* object individually.
|
|
|
|
*
|
|
|
|
* WIRESHARK_DEBUG_EP_NO_CHUNKS
|
|
|
|
* WIRESHARK_DEBUG_SE_NO_CHUNKS
|
|
|
|
*/
|
|
|
|
gboolean debug_use_chunks;
|
|
|
|
|
|
|
|
/* Do we want to use canaries?
|
|
|
|
* Export the following environment variables to disable/enable canaries
|
|
|
|
*
|
|
|
|
* WIRESHARK_DEBUG_EP_NO_CANARY
|
|
|
|
* For SE memory use of canary is default off as the memory overhead
|
|
|
|
* is considerable.
|
|
|
|
* WIRESHARK_DEBUG_SE_USE_CANARY
|
|
|
|
*/
|
|
|
|
gboolean debug_use_canary;
|
|
|
|
|
2010-09-02 18:02:06 +00:00
|
|
|
/* Do we want to verify no one is using a pointer to an ep_ or se_
|
|
|
|
* allocated thing where they shouldn't be?
|
|
|
|
*
|
|
|
|
* Export WIRESHARK_EP_VERIFY_POINTERS or WIRESHARK_SE_VERIFY_POINTERS
|
|
|
|
* to turn this on.
|
|
|
|
*/
|
|
|
|
gboolean debug_verify_pointers;
|
|
|
|
|
2012-10-10 19:05:30 +00:00
|
|
|
} emem_pool_t;
|
2005-07-22 07:46:58 +00:00
|
|
|
|
2012-10-10 19:05:30 +00:00
|
|
|
static emem_pool_t ep_packet_mem;
|
2012-10-08 18:33:00 +00:00
|
|
|
static emem_pool_t se_packet_mem;
|
2005-07-22 07:46:58 +00:00
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
/*
|
|
|
|
* Memory scrubbing is expensive but can be useful to ensure we don't:
|
|
|
|
* - use memory before initializing it
|
|
|
|
* - use memory after freeing it
|
|
|
|
* Export WIRESHARK_DEBUG_SCRUB_MEMORY to turn it on.
|
|
|
|
*/
|
|
|
|
static gboolean debug_use_memory_scrubber = FALSE;
|
|
|
|
|
2006-04-28 16:40:39 +00:00
|
|
|
#if defined (_WIN32)
|
|
|
|
static SYSTEM_INFO sysinfo;
|
|
|
|
static OSVERSIONINFO versinfo;
|
|
|
|
static int pagesize;
|
|
|
|
#elif defined(USE_GUARD_PAGES)
|
|
|
|
static intptr_t pagesize;
|
|
|
|
#endif /* _WIN32 / USE_GUARD_PAGES */
|
2006-03-10 05:39:57 +00:00
|
|
|
|
2012-10-08 18:33:00 +00:00
|
|
|
static void *emem_alloc_chunk(size_t size, emem_pool_t *mem);
|
|
|
|
static void *emem_alloc_glib(size_t size, emem_pool_t *mem);
|
2009-10-19 22:31:08 +00:00
|
|
|
|
2006-01-09 23:11:40 +00:00
|
|
|
/*
|
|
|
|
* Set a canary value to be placed between memchunks.
|
|
|
|
*/
|
2009-09-29 06:50:35 +00:00
|
|
|
static void
|
2009-10-19 22:31:08 +00:00
|
|
|
emem_canary_init(guint8 *canary)
|
|
|
|
{
|
2006-01-10 21:12:48 +00:00
|
|
|
int i;
|
2009-10-11 07:15:15 +00:00
|
|
|
static GRand *rand_state = NULL;
|
2006-01-09 23:11:40 +00:00
|
|
|
|
|
|
|
if (rand_state == NULL) {
|
|
|
|
rand_state = g_rand_new();
|
|
|
|
}
|
2006-01-10 21:12:48 +00:00
|
|
|
for (i = 0; i < EMEM_CANARY_DATA_SIZE; i ++) {
|
2010-01-19 21:28:48 +00:00
|
|
|
canary[i] = (guint8) g_rand_int_range(rand_state, 1, 0x100);
|
2006-01-10 21:12:48 +00:00
|
|
|
}
|
|
|
|
return;
|
2006-01-09 23:11:40 +00:00
|
|
|
}
|
|
|
|
|
2010-01-19 21:28:48 +00:00
|
|
|
static void *
|
|
|
|
emem_canary_next(guint8 *mem_canary, guint8 *canary, int *len)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < EMEM_CANARY_SIZE-1; i++)
|
|
|
|
if (mem_canary[i] != canary[i])
|
|
|
|
return (void *) -1;
|
|
|
|
|
|
|
|
for (; i < EMEM_CANARY_DATA_SIZE; i++) {
|
|
|
|
if (canary[i] == '\0') {
|
|
|
|
memcpy(&ptr, &canary[i+1], sizeof(void *));
|
|
|
|
|
|
|
|
if (len)
|
|
|
|
*len = i + 1 + sizeof(void *);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mem_canary[i] != canary[i])
|
|
|
|
return (void *) -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (void *) -1;
|
|
|
|
}
|
|
|
|
|
2006-01-10 21:12:48 +00:00
|
|
|
/*
|
2011-02-25 15:49:28 +00:00
|
|
|
* Given an allocation size, return the amount of room needed for the canary
|
|
|
|
* (with a minimum of 8 bytes) while using the canary to pad to an 8-byte
|
|
|
|
* boundary.
|
2006-01-10 21:12:48 +00:00
|
|
|
*/
|
2006-01-11 02:05:05 +00:00
|
|
|
static guint8
|
2009-10-19 22:31:08 +00:00
|
|
|
emem_canary_pad (size_t allocation)
|
|
|
|
{
|
2006-01-10 21:12:48 +00:00
|
|
|
guint8 pad;
|
|
|
|
|
|
|
|
pad = EMEM_CANARY_SIZE - (allocation % EMEM_CANARY_SIZE);
|
|
|
|
if (pad < EMEM_CANARY_SIZE)
|
|
|
|
pad += EMEM_CANARY_SIZE;
|
|
|
|
|
|
|
|
return pad;
|
|
|
|
}
|
2006-03-10 05:39:57 +00:00
|
|
|
|
2008-08-05 02:23:35 +00:00
|
|
|
/* used for debugging canaries, will block */
|
|
|
|
#ifdef DEBUG_INTENSE_CANARY_CHECKS
|
|
|
|
gboolean intense_canary_checking = FALSE;
|
|
|
|
|
|
|
|
/* used to intensivelly check ep canaries
|
|
|
|
*/
|
2009-10-19 22:31:08 +00:00
|
|
|
void
|
|
|
|
ep_check_canary_integrity(const char* fmt, ...)
|
|
|
|
{
|
2009-04-14 14:08:19 +00:00
|
|
|
va_list ap;
|
|
|
|
static gchar there[128] = {
|
|
|
|
'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,
|
|
|
|
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,
|
|
|
|
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,
|
|
|
|
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 };
|
|
|
|
gchar here[128];
|
2008-08-05 02:23:35 +00:00
|
|
|
emem_chunk_t* npc = NULL;
|
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
if (! intense_canary_checking ) return;
|
2008-08-05 02:23:35 +00:00
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
va_start(ap,fmt);
|
2010-01-20 06:37:56 +00:00
|
|
|
g_vsnprintf(here, sizeof(here), fmt, ap);
|
2009-04-14 14:08:19 +00:00
|
|
|
va_end(ap);
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2012-10-10 19:05:30 +00:00
|
|
|
for (npc = ep_packet_mem.free_list; npc != NULL; npc = npc->next) {
|
2010-01-19 21:28:48 +00:00
|
|
|
void *canary_next = npc->canary_last;
|
2008-08-05 02:23:35 +00:00
|
|
|
|
2010-01-19 21:28:48 +00:00
|
|
|
while (canary_next != NULL) {
|
2012-10-10 19:05:30 +00:00
|
|
|
canary_next = emem_canary_next(ep_packet_mem.canary, canary_next, NULL);
|
2011-04-21 05:31:11 +00:00
|
|
|
/* XXX, check if canary_next is inside allocated memory? */
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2011-04-21 05:31:11 +00:00
|
|
|
if (canary_next == (void *) -1)
|
2010-01-20 06:37:56 +00:00
|
|
|
g_error("Per-packet memory corrupted\nbetween: %s\nand: %s", there, here);
|
2008-08-05 02:23:35 +00:00
|
|
|
}
|
2009-04-14 14:08:19 +00:00
|
|
|
}
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2010-01-20 06:37:56 +00:00
|
|
|
g_strlcpy(there, here, sizeof(there));
|
2008-08-05 02:23:35 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
static void
|
2012-10-08 18:33:00 +00:00
|
|
|
emem_init_chunk(emem_pool_t *mem)
|
2009-10-19 22:31:08 +00:00
|
|
|
{
|
|
|
|
if (mem->debug_use_canary)
|
|
|
|
emem_canary_init(mem->canary);
|
|
|
|
|
|
|
|
if (mem->debug_use_chunks)
|
|
|
|
mem->memory_alloc = emem_alloc_chunk;
|
|
|
|
else
|
|
|
|
mem->memory_alloc = emem_alloc_glib;
|
|
|
|
}
|
|
|
|
|
2006-01-10 21:12:48 +00:00
|
|
|
|
2005-07-22 07:46:58 +00:00
|
|
|
/* Initialize the packet-lifetime memory allocation pool.
|
2006-05-31 17:38:42 +00:00
|
|
|
* This function should be called only once when Wireshark or TShark starts
|
2005-07-22 07:46:58 +00:00
|
|
|
* up.
|
|
|
|
*/
|
2009-10-20 17:43:05 +00:00
|
|
|
static void
|
2012-10-10 19:05:30 +00:00
|
|
|
ep_init_chunk(void)
|
2005-07-22 07:46:58 +00:00
|
|
|
{
|
2012-10-10 19:05:30 +00:00
|
|
|
ep_packet_mem.free_list=NULL;
|
|
|
|
ep_packet_mem.used_list=NULL;
|
|
|
|
ep_packet_mem.trees=NULL; /* not used by this allocator */
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2012-10-10 19:05:30 +00:00
|
|
|
ep_packet_mem.debug_use_chunks = (getenv("WIRESHARK_DEBUG_EP_NO_CHUNKS") == NULL);
|
|
|
|
ep_packet_mem.debug_use_canary = ep_packet_mem.debug_use_chunks && (getenv("WIRESHARK_DEBUG_EP_NO_CANARY") == NULL);
|
|
|
|
ep_packet_mem.debug_verify_pointers = (getenv("WIRESHARK_EP_VERIFY_POINTERS") != NULL);
|
2009-08-11 09:24:56 +00:00
|
|
|
|
2008-08-05 02:23:35 +00:00
|
|
|
#ifdef DEBUG_INTENSE_CANARY_CHECKS
|
2010-01-19 21:28:48 +00:00
|
|
|
intense_canary_checking = (getenv("WIRESHARK_DEBUG_EP_INTENSE_CANARY") != NULL);
|
2008-08-05 02:23:35 +00:00
|
|
|
#endif
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2012-10-10 19:05:30 +00:00
|
|
|
emem_init_chunk(&ep_packet_mem);
|
2009-10-20 17:43:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the capture-lifetime memory allocation pool.
|
|
|
|
* This function should be called only once when Wireshark or TShark starts
|
|
|
|
* up.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
se_init_chunk(void)
|
|
|
|
{
|
|
|
|
se_packet_mem.free_list = NULL;
|
|
|
|
se_packet_mem.used_list = NULL;
|
2010-01-20 06:37:56 +00:00
|
|
|
se_packet_mem.trees = NULL;
|
2009-10-20 17:43:05 +00:00
|
|
|
|
|
|
|
se_packet_mem.debug_use_chunks = (getenv("WIRESHARK_DEBUG_SE_NO_CHUNKS") == NULL);
|
|
|
|
se_packet_mem.debug_use_canary = se_packet_mem.debug_use_chunks && (getenv("WIRESHARK_DEBUG_SE_USE_CANARY") != NULL);
|
2010-09-02 18:02:06 +00:00
|
|
|
se_packet_mem.debug_verify_pointers = (getenv("WIRESHARK_SE_VERIFY_POINTERS") != NULL);
|
2009-10-20 17:43:05 +00:00
|
|
|
|
|
|
|
emem_init_chunk(&se_packet_mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize all the allocators here.
|
|
|
|
* This function should be called only once when Wireshark or TShark starts
|
|
|
|
* up.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
emem_init(void)
|
|
|
|
{
|
2012-10-10 19:05:30 +00:00
|
|
|
ep_init_chunk();
|
2009-10-20 17:43:05 +00:00
|
|
|
se_init_chunk();
|
|
|
|
|
|
|
|
if (getenv("WIRESHARK_DEBUG_SCRUB_MEMORY"))
|
|
|
|
debug_use_memory_scrubber = TRUE;
|
2006-04-28 16:40:39 +00:00
|
|
|
|
|
|
|
#if defined (_WIN32)
|
|
|
|
/* Set up our guard page info for Win32 */
|
|
|
|
GetSystemInfo(&sysinfo);
|
|
|
|
pagesize = sysinfo.dwPageSize;
|
|
|
|
|
2006-05-18 00:25:14 +00:00
|
|
|
/* calling GetVersionEx using the OSVERSIONINFO structure.
|
|
|
|
* OSVERSIONINFOEX requires Win NT4 with SP6 or newer NT Versions.
|
|
|
|
* OSVERSIONINFOEX will fail on Win9x and older NT Versions.
|
|
|
|
* See also:
|
|
|
|
* http://msdn.microsoft.com/library/en-us/sysinfo/base/getversionex.asp
|
|
|
|
* http://msdn.microsoft.com/library/en-us/sysinfo/base/osversioninfo_str.asp
|
|
|
|
* http://msdn.microsoft.com/library/en-us/sysinfo/base/osversioninfoex_str.asp
|
|
|
|
*/
|
|
|
|
versinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
|
|
|
GetVersionEx(&versinfo);
|
|
|
|
|
2006-04-28 16:40:39 +00:00
|
|
|
#elif defined(USE_GUARD_PAGES)
|
|
|
|
pagesize = sysconf(_SC_PAGESIZE);
|
2012-11-05 09:04:53 +00:00
|
|
|
if (pagesize == -1)
|
|
|
|
fprintf(stderr, "Warning: call to sysconf() for _SC_PAGESIZE has failed...\n");
|
2006-06-17 02:31:56 +00:00
|
|
|
#ifdef NEED_DEV_ZERO
|
2008-05-23 05:55:34 +00:00
|
|
|
dev_zero_fd = ws_open("/dev/zero", O_RDWR);
|
2006-06-17 02:31:56 +00:00
|
|
|
g_assert(dev_zero_fd != -1);
|
|
|
|
#endif
|
2006-04-28 16:40:39 +00:00
|
|
|
#endif /* _WIN32 / USE_GUARD_PAGES */
|
2006-05-18 00:25:14 +00:00
|
|
|
}
|
2009-08-11 09:24:56 +00:00
|
|
|
|
2009-10-06 16:20:26 +00:00
|
|
|
#ifdef SHOW_EMEM_STATS
|
2009-10-09 21:00:00 +00:00
|
|
|
#define NUM_ALLOC_DIST 10
|
|
|
|
static guint allocations[NUM_ALLOC_DIST] = { 0 };
|
2009-10-08 11:46:15 +00:00
|
|
|
static guint total_no_chunks = 0;
|
2009-10-06 16:20:26 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
print_alloc_stats()
|
|
|
|
{
|
|
|
|
guint num_chunks = 0;
|
|
|
|
guint num_allocs = 0;
|
|
|
|
guint total_used = 0;
|
|
|
|
guint total_allocation = 0;
|
|
|
|
guint used_for_canaries = 0;
|
2009-10-09 21:00:00 +00:00
|
|
|
guint total_headers;
|
2010-01-19 21:28:48 +00:00
|
|
|
guint i;
|
2009-10-06 16:20:26 +00:00
|
|
|
emem_chunk_t *chunk;
|
|
|
|
guint total_space_allocated_from_os, total_space_wasted;
|
2009-10-08 11:46:15 +00:00
|
|
|
gboolean ep_stat=TRUE;
|
|
|
|
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf(stderr, "\n-------- EP allocator statistics --------\n");
|
2009-10-16 21:36:42 +00:00
|
|
|
fprintf(stderr, "%s chunks, %s canaries, %s memory scrubber\n",
|
2012-10-10 19:05:30 +00:00
|
|
|
ep_packet_mem.debug_use_chunks ? "Using" : "Not using",
|
|
|
|
ep_packet_mem.debug_use_canary ? "using" : "not using",
|
2009-10-16 21:36:42 +00:00
|
|
|
debug_use_memory_scrubber ? "using" : "not using");
|
2009-10-08 11:46:15 +00:00
|
|
|
|
2012-10-10 19:05:30 +00:00
|
|
|
if (! (ep_packet_mem.free_list || !ep_packet_mem.used_list)) {
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf(stderr, "No memory allocated\n");
|
2009-10-08 11:46:15 +00:00
|
|
|
ep_stat = FALSE;
|
|
|
|
}
|
2012-10-10 19:05:30 +00:00
|
|
|
if (ep_packet_mem.debug_use_chunks && ep_stat) {
|
2009-10-08 11:46:15 +00:00
|
|
|
/* Nothing interesting without chunks */
|
2009-10-09 21:27:46 +00:00
|
|
|
/* Only look at the used_list since those chunks are fully
|
|
|
|
* used. Looking at the free list would skew our view of what
|
|
|
|
* we have wasted.
|
2009-10-08 11:46:15 +00:00
|
|
|
*/
|
2012-10-10 19:05:30 +00:00
|
|
|
for (chunk = ep_packet_mem.used_list; chunk; chunk = chunk->next) {
|
2009-10-08 11:46:15 +00:00
|
|
|
num_chunks++;
|
|
|
|
total_used += (chunk->amount_free_init - chunk->amount_free);
|
|
|
|
total_allocation += chunk->amount_free_init;
|
|
|
|
}
|
|
|
|
if (num_chunks > 0) {
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf (stderr, "\n");
|
|
|
|
fprintf (stderr, "\n---- Buffer space ----\n");
|
|
|
|
fprintf (stderr, "\tChunk allocation size: %10u\n", EMEM_PACKET_CHUNK_SIZE);
|
|
|
|
fprintf (stderr, "\t* Number of chunks: %10u\n", num_chunks);
|
|
|
|
fprintf (stderr, "\t-------------------------------------------\n");
|
|
|
|
fprintf (stderr, "\t= %u (%u including guard pages) total space used for buffers\n",
|
2009-10-08 11:46:15 +00:00
|
|
|
total_allocation, EMEM_PACKET_CHUNK_SIZE * num_chunks);
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf (stderr, "\t-------------------------------------------\n");
|
2009-10-08 11:46:15 +00:00
|
|
|
total_space_allocated_from_os = total_allocation
|
2010-01-19 21:28:48 +00:00
|
|
|
+ sizeof(emem_chunk_t) * num_chunks;
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf (stderr, "Total allocated from OS: %u\n\n",
|
2009-10-08 11:46:15 +00:00
|
|
|
total_space_allocated_from_os);
|
|
|
|
}else{
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf (stderr, "No fully used chunks, nothing to do\n");
|
2009-10-08 11:46:15 +00:00
|
|
|
}
|
|
|
|
/* Reset stats */
|
|
|
|
num_chunks = 0;
|
|
|
|
num_allocs = 0;
|
|
|
|
total_used = 0;
|
|
|
|
total_allocation = 0;
|
|
|
|
used_for_canaries = 0;
|
|
|
|
}
|
|
|
|
|
2009-10-06 16:20:26 +00:00
|
|
|
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf(stderr, "\n-------- SE allocator statistics --------\n");
|
|
|
|
fprintf(stderr, "Total number of chunk allocations %u\n",
|
2009-10-08 11:46:15 +00:00
|
|
|
total_no_chunks);
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf(stderr, "%s chunks, %s canaries\n",
|
2009-10-19 22:31:08 +00:00
|
|
|
se_packet_mem.debug_use_chunks ? "Using" : "Not using",
|
|
|
|
se_packet_mem.debug_use_canary ? "using" : "not using");
|
2009-10-06 16:20:26 +00:00
|
|
|
|
|
|
|
if (! (se_packet_mem.free_list || !se_packet_mem.used_list)) {
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf(stderr, "No memory allocated\n");
|
2009-10-06 16:20:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
if (!se_packet_mem.debug_use_chunks )
|
2009-10-09 21:00:00 +00:00
|
|
|
return; /* Nothing interesting without chunks?? */
|
2009-10-06 16:20:26 +00:00
|
|
|
|
|
|
|
/* Only look at the used_list since those chunks are fully used.
|
2009-10-09 21:27:46 +00:00
|
|
|
* Looking at the free list would skew our view of what we have wasted.
|
2009-10-06 16:20:26 +00:00
|
|
|
*/
|
|
|
|
for (chunk = se_packet_mem.used_list; chunk; chunk = chunk->next) {
|
|
|
|
num_chunks++;
|
|
|
|
total_used += (chunk->amount_free_init - chunk->amount_free);
|
|
|
|
total_allocation += chunk->amount_free_init;
|
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
if (se_packet_mem.debug_use_canary){
|
2010-01-19 21:28:48 +00:00
|
|
|
void *ptr = chunk->canary_last;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
while (ptr != NULL) {
|
|
|
|
ptr = emem_canary_next(se_packet_mem.canary, ptr, &len);
|
|
|
|
|
|
|
|
if (ptr == (void *) -1)
|
|
|
|
g_error("Memory corrupted");
|
|
|
|
used_for_canaries += len;
|
2009-10-08 11:46:15 +00:00
|
|
|
}
|
2009-10-06 16:20:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_chunks == 0) {
|
|
|
|
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf (stderr, "No fully used chunks, nothing to do\n");
|
2009-10-06 16:20:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf (stderr, "\n");
|
|
|
|
fprintf (stderr, "---------- Allocations from the OS ----------\n");
|
|
|
|
fprintf (stderr, "---- Headers ----\n");
|
|
|
|
fprintf (stderr, "\t( Chunk header size: %10lu\n",
|
|
|
|
sizeof(emem_chunk_t));
|
|
|
|
fprintf (stderr, "\t* Number of chunks: %10u\n", num_chunks);
|
|
|
|
fprintf (stderr, "\t-------------------------------------------\n");
|
2009-10-09 21:00:00 +00:00
|
|
|
|
|
|
|
total_headers = sizeof(emem_chunk_t) * num_chunks;
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf (stderr, "\t= %u bytes used for headers\n", total_headers);
|
|
|
|
fprintf (stderr, "\n---- Buffer space ----\n");
|
|
|
|
fprintf (stderr, "\tChunk allocation size: %10u\n",
|
|
|
|
EMEM_PACKET_CHUNK_SIZE);
|
|
|
|
fprintf (stderr, "\t* Number of chunks: %10u\n", num_chunks);
|
|
|
|
fprintf (stderr, "\t-------------------------------------------\n");
|
|
|
|
fprintf (stderr, "\t= %u (%u including guard pages) bytes used for buffers\n",
|
2009-10-06 16:20:26 +00:00
|
|
|
total_allocation, EMEM_PACKET_CHUNK_SIZE * num_chunks);
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf (stderr, "\t-------------------------------------------\n");
|
2009-10-09 21:00:00 +00:00
|
|
|
total_space_allocated_from_os = (EMEM_PACKET_CHUNK_SIZE * num_chunks)
|
|
|
|
+ total_headers;
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf (stderr, "Total bytes allocated from the OS: %u\n\n",
|
2009-10-06 16:20:26 +00:00
|
|
|
total_space_allocated_from_os);
|
|
|
|
|
2009-10-09 21:00:00 +00:00
|
|
|
for (i = 0; i < NUM_ALLOC_DIST; i++)
|
|
|
|
num_allocs += allocations[i];
|
|
|
|
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf (stderr, "---------- Allocations from the SE pool ----------\n");
|
|
|
|
fprintf (stderr, " Number of SE allocations: %10u\n",
|
|
|
|
num_allocs);
|
|
|
|
fprintf (stderr, " Bytes used (incl. canaries): %10u\n",
|
|
|
|
total_used);
|
|
|
|
fprintf (stderr, " Bytes used for canaries: %10u\n",
|
|
|
|
used_for_canaries);
|
|
|
|
fprintf (stderr, "Bytes unused (wasted, excl. guard pages): %10u\n",
|
|
|
|
total_allocation - total_used);
|
|
|
|
fprintf (stderr, "Bytes unused (wasted, incl. guard pages): %10u\n\n",
|
|
|
|
total_space_allocated_from_os - total_used);
|
|
|
|
|
|
|
|
fprintf (stderr, "---------- Statistics ----------\n");
|
|
|
|
fprintf (stderr, "Average SE allocation size (incl. canaries): %6.2f\n",
|
2009-10-06 16:20:26 +00:00
|
|
|
(float)total_used/(float)num_allocs);
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf (stderr, "Average SE allocation size (excl. canaries): %6.2f\n",
|
2009-10-06 16:20:26 +00:00
|
|
|
(float)(total_used - used_for_canaries)/(float)num_allocs);
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf (stderr, " Average wasted bytes per allocation: %6.2f\n",
|
2009-10-06 16:20:26 +00:00
|
|
|
(total_allocation - total_used)/(float)num_allocs);
|
|
|
|
total_space_wasted = (total_allocation - total_used)
|
2010-01-19 21:28:48 +00:00
|
|
|
+ (sizeof(emem_chunk_t));
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf (stderr, " Space used for headers + unused allocation: %8u\n",
|
2009-10-06 16:20:26 +00:00
|
|
|
total_space_wasted);
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf (stderr, "--> %% overhead/waste: %4.2f\n",
|
2009-10-06 16:20:26 +00:00
|
|
|
100 * (float)total_space_wasted/(float)total_space_allocated_from_os);
|
|
|
|
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf (stderr, "\nAllocation distribution (sizes include canaries):\n");
|
2009-10-09 21:00:00 +00:00
|
|
|
for (i = 0; i < (NUM_ALLOC_DIST-1); i++)
|
2009-10-09 21:27:46 +00:00
|
|
|
fprintf (stderr, "size < %5d: %8u\n", 32<<i, allocations[i]);
|
|
|
|
fprintf (stderr, "size > %5d: %8u\n", 32<<i, allocations[i]);
|
2009-10-06 16:20:26 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-09-20 12:02:40 +00:00
|
|
|
static gboolean
|
2011-04-22 12:25:01 +00:00
|
|
|
emem_verify_pointer_list(const emem_chunk_t *chunk_list, const void *ptr)
|
2009-09-20 12:02:40 +00:00
|
|
|
{
|
|
|
|
const gchar *cptr = ptr;
|
2011-04-22 12:25:01 +00:00
|
|
|
const emem_chunk_t *chunk;
|
2009-09-24 13:37:02 +00:00
|
|
|
|
2011-04-22 12:25:01 +00:00
|
|
|
for (chunk = chunk_list; chunk; chunk = chunk->next) {
|
|
|
|
if (cptr >= (chunk->buf + chunk->free_offset_init) && cptr < (chunk->buf + chunk->free_offset))
|
|
|
|
return TRUE;
|
2009-09-20 12:02:40 +00:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-04-22 12:25:01 +00:00
|
|
|
static gboolean
|
2012-10-08 18:33:00 +00:00
|
|
|
emem_verify_pointer(const emem_pool_t *hdr, const void *ptr)
|
2011-04-22 12:25:01 +00:00
|
|
|
{
|
|
|
|
return emem_verify_pointer_list(hdr->free_list, ptr) || emem_verify_pointer_list(hdr->used_list, ptr);
|
|
|
|
}
|
|
|
|
|
2009-09-20 12:02:40 +00:00
|
|
|
gboolean
|
|
|
|
ep_verify_pointer(const void *ptr)
|
|
|
|
{
|
2012-10-10 19:05:30 +00:00
|
|
|
if (ep_packet_mem.debug_verify_pointers)
|
|
|
|
return emem_verify_pointer(&ep_packet_mem, ptr);
|
2010-09-02 18:02:06 +00:00
|
|
|
else
|
|
|
|
return FALSE;
|
2009-09-20 12:02:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
se_verify_pointer(const void *ptr)
|
|
|
|
{
|
2010-09-02 18:02:06 +00:00
|
|
|
if (se_packet_mem.debug_verify_pointers)
|
|
|
|
return emem_verify_pointer(&se_packet_mem, ptr);
|
|
|
|
else
|
|
|
|
return FALSE;
|
2009-09-20 12:02:40 +00:00
|
|
|
}
|
|
|
|
|
2009-10-16 21:36:42 +00:00
|
|
|
static void
|
|
|
|
emem_scrub_memory(char *buf, size_t size, gboolean alloc)
|
|
|
|
{
|
|
|
|
guint scrubbed_value;
|
|
|
|
guint offset;
|
|
|
|
|
|
|
|
if (!debug_use_memory_scrubber)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (alloc) /* this memory is being allocated */
|
2012-04-10 20:58:14 +00:00
|
|
|
scrubbed_value = 0xBADDCAFE;
|
2009-10-16 21:36:42 +00:00
|
|
|
else /* this memory is being freed */
|
2012-04-10 20:58:14 +00:00
|
|
|
scrubbed_value = 0xDEADBEEF;
|
2009-10-16 21:36:42 +00:00
|
|
|
|
|
|
|
/* We shouldn't need to check the alignment of the starting address
|
|
|
|
* since this is malloc'd memory (or 'pagesize' bytes into malloc'd
|
|
|
|
* memory).
|
|
|
|
*/
|
|
|
|
|
2011-04-22 04:01:30 +00:00
|
|
|
/* XXX - if the above is *NOT* true, we should use memcpy here,
|
|
|
|
* in order to avoid problems on alignment-sensitive platforms, e.g.
|
2009-10-23 21:07:28 +00:00
|
|
|
* http://stackoverflow.com/questions/108866/is-there-memset-that-accepts-integers-larger-than-char
|
|
|
|
*/
|
|
|
|
|
2009-10-16 21:36:42 +00:00
|
|
|
for (offset = 0; offset + sizeof(guint) <= size; offset += sizeof(guint))
|
2011-04-22 04:01:30 +00:00
|
|
|
*(guint*)(void*)(buf+offset) = scrubbed_value;
|
2009-10-16 21:36:42 +00:00
|
|
|
|
|
|
|
/* Initialize the last bytes, if any */
|
|
|
|
if (offset < size) {
|
2012-04-10 20:58:14 +00:00
|
|
|
*(guint8*)(buf+offset) = scrubbed_value >> 24;
|
2009-10-16 21:36:42 +00:00
|
|
|
offset++;
|
|
|
|
if (offset < size) {
|
2012-04-10 20:58:14 +00:00
|
|
|
*(guint8*)(buf+offset) = (scrubbed_value >> 16) & 0xFF;
|
2009-10-16 21:36:42 +00:00
|
|
|
offset++;
|
|
|
|
if (offset < size) {
|
2012-04-10 20:58:14 +00:00
|
|
|
*(guint8*)(buf+offset) = (scrubbed_value >> 8) & 0xFF;
|
2009-10-16 21:36:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-01-19 20:54:27 +00:00
|
|
|
static emem_chunk_t *
|
2011-05-09 08:55:35 +00:00
|
|
|
emem_create_chunk(size_t size)
|
|
|
|
{
|
2009-09-06 08:10:12 +00:00
|
|
|
emem_chunk_t *npc;
|
|
|
|
|
2009-09-20 09:03:02 +00:00
|
|
|
npc = g_new(emem_chunk_t, 1);
|
2009-09-06 08:10:12 +00:00
|
|
|
npc->next = NULL;
|
2010-01-19 21:28:48 +00:00
|
|
|
npc->canary_last = NULL;
|
2006-03-10 05:39:57 +00:00
|
|
|
|
2006-02-27 20:51:53 +00:00
|
|
|
#if defined (_WIN32)
|
2009-09-06 08:10:12 +00:00
|
|
|
/*
|
|
|
|
* MSDN documents VirtualAlloc/VirtualProtect at
|
|
|
|
* http://msdn.microsoft.com/library/en-us/memory/base/creating_guard_pages.asp
|
|
|
|
*/
|
2006-04-26 21:06:52 +00:00
|
|
|
|
2009-09-06 08:10:12 +00:00
|
|
|
/* XXX - is MEM_COMMIT|MEM_RESERVE correct? */
|
2011-05-09 08:55:35 +00:00
|
|
|
npc->buf = VirtualAlloc(NULL, size,
|
2009-09-06 08:10:12 +00:00
|
|
|
MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
|
2010-01-19 20:53:02 +00:00
|
|
|
|
2010-01-20 20:26:01 +00:00
|
|
|
if (npc->buf == NULL) {
|
|
|
|
g_free(npc);
|
2012-03-19 02:00:42 +00:00
|
|
|
if (getenv("WIRESHARK_ABORT_ON_OUT_OF_MEMORY"))
|
|
|
|
abort();
|
|
|
|
else
|
|
|
|
THROW(OutOfMemoryError);
|
2010-01-20 20:26:01 +00:00
|
|
|
}
|
|
|
|
|
2010-01-19 20:53:02 +00:00
|
|
|
#elif defined(USE_GUARD_PAGES)
|
2011-05-09 08:55:35 +00:00
|
|
|
npc->buf = mmap(NULL, size,
|
2010-01-19 20:53:02 +00:00
|
|
|
PROT_READ|PROT_WRITE, ANON_PAGE_MODE, ANON_FD, 0);
|
|
|
|
|
2010-01-20 20:26:01 +00:00
|
|
|
if (npc->buf == MAP_FAILED) {
|
2010-01-19 20:54:27 +00:00
|
|
|
g_free(npc);
|
2012-03-19 02:00:42 +00:00
|
|
|
if (getenv("WIRESHARK_ABORT_ON_OUT_OF_MEMORY"))
|
|
|
|
abort();
|
|
|
|
else
|
|
|
|
THROW(OutOfMemoryError);
|
2009-09-06 08:10:12 +00:00
|
|
|
}
|
2010-01-19 20:53:02 +00:00
|
|
|
|
2010-01-20 20:26:01 +00:00
|
|
|
#else /* Is there a draft in here? */
|
2011-05-09 08:55:35 +00:00
|
|
|
npc->buf = g_malloc(size);
|
2010-01-20 20:26:01 +00:00
|
|
|
/* g_malloc() can't fail */
|
|
|
|
#endif
|
|
|
|
|
2009-10-08 13:24:03 +00:00
|
|
|
#ifdef SHOW_EMEM_STATS
|
2009-10-08 11:46:15 +00:00
|
|
|
total_no_chunks++;
|
|
|
|
#endif
|
2010-01-19 20:53:02 +00:00
|
|
|
|
2011-05-09 12:52:55 +00:00
|
|
|
npc->amount_free = npc->amount_free_init = (unsigned int) size;
|
2011-05-09 08:55:35 +00:00
|
|
|
npc->free_offset = npc->free_offset_init = 0;
|
|
|
|
return npc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
emem_destroy_chunk(emem_chunk_t *npc)
|
|
|
|
{
|
|
|
|
#if defined (_WIN32)
|
2012-10-10 19:05:30 +00:00
|
|
|
VirtualFree(npc->buf, 0, MEM_RELEASE);
|
2011-05-09 08:55:35 +00:00
|
|
|
#elif defined(USE_GUARD_PAGES)
|
2012-11-05 09:04:53 +00:00
|
|
|
|
|
|
|
/* we cannot recover from a munmap() failure, but we */
|
|
|
|
/* can print an informative error message to stderr */
|
|
|
|
|
|
|
|
if (munmap(npc->buf, npc->amount_free_init) != 0)
|
|
|
|
fprintf(stderr, "Warning: Unable to unmap memory chunk which has address %p and size %u\n",
|
|
|
|
npc->buf, npc->amount_free_init);
|
2011-05-09 08:55:35 +00:00
|
|
|
#else
|
|
|
|
g_free(npc->buf);
|
|
|
|
#endif
|
|
|
|
#ifdef SHOW_EMEM_STATS
|
|
|
|
total_no_chunks--;
|
|
|
|
#endif
|
|
|
|
g_free(npc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static emem_chunk_t *
|
|
|
|
emem_create_chunk_gp(size_t size)
|
|
|
|
{
|
2010-01-19 20:53:02 +00:00
|
|
|
#if defined (_WIN32)
|
2011-05-09 08:55:35 +00:00
|
|
|
BOOL ret;
|
|
|
|
char *buf_end, *prot1, *prot2;
|
|
|
|
DWORD oldprot;
|
|
|
|
#elif defined(USE_GUARD_PAGES)
|
|
|
|
int ret;
|
|
|
|
char *buf_end, *prot1, *prot2;
|
|
|
|
#endif /* _WIN32 / USE_GUARD_PAGES */
|
|
|
|
emem_chunk_t *npc;
|
|
|
|
|
|
|
|
npc = emem_create_chunk(size);
|
|
|
|
|
|
|
|
#if defined (_WIN32)
|
|
|
|
buf_end = npc->buf + size;
|
2006-02-27 20:51:53 +00:00
|
|
|
|
2009-09-06 08:10:12 +00:00
|
|
|
/* Align our guard pages on page-sized boundaries */
|
2012-09-11 13:11:33 +00:00
|
|
|
prot1 = (char *) ((((intptr_t) npc->buf + pagesize - 1) / pagesize) * pagesize);
|
|
|
|
prot2 = (char *) ((((intptr_t) buf_end - (1 * pagesize)) / pagesize) * pagesize);
|
2006-02-27 20:51:53 +00:00
|
|
|
|
2009-09-06 08:10:12 +00:00
|
|
|
ret = VirtualProtect(prot1, pagesize, PAGE_NOACCESS, &oldprot);
|
|
|
|
g_assert(ret != 0 || versinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
|
|
|
|
ret = VirtualProtect(prot2, pagesize, PAGE_NOACCESS, &oldprot);
|
|
|
|
g_assert(ret != 0 || versinfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);
|
2006-02-27 20:51:53 +00:00
|
|
|
|
2009-09-06 08:10:12 +00:00
|
|
|
npc->amount_free_init = (unsigned int) (prot2 - prot1 - pagesize);
|
|
|
|
npc->free_offset_init = (unsigned int) (prot1 - npc->buf) + pagesize;
|
2006-02-27 20:51:53 +00:00
|
|
|
#elif defined(USE_GUARD_PAGES)
|
2011-05-09 08:55:35 +00:00
|
|
|
buf_end = npc->buf + size;
|
2010-01-19 20:53:02 +00:00
|
|
|
|
2009-09-06 08:10:12 +00:00
|
|
|
/* Align our guard pages on page-sized boundaries */
|
|
|
|
prot1 = (char *) ((((intptr_t) npc->buf + pagesize - 1) / pagesize) * pagesize);
|
|
|
|
prot2 = (char *) ((((intptr_t) buf_end - (1 * pagesize)) / pagesize) * pagesize);
|
2010-01-19 20:53:02 +00:00
|
|
|
|
2009-09-06 08:10:12 +00:00
|
|
|
ret = mprotect(prot1, pagesize, PROT_NONE);
|
|
|
|
g_assert(ret != -1);
|
|
|
|
ret = mprotect(prot2, pagesize, PROT_NONE);
|
|
|
|
g_assert(ret != -1);
|
|
|
|
|
|
|
|
npc->amount_free_init = prot2 - prot1 - pagesize;
|
|
|
|
npc->free_offset_init = (prot1 - npc->buf) + pagesize;
|
2010-09-02 18:02:06 +00:00
|
|
|
#else
|
2011-05-09 08:55:35 +00:00
|
|
|
npc->amount_free_init = size;
|
2009-09-06 08:10:12 +00:00
|
|
|
npc->free_offset_init = 0;
|
2006-02-27 20:51:53 +00:00
|
|
|
#endif /* USE_GUARD_PAGES */
|
2010-01-19 20:53:02 +00:00
|
|
|
|
|
|
|
npc->amount_free = npc->amount_free_init;
|
|
|
|
npc->free_offset = npc->free_offset_init;
|
2010-01-19 20:54:27 +00:00
|
|
|
return npc;
|
2006-02-27 20:51:53 +00:00
|
|
|
}
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2009-08-09 19:23:13 +00:00
|
|
|
static void *
|
2012-10-08 18:33:00 +00:00
|
|
|
emem_alloc_chunk(size_t size, emem_pool_t *mem)
|
2005-07-22 07:46:58 +00:00
|
|
|
{
|
2006-03-07 16:31:47 +00:00
|
|
|
void *buf;
|
2005-07-22 07:46:58 +00:00
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
size_t asize = size;
|
|
|
|
gboolean use_canary = mem->debug_use_canary;
|
|
|
|
guint8 pad;
|
|
|
|
emem_chunk_t *free_list;
|
2009-10-16 21:36:42 +00:00
|
|
|
|
2011-02-25 15:49:28 +00:00
|
|
|
/* Allocate room for at least 8 bytes of canary plus some padding
|
|
|
|
* so the canary ends on an 8-byte boundary.
|
2012-05-04 01:33:58 +00:00
|
|
|
* But first add the room needed for the pointer to the next canary
|
|
|
|
* (so the entire allocation will end on an 8-byte boundary).
|
2009-10-19 22:31:08 +00:00
|
|
|
*/
|
2010-01-19 21:28:48 +00:00
|
|
|
if (use_canary) {
|
|
|
|
asize += sizeof(void *);
|
2012-05-04 01:33:58 +00:00
|
|
|
pad = emem_canary_pad(asize);
|
2010-01-19 21:28:48 +00:00
|
|
|
} else
|
2012-05-04 21:56:32 +00:00
|
|
|
pad = (WS_MEM_ALIGN - (asize & (WS_MEM_ALIGN-1))) & (WS_MEM_ALIGN-1);
|
2009-08-11 09:24:56 +00:00
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
asize += pad;
|
2005-07-22 07:46:58 +00:00
|
|
|
|
2009-10-06 16:20:26 +00:00
|
|
|
#ifdef SHOW_EMEM_STATS
|
2009-10-19 22:31:08 +00:00
|
|
|
/* Do this check here so we can include the canary size */
|
|
|
|
if (mem == &se_packet_mem) {
|
|
|
|
if (asize < 32)
|
|
|
|
allocations[0]++;
|
|
|
|
else if (asize < 64)
|
|
|
|
allocations[1]++;
|
|
|
|
else if (asize < 128)
|
|
|
|
allocations[2]++;
|
|
|
|
else if (asize < 256)
|
|
|
|
allocations[3]++;
|
|
|
|
else if (asize < 512)
|
|
|
|
allocations[4]++;
|
|
|
|
else if (asize < 1024)
|
|
|
|
allocations[5]++;
|
|
|
|
else if (asize < 2048)
|
|
|
|
allocations[6]++;
|
|
|
|
else if (asize < 4096)
|
|
|
|
allocations[7]++;
|
|
|
|
else if (asize < 8192)
|
|
|
|
allocations[8]++;
|
|
|
|
else if (asize < 16384)
|
|
|
|
allocations[8]++;
|
|
|
|
else
|
|
|
|
allocations[(NUM_ALLOC_DIST-1)]++;
|
|
|
|
}
|
2009-10-06 16:20:26 +00:00
|
|
|
#endif
|
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
/* make sure we dont try to allocate too much (arbitrary limit) */
|
2009-11-30 17:31:13 +00:00
|
|
|
DISSECTOR_ASSERT(size<(EMEM_PACKET_CHUNK_SIZE>>2));
|
2009-10-19 22:31:08 +00:00
|
|
|
|
|
|
|
if (!mem->free_list)
|
2011-05-09 08:55:35 +00:00
|
|
|
mem->free_list = emem_create_chunk_gp(EMEM_PACKET_CHUNK_SIZE);
|
2009-10-19 22:31:08 +00:00
|
|
|
|
|
|
|
/* oops, we need to allocate more memory to serve this request
|
|
|
|
* than we have free. move this node to the used list and try again
|
|
|
|
*/
|
2010-01-19 21:28:48 +00:00
|
|
|
if(asize > mem->free_list->amount_free) {
|
2009-10-19 22:31:08 +00:00
|
|
|
emem_chunk_t *npc;
|
|
|
|
npc=mem->free_list;
|
|
|
|
mem->free_list=mem->free_list->next;
|
|
|
|
npc->next=mem->used_list;
|
|
|
|
mem->used_list=npc;
|
2005-07-22 07:46:58 +00:00
|
|
|
|
2009-09-06 08:10:12 +00:00
|
|
|
if (!mem->free_list)
|
2011-05-09 08:55:35 +00:00
|
|
|
mem->free_list = emem_create_chunk_gp(EMEM_PACKET_CHUNK_SIZE);
|
2009-10-19 22:31:08 +00:00
|
|
|
}
|
2005-08-12 08:51:08 +00:00
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
free_list = mem->free_list;
|
2005-08-12 08:51:08 +00:00
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
buf = free_list->buf + free_list->free_offset;
|
2006-01-10 21:12:48 +00:00
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
free_list->amount_free -= (unsigned int) asize;
|
|
|
|
free_list->free_offset += (unsigned int) asize;
|
2005-08-12 08:51:08 +00:00
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
if (use_canary) {
|
2010-01-19 21:28:48 +00:00
|
|
|
char *cptr = (char *)buf + size;
|
|
|
|
|
|
|
|
memcpy(cptr, mem->canary, pad-1);
|
|
|
|
cptr[pad-1] = '\0';
|
|
|
|
memcpy(cptr + pad, &free_list->canary_last, sizeof(void *));
|
|
|
|
|
|
|
|
free_list->canary_last = cptr;
|
2009-10-19 22:31:08 +00:00
|
|
|
}
|
2005-08-12 08:51:08 +00:00
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
return buf;
|
|
|
|
}
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
static void *
|
2012-10-08 18:33:00 +00:00
|
|
|
emem_alloc_glib(size_t size, emem_pool_t *mem)
|
2009-10-19 22:31:08 +00:00
|
|
|
{
|
|
|
|
emem_chunk_t *npc;
|
|
|
|
|
|
|
|
npc=g_new(emem_chunk_t, 1);
|
|
|
|
npc->next=mem->used_list;
|
|
|
|
npc->buf=g_malloc(size);
|
2010-01-19 21:28:48 +00:00
|
|
|
npc->canary_last = NULL;
|
2009-10-19 22:31:08 +00:00
|
|
|
mem->used_list=npc;
|
|
|
|
/* There's no padding/alignment involved (from our point of view) when
|
|
|
|
* we fetch the memory directly from the system pool, so WYSIWYG */
|
|
|
|
npc->free_offset = npc->free_offset_init = 0;
|
|
|
|
npc->amount_free = npc->amount_free_init = (unsigned int) size;
|
|
|
|
|
|
|
|
return npc->buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate 'size' amount of memory. */
|
|
|
|
static void *
|
2012-10-08 18:33:00 +00:00
|
|
|
emem_alloc(size_t size, emem_pool_t *mem)
|
2009-10-19 22:31:08 +00:00
|
|
|
{
|
|
|
|
void *buf = mem->memory_alloc(size, mem);
|
2005-08-26 15:45:31 +00:00
|
|
|
|
2009-10-16 21:36:42 +00:00
|
|
|
/* XXX - this is a waste of time if the allocator function is going to
|
|
|
|
* memset this straight back to 0.
|
|
|
|
*/
|
|
|
|
emem_scrub_memory(buf, size, TRUE);
|
|
|
|
|
2005-08-12 08:51:08 +00:00
|
|
|
return buf;
|
|
|
|
}
|
2009-08-09 19:23:13 +00:00
|
|
|
|
|
|
|
/* allocate 'size' amount of memory with an allocation lifetime until the
|
|
|
|
* next packet.
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
ep_alloc(size_t size)
|
|
|
|
{
|
2012-10-10 19:05:30 +00:00
|
|
|
return emem_alloc(size, &ep_packet_mem);
|
2009-08-09 19:23:13 +00:00
|
|
|
}
|
|
|
|
|
2005-08-12 08:51:08 +00:00
|
|
|
/* allocate 'size' amount of memory with an allocation lifetime until the
|
|
|
|
* next capture.
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
se_alloc(size_t size)
|
|
|
|
{
|
2009-10-19 22:31:08 +00:00
|
|
|
return emem_alloc(size, &se_packet_mem);
|
2005-07-22 07:46:58 +00:00
|
|
|
}
|
|
|
|
|
2011-05-09 08:55:35 +00:00
|
|
|
void *
|
|
|
|
sl_alloc(struct ws_memory_slab *mem_chunk)
|
|
|
|
{
|
|
|
|
emem_chunk_t *chunk;
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
/* XXX, debug_use_slices -> fallback to g_slice_alloc0 */
|
|
|
|
|
2011-05-09 12:52:55 +00:00
|
|
|
if ((mem_chunk->freed != NULL)) {
|
2011-05-09 08:55:35 +00:00
|
|
|
ptr = mem_chunk->freed;
|
|
|
|
memcpy(&mem_chunk->freed, ptr, sizeof(void *));
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(chunk = mem_chunk->chunk_list) || chunk->amount_free < (guint) mem_chunk->item_size) {
|
2011-05-09 12:52:55 +00:00
|
|
|
size_t alloc_size = mem_chunk->item_size * mem_chunk->count;
|
|
|
|
|
|
|
|
/* align to page-size */
|
|
|
|
#if defined (_WIN32) || defined(USE_GUARD_PAGES)
|
|
|
|
alloc_size = (alloc_size + (pagesize - 1)) & ~(pagesize - 1);
|
|
|
|
#endif
|
2011-05-09 08:55:35 +00:00
|
|
|
|
2011-05-09 12:52:55 +00:00
|
|
|
chunk = emem_create_chunk(alloc_size); /* NOTE: using version without guard pages! */
|
2011-05-09 08:55:35 +00:00
|
|
|
chunk->next = mem_chunk->chunk_list;
|
|
|
|
mem_chunk->chunk_list = chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr = chunk->buf + chunk->free_offset;
|
|
|
|
chunk->free_offset += mem_chunk->item_size;
|
|
|
|
chunk->amount_free -= mem_chunk->item_size;
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sl_free(struct ws_memory_slab *mem_chunk, gpointer ptr)
|
|
|
|
{
|
|
|
|
/* XXX, debug_use_slices -> fallback to g_slice_free1 */
|
|
|
|
|
|
|
|
/* XXX, abort if ptr not found in emem_verify_pointer_list()? */
|
2011-05-09 12:52:55 +00:00
|
|
|
if (ptr != NULL /* && emem_verify_pointer_list(mem_chunk->chunk_list, ptr) */) {
|
|
|
|
memcpy(ptr, &(mem_chunk->freed), sizeof(void *));
|
2011-05-09 08:55:35 +00:00
|
|
|
mem_chunk->freed = ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
void *
|
|
|
|
ep_alloc0(size_t size)
|
|
|
|
{
|
2005-07-26 18:32:12 +00:00
|
|
|
return memset(ep_alloc(size),'\0',size);
|
|
|
|
}
|
2005-07-24 00:29:57 +00:00
|
|
|
|
2011-03-26 02:11:42 +00:00
|
|
|
void *
|
|
|
|
se_alloc0(size_t size)
|
2009-10-19 22:31:08 +00:00
|
|
|
{
|
2011-03-26 02:11:42 +00:00
|
|
|
return memset(se_alloc(size),'\0',size);
|
|
|
|
}
|
|
|
|
|
2011-05-09 08:55:35 +00:00
|
|
|
void *
|
|
|
|
sl_alloc0(struct ws_memory_slab *mem_chunk)
|
|
|
|
{
|
|
|
|
return memset(sl_alloc(mem_chunk), '\0', mem_chunk->item_size);
|
|
|
|
}
|
2011-03-26 02:11:42 +00:00
|
|
|
|
|
|
|
static gchar *
|
|
|
|
emem_strdup(const gchar *src, void *allocator(size_t))
|
|
|
|
{
|
|
|
|
guint len;
|
|
|
|
gchar *dst;
|
|
|
|
|
|
|
|
/* If str is NULL, just return the string "<NULL>" so that the callers don't
|
|
|
|
* have to bother checking it.
|
|
|
|
*/
|
|
|
|
if(!src)
|
|
|
|
return "<NULL>";
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2011-03-26 02:11:42 +00:00
|
|
|
len = (guint) strlen(src);
|
|
|
|
dst = memcpy(allocator(len+1), src, len+1);
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2005-07-24 00:29:57 +00:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
gchar *
|
2011-03-26 02:11:42 +00:00
|
|
|
ep_strdup(const gchar *src)
|
|
|
|
{
|
|
|
|
return emem_strdup(src, ep_alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
gchar *
|
|
|
|
se_strdup(const gchar *src)
|
|
|
|
{
|
|
|
|
return emem_strdup(src, se_alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *
|
|
|
|
emem_strndup(const gchar *src, size_t len, void *allocator(size_t))
|
2009-10-19 22:31:08 +00:00
|
|
|
{
|
2011-03-26 02:11:42 +00:00
|
|
|
gchar *dst = allocator(len+1);
|
2010-02-04 06:42:04 +00:00
|
|
|
guint i;
|
|
|
|
|
|
|
|
for (i = 0; (i < len) && src[i]; i++)
|
|
|
|
dst[i] = src[i];
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2010-02-04 06:42:04 +00:00
|
|
|
dst[i] = '\0';
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2005-07-24 00:29:57 +00:00
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2011-03-26 02:11:42 +00:00
|
|
|
gchar *
|
|
|
|
ep_strndup(const gchar *src, size_t len)
|
|
|
|
{
|
|
|
|
return emem_strndup(src, len, ep_alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
gchar *
|
|
|
|
se_strndup(const gchar *src, size_t len)
|
|
|
|
{
|
|
|
|
return emem_strndup(src, len, se_alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
void *
|
|
|
|
ep_memdup(const void* src, size_t len)
|
|
|
|
{
|
2005-07-24 00:29:57 +00:00
|
|
|
return memcpy(ep_alloc(len), src, len);
|
|
|
|
}
|
|
|
|
|
2011-03-26 02:11:42 +00:00
|
|
|
void *
|
|
|
|
se_memdup(const void* src, size_t len)
|
|
|
|
{
|
|
|
|
return memcpy(se_alloc(len), src, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *
|
|
|
|
emem_strdup_vprintf(const gchar *fmt, va_list ap, void *allocator(size_t))
|
2009-10-19 22:31:08 +00:00
|
|
|
{
|
2005-10-10 08:24:56 +00:00
|
|
|
va_list ap2;
|
2009-04-07 16:36:52 +00:00
|
|
|
gsize len;
|
2005-10-10 08:24:56 +00:00
|
|
|
gchar* dst;
|
|
|
|
|
|
|
|
G_VA_COPY(ap2, ap);
|
|
|
|
|
|
|
|
len = g_printf_string_upper_bound(fmt, ap);
|
|
|
|
|
2011-03-26 02:11:42 +00:00
|
|
|
dst = allocator(len+1);
|
2009-04-07 16:36:52 +00:00
|
|
|
g_vsnprintf (dst, (gulong) len, fmt, ap2);
|
2005-10-10 08:24:56 +00:00
|
|
|
va_end(ap2);
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
gchar *
|
2011-03-26 02:11:42 +00:00
|
|
|
ep_strdup_vprintf(const gchar *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
return emem_strdup_vprintf(fmt, ap, ep_alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
gchar *
|
|
|
|
se_strdup_vprintf(const gchar* fmt, va_list ap)
|
|
|
|
{
|
|
|
|
return emem_strdup_vprintf(fmt, ap, se_alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
gchar *
|
|
|
|
ep_strdup_printf(const gchar *fmt, ...)
|
2009-10-19 22:31:08 +00:00
|
|
|
{
|
2005-07-24 00:29:57 +00:00
|
|
|
va_list ap;
|
2011-03-26 02:11:42 +00:00
|
|
|
gchar *dst;
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2011-03-26 02:11:42 +00:00
|
|
|
va_start(ap, fmt);
|
2005-10-10 08:24:56 +00:00
|
|
|
dst = ep_strdup_vprintf(fmt, ap);
|
2005-07-24 00:29:57 +00:00
|
|
|
va_end(ap);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2011-03-26 02:11:42 +00:00
|
|
|
gchar *
|
|
|
|
se_strdup_printf(const gchar *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
gchar *dst;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
dst = se_strdup_vprintf(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
gchar **
|
|
|
|
ep_strsplit(const gchar* string, const gchar* sep, int max_tokens)
|
|
|
|
{
|
2005-07-26 18:32:12 +00:00
|
|
|
gchar* splitted;
|
|
|
|
gchar* s;
|
|
|
|
guint tokens;
|
2005-07-27 00:45:02 +00:00
|
|
|
guint str_len;
|
|
|
|
guint sep_len;
|
2005-07-26 18:32:12 +00:00
|
|
|
guint i;
|
|
|
|
gchar** vec;
|
|
|
|
enum { AT_START, IN_PAD, IN_TOKEN } state;
|
|
|
|
guint curr_tok = 0;
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
if ( ! string
|
|
|
|
|| ! sep
|
|
|
|
|| ! sep[0])
|
2005-07-26 18:32:12 +00:00
|
|
|
return NULL;
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2005-07-26 18:32:12 +00:00
|
|
|
s = splitted = ep_strdup(string);
|
2009-04-07 16:36:52 +00:00
|
|
|
str_len = (guint) strlen(splitted);
|
|
|
|
sep_len = (guint) strlen(sep);
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2005-07-26 18:32:12 +00:00
|
|
|
if (max_tokens < 1) max_tokens = INT_MAX;
|
|
|
|
|
|
|
|
tokens = 1;
|
2006-01-09 23:11:40 +00:00
|
|
|
|
|
|
|
|
2005-07-26 18:32:12 +00:00
|
|
|
while (tokens <= (guint)max_tokens && ( s = strstr(s,sep) )) {
|
|
|
|
tokens++;
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2005-07-26 18:32:12 +00:00
|
|
|
for(i=0; i < sep_len; i++ )
|
|
|
|
s[i] = '\0';
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2005-07-26 18:32:12 +00:00
|
|
|
s += sep_len;
|
2006-01-09 23:11:40 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-07-27 00:45:02 +00:00
|
|
|
vec = ep_alloc_array(gchar*,tokens+1);
|
2005-07-26 18:32:12 +00:00
|
|
|
state = AT_START;
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2005-07-26 18:32:12 +00:00
|
|
|
for (i=0; i< str_len; i++) {
|
|
|
|
switch(state) {
|
|
|
|
case AT_START:
|
|
|
|
switch(splitted[i]) {
|
|
|
|
case '\0':
|
|
|
|
state = IN_PAD;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
vec[curr_tok] = &(splitted[i]);
|
|
|
|
curr_tok++;
|
|
|
|
state = IN_TOKEN;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case IN_TOKEN:
|
|
|
|
switch(splitted[i]) {
|
|
|
|
case '\0':
|
|
|
|
state = IN_PAD;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case IN_PAD:
|
|
|
|
switch(splitted[i]) {
|
|
|
|
default:
|
|
|
|
vec[curr_tok] = &(splitted[i]);
|
|
|
|
curr_tok++;
|
|
|
|
state = IN_TOKEN;
|
|
|
|
case '\0':
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2005-07-26 18:32:12 +00:00
|
|
|
vec[curr_tok] = NULL;
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2005-07-26 18:32:12 +00:00
|
|
|
return vec;
|
|
|
|
}
|
2005-07-24 00:29:57 +00:00
|
|
|
|
2011-03-26 02:39:04 +00:00
|
|
|
gchar *
|
|
|
|
ep_strconcat(const gchar *string1, ...)
|
|
|
|
{
|
|
|
|
gsize l;
|
|
|
|
va_list args;
|
|
|
|
gchar *s;
|
|
|
|
gchar *concat;
|
|
|
|
gchar *ptr;
|
|
|
|
|
|
|
|
if (!string1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
l = 1 + strlen(string1);
|
|
|
|
va_start(args, string1);
|
|
|
|
s = va_arg(args, gchar*);
|
|
|
|
while (s) {
|
|
|
|
l += strlen(s);
|
|
|
|
s = va_arg(args, gchar*);
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
concat = ep_alloc(l);
|
|
|
|
ptr = concat;
|
|
|
|
|
|
|
|
ptr = g_stpcpy(ptr, string1);
|
|
|
|
va_start(args, string1);
|
|
|
|
s = va_arg(args, gchar*);
|
|
|
|
while (s) {
|
|
|
|
ptr = g_stpcpy(ptr, s);
|
|
|
|
s = va_arg(args, gchar*);
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
return concat;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-16 00:55:08 +00:00
|
|
|
|
2009-08-09 20:10:19 +00:00
|
|
|
/* release all allocated memory back to the pool. */
|
|
|
|
static void
|
2012-10-08 18:33:00 +00:00
|
|
|
emem_free_all(emem_pool_t *mem)
|
2005-07-22 07:46:58 +00:00
|
|
|
{
|
2009-10-19 22:31:08 +00:00
|
|
|
gboolean use_chunks = mem->debug_use_chunks;
|
|
|
|
|
2005-07-22 07:46:58 +00:00
|
|
|
emem_chunk_t *npc;
|
2009-08-09 20:10:19 +00:00
|
|
|
emem_tree_t *tree_list;
|
2005-07-22 07:46:58 +00:00
|
|
|
|
2005-08-26 15:45:31 +00:00
|
|
|
/* move all used chunks over to the free list */
|
2009-08-09 20:10:19 +00:00
|
|
|
while(mem->used_list){
|
|
|
|
npc=mem->used_list;
|
|
|
|
mem->used_list=mem->used_list->next;
|
|
|
|
npc->next=mem->free_list;
|
|
|
|
mem->free_list=npc;
|
2005-08-12 08:51:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clear them all out */
|
2009-08-09 20:10:19 +00:00
|
|
|
npc = mem->free_list;
|
2005-08-26 15:45:31 +00:00
|
|
|
while (npc != NULL) {
|
2009-08-11 09:24:56 +00:00
|
|
|
if (use_chunks) {
|
2010-01-19 21:28:48 +00:00
|
|
|
while (npc->canary_last != NULL) {
|
|
|
|
npc->canary_last = emem_canary_next(mem->canary, npc->canary_last, NULL);
|
|
|
|
/* XXX, check if canary_last is inside allocated memory? */
|
|
|
|
|
|
|
|
if (npc->canary_last == (void *) -1)
|
|
|
|
g_error("Memory corrupted");
|
2009-08-09 20:10:19 +00:00
|
|
|
}
|
2009-10-16 21:36:42 +00:00
|
|
|
|
|
|
|
emem_scrub_memory((npc->buf + npc->free_offset_init),
|
|
|
|
(npc->free_offset - npc->free_offset_init),
|
|
|
|
FALSE);
|
|
|
|
|
2012-10-10 19:05:30 +00:00
|
|
|
npc->amount_free = npc->amount_free_init;
|
|
|
|
npc->free_offset = npc->free_offset_init;
|
|
|
|
npc = npc->next;
|
2009-08-09 20:10:19 +00:00
|
|
|
} else {
|
|
|
|
emem_chunk_t *next = npc->next;
|
|
|
|
|
2009-10-16 21:36:42 +00:00
|
|
|
emem_scrub_memory(npc->buf, npc->amount_free_init, FALSE);
|
|
|
|
|
2009-08-09 20:10:19 +00:00
|
|
|
g_free(npc->buf);
|
|
|
|
g_free(npc);
|
|
|
|
npc = next;
|
|
|
|
}
|
2005-08-12 08:51:08 +00:00
|
|
|
}
|
2005-08-26 15:45:31 +00:00
|
|
|
|
2012-10-10 19:05:30 +00:00
|
|
|
if (!use_chunks) {
|
|
|
|
/* We've freed all this memory already */
|
|
|
|
mem->free_list = NULL;
|
|
|
|
}
|
2009-10-20 17:16:40 +00:00
|
|
|
|
2009-08-09 20:10:19 +00:00
|
|
|
/* release/reset all allocated trees */
|
2009-10-19 22:31:08 +00:00
|
|
|
for(tree_list=mem->trees;tree_list;tree_list=tree_list->next){
|
2009-08-09 20:10:19 +00:00
|
|
|
tree_list->tree=NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-10 19:05:30 +00:00
|
|
|
/* release all allocated memory back to the pool. */
|
2012-10-08 16:42:54 +00:00
|
|
|
void
|
2012-10-10 19:05:30 +00:00
|
|
|
ep_free_all(void)
|
2009-08-09 20:10:19 +00:00
|
|
|
{
|
2012-10-10 19:05:30 +00:00
|
|
|
emem_free_all(&ep_packet_mem);
|
2005-08-12 08:51:08 +00:00
|
|
|
}
|
2009-08-09 20:10:19 +00:00
|
|
|
|
|
|
|
/* release all allocated memory back to the pool. */
|
2005-08-12 08:51:08 +00:00
|
|
|
void
|
|
|
|
se_free_all(void)
|
|
|
|
{
|
2009-10-06 16:20:26 +00:00
|
|
|
#ifdef SHOW_EMEM_STATS
|
|
|
|
print_alloc_stats();
|
|
|
|
#endif
|
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
emem_free_all(&se_packet_mem);
|
2005-07-22 07:46:58 +00:00
|
|
|
}
|
|
|
|
|
2011-05-09 08:55:35 +00:00
|
|
|
void
|
|
|
|
sl_free_all(struct ws_memory_slab *mem_chunk)
|
|
|
|
{
|
|
|
|
emem_chunk_t *chunk_list = mem_chunk->chunk_list;
|
|
|
|
|
|
|
|
mem_chunk->chunk_list = NULL;
|
2011-05-09 12:52:55 +00:00
|
|
|
mem_chunk->freed = NULL;
|
2011-05-09 08:55:35 +00:00
|
|
|
while (chunk_list) {
|
|
|
|
emem_chunk_t *chunk = chunk_list;
|
|
|
|
|
|
|
|
chunk_list = chunk_list->next;
|
|
|
|
emem_destroy_chunk(chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-20 17:16:40 +00:00
|
|
|
ep_stack_t
|
|
|
|
ep_stack_new(void) {
|
2009-04-14 14:08:19 +00:00
|
|
|
ep_stack_t s = ep_new(struct _ep_stack_frame_t*);
|
|
|
|
*s = ep_new0(struct _ep_stack_frame_t);
|
|
|
|
return s;
|
2005-10-01 10:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* for ep_stack_t we'll keep the popped frames so we reuse them instead
|
|
|
|
of allocating new ones.
|
|
|
|
*/
|
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
void *
|
|
|
|
ep_stack_push(ep_stack_t stack, void* data)
|
|
|
|
{
|
2009-04-14 14:08:19 +00:00
|
|
|
struct _ep_stack_frame_t* frame;
|
|
|
|
struct _ep_stack_frame_t* head = (*stack);
|
2005-10-01 10:36:57 +00:00
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
if (head->above) {
|
|
|
|
frame = head->above;
|
|
|
|
} else {
|
|
|
|
frame = ep_new(struct _ep_stack_frame_t);
|
|
|
|
head->above = frame;
|
|
|
|
frame->below = head;
|
|
|
|
frame->above = NULL;
|
|
|
|
}
|
2005-10-01 10:36:57 +00:00
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
frame->payload = data;
|
|
|
|
(*stack) = frame;
|
2005-10-01 10:36:57 +00:00
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
return data;
|
2005-10-01 10:36:57 +00:00
|
|
|
}
|
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
void *
|
|
|
|
ep_stack_pop(ep_stack_t stack)
|
|
|
|
{
|
2006-01-09 23:11:40 +00:00
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
if ((*stack)->below) {
|
|
|
|
(*stack) = (*stack)->below;
|
|
|
|
return (*stack)->above->payload;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-10-01 10:36:57 +00:00
|
|
|
}
|
|
|
|
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_t *
|
2007-04-23 10:59:26 +00:00
|
|
|
se_tree_create(int type, const char *name)
|
2006-03-05 04:01:34 +00:00
|
|
|
{
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_t *tree_list;
|
2006-03-05 04:01:34 +00:00
|
|
|
|
2009-10-05 18:20:38 +00:00
|
|
|
tree_list=g_malloc(sizeof(emem_tree_t));
|
2009-10-19 22:31:08 +00:00
|
|
|
tree_list->next=se_packet_mem.trees;
|
2006-03-05 04:01:34 +00:00
|
|
|
tree_list->type=type;
|
|
|
|
tree_list->tree=NULL;
|
2006-03-11 13:02:41 +00:00
|
|
|
tree_list->name=name;
|
2006-08-12 23:27:22 +00:00
|
|
|
tree_list->malloc=se_alloc;
|
2009-10-19 22:31:08 +00:00
|
|
|
se_packet_mem.trees=tree_list;
|
2006-03-05 04:01:34 +00:00
|
|
|
|
|
|
|
return tree_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2006-08-14 09:36:15 +00:00
|
|
|
emem_tree_lookup32(emem_tree_t *se_tree, guint32 key)
|
2006-03-05 04:01:34 +00:00
|
|
|
{
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_node_t *node;
|
2006-03-05 04:01:34 +00:00
|
|
|
|
|
|
|
node=se_tree->tree;
|
|
|
|
|
|
|
|
while(node){
|
|
|
|
if(key==node->key32){
|
|
|
|
return node->data;
|
|
|
|
}
|
|
|
|
if(key<node->key32){
|
|
|
|
node=node->left;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(key>node->key32){
|
|
|
|
node=node->right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-03-18 06:15:39 +00:00
|
|
|
void *
|
2006-08-14 09:36:15 +00:00
|
|
|
emem_tree_lookup32_le(emem_tree_t *se_tree, guint32 key)
|
2006-03-18 06:15:39 +00:00
|
|
|
{
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_node_t *node;
|
2006-03-18 06:15:39 +00:00
|
|
|
|
|
|
|
node=se_tree->tree;
|
|
|
|
|
|
|
|
if(!node){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while(node){
|
|
|
|
if(key==node->key32){
|
|
|
|
return node->data;
|
|
|
|
}
|
|
|
|
if(key<node->key32){
|
|
|
|
if(node->left){
|
|
|
|
node=node->left;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(key>node->key32){
|
|
|
|
if(node->right){
|
|
|
|
node=node->right;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-29 06:50:35 +00:00
|
|
|
if(!node){
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-04-26 21:06:52 +00:00
|
|
|
/* If we are still at the root of the tree this means that this node
|
2009-04-14 14:08:19 +00:00
|
|
|
* is either smaller than the search key and then we return this
|
|
|
|
* node or else there is no smaller key available and then
|
2006-03-18 06:15:39 +00:00
|
|
|
* we return NULL.
|
|
|
|
*/
|
|
|
|
if(!node->parent){
|
|
|
|
if(key>node->key32){
|
|
|
|
return node->data;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(node->parent->left==node){
|
2006-04-26 21:06:52 +00:00
|
|
|
/* left child */
|
2006-03-18 06:15:39 +00:00
|
|
|
|
|
|
|
if(key>node->key32){
|
|
|
|
/* if this is a left child and its key is smaller than
|
|
|
|
* the search key, then this is the node we want.
|
|
|
|
*/
|
|
|
|
return node->data;
|
|
|
|
} else {
|
|
|
|
/* if this is a left child and its key is bigger than
|
|
|
|
* the search key, we have to check if any
|
|
|
|
* of our ancestors are smaller than the search key.
|
|
|
|
*/
|
|
|
|
while(node){
|
|
|
|
if(key>node->key32){
|
|
|
|
return node->data;
|
|
|
|
}
|
|
|
|
node=node->parent;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
2006-04-26 21:06:52 +00:00
|
|
|
/* right child */
|
2006-03-18 06:15:39 +00:00
|
|
|
|
|
|
|
if(node->key32<key){
|
2006-04-26 21:06:52 +00:00
|
|
|
/* if this is the right child and its key is smaller
|
2006-03-18 06:15:39 +00:00
|
|
|
* than the search key then this is the one we want.
|
|
|
|
*/
|
|
|
|
return node->data;
|
|
|
|
} else {
|
2006-04-26 21:06:52 +00:00
|
|
|
/* if this is the right child and its key is larger
|
|
|
|
* than the search key then our parent is the one we
|
2006-03-18 06:15:39 +00:00
|
|
|
* want.
|
|
|
|
*/
|
|
|
|
return node->parent->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-03-05 04:01:34 +00:00
|
|
|
|
2006-08-14 08:29:29 +00:00
|
|
|
static inline emem_tree_node_t *
|
|
|
|
emem_tree_parent(emem_tree_node_t *node)
|
2006-03-05 04:01:34 +00:00
|
|
|
{
|
2006-03-11 23:16:34 +00:00
|
|
|
return node->parent;
|
|
|
|
}
|
|
|
|
|
2006-08-14 08:29:29 +00:00
|
|
|
static inline emem_tree_node_t *
|
|
|
|
emem_tree_grandparent(emem_tree_node_t *node)
|
2006-03-11 23:16:34 +00:00
|
|
|
{
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_node_t *parent;
|
2006-03-11 23:16:34 +00:00
|
|
|
|
2006-08-12 22:43:12 +00:00
|
|
|
parent=emem_tree_parent(node);
|
2006-03-11 23:16:34 +00:00
|
|
|
if(parent){
|
|
|
|
return parent->parent;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-10-19 22:31:08 +00:00
|
|
|
|
2006-08-14 08:29:29 +00:00
|
|
|
static inline emem_tree_node_t *
|
|
|
|
emem_tree_uncle(emem_tree_node_t *node)
|
2006-03-11 23:16:34 +00:00
|
|
|
{
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_node_t *parent, *grandparent;
|
2006-03-11 23:16:34 +00:00
|
|
|
|
2006-08-12 22:43:12 +00:00
|
|
|
parent=emem_tree_parent(node);
|
2006-03-11 23:16:34 +00:00
|
|
|
if(!parent){
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-08-12 22:43:12 +00:00
|
|
|
grandparent=emem_tree_parent(parent);
|
2006-03-11 23:16:34 +00:00
|
|
|
if(!grandparent){
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-03-05 04:01:34 +00:00
|
|
|
if(parent==grandparent->left){
|
|
|
|
return grandparent->right;
|
|
|
|
}
|
|
|
|
return grandparent->left;
|
|
|
|
}
|
|
|
|
|
2006-08-14 08:29:29 +00:00
|
|
|
static inline void rb_insert_case1(emem_tree_t *se_tree, emem_tree_node_t *node);
|
|
|
|
static inline void rb_insert_case2(emem_tree_t *se_tree, emem_tree_node_t *node);
|
2006-03-05 04:01:34 +00:00
|
|
|
|
|
|
|
static inline void
|
2006-08-14 08:29:29 +00:00
|
|
|
rotate_left(emem_tree_t *se_tree, emem_tree_node_t *node)
|
2006-03-05 04:01:34 +00:00
|
|
|
{
|
|
|
|
if(node->parent){
|
|
|
|
if(node->parent->left==node){
|
|
|
|
node->parent->left=node->right;
|
|
|
|
} else {
|
|
|
|
node->parent->right=node->right;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
se_tree->tree=node->right;
|
|
|
|
}
|
|
|
|
node->right->parent=node->parent;
|
|
|
|
node->parent=node->right;
|
|
|
|
node->right=node->right->left;
|
|
|
|
if(node->right){
|
|
|
|
node->right->parent=node;
|
|
|
|
}
|
|
|
|
node->parent->left=node;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2006-08-14 08:29:29 +00:00
|
|
|
rotate_right(emem_tree_t *se_tree, emem_tree_node_t *node)
|
2006-03-05 04:01:34 +00:00
|
|
|
{
|
|
|
|
if(node->parent){
|
|
|
|
if(node->parent->left==node){
|
|
|
|
node->parent->left=node->left;
|
|
|
|
} else {
|
|
|
|
node->parent->right=node->left;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
se_tree->tree=node->left;
|
|
|
|
}
|
|
|
|
node->left->parent=node->parent;
|
|
|
|
node->parent=node->left;
|
|
|
|
node->left=node->left->right;
|
|
|
|
if(node->left){
|
|
|
|
node->left->parent=node;
|
|
|
|
}
|
|
|
|
node->parent->right=node;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2006-08-14 08:29:29 +00:00
|
|
|
rb_insert_case5(emem_tree_t *se_tree, emem_tree_node_t *node)
|
2006-03-05 04:01:34 +00:00
|
|
|
{
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_node_t *grandparent;
|
|
|
|
emem_tree_node_t *parent;
|
2006-03-11 23:16:34 +00:00
|
|
|
|
2006-08-12 22:43:12 +00:00
|
|
|
parent=emem_tree_parent(node);
|
|
|
|
grandparent=emem_tree_parent(parent);
|
2006-08-14 08:29:29 +00:00
|
|
|
parent->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
|
|
|
|
grandparent->u.rb_color=EMEM_TREE_RB_COLOR_RED;
|
2006-03-05 04:01:34 +00:00
|
|
|
if( (node==parent->left) && (parent==grandparent->left) ){
|
|
|
|
rotate_right(se_tree, grandparent);
|
|
|
|
} else {
|
|
|
|
rotate_left(se_tree, grandparent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2006-08-14 08:29:29 +00:00
|
|
|
rb_insert_case4(emem_tree_t *se_tree, emem_tree_node_t *node)
|
2006-03-05 04:01:34 +00:00
|
|
|
{
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_node_t *grandparent;
|
|
|
|
emem_tree_node_t *parent;
|
2006-03-11 23:16:34 +00:00
|
|
|
|
2006-08-12 22:43:12 +00:00
|
|
|
parent=emem_tree_parent(node);
|
|
|
|
grandparent=emem_tree_parent(parent);
|
2006-03-11 23:16:34 +00:00
|
|
|
if(!grandparent){
|
|
|
|
return;
|
|
|
|
}
|
2006-03-05 04:01:34 +00:00
|
|
|
if( (node==parent->right) && (parent==grandparent->left) ){
|
|
|
|
rotate_left(se_tree, parent);
|
|
|
|
node=node->left;
|
|
|
|
} else if( (node==parent->left) && (parent==grandparent->right) ){
|
|
|
|
rotate_right(se_tree, parent);
|
|
|
|
node=node->right;
|
|
|
|
}
|
2006-03-11 23:16:34 +00:00
|
|
|
rb_insert_case5(se_tree, node);
|
2006-03-05 04:01:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2006-08-14 08:29:29 +00:00
|
|
|
rb_insert_case3(emem_tree_t *se_tree, emem_tree_node_t *node)
|
2006-03-05 04:01:34 +00:00
|
|
|
{
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_node_t *grandparent;
|
|
|
|
emem_tree_node_t *parent;
|
|
|
|
emem_tree_node_t *uncle;
|
2006-03-07 16:31:47 +00:00
|
|
|
|
2006-08-12 22:43:12 +00:00
|
|
|
uncle=emem_tree_uncle(node);
|
2006-08-14 08:29:29 +00:00
|
|
|
if(uncle && (uncle->u.rb_color==EMEM_TREE_RB_COLOR_RED)){
|
2006-08-12 22:43:12 +00:00
|
|
|
parent=emem_tree_parent(node);
|
2006-08-14 08:29:29 +00:00
|
|
|
parent->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
|
|
|
|
uncle->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
|
2006-08-12 22:43:12 +00:00
|
|
|
grandparent=emem_tree_grandparent(node);
|
2006-08-14 08:29:29 +00:00
|
|
|
grandparent->u.rb_color=EMEM_TREE_RB_COLOR_RED;
|
2006-03-11 06:12:36 +00:00
|
|
|
rb_insert_case1(se_tree, grandparent);
|
2006-03-05 04:01:34 +00:00
|
|
|
} else {
|
2006-03-11 23:16:34 +00:00
|
|
|
rb_insert_case4(se_tree, node);
|
2006-03-05 04:01:34 +00:00
|
|
|
}
|
2006-03-07 16:31:47 +00:00
|
|
|
}
|
2006-03-05 04:01:34 +00:00
|
|
|
|
|
|
|
static inline void
|
2006-08-14 08:29:29 +00:00
|
|
|
rb_insert_case2(emem_tree_t *se_tree, emem_tree_node_t *node)
|
2006-03-05 04:01:34 +00:00
|
|
|
{
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_node_t *parent;
|
2006-03-11 23:16:34 +00:00
|
|
|
|
2006-08-12 22:43:12 +00:00
|
|
|
parent=emem_tree_parent(node);
|
2006-03-11 23:16:34 +00:00
|
|
|
/* parent is always non-NULL here */
|
2006-08-14 08:29:29 +00:00
|
|
|
if(parent->u.rb_color==EMEM_TREE_RB_COLOR_BLACK){
|
2006-03-05 04:01:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-03-11 23:16:34 +00:00
|
|
|
rb_insert_case3(se_tree, node);
|
2006-03-05 04:01:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2006-08-14 08:29:29 +00:00
|
|
|
rb_insert_case1(emem_tree_t *se_tree, emem_tree_node_t *node)
|
2006-03-05 04:01:34 +00:00
|
|
|
{
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_node_t *parent;
|
2006-03-05 04:01:34 +00:00
|
|
|
|
2006-08-12 22:43:12 +00:00
|
|
|
parent=emem_tree_parent(node);
|
2006-03-05 04:01:34 +00:00
|
|
|
if(!parent){
|
2006-08-14 08:29:29 +00:00
|
|
|
node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
|
2006-03-05 04:01:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-03-11 23:16:34 +00:00
|
|
|
rb_insert_case2(se_tree, node);
|
2006-03-05 04:01:34 +00:00
|
|
|
}
|
2006-03-07 16:31:47 +00:00
|
|
|
|
2006-03-05 04:01:34 +00:00
|
|
|
/* insert a new node in the tree. if this node matches an already existing node
|
|
|
|
* then just replace the data for that node */
|
2006-03-07 16:31:47 +00:00
|
|
|
void
|
2006-08-14 09:36:15 +00:00
|
|
|
emem_tree_insert32(emem_tree_t *se_tree, guint32 key, void *data)
|
2006-03-05 04:01:34 +00:00
|
|
|
{
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_node_t *node;
|
2006-03-05 04:01:34 +00:00
|
|
|
|
|
|
|
node=se_tree->tree;
|
|
|
|
|
|
|
|
/* is this the first node ?*/
|
|
|
|
if(!node){
|
2006-08-14 08:29:29 +00:00
|
|
|
node=se_tree->malloc(sizeof(emem_tree_node_t));
|
2006-03-05 04:01:34 +00:00
|
|
|
switch(se_tree->type){
|
2006-08-14 08:29:29 +00:00
|
|
|
case EMEM_TREE_TYPE_RED_BLACK:
|
|
|
|
node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
|
2006-03-05 04:01:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
node->parent=NULL;
|
|
|
|
node->left=NULL;
|
|
|
|
node->right=NULL;
|
|
|
|
node->key32=key;
|
|
|
|
node->data=data;
|
2007-12-04 01:44:05 +00:00
|
|
|
node->u.is_subtree = EMEM_TREE_NODE_IS_DATA;
|
2006-03-05 04:01:34 +00:00
|
|
|
se_tree->tree=node;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* it was not the new root so walk the tree until we find where to
|
|
|
|
* insert this new leaf.
|
|
|
|
*/
|
|
|
|
while(1){
|
|
|
|
/* this node already exists, so just replace the data pointer*/
|
|
|
|
if(key==node->key32){
|
|
|
|
node->data=data;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(key<node->key32) {
|
|
|
|
if(!node->left){
|
|
|
|
/* new node to the left */
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_node_t *new_node;
|
|
|
|
new_node=se_tree->malloc(sizeof(emem_tree_node_t));
|
2006-03-05 04:01:34 +00:00
|
|
|
node->left=new_node;
|
|
|
|
new_node->parent=node;
|
|
|
|
new_node->left=NULL;
|
|
|
|
new_node->right=NULL;
|
|
|
|
new_node->key32=key;
|
|
|
|
new_node->data=data;
|
2007-12-04 01:44:05 +00:00
|
|
|
new_node->u.is_subtree=EMEM_TREE_NODE_IS_DATA;
|
2006-03-05 04:01:34 +00:00
|
|
|
node=new_node;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node=node->left;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(key>node->key32) {
|
|
|
|
if(!node->right){
|
|
|
|
/* new node to the right */
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_node_t *new_node;
|
|
|
|
new_node=se_tree->malloc(sizeof(emem_tree_node_t));
|
2006-03-05 04:01:34 +00:00
|
|
|
node->right=new_node;
|
|
|
|
new_node->parent=node;
|
|
|
|
new_node->left=NULL;
|
|
|
|
new_node->right=NULL;
|
|
|
|
new_node->key32=key;
|
|
|
|
new_node->data=data;
|
2007-12-04 01:44:05 +00:00
|
|
|
new_node->u.is_subtree=EMEM_TREE_NODE_IS_DATA;
|
2006-03-05 04:01:34 +00:00
|
|
|
node=new_node;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node=node->right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* node will now point to the newly created node */
|
|
|
|
switch(se_tree->type){
|
2006-08-14 08:29:29 +00:00
|
|
|
case EMEM_TREE_TYPE_RED_BLACK:
|
|
|
|
node->u.rb_color=EMEM_TREE_RB_COLOR_RED;
|
2006-03-05 04:01:34 +00:00
|
|
|
rb_insert_case1(se_tree, node);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
static void *
|
|
|
|
lookup_or_insert32(emem_tree_t *se_tree, guint32 key, void*(*func)(void*),void* ud, int is_subtree)
|
|
|
|
{
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_node_t *node;
|
2006-04-26 21:06:52 +00:00
|
|
|
|
2006-03-10 21:58:49 +00:00
|
|
|
node=se_tree->tree;
|
2006-04-26 21:06:52 +00:00
|
|
|
|
2006-03-10 21:58:49 +00:00
|
|
|
/* is this the first node ?*/
|
|
|
|
if(!node){
|
2006-08-14 08:29:29 +00:00
|
|
|
node=se_tree->malloc(sizeof(emem_tree_node_t));
|
2006-03-10 21:58:49 +00:00
|
|
|
switch(se_tree->type){
|
2006-08-14 08:29:29 +00:00
|
|
|
case EMEM_TREE_TYPE_RED_BLACK:
|
|
|
|
node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
|
2006-03-10 21:58:49 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
node->parent=NULL;
|
|
|
|
node->left=NULL;
|
|
|
|
node->right=NULL;
|
|
|
|
node->key32=key;
|
|
|
|
node->data= func(ud);
|
2007-12-04 01:44:05 +00:00
|
|
|
node->u.is_subtree = is_subtree;
|
2006-03-10 21:58:49 +00:00
|
|
|
se_tree->tree=node;
|
|
|
|
return node->data;
|
|
|
|
}
|
2006-04-26 21:06:52 +00:00
|
|
|
|
2006-03-10 21:58:49 +00:00
|
|
|
/* it was not the new root so walk the tree until we find where to
|
|
|
|
* insert this new leaf.
|
|
|
|
*/
|
|
|
|
while(1){
|
|
|
|
/* this node already exists, so just return the data pointer*/
|
|
|
|
if(key==node->key32){
|
|
|
|
return node->data;
|
|
|
|
}
|
|
|
|
if(key<node->key32) {
|
|
|
|
if(!node->left){
|
|
|
|
/* new node to the left */
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_node_t *new_node;
|
|
|
|
new_node=se_tree->malloc(sizeof(emem_tree_node_t));
|
2006-03-10 21:58:49 +00:00
|
|
|
node->left=new_node;
|
|
|
|
new_node->parent=node;
|
|
|
|
new_node->left=NULL;
|
|
|
|
new_node->right=NULL;
|
|
|
|
new_node->key32=key;
|
|
|
|
new_node->data= func(ud);
|
2007-12-04 01:44:05 +00:00
|
|
|
new_node->u.is_subtree = is_subtree;
|
2006-03-10 21:58:49 +00:00
|
|
|
node=new_node;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node=node->left;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(key>node->key32) {
|
|
|
|
if(!node->right){
|
|
|
|
/* new node to the right */
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_node_t *new_node;
|
|
|
|
new_node=se_tree->malloc(sizeof(emem_tree_node_t));
|
2006-03-10 21:58:49 +00:00
|
|
|
node->right=new_node;
|
|
|
|
new_node->parent=node;
|
|
|
|
new_node->left=NULL;
|
|
|
|
new_node->right=NULL;
|
|
|
|
new_node->key32=key;
|
|
|
|
new_node->data= func(ud);
|
2007-12-04 01:44:05 +00:00
|
|
|
new_node->u.is_subtree = is_subtree;
|
2006-03-10 21:58:49 +00:00
|
|
|
node=new_node;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node=node->right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2006-04-26 21:06:52 +00:00
|
|
|
|
2006-03-10 21:58:49 +00:00
|
|
|
/* node will now point to the newly created node */
|
|
|
|
switch(se_tree->type){
|
2006-08-14 08:29:29 +00:00
|
|
|
case EMEM_TREE_TYPE_RED_BLACK:
|
|
|
|
node->u.rb_color=EMEM_TREE_RB_COLOR_RED;
|
2006-03-10 21:58:49 +00:00
|
|
|
rb_insert_case1(se_tree, node);
|
|
|
|
break;
|
|
|
|
}
|
2006-04-26 21:06:52 +00:00
|
|
|
|
2006-03-10 21:58:49 +00:00
|
|
|
return node->data;
|
|
|
|
}
|
2006-03-05 04:01:34 +00:00
|
|
|
|
2006-03-07 16:31:47 +00:00
|
|
|
/* When the se data is released, this entire tree will dissapear as if it
|
add helper to build (red/black) trees from a key that is a vector of guin32 arrays.
test this functionality by calling these vector insert/lookup tree functions from the nfs dissector for when filehandles are used as a key.
these vector functions could also be used to efficiently store conversations :
se_tree_key_t[6] = {
{ addr_len/4, &src_addr },
{ addr_len/4, &dst_addr },
{ 1, &src_port32 },
{ 1, &dst_port32 },
{ 1, &protocol32 },
{ 0, NULL }
}
(the nfs dissector needs a LOT of work. It is very painful to work with
very large nfs traces with all the memory it wastes (and eats) as well as how slow all the tables make it)
svn path=/trunk/; revision=17477
2006-03-06 10:25:19 +00:00
|
|
|
* never existed including all metadata associated with the tree.
|
|
|
|
*/
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_t *
|
2007-04-23 10:59:26 +00:00
|
|
|
se_tree_create_non_persistent(int type, const char *name)
|
add helper to build (red/black) trees from a key that is a vector of guin32 arrays.
test this functionality by calling these vector insert/lookup tree functions from the nfs dissector for when filehandles are used as a key.
these vector functions could also be used to efficiently store conversations :
se_tree_key_t[6] = {
{ addr_len/4, &src_addr },
{ addr_len/4, &dst_addr },
{ 1, &src_port32 },
{ 1, &dst_port32 },
{ 1, &protocol32 },
{ 0, NULL }
}
(the nfs dissector needs a LOT of work. It is very painful to work with
very large nfs traces with all the memory it wastes (and eats) as well as how slow all the tables make it)
svn path=/trunk/; revision=17477
2006-03-06 10:25:19 +00:00
|
|
|
{
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_t *tree_list;
|
add helper to build (red/black) trees from a key that is a vector of guin32 arrays.
test this functionality by calling these vector insert/lookup tree functions from the nfs dissector for when filehandles are used as a key.
these vector functions could also be used to efficiently store conversations :
se_tree_key_t[6] = {
{ addr_len/4, &src_addr },
{ addr_len/4, &dst_addr },
{ 1, &src_port32 },
{ 1, &dst_port32 },
{ 1, &protocol32 },
{ 0, NULL }
}
(the nfs dissector needs a LOT of work. It is very painful to work with
very large nfs traces with all the memory it wastes (and eats) as well as how slow all the tables make it)
svn path=/trunk/; revision=17477
2006-03-06 10:25:19 +00:00
|
|
|
|
2006-08-14 08:29:29 +00:00
|
|
|
tree_list=se_alloc(sizeof(emem_tree_t));
|
add helper to build (red/black) trees from a key that is a vector of guin32 arrays.
test this functionality by calling these vector insert/lookup tree functions from the nfs dissector for when filehandles are used as a key.
these vector functions could also be used to efficiently store conversations :
se_tree_key_t[6] = {
{ addr_len/4, &src_addr },
{ addr_len/4, &dst_addr },
{ 1, &src_port32 },
{ 1, &dst_port32 },
{ 1, &protocol32 },
{ 0, NULL }
}
(the nfs dissector needs a LOT of work. It is very painful to work with
very large nfs traces with all the memory it wastes (and eats) as well as how slow all the tables make it)
svn path=/trunk/; revision=17477
2006-03-06 10:25:19 +00:00
|
|
|
tree_list->next=NULL;
|
|
|
|
tree_list->type=type;
|
|
|
|
tree_list->tree=NULL;
|
2006-03-11 13:02:41 +00:00
|
|
|
tree_list->name=name;
|
2006-08-12 23:27:22 +00:00
|
|
|
tree_list->malloc=se_alloc;
|
add helper to build (red/black) trees from a key that is a vector of guin32 arrays.
test this functionality by calling these vector insert/lookup tree functions from the nfs dissector for when filehandles are used as a key.
these vector functions could also be used to efficiently store conversations :
se_tree_key_t[6] = {
{ addr_len/4, &src_addr },
{ addr_len/4, &dst_addr },
{ 1, &src_port32 },
{ 1, &dst_port32 },
{ 1, &protocol32 },
{ 0, NULL }
}
(the nfs dissector needs a LOT of work. It is very painful to work with
very large nfs traces with all the memory it wastes (and eats) as well as how slow all the tables make it)
svn path=/trunk/; revision=17477
2006-03-06 10:25:19 +00:00
|
|
|
|
|
|
|
return tree_list;
|
|
|
|
}
|
|
|
|
|
2006-08-17 08:17:48 +00:00
|
|
|
/* This tree is PErmanent and will never be released
|
|
|
|
*/
|
|
|
|
emem_tree_t *
|
2008-06-25 03:05:19 +00:00
|
|
|
pe_tree_create(int type, const char *name)
|
2006-08-17 08:17:48 +00:00
|
|
|
{
|
|
|
|
emem_tree_t *tree_list;
|
|
|
|
|
2009-09-20 09:03:02 +00:00
|
|
|
tree_list=g_new(emem_tree_t, 1);
|
2006-08-17 08:17:48 +00:00
|
|
|
tree_list->next=NULL;
|
|
|
|
tree_list->type=type;
|
|
|
|
tree_list->tree=NULL;
|
|
|
|
tree_list->name=name;
|
2006-11-15 23:48:56 +00:00
|
|
|
tree_list->malloc=(void *(*)(size_t)) g_malloc;
|
2006-08-17 08:17:48 +00:00
|
|
|
|
|
|
|
return tree_list;
|
|
|
|
}
|
|
|
|
|
2006-08-14 09:36:15 +00:00
|
|
|
/* create another (sub)tree using the same memory allocation scope
|
|
|
|
* as the parent tree.
|
|
|
|
*/
|
|
|
|
static emem_tree_t *
|
2008-06-25 03:05:19 +00:00
|
|
|
emem_tree_create_subtree(emem_tree_t *parent_tree, const char *name)
|
2006-08-14 09:36:15 +00:00
|
|
|
{
|
|
|
|
emem_tree_t *tree_list;
|
|
|
|
|
|
|
|
tree_list=parent_tree->malloc(sizeof(emem_tree_t));
|
|
|
|
tree_list->next=NULL;
|
|
|
|
tree_list->type=parent_tree->type;
|
|
|
|
tree_list->tree=NULL;
|
|
|
|
tree_list->name=name;
|
|
|
|
tree_list->malloc=parent_tree->malloc;
|
|
|
|
|
|
|
|
return tree_list;
|
|
|
|
}
|
|
|
|
|
2009-10-19 22:31:08 +00:00
|
|
|
static void *
|
|
|
|
create_sub_tree(void* d)
|
|
|
|
{
|
2006-08-14 08:29:29 +00:00
|
|
|
emem_tree_t *se_tree = d;
|
2006-08-14 09:36:15 +00:00
|
|
|
return emem_tree_create_subtree(se_tree, "subtree");
|
2006-03-10 21:58:49 +00:00
|
|
|
}
|
|
|
|
|
add helper to build (red/black) trees from a key that is a vector of guin32 arrays.
test this functionality by calling these vector insert/lookup tree functions from the nfs dissector for when filehandles are used as a key.
these vector functions could also be used to efficiently store conversations :
se_tree_key_t[6] = {
{ addr_len/4, &src_addr },
{ addr_len/4, &dst_addr },
{ 1, &src_port32 },
{ 1, &dst_port32 },
{ 1, &protocol32 },
{ 0, NULL }
}
(the nfs dissector needs a LOT of work. It is very painful to work with
very large nfs traces with all the memory it wastes (and eats) as well as how slow all the tables make it)
svn path=/trunk/; revision=17477
2006-03-06 10:25:19 +00:00
|
|
|
/* insert a new node in the tree. if this node matches an already existing node
|
|
|
|
* then just replace the data for that node */
|
|
|
|
|
2012-08-09 13:59:29 +00:00
|
|
|
void
|
|
|
|
emem_tree_insert32_array(emem_tree_t *se_tree, emem_tree_key_t *key, void *data)
|
|
|
|
{
|
2012-08-09 21:46:34 +00:00
|
|
|
emem_tree_t *insert_tree = NULL;
|
|
|
|
emem_tree_key_t *cur_key;
|
2012-08-09 22:45:43 +00:00
|
|
|
guint32 i, insert_key32 = 0;
|
2012-08-09 13:59:29 +00:00
|
|
|
|
2012-08-09 21:46:34 +00:00
|
|
|
if(!se_tree || !key) return;
|
2012-08-09 13:59:29 +00:00
|
|
|
|
2012-08-09 21:46:34 +00:00
|
|
|
for (cur_key = key; cur_key->length > 0; cur_key++) {
|
|
|
|
if(cur_key->length > 100) {
|
|
|
|
DISSECTOR_ASSERT_NOT_REACHED();
|
|
|
|
}
|
2012-08-09 13:59:29 +00:00
|
|
|
|
2012-08-09 21:46:34 +00:00
|
|
|
for (i = 0; i < cur_key->length; i++) {
|
|
|
|
/* Insert using the previous key32 */
|
|
|
|
if (!insert_tree) {
|
|
|
|
insert_tree = se_tree;
|
|
|
|
} else {
|
|
|
|
insert_tree = lookup_or_insert32(insert_tree, insert_key32, create_sub_tree, se_tree, EMEM_TREE_NODE_IS_SUBTREE);
|
|
|
|
}
|
|
|
|
insert_key32 = cur_key->key[i];
|
|
|
|
}
|
2012-08-09 13:59:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-09 21:46:34 +00:00
|
|
|
if(!insert_tree) {
|
|
|
|
/* We didn't get a valid key. Should we return NULL instead? */
|
2009-04-14 14:08:19 +00:00
|
|
|
DISSECTOR_ASSERT_NOT_REACHED();
|
add helper to build (red/black) trees from a key that is a vector of guin32 arrays.
test this functionality by calling these vector insert/lookup tree functions from the nfs dissector for when filehandles are used as a key.
these vector functions could also be used to efficiently store conversations :
se_tree_key_t[6] = {
{ addr_len/4, &src_addr },
{ addr_len/4, &dst_addr },
{ 1, &src_port32 },
{ 1, &dst_port32 },
{ 1, &protocol32 },
{ 0, NULL }
}
(the nfs dissector needs a LOT of work. It is very painful to work with
very large nfs traces with all the memory it wastes (and eats) as well as how slow all the tables make it)
svn path=/trunk/; revision=17477
2006-03-06 10:25:19 +00:00
|
|
|
}
|
2012-08-09 21:46:34 +00:00
|
|
|
|
|
|
|
emem_tree_insert32(insert_tree, insert_key32, data);
|
|
|
|
|
add helper to build (red/black) trees from a key that is a vector of guin32 arrays.
test this functionality by calling these vector insert/lookup tree functions from the nfs dissector for when filehandles are used as a key.
these vector functions could also be used to efficiently store conversations :
se_tree_key_t[6] = {
{ addr_len/4, &src_addr },
{ addr_len/4, &dst_addr },
{ 1, &src_port32 },
{ 1, &dst_port32 },
{ 1, &protocol32 },
{ 0, NULL }
}
(the nfs dissector needs a LOT of work. It is very painful to work with
very large nfs traces with all the memory it wastes (and eats) as well as how slow all the tables make it)
svn path=/trunk/; revision=17477
2006-03-06 10:25:19 +00:00
|
|
|
}
|
2006-03-07 01:15:29 +00:00
|
|
|
|
2010-02-07 11:01:13 +00:00
|
|
|
void *
|
2012-08-09 13:59:29 +00:00
|
|
|
emem_tree_lookup32_array(emem_tree_t *se_tree, emem_tree_key_t *key)
|
|
|
|
{
|
2012-08-09 21:46:34 +00:00
|
|
|
emem_tree_t *lookup_tree = NULL;
|
|
|
|
emem_tree_key_t *cur_key;
|
2012-08-09 22:45:43 +00:00
|
|
|
guint32 i, lookup_key32 = 0;
|
2012-08-09 13:59:29 +00:00
|
|
|
|
|
|
|
if(!se_tree || !key) return NULL; /* prevent searching on NULL pointer */
|
|
|
|
|
2012-08-09 21:46:34 +00:00
|
|
|
for (cur_key = key; cur_key->length > 0; cur_key++) {
|
|
|
|
if(cur_key->length > 100) {
|
|
|
|
DISSECTOR_ASSERT_NOT_REACHED();
|
|
|
|
}
|
2012-08-09 13:59:29 +00:00
|
|
|
|
2012-08-09 21:46:34 +00:00
|
|
|
for (i = 0; i < cur_key->length; i++) {
|
|
|
|
/* Lookup using the previous key32 */
|
|
|
|
if (!lookup_tree) {
|
|
|
|
lookup_tree = se_tree;
|
|
|
|
} else {
|
|
|
|
lookup_tree = emem_tree_lookup32(lookup_tree, lookup_key32);
|
|
|
|
if (!lookup_tree) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lookup_key32 = cur_key->key[i];
|
|
|
|
}
|
2012-08-09 13:59:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-09 21:46:34 +00:00
|
|
|
if(!lookup_tree) {
|
|
|
|
/* We didn't get a valid key. Should we return NULL instead? */
|
2010-02-07 11:01:13 +00:00
|
|
|
DISSECTOR_ASSERT_NOT_REACHED();
|
|
|
|
}
|
2012-08-09 21:46:34 +00:00
|
|
|
|
|
|
|
return emem_tree_lookup32(lookup_tree, lookup_key32);
|
2012-08-09 13:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
emem_tree_lookup32_array_le(emem_tree_t *se_tree, emem_tree_key_t *key)
|
|
|
|
{
|
2012-08-09 21:46:34 +00:00
|
|
|
emem_tree_t *lookup_tree = NULL;
|
|
|
|
emem_tree_key_t *cur_key;
|
2012-08-09 22:45:43 +00:00
|
|
|
guint32 i, lookup_key32 = 0;
|
2012-08-09 13:59:29 +00:00
|
|
|
|
|
|
|
if(!se_tree || !key) return NULL; /* prevent searching on NULL pointer */
|
|
|
|
|
2012-08-09 21:46:34 +00:00
|
|
|
for (cur_key = key; cur_key->length > 0; cur_key++) {
|
|
|
|
if(cur_key->length > 100) {
|
|
|
|
DISSECTOR_ASSERT_NOT_REACHED();
|
|
|
|
}
|
2012-08-09 13:59:29 +00:00
|
|
|
|
2012-08-09 21:46:34 +00:00
|
|
|
for (i = 0; i < cur_key->length; i++) {
|
|
|
|
/* Lookup using the previous key32 */
|
|
|
|
if (!lookup_tree) {
|
|
|
|
lookup_tree = se_tree;
|
|
|
|
} else {
|
|
|
|
lookup_tree = emem_tree_lookup32_le(lookup_tree, lookup_key32);
|
|
|
|
if (!lookup_tree) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lookup_key32 = cur_key->key[i];
|
|
|
|
}
|
2012-08-09 13:59:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-09 21:46:34 +00:00
|
|
|
if(!lookup_tree) {
|
|
|
|
/* We didn't get a valid key. Should we return NULL instead? */
|
|
|
|
DISSECTOR_ASSERT_NOT_REACHED();
|
2012-08-09 13:59:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-09 21:46:34 +00:00
|
|
|
return emem_tree_lookup32_le(lookup_tree, lookup_key32);
|
2012-08-09 13:59:29 +00:00
|
|
|
|
2010-02-07 11:01:13 +00:00
|
|
|
}
|
2006-03-07 06:33:47 +00:00
|
|
|
|
2007-12-04 03:26:50 +00:00
|
|
|
/* Strings are stored as an array of uint32 containing the string characters
|
2008-05-22 15:46:27 +00:00
|
|
|
with 4 characters in each uint32.
|
2007-12-04 03:26:50 +00:00
|
|
|
The first byte of the string is stored as the most significant byte.
|
|
|
|
If the string is not a multiple of 4 characters in length the last
|
|
|
|
uint32 containing the string bytes are padded with 0 bytes.
|
|
|
|
After the uint32's containing the string, there is one final terminator
|
|
|
|
uint32 with the value 0x00000001
|
|
|
|
*/
|
2007-01-25 06:51:30 +00:00
|
|
|
void
|
2007-12-04 03:26:50 +00:00
|
|
|
emem_tree_insert_string(emem_tree_t* se_tree, const gchar* k, void* v, guint32 flags)
|
|
|
|
{
|
|
|
|
emem_tree_key_t key[2];
|
|
|
|
guint32 *aligned=NULL;
|
2009-04-07 16:36:52 +00:00
|
|
|
guint32 len = (guint32) strlen(k);
|
2010-01-28 18:45:46 +00:00
|
|
|
guint32 divx = (len+3)/4+1;
|
2007-12-04 03:26:50 +00:00
|
|
|
guint32 i;
|
|
|
|
guint32 tmp;
|
2006-03-07 16:31:47 +00:00
|
|
|
|
2010-01-28 18:45:46 +00:00
|
|
|
aligned = g_malloc(divx * sizeof (guint32));
|
2006-03-07 16:31:47 +00:00
|
|
|
|
2007-12-04 03:26:50 +00:00
|
|
|
/* pack the bytes one one by one into guint32s */
|
|
|
|
tmp = 0;
|
|
|
|
for (i = 0;i < len;i++) {
|
|
|
|
unsigned char ch;
|
|
|
|
|
|
|
|
ch = (unsigned char)k[i];
|
|
|
|
if (flags & EMEM_TREE_STRING_NOCASE) {
|
|
|
|
if(isupper(ch)) {
|
|
|
|
ch = tolower(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmp <<= 8;
|
|
|
|
tmp |= ch;
|
|
|
|
if (i%4 == 3) {
|
|
|
|
aligned[i/4] = tmp;
|
|
|
|
tmp = 0;
|
2008-05-22 15:46:27 +00:00
|
|
|
}
|
2007-12-04 03:26:50 +00:00
|
|
|
}
|
|
|
|
/* add required padding to the last uint32 */
|
|
|
|
if (i%4 != 0) {
|
|
|
|
while (i%4 != 0) {
|
|
|
|
i++;
|
|
|
|
tmp <<= 8;
|
|
|
|
}
|
|
|
|
aligned[i/4-1] = tmp;
|
2006-03-07 01:15:29 +00:00
|
|
|
}
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2007-12-04 03:26:50 +00:00
|
|
|
/* add the terminator */
|
2010-01-28 18:45:46 +00:00
|
|
|
aligned[divx-1] = 0x00000001;
|
2007-12-04 03:26:50 +00:00
|
|
|
|
2010-01-28 18:45:46 +00:00
|
|
|
key[0].length = divx;
|
2007-12-04 03:26:50 +00:00
|
|
|
key[0].key = aligned;
|
|
|
|
key[1].length = 0;
|
|
|
|
key[1].key = NULL;
|
|
|
|
|
2006-03-07 16:31:47 +00:00
|
|
|
|
2007-12-04 03:26:50 +00:00
|
|
|
emem_tree_insert32_array(se_tree, key, v);
|
2009-10-05 19:18:09 +00:00
|
|
|
g_free(aligned);
|
2006-03-07 01:15:29 +00:00
|
|
|
}
|
|
|
|
|
2006-08-14 09:36:15 +00:00
|
|
|
void *
|
2007-12-04 03:26:50 +00:00
|
|
|
emem_tree_lookup_string(emem_tree_t* se_tree, const gchar* k, guint32 flags)
|
|
|
|
{
|
|
|
|
emem_tree_key_t key[2];
|
|
|
|
guint32 *aligned=NULL;
|
2009-04-07 16:36:52 +00:00
|
|
|
guint32 len = (guint) strlen(k);
|
2010-01-28 18:45:46 +00:00
|
|
|
guint32 divx = (len+3)/4+1;
|
2007-12-04 03:26:50 +00:00
|
|
|
guint32 i;
|
|
|
|
guint32 tmp;
|
2007-04-28 21:09:20 +00:00
|
|
|
void *ret;
|
2006-03-07 16:31:47 +00:00
|
|
|
|
2010-01-28 18:45:46 +00:00
|
|
|
aligned = g_malloc(divx * sizeof (guint32));
|
2006-03-07 16:31:47 +00:00
|
|
|
|
2007-12-04 03:26:50 +00:00
|
|
|
/* pack the bytes one one by one into guint32s */
|
|
|
|
tmp = 0;
|
|
|
|
for (i = 0;i < len;i++) {
|
|
|
|
unsigned char ch;
|
|
|
|
|
|
|
|
ch = (unsigned char)k[i];
|
|
|
|
if (flags & EMEM_TREE_STRING_NOCASE) {
|
|
|
|
if(isupper(ch)) {
|
|
|
|
ch = tolower(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmp <<= 8;
|
|
|
|
tmp |= ch;
|
|
|
|
if (i%4 == 3) {
|
|
|
|
aligned[i/4] = tmp;
|
|
|
|
tmp = 0;
|
2008-05-22 15:46:27 +00:00
|
|
|
}
|
2007-12-04 03:26:50 +00:00
|
|
|
}
|
|
|
|
/* add required padding to the last uint32 */
|
|
|
|
if (i%4 != 0) {
|
|
|
|
while (i%4 != 0) {
|
|
|
|
i++;
|
|
|
|
tmp <<= 8;
|
|
|
|
}
|
|
|
|
aligned[i/4-1] = tmp;
|
2006-03-07 01:15:29 +00:00
|
|
|
}
|
2008-05-22 15:46:27 +00:00
|
|
|
|
2007-12-04 03:26:50 +00:00
|
|
|
/* add the terminator */
|
2010-01-28 18:45:46 +00:00
|
|
|
aligned[divx-1] = 0x00000001;
|
2007-12-04 03:26:50 +00:00
|
|
|
|
2010-01-28 18:45:46 +00:00
|
|
|
key[0].length = divx;
|
2007-12-04 03:26:50 +00:00
|
|
|
key[0].key = aligned;
|
|
|
|
key[1].length = 0;
|
|
|
|
key[1].key = NULL;
|
|
|
|
|
2006-03-07 16:31:47 +00:00
|
|
|
|
2007-04-28 21:09:20 +00:00
|
|
|
ret = emem_tree_lookup32_array(se_tree, key);
|
2009-10-05 19:18:09 +00:00
|
|
|
g_free(aligned);
|
2007-04-28 21:09:20 +00:00
|
|
|
return ret;
|
2006-03-07 01:15:29 +00:00
|
|
|
}
|
2006-11-11 22:39:25 +00:00
|
|
|
|
2007-12-03 22:47:23 +00:00
|
|
|
static gboolean
|
|
|
|
emem_tree_foreach_nodes(emem_tree_node_t* node, tree_foreach_func callback, void *user_data)
|
|
|
|
{
|
2007-12-04 01:44:05 +00:00
|
|
|
gboolean stop_traverse = FALSE;
|
2007-12-03 22:47:23 +00:00
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if(node->left) {
|
|
|
|
stop_traverse = emem_tree_foreach_nodes(node->left, callback, user_data);
|
|
|
|
if (stop_traverse) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-04 01:44:05 +00:00
|
|
|
if (node->u.is_subtree == EMEM_TREE_NODE_IS_SUBTREE) {
|
|
|
|
stop_traverse = emem_tree_foreach(node->data, callback, user_data);
|
|
|
|
} else {
|
|
|
|
stop_traverse = callback(node->data, user_data);
|
|
|
|
}
|
|
|
|
|
2007-12-03 22:47:23 +00:00
|
|
|
if (stop_traverse) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(node->right) {
|
2007-12-04 01:44:05 +00:00
|
|
|
stop_traverse = emem_tree_foreach_nodes(node->right, callback, user_data);
|
2007-12-03 22:47:23 +00:00
|
|
|
if (stop_traverse) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-12-04 01:44:05 +00:00
|
|
|
gboolean
|
2007-12-03 22:47:23 +00:00
|
|
|
emem_tree_foreach(emem_tree_t* emem_tree, tree_foreach_func callback, void *user_data)
|
|
|
|
{
|
|
|
|
if (!emem_tree)
|
2007-12-04 01:44:05 +00:00
|
|
|
return FALSE;
|
2007-12-03 22:47:23 +00:00
|
|
|
|
2007-12-04 01:44:05 +00:00
|
|
|
if(!emem_tree->tree)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return emem_tree_foreach_nodes(emem_tree->tree, callback, user_data);
|
2007-12-03 22:47:23 +00:00
|
|
|
}
|
|
|
|
|
2012-10-06 14:35:59 +00:00
|
|
|
static void emem_print_subtree(emem_tree_t* emem_tree, guint32 level);
|
2006-11-11 22:39:25 +00:00
|
|
|
|
|
|
|
static void
|
2012-10-06 14:35:59 +00:00
|
|
|
emem_tree_print_nodes(const char *prefix, emem_tree_node_t* node, guint32 level)
|
2006-11-11 22:39:25 +00:00
|
|
|
{
|
2012-10-06 14:35:59 +00:00
|
|
|
guint32 i;
|
2007-01-25 06:51:30 +00:00
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return;
|
|
|
|
|
2006-11-11 22:39:25 +00:00
|
|
|
for(i=0;i<level;i++){
|
|
|
|
printf(" ");
|
|
|
|
}
|
2007-01-25 06:51:30 +00:00
|
|
|
|
2012-10-06 14:35:59 +00:00
|
|
|
printf("%sNODE:%p parent:%p left:%p right:%p colour:%s key:%u %s:%p\n", prefix,
|
2008-02-11 18:43:15 +00:00
|
|
|
(void *)node,(void *)(node->parent),(void *)(node->left),(void *)(node->right),
|
2012-10-06 14:35:59 +00:00
|
|
|
(node->u.rb_color)?"Black":"Red",(node->key32),(node->u.is_subtree)?"tree":"data",node->data);
|
2006-11-11 22:39:25 +00:00
|
|
|
if(node->left)
|
2012-10-06 14:35:59 +00:00
|
|
|
emem_tree_print_nodes("L-", node->left, level+1);
|
2006-11-11 22:39:25 +00:00
|
|
|
if(node->right)
|
2012-10-06 14:35:59 +00:00
|
|
|
emem_tree_print_nodes("R-", node->right, level+1);
|
|
|
|
|
|
|
|
if (node->u.is_subtree)
|
|
|
|
emem_print_subtree(node->data, level+1);
|
2006-11-11 22:39:25 +00:00
|
|
|
}
|
2012-10-06 14:35:59 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
emem_print_subtree(emem_tree_t* emem_tree, guint32 level)
|
2006-11-11 22:39:25 +00:00
|
|
|
{
|
2012-10-06 14:35:59 +00:00
|
|
|
guint32 i;
|
2012-10-08 17:52:42 +00:00
|
|
|
|
2007-01-25 06:51:30 +00:00
|
|
|
if (!emem_tree)
|
|
|
|
return;
|
|
|
|
|
2012-10-06 14:35:59 +00:00
|
|
|
for(i=0;i<level;i++){
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("EMEM tree:%p type:%s name:%s root:%p\n",emem_tree,(emem_tree->type==1)?"RedBlack":"unknown",emem_tree->name,(void *)(emem_tree->tree));
|
2006-11-11 22:39:25 +00:00
|
|
|
if(emem_tree->tree)
|
2012-10-06 14:35:59 +00:00
|
|
|
emem_tree_print_nodes("Root-", emem_tree->tree, level);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
emem_print_tree(emem_tree_t* emem_tree)
|
|
|
|
{
|
|
|
|
emem_print_subtree(emem_tree, 0);
|
2006-11-11 22:39:25 +00:00
|
|
|
}
|
2009-03-27 23:05:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* String buffers
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Presumably we're using these routines for building strings for the tree.
|
|
|
|
* Use ITEM_LABEL_LENGTH as the basis for our default lengths.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DEFAULT_STRBUF_LEN (ITEM_LABEL_LENGTH / 10)
|
|
|
|
#define MAX_STRBUF_LEN 65536
|
|
|
|
|
|
|
|
static gsize
|
2009-10-19 22:31:08 +00:00
|
|
|
next_size(gsize cur_alloc_len, gsize wanted_alloc_len, gsize max_alloc_len)
|
|
|
|
{
|
2009-04-14 14:08:19 +00:00
|
|
|
if (max_alloc_len < 1 || max_alloc_len > MAX_STRBUF_LEN) {
|
|
|
|
max_alloc_len = MAX_STRBUF_LEN;
|
2009-03-27 23:05:37 +00:00
|
|
|
}
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
if (cur_alloc_len < 1) {
|
|
|
|
cur_alloc_len = DEFAULT_STRBUF_LEN;
|
2009-03-27 23:05:37 +00:00
|
|
|
}
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
while (cur_alloc_len < wanted_alloc_len) {
|
|
|
|
cur_alloc_len *= 2;
|
2009-03-27 23:05:37 +00:00
|
|
|
}
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
return cur_alloc_len < max_alloc_len ? cur_alloc_len : max_alloc_len;
|
2009-03-27 23:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-10-19 22:31:08 +00:00
|
|
|
ep_strbuf_grow(emem_strbuf_t *strbuf, gsize wanted_alloc_len)
|
|
|
|
{
|
2009-03-27 23:05:37 +00:00
|
|
|
gsize new_alloc_len;
|
|
|
|
gchar *new_str;
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
if (!strbuf || (wanted_alloc_len <= strbuf->alloc_len) || (strbuf->alloc_len >= strbuf->max_alloc_len)) {
|
2009-03-27 23:05:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
new_alloc_len = next_size(strbuf->alloc_len, wanted_alloc_len, strbuf->max_alloc_len);
|
|
|
|
new_str = ep_alloc(new_alloc_len);
|
|
|
|
g_strlcpy(new_str, strbuf->str, new_alloc_len);
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
strbuf->alloc_len = new_alloc_len;
|
|
|
|
strbuf->str = new_str;
|
2009-03-27 23:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emem_strbuf_t *
|
2009-10-19 22:31:08 +00:00
|
|
|
ep_strbuf_sized_new(gsize alloc_len, gsize max_alloc_len)
|
|
|
|
{
|
2009-03-27 23:05:37 +00:00
|
|
|
emem_strbuf_t *strbuf;
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2009-03-27 23:05:37 +00:00
|
|
|
strbuf = ep_alloc(sizeof(emem_strbuf_t));
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
if ((max_alloc_len == 0) || (max_alloc_len > MAX_STRBUF_LEN))
|
|
|
|
max_alloc_len = MAX_STRBUF_LEN;
|
|
|
|
if (alloc_len == 0)
|
|
|
|
alloc_len = 1;
|
|
|
|
else if (alloc_len > max_alloc_len)
|
|
|
|
alloc_len = max_alloc_len;
|
|
|
|
|
|
|
|
strbuf->str = ep_alloc(alloc_len);
|
|
|
|
strbuf->str[0] = '\0';
|
|
|
|
|
|
|
|
strbuf->len = 0;
|
|
|
|
strbuf->alloc_len = alloc_len;
|
|
|
|
strbuf->max_alloc_len = max_alloc_len;
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2009-03-27 23:05:37 +00:00
|
|
|
return strbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
emem_strbuf_t *
|
2009-10-19 22:31:08 +00:00
|
|
|
ep_strbuf_new(const gchar *init)
|
|
|
|
{
|
2009-03-27 23:05:37 +00:00
|
|
|
emem_strbuf_t *strbuf;
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2010-12-06 17:30:08 +00:00
|
|
|
strbuf = ep_strbuf_sized_new(next_size(0, init?strlen(init)+1:0, 0), 0); /* +1 for NULL terminator */
|
2009-04-14 14:08:19 +00:00
|
|
|
if (init) {
|
|
|
|
gsize full_len;
|
|
|
|
full_len = g_strlcpy(strbuf->str, init, strbuf->alloc_len);
|
|
|
|
strbuf->len = MIN(full_len, strbuf->alloc_len-1);
|
|
|
|
}
|
|
|
|
|
2009-03-27 23:05:37 +00:00
|
|
|
return strbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
emem_strbuf_t *
|
2009-10-19 22:31:08 +00:00
|
|
|
ep_strbuf_new_label(const gchar *init)
|
|
|
|
{
|
2009-03-27 23:05:37 +00:00
|
|
|
emem_strbuf_t *strbuf;
|
2009-04-14 14:08:19 +00:00
|
|
|
gsize full_len;
|
|
|
|
|
|
|
|
/* Be optimistic: Allocate default size strbuf string and only */
|
|
|
|
/* request an increase if needed. */
|
|
|
|
/* XXX: Is it reasonable to assume that much of the usage of */
|
|
|
|
/* ep_strbuf_new_label will have init==NULL or */
|
|
|
|
/* strlen(init) < DEFAULT_STRBUF_LEN) ??? */
|
|
|
|
strbuf = ep_strbuf_sized_new(DEFAULT_STRBUF_LEN, ITEM_LABEL_LENGTH);
|
|
|
|
|
|
|
|
if (!init)
|
|
|
|
return strbuf;
|
|
|
|
|
|
|
|
/* full_len does not count the trailing '\0'. */
|
|
|
|
full_len = g_strlcpy(strbuf->str, init, strbuf->alloc_len);
|
|
|
|
if (full_len < strbuf->alloc_len) {
|
|
|
|
strbuf->len += full_len;
|
|
|
|
} else {
|
|
|
|
strbuf = ep_strbuf_sized_new(full_len+1, ITEM_LABEL_LENGTH);
|
|
|
|
full_len = g_strlcpy(strbuf->str, init, strbuf->alloc_len);
|
|
|
|
strbuf->len = MIN(full_len, strbuf->alloc_len-1);
|
2009-03-27 23:05:37 +00:00
|
|
|
}
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2009-03-27 23:05:37 +00:00
|
|
|
return strbuf;
|
|
|
|
}
|
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
emem_strbuf_t *
|
2009-10-19 22:31:08 +00:00
|
|
|
ep_strbuf_append(emem_strbuf_t *strbuf, const gchar *str)
|
|
|
|
{
|
2009-04-14 14:08:19 +00:00
|
|
|
gsize add_len, full_len;
|
2009-03-30 19:38:47 +00:00
|
|
|
|
|
|
|
if (!strbuf || !str || str[0] == '\0') {
|
2009-04-14 14:08:19 +00:00
|
|
|
return strbuf;
|
2009-03-27 23:05:37 +00:00
|
|
|
}
|
2009-03-28 05:14:16 +00:00
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
/* Be optimistic; try the g_strlcpy first & see if enough room. */
|
2009-09-06 08:10:12 +00:00
|
|
|
/* Note: full_len doesn't count the trailing '\0'; add_len does allow for same */
|
2009-04-14 14:08:19 +00:00
|
|
|
add_len = strbuf->alloc_len - strbuf->len;
|
|
|
|
full_len = g_strlcpy(&strbuf->str[strbuf->len], str, add_len);
|
|
|
|
if (full_len < add_len) {
|
|
|
|
strbuf->len += full_len;
|
|
|
|
} else {
|
|
|
|
strbuf->str[strbuf->len] = '\0'; /* end string at original length again */
|
|
|
|
ep_strbuf_grow(strbuf, strbuf->len + full_len + 1);
|
|
|
|
add_len = strbuf->alloc_len - strbuf->len;
|
|
|
|
full_len = g_strlcpy(&strbuf->str[strbuf->len], str, add_len);
|
|
|
|
strbuf->len += MIN(add_len-1, full_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return strbuf;
|
2009-03-27 23:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-10-19 22:31:08 +00:00
|
|
|
ep_strbuf_append_vprintf(emem_strbuf_t *strbuf, const gchar *format, va_list ap)
|
|
|
|
{
|
2009-03-27 23:05:37 +00:00
|
|
|
va_list ap2;
|
|
|
|
gsize add_len, full_len;
|
|
|
|
|
|
|
|
G_VA_COPY(ap2, ap);
|
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
/* Be optimistic; try the g_vsnprintf first & see if enough room. */
|
2009-09-06 08:10:12 +00:00
|
|
|
/* Note: full_len doesn't count the trailing '\0'; add_len does allow for same. */
|
2009-04-14 14:08:19 +00:00
|
|
|
add_len = strbuf->alloc_len - strbuf->len;
|
|
|
|
full_len = g_vsnprintf(&strbuf->str[strbuf->len], (gulong) add_len, format, ap);
|
|
|
|
if (full_len < add_len) {
|
|
|
|
strbuf->len += full_len;
|
|
|
|
} else {
|
|
|
|
strbuf->str[strbuf->len] = '\0'; /* end string at original length again */
|
|
|
|
ep_strbuf_grow(strbuf, strbuf->len + full_len + 1);
|
2009-03-27 23:05:37 +00:00
|
|
|
add_len = strbuf->alloc_len - strbuf->len;
|
2009-04-14 14:08:19 +00:00
|
|
|
full_len = g_vsnprintf(&strbuf->str[strbuf->len], (gulong) add_len, format, ap2);
|
|
|
|
strbuf->len += MIN(add_len-1, full_len);
|
2009-03-27 23:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
va_end(ap2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-10-19 22:31:08 +00:00
|
|
|
ep_strbuf_append_printf(emem_strbuf_t *strbuf, const gchar *format, ...)
|
|
|
|
{
|
2009-03-27 23:05:37 +00:00
|
|
|
va_list ap;
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2009-03-27 23:05:37 +00:00
|
|
|
va_start(ap, format);
|
|
|
|
ep_strbuf_append_vprintf(strbuf, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2009-03-28 00:27:03 +00:00
|
|
|
void
|
2009-10-19 22:31:08 +00:00
|
|
|
ep_strbuf_printf(emem_strbuf_t *strbuf, const gchar *format, ...)
|
|
|
|
{
|
2009-03-28 00:27:03 +00:00
|
|
|
va_list ap;
|
|
|
|
if (!strbuf) {
|
|
|
|
return;
|
|
|
|
}
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2009-03-28 00:27:03 +00:00
|
|
|
strbuf->len = 0;
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2009-03-28 00:27:03 +00:00
|
|
|
va_start(ap, format);
|
|
|
|
ep_strbuf_append_vprintf(strbuf, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
emem_strbuf_t *
|
2009-10-19 22:31:08 +00:00
|
|
|
ep_strbuf_append_c(emem_strbuf_t *strbuf, const gchar c)
|
|
|
|
{
|
2009-03-28 00:27:03 +00:00
|
|
|
if (!strbuf) {
|
2009-04-14 14:08:19 +00:00
|
|
|
return strbuf;
|
2009-03-28 00:27:03 +00:00
|
|
|
}
|
2009-03-30 19:38:47 +00:00
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
/* +1 for the new character & +1 for the trailing '\0'. */
|
|
|
|
if (strbuf->alloc_len < strbuf->len + 1 + 1) {
|
|
|
|
ep_strbuf_grow(strbuf, strbuf->len + 1 + 1);
|
|
|
|
}
|
|
|
|
if (strbuf->alloc_len >= strbuf->len + 1 + 1) {
|
2009-03-28 00:27:03 +00:00
|
|
|
strbuf->str[strbuf->len] = c;
|
|
|
|
strbuf->len++;
|
|
|
|
strbuf->str[strbuf->len] = '\0';
|
|
|
|
}
|
2009-04-14 14:08:19 +00:00
|
|
|
|
|
|
|
return strbuf;
|
2009-03-28 00:27:03 +00:00
|
|
|
}
|
|
|
|
|
2012-08-18 19:20:31 +00:00
|
|
|
emem_strbuf_t *
|
|
|
|
ep_strbuf_append_unichar(emem_strbuf_t *strbuf, const gunichar c)
|
|
|
|
{
|
|
|
|
gchar buf[6];
|
|
|
|
gint charlen;
|
|
|
|
|
|
|
|
if (!strbuf) {
|
|
|
|
return strbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
charlen = g_unichar_to_utf8(c, buf);
|
|
|
|
|
|
|
|
/* +charlen for the new character & +1 for the trailing '\0'. */
|
|
|
|
if (strbuf->alloc_len < strbuf->len + charlen + 1) {
|
|
|
|
ep_strbuf_grow(strbuf, strbuf->len + charlen + 1);
|
|
|
|
}
|
|
|
|
if (strbuf->alloc_len >= strbuf->len + charlen + 1) {
|
|
|
|
memcpy(&strbuf->str[strbuf->len], buf, charlen);
|
|
|
|
strbuf->len += charlen;
|
|
|
|
strbuf->str[strbuf->len] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return strbuf;
|
|
|
|
}
|
|
|
|
|
2009-04-14 14:08:19 +00:00
|
|
|
emem_strbuf_t *
|
2009-10-19 22:31:08 +00:00
|
|
|
ep_strbuf_truncate(emem_strbuf_t *strbuf, gsize len)
|
|
|
|
{
|
2009-03-27 23:05:37 +00:00
|
|
|
if (!strbuf || len >= strbuf->len) {
|
2009-04-14 14:08:19 +00:00
|
|
|
return strbuf;
|
2009-03-27 23:05:37 +00:00
|
|
|
}
|
|
|
|
|
2009-03-30 19:38:47 +00:00
|
|
|
strbuf->str[len] = '\0';
|
2009-03-27 23:05:37 +00:00
|
|
|
strbuf->len = len;
|
2009-04-14 14:08:19 +00:00
|
|
|
|
|
|
|
return strbuf;
|
2009-03-27 23:05:37 +00:00
|
|
|
}
|
2009-04-14 14:08:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Editor modelines
|
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*
|
2011-09-21 17:49:11 +00:00
|
|
|
* ex: set shiftwidth=8 tabstop=8 noexpandtab:
|
2009-04-14 14:08:19 +00:00
|
|
|
* :indentSize=8:tabSize=8:noTabs=false:
|
|
|
|
*/
|