objc-sel-set.mm 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 1999-2004,2008 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. * objc-sel-set.h
  25. * A cut-down copy of CFSet used for SEL uniquing.
  26. */
  27. // NOTE: even on a 64-bit system, the implementation is still limited
  28. // to 32-bit integers (like, the count), but SEL can be any size.
  29. #include <stdint.h>
  30. #include "objc-private.h"
  31. #include "objc-sel-set.h"
  32. #if !__OBJC2__
  33. #if !SUPPORT_MOD
  34. // mod-free power of 2 version
  35. #define CONSTRAIN(val, range) ((val) & ((range)-1))
  36. #define SIZE 27
  37. static const uint32_t __objc_sel_set_capacities[SIZE+1] = {
  38. 3, 6, 12, 24, 48, 96, 192, 384, 768, 1536, 3072, 6144, 12288, 24576,
  39. 49152, 98304, 196608, 393216, 786432, 1572864, 3145728, 6291456,
  40. 12582912, 25165824, 50331648, 100663296, 201326592, UINT32_MAX
  41. };
  42. static const uint32_t __objc_sel_set_buckets[SIZE] = { // powers of 2
  43. 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768,
  44. 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608,
  45. 16777216, 33554432, 67108864, 134217728, 268435456
  46. };
  47. #else
  48. // prime version
  49. #define CONSTRAIN(val, range) ((val) % (range))
  50. #define SIZE 42
  51. static const uint32_t __objc_sel_set_capacities[SIZE+1] = {
  52. 4, 8, 17, 29, 47, 76, 123, 199, 322, 521, 843, 1364, 2207, 3571,
  53. 5778, 9349, 15127, 24476, 39603, 64079, 103682, 167761, 271443,
  54. 439204, 710647, 1149851, 1860498, 3010349, 4870847, 7881196, 12752043,
  55. 20633239, 33385282, 54018521, 87403803, 141422324, 228826127, 370248451,
  56. 599074578, 969323029, 1568397607, 2537720636U, UINT32_MAX
  57. };
  58. static const uint32_t __objc_sel_set_buckets[SIZE] = { // primes
  59. 5, 11, 23, 41, 67, 113, 199, 317, 521, 839, 1361, 2207, 3571, 5779,
  60. 9349, 15121, 24473, 39607, 64081, 103681, 167759, 271429, 439199,
  61. 710641, 1149857, 1860503, 3010349, 4870843, 7881193, 12752029, 20633237,
  62. 33385273, 54018521, 87403763, 141422317, 228826121, 370248451, 599074561,
  63. 969323023, 1568397599, 2537720629U, 4106118251U
  64. };
  65. #endif
  66. struct __objc_sel_set {
  67. uint32_t _count; /* number of slots used */
  68. uint32_t _capacity; /* maximum number of used slots */
  69. uint32_t _bucketsNum; /* number of slots */
  70. SEL *_buckets; /* can be NULL if not allocated yet */
  71. };
  72. struct __objc_sel_set_finds {
  73. SEL match;
  74. uint32_t nomatch;
  75. };
  76. // candidate may not be 0; match is 0 if not present
  77. static struct __objc_sel_set_finds __objc_sel_set_findBuckets(struct __objc_sel_set *sset, SEL candidate) {
  78. struct __objc_sel_set_finds ret = {0, 0xffffffff};
  79. uint32_t probe = CONSTRAIN((uint32_t)_objc_strhash((const char *)candidate), sset->_bucketsNum);
  80. for (;;) {
  81. SEL currentSel = sset->_buckets[probe];
  82. if (!currentSel) {
  83. ret.nomatch = probe;
  84. return ret;
  85. } else if (!ret.match && 0 == strcmp((const char *)currentSel, (const char *)candidate)) {
  86. ret.match = currentSel;
  87. }
  88. probe++;
  89. if (sset->_bucketsNum <= probe) {
  90. probe -= sset->_bucketsNum;
  91. }
  92. }
  93. }
  94. // create a set with given starting capacity, will resize as needed
  95. struct __objc_sel_set *__objc_sel_set_create(size_t selrefs) {
  96. uint32_t idx;
  97. struct __objc_sel_set *sset = (struct __objc_sel_set *)
  98. malloc(sizeof(struct __objc_sel_set));
  99. if (!sset) _objc_fatal("objc_sel_set failure");
  100. sset->_count = 0;
  101. // heuristic to convert executable's selrefs count to table size
  102. #if TARGET_OS_IPHONE && !TARGET_OS_IOSMAC
  103. for (idx = 0; __objc_sel_set_capacities[idx] < selrefs; idx++);
  104. if (idx > 0 && selrefs < 1536) idx--;
  105. #else
  106. if (selrefs < 1024) selrefs = 1024;
  107. for (idx = 0; __objc_sel_set_capacities[idx] < selrefs; idx++);
  108. idx++;
  109. #endif
  110. if (SIZE <= idx) _objc_fatal("objc_sel_set failure");
  111. sset->_capacity = __objc_sel_set_capacities[idx];
  112. sset->_bucketsNum = __objc_sel_set_buckets[idx];
  113. sset->_buckets = (SEL *)calloc(sset->_bucketsNum, sizeof(SEL));
  114. if (!sset->_buckets) _objc_fatal("objc_sel_set failure");
  115. return sset;
  116. }
  117. // returns 0 on failure; candidate may not be 0
  118. SEL __objc_sel_set_get(struct __objc_sel_set *sset, SEL candidate) {
  119. return __objc_sel_set_findBuckets(sset, candidate).match;
  120. }
  121. // value may not be 0; should not be called unless it is known the value is not in the set
  122. void __objc_sel_set_add(struct __objc_sel_set *sset, SEL value) {
  123. if (sset->_count == sset->_capacity) {
  124. SEL *oldbuckets = sset->_buckets;
  125. uint32_t oldnbuckets = sset->_bucketsNum;
  126. uint32_t idx, capacity = sset->_count + 1;
  127. for (idx = 0; __objc_sel_set_capacities[idx] < capacity; idx++);
  128. if (SIZE <= idx) _objc_fatal("objc_sel_set failure");
  129. sset->_capacity = __objc_sel_set_capacities[idx];
  130. sset->_bucketsNum = __objc_sel_set_buckets[idx];
  131. sset->_buckets = (SEL *)
  132. calloc(sset->_bucketsNum, sizeof(SEL));
  133. if (!sset->_buckets) _objc_fatal("objc_sel_set failure");
  134. for (idx = 0; idx < oldnbuckets; idx++) {
  135. SEL currentSel = oldbuckets[idx];
  136. if (currentSel) {
  137. uint32_t nomatch = __objc_sel_set_findBuckets(sset, currentSel).nomatch;
  138. sset->_buckets[nomatch] = currentSel;
  139. }
  140. }
  141. free(oldbuckets);
  142. }
  143. {
  144. uint32_t nomatch = __objc_sel_set_findBuckets(sset, value).nomatch;
  145. sset->_buckets[nomatch] = value;
  146. sset->_count++;
  147. }
  148. }
  149. // !__OBJC2__
  150. #endif