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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include "gnuc_format_check.h"
31 /* Functions for handling memory allocation and garbage collection with
32 * a packet lifetime scope.
33 * These functions are used to allocate memory that will only remain persistent
34 * until Wireshark starts dissecting the next packet in the list.
35 * Everytime Wireshark starts decoding the next packet all memory allocated
36 * through these functions will be released back to the free pool.
38 * These functions are very fast and offer automatic garbage collection:
39 * Everytime a new packet is dissected, all memory allocations done in
40 * the previous packet is freed.
42 /* Initialize packet-lifetime memory allocation pool. This function is called
43 * once when [t]Wireshark is initialized to set up the required structures.
45 void ep_init_chunk(void);
47 /* Allocate memory with a packet lifetime scope */
48 void *ep_alloc(size_t size);
49 #define ep_new(type) ((type*)ep_alloc(sizeof(type)))
51 /* Allocate memory with a packet lifetime scope and fill it with zeros*/
52 void* ep_alloc0(size_t size);
53 #define ep_new0(type) ((type*)ep_alloc0(sizeof(type)))
55 /* Duplicate a string with a packet lifetime scope */
56 gchar* ep_strdup(const gchar* src);
58 /* Duplicate at most n characters of a string with a packet lifetime scope */
59 gchar* ep_strndup(const gchar* src, size_t len);
61 /* Duplicate a buffer with a packet lifetime scope */
62 void* ep_memdup(const void* src, size_t len);
64 /* Create a formatted string with a packet lifetime scope */
65 gchar* ep_strdup_vprintf(const gchar* fmt, va_list ap);
66 gchar* ep_strdup_printf(const gchar* fmt, ...)
67 GNUC_FORMAT_CHECK(printf, 1, 2);
69 /* allocates with a packet lifetime scope an array of type made of num elements */
70 #define ep_alloc_array(type,num) (type*)ep_alloc(sizeof(type)*(num))
72 /* allocates with a packet lifetime scope an array of type made of num elements,
73 * initialised to zero.
75 #define ep_alloc_array0(type,num) (type*)ep_alloc0(sizeof(type)*(num))
78 * Splits a string into a maximum of max_tokens pieces, using the given
79 * delimiter. If max_tokens is reached, the remainder of string is appended
80 * to the last token. Consecutive delimiters are treated as a single delimiter.
82 * the vector and all the strings are allocated with packet lifetime scope
84 gchar** ep_strsplit(const gchar* string, const gchar* delimiter, int max_tokens);
86 /* release all memory allocated in the previous packet dissector */
87 void ep_free_all(void);
90 /* a stack implemented using ephemeral allocators */
92 typedef struct _ep_stack_frame_t** ep_stack_t;
94 struct _ep_stack_frame_t {
96 struct _ep_stack_frame_t* below;
97 struct _ep_stack_frame_t* above;
101 * creates an empty stack with a packet lifetime scope
103 ep_stack_t ep_stack_new(void);
106 * pushes item into stack, returns item
108 void* ep_stack_push(ep_stack_t stack, void* item);
111 * pops an item from the stack
113 void* ep_stack_pop(ep_stack_t stack);
116 * returns the item on top of the stack without popping it
118 #define ep_stack_peek(stack) ((*(stack))->payload)
121 /* Functions for handling memory allocation and garbage collection with
122 * a capture lifetime scope.
123 * These functions are used to allocate memory that will only remain persistent
124 * until Wireshark opens a new capture or capture file.
125 * Everytime Wireshark starts a new capture or opens a new capture file
126 * all the data allocated through these functions will be released back
129 * These functions are very fast and offer automatic garbage collection.
131 /* Initialize capture-lifetime memory allocation pool. This function is called
132 * once when [t]Wireshark is initialized to set up the required structures.
134 void se_init_chunk(void);
136 /* Allocate memory with a capture lifetime scope */
137 void *se_alloc(size_t size);
139 /* Allocate memory with a capture lifetime scope and fill it with zeros*/
140 void* se_alloc0(size_t size);
142 /* Duplicate a string with a capture lifetime scope */
143 gchar* se_strdup(const gchar* src);
145 /* Duplicate at most n characters of a string with a capture lifetime scope */
146 gchar* se_strndup(const gchar* src, size_t len);
148 /* Duplicate a buffer with a capture lifetime scope */
149 void* se_memdup(const void* src, size_t len);
151 /* Create a formatted string with a capture lifetime scope */
152 gchar* se_strdup_vprintf(const gchar* fmt, va_list ap);
153 gchar* se_strdup_printf(const gchar* fmt, ...)
154 GNUC_FORMAT_CHECK(printf, 1, 2);
156 /* allocates with a capture lifetime scope an array of type made of num elements */
157 #define se_alloc_array(type,num) (type*)se_alloc(sizeof(type)*(num))
159 /* release all memory allocated */
160 void se_free_all(void);
165 /**************************************************************
167 **************************************************************/
168 #define EMEM_TREE_RB_COLOR_RED 0x00
169 #define EMEM_TREE_RB_COLOR_BLACK 0x01
170 typedef struct _emem_tree_node_t {
171 struct _emem_tree_node_t *parent;
172 struct _emem_tree_node_t *left;
173 struct _emem_tree_node_t *right;
181 /* Right now we only do basic red/black trees but in the future we might want
182 * to try something different, such as a tree where each node keeps track
183 * of how many times it has been looked up, and letting often looked up
184 * nodes bubble upwards in the tree using rotate_right/left.
185 * That would probably be good for things like nfs filehandles
187 #define EMEM_TREE_TYPE_RED_BLACK 1
188 typedef struct _emem_tree_t {
189 struct _emem_tree_t *next;
191 const char *name; /* just a string to make debugging easier */
192 emem_tree_node_t *tree;
193 void *(*malloc)(size_t);
196 /* list of all trees with se allocation scope so that they can all be reset
197 * automatically when we free all se memory
199 extern emem_tree_t *se_trees;
202 /* *******************************************************************
203 * Tree functions for SE memory allocation scope
204 * ******************************************************************* */
205 /* This function is used to create a se based tree with monitoring.
206 * When the SE heap is released back to the system the pointer to the
207 * tree is automatically reset to NULL.
209 * type is : EMEM_TREE_TYPE_RED_BLACK for a standard red/black tree.
211 emem_tree_t *se_tree_create(int type, const char *name);
213 /* This function is similar to the se_tree_create() call but with the
214 * difference that when the se memory is release everything including the
215 * pointer to the tree itself will be released.
216 * This tree will not be just reset to zero it will be completely forgotten
218 * Use this function for when you want to store the pointer to a tree inside
219 * another structure that is also se allocated so that when the structure is
220 * released, the tree will be completely released as well.
222 emem_tree_t *se_tree_create_non_persistent(int type, const char *name);
225 * Insert data into the tree and key it by a 32bit integer value
227 #define se_tree_insert32 emem_tree_insert32
230 * Retreive the data at the search key. the search key is a 32bit integer value
232 #define se_tree_lookup32 emem_tree_lookup32
234 /* se_tree_lookup32_le
235 * Retreive the data for the largest key that is less than or equal
238 #define se_tree_lookup32_le emem_tree_lookup32_le
240 /* se_tree_insert32_array
241 * Insert data into the tree and key it by a 32bit integer value
243 #define se_tree_insert32_array emem_tree_insert32_array
245 /* se_tree_lookup32_array
246 * Lookup data from the tree that is index by an array
248 #define se_tree_lookup32_array emem_tree_lookup32_array
252 /* Create a new string based hash table */
253 #define se_tree_create_string() se_tree_create(SE_TREE_TYPE_RED_BLACK)
255 /* Insert a new value under a string key */
256 #define se_tree_insert_string emem_tree_insert_string
258 /* Lookup the value under a string key */
259 #define se_tree_lookup_string emem_tree_lookup_string
261 /* Traverse a tree */
262 #define se_tree_foreach emem_tree_foreach
265 /* *******************************************************************
266 * Tree functions for PE memory allocation scope
267 * ******************************************************************* */
268 /* These trees have PErmanent allocation scope and will never be released
270 emem_tree_t *pe_tree_create(int type, char *name);
271 #define pe_tree_insert32 emem_tree_insert32
272 #define pe_tree_lookup32 emem_tree_lookup32
273 #define pe_tree_lookup32_le emem_tree_lookup32_le
274 #define pe_tree_insert32_array emem_tree_insert32_array
275 #define pe_tree_lookup32_array emem_tree_lookup32_array
276 #define pe_tree_insert_string emem_tree_insert_string
277 #define pe_tree_lookup_string emem_tree_lookup_string
278 #define pe_tree_foreach emem_tree_foreach
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
291 void emem_tree_insert32(emem_tree_t *se_tree, guint32 key, void *data);
293 /* This function will look up a node in the tree indexed by a guint32 integer
296 void *emem_tree_lookup32(emem_tree_t *se_tree, guint32 key);
298 /* This function will look up a node in the tree indexed by a guint32 integer
300 * The function will return the node that has the largest key that is
301 * equal to or smaller than the search key, or NULL if no such key was
304 void *emem_tree_lookup32_le(emem_tree_t *se_tree, guint32 key);
306 typedef struct _emem_tree_key_t {
307 guint32 length; /*length in guint32 words */
311 /* This function is used to insert a node indexed by a sequence of guint32
313 * The data pointer should be allocated by SE allocators so that the
314 * data will be released at the same time as the tree itself is destroyed.
316 * Note: all the "key" members of the "key" argument MUST be aligned on
317 * 32-bit boundaries; otherwise, this code will crash on platforms such
318 * as SPARC that require aligned pointers.
320 * If you use ...32_array() calls you MUST make sure that every single node
321 * you add to a specific tree always has a key of exactly the same number of
322 * keylen words or things will most likely crash. Or at least that every single
323 * item that sits behind the same top level node always have exactly the same
326 * One way to guarantee this is the way that NFS does this for the
327 * nfs_name_snoop_known tree which holds filehandles for both v2 and v3.
328 * v2 filehandles are always 32 bytes (8 words) while v3 filehandles can have
329 * any length (though 32bytes are most common).
330 * The NFS dissector handles this by providing a guint32 containing the length
331 * as the very first item in this vector :
333 * emem_tree_key_t fhkey[3];
335 * fhlen=nns->fh_length;
337 * fhkey[0].key=&fhlen;
338 * fhkey[1].length=fhlen/4;
339 * fhkey[1].key=nns->fh;
342 void emem_tree_insert32_array(emem_tree_t *se_tree, emem_tree_key_t *key, void *data);
344 /* This function will look up a node in the tree indexed by a sequence of
345 * guint32 integer values.
347 void *emem_tree_lookup32_array(emem_tree_t *se_tree, emem_tree_key_t *key);
349 /* Insert a new value under a string key */
350 void emem_tree_insert_string(emem_tree_t* h, const gchar* k, void* v);
352 /* Lookup the value under a string key */
353 void* emem_tree_lookup_string(emem_tree_t* h, const gchar* k);
356 /* traverse a tree. if the callback returns TRUE the traversal will end */
357 typedef gboolean (*tree_foreach_func)(void *value, void *userdata);
359 void emem_tree_foreach(emem_tree_t* emem_tree, tree_foreach_func callback, void *user_data);
364 void emem_print_tree(emem_tree_t* emem_tree);