class.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /* GNU Objective C Runtime class related functions
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. Contributed by Kresten Krab Thorup and Dennis Glatting.
  4. Lock-free class table code designed and written from scratch by
  5. Nicola Pero, 2001.
  6. This file is part of GCC.
  7. GCC is free software; you can redistribute it and/or modify it under the
  8. terms of the GNU General Public License as published by the Free Software
  9. Foundation; either version 3, or (at your option) any later version.
  10. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  13. details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. /* The code in this file critically affects class method invocation
  22. speed. This long preamble comment explains why, and the issues
  23. involved.
  24. One of the traditional weaknesses of the GNU Objective-C runtime is
  25. that class method invocations are slow. The reason is that when you
  26. write
  27. array = [NSArray new];
  28. this gets basically compiled into the equivalent of
  29. array = [(objc_get_class ("NSArray")) new];
  30. objc_get_class returns the class pointer corresponding to the string
  31. `NSArray'; and because of the lookup, the operation is more
  32. complicated and slow than a simple instance method invocation.
  33. Most high performance Objective-C code (using the GNU Objc runtime)
  34. I had the opportunity to read (or write) work around this problem by
  35. caching the class pointer:
  36. Class arrayClass = [NSArray class];
  37. ... later on ...
  38. array = [arrayClass new];
  39. array = [arrayClass new];
  40. array = [arrayClass new];
  41. In this case, you always perform a class lookup (the first one), but
  42. then all the [arrayClass new] methods run exactly as fast as an
  43. instance method invocation. It helps if you have many class method
  44. invocations to the same class.
  45. The long-term solution to this problem would be to modify the
  46. compiler to output tables of class pointers corresponding to all the
  47. class method invocations, and to add code to the runtime to update
  48. these tables - that should in the end allow class method invocations
  49. to perform precisely as fast as instance method invocations, because
  50. no class lookup would be involved. I think the Apple Objective-C
  51. runtime uses this technique. Doing this involves synchronized
  52. modifications in the runtime and in the compiler.
  53. As a first medicine to the problem, I [NP] have redesigned and
  54. rewritten the way the runtime is performing class lookup. This
  55. doesn't give as much speed as the other (definitive) approach, but
  56. at least a class method invocation now takes approximately 4.5 times
  57. an instance method invocation on my machine (it would take approx 12
  58. times before the rewriting), which is a lot better.
  59. One of the main reason the new class lookup is so faster is because
  60. I implemented it in a way that can safely run multithreaded without
  61. using locks - a so-called `lock-free' data structure. The atomic
  62. operation is pointer assignment. The reason why in this problem
  63. lock-free data structures work so well is that you never remove
  64. classes from the table - and the difficult thing with lock-free data
  65. structures is freeing data when is removed from the structures. */
  66. #include "objc-private/common.h"
  67. #include "objc-private/error.h"
  68. #include "objc/runtime.h"
  69. #include "objc/thr.h"
  70. #include "objc-private/module-abi-8.h" /* For CLS_ISCLASS and similar. */
  71. #include "objc-private/runtime.h" /* the kitchen sink */
  72. #include "objc-private/sarray.h" /* For sarray_put_at_safe. */
  73. #include "objc-private/selector.h" /* For sarray_put_at_safe. */
  74. #include <string.h> /* For memset */
  75. /* We use a table which maps a class name to the corresponding class
  76. pointer. The first part of this file defines this table, and
  77. functions to do basic operations on the table. The second part of
  78. the file implements some higher level Objective-C functionality for
  79. classes by using the functions provided in the first part to manage
  80. the table. */
  81. /**
  82. ** Class Table Internals
  83. **/
  84. /* A node holding a class */
  85. typedef struct class_node
  86. {
  87. struct class_node *next; /* Pointer to next entry on the list.
  88. NULL indicates end of list. */
  89. const char *name; /* The class name string */
  90. int length; /* The class name string length */
  91. Class pointer; /* The Class pointer */
  92. } *class_node_ptr;
  93. /* A table containing classes is a class_node_ptr (pointing to the
  94. first entry in the table - if it is NULL, then the table is
  95. empty). */
  96. /* We have 1024 tables. Each table contains all class names which
  97. have the same hash (which is a number between 0 and 1023). To look
  98. up a class_name, we compute its hash, and get the corresponding
  99. table. Once we have the table, we simply compare strings directly
  100. till we find the one which we want (using the length first). The
  101. number of tables is quite big on purpose (a normal big application
  102. has less than 1000 classes), so that you shouldn't normally get any
  103. collisions, and get away with a single comparison (which we can't
  104. avoid since we need to know that you have got the right thing). */
  105. #define CLASS_TABLE_SIZE 1024
  106. #define CLASS_TABLE_MASK 1023
  107. static class_node_ptr class_table_array[CLASS_TABLE_SIZE];
  108. /* The table writing mutex - we lock on writing to avoid conflicts
  109. between different writers, but we read without locks. That is
  110. possible because we assume pointer assignment to be an atomic
  111. operation. TODO: This is only true under certain circumstances,
  112. which should be clarified. */
  113. static objc_mutex_t __class_table_lock = NULL;
  114. /* CLASS_TABLE_HASH is how we compute the hash of a class name. It is
  115. a macro - *not* a function - arguments *are* modified directly.
  116. INDEX should be a variable holding an int;
  117. HASH should be a variable holding an int;
  118. CLASS_NAME should be a variable holding a (char *) to the class_name.
  119. After the macro is executed, INDEX contains the length of the
  120. string, and HASH the computed hash of the string; CLASS_NAME is
  121. untouched. */
  122. #define CLASS_TABLE_HASH(INDEX, HASH, CLASS_NAME) \
  123. HASH = 0; \
  124. for (INDEX = 0; CLASS_NAME[INDEX] != '\0'; INDEX++) \
  125. { \
  126. HASH = (HASH << 4) ^ (HASH >> 28) ^ CLASS_NAME[INDEX]; \
  127. } \
  128. \
  129. HASH = (HASH ^ (HASH >> 10) ^ (HASH >> 20)) & CLASS_TABLE_MASK;
  130. /* Setup the table. */
  131. static void
  132. class_table_setup (void)
  133. {
  134. /* Start - nothing in the table. */
  135. memset (class_table_array, 0, sizeof (class_node_ptr) * CLASS_TABLE_SIZE);
  136. /* The table writing mutex. */
  137. __class_table_lock = objc_mutex_allocate ();
  138. }
  139. /* Insert a class in the table (used when a new class is
  140. registered). */
  141. static void
  142. class_table_insert (const char *class_name, Class class_pointer)
  143. {
  144. int hash, length;
  145. class_node_ptr new_node;
  146. /* Find out the class name's hash and length. */
  147. CLASS_TABLE_HASH (length, hash, class_name);
  148. /* Prepare the new node holding the class. */
  149. new_node = objc_malloc (sizeof (struct class_node));
  150. new_node->name = class_name;
  151. new_node->length = length;
  152. new_node->pointer = class_pointer;
  153. /* Lock the table for modifications. */
  154. objc_mutex_lock (__class_table_lock);
  155. /* Insert the new node in the table at the beginning of the table at
  156. class_table_array[hash]. */
  157. new_node->next = class_table_array[hash];
  158. class_table_array[hash] = new_node;
  159. objc_mutex_unlock (__class_table_lock);
  160. }
  161. /* Get a class from the table. This does not need mutex protection.
  162. Currently, this function is called each time you call a static
  163. method, this is why it must be very fast. */
  164. static inline Class
  165. class_table_get_safe (const char *class_name)
  166. {
  167. class_node_ptr node;
  168. int length, hash;
  169. /* Compute length and hash. */
  170. CLASS_TABLE_HASH (length, hash, class_name);
  171. node = class_table_array[hash];
  172. if (node != NULL)
  173. {
  174. do
  175. {
  176. if (node->length == length)
  177. {
  178. /* Compare the class names. */
  179. int i;
  180. for (i = 0; i < length; i++)
  181. {
  182. if ((node->name)[i] != class_name[i])
  183. break;
  184. }
  185. if (i == length)
  186. {
  187. /* They are equal! */
  188. return node->pointer;
  189. }
  190. }
  191. }
  192. while ((node = node->next) != NULL);
  193. }
  194. return Nil;
  195. }
  196. /* Enumerate over the class table. */
  197. struct class_table_enumerator
  198. {
  199. int hash;
  200. class_node_ptr node;
  201. };
  202. static Class
  203. class_table_next (struct class_table_enumerator **e)
  204. {
  205. struct class_table_enumerator *enumerator = *e;
  206. class_node_ptr next;
  207. if (enumerator == NULL)
  208. {
  209. *e = objc_malloc (sizeof (struct class_table_enumerator));
  210. enumerator = *e;
  211. enumerator->hash = 0;
  212. enumerator->node = NULL;
  213. next = class_table_array[enumerator->hash];
  214. }
  215. else
  216. next = enumerator->node->next;
  217. if (next != NULL)
  218. {
  219. enumerator->node = next;
  220. return enumerator->node->pointer;
  221. }
  222. else
  223. {
  224. enumerator->hash++;
  225. while (enumerator->hash < CLASS_TABLE_SIZE)
  226. {
  227. next = class_table_array[enumerator->hash];
  228. if (next != NULL)
  229. {
  230. enumerator->node = next;
  231. return enumerator->node->pointer;
  232. }
  233. enumerator->hash++;
  234. }
  235. /* Ok - table finished - done. */
  236. objc_free (enumerator);
  237. return Nil;
  238. }
  239. }
  240. #if 0 /* DEBUGGING FUNCTIONS */
  241. /* Debugging function - print the class table. */
  242. void
  243. class_table_print (void)
  244. {
  245. int i;
  246. for (i = 0; i < CLASS_TABLE_SIZE; i++)
  247. {
  248. class_node_ptr node;
  249. printf ("%d:\n", i);
  250. node = class_table_array[i];
  251. while (node != NULL)
  252. {
  253. printf ("\t%s\n", node->name);
  254. node = node->next;
  255. }
  256. }
  257. }
  258. /* Debugging function - print an histogram of number of classes in
  259. function of hash key values. Useful to evaluate the hash function
  260. in real cases. */
  261. void
  262. class_table_print_histogram (void)
  263. {
  264. int i, j;
  265. int counter = 0;
  266. for (i = 0; i < CLASS_TABLE_SIZE; i++)
  267. {
  268. class_node_ptr node;
  269. node = class_table_array[i];
  270. while (node != NULL)
  271. {
  272. counter++;
  273. node = node->next;
  274. }
  275. if (((i + 1) % 50) == 0)
  276. {
  277. printf ("%4d:", i + 1);
  278. for (j = 0; j < counter; j++)
  279. printf ("X");
  280. printf ("\n");
  281. counter = 0;
  282. }
  283. }
  284. printf ("%4d:", i + 1);
  285. for (j = 0; j < counter; j++)
  286. printf ("X");
  287. printf ("\n");
  288. }
  289. #endif /* DEBUGGING FUNCTIONS */
  290. /**
  291. ** Objective-C runtime functions
  292. **/
  293. /* From now on, the only access to the class table data structure
  294. should be via the class_table_* functions. */
  295. /* This is a hook which is called by objc_get_class and
  296. objc_lookup_class if the runtime is not able to find the class.
  297. This may e.g. try to load in the class using dynamic loading.
  298. This hook was a public, global variable in the Traditional GNU
  299. Objective-C Runtime API (objc/objc-api.h). The modern GNU
  300. Objective-C Runtime API (objc/runtime.h) provides the
  301. objc_setGetUnknownClassHandler() function instead.
  302. */
  303. Class (*_objc_lookup_class) (const char *name) = 0; /* !T:SAFE */
  304. /* The handler currently in use. PS: if both
  305. __obj_get_unknown_class_handler and _objc_lookup_class are defined,
  306. __objc_get_unknown_class_handler is called first. */
  307. static objc_get_unknown_class_handler
  308. __objc_get_unknown_class_handler = NULL;
  309. objc_get_unknown_class_handler
  310. objc_setGetUnknownClassHandler (objc_get_unknown_class_handler
  311. new_handler)
  312. {
  313. objc_get_unknown_class_handler old_handler
  314. = __objc_get_unknown_class_handler;
  315. __objc_get_unknown_class_handler = new_handler;
  316. return old_handler;
  317. }
  318. /* True when class links has been resolved. */
  319. BOOL __objc_class_links_resolved = NO; /* !T:UNUSED */
  320. void
  321. __objc_init_class_tables (void)
  322. {
  323. /* Allocate the class hash table. */
  324. if (__class_table_lock)
  325. return;
  326. objc_mutex_lock (__objc_runtime_mutex);
  327. class_table_setup ();
  328. objc_mutex_unlock (__objc_runtime_mutex);
  329. }
  330. /* This function adds a class to the class hash table, and assigns the
  331. class a number, unless it's already known. Return 'YES' if the
  332. class was added. Return 'NO' if the class was already known. */
  333. BOOL
  334. __objc_add_class_to_hash (Class class)
  335. {
  336. Class existing_class;
  337. objc_mutex_lock (__objc_runtime_mutex);
  338. /* Make sure the table is there. */
  339. assert (__class_table_lock);
  340. /* Make sure it's not a meta class. */
  341. assert (CLS_ISCLASS (class));
  342. /* Check to see if the class is already in the hash table. */
  343. existing_class = class_table_get_safe (class->name);
  344. if (existing_class)
  345. {
  346. objc_mutex_unlock (__objc_runtime_mutex);
  347. return NO;
  348. }
  349. else
  350. {
  351. /* The class isn't in the hash table. Add the class and assign
  352. a class number. */
  353. static unsigned int class_number = 1;
  354. CLS_SETNUMBER (class, class_number);
  355. CLS_SETNUMBER (class->class_pointer, class_number);
  356. ++class_number;
  357. class_table_insert (class->name, class);
  358. objc_mutex_unlock (__objc_runtime_mutex);
  359. return YES;
  360. }
  361. }
  362. Class
  363. objc_getClass (const char *name)
  364. {
  365. Class class;
  366. if (name == NULL)
  367. return Nil;
  368. class = class_table_get_safe (name);
  369. if (class)
  370. return class;
  371. if (__objc_get_unknown_class_handler)
  372. return (*__objc_get_unknown_class_handler) (name);
  373. if (_objc_lookup_class)
  374. return (*_objc_lookup_class) (name);
  375. return Nil;
  376. }
  377. Class
  378. objc_lookUpClass (const char *name)
  379. {
  380. if (name == NULL)
  381. return Nil;
  382. else
  383. return class_table_get_safe (name);
  384. }
  385. Class
  386. objc_getMetaClass (const char *name)
  387. {
  388. Class class = objc_getClass (name);
  389. if (class)
  390. return class->class_pointer;
  391. else
  392. return Nil;
  393. }
  394. Class
  395. objc_getRequiredClass (const char *name)
  396. {
  397. Class class = objc_getClass (name);
  398. if (class)
  399. return class;
  400. else
  401. _objc_abort ("objc_getRequiredClass ('%s') failed: class not found\n", name);
  402. }
  403. int
  404. objc_getClassList (Class *returnValue, int maxNumberOfClassesToReturn)
  405. {
  406. /* Iterate over all entries in the table. */
  407. int hash, count = 0;
  408. for (hash = 0; hash < CLASS_TABLE_SIZE; hash++)
  409. {
  410. class_node_ptr node = class_table_array[hash];
  411. while (node != NULL)
  412. {
  413. if (returnValue)
  414. {
  415. if (count < maxNumberOfClassesToReturn)
  416. returnValue[count] = node->pointer;
  417. else
  418. return count;
  419. }
  420. count++;
  421. node = node->next;
  422. }
  423. }
  424. return count;
  425. }
  426. Class
  427. objc_allocateClassPair (Class super_class, const char *class_name, size_t extraBytes)
  428. {
  429. Class new_class;
  430. Class new_meta_class;
  431. if (class_name == NULL)
  432. return Nil;
  433. if (objc_getClass (class_name))
  434. return Nil;
  435. if (super_class)
  436. {
  437. /* If you want to build a hierarchy of classes, you need to
  438. build and register them one at a time. The risk is that you
  439. are able to cause confusion by registering a subclass before
  440. the superclass or similar. */
  441. if (CLS_IS_IN_CONSTRUCTION (super_class))
  442. return Nil;
  443. }
  444. /* Technically, we should create the metaclass first, then use
  445. class_createInstance() to create the class. That complication
  446. would be relevant if we had class variables, but we don't, so we
  447. just ignore it and create everything directly and assume all
  448. classes have the same size. */
  449. new_class = objc_calloc (1, sizeof (struct objc_class) + extraBytes);
  450. new_meta_class = objc_calloc (1, sizeof (struct objc_class) + extraBytes);
  451. /* We create an unresolved class, similar to one generated by the
  452. compiler. It will be resolved later when we register it.
  453. Note how the metaclass details are not that important; when the
  454. class is resolved, the ones that matter will be fixed up. */
  455. new_class->class_pointer = new_meta_class;
  456. new_meta_class->class_pointer = 0;
  457. if (super_class)
  458. {
  459. /* Force the name of the superclass in place of the link to the
  460. actual superclass, which will be put there when the class is
  461. resolved. */
  462. const char *super_class_name = class_getName (super_class);
  463. new_class->super_class = (void *)super_class_name;
  464. new_meta_class->super_class = (void *)super_class_name;
  465. }
  466. else
  467. {
  468. new_class->super_class = (void *)0;
  469. new_meta_class->super_class = (void *)0;
  470. }
  471. new_class->name = objc_malloc (strlen (class_name) + 1);
  472. strcpy ((char*)new_class->name, class_name);
  473. new_meta_class->name = new_class->name;
  474. new_class->version = 0;
  475. new_meta_class->version = 0;
  476. new_class->info = _CLS_CLASS | _CLS_IN_CONSTRUCTION;
  477. new_meta_class->info = _CLS_META | _CLS_IN_CONSTRUCTION;
  478. if (super_class)
  479. new_class->instance_size = super_class->instance_size;
  480. else
  481. new_class->instance_size = 0;
  482. new_meta_class->instance_size = sizeof (struct objc_class);
  483. return new_class;
  484. }
  485. void
  486. objc_registerClassPair (Class class_)
  487. {
  488. if (class_ == Nil)
  489. return;
  490. if ((! CLS_ISCLASS (class_)) || (! CLS_IS_IN_CONSTRUCTION (class_)))
  491. return;
  492. if ((! CLS_ISMETA (class_->class_pointer)) || (! CLS_IS_IN_CONSTRUCTION (class_->class_pointer)))
  493. return;
  494. objc_mutex_lock (__objc_runtime_mutex);
  495. if (objc_getClass (class_->name))
  496. {
  497. objc_mutex_unlock (__objc_runtime_mutex);
  498. return;
  499. }
  500. CLS_SET_NOT_IN_CONSTRUCTION (class_);
  501. CLS_SET_NOT_IN_CONSTRUCTION (class_->class_pointer);
  502. __objc_init_class (class_);
  503. /* Resolve class links immediately. No point in waiting. */
  504. __objc_resolve_class_links ();
  505. objc_mutex_unlock (__objc_runtime_mutex);
  506. }
  507. void
  508. objc_disposeClassPair (Class class_)
  509. {
  510. if (class_ == Nil)
  511. return;
  512. if ((! CLS_ISCLASS (class_)) || (! CLS_IS_IN_CONSTRUCTION (class_)))
  513. return;
  514. if ((! CLS_ISMETA (class_->class_pointer)) || (! CLS_IS_IN_CONSTRUCTION (class_->class_pointer)))
  515. return;
  516. /* Undo any class_addIvar(). */
  517. if (class_->ivars)
  518. {
  519. int i;
  520. for (i = 0; i < class_->ivars->ivar_count; i++)
  521. {
  522. struct objc_ivar *ivar = &(class_->ivars->ivar_list[i]);
  523. objc_free ((char *)ivar->ivar_name);
  524. objc_free ((char *)ivar->ivar_type);
  525. }
  526. objc_free (class_->ivars);
  527. }
  528. /* Undo any class_addMethod(). */
  529. if (class_->methods)
  530. {
  531. struct objc_method_list *list = class_->methods;
  532. while (list)
  533. {
  534. int i;
  535. struct objc_method_list *next = list->method_next;
  536. for (i = 0; i < list->method_count; i++)
  537. {
  538. struct objc_method *method = &(list->method_list[i]);
  539. objc_free ((char *)method->method_name);
  540. objc_free ((char *)method->method_types);
  541. }
  542. objc_free (list);
  543. list = next;
  544. }
  545. }
  546. /* Undo any class_addProtocol(). */
  547. if (class_->protocols)
  548. {
  549. struct objc_protocol_list *list = class_->protocols;
  550. while (list)
  551. {
  552. struct objc_protocol_list *next = list->next;
  553. objc_free (list);
  554. list = next;
  555. }
  556. }
  557. /* Undo any class_addMethod() on the meta-class. */
  558. if (class_->class_pointer->methods)
  559. {
  560. struct objc_method_list *list = class_->class_pointer->methods;
  561. while (list)
  562. {
  563. int i;
  564. struct objc_method_list *next = list->method_next;
  565. for (i = 0; i < list->method_count; i++)
  566. {
  567. struct objc_method *method = &(list->method_list[i]);
  568. objc_free ((char *)method->method_name);
  569. objc_free ((char *)method->method_types);
  570. }
  571. objc_free (list);
  572. list = next;
  573. }
  574. }
  575. /* Undo objc_allocateClassPair(). */
  576. objc_free ((char *)(class_->name));
  577. objc_free (class_->class_pointer);
  578. objc_free (class_);
  579. }
  580. /* Traditional GNU Objective-C Runtime API. Important: this method is
  581. called automatically by the compiler while messaging (if using the
  582. traditional ABI), so it is worth keeping it fast; don't make it
  583. just a wrapper around objc_getClass(). */
  584. /* Note that this is roughly equivalent to objc_getRequiredClass(). */
  585. /* Get the class object for the class named NAME. If NAME does not
  586. identify a known class, the hook _objc_lookup_class is called. If
  587. this fails, an error message is issued and the system aborts. */
  588. Class
  589. objc_get_class (const char *name)
  590. {
  591. Class class;
  592. class = class_table_get_safe (name);
  593. if (class)
  594. return class;
  595. if (__objc_get_unknown_class_handler)
  596. class = (*__objc_get_unknown_class_handler) (name);
  597. if ((!class) && _objc_lookup_class)
  598. class = (*_objc_lookup_class) (name);
  599. if (class)
  600. return class;
  601. _objc_abort ("objc runtime: cannot find class %s\n", name);
  602. return 0;
  603. }
  604. /* This is used by the compiler too. */
  605. Class
  606. objc_get_meta_class (const char *name)
  607. {
  608. return objc_get_class (name)->class_pointer;
  609. }
  610. /* This is not used by GCC, but the clang compiler seems to use it
  611. when targeting the GNU runtime. That's wrong, but we have it to
  612. be compatible. */
  613. Class
  614. objc_lookup_class (const char *name)
  615. {
  616. return objc_getClass (name);
  617. }
  618. /* This is used when the implementation of a method changes. It goes
  619. through all classes, looking for the ones that have these methods
  620. (either method_a or method_b; method_b can be NULL), and reloads
  621. the implementation for these. You should call this with the
  622. runtime mutex already locked. */
  623. void
  624. __objc_update_classes_with_methods (struct objc_method *method_a, struct objc_method *method_b)
  625. {
  626. int hash;
  627. /* Iterate over all classes. */
  628. for (hash = 0; hash < CLASS_TABLE_SIZE; hash++)
  629. {
  630. class_node_ptr node = class_table_array[hash];
  631. while (node != NULL)
  632. {
  633. /* We execute this loop twice: the first time, we iterate
  634. over all methods in the class (instance methods), while
  635. the second time we iterate over all methods in the meta
  636. class (class methods). */
  637. Class class = Nil;
  638. BOOL done = NO;
  639. while (done == NO)
  640. {
  641. struct objc_method_list * method_list;
  642. if (class == Nil)
  643. {
  644. /* The first time, we work on the class. */
  645. class = node->pointer;
  646. }
  647. else
  648. {
  649. /* The second time, we work on the meta class. */
  650. class = class->class_pointer;
  651. done = YES;
  652. }
  653. method_list = class->methods;
  654. while (method_list)
  655. {
  656. int i;
  657. for (i = 0; i < method_list->method_count; ++i)
  658. {
  659. struct objc_method *method = &method_list->method_list[i];
  660. /* If the method is one of the ones we are
  661. looking for, update the implementation. */
  662. if (method == method_a)
  663. sarray_at_put_safe (class->dtable,
  664. (sidx) method_a->method_name->sel_id,
  665. method_a->method_imp);
  666. if (method == method_b)
  667. {
  668. if (method_b != NULL)
  669. sarray_at_put_safe (class->dtable,
  670. (sidx) method_b->method_name->sel_id,
  671. method_b->method_imp);
  672. }
  673. }
  674. method_list = method_list->method_next;
  675. }
  676. }
  677. node = node->next;
  678. }
  679. }
  680. }
  681. /* Resolve super/subclass links for all classes. The only thing we
  682. can be sure of is that the class_pointer for class objects point to
  683. the right meta class objects. */
  684. void
  685. __objc_resolve_class_links (void)
  686. {
  687. struct class_table_enumerator *es = NULL;
  688. Class object_class = objc_get_class ("Object");
  689. Class class1;
  690. assert (object_class);
  691. objc_mutex_lock (__objc_runtime_mutex);
  692. /* Assign subclass links. */
  693. while ((class1 = class_table_next (&es)))
  694. {
  695. /* Make sure we have what we think we have. */
  696. assert (CLS_ISCLASS (class1));
  697. assert (CLS_ISMETA (class1->class_pointer));
  698. /* The class_pointer of all meta classes point to Object's meta
  699. class. */
  700. class1->class_pointer->class_pointer = object_class->class_pointer;
  701. if (! CLS_ISRESOLV (class1))
  702. {
  703. CLS_SETRESOLV (class1);
  704. CLS_SETRESOLV (class1->class_pointer);
  705. if (class1->super_class)
  706. {
  707. Class a_super_class
  708. = objc_get_class ((char *) class1->super_class);
  709. assert (a_super_class);
  710. DEBUG_PRINTF ("making class connections for: %s\n",
  711. class1->name);
  712. /* Assign subclass links for superclass. */
  713. class1->sibling_class = a_super_class->subclass_list;
  714. a_super_class->subclass_list = class1;
  715. /* Assign subclass links for meta class of superclass. */
  716. if (a_super_class->class_pointer)
  717. {
  718. class1->class_pointer->sibling_class
  719. = a_super_class->class_pointer->subclass_list;
  720. a_super_class->class_pointer->subclass_list
  721. = class1->class_pointer;
  722. }
  723. }
  724. else /* A root class, make its meta object be a subclass of
  725. Object. */
  726. {
  727. class1->class_pointer->sibling_class
  728. = object_class->subclass_list;
  729. object_class->subclass_list = class1->class_pointer;
  730. }
  731. }
  732. }
  733. /* Assign superclass links. */
  734. es = NULL;
  735. while ((class1 = class_table_next (&es)))
  736. {
  737. Class sub_class;
  738. for (sub_class = class1->subclass_list; sub_class;
  739. sub_class = sub_class->sibling_class)
  740. {
  741. sub_class->super_class = class1;
  742. if (CLS_ISCLASS (sub_class))
  743. sub_class->class_pointer->super_class = class1->class_pointer;
  744. }
  745. }
  746. objc_mutex_unlock (__objc_runtime_mutex);
  747. }
  748. const char *
  749. class_getName (Class class_)
  750. {
  751. if (class_ == Nil)
  752. return "nil";
  753. return class_->name;
  754. }
  755. BOOL
  756. class_isMetaClass (Class class_)
  757. {
  758. /* CLS_ISMETA includes the check for Nil class_. */
  759. return CLS_ISMETA (class_);
  760. }
  761. /* Even inside libobjc it may be worth using class_getSuperclass
  762. instead of accessing class_->super_class directly because it
  763. resolves the class links if needed. If you access
  764. class_->super_class directly, make sure to deal with the situation
  765. where the class is not resolved yet! */
  766. Class
  767. class_getSuperclass (Class class_)
  768. {
  769. if (class_ == Nil)
  770. return Nil;
  771. /* Classes that are in construction are not resolved, and still have
  772. the class name (instead of a class pointer) in the
  773. class_->super_class field. In that case we need to lookup the
  774. superclass name to return the superclass. We can not resolve the
  775. class until it is registered. */
  776. if (CLS_IS_IN_CONSTRUCTION (class_))
  777. {
  778. if (CLS_ISMETA (class_))
  779. return object_getClass ((id)objc_lookUpClass ((const char *)(class_->super_class)));
  780. else
  781. return objc_lookUpClass ((const char *)(class_->super_class));
  782. }
  783. /* If the class is not resolved yet, super_class would point to a
  784. string (the name of the super class) as opposed to the actual
  785. super class. In that case, we need to resolve the class links
  786. before we can return super_class. */
  787. if (! CLS_ISRESOLV (class_))
  788. __objc_resolve_class_links ();
  789. return class_->super_class;
  790. }
  791. int
  792. class_getVersion (Class class_)
  793. {
  794. if (class_ == Nil)
  795. return 0;
  796. return (int)(class_->version);
  797. }
  798. void
  799. class_setVersion (Class class_, int version)
  800. {
  801. if (class_ == Nil)
  802. return;
  803. class_->version = version;
  804. }
  805. size_t
  806. class_getInstanceSize (Class class_)
  807. {
  808. if (class_ == Nil)
  809. return 0;
  810. return class_->instance_size;
  811. }