]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-sel-set.m
objc4-371.tar.gz
[apple/objc4.git] / runtime / objc-sel-set.m
1 /*
2 * Copyright (c) 1999-2004 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * objc-sel-set.h
26 * A cut-down copy of CFSet used for SEL uniquing.
27 */
28
29
30 // NOTE: even on a 64-bit system, the implementation is still limited
31 // to 32-bit integers (like, the count), but SEL can be any size.
32
33 #include <stdint.h>
34 #import "objc-private.h"
35 #import "objc-sel-set.h"
36
37 static const uint32_t __objc_sel_set_capacities[43] = {
38 4, 8, 17, 29, 47, 76, 123, 199, 322, 521, 843, 1364, 2207, 3571, 5778, 9349,
39 15127, 24476, 39603, 64079, 103682, 167761, 271443, 439204, 710647, 1149851, 1860498,
40 3010349, 4870847, 7881196, 12752043, 20633239, 33385282, 54018521, 87403803, 141422324,
41 228826127, 370248451, 599074578, 969323029, 1568397607, 2537720636U, UINT32_MAX
42 };
43
44 static const uint32_t __objc_sel_set_buckets[42] = { // primes
45 5, 11, 23, 41, 67, 113, 199, 317, 521, 839, 1361, 2207, 3571, 5779, 9349, 15121,
46 24473, 39607, 64081, 103681, 167759, 271429, 439199, 710641, 1149857, 1860503, 3010349,
47 4870843, 7881193, 12752029, 20633237, 33385273, 54018521, 87403763, 141422317, 228826121,
48 370248451, 599074561, 969323023, 1568397599, 2537720629U, 4106118251U
49 };
50
51 struct __objc_sel_set {
52 uint32_t _count; /* number of slots used */
53 uint32_t _capacity; /* maximum number of used slots */
54 uint32_t _bucketsNum; /* number of slots */
55 SEL *_buckets; /* can be NULL if not allocated yet */
56 };
57
58 struct __objc_sel_set_finds {
59 SEL match;
60 uint32_t nomatch;
61 };
62
63 // candidate may not be 0; match is 0 if not present
64 static struct __objc_sel_set_finds __objc_sel_set_findBuckets(struct __objc_sel_set *sset, SEL candidate) {
65 struct __objc_sel_set_finds ret = {0, 0xffffffff};
66 uint32_t probe = (uint32_t)_objc_strhash((const char *)candidate) % sset->_bucketsNum;
67 for (;;) {
68 SEL currentSel = sset->_buckets[probe];
69 if (!currentSel) {
70 ret.nomatch = probe;
71 return ret;
72 } else if (!ret.match && 0 == _objc_strcmp((const char *)currentSel, (const char *)candidate)) {
73 ret.match = currentSel;
74 }
75 probe++;
76 if (sset->_bucketsNum <= probe) {
77 probe -= sset->_bucketsNum;
78 }
79 }
80 }
81
82 // create a set with given starting capacity, will resize as needed
83 __private_extern__ struct __objc_sel_set *__objc_sel_set_create(uint32_t capacity) {
84 struct __objc_sel_set *sset = _malloc_internal(sizeof(struct __objc_sel_set));
85 if (!sset) _objc_fatal("objc_sel_set failure");
86 sset->_count = 0;
87 uint32_t idx;
88 for (idx = 0; __objc_sel_set_capacities[idx] < capacity; idx++);
89 if (42 <= idx) _objc_fatal("objc_sel_set failure");
90 sset->_capacity = __objc_sel_set_capacities[idx];
91 sset->_bucketsNum = __objc_sel_set_buckets[idx];
92 sset->_buckets = _calloc_internal(sset->_bucketsNum, sizeof(SEL));
93 if (!sset->_buckets) _objc_fatal("objc_sel_set failure");
94 return sset;
95 }
96
97 // returns 0 on failure; candidate may not be 0
98 __private_extern__ SEL __objc_sel_set_get(struct __objc_sel_set *sset, SEL candidate) {
99 return __objc_sel_set_findBuckets(sset, candidate).match;
100 }
101
102 // value may not be 0; should not be called unless it is known the value is not in the set
103 __private_extern__ void __objc_sel_set_add(struct __objc_sel_set *sset, SEL value) {
104 if (sset->_count == sset->_capacity) {
105 SEL *oldbuckets = sset->_buckets;
106 uint32_t oldnbuckets = sset->_bucketsNum;
107 uint32_t idx, capacity = sset->_count + 1;
108 for (idx = 0; __objc_sel_set_capacities[idx] < capacity; idx++);
109 if (42 <= idx) _objc_fatal("objc_sel_set failure");
110 sset->_capacity = __objc_sel_set_capacities[idx];
111 sset->_bucketsNum = __objc_sel_set_buckets[idx];
112 sset->_buckets = _calloc_internal(sset->_bucketsNum, sizeof(SEL));
113 if (!sset->_buckets) _objc_fatal("objc_sel_set failure");
114 for (idx = 0; idx < oldnbuckets; idx++) {
115 SEL currentSel = oldbuckets[idx];
116 if (currentSel) {
117 uint32_t nomatch = __objc_sel_set_findBuckets(sset, currentSel).nomatch;
118 sset->_buckets[nomatch] = currentSel;
119 }
120 }
121 _free_internal(oldbuckets);
122 }
123 uint32_t nomatch = __objc_sel_set_findBuckets(sset, value).nomatch;
124 sset->_buckets[nomatch] = value;
125 sset->_count++;
126 }