forked from gnustep/libobjc2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_table.c
556 lines (484 loc) · 14.6 KB
/
class_table.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#include "objc/runtime.h"
#include "objc/hooks.h"
#include "objc/developer.h"
#include "alias.h"
#include "class.h"
#include "method_list.h"
#include "selector.h"
#include "lock.h"
#include "ivar.h"
#include "dtable.h"
#include "visibility.h"
#include <stdlib.h>
#include <assert.h>
void objc_register_selectors_from_class(Class class);
void objc_init_protocols(struct objc_protocol_list *protos);
void objc_compute_ivar_offsets(Class class);
////////////////////////////////////////////////////////////////////////////////
// +load method hash table
////////////////////////////////////////////////////////////////////////////////
static int imp_compare(const void *i1, void *i2)
{
return i1 == i2;
}
static int32_t imp_hash(const void *imp)
{
return (int32_t)(((uintptr_t)imp) >> 4);
}
#define MAP_TABLE_NAME load_messages
#define MAP_TABLE_COMPARE_FUNCTION imp_compare
#define MAP_TABLE_HASH_KEY imp_hash
#define MAP_TABLE_HASH_VALUE imp_hash
#include "hash_table.h"
static load_messages_table *load_table;
SEL loadSel;
PRIVATE void objc_init_load_messages_table(void)
{
load_messages_initialize(&load_table, 4096);
loadSel = sel_registerName("load");
}
PRIVATE void objc_send_load_message(Class class)
{
Class meta = class->isa;
for (struct objc_method_list *l=meta->methods ; NULL!=l ; l=l->next)
{
for (int i=0 ; i<l->count ; i++)
{
Method m = &l->methods[i];
if (sel_isEqual(m->selector, loadSel))
{
if (load_messages_table_get(load_table, m->imp) == 0)
{
m->imp((id)class, loadSel);
load_messages_insert(load_table, m->imp);
}
}
}
}
}
// Get the functions for string hashing
#include "string_hash.h"
static int class_compare(const char *name, const Class class)
{
return string_compare(name, class->name);
}
static int class_hash(const Class class)
{
return string_hash(class->name);
}
#define MAP_TABLE_NAME class_table_internal
#define MAP_TABLE_COMPARE_FUNCTION class_compare
#define MAP_TABLE_HASH_KEY string_hash
#define MAP_TABLE_HASH_VALUE class_hash
// This defines the maximum number of classes that the runtime supports.
/*
#define MAP_TABLE_STATIC_SIZE 2048
#define MAP_TABLE_STATIC_NAME class_table
*/
#include "hash_table.h"
static class_table_internal_table *class_table;
#define unresolved_class_next subclass_list
#define unresolved_class_prev sibling_class
/**
* Linked list using the subclass_list pointer in unresolved classes.
*/
static Class unresolved_class_list;
static enum objc_developer_mode_np mode;
void objc_setDeveloperMode_np(enum objc_developer_mode_np newMode)
{
mode = newMode;
}
////////////////////////////////////////////////////////////////////////////////
// Class table manipulation
////////////////////////////////////////////////////////////////////////////////
PRIVATE Class zombie_class;
PRIVATE void class_table_insert(Class class)
{
if (!objc_test_class_flag(class, objc_class_flag_resolved))
{
if (Nil != unresolved_class_list)
{
unresolved_class_list->unresolved_class_prev = class;
}
class->unresolved_class_next = unresolved_class_list;
unresolved_class_list = class;
}
if ((0 == zombie_class) && (strcmp("NSZombie", class->name) == 0))
{
zombie_class = class;
}
class_table_internal_insert(class_table, class);
}
PRIVATE Class class_table_get_safe(const char *class_name)
{
if (NULL == class_name) { return Nil; }
return class_table_internal_table_get(class_table, class_name);
}
PRIVATE Class class_table_next(void **e)
{
return class_table_internal_next(class_table,
(struct class_table_internal_table_enumerator**)e);
}
PRIVATE void init_class_tables(void)
{
class_table_internal_initialize(&class_table, 4096);
objc_init_load_messages_table();
}
////////////////////////////////////////////////////////////////////////////////
// Loader functions
////////////////////////////////////////////////////////////////////////////////
PRIVATE BOOL objc_resolve_class(Class cls)
{
// Skip this if the class is already resolved.
if (objc_test_class_flag(cls, objc_class_flag_resolved)) { return YES; }
// We can only resolve the class if its superclass is resolved.
if (cls->super_class)
{
Class super = (Class)objc_getClass((char*)cls->super_class);
if (Nil == super) { return NO; }
if (!objc_test_class_flag(super, objc_class_flag_resolved))
{
if (!objc_resolve_class(super))
{
return NO;
}
}
}
// Remove the class from the unresolved class list
if (Nil == cls->unresolved_class_prev)
{
unresolved_class_list = cls->unresolved_class_next;
}
else
{
cls->unresolved_class_prev->unresolved_class_next =
cls->unresolved_class_next;
}
if (Nil != cls->unresolved_class_next)
{
cls->unresolved_class_next->unresolved_class_prev =
cls->unresolved_class_prev;
}
cls->unresolved_class_prev = Nil;
cls->unresolved_class_next = Nil;
// The superclass for the metaclass. This is the metaclass for the
// superclass if one exists, otherwise it is the root class itself
Class superMeta = Nil;
// The metaclass for the metaclass. This is always the root class's
// metaclass.
Class metaMeta = Nil;
// Resolve the superclass pointer
if (NULL == cls->super_class)
{
superMeta = cls;
metaMeta = cls->isa;
}
else
{
// Resolve the superclass if it isn't already resolved
Class super = (Class)objc_getClass((char*)cls->super_class);
if (!objc_test_class_flag(super, objc_class_flag_resolved))
{
objc_resolve_class(super);
}
superMeta = super->isa;
// Set the superclass pointer for the class and the superclass
cls->super_class = super;
do
{
metaMeta = super->isa;
super = super->super_class;
} while (Nil != super);
}
Class meta = cls->isa;
// Make the root class the superclass of the metaclass (e.g. NSObject is
// the superclass of all metaclasses in classes that inherit from NSObject)
meta->super_class = superMeta;
meta->isa = metaMeta;
// Don't register root classes as children of anything
if (Nil != cls->super_class)
{
// Set up the class links
cls->sibling_class = cls->super_class->subclass_list;
cls->super_class->subclass_list = cls;
}
// Set up the metaclass links
meta->sibling_class = superMeta->subclass_list;
superMeta->subclass_list = meta;
// Mark this class (and its metaclass) as resolved
objc_set_class_flag(cls, objc_class_flag_resolved);
objc_set_class_flag(cls->isa, objc_class_flag_resolved);
// Fix up the ivar offsets
objc_compute_ivar_offsets(cls);
// Send the +load message, if required
if (!objc_test_class_flag(cls, objc_class_flag_user_created))
{
objc_send_load_message(cls);
}
if (_objc_load_callback)
{
_objc_load_callback(cls, 0);
}
return YES;
}
PRIVATE void objc_resolve_class_links(void)
{
LOCK_RUNTIME_FOR_SCOPE();
Class class = unresolved_class_list;
BOOL resolvedClass;
do
{
resolvedClass = NO;
while ((Nil != class))
{
Class next = class->unresolved_class_next;
objc_resolve_class(class);
if (resolvedClass ||
objc_test_class_flag(class, objc_class_flag_resolved))
{
resolvedClass = YES;
}
class = next;
}
} while (resolvedClass);
}
void __objc_resolve_class_links(void)
{
static BOOL warned = NO;
if (!warned)
{
fprintf(stderr,
"Warning: Calling deprecated private ObjC runtime function %s\n", __func__);
warned = YES;
}
objc_resolve_class_links();
}
static void reload_class(struct objc_class *class, struct objc_class *old)
{
const char *superclassName = (char*)class->super_class;
class->super_class = class_table_get_safe(superclassName);
// Checking the instance sizes are equal here is a quick-and-dirty test.
// It's not actually needed, because we're testing the ivars are at the
// same locations next, but it lets us skip those tests if the total size
// is different.
BOOL equalLayouts = (class->super_class == old->super_class) &&
(class->instance_size == old->instance_size);
// If either of the classes has an empty ivar list, then the other one must too.
if ((NULL == class->ivars) || (NULL == old->ivars))
{
equalLayouts &= (class->ivars == old->ivars);
}
else
{
// If the class sizes are the same, ensure that the ivars have the same
// types, names, and offsets. Note: Renaming an ivar is treated as a
// conflict because name changes are often accompanied by semantic
// changes. For example, an object ivar at offset 16 goes from being
// called 'delegate' to being called 'view' - we almost certainly don't
// want methods that expect to be working with the delegate ivar to
// work with the view ivar now!
for (int i=0 ; equalLayouts && (i<old->ivars->count) ; i++)
{
struct objc_ivar *oldIvar = &old->ivars->ivar_list[i];
struct objc_ivar *newIvar = &class->ivars->ivar_list[i];
equalLayouts &= strcmp(oldIvar->name, newIvar->name) == 0;
equalLayouts &= strcmp(oldIvar->type, newIvar->type) == 0;
equalLayouts &= (oldIvar->offset == newIvar->offset);
}
}
// If the layouts are equal, then we can simply tack the class's method
// list on to the front of the old class and update the dtable.
if (equalLayouts)
{
class->methods->next = old->methods;
old->methods = class->methods;
objc_update_dtable_for_class(old);
return;
}
// If we get to here, then we are adding a new class. This is where things
// start to get a bit tricky...
// Ideally, we'd want to capture the subclass list here. Unfortunately,
// this is not possible because the subclass will contain methods that
// refer to ivars in the superclass.
//
// We can't use the non-fragile ABI's offset facility easily, because we'd
// have to have two (or more) offsets for the same ivar. This gets messy
// very quickly. Ideally, we'd want every class to include ivar offsets
// for every single (public) ivar in its superclasses. These could then be
// updated by copies of the class. Defining a development ABI is something
// to consider for a future release.
class->subclass_list = NULL;
// Replace the old class with this one in the class table. New lookups for
// this class will now return this class.
class_table_internal_table_set(class_table, (void*)class->name, class);
// Register all of the selectors used by this class and its metaclass
objc_register_selectors_from_class(class);
objc_register_selectors_from_class(class->isa);
// Set the uninstalled dtable. The compiler could do this as well.
class->dtable = uninstalled_dtable;
class->isa->dtable = uninstalled_dtable;
// If this is a root class, make the class into the metaclass's superclass.
// This means that all instance methods will be available to the class.
if (NULL == superclassName)
{
class->isa->super_class = class;
}
if (class->protocols)
{
objc_init_protocols(class->protocols);
}
}
/**
* Loads a class. This function assumes that the runtime mutex is locked.
*/
PRIVATE void objc_load_class(struct objc_class *class)
{
struct objc_class *existingClass = class_table_get_safe(class->name);
if (Nil != existingClass)
{
if (objc_developer_mode_developer != mode)
{
fprintf(stderr,
"Loading two versions of %s. The class that will be used is undefined\n",
class->name);
return;
}
reload_class(class, existingClass);
return;
}
// The compiler initialises the super class pointer to the name of the
// superclass, not the superclass pointer.
// Note: With the new ABI, the class pointer is public. We could,
// therefore, directly reference the superclass from the compiler and make
// the linker resolve it. This should be done in the GCC-incompatible ABI.
const char *superclassName = (char*)class->super_class;
// Work around a bug in some versions of GCC that don't initialize the
// class structure correctly.
class->subclass_list = NULL;
// Insert the class into the class table
class_table_insert(class);
// Register all of the selectors used by this class and its metaclass
objc_register_selectors_from_class(class);
objc_register_selectors_from_class(class->isa);
// Set the uninstalled dtable. The compiler could do this as well.
class->dtable = uninstalled_dtable;
class->isa->dtable = uninstalled_dtable;
// If this is a root class, make the class into the metaclass's superclass.
// This means that all instance methods will be available to the class.
if (NULL == superclassName)
{
class->isa->super_class = class;
}
if (class->protocols)
{
objc_init_protocols(class->protocols);
}
}
PRIVATE Class SmallObjectClasses[7];
BOOL objc_registerSmallObjectClass_np(Class class, uintptr_t mask)
{
if ((mask & OBJC_SMALL_OBJECT_MASK) != mask)
{
return NO;
}
if (sizeof(void*) == 4)
{
if (Nil == SmallObjectClasses[0])
{
SmallObjectClasses[0] = class;
return YES;
}
return NO;
}
if (Nil != SmallObjectClasses[mask])
{
return NO;
}
SmallObjectClasses[mask] = class;
return YES;
}
////////////////////////////////////////////////////////////////////////////////
// Public API
////////////////////////////////////////////////////////////////////////////////
int objc_getClassList(Class *buffer, int bufferLen)
{
if (buffer == NULL || bufferLen == 0)
{
return class_table->table_used;
}
int count = 0;
struct class_table_internal_table_enumerator *e = NULL;
Class next;
while (count < bufferLen &&
(next = class_table_internal_next(class_table, &e)))
{
buffer[count++] = next;
}
return count;
}
Class *objc_copyClassList(unsigned int *outCount)
{
int count = class_table->table_used;
Class *buffer = calloc(sizeof(Class), count);
if (NULL != outCount)
{
*outCount = count;
}
objc_getClassList(buffer, count);
return buffer;
}
Class class_getSuperclass(Class cls)
{
if (Nil == cls) { return Nil; }
if (!objc_test_class_flag(cls, objc_class_flag_resolved))
{
objc_resolve_class(cls);
}
return cls->super_class;
}
id objc_getClass(const char *name)
{
id class = (id)class_table_get_safe(name);
if (nil != class) { return class; }
// Second chance lookup via @compatibilty_alias:
class = (id)alias_getClass(name);
if (nil != class) { return class; }
// Third chance lookup via the hook:
if (0 != _objc_lookup_class)
{
class = (id)_objc_lookup_class(name);
}
return class;
}
id objc_lookUpClass(const char *name)
{
return (id)class_table_get_safe(name);
}
id objc_getMetaClass(const char *name)
{
Class cls = (Class)objc_getClass(name);
return cls == Nil ? nil : (id)cls->isa;
}
// Legacy interface compatibility
id objc_get_class(const char *name)
{
return objc_getClass(name);
}
id objc_lookup_class(const char *name)
{
return objc_getClass(name);
}
id objc_get_meta_class(const char *name)
{
return objc_getMetaClass(name);
}
Class objc_next_class(void **enum_state)
{
return class_table_next ( enum_state);
}
Class class_pose_as(Class impostor, Class super_class)
{
fprintf(stderr, "Class posing is no longer supported.\n");
fprintf(stderr, "Please use class_replaceMethod() instead.\n");
abort();
}