Regenerate a few more of the ASN.1 dissectors
[obnox/wireshark/wip.git] / epan / emem.h
1 /* emem.h
2  * Definitions for Wireshark memory management and garbage collection
3  * Ronnie Sahlberg 2005
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifndef __EMEM_H__
27 #define __EMEM_H__
28
29 #include "g_gnuc_malloc.h"
30
31 /*  Initialize all the memory allocation pools described below.
32  *  This function must be called once when *shark initialize to set up the
33  *  required structures.
34  */
35 void emem_init(void);
36
37 /* Functions for handling memory allocation and garbage collection with
38  * a packet lifetime scope.
39  * These functions are used to allocate memory that will only remain persistent
40  * until Wireshark starts dissecting the next packet in the list.
41  * Everytime Wireshark starts decoding the next packet all memory allocated
42  * through these functions will be released back to the free pool.
43  *
44  * These functions are very fast and offer automatic garbage collection:
45  * Everytime a new packet is dissected, all memory allocations done in
46  * the previous packet is freed.
47  */
48
49 /* Allocate memory with a packet lifetime scope */
50 void *ep_alloc(size_t size) G_GNUC_MALLOC;
51 #define ep_new(type) ((type*)ep_alloc(sizeof(type)))
52
53 /* Allocate memory with a packet lifetime scope and fill it with zeros*/
54 void* ep_alloc0(size_t size) G_GNUC_MALLOC;
55 #define ep_new0(type) ((type*)ep_alloc0(sizeof(type)))
56
57 /* Duplicate a string with a packet lifetime scope */
58 gchar* ep_strdup(const gchar* src) G_GNUC_MALLOC;
59
60 /* Duplicate at most n characters of a string with a packet lifetime scope */
61 gchar* ep_strndup(const gchar* src, size_t len) G_GNUC_MALLOC;
62
63 /* Duplicate a buffer with a packet lifetime scope */
64 void* ep_memdup(const void* src, size_t len) G_GNUC_MALLOC;
65
66 /* Create a formatted string with a packet lifetime scope */
67 gchar* ep_strdup_vprintf(const gchar* fmt, va_list ap) G_GNUC_MALLOC;
68 gchar* ep_strdup_printf(const gchar* fmt, ...)
69      G_GNUC_MALLOC G_GNUC_PRINTF(1, 2);
70
71 /* allocates with a packet lifetime scope an array of type made of num elements */
72 #define ep_alloc_array(type,num) (type*)ep_alloc(sizeof(type)*(num))
73
74 /* allocates with a packet lifetime scope an array of type made of num elements,
75  * initialised to zero.
76  */
77 #define ep_alloc_array0(type,num) (type*)ep_alloc0(sizeof(type)*(num))
78
79 /*
80  * Splits a string into a maximum of max_tokens pieces, using the given
81  * delimiter. If max_tokens is reached, the remainder of string is appended
82  * to the last token. Consecutive delimiters are treated as a single delimiter.
83  *
84  * The vector and all the strings are allocated with packet lifetime scope
85  */
86 gchar** ep_strsplit(const gchar* string, const gchar* delimiter, int max_tokens);
87
88 /* release all memory allocated in the previous packet dissection */
89 void ep_free_all(void);
90
91
92 /* a stack implemented using ephemeral allocators */
93
94 typedef struct _ep_stack_frame_t** ep_stack_t;
95
96 struct _ep_stack_frame_t {
97     void* payload;
98     struct _ep_stack_frame_t* below;
99     struct _ep_stack_frame_t* above;
100 };
101
102 /*
103  * creates an empty stack with a packet lifetime scope
104  */
105 ep_stack_t ep_stack_new(void) G_GNUC_MALLOC;
106
107 /*
108  * pushes item into stack, returns item
109  */
110 void* ep_stack_push(ep_stack_t stack, void* item);
111
112 /*
113  * pops an item from the stack
114  */
115 void* ep_stack_pop(ep_stack_t stack);
116
117 /*
118  * returns the item on top of the stack without popping it
119  */
120 #define ep_stack_peek(stack) ((*(stack))->payload)
121
122
123 /* Functions for handling memory allocation and garbage collection with
124  * a capture lifetime scope.
125  * These functions are used to allocate memory that will only remain persistent
126  * until Wireshark opens a new capture or capture file.
127  * Everytime Wireshark starts a new capture or opens a new capture file
128  * all the data allocated through these functions will be released back
129  * to the free pool.
130  *
131  * These functions are very fast and offer automatic garbage collection.
132  */
133
134 /* Allocate memory with a capture lifetime scope */
135 void *se_alloc(size_t size) G_GNUC_MALLOC;
136
137 /* Allocate memory with a capture lifetime scope and fill it with zeros*/
138 void* se_alloc0(size_t size) G_GNUC_MALLOC;
139
140 /* Duplicate a string with a capture lifetime scope */
141 gchar* se_strdup(const gchar* src) G_GNUC_MALLOC;
142
143 /* Duplicate at most n characters of a string with a capture lifetime scope */
144 gchar* se_strndup(const gchar* src, size_t len) G_GNUC_MALLOC;
145
146 /* Duplicate a buffer with a capture lifetime scope */
147 void* se_memdup(const void* src, size_t len) G_GNUC_MALLOC;
148
149 /* Create a formatted string with a capture lifetime scope */
150 gchar* se_strdup_vprintf(const gchar* fmt, va_list ap) G_GNUC_MALLOC;
151 gchar* se_strdup_printf(const gchar* fmt, ...)
152      G_GNUC_MALLOC G_GNUC_PRINTF(1, 2);
153
154 /* allocates with a capture lifetime scope an array of type made of num elements */
155 #define se_alloc_array(type,num) (type*)se_alloc(sizeof(type)*(num))
156
157 /* release all memory allocated */
158 void se_free_all(void);
159
160
161
162
163 /**************************************************************
164  * binary trees
165  **************************************************************/
166 typedef struct _emem_tree_node_t {
167         struct _emem_tree_node_t *parent;
168         struct _emem_tree_node_t *left;
169         struct _emem_tree_node_t *right;
170         struct {
171 #define EMEM_TREE_RB_COLOR_RED          0
172 #define EMEM_TREE_RB_COLOR_BLACK        1
173                 guint32 rb_color:1;
174 #define EMEM_TREE_NODE_IS_DATA          0
175 #define EMEM_TREE_NODE_IS_SUBTREE       1
176                 guint32 is_subtree:1;
177         } u;
178         guint32 key32;
179         void *data;
180 } emem_tree_node_t;
181
182 /* Right now we only do basic red/black trees   but in the future we might want
183  * to try something different, such as a tree where each node keeps track
184  * of how many times it has been looked up, and letting often looked up
185  * nodes bubble upwards in the tree using rotate_right/left.
186  * That would probably be good for things like nfs filehandles
187  */
188 #define EMEM_TREE_TYPE_RED_BLACK        1
189 typedef struct _emem_tree_t {
190         struct _emem_tree_t *next;
191         int type;
192         const char *name;    /* just a string to make debugging easier */
193         emem_tree_node_t *tree;
194         void *(*malloc)(size_t);
195 } emem_tree_t;
196
197 /* *******************************************************************
198  * Tree functions for SE memory allocation scope
199  * ******************************************************************* */
200 /* This function is used to create a se based tree with monitoring.
201  * When the SE heap is released back to the system the pointer to the
202  * tree is automatically reset to NULL.
203  *
204  * type is : EMEM_TREE_TYPE_RED_BLACK for a standard red/black tree.
205  */
206 emem_tree_t *se_tree_create(int type, const char *name) G_GNUC_MALLOC;
207
208 /* This function is similar to the se_tree_create() call but with the
209  * difference that when the se memory is released everything including the
210  * pointer to the tree itself will be released.
211  * This tree will not be just reset to zero, it will be completely forgotten
212  * by the allocator.
213  * Use this function for when you want to store the pointer to a tree inside
214  * another structure that is also se allocated so that when the structure is
215  * released, the tree will be completely released as well.
216  */
217 emem_tree_t *se_tree_create_non_persistent(int type, const char *name) G_GNUC_MALLOC;
218
219 /* se_tree_insert32
220  * Insert data into the tree and key it by a 32bit integer value
221  */
222 #define se_tree_insert32 emem_tree_insert32
223
224 /* se_tree_lookup32
225  * Retrieve the data at the search key. The search key is a 32bit integer value
226  */
227 #define se_tree_lookup32 emem_tree_lookup32
228
229 /* se_tree_lookup32_le
230  * Retrieve the data for the largest key that is less than or equal
231  * to the search key.
232  */
233 #define se_tree_lookup32_le emem_tree_lookup32_le
234
235 /* se_tree_insert32_array
236  * Insert data into the tree and key it by a 32bit integer value
237  */
238 #define se_tree_insert32_array emem_tree_insert32_array
239
240 /* se_tree_lookup32_array
241  * Lookup data from the tree that is index by an array
242  */
243 #define se_tree_lookup32_array emem_tree_lookup32_array
244
245 /* se_tree_lookup32_array_le
246  * Retrieve the data for the largest key that is less than or equal
247  * to the search key.
248  */
249 #define se_tree_lookup32_array_le emem_tree_lookup32_array_le
250
251 /* Create a new string based hash table */
252 #define se_tree_create_string() se_tree_create(SE_TREE_TYPE_RED_BLACK)
253
254 /* Insert a new value under a string key */
255 #define se_tree_insert_string emem_tree_insert_string
256
257 /* Lookup the value under a string key */
258 #define se_tree_lookup_string emem_tree_lookup_string
259
260 /* Traverse a tree */
261 #define se_tree_foreach emem_tree_foreach
262
263
264 /* *******************************************************************
265  * Tree functions for PE memory allocation scope
266  * ******************************************************************* */
267 /* These trees have PErmanent allocation scope and will never be released
268  */
269 emem_tree_t *pe_tree_create(int type, const char *name) G_GNUC_MALLOC;
270 #define pe_tree_insert32 emem_tree_insert32
271 #define pe_tree_lookup32 emem_tree_lookup32
272 #define pe_tree_lookup32_le emem_tree_lookup32_le
273 #define pe_tree_insert32_array emem_tree_insert32_array
274 #define pe_tree_lookup32_array emem_tree_lookup32_array
275 #define pe_tree_insert_string emem_tree_insert_string
276 #define pe_tree_lookup_string emem_tree_lookup_string
277 #define pe_tree_foreach emem_tree_foreach
278
279
280
281 /* ******************************************************************
282  * Real tree functions
283  * ****************************************************************** */
284
285 /* This function is used to insert a node indexed by a guint32 key value.
286  * The data pointer should be allocated by the appropriate storage scope
287  * so that it will be released at the same time as the tree itself is
288  * destroyed.
289  */
290 void emem_tree_insert32(emem_tree_t *se_tree, guint32 key, void *data);
291
292 /* This function will look up a node in the tree indexed by a guint32 integer
293  * value.
294  */
295 void *emem_tree_lookup32(emem_tree_t *se_tree, guint32 key);
296
297 /* This function will look up a node in the tree indexed by a guint32 integer
298  * value.
299  * The function will return the node that has the largest key that is
300  * equal to or smaller than the search key, or NULL if no such key was
301  * found.
302  */
303 void *emem_tree_lookup32_le(emem_tree_t *se_tree, guint32 key);
304
305 typedef struct _emem_tree_key_t {
306         guint32 length;                 /*length in guint32 words */
307         guint32 *key;
308 } emem_tree_key_t;
309
310 /* This function is used to insert a node indexed by a sequence of guint32
311  * key values.
312  * The data pointer should be allocated by SE allocators so that the
313  * data will be released at the same time as the tree itself is destroyed.
314  *
315  * Note: all the "key" members of the "key" argument MUST be aligned on
316  * 32-bit boundaries; otherwise, this code will crash on platforms such
317  * as SPARC that require aligned pointers.
318  *
319  * If you use ...32_array() calls you MUST make sure that every single node
320  * you add to a specific tree always has a key of exactly the same number of
321  * keylen words or things will most likely crash. Or at least that every single
322  * item that sits behind the same top level node always have exactly the same
323  * number of words.
324  *
325  * One way to guarantee this is the way that NFS does this for the
326  * nfs_name_snoop_known tree which holds filehandles for both v2 and v3.
327  * v2 filehandles are always 32 bytes (8 words) while v3 filehandles can have
328  * any length (though 32 bytes are most common).
329  * The NFS dissector handles this by providing a guint32 containing the length
330  * as the very first item in this vector :
331  *
332  *                      emem_tree_key_t fhkey[3];
333  *
334  *                      fhlen=nns->fh_length;
335  *                      fhkey[0].length=1;
336  *                      fhkey[0].key=&fhlen;
337  *                      fhkey[1].length=fhlen/4;
338  *                      fhkey[1].key=nns->fh;
339  *                      fhkey[2].length=0;
340  */
341 void emem_tree_insert32_array(emem_tree_t *se_tree, emem_tree_key_t *key, void *data);
342
343 /* This function will look up a node in the tree indexed by a sequence of
344  * guint32 integer values.
345  */
346 void *emem_tree_lookup32_array(emem_tree_t *se_tree, emem_tree_key_t *key);
347
348 /* This function will look up a node in the tree indexed by a
349  * multi-part tree value.
350  * The function will return the node that has the largest key that is
351  * equal to or smaller than the search key, or NULL if no such key was
352  * found.
353  * Note:  The key returned will be "less" in key order.  The usefullness
354  * of the returned node must be verified prior to use.
355  */
356 void *emem_tree_lookup32_array_le(emem_tree_t *se_tree, emem_tree_key_t *key);
357
358 /* case insensitive strings as keys */
359 #define EMEM_TREE_STRING_NOCASE                 0x00000001
360 /* Insert a new value under a string key */
361 void emem_tree_insert_string(emem_tree_t* h, const gchar* k, void* v, guint32 flags);
362
363 /* Lookup the value under a string key */
364 void* emem_tree_lookup_string(emem_tree_t* h, const gchar* k, guint32 flags);
365
366
367 /* traverse a tree. if the callback returns TRUE the traversal will end */
368 typedef gboolean (*tree_foreach_func)(void *value, void *userdata);
369
370 gboolean emem_tree_foreach(emem_tree_t* emem_tree, tree_foreach_func callback, void *user_data);
371
372
373 /* ******************************************************************
374  * String buffers - Growable strings similar to GStrings
375  * ****************************************************************** */
376
377 typedef struct _emem_strbuf_t {
378     gchar *str;             /* Points to the character data. It may move as text is       */
379                             /*  added. The str field is null-terminated and so can        */
380                             /*  be used as an ordinary C string.                          */
381     gsize len;              /* strlen: ie: length of str not including trailing '\0'      */
382     gsize alloc_len;        /* num bytes curently allocated for str: 1 .. MAX_STRBUF_LEN  */
383     gsize max_alloc_len;    /* max num bytes to allocate for str: 1 .. MAX_STRBUF_LEN     */
384 } emem_strbuf_t;
385
386 /*
387  * The maximum length is limited to 64K. If you need something bigger, you
388  * should probably use an actual GString or GByteArray.
389  */
390
391 /**
392  * Allocate an ephemeral string buffer with "unlimited" size.
393  *
394  * @param init The initial string for the buffer, or NULL to allocate an initial zero-length string.
395  *
396  * @return A newly-allocated string buffer.
397  */
398 emem_strbuf_t *ep_strbuf_new(const gchar *init) G_GNUC_MALLOC;
399
400 /**
401  * Allocate an ephemeral string buffer suitable for the protocol tree.
402  * The string will never grow beyond the maximum tree item length.
403  *
404  * @param init The initial string for the buffer, or NULL to allocate an initial zero-length string.
405  *
406  * @return A newly-allocated string buffer.
407  */
408 emem_strbuf_t *ep_strbuf_new_label(const gchar *init) G_GNUC_MALLOC;
409
410 /**
411  * Allocate an ephemeral string buffer with enough initial space for @alloc_len bytes
412  * and a maximum of @max_alloc_len bytes.
413  *
414  * @param alloc_len The initial size of the buffer. This value can be 0, but a nonzero
415  * value is recommended.
416  * @param max_alloc_len The maximum size of the buffer. 0 means "unlimited" (within
417  * reason).
418  *
419  * @return A newly-allocated string buffer. @str will be empty.
420  */
421 emem_strbuf_t *ep_strbuf_sized_new(gsize alloc_len, gsize max_alloc_len) G_GNUC_MALLOC;
422
423 /**
424  * Append vprintf-style formatted text to a string buffer.
425  *
426  * @param strbuf The ep_strbuf-allocated string buffer to append to.
427  * @param format A printf-style string format.
428  * @param args The list of arguments to append.
429  */
430 void ep_strbuf_append_vprintf(emem_strbuf_t *strbuf, const gchar *format, va_list ap);
431
432 /**
433  * Apply printf-style formatted text to a string buffer.
434  *
435  * @param strbuf The ep_strbuf-allocated string buffer to set to.
436  * @param format A printf-style string format.
437  */
438 void ep_strbuf_printf(emem_strbuf_t *strbuf, const gchar *format, ...)
439      G_GNUC_PRINTF(2, 3);
440
441 /**
442  * Append printf-style formatted text to a string buffer.
443  *
444  * @param strbuf The ep_strbuf-allocated string buffer to append to.
445  * @param format A printf-style string format.
446  */
447 void ep_strbuf_append_printf(emem_strbuf_t *strbuf, const gchar *format, ...)
448     G_GNUC_PRINTF(2, 3);
449
450 /**
451  * Append a string to a string buffer.
452  *
453  * @param strbuf The ep_strbuf-allocated string buffer to append to.
454  * @param str A null-terminated string.
455  *
456  * @return strbuf
457  */
458 emem_strbuf_t *ep_strbuf_append(emem_strbuf_t *strbuf, const gchar *str);
459
460 /**
461  * Append a character to a string buffer.
462  *
463  * @param strbuf The ep_strbuf-allocated string buffer to append to.
464  * @param c The character to append.
465  *
466  * @return strbuf
467  */
468 emem_strbuf_t *ep_strbuf_append_c(emem_strbuf_t *strbuf, const gchar c);
469
470 /**
471  * Chop off the end of a string buffer.
472  *
473  * @param strbuf The ep_strbuf-allocated string buffer to append to.
474  * @param len The new string length.
475  *
476  * @return strbuf
477  */
478 emem_strbuf_t *ep_strbuf_truncate(emem_strbuf_t *strbuf, gsize len);
479
480 void emem_print_tree(emem_tree_t* emem_tree);
481
482 /* #define DEBUG_INTENSE_CANARY_CHECKS */
483
484 /* Helper to troubleshoot ep memory corruption.
485  * If compiled and the environment variable WIRESHARK_DEBUG_EP_INTENSE_CANARY exists
486  * it will check the canaries and when found corrupt stop there in the hope
487  * the corruptor is still there in the stack.
488  * Some checkpoints are already set in packet.c in strategic points
489  * before and after dissection of a frame or a dissector call.
490  */
491
492 #ifdef DEBUG_INTENSE_CANARY_CHECKS
493 void ep_check_canary_integrity(const char* fmt, ...)
494     G_GNUC_PRINTF(1, 2);
495 #define EP_CHECK_CANARY(args) ep_check_canary_integrity args
496 #else
497 #define EP_CHECK_CANARY(args)
498 #endif
499
500 /**
501  * Verify that the given pointer is of ephemeral/seasonal type.
502  *
503  * @param ptr The pointer to verify
504  *
505  * @return TRUE if the pointer belongs to the ephemeral/seasonal pool.
506  */
507 gboolean ep_verify_pointer(const void *ptr);
508 gboolean se_verify_pointer(const void *ptr);
509
510 #endif /* emem.h */