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.
22 #ifndef DALVIK_HASH_H_
23 #define DALVIK_HASH_H_
25 /* compute the hash of an item with a specific type */
26 typedef u4 (*HashCompute)(const void* item);
29 * Compare a hash entry with a "loose" item after their hash values match.
30 * Returns { <0, 0, >0 } depending on ordering of items (same semantics
33 typedef int (*HashCompareFunc)(const void* tableItem, const void* looseItem);
36 * This function will be used to free entries in the table. This can be
37 * NULL if no free is required, free(), or a custom function.
39 typedef void (*HashFreeFunc)(void* ptr);
42 * Used by dvmHashForeach().
44 typedef int (*HashForeachFunc)(void* data, void* arg);
47 * Used by dvmHashForeachRemove().
49 typedef int (*HashForeachRemoveFunc)(void* data);
52 * One entry in the hash table. "data" values are expected to be (or have
53 * the same characteristics as) valid pointers. In particular, a NULL
54 * value for "data" indicates an empty slot, and HASH_TOMBSTONE indicates
55 * a no-longer-used slot that must be stepped over during probing.
57 * Attempting to add a NULL or tombstone value is an error.
59 * When an entry is released, we will call (HashFreeFunc)(entry->data).
66 #define HASH_TOMBSTONE ((void*) 0xcbcacccd) // invalid ptr value
69 * Expandable hash table.
71 * This structure should be considered opaque.
74 int tableSize; /* must be power of 2 */
75 int numEntries; /* current #of "live" entries */
76 int numDeadEntries; /* current #of tombstone entries */
77 HashEntry* pEntries; /* array on heap */
78 HashFreeFunc freeFunc;
83 * Create and initialize a HashTable structure, using "initialSize" as
84 * a basis for the initial capacity of the table. (The actual initial
85 * table size may be adjusted upward.) If you know exactly how many
86 * elements the table will hold, pass the result from dvmHashSize() in.)
88 * Returns "false" if unable to allocate the table.
90 HashTable* dvmHashTableCreate(size_t initialSize, HashFreeFunc freeFunc);
93 * Compute the capacity needed for a table to hold "size" elements. Use
94 * this when you know ahead of time how many elements the table will hold.
95 * Pass this value into dvmHashTableCreate() to ensure that you can add
96 * all elements without needing to reallocate the table.
98 size_t dvmHashSize(size_t size);
101 * Clear out a hash table, freeing the contents of any used entries.
103 void dvmHashTableClear(HashTable* pHashTable);
106 * Free a hash table. Performs a "clear" first.
108 void dvmHashTableFree(HashTable* pHashTable);
111 * Exclusive access. Important when adding items to a table, or when
112 * doing any operations on a table that could be added to by another thread.
114 INLINE void dvmHashTableLock(HashTable* pHashTable) {
115 dvmLockMutex(&pHashTable->lock);
117 INLINE void dvmHashTableUnlock(HashTable* pHashTable) {
118 dvmUnlockMutex(&pHashTable->lock);
122 * Get #of entries in hash table.
124 INLINE int dvmHashTableNumEntries(HashTable* pHashTable) {
125 return pHashTable->numEntries;
129 * Get total size of hash table (for memory usage calculations).
131 INLINE int dvmHashTableMemUsage(HashTable* pHashTable) {
132 return sizeof(HashTable) + pHashTable->tableSize * sizeof(HashEntry);
136 * Look up an entry in the table, possibly adding it if it's not there.
138 * If "item" is not found, and "doAdd" is false, NULL is returned.
139 * Otherwise, a pointer to the found or added item is returned. (You can
140 * tell the difference by seeing if return value == item.)
142 * An "add" operation may cause the entire table to be reallocated. Don't
143 * forget to lock the table before calling this.
145 void* dvmHashTableLookup(HashTable* pHashTable, u4 itemHash, void* item,
146 HashCompareFunc cmpFunc, bool doAdd);
149 * Remove an item from the hash table, given its "data" pointer. Does not
150 * invoke the "free" function; just detaches it from the table.
152 bool dvmHashTableRemove(HashTable* pHashTable, u4 hash, void* item);
155 * Execute "func" on every entry in the hash table.
157 * If "func" returns a nonzero value, terminate early and return the value.
159 int dvmHashForeach(HashTable* pHashTable, HashForeachFunc func, void* arg);
162 * Execute "func" on every entry in the hash table.
164 * If "func" returns 1 detach the entry from the hash table. Does not invoke
165 * the "free" function.
167 * Returning values other than 0 or 1 from "func" will abort the routine.
169 int dvmHashForeachRemove(HashTable* pHashTable, HashForeachRemoveFunc func);
172 * An alternative to dvmHashForeach(), using an iterator.
176 * for (dvmHashIterBegin(hashTable, &iter); !dvmHashIterDone(&iter);
177 * dvmHashIterNext(&iter))
179 * MyData* data = (MyData*)dvmHashIterData(&iter);
184 HashTable* pHashTable;
187 INLINE void dvmHashIterNext(HashIter* pIter) {
188 int i = pIter->idx +1;
189 int lim = pIter->pHashTable->tableSize;
190 for ( ; i < lim; i++) {
191 void* data = pIter->pHashTable->pEntries[i].data;
192 if (data != NULL && data != HASH_TOMBSTONE)
197 INLINE void dvmHashIterBegin(HashTable* pHashTable, HashIter* pIter) {
198 pIter->pHashTable = pHashTable;
200 dvmHashIterNext(pIter);
202 INLINE bool dvmHashIterDone(HashIter* pIter) {
203 return (pIter->idx >= pIter->pHashTable->tableSize);
205 INLINE void* dvmHashIterData(HashIter* pIter) {
206 assert(pIter->idx >= 0 && pIter->idx < pIter->pHashTable->tableSize);
207 return pIter->pHashTable->pEntries[pIter->idx].data;
212 * Evaluate hash table performance by examining the number of times we
213 * have to probe for an entry.
215 * The caller should lock the table beforehand.
217 typedef u4 (*HashCalcFunc)(const void* item);
218 void dvmHashTableProbeCount(HashTable* pHashTable, HashCalcFunc calcFunc,
219 HashCompareFunc cmpFunc);
221 #endif // DALVIK_HASH_H_