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*) calloc(pHashTable->tableSize, sizeof(HashEntry));
61 if (pHashTable->pEntries == NULL) {
70 * Clear out all entries.
72 void dvmHashTableClear(HashTable* pHashTable)
77 pEnt = pHashTable->pEntries;
78 for (i = 0; i < pHashTable->tableSize; i++, pEnt++) {
79 if (pEnt->data == HASH_TOMBSTONE) {
82 } else if (pEnt->data != NULL) {
83 // call free func then nuke entry
84 if (pHashTable->freeFunc != NULL)
85 (*pHashTable->freeFunc)(pEnt->data);
90 pHashTable->numEntries = 0;
91 pHashTable->numDeadEntries = 0;
97 void dvmHashTableFree(HashTable* pHashTable)
99 if (pHashTable == NULL)
101 dvmHashTableClear(pHashTable);
102 free(pHashTable->pEntries);
108 * Count up the number of tombstone entries in the hash table.
110 static int countTombStones(HashTable* pHashTable)
114 for (count = i = 0; i < pHashTable->tableSize; i++) {
115 if (pHashTable->pEntries[i].data == HASH_TOMBSTONE)
123 * Resize a hash table. We do this when adding an entry increased the
124 * size of the table beyond its comfy limit.
126 * This essentially requires re-inserting all elements into the new storage.
128 * If multiple threads can access the hash table, the table's lock should
129 * have been grabbed before issuing the "lookup+add" call that led to the
130 * resize, so we don't have a synchronization problem here.
132 static bool resizeHash(HashTable* pHashTable, int newSize)
134 HashEntry* pNewEntries;
137 assert(countTombStones(pHashTable) == pHashTable->numDeadEntries);
138 //ALOGI("before: dead=%d", pHashTable->numDeadEntries);
140 pNewEntries = (HashEntry*) calloc(newSize, sizeof(HashEntry));
141 if (pNewEntries == NULL)
144 for (i = 0; i < pHashTable->tableSize; i++) {
145 void* data = pHashTable->pEntries[i].data;
146 if (data != NULL && data != HASH_TOMBSTONE) {
147 int hashValue = pHashTable->pEntries[i].hashValue;
150 /* probe for new spot, wrapping around */
151 newIdx = hashValue & (newSize-1);
152 while (pNewEntries[newIdx].data != NULL)
153 newIdx = (newIdx + 1) & (newSize-1);
155 pNewEntries[newIdx].hashValue = hashValue;
156 pNewEntries[newIdx].data = data;
160 free(pHashTable->pEntries);
161 pHashTable->pEntries = pNewEntries;
162 pHashTable->tableSize = newSize;
163 pHashTable->numDeadEntries = 0;
165 assert(countTombStones(pHashTable) == 0);
172 * We probe on collisions, wrapping around the table.
174 void* dvmHashTableLookup(HashTable* pHashTable, u4 itemHash, void* item,
175 HashCompareFunc cmpFunc, bool doAdd)
181 assert(pHashTable->tableSize > 0);
182 assert(item != HASH_TOMBSTONE);
183 assert(item != NULL);
185 /* jump to the first entry and probe for a match */
186 pEntry = &pHashTable->pEntries[itemHash & (pHashTable->tableSize-1)];
187 pEnd = &pHashTable->pEntries[pHashTable->tableSize];
188 while (pEntry->data != NULL) {
189 if (pEntry->data != HASH_TOMBSTONE &&
190 pEntry->hashValue == itemHash &&
191 (*cmpFunc)(pEntry->data, item) == 0)
194 //ALOGD("+++ match on entry %d", pEntry - pHashTable->pEntries);
199 if (pEntry == pEnd) { /* wrap around to start */
200 if (pHashTable->tableSize == 1)
201 break; /* edge case - single-entry table */
202 pEntry = pHashTable->pEntries;
205 //ALOGI("+++ look probing %d...", pEntry - pHashTable->pEntries);
208 if (pEntry->data == NULL) {
210 pEntry->hashValue = itemHash;
212 pHashTable->numEntries++;
215 * We've added an entry. See if this brings us too close to full.
217 if ((pHashTable->numEntries+pHashTable->numDeadEntries) * LOAD_DENOM
218 > pHashTable->tableSize * LOAD_NUMER)
220 if (!resizeHash(pHashTable, pHashTable->tableSize * 2)) {
221 /* don't really have a way to indicate failure */
222 ALOGE("Dalvik hash resize failure");
225 /* note "pEntry" is now invalid */
227 //ALOGW("okay %d/%d/%d",
228 // pHashTable->numEntries, pHashTable->tableSize,
229 // (pHashTable->tableSize * LOAD_NUMER) / LOAD_DENOM);
232 /* full table is bad -- search for nonexistent never halts */
233 assert(pHashTable->numEntries < pHashTable->tableSize);
236 assert(result == NULL);
239 result = pEntry->data;
246 * Remove an entry from the table.
248 * Does NOT invoke the "free" function on the item.
250 bool dvmHashTableRemove(HashTable* pHashTable, u4 itemHash, void* item)
255 assert(pHashTable->tableSize > 0);
257 /* jump to the first entry and probe for a match */
258 pEntry = &pHashTable->pEntries[itemHash & (pHashTable->tableSize-1)];
259 pEnd = &pHashTable->pEntries[pHashTable->tableSize];
260 while (pEntry->data != NULL) {
261 if (pEntry->data == item) {
262 //ALOGI("+++ stepping on entry %d", pEntry - pHashTable->pEntries);
263 pEntry->data = HASH_TOMBSTONE;
264 pHashTable->numEntries--;
265 pHashTable->numDeadEntries++;
270 if (pEntry == pEnd) { /* wrap around to start */
271 if (pHashTable->tableSize == 1)
272 break; /* edge case - single-entry table */
273 pEntry = pHashTable->pEntries;
276 //ALOGI("+++ del probing %d...", pEntry - pHashTable->pEntries);
283 * Scan every entry in the hash table and evaluate it with the specified
284 * indirect function call. If the function returns 1, remove the entry from
287 * Does NOT invoke the "free" function on the item.
289 * Returning values other than 0 or 1 will abort the routine.
291 int dvmHashForeachRemove(HashTable* pHashTable, HashForeachRemoveFunc func)
295 for (i = 0; i < pHashTable->tableSize; i++) {
296 HashEntry* pEnt = &pHashTable->pEntries[i];
298 if (pEnt->data != NULL && pEnt->data != HASH_TOMBSTONE) {
299 val = (*func)(pEnt->data);
301 pEnt->data = HASH_TOMBSTONE;
302 pHashTable->numEntries--;
303 pHashTable->numDeadEntries++;
315 * Execute a function on every entry in the hash table.
317 * If "func" returns a nonzero value, terminate early and return the value.
319 int dvmHashForeach(HashTable* pHashTable, HashForeachFunc func, void* arg)
323 for (i = 0; i < pHashTable->tableSize; i++) {
324 HashEntry* pEnt = &pHashTable->pEntries[i];
326 if (pEnt->data != NULL && pEnt->data != HASH_TOMBSTONE) {
327 val = (*func)(pEnt->data, arg);
338 * Look up an entry, counting the number of times we have to probe.
340 * Returns -1 if the entry wasn't found.
342 static int countProbes(HashTable* pHashTable, u4 itemHash, const void* item,
343 HashCompareFunc cmpFunc)
349 assert(pHashTable->tableSize > 0);
350 assert(item != HASH_TOMBSTONE);
351 assert(item != NULL);
353 /* jump to the first entry and probe for a match */
354 pEntry = &pHashTable->pEntries[itemHash & (pHashTable->tableSize-1)];
355 pEnd = &pHashTable->pEntries[pHashTable->tableSize];
356 while (pEntry->data != NULL) {
357 if (pEntry->data != HASH_TOMBSTONE &&
358 pEntry->hashValue == itemHash &&
359 (*cmpFunc)(pEntry->data, item) == 0)
366 if (pEntry == pEnd) { /* wrap around to start */
367 if (pHashTable->tableSize == 1)
368 break; /* edge case - single-entry table */
369 pEntry = pHashTable->pEntries;
374 if (pEntry->data == NULL)
381 * Evaluate the amount of probing required for the specified hash table.
383 * We do this by running through all entries in the hash table, computing
384 * the hash value and then doing a lookup.
386 * The caller should lock the table before calling here.
388 void dvmHashTableProbeCount(HashTable* pHashTable, HashCalcFunc calcFunc,
389 HashCompareFunc cmpFunc)
391 int numEntries, minProbe, maxProbe, totalProbe;
394 numEntries = maxProbe = totalProbe = 0;
395 minProbe = 65536*32767;
397 for (dvmHashIterBegin(pHashTable, &iter); !dvmHashIterDone(&iter);
398 dvmHashIterNext(&iter))
400 const void* data = (const void*)dvmHashIterData(&iter);
403 count = countProbes(pHashTable, (*calcFunc)(data), data, cmpFunc);
407 if (count < minProbe)
409 if (count > maxProbe)
414 ALOGI("Probe: min=%d max=%d, total=%d in %d (%d), avg=%.3f",
415 minProbe, maxProbe, totalProbe, numEntries, pHashTable->tableSize,
416 (float) totalProbe / (float) numEntries);