blob: e96274047a523b3363125ab5ffa55405d5202725 [file] [log] [blame]
njne1b2b962005-08-14 22:13:00 +00001
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
sewardj9ebd6e02007-01-08 06:01:59 +000010 Copyright (C) 2005-2007 Nicholas Nethercote
sewardjdff46d52006-10-17 01:40:33 +000011 njn@valgrind.org
njne1b2b962005-08-14 22:13:00 +000012
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// This module implements an ordered set, a data structure with fast
35// (eg. amortised log(n) or better) insertion, lookup and deletion of
36// elements. It does not allow duplicates, and will assert if you insert a
37// duplicate to an OSet.
38//
njne2a9ad32007-09-17 05:30:48 +000039// It has two interfaces.
njne1b2b962005-08-14 22:13:00 +000040//
njne2a9ad32007-09-17 05:30:48 +000041// - The "OSetWord_" interface provides an easier-to-use interface for the
42// case where you just want to store Word-sized values. The user provides
43// the allocation and deallocation functions, and possibly a comparison
44// function.
45//
46// - The "OSetGen_" interface provides a totally generic interface, which
47// allows any kind of structure to be put into the set. The user provides
48// the allocation and deallocation functions. Also, each element has a
49// key, which the lookup is done with. The key may be the whole element
50// (eg. in an OSet of integers, each integer serves both as an element and
51// a key), or it may be only part of it (eg. if the key is a single field
52// in a struct). The user can provide a function that compares an element
53// with a key; this is very flexible, and with the right comparison
54// function even a (non-overlapping) interval list can be created. But
55// the cost of calling a function for every comparison can be high during
56// lookup. If no comparison function is provided, we assume that keys are
57// (signed or unsigned) words, and that the key is the first word in each
58// element. This fast comparison is suitable for an OSet containing
59// structs where the first element is an Addr, for example.
60//
61// Each OSet interface also has an iterator, which makes it simple to
62// traverse all the nodes in order. Note that the iterator maintains state
63// and so is non-reentrant.
njne1b2b962005-08-14 22:13:00 +000064//
65// Note that once you insert an element into an OSet, if you modify any part
66// of it looked at by your cmp() function, this may cause incorrect
67// behaviour as the sorted order maintained will be wrong.
68
69/*--------------------------------------------------------------------*/
70/*--- Types ---*/
71/*--------------------------------------------------------------------*/
72
73typedef struct _OSet OSet;
njne2a9ad32007-09-17 05:30:48 +000074
sewardj9bc8d9e2007-12-09 02:08:42 +000075// - Cmp: returns -1, 0 or 1 if key is <, == or > elem.
njne2a9ad32007-09-17 05:30:48 +000076// - Alloc: allocates a chunk of memory.
77// - Free: frees a chunk of memory allocated with Alloc.
njne1b2b962005-08-14 22:13:00 +000078
tom5a835d52007-12-30 12:28:26 +000079typedef Word (*OSetCmp_t) ( const void* key, const void* elem );
njne004d722005-12-22 06:20:59 +000080typedef void* (*OSetAlloc_t) ( SizeT szB );
81typedef void (*OSetFree_t) ( void* p );
njne1b2b962005-08-14 22:13:00 +000082
83/*--------------------------------------------------------------------*/
njne2a9ad32007-09-17 05:30:48 +000084/*--- Creating and destroying OSets (Word) ---*/
85/*--------------------------------------------------------------------*/
86
87// * Create: allocates and initialises the OSet. Arguments:
88// - alloc The allocation function used internally for allocating the
89// OSet and all its nodes.
90// - free The deallocation function used internally for freeing nodes
91// called by VG_(OSetWord_Destroy)().
92//
93// * CreateWithCmp: like Create, but you specify your own comparison
94// function.
95//
96// * Destroy: frees all nodes in the table, plus the memory used by
97// the table itself. The passed-in function is called on each node first
98// to allow the destruction of any attached resources; if NULL it is not
99// called.
100
101extern OSet* VG_(OSetWord_Create) ( OSetAlloc_t alloc, OSetFree_t free );
102extern void VG_(OSetWord_Destroy) ( OSet* os );
103
104/*--------------------------------------------------------------------*/
105/*--- Operations on OSets (Word) ---*/
106/*--------------------------------------------------------------------*/
107
108// In everything that follows, the parameter 'key' is always the *address*
109// of the key, and 'elem' is *address* of the elem, as are the return values
110// of the functions that return elems.
111//
112// * Size: The number of elements in the set.
113//
114// * Contains: Determines if the value is in the set.
115//
116// * Insert: Inserts a new element into the set. Duplicates are forbidden,
117// and will cause assertion failures.
118//
119// * Remove: Removes the value from the set, if present. Returns a Bool
120// indicating if the value was removed.
121//
122// * ResetIter: Each OSet has an iterator. This resets it to point to the
123// first element in the OSet.
124//
125// * Next: Copies the next value according to the OSet's iterator into &val,
126// advances the iterator by one, and returns True; the elements are
127// visited in order. Or, returns False if the iterator has reached the
128// set's end.
129//
130// You can thus iterate in order through a set like this:
131//
132// Word val;
133// VG_(OSetWord_ResetIter)(oset);
134// while ( VG_(OSetWord_Next)(oset, &val) ) {
135// ... do stuff with 'val' ...
136// }
137//
138// Note that iterators are cleared any time an element is inserted or
139// removed from the OSet, to avoid possible mayhem caused by the iterator
140// getting out of sync with the OSet's contents. "Cleared" means that
141// they will return False if VG_(OSetWord_Next)() is called without an
142// intervening call to VG_(OSetWord_ResetIter)().
143
144extern Int VG_(OSetWord_Size) ( OSet* os );
145extern void VG_(OSetWord_Insert) ( OSet* os, Word val );
146extern Bool VG_(OSetWord_Contains) ( OSet* os, Word val );
147extern Bool VG_(OSetWord_Remove) ( OSet* os, Word val );
148extern void VG_(OSetWord_ResetIter) ( OSet* os );
149extern Bool VG_(OSetWord_Next) ( OSet* os, Word* val );
150
151
152/*--------------------------------------------------------------------*/
153/*--- Creating and destroying OSets and OSet members (Gen) ---*/
njne1b2b962005-08-14 22:13:00 +0000154/*--------------------------------------------------------------------*/
155
njna103e142005-11-18 21:32:18 +0000156// * Create: allocates and initialises the OSet. Arguments:
njne1b2b962005-08-14 22:13:00 +0000157// - keyOff The offset of the key within the element.
njne1b2b962005-08-14 22:13:00 +0000158// - cmp The comparison function between keys and elements, or NULL
159// if the OSet should use fast comparisons.
160// - alloc The allocation function used for allocating the OSet itself;
njne2a9ad32007-09-17 05:30:48 +0000161// it's also called for each invocation of
162// VG_(OSetGen_AllocNode)().
163// - free The deallocation function used by VG_(OSetGen_FreeNode)() and
164// VG_(OSetGen_Destroy)().
njne1b2b962005-08-14 22:13:00 +0000165//
166// If cmp is NULL, keyOff must be zero. This is checked.
167//
168// * Destroy: frees all nodes in the table, plus the memory used by
njne004d722005-12-22 06:20:59 +0000169// the table itself. The passed-in function is called on each node first
170// to allow the destruction of any attached resources; if NULL it is not
171// called.
njne1b2b962005-08-14 22:13:00 +0000172//
173// * AllocNode: Allocate and zero memory for a node to go into the OSet.
njne2a9ad32007-09-17 05:30:48 +0000174// Uses the alloc function given to VG_(OSetGen_Create)() to allocated a
175// node which is big enough for both an element and the OSet metadata.
njne1b2b962005-08-14 22:13:00 +0000176// Not all elements in one OSet have to be the same size.
177//
178// Note that the element allocated will be at most word-aligned, which may
179// be less aligned than the element type would normally be.
180//
njne2a9ad32007-09-17 05:30:48 +0000181// * FreeNode: Deallocate a node allocated with OSetGen_AllocNode(). Using
njne1b2b962005-08-14 22:13:00 +0000182// a deallocation function (such as VG_(free)()) directly will likely
183// lead to assertions in Valgrind's allocator.
184
njne2a9ad32007-09-17 05:30:48 +0000185extern OSet* VG_(OSetGen_Create) ( OffT keyOff, OSetCmp_t cmp,
186 OSetAlloc_t alloc, OSetFree_t free );
187extern void VG_(OSetGen_Destroy) ( OSet* os );
188extern void* VG_(OSetGen_AllocNode) ( OSet* os, SizeT elemSize );
189extern void VG_(OSetGen_FreeNode) ( OSet* os, void* elem );
njne1b2b962005-08-14 22:13:00 +0000190
191/*--------------------------------------------------------------------*/
njne2a9ad32007-09-17 05:30:48 +0000192/*--- Operations on OSets (Gen) ---*/
njne1b2b962005-08-14 22:13:00 +0000193/*--------------------------------------------------------------------*/
194
njnc438e082005-10-15 17:50:02 +0000195// In everything that follows, the parameter 'key' is always the *address*
196// of the key, and 'elem' is *address* of the elem, as are the return values
197// of the functions that return elems.
198//
njne1b2b962005-08-14 22:13:00 +0000199// * Size: The number of elements in the set.
200//
njne2a9ad32007-09-17 05:30:48 +0000201// * Insert: Inserts a new element into the set. Note that 'elem' must
202// have been allocated using VG_(OSetGen_AllocNode)(), otherwise you will
203// get assertion failures about "bad magic". Duplicates are forbidden,
204// and will also cause assertion failures.
205//
njne1b2b962005-08-14 22:13:00 +0000206// * Contains: Determines if any element in the OSet matches the key.
207//
208// * Lookup: Returns a pointer to the element matching the key, if there is
209// one, otherwise returns NULL.
210//
njnaa260e82005-08-17 21:06:07 +0000211// * LookupWithCmp: Like Lookup, but you specify the comparison function,
212// which overrides the OSet's normal one.
213//
njne1b2b962005-08-14 22:13:00 +0000214// * Remove: Removes the element matching the key, if there is one. Returns
215// NULL if no element matches the key.
216//
217// * ResetIter: Each OSet has an iterator. This resets it to point to the
218// first element in the OSet.
219//
220// * Next: Returns a pointer to the element pointed to by the OSet's
221// iterator, and advances the iterator by one; the elements are visited
222// in order. Or, returns NULL if the iterator has reached the OSet's end.
223//
njne2a9ad32007-09-17 05:30:48 +0000224// You can thus iterate in order through a set like this:
njne1b2b962005-08-14 22:13:00 +0000225//
njne2a9ad32007-09-17 05:30:48 +0000226// VG_(OSetGen_ResetIter)(oset);
227// while ( (elem = VG_(OSetGen_Next)(oset)) ) {
njne1b2b962005-08-14 22:13:00 +0000228// ... do stuff with 'elem' ...
229// }
230//
231// Note that iterators are cleared any time an element is inserted or
232// removed from the OSet, to avoid possible mayhem caused by the iterator
233// getting out of sync with the OSet's contents. "Cleared" means that
njne2a9ad32007-09-17 05:30:48 +0000234// they will return NULL if VG_(OSetGen_Next)() is called without an
235// intervening call to VG_(OSetGen_ResetIter)().
njne1b2b962005-08-14 22:13:00 +0000236
tom5a835d52007-12-30 12:28:26 +0000237extern Int VG_(OSetGen_Size) ( const OSet* os );
njne2a9ad32007-09-17 05:30:48 +0000238extern void VG_(OSetGen_Insert) ( OSet* os, void* elem );
tom5a835d52007-12-30 12:28:26 +0000239extern Bool VG_(OSetGen_Contains) ( const OSet* os, const void* key );
240extern void* VG_(OSetGen_Lookup) ( const OSet* os, const void* key );
241extern void* VG_(OSetGen_LookupWithCmp)( OSet* os, const void* key, OSetCmp_t cmp );
njne2a9ad32007-09-17 05:30:48 +0000242extern void* VG_(OSetGen_Remove) ( OSet* os, void* key );
243extern void VG_(OSetGen_ResetIter) ( OSet* os );
244extern void* VG_(OSetGen_Next) ( OSet* os );
njne1b2b962005-08-14 22:13:00 +0000245
246#endif // __PUB_TOOL_OSET_H
247
248/*--------------------------------------------------------------------*/
249/*--- end ---*/
250/*--------------------------------------------------------------------*/