njn | e1b2b96 | 2005-08-14 22:13:00 +0000 | [diff] [blame] | 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) 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 | |
| 65 | typedef struct _OSet OSet; |
| 66 | typedef struct _OSetNode OSetNode; |
| 67 | |
njn | e004d72 | 2005-12-22 06:20:59 +0000 | [diff] [blame^] | 68 | typedef Int (*OSetCmp_t) ( void* key, void* elem ); |
| 69 | typedef void* (*OSetAlloc_t) ( SizeT szB ); |
| 70 | typedef void (*OSetFree_t) ( void* p ); |
| 71 | typedef void (*OSetNodeDestroy_t) ( void* elem ); |
njn | e1b2b96 | 2005-08-14 22:13:00 +0000 | [diff] [blame] | 72 | |
| 73 | /*--------------------------------------------------------------------*/ |
| 74 | /*--- Creating and destroying OSets and OSet members ---*/ |
| 75 | /*--------------------------------------------------------------------*/ |
| 76 | |
njn | a103e14 | 2005-11-18 21:32:18 +0000 | [diff] [blame] | 77 | // * Create: allocates and initialises the OSet. Arguments: |
njn | e1b2b96 | 2005-08-14 22:13:00 +0000 | [diff] [blame] | 78 | // - keyOff The offset of the key within the element. |
njn | e1b2b96 | 2005-08-14 22:13:00 +0000 | [diff] [blame] | 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 |
njn | e004d72 | 2005-12-22 06:20:59 +0000 | [diff] [blame^] | 89 | // the table itself. The passed-in function is called on each node first |
| 90 | // to allow the destruction of any attached resources; if NULL it is not |
| 91 | // called. |
njn | e1b2b96 | 2005-08-14 22:13:00 +0000 | [diff] [blame] | 92 | // |
| 93 | // * AllocNode: Allocate and zero memory for a node to go into the OSet. |
| 94 | // Uses the alloc function given to VG_(OSet_Create)() to allocated a node |
| 95 | // which is big enough for both an element and the OSet metadata. |
| 96 | // Not all elements in one OSet have to be the same size. |
| 97 | // |
| 98 | // Note that the element allocated will be at most word-aligned, which may |
| 99 | // be less aligned than the element type would normally be. |
| 100 | // |
| 101 | // * FreeNode: Deallocate a node allocated with OSet_AllocNode(). Using |
| 102 | // a deallocation function (such as VG_(free)()) directly will likely |
| 103 | // lead to assertions in Valgrind's allocator. |
| 104 | |
| 105 | extern OSet* VG_(OSet_Create) ( OffT keyOff, OSetCmp_t cmp, |
| 106 | OSetAlloc_t alloc, OSetFree_t free ); |
njn | e004d72 | 2005-12-22 06:20:59 +0000 | [diff] [blame^] | 107 | extern void VG_(OSet_Destroy) ( OSet* os, OSetNodeDestroy_t destroyNode ); |
njn | e1b2b96 | 2005-08-14 22:13:00 +0000 | [diff] [blame] | 108 | extern void* VG_(OSet_AllocNode) ( OSet* os, SizeT elemSize ); |
| 109 | extern void VG_(OSet_FreeNode) ( OSet* os, void* elem ); |
| 110 | |
| 111 | /*--------------------------------------------------------------------*/ |
| 112 | /*--- Operations on OSets ---*/ |
| 113 | /*--------------------------------------------------------------------*/ |
| 114 | |
njn | c438e08 | 2005-10-15 17:50:02 +0000 | [diff] [blame] | 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 | // |
njn | e1b2b96 | 2005-08-14 22:13:00 +0000 | [diff] [blame] | 119 | // * Size: The number of elements in the set. |
| 120 | // |
| 121 | // * Contains: Determines if any element in the OSet matches the key. |
| 122 | // |
| 123 | // * Lookup: Returns a pointer to the element matching the key, if there is |
| 124 | // one, otherwise returns NULL. |
| 125 | // |
njn | aa260e8 | 2005-08-17 21:06:07 +0000 | [diff] [blame] | 126 | // * LookupWithCmp: Like Lookup, but you specify the comparison function, |
| 127 | // which overrides the OSet's normal one. |
| 128 | // |
njn | e1b2b96 | 2005-08-14 22:13:00 +0000 | [diff] [blame] | 129 | // * Insert: Inserts a new element into the list. Note that 'elem' must |
| 130 | // have been allocated using VG_(OSet_AllocNode)(), otherwise you will get |
| 131 | // assertion failures about "bad magic". Duplicates are forbidden, and |
| 132 | // will also cause assertion failures. |
| 133 | // |
| 134 | // * Remove: Removes the element matching the key, if there is one. Returns |
| 135 | // NULL if no element matches the key. |
| 136 | // |
| 137 | // * ResetIter: Each OSet has an iterator. This resets it to point to the |
| 138 | // first element in the OSet. |
| 139 | // |
| 140 | // * Next: Returns a pointer to the element pointed to by the OSet's |
| 141 | // iterator, and advances the iterator by one; the elements are visited |
| 142 | // in order. Or, returns NULL if the iterator has reached the OSet's end. |
| 143 | // |
| 144 | // You can thus iterate in order through an OSet like this: |
| 145 | // |
| 146 | // VG_(OSet_ResetIter)(oset); |
| 147 | // while ( (elem = VG_(OSet_Next)(oset)) ) { |
| 148 | // ... do stuff with 'elem' ... |
| 149 | // } |
| 150 | // |
| 151 | // Note that iterators are cleared any time an element is inserted or |
| 152 | // removed from the OSet, to avoid possible mayhem caused by the iterator |
| 153 | // getting out of sync with the OSet's contents. "Cleared" means that |
| 154 | // they will return NULL if VG_(OSet_Next)() is called without an |
| 155 | // intervening call to VG_(OSet_ResetIter)(). |
| 156 | |
njn | aa260e8 | 2005-08-17 21:06:07 +0000 | [diff] [blame] | 157 | extern Int VG_(OSet_Size) ( OSet* os ); |
| 158 | extern void VG_(OSet_Insert) ( OSet* os, void* elem ); |
| 159 | extern Bool VG_(OSet_Contains) ( OSet* os, void* key ); |
| 160 | extern void* VG_(OSet_Lookup) ( OSet* os, void* key ); |
| 161 | extern void* VG_(OSet_LookupWithCmp)( OSet* os, void* key, OSetCmp_t cmp ); |
| 162 | extern void* VG_(OSet_Remove) ( OSet* os, void* key ); |
| 163 | extern void VG_(OSet_ResetIter) ( OSet* os ); |
| 164 | extern void* VG_(OSet_Next) ( OSet* os ); |
njn | e1b2b96 | 2005-08-14 22:13:00 +0000 | [diff] [blame] | 165 | |
| 166 | #endif // __PUB_TOOL_OSET_H |
| 167 | |
| 168 | /*--------------------------------------------------------------------*/ |
| 169 | /*--- end ---*/ |
| 170 | /*--------------------------------------------------------------------*/ |