blob: ed8e2de1b950ae4856a5ccf720085eabd411fc4a [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
10 Copyright (C) 2000-2005 Julian Seward
11 jseward@acm.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// 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//
39// The structure is totally generic. The user provides the allocation and
40// deallocation functions. Also, each element has a key, which the lookup
41// is done with. The key may be the whole element (eg. in an OSet of
42// integers, each integer serves both as an element and a key), or it may be
43// only part of it (eg. if the key is a single field in a struct). The user
44// can provide a function that compares an element with a key; this is very
45// flexible, and with the right comparison function even a (non-overlapping)
46// interval list can be created. But the cost of calling a function for
47// every comparison can be high during lookup. If no comparison function is
48// provided, we assume that keys are (signed or unsigned) words, and that
49// the key is the first word in each element. This fast comparison is
50// suitable for an OSet of Ints, or an OSet containing structs where the
51// first element is an Addr, for example.
52//
53// Each OSet also has an iterator, which makes it simple to traverse all the
54// nodes in order. Note that the iterator maintains state and so is
55// non-reentrant.
56//
57// Note that once you insert an element into an OSet, if you modify any part
58// of it looked at by your cmp() function, this may cause incorrect
59// behaviour as the sorted order maintained will be wrong.
60
61/*--------------------------------------------------------------------*/
62/*--- Types ---*/
63/*--------------------------------------------------------------------*/
64
65typedef struct _OSet OSet;
66typedef struct _OSetNode OSetNode;
67
68typedef Int (*OSetCmp_t) ( void* key, void* elem );
69typedef void* (*OSetAlloc_t) ( SizeT szB );
70typedef void (*OSetFree_t) ( void* p );
71
72/*--------------------------------------------------------------------*/
73/*--- Creating and destroying OSets and OSet members ---*/
74/*--------------------------------------------------------------------*/
75
76// * Create: allocates an initialises the OSet. Arguments:
77// - keyOff The offset of the key within the element.
78// - elemSize The size of the element.
79// - cmp The comparison function between keys and elements, or NULL
80// if the OSet should use fast comparisons.
81// - alloc The allocation function used for allocating the OSet itself;
82// it's also called for each invocation of VG_(OSet_AllocNode)().
83// - free The deallocation function used by VG_(OSet_FreeNode)() and
84// VG_(OSet_Destroy)().
85//
86// If cmp is NULL, keyOff must be zero. This is checked.
87//
88// * Destroy: frees all nodes in the table, plus the memory used by
89// the table itself.
90//
91// * AllocNode: Allocate and zero memory for a node to go into the OSet.
92// Uses the alloc function given to VG_(OSet_Create)() to allocated a node
93// which is big enough for both an element and the OSet metadata.
94// Not all elements in one OSet have to be the same size.
95//
96// Note that the element allocated will be at most word-aligned, which may
97// be less aligned than the element type would normally be.
98//
99// * FreeNode: Deallocate a node allocated with OSet_AllocNode(). Using
100// a deallocation function (such as VG_(free)()) directly will likely
101// lead to assertions in Valgrind's allocator.
102
103extern OSet* VG_(OSet_Create) ( OffT keyOff, OSetCmp_t cmp,
104 OSetAlloc_t alloc, OSetFree_t free );
105extern void VG_(OSet_Destroy) ( OSet* os );
106extern void* VG_(OSet_AllocNode) ( OSet* os, SizeT elemSize );
107extern void VG_(OSet_FreeNode) ( OSet* os, void* elem );
108
109/*--------------------------------------------------------------------*/
110/*--- Operations on OSets ---*/
111/*--------------------------------------------------------------------*/
112
113// * Size: The number of elements in the set.
114//
115// * Contains: Determines if any element in the OSet matches the key.
116//
117// * Lookup: Returns a pointer to the element matching the key, if there is
118// one, otherwise returns NULL.
119//
120// * Insert: Inserts a new element into the list. Note that 'elem' must
121// have been allocated using VG_(OSet_AllocNode)(), otherwise you will get
122// assertion failures about "bad magic". Duplicates are forbidden, and
123// will also cause assertion failures.
124//
125// * Remove: Removes the element matching the key, if there is one. Returns
126// NULL if no element matches the key.
127//
128// * ResetIter: Each OSet has an iterator. This resets it to point to the
129// first element in the OSet.
130//
131// * Next: Returns a pointer to the element pointed to by the OSet's
132// iterator, and advances the iterator by one; the elements are visited
133// in order. Or, returns NULL if the iterator has reached the OSet's end.
134//
135// You can thus iterate in order through an OSet like this:
136//
137// VG_(OSet_ResetIter)(oset);
138// while ( (elem = VG_(OSet_Next)(oset)) ) {
139// ... do stuff with 'elem' ...
140// }
141//
142// Note that iterators are cleared any time an element is inserted or
143// removed from the OSet, to avoid possible mayhem caused by the iterator
144// getting out of sync with the OSet's contents. "Cleared" means that
145// they will return NULL if VG_(OSet_Next)() is called without an
146// intervening call to VG_(OSet_ResetIter)().
147
148extern Int VG_(OSet_Size) ( OSet* os );
149extern void VG_(OSet_Insert) ( OSet* os, void* elem );
150extern Bool VG_(OSet_Contains) ( OSet* os, void* key );
151extern void* VG_(OSet_Lookup) ( OSet* os, void* key );
152extern void* VG_(OSet_Remove) ( OSet* os, void* key );
153extern void VG_(OSet_ResetIter) ( OSet* os );
154extern void* VG_(OSet_Next) ( OSet* os );
155
156#endif // __PUB_TOOL_OSET_H
157
158/*--------------------------------------------------------------------*/
159/*--- end ---*/
160/*--------------------------------------------------------------------*/