2 * Definitions for the Wireshark Memory Manager Red-Black Tree
3 * Based on the red-black tree implementation in epan/emem.*
4 * Copyright 2013, Evan Huus <eapache@gmail.com>
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #ifndef __WMEM_TREE_H__
28 #define __WMEM_TREE_H__
30 #include "wmem_core.h"
34 #endif /* __cplusplus */
38 * @defgroup wmem-tree Red/Black Tree
40 * Binary trees are a well-known and popular device in computer science to
41 * handle storage of objects based on a search key or identity. The
42 * particular binary tree style implemented here is the red/black tree, which
43 * has the nice property of being self-balancing. This guarantees O(log(n))
44 * time for lookups, compared to linked lists that are O(n). This means
45 * red/black trees scale very well when many objects are being stored.
51 typedef struct _wmem_tree_t wmem_tree_t
;
53 /** Creates a tree with the given allocator scope. When the scope is emptied,
54 * the tree is fully destroyed. */
57 wmem_tree_new(wmem_allocator_t
*allocator
)
60 /** Creates a tree with two allocator scopes. The base structure lives in the
61 * master scope, however the data lives in the slave scope. Every time free_all
62 * occurs in the slave scope the tree is transparently emptied without affecting
63 * the location of the master structure.
65 * WARNING: None of the tree (even the part in the master scope) can be used
66 * after the slave scope has been *destroyed*.
68 * The primary use for this function is to create trees that reset for each new
69 * capture file that is loaded. This can be done by specifying wmem_epan_scope()
70 * as the master and wmem_file_scope() as the slave.
74 wmem_tree_new_autoreset(wmem_allocator_t
*master
, wmem_allocator_t
*slave
)
77 /** Returns true if the tree is empty (has no nodes). */
80 wmem_tree_is_empty(wmem_tree_t
*tree
);
82 /** Insert a node indexed by a guint32 key value.
84 * Data is a pointer to the structure you want to be able to retrieve by
85 * searching for the same key later.
87 * NOTE: If you insert a node to a key that already exists in the tree this
88 * function will simply overwrite the old value. If the structures you are
89 * storing are allocated in a wmem pool this is not a problem as they will still
90 * be freed with the pool. If you are managing them manually however, you must
91 * either ensure the key is unique, or do a lookup before each insert.
95 wmem_tree_insert32(wmem_tree_t
*tree
, guint32 key
, void *data
);
97 /** Look up a node in the tree indexed by a guint32 integer value. If no node is
98 * found the function will return NULL.
102 wmem_tree_lookup32(wmem_tree_t
*tree
, guint32 key
);
104 /** Look up a node in the tree indexed by a guint32 integer value.
105 * Returns the node that has the largest key that is less than or equal
106 * to the search key, or NULL if no such key exists.
110 wmem_tree_lookup32_le(wmem_tree_t
*tree
, guint32 key
);
112 /** case insensitive strings as keys */
113 #define WMEM_TREE_STRING_NOCASE 0x00000001
114 /** Insert a new value under a string key. Like wmem_tree_insert32 but where the
115 * key is a null-terminated string instead of a guint32. You may pass
116 * WMEM_TREE_STRING_NOCASE to the flags argument in order to make it store the
117 * key in a case-insensitive way. */
120 wmem_tree_insert_string(wmem_tree_t
*tree
, const gchar
* key
, void *data
,
123 /** Lookup the value under a string key, like wmem_tree_lookup32 but where the
124 * keye is a null-terminated string instead of a guint32. See
125 * wmem_tree_insert_string for an explanation of flags. */
128 wmem_tree_lookup_string(wmem_tree_t
* tree
, const gchar
* key
, guint32 flags
);
130 typedef struct _wmem_tree_key_t
{
131 guint32 length
; /**< length in guint32 words */
135 /** Insert a node indexed by a sequence of guint32 key values.
137 * Takes as key an array of guint32 vectors of type wmem_tree_key_t. It will
138 * iterate through each key to search further down the tree until it reaches an
139 * element where length==0, indicating the end of the array. You MUST terminate
140 * the key array by {0, NULL} or this will crash.
142 * NOTE: length indicates the number of guint32 values in the vector, not the
145 * NOTE: all the "key" members of the "key" argument MUST be aligned on
146 * 32-bit boundaries; otherwise, this code will crash on platforms such
147 * as SPARC that require aligned pointers.
149 * If you use ...32_array() calls you MUST make sure that every single node
150 * you add to a specific tree always has a key of exactly the same number of
151 * keylen words or it will crash. Or at least that every single item that sits
152 * behind the same top level node always has exactly the same number of words.
154 * One way to guarantee this is the way that NFS does this for the
155 * nfs_name_snoop_known tree which holds filehandles for both v2 and v3.
156 * v2 filehandles are always 32 bytes (8 words) while v3 filehandles can have
157 * any length (though 32 bytes are most common).
158 * The NFS dissector handles this by providing a guint32 containing the length
159 * as the very first item in this vector :
161 * wmem_tree_key_t fhkey[3];
163 * fhlen=nns->fh_length;
165 * fhkey[0].key=&fhlen;
166 * fhkey[1].length=fhlen/4;
167 * fhkey[1].key=nns->fh;
172 wmem_tree_insert32_array(wmem_tree_t
*tree
, wmem_tree_key_t
*key
, void *data
);
174 /** Look up a node in the tree indexed by a sequence of guint32 integer values.
175 * See wmem_tree_insert32_array for details on the key.
179 wmem_tree_lookup32_array(wmem_tree_t
*tree
, wmem_tree_key_t
*key
);
181 /** Look up a node in the tree indexed by a multi-part tree value.
182 * The function will return the node that has the largest key that is
183 * equal to or smaller than the search key, or NULL if no such key was
186 * NOTE: The key returned will be "less" in key order. The usefulness
187 * of the returned node must be verified prior to use.
189 * See wmem_tree_insert32_array for details on the key.
193 wmem_tree_lookup32_array_le(wmem_tree_t
*tree
, wmem_tree_key_t
*key
);
195 /** Function type for processing one node of a tree during a traversal. Value is
196 * the value of the node, userdata is whatever was passed to the traversal
197 * function. If the function returns TRUE the traversal will end prematurely.
199 typedef gboolean (*wmem_foreach_func
)(void *value
, void *userdata
);
201 /** Traverse the tree and call callback(value, userdata) for each value found.
202 * Returns TRUE if the traversal was ended prematurely by the callback.
206 wmem_tree_foreach(wmem_tree_t
* tree
, wmem_foreach_func callback
,
209 /** Prints the structure of the tree to stdout. Primarily for debugging. */
211 wmem_print_tree(wmem_tree_t
*tree
);
218 #endif /* __cplusplus */
220 #endif /* __WMEM_TREE_H__ */
223 * Editor modelines - http://www.wireshark.org/tools/modelines.html
228 * indent-tabs-mode: nil
231 * vi: set shiftwidth=4 tabstop=8 expandtab:
232 * :indentSize=4:tabSize=8:noTabs=true: