HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / epan / emem.h
blob6104fd3f4411d4c262a68ef814f39b2a0509929e
1 /* emem.h
2 * Definitions for Wireshark memory management and garbage collection
3 * Ronnie Sahlberg 2005
5 * $Id$
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.
26 #ifndef __EMEM_H__
27 #define __EMEM_H__
29 #include <glib.h>
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.
37 WS_DLL_PUBLIC
38 void emem_init(void);
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 */
53 WS_DLL_PUBLIC
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*/
58 WS_DLL_PUBLIC
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 */
63 WS_DLL_PUBLIC
64 gchar* ep_strdup(const gchar* src) G_GNUC_MALLOC;
66 /** Duplicate at most n characters of a string with a packet lifetime scope */
67 WS_DLL_PUBLIC
68 gchar* ep_strndup(const gchar* src, size_t len) G_GNUC_MALLOC;
70 /** Duplicate a buffer with a packet lifetime scope */
71 WS_DLL_PUBLIC
72 void* ep_memdup(const void* src, size_t len) G_GNUC_MALLOC;
74 /** Create a formatted string with a packet lifetime scope */
75 WS_DLL_PUBLIC
76 gchar* ep_strdup_vprintf(const gchar* fmt, va_list ap) G_GNUC_MALLOC;
77 WS_DLL_PUBLIC
78 gchar* ep_strdup_printf(const gchar* fmt, ...)
79 G_GNUC_MALLOC G_GNUC_PRINTF(1, 2);
81 WS_DLL_PUBLIC
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))
92 /**
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
99 WS_DLL_PUBLIC
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 {
111 void* payload;
112 struct _ep_stack_frame_t* below;
113 struct _ep_stack_frame_t* above;
117 * creates an empty stack with a packet lifetime scope
119 WS_DLL_PUBLIC
120 ep_stack_t ep_stack_new(void) G_GNUC_MALLOC;
123 * pushes item into stack, returns item
125 WS_DLL_PUBLIC
126 void* ep_stack_push(ep_stack_t stack, void* item);
129 * pops an item from the stack
131 WS_DLL_PUBLIC
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
146 * to the free pool.
148 * These functions are very fast and offer automatic garbage collection.
151 /** Allocate memory with a capture lifetime scope */
152 WS_DLL_PUBLIC
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*/
157 WS_DLL_PUBLIC
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 */
162 WS_DLL_PUBLIC
163 gchar* se_strdup(const gchar* src) G_GNUC_MALLOC;
165 /** Duplicate at most n characters of a string with a capture lifetime scope */
166 WS_DLL_PUBLIC
167 gchar* se_strndup(const gchar* src, size_t len) G_GNUC_MALLOC;
169 /** Duplicate a buffer with a capture lifetime scope */
170 WS_DLL_PUBLIC
171 void* se_memdup(const void* src, size_t len) G_GNUC_MALLOC;
173 /* Create a formatted string with a capture lifetime scope */
174 WS_DLL_PUBLIC
175 gchar* se_strdup_vprintf(const gchar* fmt, va_list ap) G_GNUC_MALLOC;
176 WS_DLL_PUBLIC
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 /**************************************************************
187 * slab allocator
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
200 #else
201 #define WS_MEM_ALIGN G_MEM_ALIGN
202 #endif
204 /**************************************************************
205 * binary trees
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;
211 struct {
212 #define EMEM_TREE_RB_COLOR_RED 0
213 #define EMEM_TREE_RB_COLOR_BLACK 1
214 guint32 rb_color:1;
215 #define EMEM_TREE_NODE_IS_DATA 0
216 #define EMEM_TREE_NODE_IS_SUBTREE 1
217 guint32 is_subtree:1;
218 } u;
219 guint32 key32;
220 void *data;
221 } emem_tree_node_t;
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;
232 int type;
233 const char *name; /**< just a string to make debugging easier */
234 emem_tree_node_t *tree;
235 void *(*malloc)(size_t);
236 } emem_tree_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.
247 WS_DLL_PUBLIC
248 emem_tree_t *se_tree_create(int type, const char *name) G_GNUC_MALLOC;
250 /** se_tree_insert32
251 * Insert data into the tree and key it by a 32bit integer value
253 #define se_tree_insert32 emem_tree_insert32
255 /** se_tree_lookup32
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
262 * to the search key.
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
278 * to the search key.
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
289 * destroyed.
291 WS_DLL_PUBLIC
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
295 * value.
297 WS_DLL_PUBLIC
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
301 * value.
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
304 * found.
306 WS_DLL_PUBLIC
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 */
311 guint32 *key;
312 } emem_tree_key_t;
314 /** This function is used to insert a node indexed by a sequence of guint32
315 * key values.
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
327 * number of words.
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;
339 * fhkey[0].length=1;
340 * fhkey[0].key=&fhlen;
341 * fhkey[1].length=fhlen/4;
342 * fhkey[1].key=nns->fh;
343 * fhkey[2].length=0;
345 WS_DLL_PUBLIC
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.
351 WS_DLL_PUBLIC
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
358 * found.
359 * Note: The key returned will be "less" in key order. The usefullness
360 * of the returned node must be verified prior to use.
362 WS_DLL_PUBLIC
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 */
368 WS_DLL_PUBLIC
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 */
372 WS_DLL_PUBLIC
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);
379 WS_DLL_PUBLIC
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 */
394 } emem_strbuf_t;
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.
408 WS_DLL_PUBLIC
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.
419 WS_DLL_PUBLIC
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
429 * reason).
431 * @return A newly-allocated string buffer. str will be empty.
433 WS_DLL_PUBLIC
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.
443 WS_DLL_PUBLIC
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.
452 WS_DLL_PUBLIC
453 void ep_strbuf_printf(emem_strbuf_t *strbuf, const gchar *format, ...)
454 G_GNUC_PRINTF(2, 3);
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.
462 WS_DLL_PUBLIC
463 void ep_strbuf_append_printf(emem_strbuf_t *strbuf, const gchar *format, ...)
464 G_GNUC_PRINTF(2, 3);
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.
472 * @return strbuf
474 WS_DLL_PUBLIC
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.
483 * @return strbuf
485 WS_DLL_PUBLIC
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.
494 * @return strbuf
496 WS_DLL_PUBLIC
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.
505 * @return strbuf
507 WS_DLL_PUBLIC
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, ...)
531 G_GNUC_PRINTF(1, 2);
532 #define EP_CHECK_CANARY(args) ep_check_canary_integrity args
533 #else
534 #define EP_CHECK_CANARY(args)
535 #endif
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);
554 #endif /* emem.h */