1
2/*--------------------------------------------------------------------*/
3/*--- OSet: a fast data structure with no dups.    pub_tool_oset.h ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7   This file is part of Valgrind, a dynamic binary instrumentation
8   framework.
9
10   Copyright (C) 2005-2013 Nicholas Nethercote
11      njn@valgrind.org
12
13   This program is free software; you can redistribute it and/or
14   modify it under the terms of the GNU General Public License as
15   published by the Free Software Foundation; either version 2 of the
16   License, or (at your option) any later version.
17
18   This program is distributed in the hope that it will be useful, but
19   WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21   General Public License for more details.
22
23   You should have received a copy of the GNU General Public License
24   along with this program; if not, write to the Free Software
25   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26   02111-1307, USA.
27
28   The GNU General Public License is contained in the file COPYING.
29*/
30
31#ifndef __PUB_TOOL_OSET_H
32#define __PUB_TOOL_OSET_H
33
34#include "pub_tool_basics.h"   // Word
35
36// This module implements an ordered set, a data structure with fast
37// (eg. amortised log(n) or better) insertion, lookup and deletion of
38// elements.  It does not allow duplicates, and will assert if you insert a
39// duplicate to an OSet.
40//
41// It has two interfaces.
42//
43// - The "OSetWord_" interface provides an easier-to-use interface for the
44//   case where you just want to store UWord-sized values.  The user
45//   provides the allocation and deallocation functions, and possibly a
46//   comparison function.
47//
48// - The "OSetGen_" interface provides a totally generic interface, which
49//   allows any kind of structure to be put into the set.  The user provides
50//   the allocation and deallocation functions.  Also, each element has a
51//   key, which the lookup is done with.  The key may be the whole element
52//   (eg. in an OSet of integers, each integer serves both as an element and
53//   a key), or it may be only part of it (eg. if the key is a single field
54//   in a struct).  The user can provide a function that compares an element
55//   with a key;  this is very flexible, and with the right comparison
56//   function even a (non-overlapping) interval list can be created.  But
57//   the cost of calling a function for every comparison can be high during
58//   lookup.  If no comparison function is provided, we assume that keys are
59//   unsigned words, and that the key is the first word in each
60//   element.  This fast comparison is suitable for an OSet containing
61//   structs where the first element is an Addr, for example.
62//   Do not assume fast comparison works properly with signed words.
63//   A.o. iterating over the values will not return them in the correct
64//   order.
65//
66// Each OSet interface also has an iterator, which makes it simple to
67// traverse all the nodes in order.  Note that the iterator maintains state
68// and so is non-reentrant.
69//
70// Note that once you insert an element into an OSet, if you modify any part
71// of it looked at by your cmp() function, this may cause incorrect
72// behaviour as the sorted order maintained will be wrong.
73
74/*--------------------------------------------------------------------*/
75/*--- Types                                                        ---*/
76/*--------------------------------------------------------------------*/
77
78typedef struct _OSet     OSet;
79
80// - Cmp:   returns -1, 0 or 1 if key is <, == or > elem.
81// - Alloc: allocates a chunk of memory.
82// - Free:  frees a chunk of memory allocated with Alloc.
83
84typedef Word  (*OSetCmp_t)         ( const void* key, const void* elem );
85typedef void* (*OSetAlloc_t)       ( const HChar* cc, SizeT szB );
86typedef void  (*OSetFree_t)        ( void* p );
87
88/*--------------------------------------------------------------------*/
89/*--- Creating and destroying OSets (UWord)                        ---*/
90/*--------------------------------------------------------------------*/
91
92// * Create: allocates and initialises the OSet.  Arguments:
93//   - alloc     The allocation function used internally for allocating the
94//               OSet and all its nodes.
95//   - cc        Cost centre string used by 'alloc'.
96//   - free      The deallocation function used internally for freeing nodes
97//               called by VG_(OSetWord_Destroy)().
98//
99// * CreateWithCmp: like Create, but you specify your own comparison
100//   function.
101//
102// * Destroy: frees all nodes in the table, plus the memory used by
103//   the table itself.  The passed-in function is called on each node first
104//   to allow the destruction of any attached resources;  if NULL it is not
105//   called.
106
107extern OSet* VG_(OSetWord_Create)       ( OSetAlloc_t alloc, const HChar* cc,
108                                          OSetFree_t _free );
109extern void  VG_(OSetWord_Destroy)      ( OSet* os );
110
111/*--------------------------------------------------------------------*/
112/*--- Operations on OSets (UWord)                                  ---*/
113/*--------------------------------------------------------------------*/
114
115// In everything that follows, the parameter 'key' is always the *address*
116// of the key, and 'elem' is *address* of the elem, as are the return values
117// of the functions that return elems.
118//
119// * Size: The number of elements in the set.
120//
121// * Contains: Determines if the value is in the set.
122//
123// * Insert: Inserts a new element into the set.  Duplicates are forbidden,
124//   and will cause assertion failures.
125//
126// * Remove: Removes the value from the set, if present.  Returns a Bool
127//   indicating if the value was removed.
128//
129// * ResetIter: Each OSet has an iterator.  This resets it to point to the
130//   first element in the OSet.
131//
132// * Next: Copies the next value according to the OSet's iterator into &val,
133//   advances the iterator by one, and returns True;  the elements are
134//   visited in increasing order of unsigned words (UWord).  Or, returns
135//   False if the iterator has reached the set's end.
136//
137//   You can thus iterate in order through a set like this:
138//
139//     Word val;
140//     VG_(OSetWord_ResetIter)(oset);
141//     while ( VG_(OSetWord_Next)(oset, &val) ) {
142//        ... do stuff with 'val' ...
143//     }
144//
145//   Note that iterators are cleared any time an element is inserted or
146//   removed from the OSet, to avoid possible mayhem caused by the iterator
147//   getting out of sync with the OSet's contents.  "Cleared" means that
148//   they will return False if VG_(OSetWord_Next)() is called without an
149//   intervening call to VG_(OSetWord_ResetIter)().
150
151extern Word  VG_(OSetWord_Size)         ( OSet* os );
152extern void  VG_(OSetWord_Insert)       ( OSet* os, UWord val );
153extern Bool  VG_(OSetWord_Contains)     ( OSet* os, UWord val );
154extern Bool  VG_(OSetWord_Remove)       ( OSet* os, UWord val );
155extern void  VG_(OSetWord_ResetIter)    ( OSet* os );
156extern Bool  VG_(OSetWord_Next)         ( OSet* os, /*OUT*/UWord* val );
157
158
159/*--------------------------------------------------------------------*/
160/*--- Creating and destroying OSets and OSet members (Gen)         ---*/
161/*--------------------------------------------------------------------*/
162
163// * Create: allocates and initialises the OSet.  Arguments:
164//   - keyOff    The offset of the key within the element.
165//   - cmp       The comparison function between keys and elements, or NULL
166//               if the OSet should use fast comparisons.
167//   - alloc     The allocation function used for allocating the OSet itself;
168//               If a pool allocator is used, it's called to allocate pool of
169//               nodes.
170//               If no pool allocator is used, it's called for each
171//               invocation of VG_(OSetGen_AllocNode)().
172//   - cc        Cost centre string used by 'alloc'.
173//   - free      If no pool allocator is used, this is the deallocation
174//               function used by VG_(OSetGen_FreeNode)() and
175//               VG_(OSetGen_Destroy)().
176//               If a pool allocator is used, the memory used by the nodes is
177//               deallocated when the pool is deleted.
178//   (for more details about pool allocators, see pub_tool_poolalloc.h).
179//
180//
181//   If cmp is NULL, keyOff must be zero.  This is checked.
182//
183// * Destroy: frees all nodes in the table, plus the memory used by
184//   the table itself.  The passed-in function is called on each node first
185//   to allow the destruction of any attached resources;  if NULL it is not
186//   called.
187//
188// * AllocNode: Allocate and zero memory for a node to go into the OSet.
189//   If a pool allocator is used, it uses the pool allocator to allocate a node.
190//   Otherwise, uses the alloc function given to VG_(OSetGen_Create)() to
191//   allocate a node which is big enough for both an element and the OSet
192//   metadata.
193//   Not all elements in one OSet have to be the same size.
194//   However, if a pool allocator is used, elements will all have a size equal
195//   to the max user data size given at creation + the node meta data size.
196//
197//   Note that the element allocated will be at most word-aligned, which may
198//   be less aligned than the element type would normally be.
199//
200// * FreeNode: Deallocate a node allocated with OSetGen_AllocNode().  Using
201//   a deallocation function (such as VG_(free)()) directly will likely
202//   lead to assertions in Valgrind's allocator.
203
204extern OSet* VG_(OSetGen_Create)    ( PtrdiffT keyOff, OSetCmp_t cmp,
205                                      OSetAlloc_t alloc, const HChar* cc,
206                                      OSetFree_t _free);
207
208
209extern OSet* VG_(OSetGen_Create_With_Pool)    ( PtrdiffT keyOff, OSetCmp_t cmp,
210                                                OSetAlloc_t alloc,
211                                                const HChar* cc,
212                                                OSetFree_t _free,
213                                                SizeT poolSize,
214                                                SizeT maxEltSize);
215// Same as VG_(OSetGen_Create) but created OSet will use a pool allocator to
216// allocate the nodes.
217// The node size is the sum of a fixed small meta data size needed for OSet
218// + the size of the user data element.
219// The maximum size for the user data element is specified by maxEltSize.
220// (if poolSize is 0, maxEltSize is not relevant for the OSet).
221// It is interesting to use a pool allocator when an OSet has many elements,
222// and these elements have a small fixed size, or have a variable size, but
223// always <= than a (small) maximum value.
224// In such a case, allocating the nodes in pools reduces significantly
225// the memory overhead needed by each node.
226// When a node is freed (i.e. OsetGen_Freenode is called), the node is
227// put back in the pool allocator free list (for sub-sequent re-use by
228// Osetgen_Allocnode). Note that the pool memory is only released when
229// the pool is destroyed : calls to VG_(OSetGen_Free) do not cause
230// any calls to OsetFree_t _free function.
231// If there are several OSet managing similar such elements, it might be
232// interesting to use a shared pool for these OSet.
233// To have multiple OSets sharing a pool allocator, create the first OSet
234// with VG_(OSetGen_Create_With_Pool). Create subsequent OSet with
235// VG_(OSetGen_EmptyClone).
236
237extern void  VG_(OSetGen_Destroy)   ( OSet* os );
238extern void* VG_(OSetGen_AllocNode) ( OSet* os, SizeT elemSize );
239extern void  VG_(OSetGen_FreeNode)  ( OSet* os, void* elem );
240
241extern OSet* VG_(OSetGen_EmptyClone) (OSet* os);
242// Creates a new empty OSet.
243// The new OSet will have the same characteristics as os.
244// If os uses a pool allocator, this pool allocator will be shared with
245// the new OSet. A shared pool allocator is only deleted (and its memory is
246// released) when the last OSet using the shared pool is destroyed.
247
248/*-------------------------------------------------------------------*/
249/*--- Operations on OSets (Gen)                                    ---*/
250/*--------------------------------------------------------------------*/
251
252// In everything that follows, the parameter 'key' is always the *address*
253// of the key, and 'elem' is *address* of the elem, as are the return values
254// of the functions that return elems.
255//
256// * Size: The number of elements in the set.
257//
258// * Insert: Inserts a new element into the set.  Note that 'elem' must
259//   have been allocated using VG_(OSetGen_AllocNode)(), otherwise you will
260//   get assertion failures about "bad magic".  Duplicates are forbidden,
261//   and will also cause assertion failures.
262//
263// * Contains: Determines if any element in the OSet matches the key.
264//
265// * Lookup: Returns a pointer to the element matching the key, if there is
266//   one, otherwise returns NULL.
267//
268// * LookupWithCmp: Like Lookup, but you specify the comparison function,
269//   which overrides the OSet's normal one.
270//
271// * Remove: Removes the element matching the key, if there is one.  Returns
272//   NULL if no element matches the key.
273//
274// * ResetIter: Each OSet has an iterator.  This resets it to point to the
275//   first element in the OSet.
276//
277// * ResetIterAt: Like ResetIter, but instead of resetting the iterator to the
278//   smallest element, it resets the iterator to point to the smallest element
279//   in the set whose key is greater-than-or-equal to the given key.  (In many
280//   cases this will be the element whose key equals that of the given key.)
281//
282// * Next: Returns a pointer to the element pointed to by the OSet's
283//   iterator, and advances the iterator by one;  the elements are visited
284//   in order.  Or, returns NULL if the iterator has reached the OSet's end.
285//
286//   You can thus iterate in order through a set like this:
287//
288//     VG_(OSetGen_ResetIter)(oset);
289//     while ( (elem = VG_(OSetGen_Next)(oset)) ) {
290//        ... do stuff with 'elem' ...
291//     }
292//
293//   Note that iterators are cleared any time an element is inserted or
294//   removed from the OSet, to avoid possible mayhem caused by the iterator
295//   getting out of sync with the OSet's contents.  "Cleared" means that
296//   they will return NULL if VG_(OSetGen_Next)() is called without an
297//   intervening call to VG_(OSetGen_ResetIter)().
298
299extern Word  VG_(OSetGen_Size)         ( const OSet* os );
300extern void  VG_(OSetGen_Insert)       ( OSet* os, void* elem );
301extern Bool  VG_(OSetGen_Contains)     ( const OSet* os, const void* key );
302extern void* VG_(OSetGen_Lookup)       ( const OSet* os, const void* key );
303extern void* VG_(OSetGen_LookupWithCmp)( OSet* os,
304                                         const void* key, OSetCmp_t cmp );
305extern void* VG_(OSetGen_Remove)       ( OSet* os, const void* key );
306extern void  VG_(OSetGen_ResetIter)    ( OSet* os );
307extern void  VG_(OSetGen_ResetIterAt)  ( OSet* os, const void* key );
308extern void* VG_(OSetGen_Next)         ( OSet* os );
309
310
311#endif   // __PUB_TOOL_OSET_H
312
313/*--------------------------------------------------------------------*/
314/*--- end                                                          ---*/
315/*--------------------------------------------------------------------*/
316