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 #define PTRSIZE sizeof(void *)
54 # define DEFAULT_ZONE NULL
55 # define ZONE_FROM_PTR(p) NULL
56 # define ALLOCTABLE(z) ((NXHashTable *) malloc (sizeof (NXHashTable)))
57 # define ALLOCBUCKETS(z,nb)((HashBucket *) calloc (nb, sizeof (HashBucket)))
58 /* Return interior pointer so a table of classes doesn't look like objects */
59 # define ALLOCPAIRS(z,nb) (1+(const void **) calloc (nb+1, sizeof (void *)))
60 # define FREEPAIRS(p) (free((void*)(-1+p)))
62 # define DEFAULT_ZONE malloc_default_zone()
63 # define ZONE_FROM_PTR(p) malloc_zone_from_ptr(p)
64 # define ALLOCTABLE(z) ((NXHashTable *) malloc_zone_malloc ((malloc_zone_t *)z,sizeof (NXHashTable)))
65 # define ALLOCBUCKETS(z,nb)((HashBucket *) malloc_zone_calloc ((malloc_zone_t *)z, nb, sizeof (HashBucket)))
66 /* Return interior pointer so a table of classes doesn't look like objects */
67 # define ALLOCPAIRS(z,nb) (1+(const void **) malloc_zone_calloc ((malloc_zone_t *)z, nb+1, sizeof (void *)))
68 # define FREEPAIRS(p) (free((void*)(-1+p)))
72 /* nbBuckets must be a power of 2 */
73 # define BUCKETOF(table, data) (((HashBucket *)table->buckets)+((*table->prototype->hash)(table->info, data) & (table->nbBuckets-1)))
74 # define GOOD_CAPACITY(c) (c <= 1 ? 1 : 1 << (log2u (c-1)+1))
75 # define MORE_CAPACITY(b) (b*2)
77 /* iff necessary this modulo can be optimized since the nbBuckets is of the form 2**n-1 */
78 # define BUCKETOF(table, data) (((HashBucket *)table->buckets)+((*table->prototype->hash)(table->info, data) % table->nbBuckets))
79 # define GOOD_CAPACITY(c) (exp2m1u (log2u (c)+1))
80 # define MORE_CAPACITY(b) (b*2+1)
83 #define ISEQUAL(table, data1, data2) ((data1 == data2) || (*table->prototype->isEqual)(table->info, data1, data2))
84 /* beware of double evaluation */
86 /*************************************************************************
88 * Global data and bootstrap
90 *************************************************************************/
92 static int isEqualPrototype (const void *info, const void *data1, const void *data2) {
93 NXHashTablePrototype *proto1 = (NXHashTablePrototype *) data1;
94 NXHashTablePrototype *proto2 = (NXHashTablePrototype *) data2;
96 return (proto1->hash == proto2->hash) && (proto1->isEqual == proto2->isEqual) && (proto1->free == proto2->free) && (proto1->style == proto2->style);
99 static uintptr_t hashPrototype (const void *info, const void *data) {
100 NXHashTablePrototype *proto = (NXHashTablePrototype *) data;
102 return NXPtrHash(info, (void*)proto->hash) ^ NXPtrHash(info, (void*)proto->isEqual) ^ NXPtrHash(info, (void*)proto->free) ^ (uintptr_t) proto->style;
105 void NXNoEffectFree (const void *info, void *data) {};
107 static NXHashTablePrototype protoPrototype = {
108 hashPrototype, isEqualPrototype, NXNoEffectFree, 0
111 static NXHashTable *prototypes = NULL;
112 /* table of all prototypes */
114 static void bootstrap (void) {
116 prototypes = ALLOCTABLE (DEFAULT_ZONE);
117 prototypes->prototype = &protoPrototype;
118 prototypes->count = 1;
119 prototypes->nbBuckets = 1; /* has to be 1 so that the right bucket is 0 */
120 prototypes->buckets = ALLOCBUCKETS(DEFAULT_ZONE, 1);
121 prototypes->info = NULL;
122 ((HashBucket *) prototypes->buckets)[0].count = 1;
123 ((HashBucket *) prototypes->buckets)[0].elements.one = &protoPrototype;
126 int NXPtrIsEqual (const void *info, const void *data1, const void *data2) {
127 return data1 == data2;
130 /*************************************************************************
134 *************************************************************************/
136 NXHashTable *NXCreateHashTable (NXHashTablePrototype prototype, unsigned capacity, const void *info) {
137 return NXCreateHashTableFromZone(prototype, capacity, info, DEFAULT_ZONE);
140 NXHashTable *NXCreateHashTableFromZone (NXHashTablePrototype prototype, unsigned capacity, const void *info, void *z) {
142 NXHashTablePrototype *proto;
144 table = ALLOCTABLE(z);
145 if (! prototypes) bootstrap ();
146 if (! prototype.hash) prototype.hash = NXPtrHash;
147 if (! prototype.isEqual) prototype.isEqual = NXPtrIsEqual;
148 if (! prototype.free) prototype.free = NXNoEffectFree;
149 if (prototype.style) {
150 _objc_inform ("*** NXCreateHashTable: invalid style\n");
153 proto = (NXHashTablePrototype *)NXHashGet (prototypes, &prototype);
156 = (NXHashTablePrototype *) malloc(sizeof (NXHashTablePrototype));
157 bcopy ((const char*)&prototype, (char*)proto, sizeof (NXHashTablePrototype));
158 (void) NXHashInsert (prototypes, proto);
159 proto = (NXHashTablePrototype *)NXHashGet (prototypes, &prototype);
161 _objc_inform ("*** NXCreateHashTable: bug\n");
165 table->prototype = proto; table->count = 0; table->info = info;
166 table->nbBuckets = GOOD_CAPACITY(capacity);
167 table->buckets = ALLOCBUCKETS(z, table->nbBuckets);
171 static void freeBucketPairs (void (*freeProc)(const void *info, void *data), HashBucket bucket, const void *info) {
172 unsigned j = bucket.count;
176 (*freeProc) (info, (void *) bucket.elements.one);
179 pairs = bucket.elements.many;
181 (*freeProc) (info, (void *) *pairs);
184 FREEPAIRS (bucket.elements.many);
187 static void freeBuckets (NXHashTable *table, int freeObjects) {
188 unsigned i = table->nbBuckets;
189 HashBucket *buckets = (HashBucket *) table->buckets;
192 if (buckets->count) {
193 freeBucketPairs ((freeObjects) ? table->prototype->free : NXNoEffectFree, *buckets, table->info);
195 buckets->elements.one = NULL;
201 void NXFreeHashTable (NXHashTable *table) {
202 freeBuckets (table, YES);
203 free (table->buckets);
207 void NXEmptyHashTable (NXHashTable *table) {
208 freeBuckets (table, NO);
212 void NXResetHashTable (NXHashTable *table) {
213 freeBuckets (table, YES);
217 BOOL NXCompareHashTables (NXHashTable *table1, NXHashTable *table2) {
218 if (table1 == table2) return YES;
219 if (NXCountHashTable (table1) != NXCountHashTable (table2)) return NO;
222 NXHashState state = NXInitHashState (table1);
223 while (NXNextHashState (table1, &state, &data)) {
224 if (! NXHashMember (table2, data)) return NO;
230 NXHashTable *NXCopyHashTable (NXHashTable *table) {
232 NXHashState state = NXInitHashState (table);
234 __unused void *z = ZONE_FROM_PTR(table);
236 newt = ALLOCTABLE(z);
237 newt->prototype = table->prototype; newt->count = 0;
238 newt->info = table->info;
239 newt->nbBuckets = table->nbBuckets;
240 newt->buckets = ALLOCBUCKETS(z, newt->nbBuckets);
241 while (NXNextHashState (table, &state, &data))
242 NXHashInsert (newt, data);
246 unsigned NXCountHashTable (NXHashTable *table) {
250 int NXHashMember (NXHashTable *table, const void *data) {
251 HashBucket *bucket = BUCKETOF(table, data);
252 unsigned j = bucket->count;
257 return ISEQUAL(table, data, bucket->elements.one);
259 pairs = bucket->elements.many;
261 /* we don't cache isEqual because lists are short */
262 if (ISEQUAL(table, data, *pairs)) return 1;
268 void *NXHashGet (NXHashTable *table, const void *data) {
269 HashBucket *bucket = BUCKETOF(table, data);
270 unsigned j = bucket->count;
273 if (! j) return NULL;
275 return ISEQUAL(table, data, bucket->elements.one)
276 ? (void *) bucket->elements.one : NULL;
278 pairs = bucket->elements.many;
280 /* we don't cache isEqual because lists are short */
281 if (ISEQUAL(table, data, *pairs)) return (void *) *pairs;
287 unsigned _NXHashCapacity (NXHashTable *table) {
288 return table->nbBuckets;
291 void _NXHashRehashToCapacity (NXHashTable *table, unsigned newCapacity) {
292 /* Rehash: we create a pseudo table pointing really to the old guys,
293 extend self, copy the old pairs, and free the pseudo table */
297 __unused void *z = ZONE_FROM_PTR(table);
300 old->prototype = table->prototype; old->count = table->count;
301 old->nbBuckets = table->nbBuckets; old->buckets = table->buckets;
302 table->nbBuckets = newCapacity;
303 table->count = 0; table->buckets = ALLOCBUCKETS(z, table->nbBuckets);
304 state = NXInitHashState (old);
305 while (NXNextHashState (old, &state, &aux))
306 (void) NXHashInsert (table, aux);
307 freeBuckets (old, NO);
308 if (old->count != table->count)
309 _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");
314 static void _NXHashRehash (NXHashTable *table) {
315 _NXHashRehashToCapacity (table, MORE_CAPACITY(table->nbBuckets));
318 void *NXHashInsert (NXHashTable *table, const void *data) {
319 HashBucket *bucket = BUCKETOF(table, data);
320 unsigned j = bucket->count;
323 __unused void *z = ZONE_FROM_PTR(table);
326 bucket->count++; bucket->elements.one = data;
331 if (ISEQUAL(table, data, bucket->elements.one)) {
332 const void *old = bucket->elements.one;
333 bucket->elements.one = data;
336 newt = ALLOCPAIRS(z, 2);
337 newt[1] = bucket->elements.one;
339 bucket->count++; bucket->elements.many = newt;
341 if (table->count > table->nbBuckets) _NXHashRehash (table);
344 pairs = bucket->elements.many;
346 /* we don't cache isEqual because lists are short */
347 if (ISEQUAL(table, data, *pairs)) {
348 const void *old = *pairs;
354 /* we enlarge this bucket; and put new data in front */
355 newt = ALLOCPAIRS(z, bucket->count+1);
356 if (bucket->count) bcopy ((const char*)bucket->elements.many, (char*)(newt+1), bucket->count * PTRSIZE);
358 FREEPAIRS (bucket->elements.many);
359 bucket->count++; bucket->elements.many = newt;
361 if (table->count > table->nbBuckets) _NXHashRehash (table);
365 void *NXHashInsertIfAbsent (NXHashTable *table, const void *data) {
366 HashBucket *bucket = BUCKETOF(table, data);
367 unsigned j = bucket->count;
370 __unused void *z = ZONE_FROM_PTR(table);
373 bucket->count++; bucket->elements.one = data;
375 return (void *) data;
378 if (ISEQUAL(table, data, bucket->elements.one))
379 return (void *) bucket->elements.one;
380 newt = ALLOCPAIRS(z, 2);
381 newt[1] = bucket->elements.one;
383 bucket->count++; bucket->elements.many = newt;
385 if (table->count > table->nbBuckets) _NXHashRehash (table);
386 return (void *) data;
388 pairs = bucket->elements.many;
390 /* we don't cache isEqual because lists are short */
391 if (ISEQUAL(table, data, *pairs))
392 return (void *) *pairs;
395 /* we enlarge this bucket; and put new data in front */
396 newt = ALLOCPAIRS(z, bucket->count+1);
397 if (bucket->count) bcopy ((const char*)bucket->elements.many, (char*)(newt+1), bucket->count * PTRSIZE);
399 FREEPAIRS (bucket->elements.many);
400 bucket->count++; bucket->elements.many = newt;
402 if (table->count > table->nbBuckets) _NXHashRehash (table);
403 return (void *) data;
406 void *NXHashRemove (NXHashTable *table, const void *data) {
407 HashBucket *bucket = BUCKETOF(table, data);
408 unsigned j = bucket->count;
411 __unused void *z = ZONE_FROM_PTR(table);
413 if (! j) return NULL;
415 if (! ISEQUAL(table, data, bucket->elements.one)) return NULL;
416 data = bucket->elements.one;
417 table->count--; bucket->count--; bucket->elements.one = NULL;
418 return (void *) data;
420 pairs = bucket->elements.many;
422 if (ISEQUAL(table, data, pairs[0])) {
423 bucket->elements.one = pairs[1]; data = pairs[0];
425 else if (ISEQUAL(table, data, pairs[1])) {
426 bucket->elements.one = pairs[0]; data = pairs[1];
430 table->count--; bucket->count--;
431 return (void *) data;
434 if (ISEQUAL(table, data, *pairs)) {
436 /* we shrink this bucket */
437 newt = (bucket->count-1)
438 ? ALLOCPAIRS(z, bucket->count-1) : NULL;
439 if (bucket->count-1 != j)
440 bcopy ((const char*)bucket->elements.many, (char*)newt, PTRSIZE*(bucket->count-j-1));
442 bcopy ((const char*)(bucket->elements.many + bucket->count-j), (char*)(newt+bucket->count-j-1), PTRSIZE*j);
443 FREEPAIRS (bucket->elements.many);
444 table->count--; bucket->count--; bucket->elements.many = newt;
445 return (void *) data;
452 NXHashState NXInitHashState (NXHashTable *table) {
455 state.i = table->nbBuckets;
460 int NXNextHashState (NXHashTable *table, NXHashState *state, void **data) {
461 HashBucket *buckets = (HashBucket *) table->buckets;
463 while (state->j == 0) {
464 if (state->i == 0) return NO;
465 state->i--; state->j = buckets[state->i].count;
469 *data = (void *) ((buckets->count == 1)
470 ? buckets->elements.one : buckets->elements.many[state->j]);
474 /*************************************************************************
478 *************************************************************************/
480 uintptr_t NXPtrHash (const void *info, const void *data) {
481 return (((uintptr_t) data) >> 16) ^ ((uintptr_t) data);
484 uintptr_t NXStrHash (const void *info, const void *data) {
486 unsigned char *s = (unsigned char *) data;
487 /* unsigned to avoid a sign-extend */
488 /* unroll the loop */
490 if (*s == '\0') break;
491 hash ^= (uintptr_t) *s++;
492 if (*s == '\0') break;
493 hash ^= (uintptr_t) *s++ << 8;
494 if (*s == '\0') break;
495 hash ^= (uintptr_t) *s++ << 16;
496 if (*s == '\0') break;
497 hash ^= (uintptr_t) *s++ << 24;
502 int NXStrIsEqual (const void *info, const void *data1, const void *data2) {
503 if (data1 == data2) return YES;
504 if (! data1) return ! strlen ((char *) data2);
505 if (! data2) return ! strlen ((char *) data1);
506 if (((char *) data1)[0] != ((char *) data2)[0]) return NO;
507 return (strcmp ((char *) data1, (char *) data2)) ? NO : YES;
510 void NXReallyFree (const void *info, void *data) {
514 /* All the following functions are really private, made non-static only for the benefit of shlibs */
515 static uintptr_t hashPtrStructKey (const void *info, const void *data) {
516 return NXPtrHash(info, *((void **) data));
519 static int isEqualPtrStructKey (const void *info, const void *data1, const void *data2) {
520 return NXPtrIsEqual (info, *((void **) data1), *((void **) data2));
523 static uintptr_t hashStrStructKey (const void *info, const void *data) {
524 return NXStrHash(info, *((char **) data));
527 static int isEqualStrStructKey (const void *info, const void *data1, const void *data2) {
528 return NXStrIsEqual (info, *((char **) data1), *((char **) data2));
531 const NXHashTablePrototype NXPtrPrototype = {
532 NXPtrHash, NXPtrIsEqual, NXNoEffectFree, 0
535 const NXHashTablePrototype NXStrPrototype = {
536 NXStrHash, NXStrIsEqual, NXNoEffectFree, 0
539 const NXHashTablePrototype NXPtrStructKeyPrototype = {
540 hashPtrStructKey, isEqualPtrStructKey, NXReallyFree, 0
543 const NXHashTablePrototype NXStrStructKeyPrototype = {
544 hashStrStructKey, isEqualStrStructKey, NXReallyFree, 0
547 /*************************************************************************
551 *************************************************************************/
553 #if !__OBJC2__ && !TARGET_OS_WIN32
555 /* the implementation could be made faster at the expense of memory if the size of the strings were kept around */
556 static NXHashTable *uniqueStrings = NULL;
558 /* this is based on most apps using a few K of strings, and an average string size of 15 using sqrt(2*dataAlloced*perChunkOverhead) */
559 #define CHUNK_SIZE 360
561 static int accessUniqueString = 0;
563 static char *z = NULL;
564 static size_t zSize = 0;
565 mutex_t NXUniqueStringLock;
567 static const char *CopyIntoReadOnly (const char *str) {
568 size_t len = strlen (str) + 1;
571 if (len > CHUNK_SIZE/2) { /* dont let big strings waste space */
572 result = (char *)malloc (len);
573 bcopy (str, result, len);
577 mutex_locker_t lock(NXUniqueStringLock);
579 zSize = CHUNK_SIZE *((len + CHUNK_SIZE - 1) / CHUNK_SIZE);
580 /* not enough room, we try to allocate. If no room left, too bad */
581 z = (char *)malloc (zSize);
585 bcopy (str, result, len);
591 NXAtom NXUniqueString (const char *buffer) {
592 const char *previous;
594 if (! buffer) return buffer;
595 accessUniqueString++;
597 uniqueStrings = NXCreateHashTable (NXStrPrototype, 0, NULL);
598 previous = (const char *) NXHashGet (uniqueStrings, buffer);
599 if (previous) return previous;
600 previous = CopyIntoReadOnly (buffer);
601 if (NXHashInsert (uniqueStrings, previous)) {
602 _objc_inform ("*** NXUniqueString: invariant broken\n");
608 NXAtom NXUniqueStringNoCopy (const char *string) {
609 accessUniqueString++;
611 uniqueStrings = NXCreateHashTable (NXStrPrototype, 0, NULL);
612 return (const char *) NXHashInsertIfAbsent (uniqueStrings, string);
617 NXAtom NXUniqueStringWithLength (const char *buffer, int length) {
620 char stackBuf[BUF_SIZE];
622 if (length+1 > BUF_SIZE)
623 nullTermStr = (char *)malloc (length+1);
625 nullTermStr = stackBuf;
626 bcopy (buffer, nullTermStr, length);
627 nullTermStr[length] = '\0';
628 atom = NXUniqueString (nullTermStr);
629 if (length+1 > BUF_SIZE)
634 char *NXCopyStringBufferFromZone (const char *str, void *zone) {
638 return strcpy ((char *) malloc_zone_malloc((malloc_zone_t *)zone, strlen (str) + 1), str);
642 char *NXCopyStringBuffer (const char *str) {