selector.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /* GNU Objective C Runtime selector related functions
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. Contributed by Kresten Krab Thorup
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation; either version 3, or (at your option) any later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  11. details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "objc-private/common.h"
  20. #include "objc/runtime.h"
  21. #include "objc/thr.h"
  22. #include "objc-private/hash.h"
  23. #include "objc-private/objc-list.h"
  24. #include "objc-private/module-abi-8.h"
  25. #include "objc-private/runtime.h"
  26. #include "objc-private/sarray.h"
  27. #include "objc-private/selector.h"
  28. #include <stdlib.h> /* For malloc. */
  29. /* Initial selector hash table size. Value doesn't matter much. */
  30. #define SELECTOR_HASH_SIZE 128
  31. /* Tables mapping selector names to uid and opposite. */
  32. static struct sarray *__objc_selector_array = 0; /* uid -> sel !T:MUTEX */
  33. static struct sarray *__objc_selector_names = 0; /* uid -> name !T:MUTEX */
  34. static cache_ptr __objc_selector_hash = 0; /* name -> uid !T:MUTEX */
  35. /* Number of selectors stored in each of the above tables. */
  36. unsigned int __objc_selector_max_index = 0; /* !T:MUTEX */
  37. /* Forward-declare an internal function. */
  38. static SEL
  39. __sel_register_typed_name (const char *name, const char *types,
  40. struct objc_selector *orig, BOOL is_const);
  41. void __objc_init_selector_tables (void)
  42. {
  43. __objc_selector_array = sarray_new (SELECTOR_HASH_SIZE, 0);
  44. __objc_selector_names = sarray_new (SELECTOR_HASH_SIZE, 0);
  45. __objc_selector_hash
  46. = objc_hash_new (SELECTOR_HASH_SIZE,
  47. (hash_func_type) objc_hash_string,
  48. (compare_func_type) objc_compare_strings);
  49. }
  50. /* Register a bunch of selectors from the table of selectors in a
  51. module. 'selectors' should not be NULL. The list is terminated by
  52. a selectors with a NULL sel_id. The selectors are assumed to
  53. contain the 'name' in the sel_id field; this is replaced with the
  54. final selector id after they are registered. */
  55. void
  56. __objc_register_selectors_from_module (struct objc_selector *selectors)
  57. {
  58. int i;
  59. for (i = 0; selectors[i].sel_id; ++i)
  60. {
  61. const char *name, *type;
  62. name = (char *) selectors[i].sel_id;
  63. type = (char *) selectors[i].sel_types;
  64. /* Constructors are constant static data and we can safely store
  65. pointers to them in the runtime structures, so we set
  66. is_const == YES. */
  67. __sel_register_typed_name (name, type, (struct objc_selector *) &(selectors[i]),
  68. /* is_const */ YES);
  69. }
  70. }
  71. /* This routine is given a class and records all of the methods in its
  72. class structure in the record table. */
  73. void
  74. __objc_register_selectors_from_class (Class class)
  75. {
  76. struct objc_method_list * method_list;
  77. method_list = class->methods;
  78. while (method_list)
  79. {
  80. __objc_register_selectors_from_list (method_list);
  81. method_list = method_list->method_next;
  82. }
  83. }
  84. /* This routine is given a list of methods and records each of the
  85. methods in the record table. This is the routine that does the
  86. actual recording work.
  87. The name and type pointers in the method list must be permanent and
  88. immutable. */
  89. void
  90. __objc_register_selectors_from_list (struct objc_method_list *method_list)
  91. {
  92. int i = 0;
  93. objc_mutex_lock (__objc_runtime_mutex);
  94. while (i < method_list->method_count)
  95. {
  96. Method method = &method_list->method_list[i];
  97. if (method->method_name)
  98. {
  99. method->method_name
  100. = __sel_register_typed_name ((const char *) method->method_name,
  101. method->method_types, 0, YES);
  102. }
  103. i += 1;
  104. }
  105. objc_mutex_unlock (__objc_runtime_mutex);
  106. }
  107. /* The same as __objc_register_selectors_from_list, but works on a
  108. struct objc_method_description_list* instead of a struct
  109. objc_method_list*. This is only used for protocols, which have
  110. lists of method descriptions, not methods. */
  111. void
  112. __objc_register_selectors_from_description_list
  113. (struct objc_method_description_list *method_list)
  114. {
  115. int i = 0;
  116. objc_mutex_lock (__objc_runtime_mutex);
  117. while (i < method_list->count)
  118. {
  119. struct objc_method_description *method = &method_list->list[i];
  120. if (method->name)
  121. {
  122. method->name
  123. = __sel_register_typed_name ((const char *) method->name,
  124. method->types, 0, YES);
  125. }
  126. i += 1;
  127. }
  128. objc_mutex_unlock (__objc_runtime_mutex);
  129. }
  130. /* Register instance methods as class methods for root classes. */
  131. void __objc_register_instance_methods_to_class (Class class)
  132. {
  133. struct objc_method_list *method_list;
  134. struct objc_method_list *class_method_list;
  135. int max_methods_no = 16;
  136. struct objc_method_list *new_list;
  137. Method curr_method;
  138. /* Only if a root class. */
  139. if (class->super_class)
  140. return;
  141. /* Allocate a method list to hold the new class methods. */
  142. new_list = objc_calloc (sizeof (struct objc_method_list)
  143. + sizeof (struct objc_method[max_methods_no]), 1);
  144. method_list = class->methods;
  145. class_method_list = class->class_pointer->methods;
  146. curr_method = &new_list->method_list[0];
  147. /* Iterate through the method lists for the class. */
  148. while (method_list)
  149. {
  150. int i;
  151. /* Iterate through the methods from this method list. */
  152. for (i = 0; i < method_list->method_count; i++)
  153. {
  154. Method mth = &method_list->method_list[i];
  155. if (mth->method_name
  156. && ! search_for_method_in_list (class_method_list,
  157. mth->method_name))
  158. {
  159. /* This instance method isn't a class method. Add it
  160. into the new_list. */
  161. *curr_method = *mth;
  162. /* Reallocate the method list if necessary. */
  163. if (++new_list->method_count == max_methods_no)
  164. new_list =
  165. objc_realloc (new_list, sizeof (struct objc_method_list)
  166. + sizeof (struct
  167. objc_method[max_methods_no += 16]));
  168. curr_method = &new_list->method_list[new_list->method_count];
  169. }
  170. }
  171. method_list = method_list->method_next;
  172. }
  173. /* If we created any new class methods then attach the method list
  174. to the class. */
  175. if (new_list->method_count)
  176. {
  177. new_list =
  178. objc_realloc (new_list, sizeof (struct objc_method_list)
  179. + sizeof (struct objc_method[new_list->method_count]));
  180. new_list->method_next = class->class_pointer->methods;
  181. class->class_pointer->methods = new_list;
  182. }
  183. else
  184. objc_free(new_list);
  185. __objc_update_dispatch_table_for_class (class->class_pointer);
  186. }
  187. BOOL
  188. sel_isEqual (SEL s1, SEL s2)
  189. {
  190. if (s1 == 0 || s2 == 0)
  191. return s1 == s2;
  192. else
  193. return s1->sel_id == s2->sel_id;
  194. }
  195. /* Return YES iff t1 and t2 have same method types. Ignore the
  196. argframe layout. */
  197. static BOOL
  198. sel_types_match (const char *t1, const char *t2)
  199. {
  200. if (! t1 || ! t2)
  201. return NO;
  202. while (*t1 && *t2)
  203. {
  204. if (*t1 == '+') t1++;
  205. if (*t2 == '+') t2++;
  206. while (isdigit ((unsigned char) *t1)) t1++;
  207. while (isdigit ((unsigned char) *t2)) t2++;
  208. /* xxx Remove these next two lines when qualifiers are put in
  209. all selectors, not just Protocol selectors. */
  210. t1 = objc_skip_type_qualifiers (t1);
  211. t2 = objc_skip_type_qualifiers (t2);
  212. if (! *t1 && ! *t2)
  213. return YES;
  214. if (*t1 != *t2)
  215. return NO;
  216. t1++;
  217. t2++;
  218. }
  219. return NO;
  220. }
  221. /* Return selector representing name. */
  222. SEL
  223. sel_get_any_uid (const char *name)
  224. {
  225. struct objc_list *l;
  226. sidx i;
  227. objc_mutex_lock (__objc_runtime_mutex);
  228. i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name);
  229. if (soffset_decode (i) == 0)
  230. {
  231. objc_mutex_unlock (__objc_runtime_mutex);
  232. return 0;
  233. }
  234. l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
  235. objc_mutex_unlock (__objc_runtime_mutex);
  236. if (l == 0)
  237. return 0;
  238. return (SEL) l->head;
  239. }
  240. SEL
  241. sel_getTypedSelector (const char *name)
  242. {
  243. sidx i;
  244. if (name == NULL)
  245. return NULL;
  246. objc_mutex_lock (__objc_runtime_mutex);
  247. /* Look for a typed selector. */
  248. i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name);
  249. if (i != 0)
  250. {
  251. struct objc_list *l;
  252. SEL returnValue = NULL;
  253. for (l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
  254. l; l = l->tail)
  255. {
  256. SEL s = (SEL) l->head;
  257. if (s->sel_types)
  258. {
  259. if (returnValue == NULL)
  260. {
  261. /* First typed selector that we find. Keep it in
  262. returnValue, but keep checking as we want to
  263. detect conflicts. */
  264. returnValue = s;
  265. }
  266. else
  267. {
  268. /* We had already found a typed selectors, so we
  269. have multiple ones. Double-check that they have
  270. different types, just in case for some reason we
  271. got duplicates with the same types. If so, it's
  272. OK, we'll ignore the duplicate. */
  273. if (returnValue->sel_types == s->sel_types)
  274. continue;
  275. else if (sel_types_match (returnValue->sel_types, s->sel_types))
  276. continue;
  277. else
  278. {
  279. /* The types of the two selectors are different;
  280. it's a conflict. Too bad. Return NULL. */
  281. objc_mutex_unlock (__objc_runtime_mutex);
  282. return NULL;
  283. }
  284. }
  285. }
  286. }
  287. if (returnValue != NULL)
  288. {
  289. objc_mutex_unlock (__objc_runtime_mutex);
  290. return returnValue;
  291. }
  292. }
  293. /* No typed selector found. Return NULL. */
  294. objc_mutex_unlock (__objc_runtime_mutex);
  295. return 0;
  296. }
  297. SEL *
  298. sel_copyTypedSelectorList (const char *name, unsigned int *numberOfReturnedSelectors)
  299. {
  300. unsigned int count = 0;
  301. SEL *returnValue = NULL;
  302. sidx i;
  303. if (name == NULL)
  304. {
  305. if (numberOfReturnedSelectors)
  306. *numberOfReturnedSelectors = 0;
  307. return NULL;
  308. }
  309. objc_mutex_lock (__objc_runtime_mutex);
  310. /* Count how many selectors we have. */
  311. i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name);
  312. if (i != 0)
  313. {
  314. struct objc_list *selector_list = NULL;
  315. selector_list = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
  316. /* Count how many selectors we have. */
  317. {
  318. struct objc_list *l;
  319. for (l = selector_list; l; l = l->tail)
  320. count++;
  321. }
  322. if (count != 0)
  323. {
  324. /* Allocate enough memory to hold them. */
  325. returnValue = (SEL *)(malloc (sizeof (SEL) * (count + 1)));
  326. /* Copy the selectors. */
  327. {
  328. unsigned int j;
  329. for (j = 0; j < count; j++)
  330. {
  331. returnValue[j] = (SEL)(selector_list->head);
  332. selector_list = selector_list->tail;
  333. }
  334. returnValue[j] = NULL;
  335. }
  336. }
  337. }
  338. objc_mutex_unlock (__objc_runtime_mutex);
  339. if (numberOfReturnedSelectors)
  340. *numberOfReturnedSelectors = count;
  341. return returnValue;
  342. }
  343. /* Get the name of a selector. If the selector is unknown, the empty
  344. string "" is returned. */
  345. const char *sel_getName (SEL selector)
  346. {
  347. const char *ret;
  348. if (selector == NULL)
  349. return "<null selector>";
  350. objc_mutex_lock (__objc_runtime_mutex);
  351. if ((soffset_decode ((sidx)selector->sel_id) > 0)
  352. && (soffset_decode ((sidx)selector->sel_id) <= __objc_selector_max_index))
  353. ret = sarray_get_safe (__objc_selector_names, (sidx) selector->sel_id);
  354. else
  355. ret = 0;
  356. objc_mutex_unlock (__objc_runtime_mutex);
  357. return ret;
  358. }
  359. BOOL
  360. sel_is_mapped (SEL selector)
  361. {
  362. unsigned int idx = soffset_decode ((sidx)selector->sel_id);
  363. return ((idx > 0) && (idx <= __objc_selector_max_index));
  364. }
  365. const char *sel_getTypeEncoding (SEL selector)
  366. {
  367. if (selector)
  368. return selector->sel_types;
  369. else
  370. return 0;
  371. }
  372. /* The uninstalled dispatch table. */
  373. extern struct sarray *__objc_uninstalled_dtable;
  374. /* __sel_register_typed_name allocates lots of struct objc_selector:s
  375. of 8 (16, if pointers are 64 bits) bytes at startup. To reduce the
  376. number of malloc calls and memory lost to malloc overhead, we
  377. allocate objc_selector:s in blocks here. This is only called from
  378. __sel_register_typed_name, and __sel_register_typed_name may only
  379. be called when __objc_runtime_mutex is locked.
  380. Note that the objc_selector:s allocated from
  381. __sel_register_typed_name are never freed.
  382. 62 because 62 * sizeof (struct objc_selector) = 496 (992). This
  383. should let malloc add some overhead and use a nice, round 512
  384. (1024) byte chunk. */
  385. #define SELECTOR_POOL_SIZE 62
  386. static struct objc_selector *selector_pool;
  387. static int selector_pool_left;
  388. static struct objc_selector *
  389. pool_alloc_selector(void)
  390. {
  391. if (!selector_pool_left)
  392. {
  393. selector_pool = objc_malloc (sizeof (struct objc_selector)
  394. * SELECTOR_POOL_SIZE);
  395. selector_pool_left = SELECTOR_POOL_SIZE;
  396. }
  397. return &selector_pool[--selector_pool_left];
  398. }
  399. /* Store the passed selector name in the selector record and return
  400. its selector value (value returned by sel_get_uid). Assume that
  401. the calling function has locked down __objc_runtime_mutex. The
  402. 'is_const' parameter tells us if the name and types parameters are
  403. really constant or not. If YES then they are constant and we can
  404. just store the pointers. If NO then we need to copy name and types
  405. because the pointers may disappear later on. If the 'orig'
  406. parameter is not NULL, then we are registering a selector from a
  407. module, and 'orig' is that selector. In this case, we can put the
  408. selector in the tables if needed, and orig->sel_id is updated with
  409. the selector ID of the registered selector, and 'orig' is
  410. returned. */
  411. static SEL
  412. __sel_register_typed_name (const char *name, const char *types,
  413. struct objc_selector *orig, BOOL is_const)
  414. {
  415. struct objc_selector *j;
  416. sidx i;
  417. struct objc_list *l;
  418. i = (sidx) objc_hash_value_for_key (__objc_selector_hash, name);
  419. if (soffset_decode (i) != 0)
  420. {
  421. /* There are already selectors with that name. Examine them to
  422. see if the one we're registering already exists. */
  423. for (l = (struct objc_list *)sarray_get_safe (__objc_selector_array, i);
  424. l; l = l->tail)
  425. {
  426. SEL s = (SEL)l->head;
  427. if (types == 0 || s->sel_types == 0)
  428. {
  429. if (s->sel_types == types)
  430. {
  431. if (orig)
  432. {
  433. orig->sel_id = (void *)i;
  434. return orig;
  435. }
  436. else
  437. return s;
  438. }
  439. }
  440. else if (sel_types_match (s->sel_types, types))
  441. {
  442. if (orig)
  443. {
  444. orig->sel_id = (void *)i;
  445. return orig;
  446. }
  447. else
  448. return s;
  449. }
  450. }
  451. /* A selector with this specific name/type combination does not
  452. exist yet. We need to register it. */
  453. if (orig)
  454. j = orig;
  455. else
  456. j = pool_alloc_selector ();
  457. j->sel_id = (void *)i;
  458. /* Can we use the pointer or must we copy types ? Don't copy if
  459. NULL. */
  460. if ((is_const) || (types == 0))
  461. j->sel_types = types;
  462. else
  463. {
  464. j->sel_types = (char *)objc_malloc (strlen (types) + 1);
  465. strcpy ((char *)j->sel_types, types);
  466. }
  467. l = (struct objc_list *)sarray_get_safe (__objc_selector_array, i);
  468. }
  469. else
  470. {
  471. /* There are no other selectors with this name registered in the
  472. runtime tables. */
  473. const char *new_name;
  474. /* Determine i. */
  475. __objc_selector_max_index += 1;
  476. i = soffset_encode (__objc_selector_max_index);
  477. /* Prepare the selector. */
  478. if (orig)
  479. j = orig;
  480. else
  481. j = pool_alloc_selector ();
  482. j->sel_id = (void *)i;
  483. /* Can we use the pointer or must we copy types ? Don't copy if
  484. NULL. */
  485. if (is_const || (types == 0))
  486. j->sel_types = types;
  487. else
  488. {
  489. j->sel_types = (char *)objc_malloc (strlen (types) + 1);
  490. strcpy ((char *)j->sel_types, types);
  491. }
  492. /* Since this is the first selector with this name, we need to
  493. register the correspondence between 'i' (the sel_id) and
  494. 'name' (the actual string) in __objc_selector_names and
  495. __objc_selector_hash. */
  496. /* Can we use the pointer or must we copy name ? Don't copy if
  497. NULL. (FIXME: Can the name really be NULL here ?) */
  498. if (is_const || (name == 0))
  499. new_name = name;
  500. else
  501. {
  502. new_name = (char *)objc_malloc (strlen (name) + 1);
  503. strcpy ((char *)new_name, name);
  504. }
  505. /* This maps the sel_id to the name. */
  506. sarray_at_put_safe (__objc_selector_names, i, (void *)new_name);
  507. /* This maps the name to the sel_id. */
  508. objc_hash_add (&__objc_selector_hash, (void *)new_name, (void *)i);
  509. l = 0;
  510. }
  511. DEBUG_PRINTF ("Record selector %s[%s] as: %ld\n", name, types,
  512. (long)soffset_decode (i));
  513. /* Now add the selector to the list of selectors with that id. */
  514. l = list_cons ((void *)j, l);
  515. sarray_at_put_safe (__objc_selector_array, i, (void *)l);
  516. sarray_realloc (__objc_uninstalled_dtable, __objc_selector_max_index + 1);
  517. return (SEL)j;
  518. }
  519. SEL
  520. sel_registerName (const char *name)
  521. {
  522. SEL ret;
  523. if (name == NULL)
  524. return NULL;
  525. objc_mutex_lock (__objc_runtime_mutex);
  526. /* Assume that name is not constant static memory and needs to be
  527. copied before put into a runtime structure. is_const == NO. */
  528. ret = __sel_register_typed_name (name, 0, 0, NO);
  529. objc_mutex_unlock (__objc_runtime_mutex);
  530. return ret;
  531. }
  532. SEL
  533. sel_registerTypedName (const char *name, const char *type)
  534. {
  535. SEL ret;
  536. if (name == NULL)
  537. return NULL;
  538. objc_mutex_lock (__objc_runtime_mutex);
  539. /* Assume that name and type are not constant static memory and need
  540. to be copied before put into a runtime structure. is_const ==
  541. NO. */
  542. ret = __sel_register_typed_name (name, type, 0, NO);
  543. objc_mutex_unlock (__objc_runtime_mutex);
  544. return ret;
  545. }
  546. /* Return the selector representing name. */
  547. SEL
  548. sel_getUid (const char *name)
  549. {
  550. return sel_registerTypedName (name, 0);
  551. }