Fix a couple of typos and use 2 spaces consistently after a period.
[obnox/wireshark/wip.git] / doc / README.binarytrees
1 $Id$
2
3 1. Introduction
4
5 Binary trees are a well known and popular device in computer science to handle
6 storage of object based on a search key or identity.
7 One particular class of binary trees are Red/Black trees which have nice 
8 properties such as being self-balanced.
9 Such trees are often very fast for accessing data, and may average O(log(n))
10 for lookups, compared to linked lists that are of order O(n).
11
12 Benefits of using binary trees are that they are incredibly fast for 
13 accessing data and they scale very well with good characteristics even to
14 very large numbers of objects.
15
16 Wireshark provides its own version of red black binary trees designed in 
17 particular to be easy to use and to eliminate most of the memory management
18 often associated with such trees.
19
20 The trees supported by wireshark are currently all created using SEasonal 
21 storage which means that when you load a new trace into wireshark, the SEasonal
22 memory management will automatically release every single byte of data
23 associated with the tree.
24
25 Please see README.malloc for a description of EPhemeral and SEasonal storage.
26
27
28 2. Basic Usage
29 For most users it will be sufficient to only know and use three functions
30 emem_tree_t *se_tree_create(int type, char *name);
31 void se_tree_insert32(emem_tree_t *se_tree, guint32 key, void *data);
32 void *se_tree_lookup32(emem_tree_t *se_tree, guint32 key);
33
34
35 2.1 se_tree_create(int type, char *name);
36 se_tree_create() is used to initialize a tree that will be automatically 
37 cleared and reset every time wireshark is resetting all SEasonal storage.
38 That is every time you load a new capture file into wireshark or when
39 you rescan the entire capture file from scratch.
40
41 Name is just a literal text string and serves no other purpose than making
42 debugging of the trees easier. Specify a name here that uniquely identifies
43 both the protocol you create the tree for and its purpose.
44
45
46 This function is most likely called once from your 
47 proto_register_<yourprotocol>() function.
48
49 Example (from packet-tcp.c):
50 #include <epan/emem.h>
51 ...
52 static emem_tree_t *tcp_pdu_time_table = NULL;
53 ...
54 void proto_register_...(void) {
55    ...
56    tcp_pdu_time_table=se_tree_create(EMEM_TREE_TYPE_RED_BLACK, "PROTO_mytree");
57    ...
58 }
59
60 That is how easy it is to create a binary tree. You only need to create it
61 once when wireshark starts and the tree will remain there until you exit
62 wireshark.  Every time a new capture is loaded, all nodes allocated to the
63 tree are freed automatically and the tree is reset without you having to do
64 anything at all.
65
66
67 2.2 se_tree_insert32(emem_tree_t *se_tree, guint32 key, void *data);
68 This function is used to add a new node into the tree. 
69 This function is used for such trees where you always use a guint32 as the 
70 identifier/key for the node. Most trees you want to use are likely in this 
71 category.
72
73 As data you should specify a pointer to the data structure you want to be
74 able to retrieve later when you look for that same key.
75
76 NOTE: If you insert a node to a key that already exists in the tree
77 this function will allow you to do that. It will just drop the old pointer
78 to data and replace it with the new one you just provided.
79 This should not be a problem as long as the old and the new data blobs
80 are se_allocated() since you cant free() such memory explicitly anyway
81 and the old one will be release automatically anyway when the SEasonal
82 system reclaims all the SE data.
83
84 NOTE: It is a good idea to only provide data that point to blobs allocated
85 by se_alloc(). By doing that you will have a tree where the tree and all
86 the data pointed to will be automatically released by the system at the
87 same time.  This is very neat and makes it real difficult to have memory
88 leaks in your code.
89
90 NOTE: When you insert items in the tree, it is very likely that you only
91 want to add any data to the tree during the very first time you process
92 a particular packet.
93 Wireshark may reprocess the same packet multiple times afterward by the user
94 clicking on the packet or for other reasons. 
95 You probably DO want to protect the insert call within an if statement such
96 as
97
98 Example:
99     /* only TRUE first time we process this packet*/
100     if(!pinfo->fd->flags.visited){ 
101        ...
102        data=se_alloc(...);
103        data->...
104        ...
105        se_tree_insert32(se_tree, key, data);
106        ...
107     }
108
109 Please do think about how and when you want to add items to the tree.
110 If you don't think you should use the if statement to protect the insert
111 please reconsider and think it through one extra time.
112
113
114 2.3 se_tree_lookup32(emem_tree_t *se_tree, guint32 key);
115 This function is used to read back from the tree the data value that is 
116 associated with this key.
117 If no such node was found the function will return NULL.
118
119 Example:
120     ...
121     data_structure = se_tree_lookup32(se_tree, key);
122     if(data_structure){
123       ...
124     }
125
126 Don't forget to check that the returned pointer is non-NULL before you 
127 dereference it, please.
128
129 2.4 se_tree_lookup32_le(emem_tree_t *se_tree, guint32 key);
130 The function will return the node that has the largest key that is
131 equal to or smaller than the search key, or NULL if no such key was found.
132
133
134 Simple as that, can it be easier?
135
136
137 3. Advanced Usage
138 This will list some of the more unconventional and hopefully rarely used 
139 functions.
140
141 3.1 se_tree_create_non_persistent(int type, char *name);
142 Sometimes you don't want a tree that is automatically reset to become an empty
143 tree. You might want a tree that is completely destroyed once the next
144 capture file is read and even the pointer to the tree itself becomes invalid.
145
146 This would most likely be the case when you do NOT want a global tree
147 but instead a tree that is held inside some other SE allocated structure.
148 So that when that encapsulating structure is released the entire tree will
149 disappear completely as well.
150
151 Maybe you have a normal tree to track all conversations for your protocol
152 and for each conversation you se_alloc() a structure to maintain some 
153 data about that conversation. Assume you want to add to that structure
154 another binary tree   a binary tree that is private to that structure/
155 conversation to hold some other data.
156 In that case, this is the function you want.
157
158
159 3.2 se_tree_insert32_array / se_tree_lookup32_array
160 typedef struct _emem_tree_key_t {
161         guint32 length;                 /*length in guint32 words */
162         guint32 *key;
163 } emem_tree_key_t;
164 void se_tree_insert32_array(emem_tree_t *se_tree, emem_tree_key_t *key, void *data);
165 void *se_tree_lookup32_array(emem_tree_t *se_tree, emem_tree_key_t *key);
166
167 NOTE: the *key parameter taken by these functions WILL be modified by these
168 functions so if you want to reuse the key for a second call you will have
169 to reinitialize key.
170
171
172 These functions are used to insert and lookup a tree where nodes are NOT 
173 indexed by a single guint32 but more like an array of guint32 elements.
174
175 These functions take as key an array of guint32 vectors : emem_tree_key_t.
176 The functions will use vector by vector to search further down the tree 
177 until an array element where length==0 is found indicating the end of the 
178 array.
179
180 NOTE: you MUST terminate the emem_tree_key_t array by {0, NULL}
181 If you forget to do this wireshark will immediately crash.
182
183 NOTE: length indicates the number of guint32 values in the vector, not the
184 number of bytes.
185
186 If your keys are always of exactly the same length, always, and you are willing
187 to bet that there can never ever be a key of a different length you can
188 get away with a simple :
189   emem_tree_key_t key[2];
190   key[0].length= LEN;
191   key[0].key= KEY;
192   key[1].length=0;
193   key[1].key=NULL;
194   se_tree_insert32_array(se_tree, &key[0], data);
195 But IF you would accidentally pass a key with a different number of guint32s
196 in its vectors to this same se_tree  you will crash.
197 Don't use key like this. Please.
198
199
200 Instead use this simple workaround to make it all safe :
201 Specify the first index as 1 guint32 holding the total number of guints in the
202 rest of the key.
203 See NFS that does this to handle file handles that may be of different lengths
204 in the same tree :
205   emem_tree_key_t fhkey[3];
206   guint32 tmplen;
207
208   tmplen = <length of filehandle/4>;
209   fhkey[0].length = 1;
210   fhkey[0].key    = &tmplen;
211   fhkey[1].length = tmplen;
212   fhkey[1].key    = <filehandle>;
213   fhkey[2].length = 0;
214   fhkey[2].key    = NULL;
215
216 ( /4 since we specify the length here in number of guint32 not number of bytes)
217
218 3.3 se_tree_lookup32_array_le(emem_tree_t *se_tree, emem_tree_key_t *key);
219 Much like the se_tree_lookup32_le, this function will return the node that has
220 the largest key that is equal to or smaller than the search key, or NULL if
221 no such key was found.
222
223 When using _array_ trees, the tree that is created is a "tree of trees" where the
224 last leaf has the indexed data.  Thus if you have 3 keys in the emme_tree_key_t
225 structure, the "1st" tree indexes key[0].  Each node in this tree points to a
226 tree indexed using key[1].  The nodes of the final tree will contain the data.
227
228 This construction must be taken into account when using se_tree_lookup32_array_le.
229 The program should verify that the node returned contains data that is expected.
230
231 3.4 se_tree_insert_string / se_tree_lookup_string
232 void emem_tree_insert_string(emem_tree_t* h, const gchar* k, void* v, guint32 flags);
233 void* emem_tree_lookup_string(emem_tree_t* h, const gchar* k, guint32 flags);
234 These functions are essentially wrappers for se_tree_insert32_array and
235 se_tree_lookup32_array, tailored to text strings. They extend the text string
236 into an array key and use that to key the se_tree_insert32_array and
237 se_tree_lookup32_array functions.
238 In order to support text string in a case insensitive way add the
239 EMEM_TREE_STRING_NOCASE flag. This will uppercase all string data before using
240 it as key data.
241