2 * Definitions for Wireshark memory management and garbage collection
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include "ws_symbol_export.h"
33 /** Initialize all the memory allocation pools described below.
34 * This function must be called once when *shark initialize to set up the
35 * required structures.
40 /* Functions for handling memory allocation and garbage collection with
41 * a packet lifetime scope.
42 * These functions are used to allocate memory that will only remain persistent
43 * until Wireshark starts dissecting the next packet in the list.
44 * Everytime Wireshark starts decoding the next packet all memory allocated
45 * through these functions will be released back to the free pool.
47 * These functions are very fast and offer automatic garbage collection:
48 * Everytime a new packet is dissected, all memory allocations done in
49 * the previous packet is freed.
52 /** Allocate memory with a packet lifetime scope */
54 void *ep_alloc(size_t size
) G_GNUC_MALLOC
;
55 #define ep_new(type) ((type*)ep_alloc(sizeof(type)))
57 /** Allocate memory with a packet lifetime scope and fill it with zeros*/
59 void* ep_alloc0(size_t size
) G_GNUC_MALLOC
;
60 #define ep_new0(type) ((type*)ep_alloc0(sizeof(type)))
62 /** Duplicate a string with a packet lifetime scope */
64 gchar
* ep_strdup(const gchar
* src
) G_GNUC_MALLOC
;
66 /** Duplicate at most n characters of a string with a packet lifetime scope */
68 gchar
* ep_strndup(const gchar
* src
, size_t len
) G_GNUC_MALLOC
;
70 /** Duplicate a buffer with a packet lifetime scope */
72 void* ep_memdup(const void* src
, size_t len
) G_GNUC_MALLOC
;
74 /** Create a formatted string with a packet lifetime scope */
76 gchar
* ep_strdup_vprintf(const gchar
* fmt
, va_list ap
) G_GNUC_MALLOC
;
78 gchar
* ep_strdup_printf(const gchar
* fmt
, ...)
79 G_GNUC_MALLOC
G_GNUC_PRINTF(1, 2);
82 gchar
*ep_strconcat(const gchar
*string
, ...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED
;
84 /** allocates with a packet lifetime scope an array of type made of num elements */
85 #define ep_alloc_array(type,num) (type*)ep_alloc(sizeof(type)*(num))
87 /** allocates with a packet lifetime scope an array of type made of num elements,
88 * initialised to zero.
90 #define ep_alloc_array0(type,num) (type*)ep_alloc0(sizeof(type)*(num))
93 * Splits a string into a maximum of max_tokens pieces, using the given
94 * delimiter. If max_tokens is reached, the remainder of string is appended
95 * to the last token. Consecutive delimiters are treated as a single delimiter.
97 * The vector and all the strings are allocated with packet lifetime scope
100 gchar
** ep_strsplit(const gchar
* string
, const gchar
* delimiter
, int max_tokens
);
102 /** release all memory allocated in the previous packet dissection */
103 void ep_free_all(void);
106 /** a stack implemented using ephemeral allocators */
108 typedef struct _ep_stack_frame_t
** ep_stack_t
;
110 struct _ep_stack_frame_t
{
112 struct _ep_stack_frame_t
* below
;
113 struct _ep_stack_frame_t
* above
;
117 * creates an empty stack with a packet lifetime scope
120 ep_stack_t
ep_stack_new(void) G_GNUC_MALLOC
;
123 * pushes item into stack, returns item
126 void* ep_stack_push(ep_stack_t stack
, void* item
);
129 * pops an item from the stack
132 void* ep_stack_pop(ep_stack_t stack
);
135 * returns the item on top of the stack without popping it
137 #define ep_stack_peek(stack) ((*(stack))->payload)
140 /* Functions for handling memory allocation and garbage collection with
141 * a capture lifetime scope.
142 * These functions are used to allocate memory that will only remain persistent
143 * until Wireshark opens a new capture or capture file.
144 * Everytime Wireshark starts a new capture or opens a new capture file
145 * all the data allocated through these functions will be released back
148 * These functions are very fast and offer automatic garbage collection.
151 /** Allocate memory with a capture lifetime scope */
153 void *se_alloc(size_t size
) G_GNUC_MALLOC
;
154 #define se_new(type) ((type*)se_alloc(sizeof(type)))
156 /** Allocate memory with a capture lifetime scope and fill it with zeros*/
158 void* se_alloc0(size_t size
) G_GNUC_MALLOC
;
159 #define se_new0(type) ((type*)se_alloc0(sizeof(type)))
161 /** Duplicate a string with a capture lifetime scope */
163 gchar
* se_strdup(const gchar
* src
) G_GNUC_MALLOC
;
165 /** Duplicate at most n characters of a string with a capture lifetime scope */
167 gchar
* se_strndup(const gchar
* src
, size_t len
) G_GNUC_MALLOC
;
169 /** Duplicate a buffer with a capture lifetime scope */
171 void* se_memdup(const void* src
, size_t len
) G_GNUC_MALLOC
;
173 /* Create a formatted string with a capture lifetime scope */
175 gchar
* se_strdup_vprintf(const gchar
* fmt
, va_list ap
) G_GNUC_MALLOC
;
177 gchar
* se_strdup_printf(const gchar
* fmt
, ...)
178 G_GNUC_MALLOC
G_GNUC_PRINTF(1, 2);
180 /** allocates with a capture lifetime scope an array of type made of num elements */
181 #define se_alloc_array(type,num) (type*)se_alloc(sizeof(type)*(num))
183 /** release all memory allocated */
184 void se_free_all(void);
186 /**************************************************************
188 **************************************************************/
189 struct _emem_chunk_t
;
191 /* G_MEM_ALIGN is not always enough: http://mail.gnome.org/archives/gtk-devel-list/2004-December/msg00091.html
192 * So, we check (in configure) if we need 8-byte alignment. (Windows
193 * shouldn't need such a check until someone trys running it 32-bit on a CPU
194 * with more stringent alignment requirements than i386.)
196 * Yes, this ignores the possibility of needing 16-byte alignment for long doubles.
198 #if defined(NEED_8_BYTE_ALIGNMENT) && (G_MEM_ALIGN < 8)
199 #define WS_MEM_ALIGN 8
201 #define WS_MEM_ALIGN G_MEM_ALIGN
204 /**************************************************************
206 **************************************************************/
207 typedef struct _emem_tree_node_t
{
208 struct _emem_tree_node_t
*parent
;
209 struct _emem_tree_node_t
*left
;
210 struct _emem_tree_node_t
*right
;
212 #define EMEM_TREE_RB_COLOR_RED 0
213 #define EMEM_TREE_RB_COLOR_BLACK 1
215 #define EMEM_TREE_NODE_IS_DATA 0
216 #define EMEM_TREE_NODE_IS_SUBTREE 1
217 guint32 is_subtree
:1;
223 /** Right now we only do basic red/black trees but in the future we might want
224 * to try something different, such as a tree where each node keeps track
225 * of how many times it has been looked up, and letting often looked up
226 * nodes bubble upwards in the tree using rotate_right/left.
227 * That would probably be good for things like nfs filehandles
229 #define EMEM_TREE_TYPE_RED_BLACK 1
230 typedef struct _emem_tree_t
{
231 struct _emem_tree_t
*next
;
233 const char *name
; /**< just a string to make debugging easier */
234 emem_tree_node_t
*tree
;
235 void *(*malloc
)(size_t);
238 /* *******************************************************************
239 * Tree functions for SE memory allocation scope
240 * ******************************************************************* */
241 /** This function is used to create a se based tree with monitoring.
242 * When the SE heap is released back to the system the pointer to the
243 * tree is automatically reset to NULL.
245 * type is : EMEM_TREE_TYPE_RED_BLACK for a standard red/black tree.
248 emem_tree_t
*se_tree_create(int type
, const char *name
) G_GNUC_MALLOC
;
251 * Insert data into the tree and key it by a 32bit integer value
253 #define se_tree_insert32 emem_tree_insert32
256 * Retrieve the data at the search key. The search key is a 32bit integer value
258 #define se_tree_lookup32 emem_tree_lookup32
260 /** se_tree_lookup32_le
261 * Retrieve the data for the largest key that is less than or equal
264 #define se_tree_lookup32_le emem_tree_lookup32_le
266 /** se_tree_insert32_array
267 * Insert data into the tree and key it by a 32bit integer value
269 #define se_tree_insert32_array emem_tree_insert32_array
271 /** se_tree_lookup32_array
272 * Lookup data from the tree that is index by an array
274 #define se_tree_lookup32_array emem_tree_lookup32_array
276 /** se_tree_lookup32_array_le
277 * Retrieve the data for the largest key that is less than or equal
280 #define se_tree_lookup32_array_le emem_tree_lookup32_array_le
282 /* ******************************************************************
283 * Real tree functions
284 * ****************************************************************** */
286 /** This function is used to insert a node indexed by a guint32 key value.
287 * The data pointer should be allocated by the appropriate storage scope
288 * so that it will be released at the same time as the tree itself is
292 void emem_tree_insert32(emem_tree_t
*se_tree
, guint32 key
, void *data
);
294 /** This function will look up a node in the tree indexed by a guint32 integer
298 void *emem_tree_lookup32(emem_tree_t
*se_tree
, guint32 key
);
300 /** This function will look up a node in the tree indexed by a guint32 integer
302 * The function will return the node that has the largest key that is
303 * equal to or smaller than the search key, or NULL if no such key was
307 void *emem_tree_lookup32_le(emem_tree_t
*se_tree
, guint32 key
);
309 typedef struct _emem_tree_key_t
{
310 guint32 length
; /**< length in guint32 words */
314 /** This function is used to insert a node indexed by a sequence of guint32
316 * The data pointer should be allocated by SE allocators so that the
317 * data will be released at the same time as the tree itself is destroyed.
319 * Note: all the "key" members of the "key" argument MUST be aligned on
320 * 32-bit boundaries; otherwise, this code will crash on platforms such
321 * as SPARC that require aligned pointers.
323 * If you use ...32_array() calls you MUST make sure that every single node
324 * you add to a specific tree always has a key of exactly the same number of
325 * keylen words or things will most likely crash. Or at least that every single
326 * item that sits behind the same top level node always have exactly the same
329 * One way to guarantee this is the way that NFS does this for the
330 * nfs_name_snoop_known tree which holds filehandles for both v2 and v3.
331 * v2 filehandles are always 32 bytes (8 words) while v3 filehandles can have
332 * any length (though 32 bytes are most common).
333 * The NFS dissector handles this by providing a guint32 containing the length
334 * as the very first item in this vector :
336 * emem_tree_key_t fhkey[3];
338 * fhlen=nns->fh_length;
340 * fhkey[0].key=&fhlen;
341 * fhkey[1].length=fhlen/4;
342 * fhkey[1].key=nns->fh;
346 void emem_tree_insert32_array(emem_tree_t
*se_tree
, emem_tree_key_t
*key
, void *data
);
348 /** This function will look up a node in the tree indexed by a sequence of
349 * guint32 integer values.
352 void *emem_tree_lookup32_array(emem_tree_t
*se_tree
, emem_tree_key_t
*key
);
354 /** This function will look up a node in the tree indexed by a
355 * multi-part tree value.
356 * The function will return the node that has the largest key that is
357 * equal to or smaller than the search key, or NULL if no such key was
359 * Note: The key returned will be "less" in key order. The usefullness
360 * of the returned node must be verified prior to use.
363 void *emem_tree_lookup32_array_le(emem_tree_t
*se_tree
, emem_tree_key_t
*key
);
365 /** case insensitive strings as keys */
366 #define EMEM_TREE_STRING_NOCASE 0x00000001
367 /** Insert a new value under a string key */
369 void emem_tree_insert_string(emem_tree_t
* h
, const gchar
* k
, void* v
, guint32 flags
);
371 /** Lookup the value under a string key */
373 void* emem_tree_lookup_string(emem_tree_t
* h
, const gchar
* k
, guint32 flags
);
376 /** traverse a tree. if the callback returns TRUE the traversal will end */
377 typedef gboolean (*tree_foreach_func
)(void *value
, void *userdata
);
380 gboolean
emem_tree_foreach(emem_tree_t
* emem_tree
, tree_foreach_func callback
, void *user_data
);
383 /* ******************************************************************
384 * String buffers - Growable strings similar to GStrings
385 * ****************************************************************** */
387 typedef struct _emem_strbuf_t
{
388 gchar
*str
; /**< Points to the character data. It may move as text is */
389 /* added. The str field is null-terminated and so can */
390 /* be used as an ordinary C string. */
391 gsize len
; /**< strlen: ie: length of str not including trailing '\0' */
392 gsize alloc_len
; /**< num bytes curently allocated for str: 1 .. MAX_STRBUF_LEN */
393 gsize max_alloc_len
; /**< max num bytes to allocate for str: 1 .. MAX_STRBUF_LEN */
397 * The maximum length is limited to 64K. If you need something bigger, you
398 * should probably use an actual GString or GByteArray.
402 * Allocate an ephemeral string buffer with "unlimited" size.
404 * @param init The initial string for the buffer, or NULL to allocate an initial zero-length string.
406 * @return A newly-allocated string buffer.
409 emem_strbuf_t
*ep_strbuf_new(const gchar
*init
) G_GNUC_MALLOC
;
412 * Allocate an ephemeral string buffer suitable for the protocol tree.
413 * The string will never grow beyond the maximum tree item length.
415 * @param init The initial string for the buffer, or NULL to allocate an initial zero-length string.
417 * @return A newly-allocated string buffer.
420 emem_strbuf_t
*ep_strbuf_new_label(const gchar
*init
) G_GNUC_MALLOC
;
423 * Allocate an ephemeral string buffer with enough initial space for alloc_len bytes
424 * and a maximum of max_alloc_len bytes.
426 * @param alloc_len The initial size of the buffer. This value can be 0, but a nonzero
427 * value is recommended.
428 * @param max_alloc_len The maximum size of the buffer. 0 means "unlimited" (within
431 * @return A newly-allocated string buffer. str will be empty.
434 emem_strbuf_t
*ep_strbuf_sized_new(gsize alloc_len
, gsize max_alloc_len
) G_GNUC_MALLOC
;
437 * Append vprintf-style formatted text to a string buffer.
439 * @param strbuf The ep_strbuf-allocated string buffer to append to.
440 * @param format A printf-style string format.
441 * @param ap The list of arguments to append.
444 void ep_strbuf_append_vprintf(emem_strbuf_t
*strbuf
, const gchar
*format
, va_list ap
);
447 * Apply printf-style formatted text to a string buffer.
449 * @param strbuf The ep_strbuf-allocated string buffer to set to.
450 * @param format A printf-style string format.
453 void ep_strbuf_printf(emem_strbuf_t
*strbuf
, const gchar
*format
, ...)
457 * Append printf-style formatted text to a string buffer.
459 * @param strbuf The ep_strbuf-allocated string buffer to append to.
460 * @param format A printf-style string format.
463 void ep_strbuf_append_printf(emem_strbuf_t
*strbuf
, const gchar
*format
, ...)
467 * Append a string to a string buffer.
469 * @param strbuf The ep_strbuf-allocated string buffer to append to.
470 * @param str A null-terminated string.
475 emem_strbuf_t
*ep_strbuf_append(emem_strbuf_t
*strbuf
, const gchar
*str
);
478 * Append a character to a string buffer.
480 * @param strbuf The ep_strbuf-allocated string buffer to append to.
481 * @param c The character to append.
486 emem_strbuf_t
*ep_strbuf_append_c(emem_strbuf_t
*strbuf
, const gchar c
);
489 * Append a Unicode characeter converted to UTF-8 to a string buffer.
491 * @param strbuf The ep_strbuf-allocated string buffer to append to.
492 * @param c The Unicode character to append.
497 emem_strbuf_t
*ep_strbuf_append_unichar(emem_strbuf_t
*strbuf
, const gunichar c
);
500 * Chop off the end of a string buffer.
502 * @param strbuf The ep_strbuf-allocated string buffer to append to.
503 * @param len The new string length.
508 emem_strbuf_t
*ep_strbuf_truncate(emem_strbuf_t
*strbuf
, gsize len
);
511 * Dump the whole tree (of trees) to stdout.
513 * @param emem_tree The tree to dump to standard output.
517 void emem_print_tree(emem_tree_t
* emem_tree
);
519 /* #define DEBUG_INTENSE_CANARY_CHECKS */
521 /** Helper to troubleshoot ep memory corruption.
522 * If compiled and the environment variable WIRESHARK_DEBUG_EP_INTENSE_CANARY exists
523 * it will check the canaries and when found corrupt stop there in the hope
524 * the corruptor is still there in the stack.
525 * Some checkpoints are already set in packet.c in strategic points
526 * before and after dissection of a frame or a dissector call.
529 #ifdef DEBUG_INTENSE_CANARY_CHECKS
530 void ep_check_canary_integrity(const char* fmt
, ...)
532 #define EP_CHECK_CANARY(args) ep_check_canary_integrity args
534 #define EP_CHECK_CANARY(args)
538 * Verify that the given pointer is of ephemeral type.
540 * @param ptr The pointer to verify
542 * @return TRUE if the pointer belongs to the ephemeral pool.
544 gboolean
ep_verify_pointer(const void *ptr
);
546 * Verify that the given pointer is of seasonal type.
548 * @param ptr The pointer to verify
550 * @return TRUE if the pointer belongs to the seasonal pool.
552 gboolean
se_verify_pointer(const void *ptr
);