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 * Hash table. The dominant calls are add and lookup, with removals
18 * happening very infrequently. We use probing, and don't worry much
19 * about tombstone removal.
25 /* table load factor, i.e. how full can it get before we resize */
26 //#define LOAD_NUMER 3 // 75%
27 //#define LOAD_DENOM 4
28 #define LOAD_NUMER 5 // 62.5%
30 //#define LOAD_NUMER 1 // 50%
31 //#define LOAD_DENOM 2
34 * Compute the capacity needed for a table to hold "size" elements.
36 size_t dvmHashSize(size_t size) {
37 return (size * LOAD_DENOM) / LOAD_NUMER +1;
42 * Create and initialize a hash table.
44 HashTable* dvmHashTableCreate(size_t initialSize, HashFreeFunc freeFunc)
46 HashTable* pHashTable;
48 assert(initialSize > 0);
50 pHashTable = (HashTable*) malloc(sizeof(*pHashTable));
51 if (pHashTable == NULL)
54 dvmInitMutex(&pHashTable->lock);
56 pHashTable->tableSize = dexRoundUpPower2(initialSize);
57 pHashTable->numEntries = pHashTable->numDeadEntries = 0;
58 pHashTable->freeFunc = freeFunc;
59 pHashTable->pEntries =
60 (HashEntry*) malloc(pHashTable->tableSize * sizeof(HashEntry));
61 if (pHashTable->pEntries == NULL) {
66 memset(pHashTable->pEntries, 0, pHashTable->tableSize * sizeof(HashEntry));
71 * Clear out all entries.
73 void dvmHashTableClear(HashTable* pHashTable)
78 pEnt = pHashTable->pEntries;
79 for (i = 0; i < pHashTable->tableSize; i++, pEnt++) {
80 if (pEnt->data == HASH_TOMBSTONE) {
83 } else if (pEnt->data != NULL) {
84 // call free func then nuke entry
85 if (pHashTable->freeFunc != NULL)
86 (*pHashTable->freeFunc)(pEnt->data);
91 pHashTable->numEntries = 0;
92 pHashTable->numDeadEntries = 0;
98 void dvmHashTableFree(HashTable* pHashTable)
100 if (pHashTable == NULL)
102 dvmHashTableClear(pHashTable);
103 free(pHashTable->pEntries);
109 * Count up the number of tombstone entries in the hash table.
111 static int countTombStones(HashTable* pHashTable)
115 for (count = i = 0; i < pHashTable->tableSize; i++) {
116 if (pHashTable->pEntries[i].data == HASH_TOMBSTONE)
124 * Resize a hash table. We do this when adding an entry increased the
125 * size of the table beyond its comfy limit.
127 * This essentially requires re-inserting all elements into the new storage.
129 * If multiple threads can access the hash table, the table's lock should
130 * have been grabbed before issuing the "lookup+add" call that led to the
131 * resize, so we don't have a synchronization problem here.
133 static bool resizeHash(HashTable* pHashTable, int newSize)
135 HashEntry* pNewEntries;
138 assert(countTombStones(pHashTable) == pHashTable->numDeadEntries);
139 //LOGI("before: dead=%d\n", pHashTable->numDeadEntries);
141 pNewEntries = (HashEntry*) calloc(newSize, sizeof(HashEntry));
142 if (pNewEntries == NULL)
145 for (i = 0; i < pHashTable->tableSize; i++) {
146 void* data = pHashTable->pEntries[i].data;
147 if (data != NULL && data != HASH_TOMBSTONE) {
148 int hashValue = pHashTable->pEntries[i].hashValue;
151 /* probe for new spot, wrapping around */
152 newIdx = hashValue & (newSize-1);
153 while (pNewEntries[newIdx].data != NULL)
154 newIdx = (newIdx + 1) & (newSize-1);
156 pNewEntries[newIdx].hashValue = hashValue;
157 pNewEntries[newIdx].data = data;
161 free(pHashTable->pEntries);
162 pHashTable->pEntries = pNewEntries;
163 pHashTable->tableSize = newSize;
164 pHashTable->numDeadEntries = 0;
166 assert(countTombStones(pHashTable) == 0);
173 * We probe on collisions, wrapping around the table.
175 void* dvmHashTableLookup(HashTable* pHashTable, u4 itemHash, void* item,
176 HashCompareFunc cmpFunc, bool doAdd)
182 assert(pHashTable->tableSize > 0);
183 assert(item != HASH_TOMBSTONE);
184 assert(item != NULL);
186 /* jump to the first entry and probe for a match */
187 pEntry = &pHashTable->pEntries[itemHash & (pHashTable->tableSize-1)];
188 pEnd = &pHashTable->pEntries[pHashTable->tableSize];
189 while (pEntry->data != NULL) {
190 if (pEntry->data != HASH_TOMBSTONE &&
191 pEntry->hashValue == itemHash &&
192 (*cmpFunc)(pEntry->data, item) == 0)
195 //LOGD("+++ match on entry %d\n", pEntry - pHashTable->pEntries);
200 if (pEntry == pEnd) { /* wrap around to start */
201 if (pHashTable->tableSize == 1)
202 break; /* edge case - single-entry table */
203 pEntry = pHashTable->pEntries;
206 //LOGI("+++ look probing %d...\n", pEntry - pHashTable->pEntries);
209 if (pEntry->data == NULL) {
211 pEntry->hashValue = itemHash;
213 pHashTable->numEntries++;
216 * We've added an entry. See if this brings us too close to full.
218 if ((pHashTable->numEntries+pHashTable->numDeadEntries) * LOAD_DENOM
219 > pHashTable->tableSize * LOAD_NUMER)
221 if (!resizeHash(pHashTable, pHashTable->tableSize * 2)) {
222 /* don't really have a way to indicate failure */
223 LOGE("Dalvik hash resize failure\n");
226 /* note "pEntry" is now invalid */
228 //LOGW("okay %d/%d/%d\n",
229 // pHashTable->numEntries, pHashTable->tableSize,
230 // (pHashTable->tableSize * LOAD_NUMER) / LOAD_DENOM);
233 /* full table is bad -- search for nonexistent never halts */
234 assert(pHashTable->numEntries < pHashTable->tableSize);
237 assert(result == NULL);
240 result = pEntry->data;
247 * Remove an entry from the table.
249 * Does NOT invoke the "free" function on the item.
251 bool dvmHashTableRemove(HashTable* pHashTable, u4 itemHash, void* item)
256 assert(pHashTable->tableSize > 0);
258 /* jump to the first entry and probe for a match */
259 pEntry = &pHashTable->pEntries[itemHash & (pHashTable->tableSize-1)];
260 pEnd = &pHashTable->pEntries[pHashTable->tableSize];
261 while (pEntry->data != NULL) {
262 if (pEntry->data == item) {
263 //LOGI("+++ stepping on entry %d\n", pEntry - pHashTable->pEntries);
264 pEntry->data = HASH_TOMBSTONE;
265 pHashTable->numEntries--;
266 pHashTable->numDeadEntries++;
271 if (pEntry == pEnd) { /* wrap around to start */
272 if (pHashTable->tableSize == 1)
273 break; /* edge case - single-entry table */
274 pEntry = pHashTable->pEntries;
277 //LOGI("+++ del probing %d...\n", pEntry - pHashTable->pEntries);
284 * Scan every entry in the hash table and evaluate it with the specified
285 * indirect function call. If the function returns 1, remove the entry from
288 * Does NOT invoke the "free" function on the item.
290 * Returning values other than 0 or 1 will abort the routine.
292 int dvmHashForeachRemove(HashTable* pHashTable, HashForeachRemoveFunc func)
296 for (i = 0; i < pHashTable->tableSize; i++) {
297 HashEntry* pEnt = &pHashTable->pEntries[i];
299 if (pEnt->data != NULL && pEnt->data != HASH_TOMBSTONE) {
300 val = (*func)(pEnt->data);
302 pEnt->data = HASH_TOMBSTONE;
303 pHashTable->numEntries--;
304 pHashTable->numDeadEntries++;
316 * Execute a function on every entry in the hash table.
318 * If "func" returns a nonzero value, terminate early and return the value.
320 int dvmHashForeach(HashTable* pHashTable, HashForeachFunc func, void* arg)
324 for (i = 0; i < pHashTable->tableSize; i++) {
325 HashEntry* pEnt = &pHashTable->pEntries[i];
327 if (pEnt->data != NULL && pEnt->data != HASH_TOMBSTONE) {
328 val = (*func)(pEnt->data, arg);
339 * Look up an entry, counting the number of times we have to probe.
341 * Returns -1 if the entry wasn't found.
343 static int countProbes(HashTable* pHashTable, u4 itemHash, const void* item,
344 HashCompareFunc cmpFunc)
350 assert(pHashTable->tableSize > 0);
351 assert(item != HASH_TOMBSTONE);
352 assert(item != NULL);
354 /* jump to the first entry and probe for a match */
355 pEntry = &pHashTable->pEntries[itemHash & (pHashTable->tableSize-1)];
356 pEnd = &pHashTable->pEntries[pHashTable->tableSize];
357 while (pEntry->data != NULL) {
358 if (pEntry->data != HASH_TOMBSTONE &&
359 pEntry->hashValue == itemHash &&
360 (*cmpFunc)(pEntry->data, item) == 0)
367 if (pEntry == pEnd) { /* wrap around to start */
368 if (pHashTable->tableSize == 1)
369 break; /* edge case - single-entry table */
370 pEntry = pHashTable->pEntries;
375 if (pEntry->data == NULL)
382 * Evaluate the amount of probing required for the specified hash table.
384 * We do this by running through all entries in the hash table, computing
385 * the hash value and then doing a lookup.
387 * The caller should lock the table before calling here.
389 void dvmHashTableProbeCount(HashTable* pHashTable, HashCalcFunc calcFunc,
390 HashCompareFunc cmpFunc)
392 int numEntries, minProbe, maxProbe, totalProbe;
395 numEntries = maxProbe = totalProbe = 0;
396 minProbe = 65536*32767;
398 for (dvmHashIterBegin(pHashTable, &iter); !dvmHashIterDone(&iter);
399 dvmHashIterNext(&iter))
401 const void* data = (const void*)dvmHashIterData(&iter);
404 count = countProbes(pHashTable, (*calcFunc)(data), data, cmpFunc);
408 if (count < minProbe)
410 if (count > maxProbe)
415 LOGI("Probe: min=%d max=%d, total=%d in %d (%d), avg=%.3f\n",
416 minProbe, maxProbe, totalProbe, numEntries, pHashTable->tableSize,
417 (float) totalProbe / (float) numEntries);