class.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. /* GNU Objective C Runtime class related functions
  2. Copyright (C) 1993-2022 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. do { \
  124. HASH = 0; \
  125. for (INDEX = 0; CLASS_NAME[INDEX] != '\0'; INDEX++) \
  126. { \
  127. HASH = (HASH << 4) ^ (HASH >> 28) ^ CLASS_NAME[INDEX]; \
  128. } \
  129. \
  130. HASH = (HASH ^ (HASH >> 10) ^ (HASH >> 20)) & CLASS_TABLE_MASK; \
  131. } while (0)
  132. /* Setup the table. */
  133. static void
  134. class_table_setup (void)
  135. {
  136. /* Start - nothing in the table. */
  137. memset (class_table_array, 0, sizeof (class_node_ptr) * CLASS_TABLE_SIZE);
  138. /* The table writing mutex. */
  139. __class_table_lock = objc_mutex_allocate ();
  140. }
  141. /* Insert a class in the table (used when a new class is
  142. registered). */
  143. static void
  144. class_table_insert (const char *class_name, Class class_pointer)
  145. {
  146. int hash, length;
  147. class_node_ptr new_node;
  148. /* Find out the class name's hash and length. */
  149. CLASS_TABLE_HASH (length, hash, class_name);
  150. /* Prepare the new node holding the class. */
  151. new_node = objc_malloc (sizeof (struct class_node));
  152. new_node->name = class_name;
  153. new_node->length = length;
  154. new_node->pointer = class_pointer;
  155. /* Lock the table for modifications. */
  156. objc_mutex_lock (__class_table_lock);
  157. /* Insert the new node in the table at the beginning of the table at
  158. class_table_array[hash]. */
  159. new_node->next = class_table_array[hash];
  160. class_table_array[hash] = new_node;
  161. objc_mutex_unlock (__class_table_lock);
  162. }
  163. /* Get a class from the table. This does not need mutex protection.
  164. Currently, this function is called each time you call a static
  165. method, this is why it must be very fast. */
  166. static inline Class
  167. class_table_get_safe (const char *class_name)
  168. {
  169. class_node_ptr node;
  170. int length, hash;
  171. /* Compute length and hash. */
  172. CLASS_TABLE_HASH (length, hash, class_name);
  173. node = class_table_array[hash];
  174. if (node != NULL)
  175. {
  176. do
  177. {
  178. if (node->length == length)
  179. {
  180. /* Compare the class names. */
  181. int i;
  182. for (i = 0; i < length; i++)
  183. {
  184. if ((node->name)[i] != class_name[i])
  185. break;
  186. }
  187. if (i == length)
  188. {
  189. /* They are equal! */
  190. return node->pointer;
  191. }
  192. }
  193. }
  194. while ((node = node->next) != NULL);
  195. }
  196. return Nil;
  197. }
  198. /* Enumerate over the class table. */
  199. struct class_table_enumerator
  200. {
  201. int hash;
  202. class_node_ptr node;
  203. };
  204. static Class
  205. class_table_next (struct class_table_enumerator **e)
  206. {
  207. struct class_table_enumerator *enumerator = *e;
  208. class_node_ptr next;
  209. if (enumerator == NULL)
  210. {
  211. *e = objc_malloc (sizeof (struct class_table_enumerator));
  212. enumerator = *e;
  213. enumerator->hash = 0;
  214. enumerator->node = NULL;
  215. next = class_table_array[enumerator->hash];
  216. }
  217. else
  218. next = enumerator->node->next;
  219. if (next != NULL)
  220. {
  221. enumerator->node = next;
  222. return enumerator->node->pointer;
  223. }
  224. else
  225. {
  226. enumerator->hash++;
  227. while (enumerator->hash < CLASS_TABLE_SIZE)
  228. {
  229. next = class_table_array[enumerator->hash];
  230. if (next != NULL)
  231. {
  232. enumerator->node = next;
  233. return enumerator->node->pointer;
  234. }
  235. enumerator->hash++;
  236. }
  237. /* Ok - table finished - done. */
  238. objc_free (enumerator);
  239. return Nil;
  240. }
  241. }
  242. #if 0 /* DEBUGGING FUNCTIONS */
  243. /* Debugging function - print the class table. */
  244. void
  245. class_table_print (void)
  246. {
  247. int i;
  248. for (i = 0; i < CLASS_TABLE_SIZE; i++)
  249. {
  250. class_node_ptr node;
  251. printf ("%d:\n", i);
  252. node = class_table_array[i];
  253. while (node != NULL)
  254. {
  255. printf ("\t%s\n", node->name);
  256. node = node->next;
  257. }
  258. }
  259. }
  260. /* Debugging function - print an histogram of number of classes in
  261. function of hash key values. Useful to evaluate the hash function
  262. in real cases. */
  263. void
  264. class_table_print_histogram (void)
  265. {
  266. int i, j;
  267. int counter = 0;
  268. for (i = 0; i < CLASS_TABLE_SIZE; i++)
  269. {
  270. class_node_ptr node;
  271. node = class_table_array[i];
  272. while (node != NULL)
  273. {
  274. counter++;
  275. node = node->next;
  276. }
  277. if (((i + 1) % 50) == 0)
  278. {
  279. printf ("%4d:", i + 1);
  280. for (j = 0; j < counter; j++)
  281. printf ("X");
  282. printf ("\n");
  283. counter = 0;
  284. }
  285. }
  286. printf ("%4d:", i + 1);
  287. for (j = 0; j < counter; j++)
  288. printf ("X");
  289. printf ("\n");
  290. }
  291. #endif /* DEBUGGING FUNCTIONS */
  292. /**
  293. ** Objective-C runtime functions
  294. **/
  295. /* From now on, the only access to the class table data structure
  296. should be via the class_table_* functions. */
  297. /* This is a hook which is called by objc_get_class and
  298. objc_lookup_class if the runtime is not able to find the class.
  299. This may e.g. try to load in the class using dynamic loading.
  300. This hook was a public, global variable in the Traditional GNU
  301. Objective-C Runtime API (objc/objc-api.h). The modern GNU
  302. Objective-C Runtime API (objc/runtime.h) provides the
  303. objc_setGetUnknownClassHandler() function instead.
  304. */
  305. Class (*_objc_lookup_class) (const char *name) = 0; /* !T:SAFE */
  306. /* The handler currently in use. PS: if both
  307. __obj_get_unknown_class_handler and _objc_lookup_class are defined,
  308. __objc_get_unknown_class_handler is called first. */
  309. static objc_get_unknown_class_handler
  310. __objc_get_unknown_class_handler = NULL;
  311. objc_get_unknown_class_handler
  312. objc_setGetUnknownClassHandler (objc_get_unknown_class_handler
  313. new_handler)
  314. {
  315. objc_get_unknown_class_handler old_handler
  316. = __objc_get_unknown_class_handler;
  317. __objc_get_unknown_class_handler = new_handler;
  318. return old_handler;
  319. }
  320. /* True when class links has been resolved. */
  321. BOOL __objc_class_links_resolved = NO; /* !T:UNUSED */
  322. void
  323. __objc_init_class_tables (void)
  324. {
  325. /* Allocate the class hash table. */
  326. if (__class_table_lock)
  327. return;
  328. objc_mutex_lock (__objc_runtime_mutex);
  329. class_table_setup ();
  330. objc_mutex_unlock (__objc_runtime_mutex);
  331. }
  332. /* This function adds a class to the class hash table, and assigns the
  333. class a number, unless it's already known. Return 'YES' if the
  334. class was added. Return 'NO' if the class was already known. */
  335. BOOL
  336. __objc_add_class_to_hash (Class class)
  337. {
  338. Class existing_class;
  339. objc_mutex_lock (__objc_runtime_mutex);
  340. /* Make sure the table is there. */
  341. assert (__class_table_lock);
  342. /* Make sure it's not a meta class. */
  343. assert (CLS_ISCLASS (class));
  344. /* Check to see if the class is already in the hash table. */
  345. existing_class = class_table_get_safe (class->name);
  346. if (existing_class)
  347. {
  348. objc_mutex_unlock (__objc_runtime_mutex);
  349. return NO;
  350. }
  351. else
  352. {
  353. /* The class isn't in the hash table. Add the class and assign
  354. a class number. */
  355. static unsigned int class_number = 1;
  356. CLS_SETNUMBER (class, class_number);
  357. CLS_SETNUMBER (class->class_pointer, class_number);
  358. ++class_number;
  359. class_table_insert (class->name, class);
  360. objc_mutex_unlock (__objc_runtime_mutex);
  361. return YES;
  362. }
  363. }
  364. Class
  365. objc_getClass (const char *name)
  366. {
  367. Class class;
  368. if (name == NULL)
  369. return Nil;
  370. class = class_table_get_safe (name);
  371. if (class)
  372. return class;
  373. if (__objc_get_unknown_class_handler)
  374. return (*__objc_get_unknown_class_handler) (name);
  375. if (_objc_lookup_class)
  376. return (*_objc_lookup_class) (name);
  377. return Nil;
  378. }
  379. Class
  380. objc_lookUpClass (const char *name)
  381. {
  382. if (name == NULL)
  383. return Nil;
  384. else
  385. return class_table_get_safe (name);
  386. }
  387. Class
  388. objc_getMetaClass (const char *name)
  389. {
  390. Class class = objc_getClass (name);
  391. if (class)
  392. return class->class_pointer;
  393. else
  394. return Nil;
  395. }
  396. Class
  397. objc_getRequiredClass (const char *name)
  398. {
  399. Class class = objc_getClass (name);
  400. if (class)
  401. return class;
  402. else
  403. _objc_abort ("objc_getRequiredClass ('%s') failed: class not found\n", name);
  404. }
  405. int
  406. objc_getClassList (Class *returnValue, int maxNumberOfClassesToReturn)
  407. {
  408. /* Iterate over all entries in the table. */
  409. int hash, count = 0;
  410. for (hash = 0; hash < CLASS_TABLE_SIZE; hash++)
  411. {
  412. class_node_ptr node = class_table_array[hash];
  413. while (node != NULL)
  414. {
  415. if (returnValue)
  416. {
  417. if (count < maxNumberOfClassesToReturn)
  418. returnValue[count] = node->pointer;
  419. else
  420. return count;
  421. }
  422. count++;
  423. node = node->next;
  424. }
  425. }
  426. return count;
  427. }
  428. Class
  429. objc_allocateClassPair (Class super_class, const char *class_name, size_t extraBytes)
  430. {
  431. Class new_class;
  432. Class new_meta_class;
  433. if (class_name == NULL)
  434. return Nil;
  435. if (objc_getClass (class_name))
  436. return Nil;
  437. if (super_class)
  438. {
  439. /* If you want to build a hierarchy of classes, you need to
  440. build and register them one at a time. The risk is that you
  441. are able to cause confusion by registering a subclass before
  442. the superclass or similar. */
  443. if (CLS_IS_IN_CONSTRUCTION (super_class))
  444. return Nil;
  445. }
  446. /* Technically, we should create the metaclass first, then use
  447. class_createInstance() to create the class. That complication
  448. would be relevant if we had class variables, but we don't, so we
  449. just ignore it and create everything directly and assume all
  450. classes have the same size. */
  451. new_class = objc_calloc (1, sizeof (struct objc_class) + extraBytes);
  452. new_meta_class = objc_calloc (1, sizeof (struct objc_class) + extraBytes);
  453. /* We create an unresolved class, similar to one generated by the
  454. compiler. It will be resolved later when we register it.
  455. Note how the metaclass details are not that important; when the
  456. class is resolved, the ones that matter will be fixed up. */
  457. new_class->class_pointer = new_meta_class;
  458. new_meta_class->class_pointer = 0;
  459. if (super_class)
  460. {
  461. /* Force the name of the superclass in place of the link to the
  462. actual superclass, which will be put there when the class is
  463. resolved. */
  464. const char *super_class_name = class_getName (super_class);
  465. new_class->super_class = (void *)super_class_name;
  466. new_meta_class->super_class = (void *)super_class_name;
  467. }
  468. else
  469. {
  470. new_class->super_class = (void *)0;
  471. new_meta_class->super_class = (void *)0;
  472. }
  473. new_class->name = objc_malloc (strlen (class_name) + 1);
  474. strcpy ((char*)new_class->name, class_name);
  475. new_meta_class->name = new_class->name;
  476. new_class->version = 0;
  477. new_meta_class->version = 0;
  478. new_class->info = _CLS_CLASS | _CLS_IN_CONSTRUCTION;
  479. new_meta_class->info = _CLS_META | _CLS_IN_CONSTRUCTION;
  480. if (super_class)
  481. new_class->instance_size = super_class->instance_size;
  482. else
  483. new_class->instance_size = 0;
  484. new_meta_class->instance_size = sizeof (struct objc_class);
  485. return new_class;
  486. }
  487. void
  488. objc_registerClassPair (Class class_)
  489. {
  490. if (class_ == Nil)
  491. return;
  492. if ((! CLS_ISCLASS (class_)) || (! CLS_IS_IN_CONSTRUCTION (class_)))
  493. return;
  494. if ((! CLS_ISMETA (class_->class_pointer)) || (! CLS_IS_IN_CONSTRUCTION (class_->class_pointer)))
  495. return;
  496. objc_mutex_lock (__objc_runtime_mutex);
  497. if (objc_getClass (class_->name))
  498. {
  499. objc_mutex_unlock (__objc_runtime_mutex);
  500. return;
  501. }
  502. CLS_SET_NOT_IN_CONSTRUCTION (class_);
  503. CLS_SET_NOT_IN_CONSTRUCTION (class_->class_pointer);
  504. __objc_init_class (class_);
  505. /* Resolve class links immediately. No point in waiting. */
  506. __objc_resolve_class_links ();
  507. objc_mutex_unlock (__objc_runtime_mutex);
  508. }
  509. void
  510. objc_disposeClassPair (Class class_)
  511. {
  512. if (class_ == Nil)
  513. return;
  514. if ((! CLS_ISCLASS (class_)) || (! CLS_IS_IN_CONSTRUCTION (class_)))
  515. return;
  516. if ((! CLS_ISMETA (class_->class_pointer)) || (! CLS_IS_IN_CONSTRUCTION (class_->class_pointer)))
  517. return;
  518. /* Undo any class_addIvar(). */
  519. if (class_->ivars)
  520. {
  521. int i;
  522. for (i = 0; i < class_->ivars->ivar_count; i++)
  523. {
  524. struct objc_ivar *ivar = &(class_->ivars->ivar_list[i]);
  525. objc_free ((char *)ivar->ivar_name);
  526. objc_free ((char *)ivar->ivar_type);
  527. }
  528. objc_free (class_->ivars);
  529. }
  530. /* Undo any class_addMethod(). */
  531. if (class_->methods)
  532. {
  533. struct objc_method_list *list = class_->methods;
  534. while (list)
  535. {
  536. int i;
  537. struct objc_method_list *next = list->method_next;
  538. for (i = 0; i < list->method_count; i++)
  539. {
  540. struct objc_method *method = &(list->method_list[i]);
  541. objc_free ((char *)method->method_name);
  542. objc_free ((char *)method->method_types);
  543. }
  544. objc_free (list);
  545. list = next;
  546. }
  547. }
  548. /* Undo any class_addProtocol(). */
  549. if (class_->protocols)
  550. {
  551. struct objc_protocol_list *list = class_->protocols;
  552. while (list)
  553. {
  554. struct objc_protocol_list *next = list->next;
  555. objc_free (list);
  556. list = next;
  557. }
  558. }
  559. /* Undo any class_addMethod() on the meta-class. */
  560. if (class_->class_pointer->methods)
  561. {
  562. struct objc_method_list *list = class_->class_pointer->methods;
  563. while (list)
  564. {
  565. int i;
  566. struct objc_method_list *next = list->method_next;
  567. for (i = 0; i < list->method_count; i++)
  568. {
  569. struct objc_method *method = &(list->method_list[i]);
  570. objc_free ((char *)method->method_name);
  571. objc_free ((char *)method->method_types);
  572. }
  573. objc_free (list);
  574. list = next;
  575. }
  576. }
  577. /* Undo objc_allocateClassPair(). */
  578. objc_free ((char *)(class_->name));
  579. objc_free (class_->class_pointer);
  580. objc_free (class_);
  581. }
  582. /* Traditional GNU Objective-C Runtime API. Important: this method is
  583. called automatically by the compiler while messaging (if using the
  584. traditional ABI), so it is worth keeping it fast; don't make it
  585. just a wrapper around objc_getClass(). */
  586. /* Note that this is roughly equivalent to objc_getRequiredClass(). */
  587. /* Get the class object for the class named NAME. If NAME does not
  588. identify a known class, the hook _objc_lookup_class is called. If
  589. this fails, an error message is issued and the system aborts. */
  590. Class
  591. objc_get_class (const char *name)
  592. {
  593. Class class;
  594. class = class_table_get_safe (name);
  595. if (class)
  596. return class;
  597. if (__objc_get_unknown_class_handler)
  598. class = (*__objc_get_unknown_class_handler) (name);
  599. if ((!class) && _objc_lookup_class)
  600. class = (*_objc_lookup_class) (name);
  601. if (class)
  602. return class;
  603. _objc_abort ("objc runtime: cannot find class %s\n", name);
  604. return 0;
  605. }
  606. /* This is used by the compiler too. */
  607. Class
  608. objc_get_meta_class (const char *name)
  609. {
  610. return objc_get_class (name)->class_pointer;
  611. }
  612. /* This is not used by GCC, but the clang compiler seems to use it
  613. when targeting the GNU runtime. That's wrong, but we have it to
  614. be compatible. */
  615. Class
  616. objc_lookup_class (const char *name)
  617. {
  618. return objc_getClass (name);
  619. }
  620. /* This is used when the implementation of a method changes. It goes
  621. through all classes, looking for the ones that have these methods
  622. (either method_a or method_b; method_b can be NULL), and reloads
  623. the implementation for these. You should call this with the
  624. runtime mutex already locked. */
  625. void
  626. __objc_update_classes_with_methods (struct objc_method *method_a, struct objc_method *method_b)
  627. {
  628. int hash;
  629. /* Iterate over all classes. */
  630. for (hash = 0; hash < CLASS_TABLE_SIZE; hash++)
  631. {
  632. class_node_ptr node = class_table_array[hash];
  633. while (node != NULL)
  634. {
  635. /* We execute this loop twice: the first time, we iterate
  636. over all methods in the class (instance methods), while
  637. the second time we iterate over all methods in the meta
  638. class (class methods). */
  639. Class class = Nil;
  640. BOOL done = NO;
  641. while (done == NO)
  642. {
  643. struct objc_method_list * method_list;
  644. if (class == Nil)
  645. {
  646. /* The first time, we work on the class. */
  647. class = node->pointer;
  648. }
  649. else
  650. {
  651. /* The second time, we work on the meta class. */
  652. class = class->class_pointer;
  653. done = YES;
  654. }
  655. method_list = class->methods;
  656. while (method_list)
  657. {
  658. int i;
  659. for (i = 0; i < method_list->method_count; ++i)
  660. {
  661. struct objc_method *method = &method_list->method_list[i];
  662. /* If the method is one of the ones we are
  663. looking for, update the implementation. */
  664. if (method == method_a)
  665. sarray_at_put_safe (class->dtable,
  666. (sidx) method_a->method_name->sel_id,
  667. method_a->method_imp);
  668. if (method == method_b)
  669. {
  670. if (method_b != NULL)
  671. sarray_at_put_safe (class->dtable,
  672. (sidx) method_b->method_name->sel_id,
  673. method_b->method_imp);
  674. }
  675. }
  676. method_list = method_list->method_next;
  677. }
  678. }
  679. node = node->next;
  680. }
  681. }
  682. }
  683. /* Resolve super/subclass links for all classes. The only thing we
  684. can be sure of is that the class_pointer for class objects point to
  685. the right meta class objects. */
  686. void
  687. __objc_resolve_class_links (void)
  688. {
  689. struct class_table_enumerator *es = NULL;
  690. Class object_class = objc_get_class ("Object");
  691. Class class1;
  692. assert (object_class);
  693. objc_mutex_lock (__objc_runtime_mutex);
  694. /* Assign subclass links. */
  695. while ((class1 = class_table_next (&es)))
  696. {
  697. /* Make sure we have what we think we have. */
  698. assert (CLS_ISCLASS (class1));
  699. assert (CLS_ISMETA (class1->class_pointer));
  700. /* The class_pointer of all meta classes point to Object's meta
  701. class. */
  702. class1->class_pointer->class_pointer = object_class->class_pointer;
  703. if (! CLS_ISRESOLV (class1))
  704. {
  705. CLS_SETRESOLV (class1);
  706. CLS_SETRESOLV (class1->class_pointer);
  707. if (class1->super_class)
  708. {
  709. Class a_super_class
  710. = objc_get_class ((char *) class1->super_class);
  711. assert (a_super_class);
  712. DEBUG_PRINTF ("making class connections for: %s\n",
  713. class1->name);
  714. /* Assign subclass links for superclass. */
  715. class1->sibling_class = a_super_class->subclass_list;
  716. a_super_class->subclass_list = class1;
  717. /* Assign subclass links for meta class of superclass. */
  718. if (a_super_class->class_pointer)
  719. {
  720. class1->class_pointer->sibling_class
  721. = a_super_class->class_pointer->subclass_list;
  722. a_super_class->class_pointer->subclass_list
  723. = class1->class_pointer;
  724. }
  725. }
  726. else /* A root class, make its meta object be a subclass of
  727. Object. */
  728. {
  729. class1->class_pointer->sibling_class
  730. = object_class->subclass_list;
  731. object_class->subclass_list = class1->class_pointer;
  732. }
  733. }
  734. }
  735. /* Assign superclass links. */
  736. es = NULL;
  737. while ((class1 = class_table_next (&es)))
  738. {
  739. Class sub_class;
  740. for (sub_class = class1->subclass_list; sub_class;
  741. sub_class = sub_class->sibling_class)
  742. {
  743. sub_class->super_class = class1;
  744. if (CLS_ISCLASS (sub_class))
  745. sub_class->class_pointer->super_class = class1->class_pointer;
  746. }
  747. }
  748. objc_mutex_unlock (__objc_runtime_mutex);
  749. }
  750. const char *
  751. class_getName (Class class_)
  752. {
  753. if (class_ == Nil)
  754. return "nil";
  755. return class_->name;
  756. }
  757. BOOL
  758. class_isMetaClass (Class class_)
  759. {
  760. /* CLS_ISMETA includes the check for Nil class_. */
  761. return CLS_ISMETA (class_);
  762. }
  763. /* Even inside libobjc it may be worth using class_getSuperclass
  764. instead of accessing class_->super_class directly because it
  765. resolves the class links if needed. If you access
  766. class_->super_class directly, make sure to deal with the situation
  767. where the class is not resolved yet! */
  768. Class
  769. class_getSuperclass (Class class_)
  770. {
  771. if (class_ == Nil)
  772. return Nil;
  773. /* Classes that are in construction are not resolved, and still have
  774. the class name (instead of a class pointer) in the
  775. class_->super_class field. In that case we need to lookup the
  776. superclass name to return the superclass. We cannot resolve the
  777. class until it is registered. */
  778. if (CLS_IS_IN_CONSTRUCTION (class_))
  779. {
  780. if (CLS_ISMETA (class_))
  781. return object_getClass ((id)objc_lookUpClass ((const char *)(class_->super_class)));
  782. else
  783. return objc_lookUpClass ((const char *)(class_->super_class));
  784. }
  785. /* If the class is not resolved yet, super_class would point to a
  786. string (the name of the super class) as opposed to the actual
  787. super class. In that case, we need to resolve the class links
  788. before we can return super_class. */
  789. if (! CLS_ISRESOLV (class_))
  790. __objc_resolve_class_links ();
  791. return class_->super_class;
  792. }
  793. int
  794. class_getVersion (Class class_)
  795. {
  796. if (class_ == Nil)
  797. return 0;
  798. return (int)(class_->version);
  799. }
  800. void
  801. class_setVersion (Class class_, int version)
  802. {
  803. if (class_ == Nil)
  804. return;
  805. class_->version = version;
  806. }
  807. size_t
  808. class_getInstanceSize (Class class_)
  809. {
  810. if (class_ == Nil)
  811. return 0;
  812. return class_->instance_size;
  813. }