forked from macmade/OBJC4-437.1-Runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaptable.m
471 lines (408 loc) · 14.5 KB
/
maptable.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
* Copyright (c) 1999-2003, 2005-2007 Apple Inc. All Rights Reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/* maptable.m
Copyright 1990-1996 NeXT Software, Inc.
Created by Bertrand Serlet, August 1990
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "objc-private.h"
#include "maptable.h"
#include "hashtable2.h"
/****** Macros and utilities ****************************/
#if defined(DEBUG)
#define INLINE
#else
#define INLINE inline
#endif
typedef struct _MapPair {
const void *key;
const void *value;
} MapPair;
static unsigned log2u(unsigned x) { return (x<2) ? 0 : log2u(x>>1)+1; };
static INLINE unsigned exp2m1(unsigned x) { return (1 << x) - 1; };
/* iff necessary this modulo can be optimized since the nbBuckets is of the form 2**n-1 */
static INLINE unsigned bucketOf(NXMapTable *table, const void *key) {
unsigned hash = (table->prototype->hash)(table, key);
unsigned xored = (hash & 0xffff) ^ (hash >> 16);
return ((xored * 65521) + hash) % table->nbBuckets;
}
static INLINE int isEqual(NXMapTable *table, const void *key1, const void *key2) {
return (key1 == key2) ? 1 : (table->prototype->isEqual)(table, key1, key2);
}
static INLINE unsigned nextIndex(NXMapTable *table, unsigned index) {
return (index+1 >= table->nbBuckets) ? 0 : index+1;
}
static INLINE void *allocBuckets(void *z, unsigned nb) {
MapPair *pairs = malloc_zone_malloc(z, (nb * sizeof(MapPair)));
MapPair *pair = pairs;
while (nb--) { pair->key = NX_MAPNOTAKEY; pair->value = NULL; pair++; }
return pairs;
}
/***** Global data and bootstrap **********************/
static int isEqualPrototype (const void *info, const void *data1, const void *data2) {
NXHashTablePrototype *proto1 = (NXHashTablePrototype *) data1;
NXHashTablePrototype *proto2 = (NXHashTablePrototype *) data2;
return (proto1->hash == proto2->hash) && (proto1->isEqual == proto2->isEqual) && (proto1->free == proto2->free) && (proto1->style == proto2->style);
};
static uintptr_t hashPrototype (const void *info, const void *data) {
NXHashTablePrototype *proto = (NXHashTablePrototype *) data;
return NXPtrHash(info, proto->hash) ^ NXPtrHash(info, proto->isEqual) ^ NXPtrHash(info, proto->free) ^ (uintptr_t) proto->style;
};
static NXHashTablePrototype protoPrototype = {
hashPrototype, isEqualPrototype, NXNoEffectFree, 0
};
static NXHashTable *prototypes = NULL;
/* table of all prototypes */
/**** Fundamentals Operations **************/
NXMapTable *NXCreateMapTableFromZone(NXMapTablePrototype prototype, unsigned capacity, void *z) {
NXMapTable *table = malloc_zone_malloc(z, sizeof(NXMapTable));
NXMapTablePrototype *proto;
if (! prototypes) prototypes = NXCreateHashTable(protoPrototype, 0, NULL);
if (! prototype.hash || ! prototype.isEqual || ! prototype.free || prototype.style) {
_objc_inform("*** NXCreateMapTable: invalid creation parameters\n");
return NULL;
}
proto = NXHashGet(prototypes, &prototype);
if (! proto) {
proto = malloc(sizeof(NXMapTablePrototype));
*proto = prototype;
(void)NXHashInsert(prototypes, proto);
}
table->prototype = proto; table->count = 0;
table->nbBuckets = exp2m1(log2u(capacity)+1);
table->buckets = allocBuckets(z, table->nbBuckets);
return table;
}
NXMapTable *NXCreateMapTable(NXMapTablePrototype prototype, unsigned capacity) {
return NXCreateMapTableFromZone(prototype, capacity, malloc_default_zone());
}
void NXFreeMapTable(NXMapTable *table) {
NXResetMapTable(table);
free(table->buckets);
free(table);
}
void NXResetMapTable(NXMapTable *table) {
MapPair *pairs = table->buckets;
void (*freeProc)(struct _NXMapTable *, void *, void *) = table->prototype->free;
unsigned index = table->nbBuckets;
while (index--) {
if (pairs->key != NX_MAPNOTAKEY) {
freeProc(table, (void *)pairs->key, (void *)pairs->value);
pairs->key = NX_MAPNOTAKEY; pairs->value = NULL;
}
pairs++;
}
table->count = 0;
}
BOOL NXCompareMapTables(NXMapTable *table1, NXMapTable *table2) {
if (table1 == table2) return YES;
if (table1->count != table2->count) return NO;
else {
const void *key;
const void *value;
NXMapState state = NXInitMapState(table1);
while (NXNextMapState(table1, &state, &key, &value)) {
if (NXMapMember(table2, key, (void**)&value) == NX_MAPNOTAKEY) return NO;
}
return YES;
}
}
unsigned NXCountMapTable(NXMapTable *table) { return table->count; }
static int mapSearch = 0;
static int mapSearchHit = 0;
static int mapSearchLoop = 0;
static INLINE void *_NXMapMember(NXMapTable *table, const void *key, void **value) {
MapPair *pairs = table->buckets;
unsigned index = bucketOf(table, key);
MapPair *pair = pairs + index;
if (pair->key == NX_MAPNOTAKEY) return NX_MAPNOTAKEY;
mapSearch ++;
if (isEqual(table, pair->key, key)) {
*value = (void *)pair->value;
mapSearchHit ++;
return (void *)pair->key;
} else {
unsigned index2 = index;
while ((index2 = nextIndex(table, index2)) != index) {
mapSearchLoop ++;
pair = pairs + index2;
if (pair->key == NX_MAPNOTAKEY) return NX_MAPNOTAKEY;
if (isEqual(table, pair->key, key)) {
*value = (void *)pair->value;
return (void *)pair->key;
}
}
return NX_MAPNOTAKEY;
}
}
void *NXMapMember(NXMapTable *table, const void *key, void **value) {
return _NXMapMember(table, key, value);
}
void *NXMapGet(NXMapTable *table, const void *key) {
void *value;
return (_NXMapMember(table, key, &value) != NX_MAPNOTAKEY) ? value : NULL;
}
static int mapRehash = 0;
static int mapRehashSum = 0;
static void _NXMapRehash(NXMapTable *table) {
MapPair *pairs = table->buckets;
MapPair *pair = pairs;
unsigned index = table->nbBuckets;
unsigned oldCount = table->count;
table->nbBuckets += table->nbBuckets + 1; /* 2 times + 1 */
table->count = 0;
table->buckets = allocBuckets(malloc_zone_from_ptr(table), table->nbBuckets);
mapRehash ++;
mapRehashSum += table->count;
while (index--) {
if (pair->key != NX_MAPNOTAKEY) {
(void)NXMapInsert(table, pair->key, pair->value);
}
pair++;
}
if (oldCount != table->count)
_objc_inform("*** maptable: 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");
free(pairs);
}
static int mapInsert = 0;
static int mapInsertHit = 0;
static int mapInsertLoop = 0;
void *NXMapInsert(NXMapTable *table, const void *key, const void *value) {
MapPair *pairs = table->buckets;
unsigned index = bucketOf(table, key);
MapPair *pair = pairs + index;
if (key == NX_MAPNOTAKEY) {
_objc_inform("*** NXMapInsert: invalid key: -1\n");
return NULL;
}
mapInsert ++;
if (pair->key == NX_MAPNOTAKEY) {
mapInsertHit ++;
pair->key = key; pair->value = value;
table->count++;
return NULL;
}
if (isEqual(table, pair->key, key)) {
const void *old = pair->value;
mapInsertHit ++;
if (old != value) pair->value = value;/* avoid writing unless needed! */
return (void *)old;
} else if (table->count == table->nbBuckets) {
/* no room: rehash and retry */
_NXMapRehash(table);
return NXMapInsert(table, key, value);
} else {
unsigned index2 = index;
while ((index2 = nextIndex(table, index2)) != index) {
mapInsertLoop ++;
pair = pairs + index2;
if (pair->key == NX_MAPNOTAKEY) {
#if INSERT_TAIL
pair->key = key; pair->value = value;
#else
MapPair current = {key, value};
index2 = index;
while (current.key != NX_MAPNOTAKEY) {
MapPair temp;
pair = pairs + index2;
temp = *pair;
*pair = current;
current = temp;
index2 = nextIndex(table, index2);
}
#endif
table->count++;
if (table->count * 4 > table->nbBuckets * 3) _NXMapRehash(table);
return NULL;
}
if (isEqual(table, pair->key, key)) {
const void *old = pair->value;
if (old != value) pair->value = value;/* avoid writing unless needed! */
return (void *)old;
}
}
/* no room: can't happen! */
_objc_inform("**** NXMapInsert: bug\n");
return NULL;
}
}
static int mapRemove = 0;
void *NXMapRemove(NXMapTable *table, const void *key) {
MapPair *pairs = table->buckets;
unsigned index = bucketOf(table, key);
MapPair *pair = pairs + index;
unsigned chain = 1; /* number of non-nil pairs in a row */
int found = 0;
const void *old = NULL;
if (pair->key == NX_MAPNOTAKEY) return NULL;
mapRemove ++;
/* compute chain */
{
unsigned index2 = index;
if (isEqual(table, pair->key, key)) {found ++; old = pair->value; }
while ((index2 = nextIndex(table, index2)) != index) {
pair = pairs + index2;
if (pair->key == NX_MAPNOTAKEY) break;
if (isEqual(table, pair->key, key)) {found ++; old = pair->value; }
chain++;
}
}
if (! found) return NULL;
if (found != 1) _objc_inform("**** NXMapRemove: incorrect table\n");
/* remove then reinsert */
{
MapPair buffer[16];
MapPair *aux = (chain > 16) ? malloc(sizeof(MapPair)*(chain-1)) : buffer;
int auxnb = 0;
int nb = chain;
unsigned index2 = index;
while (nb--) {
pair = pairs + index2;
if (! isEqual(table, pair->key, key)) aux[auxnb++] = *pair;
pair->key = NX_MAPNOTAKEY; pair->value = NULL;
index2 = nextIndex(table, index2);
}
table->count -= chain;
if (auxnb != chain-1) _objc_inform("**** NXMapRemove: bug\n");
while (auxnb--) NXMapInsert(table, aux[auxnb].key, aux[auxnb].value);
if (chain > 16) free(aux);
}
return (void *)old;
}
NXMapState NXInitMapState(NXMapTable *table) {
NXMapState state;
state.index = table->nbBuckets;
return state;
}
int NXNextMapState(NXMapTable *table, NXMapState *state, const void **key, const void **value) {
MapPair *pairs = table->buckets;
while (state->index--) {
MapPair *pair = pairs + state->index;
if (pair->key != NX_MAPNOTAKEY) {
*key = pair->key; *value = pair->value;
return YES;
}
}
return NO;
}
/***********************************************************************
* NXMapKeyCopyingInsert
* Like NXMapInsert, but strdups the key if necessary.
* Used to prevent stale pointers when bundles are unloaded.
**********************************************************************/
__private_extern__ void *NXMapKeyCopyingInsert(NXMapTable *table, const void *key, const void *value)
{
void *realKey;
void *realValue = NULL;
if ((realKey = NXMapMember(table, key, &realValue)) != NX_MAPNOTAKEY) {
// key DOES exist in table - use table's key for insertion
} else {
// key DOES NOT exist in table - copy the new key before insertion
realKey = _strdup_internal(key);
}
return NXMapInsert(table, realKey, value);
}
/***********************************************************************
* NXMapKeyFreeingRemove
* Like NXMapRemove, but frees the existing key if necessary.
* Used to prevent stale pointers when bundles are unloaded.
**********************************************************************/
__private_extern__ void *NXMapKeyFreeingRemove(NXMapTable *table, const void *key)
{
void *realKey;
void *realValue = NULL;
if ((realKey = NXMapMember(table, key, &realValue)) != NX_MAPNOTAKEY) {
// key DOES exist in table - remove pair and free key
realValue = NXMapRemove(table, realKey);
_free_internal(realKey); // the key from the table, not necessarily the one given
return realValue;
} else {
// key DOES NOT exist in table - nothing to do
return NULL;
}
}
/**** Conveniences *************************************/
static unsigned _mapPtrHash(NXMapTable *table, const void *key) {
return (unsigned)((((uintptr_t) key) >> (sizeof(uintptr_t)/2*8)) ^ ((uintptr_t) key));
}
static unsigned _mapStrHash(NXMapTable *table, const void *key) {
unsigned hash = 0;
unsigned char *s = (unsigned char *)key;
/* unsigned to avoid a sign-extend */
/* unroll the loop */
if (s) for (; ; ) {
if (*s == '\0') break;
hash ^= *s++;
if (*s == '\0') break;
hash ^= *s++ << 8;
if (*s == '\0') break;
hash ^= *s++ << 16;
if (*s == '\0') break;
hash ^= *s++ << 24;
}
return hash;
}
static int _mapPtrIsEqual(NXMapTable *table, const void *key1, const void *key2) {
return key1 == key2;
}
static int _mapStrIsEqual(NXMapTable *table, const void *key1, const void *key2) {
if (key1 == key2) return YES;
if (! key1) return ! strlen ((char *) key2);
if (! key2) return ! strlen ((char *) key1);
if (((char *) key1)[0] != ((char *) key2)[0]) return NO;
return (strcmp((char *) key1, (char *) key2)) ? NO : YES;
}
static void _mapNoFree(NXMapTable *table, void *key, void *value) {}
const NXMapTablePrototype NXPtrValueMapPrototype = {
_mapPtrHash, _mapPtrIsEqual, _mapNoFree, 0
};
const NXMapTablePrototype NXStrValueMapPrototype = {
_mapStrHash, _mapStrIsEqual, _mapNoFree, 0
};
#if !__OBJC2__ && !TARGET_OS_WIN32
/* This only works with class Object, which is unavailable. */
/* Method prototypes */
@interface DoesNotExist
+ class;
+ initialize;
- (id)description;
- (const char *)UTF8String;
- (unsigned long)hash;
- (BOOL)isEqual:(id)object;
- free;
@end
static unsigned _mapObjectHash(NXMapTable *table, const void *key) {
return [(id)key hash];
}
static int _mapObjectIsEqual(NXMapTable *table, const void *key1, const void *key2) {
return [(id)key1 isEqual:(id)key2];
}
static void _mapObjectFree(NXMapTable *table, void *key, void *value) {
[(id)key free];
}
const NXMapTablePrototype NXObjectMapPrototype = {
_mapObjectHash, _mapObjectIsEqual, _mapObjectFree, 0
};
#endif