objc-sel-old.mm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 1999-2007 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. * Utilities for registering and looking up selectors. The sole
  25. * purpose of the selector tables is a registry whereby there is
  26. * exactly one address (selector) associated with a given string
  27. * (method name).
  28. */
  29. #if !__OBJC2__
  30. #include "objc-private.h"
  31. #include "objc-sel-set.h"
  32. #if SUPPORT_PREOPT
  33. #include <objc-shared-cache.h>
  34. static const objc_selopt_t *builtins = NULL;
  35. #endif
  36. __BEGIN_DECLS
  37. static size_t SelrefCount = 0;
  38. static const char *_objc_empty_selector = "";
  39. static struct __objc_sel_set *_objc_selectors = NULL;
  40. static SEL _objc_search_builtins(const char *key)
  41. {
  42. #if defined(DUMP_SELECTORS)
  43. if (NULL != key) printf("\t\"%s\",\n", key);
  44. #endif
  45. if (!key) return (SEL)0;
  46. if ('\0' == *key) return (SEL)_objc_empty_selector;
  47. #if SUPPORT_PREOPT
  48. if (builtins) return (SEL)builtins->get(key);
  49. #endif
  50. return (SEL)0;
  51. }
  52. const char *sel_getName(SEL sel) {
  53. return sel ? (const char *)sel : "<null selector>";
  54. }
  55. BOOL sel_isMapped(SEL name)
  56. {
  57. SEL sel;
  58. if (!name) return NO;
  59. sel = _objc_search_builtins((const char *)name);
  60. if (sel) return YES;
  61. mutex_locker_t lock(selLock);
  62. if (_objc_selectors) {
  63. sel = __objc_sel_set_get(_objc_selectors, name);
  64. }
  65. return bool(sel);
  66. }
  67. static SEL __sel_registerName(const char *name, bool shouldLock, bool copy)
  68. {
  69. SEL result = 0;
  70. if (shouldLock) selLock.assertUnlocked();
  71. else selLock.assertLocked();
  72. if (!name) return (SEL)0;
  73. result = _objc_search_builtins(name);
  74. if (result) return result;
  75. conditional_mutex_locker_t lock(selLock, shouldLock);
  76. if (_objc_selectors) {
  77. result = __objc_sel_set_get(_objc_selectors, (SEL)name);
  78. }
  79. if (result) return result;
  80. // No match. Insert.
  81. if (!_objc_selectors) {
  82. _objc_selectors = __objc_sel_set_create(SelrefCount);
  83. }
  84. if (!result) {
  85. result = (SEL)(copy ? strdup(name) : name);
  86. __objc_sel_set_add(_objc_selectors, result);
  87. #if defined(DUMP_UNKNOWN_SELECTORS)
  88. printf("\t\"%s\",\n", name);
  89. #endif
  90. }
  91. return result;
  92. }
  93. SEL sel_registerName(const char *name) {
  94. return __sel_registerName(name, 1, 1); // YES lock, YES copy
  95. }
  96. SEL sel_registerNameNoLock(const char *name, bool copy) {
  97. return __sel_registerName(name, 0, copy); // NO lock, maybe copy
  98. }
  99. // 2001/1/24
  100. // the majority of uses of this function (which used to return NULL if not found)
  101. // did not check for NULL, so, in fact, never return NULL
  102. //
  103. SEL sel_getUid(const char *name) {
  104. return __sel_registerName(name, 2, 1); // YES lock, YES copy
  105. }
  106. BOOL sel_isEqual(SEL lhs, SEL rhs)
  107. {
  108. return bool(lhs == rhs);
  109. }
  110. /***********************************************************************
  111. * sel_init
  112. * Initialize selector tables and register selectors used internally.
  113. **********************************************************************/
  114. void sel_init(size_t selrefCount)
  115. {
  116. // save this value for later
  117. SelrefCount = selrefCount;
  118. #if SUPPORT_PREOPT
  119. builtins = preoptimizedSelectors();
  120. #endif
  121. // Register selectors used by libobjc
  122. mutex_locker_t lock(selLock);
  123. SEL_cxx_construct = sel_registerNameNoLock(".cxx_construct", NO);
  124. SEL_cxx_destruct = sel_registerNameNoLock(".cxx_destruct", NO);
  125. extern SEL FwdSel;
  126. FwdSel = sel_registerNameNoLock("forward::", NO);
  127. }
  128. __END_DECLS
  129. #endif