2 * Copyright (C) 2008 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * General purpose hash table, used for finding classes, methods, etc.
19 * When the number of elements reaches a certain percentage of the table's
20 * capacity, the table will be resized.
29 /* compute the hash of an item with a specific type */
30 typedef u4 (*HashCompute)(const void* item);
33 * Compare a hash entry with a "loose" item after their hash values match.
34 * Returns { <0, 0, >0 } depending on ordering of items (same semantics
37 typedef int (*HashCompareFunc)(const void* tableItem, const void* looseItem);
40 * This function will be used to free entries in the table. This can be
41 * NULL if no free is required, free(), or a custom function.
43 typedef void (*HashFreeFunc)(void* ptr);
46 * Used by dvmHashForeach().
48 typedef int (*HashForeachFunc)(void* data, void* arg);
51 * Used by dvmHashForeachRemove().
53 typedef int (*HashForeachRemoveFunc)(void* data);
56 * One entry in the hash table. "data" values are expected to be (or have
57 * the same characteristics as) valid pointers. In particular, a NULL
58 * value for "data" indicates an empty slot, and HASH_TOMBSTONE indicates
59 * a no-longer-used slot that must be stepped over during probing.
61 * Attempting to add a NULL or tombstone value is an error.
63 * When an entry is released, we will call (HashFreeFunc)(entry->data).
65 typedef struct HashEntry {
70 #define HASH_TOMBSTONE ((void*) 0xcbcacccd) // invalid ptr value
73 * Expandable hash table.
75 * This structure should be considered opaque.
77 typedef struct HashTable {
78 int tableSize; /* must be power of 2 */
79 int numEntries; /* current #of "live" entries */
80 int numDeadEntries; /* current #of tombstone entries */
81 HashEntry* pEntries; /* array on heap */
82 HashFreeFunc freeFunc;
87 * Create and initialize a HashTable structure, using "initialSize" as
88 * a basis for the initial capacity of the table. (The actual initial
89 * table size may be adjusted upward.) If you know exactly how many
90 * elements the table will hold, pass the result from dvmHashSize() in.)
92 * Returns "false" if unable to allocate the table.
94 HashTable* dvmHashTableCreate(size_t initialSize, HashFreeFunc freeFunc);
97 * Compute the capacity needed for a table to hold "size" elements. Use
98 * this when you know ahead of time how many elements the table will hold.
99 * Pass this value into dvmHashTableCreate() to ensure that you can add
100 * all elements without needing to reallocate the table.
102 size_t dvmHashSize(size_t size);
105 * Clear out a hash table, freeing the contents of any used entries.
107 void dvmHashTableClear(HashTable* pHashTable);
110 * Free a hash table. Performs a "clear" first.
112 void dvmHashTableFree(HashTable* pHashTable);
115 * Exclusive access. Important when adding items to a table, or when
116 * doing any operations on a table that could be added to by another thread.
118 INLINE void dvmHashTableLock(HashTable* pHashTable) {
119 dvmLockMutex(&pHashTable->lock);
121 INLINE void dvmHashTableUnlock(HashTable* pHashTable) {
122 dvmUnlockMutex(&pHashTable->lock);
126 * Get #of entries in hash table.
128 INLINE int dvmHashTableNumEntries(HashTable* pHashTable) {
129 return pHashTable->numEntries;
133 * Get total size of hash table (for memory usage calculations).
135 INLINE int dvmHashTableMemUsage(HashTable* pHashTable) {
136 return sizeof(HashTable) + pHashTable->tableSize * sizeof(HashEntry);
140 * Look up an entry in the table, possibly adding it if it's not there.
142 * If "item" is not found, and "doAdd" is false, NULL is returned.
143 * Otherwise, a pointer to the found or added item is returned. (You can
144 * tell the difference by seeing if return value == item.)
146 * An "add" operation may cause the entire table to be reallocated. Don't
147 * forget to lock the table before calling this.
149 void* dvmHashTableLookup(HashTable* pHashTable, u4 itemHash, void* item,
150 HashCompareFunc cmpFunc, bool doAdd);
153 * Remove an item from the hash table, given its "data" pointer. Does not
154 * invoke the "free" function; just detaches it from the table.
156 bool dvmHashTableRemove(HashTable* pHashTable, u4 hash, void* item);
159 * Execute "func" on every entry in the hash table.
161 * If "func" returns a nonzero value, terminate early and return the value.
163 int dvmHashForeach(HashTable* pHashTable, HashForeachFunc func, void* arg);
166 * Execute "func" on every entry in the hash table.
168 * If "func" returns 1 detach the entry from the hash table. Does not invoke
169 * the "free" function.
171 * Returning values other than 0 or 1 from "func" will abort the routine.
173 int dvmHashForeachRemove(HashTable* pHashTable, HashForeachRemoveFunc func);
176 * An alternative to dvmHashForeach(), using an iterator.
180 * for (dvmHashIterBegin(hashTable, &iter); !dvmHashIterDone(&iter);
181 * dvmHashIterNext(&iter))
183 * MyData* data = (MyData*)dvmHashIterData(&iter);
186 typedef struct HashIter {
188 HashTable* pHashTable;
191 INLINE void dvmHashIterNext(HashIter* pIter) {
192 int i = pIter->idx +1;
193 int lim = pIter->pHashTable->tableSize;
194 for ( ; i < lim; i++) {
195 void* data = pIter->pHashTable->pEntries[i].data;
196 if (data != NULL && data != HASH_TOMBSTONE)
201 INLINE void dvmHashIterBegin(HashTable* pHashTable, HashIter* pIter) {
202 pIter->pHashTable = pHashTable;
204 dvmHashIterNext(pIter);
206 INLINE bool dvmHashIterDone(HashIter* pIter) {
207 return (pIter->idx >= pIter->pHashTable->tableSize);
209 INLINE void* dvmHashIterData(HashIter* pIter) {
210 assert(pIter->idx >= 0 && pIter->idx < pIter->pHashTable->tableSize);
211 return pIter->pHashTable->pEntries[pIter->idx].data;
216 * Evaluate hash table performance by examining the number of times we
217 * have to probe for an entry.
219 * The caller should lock the table beforehand.
221 typedef u4 (*HashCalcFunc)(const void* item);
222 void dvmHashTableProbeCount(HashTable* pHashTable, HashCalcFunc calcFunc,
223 HashCompareFunc cmpFunc);
229 #endif /*_DALVIK_HASH*/