2 * Copyright (c) 1999-2008 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 Copyright 1989-1996 NeXT Software, Inc.
26 Created by Bertrand Serlet, Feb 89
29 #include "objc-private.h"
30 #include "hashtable2.h"
32 /* In order to improve efficiency, buckets contain a pointer to an array or directly the data when the array size is 1 */
37 /* an optimization consists of storing directly data when count = 1 */
43 /* private data structure; may change */
45 /*************************************************************************
47 * Macros and utilities
49 *************************************************************************/
51 static unsigned log2u (unsigned x) { return (x<2) ? 0 : log2u (x>>1)+1; };
53 #define PTRSIZE sizeof(void *)
56 # define DEFAULT_ZONE NULL
57 # define ZONE_FROM_PTR(p) NULL
58 # define ALLOCTABLE(z) ((NXHashTable *) malloc (sizeof (NXHashTable)))
59 # define ALLOCBUCKETS(z,nb)((HashBucket *) calloc (nb, sizeof (HashBucket)))
60 /* Return interior pointer so a table of classes doesn't look like objects */
61 # define ALLOCPAIRS(z,nb) (1+(const void **) calloc (nb+1, sizeof (void *)))
62 # define FREEPAIRS(p) (free((void*)(-1+p)))
64 # define DEFAULT_ZONE malloc_default_zone()
65 # define ZONE_FROM_PTR(p) malloc_zone_from_ptr(p)
66 # define ALLOCTABLE(z) ((NXHashTable *) malloc_zone_malloc ((malloc_zone_t *)z,sizeof (NXHashTable)))
67 # define ALLOCBUCKETS(z,nb)((HashBucket *) malloc_zone_calloc ((malloc_zone_t *)z, nb, sizeof (HashBucket)))
68 /* Return interior pointer so a table of classes doesn't look like objects */
69 # define ALLOCPAIRS(z,nb) (1+(const void **) malloc_zone_calloc ((malloc_zone_t *)z, nb+1, sizeof (void *)))
70 # define FREEPAIRS(p) (free((void*)(-1+p)))
74 /* nbBuckets must be a power of 2 */
75 # define BUCKETOF(table, data) (((HashBucket *)table->buckets)+((*table->prototype->hash)(table->info, data) & (table->nbBuckets-1)))
76 # define GOOD_CAPACITY(c) (c <= 1 ? 1 : 1 << (log2u (c-1)+1))
77 # define MORE_CAPACITY(b) (b*2)
79 /* iff necessary this modulo can be optimized since the nbBuckets is of the form 2**n-1 */
80 # define BUCKETOF(table, data) (((HashBucket *)table->buckets)+((*table->prototype->hash)(table->info, data) % table->nbBuckets))
81 static unsigned exp2m1 (unsigned x) { return (1 << x) - 1; };
82 # define GOOD_CAPACITY(c) (exp2m1 (log2u (c)+1))
83 # define MORE_CAPACITY(b) (b*2+1)
86 #define ISEQUAL(table, data1, data2) ((data1 == data2) || (*table->prototype->isEqual)(table->info, data1, data2))
87 /* beware of double evaluation */
89 /*************************************************************************
91 * Global data and bootstrap
93 *************************************************************************/
95 static int isEqualPrototype (const void *info, const void *data1, const void *data2) {
96 NXHashTablePrototype *proto1 = (NXHashTablePrototype *) data1;
97 NXHashTablePrototype *proto2 = (NXHashTablePrototype *) data2;
99 return (proto1->hash == proto2->hash) && (proto1->isEqual == proto2->isEqual) && (proto1->free == proto2->free) && (proto1->style == proto2->style);
102 static uintptr_t hashPrototype (const void *info, const void *data) {
103 NXHashTablePrototype *proto = (NXHashTablePrototype *) data;
105 return NXPtrHash(info, (void*)proto->hash) ^ NXPtrHash(info, (void*)proto->isEqual) ^ NXPtrHash(info, (void*)proto->free) ^ (uintptr_t) proto->style;
108 void NXNoEffectFree (const void *info, void *data) {};
110 static NXHashTablePrototype protoPrototype = {
111 hashPrototype, isEqualPrototype, NXNoEffectFree, 0
114 static NXHashTable *prototypes = NULL;
115 /* table of all prototypes */
117 static void bootstrap (void) {
119 prototypes = ALLOCTABLE (DEFAULT_ZONE);
120 prototypes->prototype = &protoPrototype;
121 prototypes->count = 1;
122 prototypes->nbBuckets = 1; /* has to be 1 so that the right bucket is 0 */
123 prototypes->buckets = ALLOCBUCKETS(DEFAULT_ZONE, 1);
124 prototypes->info = NULL;
125 ((HashBucket *) prototypes->buckets)[0].count = 1;
126 ((HashBucket *) prototypes->buckets)[0].elements.one = &protoPrototype;
129 int NXPtrIsEqual (const void *info, const void *data1, const void *data2) {
130 return data1 == data2;
133 /*************************************************************************
137 *************************************************************************/
139 NXHashTable *NXCreateHashTable (NXHashTablePrototype prototype, unsigned capacity, const void *info) {
140 return NXCreateHashTableFromZone(prototype, capacity, info, DEFAULT_ZONE);
143 NXHashTable *NXCreateHashTableFromZone (NXHashTablePrototype prototype, unsigned capacity, const void *info, void *z) {
145 NXHashTablePrototype *proto;
147 table = ALLOCTABLE(z);
148 if (! prototypes) bootstrap ();
149 if (! prototype.hash) prototype.hash = NXPtrHash;
150 if (! prototype.isEqual) prototype.isEqual = NXPtrIsEqual;
151 if (! prototype.free) prototype.free = NXNoEffectFree;
152 if (prototype.style) {
153 _objc_inform ("*** NXCreateHashTable: invalid style\n");
156 proto = (NXHashTablePrototype *)NXHashGet (prototypes, &prototype);
159 = (NXHashTablePrototype *) malloc(sizeof (NXHashTablePrototype));
160 bcopy ((const char*)&prototype, (char*)proto, sizeof (NXHashTablePrototype));
161 (void) NXHashInsert (prototypes, proto);
162 proto = (NXHashTablePrototype *)NXHashGet (prototypes, &prototype);
164 _objc_inform ("*** NXCreateHashTable: bug\n");
168 table->prototype = proto; table->count = 0; table->info = info;
169 table->nbBuckets = GOOD_CAPACITY(capacity);
170 table->buckets = ALLOCBUCKETS(z, table->nbBuckets);
174 static void freeBucketPairs (void (*freeProc)(const void *info, void *data), HashBucket bucket, const void *info) {
175 unsigned j = bucket.count;
179 (*freeProc) (info, (void *) bucket.elements.one);
182 pairs = bucket.elements.many;
184 (*freeProc) (info, (void *) *pairs);
187 FREEPAIRS (bucket.elements.many);
190 static void freeBuckets (NXHashTable *table, int freeObjects) {
191 unsigned i = table->nbBuckets;
192 HashBucket *buckets = (HashBucket *) table->buckets;
195 if (buckets->count) {
196 freeBucketPairs ((freeObjects) ? table->prototype->free : NXNoEffectFree, *buckets, table->info);
198 buckets->elements.one = NULL;
204 void NXFreeHashTable (NXHashTable *table) {
205 freeBuckets (table, YES);
206 free (table->buckets);
210 void NXEmptyHashTable (NXHashTable *table) {
211 freeBuckets (table, NO);
215 void NXResetHashTable (NXHashTable *table) {
216 freeBuckets (table, YES);
220 BOOL NXCompareHashTables (NXHashTable *table1, NXHashTable *table2) {
221 if (table1 == table2) return YES;
222 if (NXCountHashTable (table1) != NXCountHashTable (table2)) return NO;
225 NXHashState state = NXInitHashState (table1);
226 while (NXNextHashState (table1, &state, &data)) {
227 if (! NXHashMember (table2, data)) return NO;
233 NXHashTable *NXCopyHashTable (NXHashTable *table) {
235 NXHashState state = NXInitHashState (table);
237 __unused void *z = ZONE_FROM_PTR(table);
239 newt = ALLOCTABLE(z);
240 newt->prototype = table->prototype; newt->count = 0;
241 newt->info = table->info;
242 newt->nbBuckets = table->nbBuckets;
243 newt->buckets = ALLOCBUCKETS(z, newt->nbBuckets);
244 while (NXNextHashState (table, &state, &data))
245 NXHashInsert (newt, data);
249 unsigned NXCountHashTable (NXHashTable *table) {
253 int NXHashMember (NXHashTable *table, const void *data) {
254 HashBucket *bucket = BUCKETOF(table, data);
255 unsigned j = bucket->count;
260 return ISEQUAL(table, data, bucket->elements.one);
262 pairs = bucket->elements.many;
264 /* we don't cache isEqual because lists are short */
265 if (ISEQUAL(table, data, *pairs)) return 1;
271 void *NXHashGet (NXHashTable *table, const void *data) {
272 HashBucket *bucket = BUCKETOF(table, data);
273 unsigned j = bucket->count;
276 if (! j) return NULL;
278 return ISEQUAL(table, data, bucket->elements.one)
279 ? (void *) bucket->elements.one : NULL;
281 pairs = bucket->elements.many;
283 /* we don't cache isEqual because lists are short */
284 if (ISEQUAL(table, data, *pairs)) return (void *) *pairs;
290 unsigned _NXHashCapacity (NXHashTable *table) {
291 return table->nbBuckets;
294 void _NXHashRehashToCapacity (NXHashTable *table, unsigned newCapacity) {
295 /* Rehash: we create a pseudo table pointing really to the old guys,
296 extend self, copy the old pairs, and free the pseudo table */
300 __unused void *z = ZONE_FROM_PTR(table);
303 old->prototype = table->prototype; old->count = table->count;
304 old->nbBuckets = table->nbBuckets; old->buckets = table->buckets;
305 table->nbBuckets = newCapacity;
306 table->count = 0; table->buckets = ALLOCBUCKETS(z, table->nbBuckets);
307 state = NXInitHashState (old);
308 while (NXNextHashState (old, &state, &aux))
309 (void) NXHashInsert (table, aux);
310 freeBuckets (old, NO);
311 if (old->count != table->count)
312 _objc_inform("*** hashtable: count differs after rehashing; probably indicates a broken invariant: there are x and y such as isEqual(x, y) is TRUE but hash(x) != hash (y)\n");
317 static void _NXHashRehash (NXHashTable *table) {
318 _NXHashRehashToCapacity (table, MORE_CAPACITY(table->nbBuckets));
321 void *NXHashInsert (NXHashTable *table, const void *data) {
322 HashBucket *bucket = BUCKETOF(table, data);
323 unsigned j = bucket->count;
326 __unused void *z = ZONE_FROM_PTR(table);
329 bucket->count++; bucket->elements.one = data;
334 if (ISEQUAL(table, data, bucket->elements.one)) {
335 const void *old = bucket->elements.one;
336 bucket->elements.one = data;
339 newt = ALLOCPAIRS(z, 2);
340 newt[1] = bucket->elements.one;
342 bucket->count++; bucket->elements.many = newt;
344 if (table->count > table->nbBuckets) _NXHashRehash (table);
347 pairs = bucket->elements.many;
349 /* we don't cache isEqual because lists are short */
350 if (ISEQUAL(table, data, *pairs)) {
351 const void *old = *pairs;
357 /* we enlarge this bucket; and put new data in front */
358 newt = ALLOCPAIRS(z, bucket->count+1);
359 if (bucket->count) bcopy ((const char*)bucket->elements.many, (char*)(newt+1), bucket->count * PTRSIZE);
361 FREEPAIRS (bucket->elements.many);
362 bucket->count++; bucket->elements.many = newt;
364 if (table->count > table->nbBuckets) _NXHashRehash (table);
368 void *NXHashInsertIfAbsent (NXHashTable *table, const void *data) {
369 HashBucket *bucket = BUCKETOF(table, data);
370 unsigned j = bucket->count;
373 __unused void *z = ZONE_FROM_PTR(table);
376 bucket->count++; bucket->elements.one = data;
378 return (void *) data;
381 if (ISEQUAL(table, data, bucket->elements.one))
382 return (void *) bucket->elements.one;
383 newt = ALLOCPAIRS(z, 2);
384 newt[1] = bucket->elements.one;
386 bucket->count++; bucket->elements.many = newt;
388 if (table->count > table->nbBuckets) _NXHashRehash (table);
389 return (void *) data;
391 pairs = bucket->elements.many;
393 /* we don't cache isEqual because lists are short */
394 if (ISEQUAL(table, data, *pairs))
395 return (void *) *pairs;
398 /* we enlarge this bucket; and put new data in front */
399 newt = ALLOCPAIRS(z, bucket->count+1);
400 if (bucket->count) bcopy ((const char*)bucket->elements.many, (char*)(newt+1), bucket->count * PTRSIZE);
402 FREEPAIRS (bucket->elements.many);
403 bucket->count++; bucket->elements.many = newt;
405 if (table->count > table->nbBuckets) _NXHashRehash (table);
406 return (void *) data;
409 void *NXHashRemove (NXHashTable *table, const void *data) {
410 HashBucket *bucket = BUCKETOF(table, data);
411 unsigned j = bucket->count;
414 __unused void *z = ZONE_FROM_PTR(table);
416 if (! j) return NULL;
418 if (! ISEQUAL(table, data, bucket->elements.one)) return NULL;
419 data = bucket->elements.one;
420 table->count--; bucket->count--; bucket->elements.one = NULL;
421 return (void *) data;
423 pairs = bucket->elements.many;
425 if (ISEQUAL(table, data, pairs[0])) {
426 bucket->elements.one = pairs[1]; data = pairs[0];
428 else if (ISEQUAL(table, data, pairs[1])) {
429 bucket->elements.one = pairs[0]; data = pairs[1];
433 table->count--; bucket->count--;
434 return (void *) data;
437 if (ISEQUAL(table, data, *pairs)) {
439 /* we shrink this bucket */
440 newt = (bucket->count-1)
441 ? ALLOCPAIRS(z, bucket->count-1) : NULL;
442 if (bucket->count-1 != j)
443 bcopy ((const char*)bucket->elements.many, (char*)newt, PTRSIZE*(bucket->count-j-1));
445 bcopy ((const char*)(bucket->elements.many + bucket->count-j), (char*)(newt+bucket->count-j-1), PTRSIZE*j);
446 FREEPAIRS (bucket->elements.many);
447 table->count--; bucket->count--; bucket->elements.many = newt;
448 return (void *) data;
455 NXHashState NXInitHashState (NXHashTable *table) {
458 state.i = table->nbBuckets;
463 int NXNextHashState (NXHashTable *table, NXHashState *state, void **data) {
464 HashBucket *buckets = (HashBucket *) table->buckets;
466 while (state->j == 0) {
467 if (state->i == 0) return NO;
468 state->i--; state->j = buckets[state->i].count;
472 *data = (void *) ((buckets->count == 1)
473 ? buckets->elements.one : buckets->elements.many[state->j]);
477 /*************************************************************************
481 *************************************************************************/
483 uintptr_t NXPtrHash (const void *info, const void *data) {
484 return (((uintptr_t) data) >> 16) ^ ((uintptr_t) data);
487 uintptr_t NXStrHash (const void *info, const void *data) {
488 register uintptr_t hash = 0;
489 register unsigned char *s = (unsigned char *) data;
490 /* unsigned to avoid a sign-extend */
491 /* unroll the loop */
493 if (*s == '\0') break;
494 hash ^= (uintptr_t) *s++;
495 if (*s == '\0') break;
496 hash ^= (uintptr_t) *s++ << 8;
497 if (*s == '\0') break;
498 hash ^= (uintptr_t) *s++ << 16;
499 if (*s == '\0') break;
500 hash ^= (uintptr_t) *s++ << 24;
505 int NXStrIsEqual (const void *info, const void *data1, const void *data2) {
506 if (data1 == data2) return YES;
507 if (! data1) return ! strlen ((char *) data2);
508 if (! data2) return ! strlen ((char *) data1);
509 if (((char *) data1)[0] != ((char *) data2)[0]) return NO;
510 return (strcmp ((char *) data1, (char *) data2)) ? NO : YES;
513 void NXReallyFree (const void *info, void *data) {
517 /* All the following functions are really private, made non-static only for the benefit of shlibs */
518 static uintptr_t hashPtrStructKey (const void *info, const void *data) {
519 return NXPtrHash(info, *((void **) data));
522 static int isEqualPtrStructKey (const void *info, const void *data1, const void *data2) {
523 return NXPtrIsEqual (info, *((void **) data1), *((void **) data2));
526 static uintptr_t hashStrStructKey (const void *info, const void *data) {
527 return NXStrHash(info, *((char **) data));
530 static int isEqualStrStructKey (const void *info, const void *data1, const void *data2) {
531 return NXStrIsEqual (info, *((char **) data1), *((char **) data2));
534 const NXHashTablePrototype NXPtrPrototype = {
535 NXPtrHash, NXPtrIsEqual, NXNoEffectFree, 0
538 const NXHashTablePrototype NXStrPrototype = {
539 NXStrHash, NXStrIsEqual, NXNoEffectFree, 0
542 const NXHashTablePrototype NXPtrStructKeyPrototype = {
543 hashPtrStructKey, isEqualPtrStructKey, NXReallyFree, 0
546 const NXHashTablePrototype NXStrStructKeyPrototype = {
547 hashStrStructKey, isEqualStrStructKey, NXReallyFree, 0
550 /*************************************************************************
554 *************************************************************************/
556 #if !__OBJC2__ && !TARGET_OS_WIN32
558 /* the implementation could be made faster at the expense of memory if the size of the strings were kept around */
559 static NXHashTable *uniqueStrings = NULL;
561 /* this is based on most apps using a few K of strings, and an average string size of 15 using sqrt(2*dataAlloced*perChunkOverhead) */
562 #define CHUNK_SIZE 360
564 static int accessUniqueString = 0;
566 static char *z = NULL;
567 static size_t zSize = 0;
568 static mutex_t lock = MUTEX_INITIALIZER;
570 static const char *CopyIntoReadOnly (const char *str) {
571 size_t len = strlen (str) + 1;
574 if (len > CHUNK_SIZE/2) { /* dont let big strings waste space */
575 result = (char *)malloc (len);
576 bcopy (str, result, len);
582 zSize = CHUNK_SIZE *((len + CHUNK_SIZE - 1) / CHUNK_SIZE);
583 /* not enough room, we try to allocate. If no room left, too bad */
584 z = (char *)malloc (zSize);
588 bcopy (str, result, len);
591 mutex_unlock (&lock);
595 NXAtom NXUniqueString (const char *buffer) {
596 const char *previous;
598 if (! buffer) return buffer;
599 accessUniqueString++;
601 uniqueStrings = NXCreateHashTable (NXStrPrototype, 0, NULL);
602 previous = (const char *) NXHashGet (uniqueStrings, buffer);
603 if (previous) return previous;
604 previous = CopyIntoReadOnly (buffer);
605 if (NXHashInsert (uniqueStrings, previous)) {
606 _objc_inform ("*** NXUniqueString: invariant broken\n");
612 NXAtom NXUniqueStringNoCopy (const char *string) {
613 accessUniqueString++;
615 uniqueStrings = NXCreateHashTable (NXStrPrototype, 0, NULL);
616 return (const char *) NXHashInsertIfAbsent (uniqueStrings, string);
621 NXAtom NXUniqueStringWithLength (const char *buffer, int length) {
624 char stackBuf[BUF_SIZE];
626 if (length+1 > BUF_SIZE)
627 nullTermStr = (char *)malloc (length+1);
629 nullTermStr = stackBuf;
630 bcopy (buffer, nullTermStr, length);
631 nullTermStr[length] = '\0';
632 atom = NXUniqueString (nullTermStr);
633 if (length+1 > BUF_SIZE)
638 char *NXCopyStringBufferFromZone (const char *str, void *zone) {
642 return strcpy ((char *) malloc_zone_malloc((malloc_zone_t *)zone, strlen (str) + 1), str);
646 char *NXCopyStringBuffer (const char *str) {