ObjFW  Check-in [754bf24742]

Overview
Comment:More style improvements
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 754bf247423a353273b6c62f315cef2501edb99b2d5f33dec765606cbe45f9b8
User & Date: js on 2019-02-07 21:31:16
Other Links: manifest | tags
Context
2019-02-07
21:55
More style improvements check-in: 239fbb269e user: js tags: trunk
21:31
More style improvements check-in: 754bf24742 user: js tags: trunk
00:46
Make style consistent between ObjFW and ObjFW_RT check-in: ef6d69931e user: js tags: trunk
Changes

Modified src/OFApplication.h from [8ae28aad85] to [1dfb4a501e].

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@class OFArray OF_GENERIC(ObjectType);
@class OFDictionary OF_GENERIC(KeyType, ObjectType);
@class OFMutableArray OF_GENERIC(ObjectType);
@class OFMutableDictionary OF_GENERIC(KeyType, ObjectType);
@class OFSandbox;
@class OFString;

#define OF_APPLICATION_DELEGATE(cls)					\
	int								\
	main(int argc, char *argv[])					\
	{								\
		return of_application_main(&argc, &argv,		\
		    (cls *)[[cls alloc] init]);				\
	}

#ifdef OF_HAVE_PLEDGE
# define OF_HAVE_SANDBOX
#endif

/*!







|




|







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@class OFArray OF_GENERIC(ObjectType);
@class OFDictionary OF_GENERIC(KeyType, ObjectType);
@class OFMutableArray OF_GENERIC(ObjectType);
@class OFMutableDictionary OF_GENERIC(KeyType, ObjectType);
@class OFSandbox;
@class OFString;

#define OF_APPLICATION_DELEGATE(class_)					\
	int								\
	main(int argc, char *argv[])					\
	{								\
		return of_application_main(&argc, &argv,		\
		    (class_ *)[[class_ alloc] init]);			\
	}

#ifdef OF_HAVE_PLEDGE
# define OF_HAVE_SANDBOX
#endif

/*!

Modified src/OFBlock.m from [f20a52dd95] to [dd601345d1].

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
struct objc_abi_class _NSConcreteMallocBlock = {
	&_NSConcreteMallocBlock_metaclass, "OFBlock", "OFMallocBlock",
	8, OBJC_CLASS_INFO_CLASS, sizeof(of_block_literal_t), NULL, NULL
};

static struct {
	unsigned long unknown;
	struct objc_abi_selector *sel_refs;
	uint16_t cls_def_cnt, cat_def_cnt;
	void *defs[4];
} symtab = {
	0, NULL, 3, 0,
	{
		&_NSConcreteStackBlock, &_NSConcreteGlobalBlock,
		&_NSConcreteMallocBlock, NULL
	}







|
|







99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
struct objc_abi_class _NSConcreteMallocBlock = {
	&_NSConcreteMallocBlock_metaclass, "OFBlock", "OFMallocBlock",
	8, OBJC_CLASS_INFO_CLASS, sizeof(of_block_literal_t), NULL, NULL
};

static struct {
	unsigned long unknown;
	struct objc_abi_selector *selectorRefs;
	uint16_t classDefsCount, categoryDefsCount;
	void *defs[4];
} symtab = {
	0, NULL, 3, 0,
	{
		&_NSConcreteStackBlock, &_NSConcreteGlobalBlock,
		&_NSConcreteMallocBlock, NULL
	}
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
		OF_ENSURE(of_spinlock_unlock(&byrefSpinlocks[hash]));
#endif
		break;
	}
}

void
_Block_object_dispose(const void *obj_, const int flags_)
{
	const int flags = flags_ & (OF_BLOCK_FIELD_IS_BLOCK |
	    OF_BLOCK_FIELD_IS_OBJECT | OF_BLOCK_FIELD_IS_BYREF);

	if (obj_ == NULL)
		return;

	switch (flags) {
	case OF_BLOCK_FIELD_IS_BLOCK:
		_Block_release(obj_);
		break;
	case OF_BLOCK_FIELD_IS_OBJECT:
		if (!(flags_ & OF_BLOCK_BYREF_CALLER))
			[(id)obj_ release];
		break;
	case OF_BLOCK_FIELD_IS_BYREF:;
		of_block_byref_t *obj = (of_block_byref_t *)obj_;

		obj = obj->forwarding;

#ifdef OF_HAVE_ATOMIC_OPS
		if ((of_atomic_int_dec(&obj->flags) &
		    OF_BLOCK_REFCOUNT_MASK) == 0) {
			if (obj->flags & OF_BLOCK_HAS_COPY_DISPOSE)
				obj->byref_dispose(obj);

			free(obj);
		}
#else
		unsigned hash = SPINLOCK_HASH(obj);

		OF_ENSURE(of_spinlock_lock(&byrefSpinlocks[hash]));
		if ((--obj->flags & OF_BLOCK_REFCOUNT_MASK) == 0) {
			OF_ENSURE(of_spinlock_unlock(&byrefSpinlocks[hash]));

			if (obj->flags & OF_BLOCK_HAS_COPY_DISPOSE)
				obj->byref_dispose(obj);

			free(obj);
		}
		OF_ENSURE(of_spinlock_unlock(&byrefSpinlocks[hash]));
#endif
		break;
	}
}








|




|




|



|


|

|


|

|
|

|


|


|


|
|

|







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
		OF_ENSURE(of_spinlock_unlock(&byrefSpinlocks[hash]));
#endif
		break;
	}
}

void
_Block_object_dispose(const void *object_, const int flags_)
{
	const int flags = flags_ & (OF_BLOCK_FIELD_IS_BLOCK |
	    OF_BLOCK_FIELD_IS_OBJECT | OF_BLOCK_FIELD_IS_BYREF);

	if (object_ == NULL)
		return;

	switch (flags) {
	case OF_BLOCK_FIELD_IS_BLOCK:
		_Block_release(object_);
		break;
	case OF_BLOCK_FIELD_IS_OBJECT:
		if (!(flags_ & OF_BLOCK_BYREF_CALLER))
			[(id)object_ release];
		break;
	case OF_BLOCK_FIELD_IS_BYREF:;
		of_block_byref_t *object = (of_block_byref_t *)object_;

		object = object->forwarding;

#ifdef OF_HAVE_ATOMIC_OPS
		if ((of_atomic_int_dec(&object->flags) &
		    OF_BLOCK_REFCOUNT_MASK) == 0) {
			if (object->flags & OF_BLOCK_HAS_COPY_DISPOSE)
				object->byref_dispose(object);

			free(object);
		}
#else
		unsigned hash = SPINLOCK_HASH(object);

		OF_ENSURE(of_spinlock_lock(&byrefSpinlocks[hash]));
		if ((--object->flags & OF_BLOCK_REFCOUNT_MASK) == 0) {
			OF_ENSURE(of_spinlock_unlock(&byrefSpinlocks[hash]));

			if (object->flags & OF_BLOCK_HAS_COPY_DISPOSE)
				object->byref_dispose(object);

			free(object);
		}
		OF_ENSURE(of_spinlock_unlock(&byrefSpinlocks[hash]));
#endif
		break;
	}
}

Modified src/OFObject.m from [cd9c001f04] to [edba45d06c].

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
static void
enumerationMutationHandler(id object)
{
	@throw [OFEnumerationMutationException exceptionWithObject: object];
}

void OF_NO_RETURN_FUNC
of_method_not_found(id obj, SEL sel)
{
	[obj doesNotRecognizeSelector: sel];

	/*
	 * Just in case doesNotRecognizeSelector: returned, even though it must
	 * never return.
	 */
	abort();

	OF_UNREACHABLE
}

void OF_NO_RETURN_FUNC
of_method_not_found_stret(void *st, id obj, SEL sel)
{
	of_method_not_found(obj, sel);
}

id
of_alloc_object(Class class, size_t extraSize, size_t extraAlignment,
    void **extra)
{
	OFObject *instance;







|

|











|

|







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
static void
enumerationMutationHandler(id object)
{
	@throw [OFEnumerationMutationException exceptionWithObject: object];
}

void OF_NO_RETURN_FUNC
of_method_not_found(id object, SEL selector)
{
	[object doesNotRecognizeSelector: selector];

	/*
	 * Just in case doesNotRecognizeSelector: returned, even though it must
	 * never return.
	 */
	abort();

	OF_UNREACHABLE
}

void OF_NO_RETURN_FUNC
of_method_not_found_stret(void *stret, id object, SEL selector)
{
	of_method_not_found(object, selector);
}

id
of_alloc_object(Class class, size_t extraSize, size_t extraAlignment,
    void **extra)
{
	OFObject *instance;

Modified src/foundation-compat.m from [a5f000b446] to [8a3ccfcff7].

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
static id
alloc(Class self, SEL _cmd)
{
	return [OFAutoreleasePool alloc];
}

static void
addObject(Class self, SEL _cmd, id obj)
{
	[OFAutoreleasePool addObject: obj];
}

static id
autorelease(id self, SEL _cmd)
{
	[OFAutoreleasePool addObject: self];








|

|







39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
static id
alloc(Class self, SEL _cmd)
{
	return [OFAutoreleasePool alloc];
}

static void
addObject(Class self, SEL _cmd, id object)
{
	[OFAutoreleasePool addObject: object];
}

static id
autorelease(id self, SEL _cmd)
{
	[OFAutoreleasePool addObject: self];

Modified src/instance.m from [64f7431604] to [77311e8f90].

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
 * file.
 */

#include "config.h"

#import "OFObject.h"

static SEL constructSel = NULL;
static SEL destructSel = NULL;

static bool
callConstructors(Class cls, id obj)
{
	Class super = class_getSuperclass(cls);
	id (*construct)(id, SEL);
	id (*last)(id, SEL);

	if (super != nil)
		if (!callConstructors(super, obj))
			return false;

	if (constructSel == NULL)
		constructSel = sel_registerName(".cxx_construct");

	if (!class_respondsToSelector(cls, constructSel))
		return true;

	construct = (id (*)(id, SEL))
	    class_getMethodImplementation(cls, constructSel);
	last = (id (*)(id, SEL))
	    class_getMethodImplementation(super, constructSel);

	if (construct == last)
		return true;

	return (construct(obj, constructSel) != nil);
}

id
objc_constructInstance(Class cls, void *bytes)
{
	id obj = (id)bytes;

	if (cls == Nil || bytes == NULL)
		return nil;

	object_setClass(obj, cls);

	if (!callConstructors(cls, obj))
		return nil;

	return obj;
}

void *
objc_destructInstance(id obj)
{
	Class cls;
	void (*last)(id, SEL) = NULL;

	if (obj == nil)
		return NULL;

#ifdef OF_OBJFW_RUNTIME
	objc_zero_weak_references(obj);
#endif

	if (destructSel == NULL)
		destructSel = sel_registerName(".cxx_destruct");

	for (cls = object_getClass(obj); cls != Nil;
	    cls = class_getSuperclass(cls)) {
		void (*destruct)(id, SEL);

		if (class_respondsToSelector(cls, destructSel)) {
			if ((destruct = (void (*)(id, SEL))
			    class_getMethodImplementation(cls,
			    destructSel)) != last)
				destruct(obj, destructSel);

			last = destruct;
		} else
			break;
	}

	return obj;
}







|
|


|

|




|


|
|

|



|

|




|



|

|

|


|

|


|



|

|


|



|


|
|

|
|


|

|
|
|






|

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
 * file.
 */

#include "config.h"

#import "OFObject.h"

static SEL constructSelector = NULL;
static SEL destructSelector = NULL;

static bool
callConstructors(Class class, id object)
{
	Class super = class_getSuperclass(class);
	id (*construct)(id, SEL);
	id (*last)(id, SEL);

	if (super != nil)
		if (!callConstructors(super, object))
			return false;

	if (constructSelector == NULL)
		constructSelector = sel_registerName(".cxx_construct");

	if (!class_respondsToSelector(class, constructSelector))
		return true;

	construct = (id (*)(id, SEL))
	    class_getMethodImplementation(class, constructSelector);
	last = (id (*)(id, SEL))
	    class_getMethodImplementation(super, constructSelector);

	if (construct == last)
		return true;

	return (construct(object, constructSelector) != nil);
}

id
objc_constructInstance(Class class, void *bytes)
{
	id object = (id)bytes;

	if (class == Nil || bytes == NULL)
		return nil;

	object_setClass(object, class);

	if (!callConstructors(class, object))
		return nil;

	return object;
}

void *
objc_destructInstance(id object)
{
	Class class;
	void (*last)(id, SEL) = NULL;

	if (object == nil)
		return NULL;

#ifdef OF_OBJFW_RUNTIME
	objc_zero_weak_references(object);
#endif

	if (destructSelector == NULL)
		destructSelector = sel_registerName(".cxx_destruct");

	for (class = object_getClass(object); class != Nil;
	    class = class_getSuperclass(class)) {
		void (*destruct)(id, SEL);

		if (class_respondsToSelector(class, destructSelector)) {
			if ((destruct = (void (*)(id, SEL))
			    class_getMethodImplementation(class,
			    destructSelector)) != last)
				destruct(object, destructSelector);

			last = destruct;
		} else
			break;
	}

	return object;
}

Modified src/macros.h from [ac7aa6dc5d] to [6558af17e7].

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# define OF_NULL_RESETTABLE_PROPERTY
# define nonnull
# define nullable
# define null_unspecified
#endif

#if __has_feature(objc_kindof)
# define OF_KINDOF(cls) __kindof cls
#else
# define OF_KINDOF(cls) id
#endif

#if __has_feature(objc_class_property)
# define OF_HAVE_CLASS_PROPERTIES
#endif

#if defined(__clang__) || OF_GCC_VERSION >= 405







|

|







207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# define OF_NULL_RESETTABLE_PROPERTY
# define nonnull
# define nullable
# define null_unspecified
#endif

#if __has_feature(objc_kindof)
# define OF_KINDOF(class_) __kindof class_
#else
# define OF_KINDOF(class_) id
#endif

#if __has_feature(objc_class_property)
# define OF_HAVE_CLASS_PROPERTIES
#endif

#if defined(__clang__) || OF_GCC_VERSION >= 405

Modified src/runtime/ObjFW_RT.h from [eecb74dd6c] to [9b77c85f63].

104
105
106
107
108
109
110

111



112
113
114
115
116
117
118
struct objc_selector {
	uintptr_t UID;
	const char *_Nullable typeEncoding;
};

struct objc_super {
	id __unsafe_unretained _Nullable self;

	Class _Nonnull cls;



};

struct objc_method {
	struct objc_selector selector;
	IMP _Nonnull implementation;
};








>
|
>
>
>







104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
struct objc_selector {
	uintptr_t UID;
	const char *_Nullable typeEncoding;
};

struct objc_super {
	id __unsafe_unretained _Nullable self;
#ifdef __cplusplus
	Class _Nonnull class_;
#else
	Class _Nonnull class;
#endif
};

struct objc_method {
	struct objc_selector selector;
	IMP _Nonnull implementation;
};

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
extern "C" {
#endif
extern SEL _Nonnull sel_registerName(const char *_Nonnull name);
extern const char *_Nonnull sel_getName(SEL _Nonnull selector);
extern bool sel_isEqual(SEL _Nonnull selector1, SEL _Nonnull selector2);
extern Class _Nonnull objc_allocateClassPair(Class _Nullable superclass,
    const char *_Nonnull name, size_t extraBytes);
extern void objc_registerClassPair(Class _Nonnull cls);
extern unsigned int objc_getClassList(Class _Nonnull *_Nullable buffer,
    unsigned int count);
extern Class _Nonnull *_Nonnull objc_copyClassList(
    unsigned int *_Nullable length);
extern bool class_isMetaClass(Class _Nullable cls);
extern const char *_Nullable class_getName(Class _Nullable cls);
extern Class _Nullable class_getSuperclass(Class _Nullable cls);
extern unsigned long class_getInstanceSize(Class _Nullable cls);
extern bool class_respondsToSelector(Class _Nullable cls,
    SEL _Nonnull selector);
extern bool class_conformsToProtocol(Class _Nullable cls,
    Protocol *_Nonnull protocol);
extern IMP _Nullable class_getMethodImplementation(Class _Nullable cls,
    SEL _Nonnull selector);
extern IMP _Nullable class_getMethodImplementation_stret(Class _Nullable cls,
    SEL _Nonnull selector);
extern const char *_Nullable class_getMethodTypeEncoding(Class _Nullable cls,
    SEL _Nonnull selector);
extern bool class_addMethod(Class _Nonnull cls, SEL _Nonnull selector,
    IMP _Nonnull implementation, const char *_Nullable typeEncoding);
extern IMP _Nullable class_replaceMethod(Class _Nonnull cls,
    SEL _Nonnull selector, IMP _Nonnull implementation,
    const char *_Nullable typeEncoding);
extern Class _Nullable object_getClass(id _Nullable object);
extern Class _Nullable object_setClass(id _Nullable object, Class _Nonnull cls);

extern const char *_Nullable object_getClassName(id _Nullable object);
extern const char *_Nonnull protocol_getName(Protocol *_Nonnull protocol);
extern bool protocol_isEqual(Protocol *_Nonnull protocol1,
    Protocol *_Nonnull protocol2);
extern bool protocol_conformsToProtocol(Protocol *_Nonnull protocol1,
    Protocol *_Nonnull protocol2);
extern void objc_exit(void);







|




|
|
|
|
|

|

|

|

|

|

|



|
>







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
extern "C" {
#endif
extern SEL _Nonnull sel_registerName(const char *_Nonnull name);
extern const char *_Nonnull sel_getName(SEL _Nonnull selector);
extern bool sel_isEqual(SEL _Nonnull selector1, SEL _Nonnull selector2);
extern Class _Nonnull objc_allocateClassPair(Class _Nullable superclass,
    const char *_Nonnull name, size_t extraBytes);
extern void objc_registerClassPair(Class _Nonnull class_);
extern unsigned int objc_getClassList(Class _Nonnull *_Nullable buffer,
    unsigned int count);
extern Class _Nonnull *_Nonnull objc_copyClassList(
    unsigned int *_Nullable length);
extern bool class_isMetaClass(Class _Nullable class_);
extern const char *_Nullable class_getName(Class _Nullable class_);
extern Class _Nullable class_getSuperclass(Class _Nullable class_);
extern unsigned long class_getInstanceSize(Class _Nullable class_);
extern bool class_respondsToSelector(Class _Nullable class_,
    SEL _Nonnull selector);
extern bool class_conformsToProtocol(Class _Nullable class_,
    Protocol *_Nonnull protocol);
extern IMP _Nullable class_getMethodImplementation(Class _Nullable class_,
    SEL _Nonnull selector);
extern IMP _Nullable class_getMethodImplementation_stret(Class _Nullable class_,
    SEL _Nonnull selector);
extern const char *_Nullable class_getMethodTypeEncoding(Class _Nullable class_,
    SEL _Nonnull selector);
extern bool class_addMethod(Class _Nonnull class_, SEL _Nonnull selector,
    IMP _Nonnull implementation, const char *_Nullable typeEncoding);
extern IMP _Nullable class_replaceMethod(Class _Nonnull class_,
    SEL _Nonnull selector, IMP _Nonnull implementation,
    const char *_Nullable typeEncoding);
extern Class _Nullable object_getClass(id _Nullable object);
extern Class _Nullable object_setClass(id _Nullable object,
    Class _Nonnull class_);
extern const char *_Nullable object_getClassName(id _Nullable object);
extern const char *_Nonnull protocol_getName(Protocol *_Nonnull protocol);
extern bool protocol_isEqual(Protocol *_Nonnull protocol1,
    Protocol *_Nonnull protocol2);
extern bool protocol_conformsToProtocol(Protocol *_Nonnull protocol1,
    Protocol *_Nonnull protocol2);
extern void objc_exit(void);

Modified src/runtime/ObjFW_RT.sfd from [fb5bdffa34] to [a242780665].

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void objc_exception_throw_m68k(id _Nonnull object)(a0)
int objc_sync_enter_m68k(id _Nullable object)(a0)
int objc_sync_exit_m68k(id _Nullable object)(a0)
id objc_getProperty_m68k(id _Nonnull self, SEL _Nonnull _cmd, ptrdiff_t offset, bool atomic)(a0,a1,d0,d1)
void objc_setProperty_m68k(id _Nonnull self, SEL _Nonnull _cmd, ptrdiff_t offset, id value, bool atomic, signed char copy)(a0,a1,d0,a2,d1,d2)
void objc_getPropertyStruct_m68k(void *_Nonnull dest, const void *_Nonnull src, ptrdiff_t size, bool atomic, bool strong)(a0,a1,d0,d1,d2)
void objc_setPropertyStruct_m68k(void *_Nonnull dest, const void *_Nonnull src, ptrdiff_t size, bool atomic, bool strong)(a0,a1,d0,d1,d2)
void objc_enumerationMutation_m68k(id _Nonnull obj)(a0)
int __gnu_objc_personality_v0_m68k(int version, int actions, uint64_t *_Nonnull exClass, void *_Nonnull ex, void *_Nonnull ctx)(d0,d1,d2,a0,a1)
int __gnu_objc_personality_sj0_m68k(int version, int actions, uint64_t *_Nonnull exClass, void *_Nonnull ex, void *_Nonnull ctx)(d0,d1,d2,a0,a1)
id _Nullable objc_retain_m68k(id _Nullable object)(a0)
id _Nullable objc_retainBlock_m68k(id _Nullable block)(a0)
id _Nullable objc_retainAutorelease_m68k(id _Nullable object)(a0)
void objc_release_m68k(id _Nullable object)(a0)
id _Nullable objc_autorelease_m68k(id _Nullable object)(a0)







|







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void objc_exception_throw_m68k(id _Nonnull object)(a0)
int objc_sync_enter_m68k(id _Nullable object)(a0)
int objc_sync_exit_m68k(id _Nullable object)(a0)
id objc_getProperty_m68k(id _Nonnull self, SEL _Nonnull _cmd, ptrdiff_t offset, bool atomic)(a0,a1,d0,d1)
void objc_setProperty_m68k(id _Nonnull self, SEL _Nonnull _cmd, ptrdiff_t offset, id value, bool atomic, signed char copy)(a0,a1,d0,a2,d1,d2)
void objc_getPropertyStruct_m68k(void *_Nonnull dest, const void *_Nonnull src, ptrdiff_t size, bool atomic, bool strong)(a0,a1,d0,d1,d2)
void objc_setPropertyStruct_m68k(void *_Nonnull dest, const void *_Nonnull src, ptrdiff_t size, bool atomic, bool strong)(a0,a1,d0,d1,d2)
void objc_enumerationMutation_m68k(id _Nonnull object)(a0)
int __gnu_objc_personality_v0_m68k(int version, int actions, uint64_t *_Nonnull exClass, void *_Nonnull ex, void *_Nonnull ctx)(d0,d1,d2,a0,a1)
int __gnu_objc_personality_sj0_m68k(int version, int actions, uint64_t *_Nonnull exClass, void *_Nonnull ex, void *_Nonnull ctx)(d0,d1,d2,a0,a1)
id _Nullable objc_retain_m68k(id _Nullable object)(a0)
id _Nullable objc_retainBlock_m68k(id _Nullable block)(a0)
id _Nullable objc_retainAutorelease_m68k(id _Nullable object)(a0)
void objc_release_m68k(id _Nullable object)(a0)
id _Nullable objc_autorelease_m68k(id _Nullable object)(a0)
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
* These only need glue on MorphOS. As none of them are functions that are used
* in hot paths, it's easier to also use the glue on AmigaOS 3, which also has
* the benefit of having all __saveds in a single place.
SEL _Nonnull sel_registerName_m68k(const char *_Nonnull name)(a0)
const char *_Nonnull sel_getName_m68k(SEL _Nonnull selector)(a0)
bool sel_isEqual_m68k(SEL _Nonnull selector1, SEL _Nonnull selector2)(a0,a1)
Class _Nonnull objc_allocateClassPair_m68k(Class _Nullable superclass, const char *_Nonnull name, size_t extraBytes)(a0,a1,d0)
void objc_registerClassPair_m68k(Class _Nonnull cls)(a0)
unsigned int objc_getClassList_m68k(Class _Nonnull *_Nullable buffer, unsigned int count)(a0,d0)
Class _Nonnull *_Nonnull objc_copyClassList_m68k(unsigned int *_Nullable length)(a0)
bool class_isMetaClass_m68k(Class _Nullable cls)(a0)
const char *_Nullable class_getName_m68k(Class _Nullable cls)(a0)
Class _Nullable class_getSuperclass_m68k(Class _Nullable cls)(a0)
unsigned long class_getInstanceSize_m68k(Class _Nullable cls)(a0)
bool class_respondsToSelector_m68k(Class _Nullable cls, SEL _Nonnull selector)(a0,a1)
bool class_conformsToProtocol_m68k(Class _Nullable cls, Protocol *_Nonnull p)(a0,a1)
IMP _Nullable class_getMethodImplementation_m68k(Class _Nullable cls, SEL _Nonnull selector)(a0,a1)
IMP _Nullable class_getMethodImplementation_stret_m68k(Class _Nullable cls, SEL _Nonnull selector)(a0,a1)
const char *_Nullable class_getMethodTypeEncoding_m68k(Class _Nullable cls, SEL _Nonnull selector)(a0,a1)
bool class_addMethod_m68k(Class _Nonnull cls, SEL _Nonnull selector, IMP _Nonnull implementation, const char *_Nullable typeEncoding)(a0,a1,a2,a3)
IMP _Nullable class_replaceMethod_m68k(Class _Nonnull cls, SEL _Nonnull selector, IMP _Nonnull implementation, const char *_Nullable typeEncoding)(a0,a1,a2,a3)
Class _Nullable object_getClass_m68k(id _Nullable object)(a0)
Class _Nullable object_setClass_m68k(id _Nullable object, Class _Nonnull cls)(a0,a1)
const char *_Nullable object_getClassName_m68k(id _Nullable object)(a0)
const char *_Nonnull protocol_getName_m68k(Protocol *_Nonnull protocol)(a0)
bool protocol_isEqual_m68k(Protocol *_Nonnull protocol1, Protocol *_Nonnull protocol2)(a0,a1)
bool protocol_conformsToProtocol_m68k(Protocol *_Nonnull protocol1, Protocol *_Nonnull protocol2)(a0,a1)
void objc_exit_m68k(void)()
_Nullable objc_uncaught_exception_handler objc_setUncaughtExceptionHandler_m68k(objc_uncaught_exception_handler _Nullable handler)(a0)
void objc_setForwardHandler_m68k(IMP _Nullable forward, IMP _Nullable stretForward)(a0,a1)
void objc_setEnumerationMutationHandler_m68k(objc_enumeration_mutation_handler _Nullable handler)(a0)
void objc_zero_weak_references_m68k(id _Nonnull value)(a0)
* SysV functions for MorphOS could be added here for performance. Having them
* in addition to the m68k functions allows m68k applications to call into the
* PPC library, while native code can use the SysV functions.
==end







|


|
|
|
|
|
|
|
|
|
|
|

|













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
* These only need glue on MorphOS. As none of them are functions that are used
* in hot paths, it's easier to also use the glue on AmigaOS 3, which also has
* the benefit of having all __saveds in a single place.
SEL _Nonnull sel_registerName_m68k(const char *_Nonnull name)(a0)
const char *_Nonnull sel_getName_m68k(SEL _Nonnull selector)(a0)
bool sel_isEqual_m68k(SEL _Nonnull selector1, SEL _Nonnull selector2)(a0,a1)
Class _Nonnull objc_allocateClassPair_m68k(Class _Nullable superclass, const char *_Nonnull name, size_t extraBytes)(a0,a1,d0)
void objc_registerClassPair_m68k(Class _Nonnull class_)(a0)
unsigned int objc_getClassList_m68k(Class _Nonnull *_Nullable buffer, unsigned int count)(a0,d0)
Class _Nonnull *_Nonnull objc_copyClassList_m68k(unsigned int *_Nullable length)(a0)
bool class_isMetaClass_m68k(Class _Nullable class_)(a0)
const char *_Nullable class_getName_m68k(Class _Nullable class_)(a0)
Class _Nullable class_getSuperclass_m68k(Class _Nullable class_)(a0)
unsigned long class_getInstanceSize_m68k(Class _Nullable class_)(a0)
bool class_respondsToSelector_m68k(Class _Nullable class_, SEL _Nonnull selector)(a0,a1)
bool class_conformsToProtocol_m68k(Class _Nullable class_, Protocol *_Nonnull p)(a0,a1)
IMP _Nullable class_getMethodImplementation_m68k(Class _Nullable class_, SEL _Nonnull selector)(a0,a1)
IMP _Nullable class_getMethodImplementation_stret_m68k(Class _Nullable class_, SEL _Nonnull selector)(a0,a1)
const char *_Nullable class_getMethodTypeEncoding_m68k(Class _Nullable class_, SEL _Nonnull selector)(a0,a1)
bool class_addMethod_m68k(Class _Nonnull class_, SEL _Nonnull selector, IMP _Nonnull implementation, const char *_Nullable typeEncoding)(a0,a1,a2,a3)
IMP _Nullable class_replaceMethod_m68k(Class _Nonnull class_, SEL _Nonnull selector, IMP _Nonnull implementation, const char *_Nullable typeEncoding)(a0,a1,a2,a3)
Class _Nullable object_getClass_m68k(id _Nullable object)(a0)
Class _Nullable object_setClass_m68k(id _Nullable object, Class _Nonnull class_)(a0,a1)
const char *_Nullable object_getClassName_m68k(id _Nullable object)(a0)
const char *_Nonnull protocol_getName_m68k(Protocol *_Nonnull protocol)(a0)
bool protocol_isEqual_m68k(Protocol *_Nonnull protocol1, Protocol *_Nonnull protocol2)(a0,a1)
bool protocol_conformsToProtocol_m68k(Protocol *_Nonnull protocol1, Protocol *_Nonnull protocol2)(a0,a1)
void objc_exit_m68k(void)()
_Nullable objc_uncaught_exception_handler objc_setUncaughtExceptionHandler_m68k(objc_uncaught_exception_handler _Nullable handler)(a0)
void objc_setForwardHandler_m68k(IMP _Nullable forward, IMP _Nullable stretForward)(a0,a1)
void objc_setEnumerationMutationHandler_m68k(objc_enumeration_mutation_handler _Nullable handler)(a0)
void objc_zero_weak_references_m68k(id _Nonnull value)(a0)
* SysV functions for MorphOS could be added here for performance. Having them
* in addition to the m68k functions allows m68k applications to call into the
* PPC library, while native code can use the SysV functions.
==end

Modified src/runtime/amiga-glue.m from [850cd5db39] to [6e9a75e691].

390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406

	return objc_allocateClassPair(superclass, name, extraBytes);
}

void __saveds
objc_registerClassPair_m68k(void)
{
	OBJC_M68K_ARG(Class, cls, a0)

	objc_registerClassPair(cls);
}

unsigned int __saveds
objc_getClassList_m68k(void)
{
	OBJC_M68K_ARG(Class *, buffer, a0)
	OBJC_M68K_ARG(unsigned int, count, d0)







|

|







390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406

	return objc_allocateClassPair(superclass, name, extraBytes);
}

void __saveds
objc_registerClassPair_m68k(void)
{
	OBJC_M68K_ARG(Class, class, a0)

	objc_registerClassPair(class);
}

unsigned int __saveds
objc_getClassList_m68k(void)
{
	OBJC_M68K_ARG(Class *, buffer, a0)
	OBJC_M68K_ARG(unsigned int, count, d0)
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

	return objc_copyClassList(length);
}

bool __saveds
class_isMetaClass_m68k(void)
{
	OBJC_M68K_ARG(Class, cls, a0)

	return class_isMetaClass(cls);
}

const char *__saveds
class_getName_m68k(void)
{
	OBJC_M68K_ARG(Class, cls, a0)

	return class_getName(cls);
}

Class __saveds
class_getSuperclass_m68k(void)
{
	OBJC_M68K_ARG(Class, cls, a0)

	return class_getSuperclass(cls);
}

unsigned long __saveds
class_getInstanceSize_m68k(void)
{
	OBJC_M68K_ARG(Class, cls, a0)

	return class_getInstanceSize(cls);
}

bool __saveds
class_respondsToSelector_m68k(void)
{
	OBJC_M68K_ARG(Class, cls, a0)
	OBJC_M68K_ARG(SEL, selector, a1)

	return class_respondsToSelector(cls, selector);
}

bool __saveds
class_conformsToProtocol_m68k(void)
{
	OBJC_M68K_ARG(Class, cls, a0)
	OBJC_M68K_ARG(Protocol *, protocol, a1)

	return class_conformsToProtocol(cls, protocol);
}

IMP __saveds
class_getMethodImplementation_m68k(void)
{
	OBJC_M68K_ARG(Class, cls, a0)
	OBJC_M68K_ARG(SEL, selector, a1)

	return class_getMethodImplementation(cls, selector);
}

IMP __saveds
class_getMethodImplementation_stret_m68k(void)
{
	OBJC_M68K_ARG(Class, cls, a0)
	OBJC_M68K_ARG(SEL, selector, a1)

	return class_getMethodImplementation_stret(cls, selector);
}

const char *__saveds
class_getMethodTypeEncoding_m68k(void)
{
	OBJC_M68K_ARG(Class, cls, a0)
	OBJC_M68K_ARG(SEL, selector, a1)

	return class_getMethodTypeEncoding(cls, selector);
}

bool __saveds
class_addMethod_m68k(void)
{
	OBJC_M68K_ARG(Class, cls, a0)
	OBJC_M68K_ARG(SEL, selector, a1)
	OBJC_M68K_ARG(IMP, implementation, a2)
	OBJC_M68K_ARG(const char *, typeEncoding, a3)

	return class_addMethod(cls, selector, implementation, typeEncoding);
}

IMP __saveds
class_replaceMethod_m68k(void)
{
	OBJC_M68K_ARG(Class, cls, a0)
	OBJC_M68K_ARG(SEL, selector, a1)
	OBJC_M68K_ARG(IMP, implementation, a2)
	OBJC_M68K_ARG(const char *, typeEncoding, a3)

	return class_replaceMethod(cls, selector, implementation, typeEncoding);

}

Class __saveds
object_getClass_m68k(void)
{
	OBJC_M68K_ARG(id, object, a0)

	return object_getClass(object);
}

Class __saveds
object_setClass_m68k(void)
{
	OBJC_M68K_ARG(id, object, a0)
	OBJC_M68K_ARG(Class, cls, a1)

	return object_setClass(object, cls);
}

const char *__saveds
object_getClassName_m68k(void)
{
	OBJC_M68K_ARG(id, object, a0)








|

|





|

|





|

|





|

|





|


|





|


|





|


|





|


|





|


|





|




|





|




|
>














|

|







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

	return objc_copyClassList(length);
}

bool __saveds
class_isMetaClass_m68k(void)
{
	OBJC_M68K_ARG(Class, class, a0)

	return class_isMetaClass(class);
}

const char *__saveds
class_getName_m68k(void)
{
	OBJC_M68K_ARG(Class, class, a0)

	return class_getName(class);
}

Class __saveds
class_getSuperclass_m68k(void)
{
	OBJC_M68K_ARG(Class, class, a0)

	return class_getSuperclass(class);
}

unsigned long __saveds
class_getInstanceSize_m68k(void)
{
	OBJC_M68K_ARG(Class, class, a0)

	return class_getInstanceSize(class);
}

bool __saveds
class_respondsToSelector_m68k(void)
{
	OBJC_M68K_ARG(Class, class, a0)
	OBJC_M68K_ARG(SEL, selector, a1)

	return class_respondsToSelector(class, selector);
}

bool __saveds
class_conformsToProtocol_m68k(void)
{
	OBJC_M68K_ARG(Class, class, a0)
	OBJC_M68K_ARG(Protocol *, protocol, a1)

	return class_conformsToProtocol(class, protocol);
}

IMP __saveds
class_getMethodImplementation_m68k(void)
{
	OBJC_M68K_ARG(Class, class, a0)
	OBJC_M68K_ARG(SEL, selector, a1)

	return class_getMethodImplementation(class, selector);
}

IMP __saveds
class_getMethodImplementation_stret_m68k(void)
{
	OBJC_M68K_ARG(Class, class, a0)
	OBJC_M68K_ARG(SEL, selector, a1)

	return class_getMethodImplementation_stret(class, selector);
}

const char *__saveds
class_getMethodTypeEncoding_m68k(void)
{
	OBJC_M68K_ARG(Class, class, a0)
	OBJC_M68K_ARG(SEL, selector, a1)

	return class_getMethodTypeEncoding(class, selector);
}

bool __saveds
class_addMethod_m68k(void)
{
	OBJC_M68K_ARG(Class, class, a0)
	OBJC_M68K_ARG(SEL, selector, a1)
	OBJC_M68K_ARG(IMP, implementation, a2)
	OBJC_M68K_ARG(const char *, typeEncoding, a3)

	return class_addMethod(class, selector, implementation, typeEncoding);
}

IMP __saveds
class_replaceMethod_m68k(void)
{
	OBJC_M68K_ARG(Class, class, a0)
	OBJC_M68K_ARG(SEL, selector, a1)
	OBJC_M68K_ARG(IMP, implementation, a2)
	OBJC_M68K_ARG(const char *, typeEncoding, a3)

	return class_replaceMethod(class, selector, implementation,
	    typeEncoding);
}

Class __saveds
object_getClass_m68k(void)
{
	OBJC_M68K_ARG(id, object, a0)

	return object_getClass(object);
}

Class __saveds
object_setClass_m68k(void)
{
	OBJC_M68K_ARG(id, object, a0)
	OBJC_M68K_ARG(Class, class, a1)

	return object_setClass(object, class);
}

const char *__saveds
object_getClassName_m68k(void)
{
	OBJC_M68K_ARG(id, object, a0)

Modified src/runtime/arc.m from [7827e43c55] to [39e51eca73].

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

static struct objc_hashtable *hashtable;
#ifdef OF_HAVE_THREADS
static of_spinlock_t spinlock;
#endif

static uint32_t
hash(const void *obj)
{
	return (uint32_t)(uintptr_t)obj;
}

static bool
equal(const void *obj1, const void *obj2)
{
	return (obj1 == obj2);
}

OF_CONSTRUCTOR()
{
	hashtable = objc_hashtable_new(hash, equal, 2);

#ifdef OF_HAVE_THREADS







|

|



|

|







34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

static struct objc_hashtable *hashtable;
#ifdef OF_HAVE_THREADS
static of_spinlock_t spinlock;
#endif

static uint32_t
hash(const void *object)
{
	return (uint32_t)(uintptr_t)object;
}

static bool
equal(const void *object1, const void *object2)
{
	return (object1 == object2);
}

OF_CONSTRUCTOR()
{
	hashtable = objc_hashtable_new(hash, equal, 2);

#ifdef OF_HAVE_THREADS

Modified src/runtime/category.m from [ca01b62fa9] to [153181212b].

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
			    &methodList->methods[i]);
}

static void
registerCategory(struct objc_abi_category *category)
{
	struct objc_abi_category **categories;
	Class cls = objc_classname_to_class(category->className, false);

	if (categoriesMap == NULL)
		categoriesMap = objc_hashtable_new(
		    objc_hash_string, objc_equal_string, 2);

	categories = (struct objc_abi_category **)objc_hashtable_get(
	    categoriesMap, category->className);







|







43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
			    &methodList->methods[i]);
}

static void
registerCategory(struct objc_abi_category *category)
{
	struct objc_abi_category **categories;
	Class class = objc_classname_to_class(category->className, false);

	if (categoriesMap == NULL)
		categoriesMap = objc_hashtable_new(
		    objc_hash_string, objc_equal_string, 2);

	categories = (struct objc_abi_category **)objc_hashtable_get(
	    categoriesMap, category->className);
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
			    category->className);

		newCategories[i] = category;
		newCategories[i + 1] = NULL;
		objc_hashtable_set(categoriesMap, category->className,
		    newCategories);

		if (cls != Nil && cls->info & OBJC_CLASS_INFO_SETUP) {
			objc_update_dtable(cls);
			objc_update_dtable(cls->isa);
		}

		return;
	}

	if ((categories = malloc(
	    2 * sizeof(struct objc_abi_category *))) == NULL)
		OBJC_ERROR("Not enough memory for category %s of class %s!\n",
		    category->categoryName, category->className);

	categories[0] = category;
	categories[1] = NULL;
	objc_hashtable_set(categoriesMap, category->className, categories);

	if (cls != Nil && cls->info & OBJC_CLASS_INFO_SETUP) {
		objc_update_dtable(cls);
		objc_update_dtable(cls->isa);
	}
}

void
objc_register_all_categories(struct objc_abi_symtab *symtab)
{
	struct objc_abi_category **categories =
	    (struct objc_abi_category **)symtab->defs + symtab->classDefsCount;

	for (size_t i = 0; i < symtab->categoryDefsCount; i++) {
		registerSelectors(categories[i]);
		registerCategory(categories[i]);
	}
}

struct objc_category **
objc_categories_for_class(Class cls)
{
	if (categoriesMap == NULL)
		return NULL;

	return (struct objc_category **)objc_hashtable_get(categoriesMap,
	    cls->name);
}

void
objc_unregister_all_categories(void)
{
	if (categoriesMap == NULL)
		return;







|
|
|














|
|
|
















|





|







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
			    category->className);

		newCategories[i] = category;
		newCategories[i + 1] = NULL;
		objc_hashtable_set(categoriesMap, category->className,
		    newCategories);

		if (class != Nil && class->info & OBJC_CLASS_INFO_SETUP) {
			objc_update_dtable(class);
			objc_update_dtable(class->isa);
		}

		return;
	}

	if ((categories = malloc(
	    2 * sizeof(struct objc_abi_category *))) == NULL)
		OBJC_ERROR("Not enough memory for category %s of class %s!\n",
		    category->categoryName, category->className);

	categories[0] = category;
	categories[1] = NULL;
	objc_hashtable_set(categoriesMap, category->className, categories);

	if (class != Nil && class->info & OBJC_CLASS_INFO_SETUP) {
		objc_update_dtable(class);
		objc_update_dtable(class->isa);
	}
}

void
objc_register_all_categories(struct objc_abi_symtab *symtab)
{
	struct objc_abi_category **categories =
	    (struct objc_abi_category **)symtab->defs + symtab->classDefsCount;

	for (size_t i = 0; i < symtab->categoryDefsCount; i++) {
		registerSelectors(categories[i]);
		registerCategory(categories[i]);
	}
}

struct objc_category **
objc_categories_for_class(Class class)
{
	if (categoriesMap == NULL)
		return NULL;

	return (struct objc_category **)objc_hashtable_get(categoriesMap,
	    class->name);
}

void
objc_unregister_all_categories(void)
{
	if (categoriesMap == NULL)
		return;

Modified src/runtime/class.m from [66376a6ccb] to [f8bb9979ba].

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
static Class *loadQueue = NULL;
static size_t loadQueueCount = 0;
static struct objc_dtable *emptyDTable = NULL;
static unsigned lookupsUntilFastPath = 128;
static struct objc_sparsearray *fastPath = NULL;

static void
registerClass(struct objc_abi_class *cls)
{
	if (classes == NULL)
		classes = objc_hashtable_new(
		    objc_hash_string, objc_equal_string, 2);

	objc_hashtable_set(classes, cls->name, cls);

	if (emptyDTable == NULL)
		emptyDTable = objc_dtable_new();

	cls->DTable = emptyDTable;
	cls->metaclass->DTable = emptyDTable;

	if (strcmp(cls->name, "Protocol") != 0)
		classesCount++;
}

bool
class_registerAlias_np(Class cls, const char *name)
{
	objc_global_mutex_lock();

	if (classes == NULL) {
		objc_global_mutex_unlock();

		return NO;
	}

	objc_hashtable_set(classes, name, (Class)((uintptr_t)cls | 1));

	objc_global_mutex_unlock();

	return YES;
}

static void
registerSelectors(struct objc_abi_class *cls)
{
	struct objc_abi_method_list *methodList;

	for (methodList = cls->methodList; methodList != NULL;
	    methodList = methodList->next)
		for (unsigned int i = 0; i < methodList->count; i++)
			objc_register_selector((struct objc_abi_selector *)
			    &methodList->methods[i]);
}

Class
objc_classname_to_class(const char *name, bool cache)
{
	Class cls;

	if (classes == NULL)
		return Nil;

	/*
	 * Fast path
	 *







|





|




|
|

|




|









|







|



|









|







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
static Class *loadQueue = NULL;
static size_t loadQueueCount = 0;
static struct objc_dtable *emptyDTable = NULL;
static unsigned lookupsUntilFastPath = 128;
static struct objc_sparsearray *fastPath = NULL;

static void
registerClass(struct objc_abi_class *rawClass)
{
	if (classes == NULL)
		classes = objc_hashtable_new(
		    objc_hash_string, objc_equal_string, 2);

	objc_hashtable_set(classes, rawClass->name, rawClass);

	if (emptyDTable == NULL)
		emptyDTable = objc_dtable_new();

	rawClass->DTable = emptyDTable;
	rawClass->metaclass->DTable = emptyDTable;

	if (strcmp(rawClass->name, "Protocol") != 0)
		classesCount++;
}

bool
class_registerAlias_np(Class class, const char *name)
{
	objc_global_mutex_lock();

	if (classes == NULL) {
		objc_global_mutex_unlock();

		return NO;
	}

	objc_hashtable_set(classes, name, (Class)((uintptr_t)class | 1));

	objc_global_mutex_unlock();

	return YES;
}

static void
registerSelectors(struct objc_abi_class *rawClass)
{
	struct objc_abi_method_list *methodList;

	for (methodList = rawClass->methodList; methodList != NULL;
	    methodList = methodList->next)
		for (unsigned int i = 0; i < methodList->count; i++)
			objc_register_selector((struct objc_abi_selector *)
			    &methodList->methods[i]);
}

Class
objc_classname_to_class(const char *name, bool cache)
{
	Class class;

	if (classes == NULL)
		return Nil;

	/*
	 * Fast path
	 *
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
	 * Runtime internal usage does not use the fast path and does not count
	 * as a call into objc_classname_to_class(). The reason for this is
	 * that if the runtime calls into objc_classname_to_class(), it already
	 * has the lock and thus the performance gain would be small, but it
	 * would waste memory.
	 */
	if (cache && fastPath != NULL) {
		cls = objc_sparsearray_get(fastPath, (uintptr_t)name);

		if (cls != Nil)
			return cls;
	}

	objc_global_mutex_lock();

	cls = (Class)((uintptr_t)objc_hashtable_get(classes, name) & ~1);

	if (cache && fastPath == NULL && --lookupsUntilFastPath == 0)
		fastPath = objc_sparsearray_new(sizeof(uintptr_t));

	if (cache && fastPath != NULL)
		objc_sparsearray_set(fastPath, (uintptr_t)name, cls);

	objc_global_mutex_unlock();

	return cls;
}

static void
callMethod(Class cls, const char *method)
{
	SEL selector = sel_registerName(method);

	for (struct objc_method_list *methodList = cls->isa->methodList;
	    methodList != NULL; methodList = methodList->next)
		for (unsigned int i = 0; i < methodList->count; i++)
			if (sel_isEqual((SEL)&methodList->methods[i].selector,
			    selector))
				((void (*)(id, SEL))methodList->methods[i]
				    .implementation)(cls, selector);
}

static bool
hasLoad(Class cls)
{
	SEL selector = sel_registerName("load");

	for (struct objc_method_list *methodList = cls->isa->methodList;
	    methodList != NULL; methodList = methodList->next)
		for (size_t i = 0; i < methodList->count; i++)
			if (sel_isEqual((SEL)&methodList->methods[i].selector,
			    selector))
				return true;

	return false;
}

static void
callLoad(Class cls)
{
	if (cls->info & OBJC_CLASS_INFO_LOADED)
		return;

	if (cls->superclass != Nil)
		callLoad(cls->superclass);

	callMethod(cls, "load");

	cls->info |= OBJC_CLASS_INFO_LOADED;
}

void
objc_update_dtable(Class cls)
{
	struct objc_category **categories;

	if (!(cls->info & OBJC_CLASS_INFO_DTABLE))
		return;

	if (cls->DTable == emptyDTable)
		cls->DTable = objc_dtable_new();

	if (cls->superclass != Nil)
		objc_dtable_copy(cls->DTable, cls->superclass->DTable);

	for (struct objc_method_list *methodList = cls->methodList;
	    methodList != NULL; methodList = methodList->next)
		for (unsigned int i = 0; i < methodList->count; i++)
			objc_dtable_set(cls->DTable,
			    (uint32_t)methodList->methods[i].selector.UID,
			    methodList->methods[i].implementation);

	if ((categories = objc_categories_for_class(cls)) != NULL) {
		for (unsigned int i = 0; categories[i] != NULL; i++) {
			struct objc_method_list *methodList =
			    (cls->info & OBJC_CLASS_INFO_CLASS
			    ? categories[i]->instanceMethods
			    : categories[i]->classMethods);

			for (; methodList != NULL;
			    methodList = methodList->next)
				for (unsigned int j = 0;
				    j < methodList->count; j++)
					objc_dtable_set(cls->DTable, (uint32_t)
					    methodList->methods[j].selector.UID,

					    methodList->methods[j]
					    .implementation);
		}
	}

	if (cls->subclassList != NULL)
		for (Class *iter = cls->subclassList; *iter != NULL; iter++)
			objc_update_dtable(*iter);
}

static void
addSubclass(Class cls)
{
	size_t i;

	if (cls->superclass->subclassList == NULL) {
		if ((cls->superclass->subclassList =
		    malloc(2 * sizeof(Class))) == NULL)
			OBJC_ERROR("Not enough memory for subclass list of "
			    "class %s!", cls->superclass->name);

		cls->superclass->subclassList[0] = cls;
		cls->superclass->subclassList[1] = Nil;

		return;
	}

	for (i = 0; cls->superclass->subclassList[i] != Nil; i++);

	cls->superclass->subclassList =
	    realloc(cls->superclass->subclassList, (i + 2) * sizeof(Class));

	if (cls->superclass->subclassList == NULL)
		OBJC_ERROR("Not enough memory for subclass list of class %s\n",
		    cls->superclass->name);

	cls->superclass->subclassList[i] = cls;
	cls->superclass->subclassList[i + 1] = Nil;
}


static void
updateIVarOffsets(Class cls)
{
	if (!(cls->info & OBJC_CLASS_INFO_NEW_ABI))
		return;

	if (cls->instanceSize > 0)
		return;

	cls->instanceSize = -cls->instanceSize;

	if (cls->superclass != Nil) {
		cls->instanceSize += cls->superclass->instanceSize;

		if (cls->iVars != NULL) {
			for (unsigned int i = 0; i < cls->iVars->count; i++) {
				cls->iVars->iVars[i].offset +=
				    cls->superclass->instanceSize;
				*cls->iVarOffsets[i] =
				    cls->iVars->iVars[i].offset;
			}
		}
	} else
		for (unsigned int i = 0; i < cls->iVars->count; i++)
			*cls->iVarOffsets[i] = cls->iVars->iVars[i].offset;
}

static void
setupClass(Class cls)
{
	const char *superclass;

	if (cls->info & OBJC_CLASS_INFO_SETUP)
		return;

	if ((superclass = ((struct objc_abi_class *)cls)->superclass) != NULL) {

		Class super = objc_classname_to_class(superclass, false);

		if (super == Nil)
			return;

		setupClass(super);

		if (!(super->info & OBJC_CLASS_INFO_SETUP))
			return;

		cls->superclass = super;
		cls->isa->superclass = super->isa;

		addSubclass(cls);
		addSubclass(cls->isa);
	} else
		cls->isa->superclass = cls;

	updateIVarOffsets(cls);

	cls->info |= OBJC_CLASS_INFO_SETUP;
	cls->isa->info |= OBJC_CLASS_INFO_SETUP;
}

static void
initializeClass(Class cls)
{
	if (cls->info & OBJC_CLASS_INFO_INITIALIZED)
		return;

	if (cls->superclass)
		initializeClass(cls->superclass);

	cls->info |= OBJC_CLASS_INFO_DTABLE;
	cls->isa->info |= OBJC_CLASS_INFO_DTABLE;

	objc_update_dtable(cls);
	objc_update_dtable(cls->isa);

	/*
	 * Set it first to prevent calling it recursively due to message sends
	 * in the initialize method
	 */
	cls->info |= OBJC_CLASS_INFO_INITIALIZED;
	cls->isa->info |= OBJC_CLASS_INFO_INITIALIZED;

	callMethod(cls, "initialize");
}

void
objc_initialize_class(Class cls)
{
	if (cls->info & OBJC_CLASS_INFO_INITIALIZED)
		return;

	objc_global_mutex_lock();

	/*
	 * It's possible that two threads try to initialize a class at the same
	 * time. Make sure that the thread which held the lock did not already
	 * initialize it.
	 */
	if (cls->info & OBJC_CLASS_INFO_INITIALIZED) {
		objc_global_mutex_unlock();
		return;
	}

	setupClass(cls);

	if (!(cls->info & OBJC_CLASS_INFO_SETUP)) {
		objc_global_mutex_unlock();
		return;
	}

	initializeClass(cls);

	objc_global_mutex_unlock();
}

static void
processLoadQueue()
{







|

|
|




|





|



|



|



|





|



|



|










|

|


|
|

|

|



|



|


|
|

|
|

|


|



|


|







|
|
>





|
|




|



|
|


|

|
|




|

|
|

|

|

|
|




|

|


|


|

|
|

|
|
|
|
|
|



|
|



|

|

|


|
>
|









|
|

|
|

|

|

|
|



|

|


|
|

|
|

|
|





|
|

|



|

|









|




|

|




|







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
	 * Runtime internal usage does not use the fast path and does not count
	 * as a call into objc_classname_to_class(). The reason for this is
	 * that if the runtime calls into objc_classname_to_class(), it already
	 * has the lock and thus the performance gain would be small, but it
	 * would waste memory.
	 */
	if (cache && fastPath != NULL) {
		class = objc_sparsearray_get(fastPath, (uintptr_t)name);

		if (class != Nil)
			return class;
	}

	objc_global_mutex_lock();

	class = (Class)((uintptr_t)objc_hashtable_get(classes, name) & ~1);

	if (cache && fastPath == NULL && --lookupsUntilFastPath == 0)
		fastPath = objc_sparsearray_new(sizeof(uintptr_t));

	if (cache && fastPath != NULL)
		objc_sparsearray_set(fastPath, (uintptr_t)name, class);

	objc_global_mutex_unlock();

	return class;
}

static void
callMethod(Class class, const char *method)
{
	SEL selector = sel_registerName(method);

	for (struct objc_method_list *methodList = class->isa->methodList;
	    methodList != NULL; methodList = methodList->next)
		for (unsigned int i = 0; i < methodList->count; i++)
			if (sel_isEqual((SEL)&methodList->methods[i].selector,
			    selector))
				((void (*)(id, SEL))methodList->methods[i]
				    .implementation)(class, selector);
}

static bool
hasLoad(Class class)
{
	SEL selector = sel_registerName("load");

	for (struct objc_method_list *methodList = class->isa->methodList;
	    methodList != NULL; methodList = methodList->next)
		for (size_t i = 0; i < methodList->count; i++)
			if (sel_isEqual((SEL)&methodList->methods[i].selector,
			    selector))
				return true;

	return false;
}

static void
callLoad(Class class)
{
	if (class->info & OBJC_CLASS_INFO_LOADED)
		return;

	if (class->superclass != Nil)
		callLoad(class->superclass);

	callMethod(class, "load");

	class->info |= OBJC_CLASS_INFO_LOADED;
}

void
objc_update_dtable(Class class)
{
	struct objc_category **categories;

	if (!(class->info & OBJC_CLASS_INFO_DTABLE))
		return;

	if (class->DTable == emptyDTable)
		class->DTable = objc_dtable_new();

	if (class->superclass != Nil)
		objc_dtable_copy(class->DTable, class->superclass->DTable);

	for (struct objc_method_list *methodList = class->methodList;
	    methodList != NULL; methodList = methodList->next)
		for (unsigned int i = 0; i < methodList->count; i++)
			objc_dtable_set(class->DTable,
			    (uint32_t)methodList->methods[i].selector.UID,
			    methodList->methods[i].implementation);

	if ((categories = objc_categories_for_class(class)) != NULL) {
		for (unsigned int i = 0; categories[i] != NULL; i++) {
			struct objc_method_list *methodList =
			    (class->info & OBJC_CLASS_INFO_CLASS
			    ? categories[i]->instanceMethods
			    : categories[i]->classMethods);

			for (; methodList != NULL;
			    methodList = methodList->next)
				for (unsigned int j = 0;
				    j < methodList->count; j++)
					objc_dtable_set(class->DTable,
					    (uint32_t)methodList->methods[j]
					    .selector.UID,
					    methodList->methods[j]
					    .implementation);
		}
	}

	if (class->subclassList != NULL)
		for (Class *iter = class->subclassList; *iter != NULL; iter++)
			objc_update_dtable(*iter);
}

static void
addSubclass(Class class)
{
	size_t i;

	if (class->superclass->subclassList == NULL) {
		if ((class->superclass->subclassList =
		    malloc(2 * sizeof(Class))) == NULL)
			OBJC_ERROR("Not enough memory for subclass list of "
			    "class %s!", class->superclass->name);

		class->superclass->subclassList[0] = class;
		class->superclass->subclassList[1] = Nil;

		return;
	}

	for (i = 0; class->superclass->subclassList[i] != Nil; i++);

	class->superclass->subclassList =
	    realloc(class->superclass->subclassList, (i + 2) * sizeof(Class));

	if (class->superclass->subclassList == NULL)
		OBJC_ERROR("Not enough memory for subclass list of class %s\n",
		    class->superclass->name);

	class->superclass->subclassList[i] = class;
	class->superclass->subclassList[i + 1] = Nil;
}


static void
updateIVarOffsets(Class class)
{
	if (!(class->info & OBJC_CLASS_INFO_NEW_ABI))
		return;

	if (class->instanceSize > 0)
		return;

	class->instanceSize = -class->instanceSize;

	if (class->superclass != Nil) {
		class->instanceSize += class->superclass->instanceSize;

		if (class->iVars != NULL) {
			for (unsigned int i = 0; i < class->iVars->count; i++) {
				class->iVars->iVars[i].offset +=
				    class->superclass->instanceSize;
				*class->iVarOffsets[i] =
				    class->iVars->iVars[i].offset;
			}
		}
	} else
		for (unsigned int i = 0; i < class->iVars->count; i++)
			*class->iVarOffsets[i] = class->iVars->iVars[i].offset;
}

static void
setupClass(Class class)
{
	const char *superclassName;

	if (class->info & OBJC_CLASS_INFO_SETUP)
		return;

	superclassName = ((struct objc_abi_class *)class)->superclass;
	if (superclassName != NULL) {
		Class super = objc_classname_to_class(superclassName, false);

		if (super == Nil)
			return;

		setupClass(super);

		if (!(super->info & OBJC_CLASS_INFO_SETUP))
			return;

		class->superclass = super;
		class->isa->superclass = super->isa;

		addSubclass(class);
		addSubclass(class->isa);
	} else
		class->isa->superclass = class;

	updateIVarOffsets(class);

	class->info |= OBJC_CLASS_INFO_SETUP;
	class->isa->info |= OBJC_CLASS_INFO_SETUP;
}

static void
initializeClass(Class class)
{
	if (class->info & OBJC_CLASS_INFO_INITIALIZED)
		return;

	if (class->superclass)
		initializeClass(class->superclass);

	class->info |= OBJC_CLASS_INFO_DTABLE;
	class->isa->info |= OBJC_CLASS_INFO_DTABLE;

	objc_update_dtable(class);
	objc_update_dtable(class->isa);

	/*
	 * Set it first to prevent calling it recursively due to message sends
	 * in the initialize method
	 */
	class->info |= OBJC_CLASS_INFO_INITIALIZED;
	class->isa->info |= OBJC_CLASS_INFO_INITIALIZED;

	callMethod(class, "initialize");
}

void
objc_initialize_class(Class class)
{
	if (class->info & OBJC_CLASS_INFO_INITIALIZED)
		return;

	objc_global_mutex_lock();

	/*
	 * It's possible that two threads try to initialize a class at the same
	 * time. Make sure that the thread which held the lock did not already
	 * initialize it.
	 */
	if (class->info & OBJC_CLASS_INFO_INITIALIZED) {
		objc_global_mutex_unlock();
		return;
	}

	setupClass(class);

	if (!(class->info & OBJC_CLASS_INFO_SETUP)) {
		objc_global_mutex_unlock();
		return;
	}

	initializeClass(class);

	objc_global_mutex_unlock();
}

static void
processLoadQueue()
{
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
	}
}

void
objc_register_all_classes(struct objc_abi_symtab *symtab)
{
	for (uint16_t i = 0; i < symtab->classDefsCount; i++) {
		struct objc_abi_class *cls =
		    (struct objc_abi_class *)symtab->defs[i];

		registerClass(cls);
		registerSelectors(cls);
		registerSelectors(cls->metaclass);
	}

	for (uint16_t i = 0; i < symtab->classDefsCount; i++) {
		Class cls = (Class)symtab->defs[i];

		if (hasLoad(cls)) {
			setupClass(cls);

			if (cls->info & OBJC_CLASS_INFO_SETUP)
				callLoad(cls);
			else {
				loadQueue = realloc(loadQueue,
				    sizeof(Class) * (loadQueueCount + 1));

				if (loadQueue == NULL)
					OBJC_ERROR("Not enough memory for load "
					    "queue!");

				loadQueue[loadQueueCount++] = cls;
			}
		} else
			cls->info |= OBJC_CLASS_INFO_LOADED;
	}

	processLoadQueue();
}

Class
objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes)
{
	struct objc_class *cls, *metaclass;
	Class iter, rootclass = Nil;

	if (extraBytes > LONG_MAX)
		OBJC_ERROR("extra_bytes out of range!")

	if ((cls = calloc(1, sizeof(*cls))) == NULL ||
	    (metaclass = calloc(1, sizeof(*cls))) == NULL)
		OBJC_ERROR("Not enough memory to allocate class pair for class "
		    "%s!", name)

	cls->isa = metaclass;
	cls->superclass = superclass;
	cls->name = name;
	cls->info = OBJC_CLASS_INFO_CLASS;
	cls->instanceSize = (superclass != Nil ?
	    superclass->instanceSize : 0) + (long)extraBytes;

	for (iter = superclass; iter != Nil; iter = iter->superclass)
		rootclass = iter;

	metaclass->isa = (rootclass != Nil ? rootclass->isa : cls);
	metaclass->superclass = (superclass != Nil ? superclass->isa : Nil);
	metaclass->name = name;
	metaclass->info = OBJC_CLASS_INFO_CLASS;
	metaclass->instanceSize = (superclass != Nil ?
	    superclass->isa->instanceSize : 0) + (long)extraBytes;

	return cls;
}

void
objc_registerClassPair(Class cls)
{
	objc_global_mutex_lock();

	registerClass((struct objc_abi_class *)cls);

	if (cls->superclass != Nil) {
		addSubclass(cls);
		addSubclass(cls->isa);
	}

	cls->info |= OBJC_CLASS_INFO_SETUP;
	cls->isa->info |= OBJC_CLASS_INFO_SETUP;

	if (hasLoad(cls))
		callLoad(cls);
	else
		cls->info |= OBJC_CLASS_INFO_LOADED;

	processLoadQueue();

	objc_global_mutex_unlock();
}

Class
objc_lookUpClass(const char *name)
{
	Class cls;

	if ((cls = objc_classname_to_class(name, true)) == NULL)
		return Nil;

	if (cls->info & OBJC_CLASS_INFO_SETUP)
		return cls;

	objc_global_mutex_lock();

	setupClass(cls);

	objc_global_mutex_unlock();

	if (!(cls->info & OBJC_CLASS_INFO_SETUP))
		return Nil;

	return cls;
}

Class
objc_getClass(const char *name)
{
	return objc_lookUpClass(name);
}

Class
objc_getRequiredClass(const char *name)
{
	Class cls;

	if ((cls = objc_getClass(name)) == Nil)
		OBJC_ERROR("Class %s not found!", name);

	return cls;
}

Class
objc_lookup_class(const char *name)
{
	return objc_getClass(name);
}







|


|
|
|



|

|
|

|
|








|


|








|





|
|



|
|
|
|
|





|






|



|



|

|
|
|


|
|

|
|

|









|

|


|
|



|



|


|











|

|


|







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
	}
}

void
objc_register_all_classes(struct objc_abi_symtab *symtab)
{
	for (uint16_t i = 0; i < symtab->classDefsCount; i++) {
		struct objc_abi_class *rawClass =
		    (struct objc_abi_class *)symtab->defs[i];

		registerClass(rawClass);
		registerSelectors(rawClass);
		registerSelectors(rawClass->metaclass);
	}

	for (uint16_t i = 0; i < symtab->classDefsCount; i++) {
		Class class = (Class)symtab->defs[i];

		if (hasLoad(class)) {
			setupClass(class);

			if (class->info & OBJC_CLASS_INFO_SETUP)
				callLoad(class);
			else {
				loadQueue = realloc(loadQueue,
				    sizeof(Class) * (loadQueueCount + 1));

				if (loadQueue == NULL)
					OBJC_ERROR("Not enough memory for load "
					    "queue!");

				loadQueue[loadQueueCount++] = class;
			}
		} else
			class->info |= OBJC_CLASS_INFO_LOADED;
	}

	processLoadQueue();
}

Class
objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes)
{
	struct objc_class *class, *metaclass;
	Class iter, rootclass = Nil;

	if (extraBytes > LONG_MAX)
		OBJC_ERROR("extra_bytes out of range!")

	if ((class = calloc(1, sizeof(*class))) == NULL ||
	    (metaclass = calloc(1, sizeof(*class))) == NULL)
		OBJC_ERROR("Not enough memory to allocate class pair for class "
		    "%s!", name)

	class->isa = metaclass;
	class->superclass = superclass;
	class->name = name;
	class->info = OBJC_CLASS_INFO_CLASS;
	class->instanceSize = (superclass != Nil ?
	    superclass->instanceSize : 0) + (long)extraBytes;

	for (iter = superclass; iter != Nil; iter = iter->superclass)
		rootclass = iter;

	metaclass->isa = (rootclass != Nil ? rootclass->isa : class);
	metaclass->superclass = (superclass != Nil ? superclass->isa : Nil);
	metaclass->name = name;
	metaclass->info = OBJC_CLASS_INFO_CLASS;
	metaclass->instanceSize = (superclass != Nil ?
	    superclass->isa->instanceSize : 0) + (long)extraBytes;

	return class;
}

void
objc_registerClassPair(Class class)
{
	objc_global_mutex_lock();

	registerClass((struct objc_abi_class *)class);

	if (class->superclass != Nil) {
		addSubclass(class);
		addSubclass(class->isa);
	}

	class->info |= OBJC_CLASS_INFO_SETUP;
	class->isa->info |= OBJC_CLASS_INFO_SETUP;

	if (hasLoad(class))
		callLoad(class);
	else
		class->info |= OBJC_CLASS_INFO_LOADED;

	processLoadQueue();

	objc_global_mutex_unlock();
}

Class
objc_lookUpClass(const char *name)
{
	Class class;

	if ((class = objc_classname_to_class(name, true)) == NULL)
		return Nil;

	if (class->info & OBJC_CLASS_INFO_SETUP)
		return class;

	objc_global_mutex_lock();

	setupClass(class);

	objc_global_mutex_unlock();

	if (!(class->info & OBJC_CLASS_INFO_SETUP))
		return Nil;

	return class;
}

Class
objc_getClass(const char *name)
{
	return objc_lookUpClass(name);
}

Class
objc_getRequiredClass(const char *name)
{
	Class class;

	if ((class = objc_getClass(name)) == Nil)
		OBJC_ERROR("Class %s not found!", name);

	return class;
}

Class
objc_lookup_class(const char *name)
{
	return objc_getClass(name);
}
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721

722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
		return classesCount;

	if (classesCount < count)
		count = classesCount;

	j = 0;
	for (uint32_t i = 0; i < classes->size; i++) {
		void *cls;

		if (j >= count) {
			objc_global_mutex_unlock();
			return j;
		}

		if (classes->data[i] == NULL)
			continue;

		if (strcmp(classes->data[i]->key, "Protocol") == 0)
			continue;

		cls = (Class)classes->data[i]->object;

		if (cls == Nil || (uintptr_t)cls & 1)
			continue;

		buffer[j++] = cls;
	}

	objc_global_mutex_unlock();

	return j;
}

Class *
objc_copyClassList(unsigned int *len)
{
	Class *ret;
	unsigned int count;

	objc_global_mutex_lock();

	if ((ret = malloc((classesCount + 1) * sizeof(Class))) == NULL)
		OBJC_ERROR("Failed to allocate memory for class list!");

	count = objc_getClassList(ret, classesCount);
	OF_ENSURE(count == classesCount);

	ret[count] = Nil;

	if (len != NULL)
		*len = count;

	objc_global_mutex_unlock();

	return ret;
}

bool
class_isMetaClass(Class cls)
{
	if (cls == Nil)
		return false;

	return (cls->info & OBJC_CLASS_INFO_METACLASS);
}

const char *
class_getName(Class cls)
{
	if (cls == Nil)
		return "";

	return cls->name;
}

Class
class_getSuperclass(Class cls)
{
	if (cls == Nil)
		return Nil;

	return cls->superclass;
}

unsigned long
class_getInstanceSize(Class cls)
{
	if (cls == Nil)
		return 0;

	return cls->instanceSize;
}

IMP
class_getMethodImplementation(Class cls, SEL selector)
{
	/*
	 * We use a dummy object here so that the normal lookup is used, even
	 * though we don't have an object. Doing so is safe, as objc_msg_lookup
	 * does not access the object, but only its class.
	 *
	 * Just looking it up in the dispatch table could result in returning
	 * NULL instead of the forwarding handler, it would also mean
	 * +[resolveClassMethod:] / +[resolveInstanceMethod:] would not be
	 * called.
	 */
	struct {
		Class isa;
	} dummy;

	if (cls == Nil)
		return NULL;

	dummy.isa = cls;
	return objc_msg_lookup((id)&dummy, selector);
}

IMP
class_getMethodImplementation_stret(Class cls, SEL selector)
{
	/*
	 * Same as above, but use objc_msg_lookup_stret instead, so that the
	 * correct forwarding handler is returned.
	 */
	struct {
		Class isa;
	} dummy;

	if (cls == Nil)
		return NULL;

	dummy.isa = cls;
	return objc_msg_lookup_stret((id)&dummy, selector);
}

static struct objc_method *
getMethod(Class cls, SEL selector)
{
	struct objc_category **categories;

	if ((categories = objc_categories_for_class(cls)) != NULL) {
		for (; *categories != NULL; categories++) {
			struct objc_method_list *methodList =
			    (cls->info & OBJC_CLASS_INFO_METACLASS
			    ? (*categories)->classMethods
			    : (*categories)->instanceMethods);

			for (; methodList != NULL;
			    methodList = methodList->next)
				for (unsigned int i = 0;
				    i < methodList->count; i++)
					if (sel_isEqual((SEL)
					    &methodList->methods[i].selector,
					    selector))
						return &methodList->methods[i];
		}
	}

	for (struct objc_method_list *methodList = cls->methodList;
	    methodList != NULL; methodList = methodList->next)
		for (unsigned int i = 0; i < methodList->count; i++)
			if (sel_isEqual((SEL)&methodList->methods[i].selector,
			    selector))
				return &methodList->methods[i];

	return NULL;
}

static void
addMethod(Class cls, SEL selector, IMP implementation, const char *typeEncoding)

{
	struct objc_method_list *methodList;

	/* FIXME: We need a way to free this at objc_exit() */
	if ((methodList = malloc(sizeof(struct objc_method_list))) == NULL)
		OBJC_ERROR("Not enough memory to replace method!");

	methodList->next = cls->methodList;
	methodList->count = 1;
	methodList->methods[0].selector.UID = selector->UID;
	methodList->methods[0].selector.typeEncoding = typeEncoding;
	methodList->methods[0].implementation = implementation;

	cls->methodList = methodList;

	objc_update_dtable(cls);
}

const char *
class_getMethodTypeEncoding(Class cls, SEL selector)
{
	struct objc_method *method;

	if (cls == Nil)
		return NULL;

	objc_global_mutex_lock();

	if ((method = getMethod(cls, selector)) != NULL) {
		const char *ret = method->selector.typeEncoding;
		objc_global_mutex_unlock();
		return ret;
	}

	objc_global_mutex_unlock();

	if (cls->superclass != Nil)
		return class_getMethodTypeEncoding(cls->superclass, selector);

	return NULL;
}

bool
class_addMethod(Class cls, SEL selector, IMP implementation,
    const char *typeEncoding)
{
	bool ret;

	objc_global_mutex_lock();

	if (getMethod(cls, selector) == NULL) {
		addMethod(cls, selector, implementation, typeEncoding);
		ret = true;
	} else
		ret = false;

	objc_global_mutex_unlock();

	return ret;
}

IMP
class_replaceMethod(Class cls, SEL selector, IMP implementation,
    const char *typeEncoding)
{
	struct objc_method *method;
	IMP oldImplementation;

	objc_global_mutex_lock();

	if ((method = getMethod(cls, selector)) != NULL) {
		oldImplementation = method->implementation;
		method->implementation = implementation;
		objc_update_dtable(cls);
	} else {
		oldImplementation = NULL;
		addMethod(cls, selector, implementation, typeEncoding);
	}

	objc_global_mutex_unlock();

	return oldImplementation;
}








|












|

|


|








|














|
|







|

|


|



|

|


|



|

|


|



|

|


|



|















|


|




|









|


|




|



|


|














|










|
>







|





|

|



|



|




|







|
|





|






|
|










|







|


|


|







554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
		return classesCount;

	if (classesCount < count)
		count = classesCount;

	j = 0;
	for (uint32_t i = 0; i < classes->size; i++) {
		void *class;

		if (j >= count) {
			objc_global_mutex_unlock();
			return j;
		}

		if (classes->data[i] == NULL)
			continue;

		if (strcmp(classes->data[i]->key, "Protocol") == 0)
			continue;

		class = (Class)classes->data[i]->object;

		if (class == Nil || (uintptr_t)class & 1)
			continue;

		buffer[j++] = class;
	}

	objc_global_mutex_unlock();

	return j;
}

Class *
objc_copyClassList(unsigned int *length)
{
	Class *ret;
	unsigned int count;

	objc_global_mutex_lock();

	if ((ret = malloc((classesCount + 1) * sizeof(Class))) == NULL)
		OBJC_ERROR("Failed to allocate memory for class list!");

	count = objc_getClassList(ret, classesCount);
	OF_ENSURE(count == classesCount);

	ret[count] = Nil;

	if (length != NULL)
		*length = count;

	objc_global_mutex_unlock();

	return ret;
}

bool
class_isMetaClass(Class class)
{
	if (class == Nil)
		return false;

	return (class->info & OBJC_CLASS_INFO_METACLASS);
}

const char *
class_getName(Class class)
{
	if (class == Nil)
		return "";

	return class->name;
}

Class
class_getSuperclass(Class class)
{
	if (class == Nil)
		return Nil;

	return class->superclass;
}

unsigned long
class_getInstanceSize(Class class)
{
	if (class == Nil)
		return 0;

	return class->instanceSize;
}

IMP
class_getMethodImplementation(Class class, SEL selector)
{
	/*
	 * We use a dummy object here so that the normal lookup is used, even
	 * though we don't have an object. Doing so is safe, as objc_msg_lookup
	 * does not access the object, but only its class.
	 *
	 * Just looking it up in the dispatch table could result in returning
	 * NULL instead of the forwarding handler, it would also mean
	 * +[resolveClassMethod:] / +[resolveInstanceMethod:] would not be
	 * called.
	 */
	struct {
		Class isa;
	} dummy;

	if (class == Nil)
		return NULL;

	dummy.isa = class;
	return objc_msg_lookup((id)&dummy, selector);
}

IMP
class_getMethodImplementation_stret(Class class, SEL selector)
{
	/*
	 * Same as above, but use objc_msg_lookup_stret instead, so that the
	 * correct forwarding handler is returned.
	 */
	struct {
		Class isa;
	} dummy;

	if (class == Nil)
		return NULL;

	dummy.isa = class;
	return objc_msg_lookup_stret((id)&dummy, selector);
}

static struct objc_method *
getMethod(Class class, SEL selector)
{
	struct objc_category **categories;

	if ((categories = objc_categories_for_class(class)) != NULL) {
		for (; *categories != NULL; categories++) {
			struct objc_method_list *methodList =
			    (class->info & OBJC_CLASS_INFO_METACLASS
			    ? (*categories)->classMethods
			    : (*categories)->instanceMethods);

			for (; methodList != NULL;
			    methodList = methodList->next)
				for (unsigned int i = 0;
				    i < methodList->count; i++)
					if (sel_isEqual((SEL)
					    &methodList->methods[i].selector,
					    selector))
						return &methodList->methods[i];
		}
	}

	for (struct objc_method_list *methodList = class->methodList;
	    methodList != NULL; methodList = methodList->next)
		for (unsigned int i = 0; i < methodList->count; i++)
			if (sel_isEqual((SEL)&methodList->methods[i].selector,
			    selector))
				return &methodList->methods[i];

	return NULL;
}

static void
addMethod(Class class, SEL selector, IMP implementation,
    const char *typeEncoding)
{
	struct objc_method_list *methodList;

	/* FIXME: We need a way to free this at objc_exit() */
	if ((methodList = malloc(sizeof(struct objc_method_list))) == NULL)
		OBJC_ERROR("Not enough memory to replace method!");

	methodList->next = class->methodList;
	methodList->count = 1;
	methodList->methods[0].selector.UID = selector->UID;
	methodList->methods[0].selector.typeEncoding = typeEncoding;
	methodList->methods[0].implementation = implementation;

	class->methodList = methodList;

	objc_update_dtable(class);
}

const char *
class_getMethodTypeEncoding(Class class, SEL selector)
{
	struct objc_method *method;

	if (class == Nil)
		return NULL;

	objc_global_mutex_lock();

	if ((method = getMethod(class, selector)) != NULL) {
		const char *ret = method->selector.typeEncoding;
		objc_global_mutex_unlock();
		return ret;
	}

	objc_global_mutex_unlock();

	if (class->superclass != Nil)
		return class_getMethodTypeEncoding(class->superclass, selector);

	return NULL;
}

bool
class_addMethod(Class class, SEL selector, IMP implementation,
    const char *typeEncoding)
{
	bool ret;

	objc_global_mutex_lock();

	if (getMethod(class, selector) == NULL) {
		addMethod(class, selector, implementation, typeEncoding);
		ret = true;
	} else
		ret = false;

	objc_global_mutex_unlock();

	return ret;
}

IMP
class_replaceMethod(Class class, SEL selector, IMP implementation,
    const char *typeEncoding)
{
	struct objc_method *method;
	IMP oldImplementation;

	objc_global_mutex_lock();

	if ((method = getMethod(class, selector)) != NULL) {
		oldImplementation = method->implementation;
		method->implementation = implementation;
		objc_update_dtable(class);
	} else {
		oldImplementation = NULL;
		addMethod(class, selector, implementation, typeEncoding);
	}

	objc_global_mutex_unlock();

	return oldImplementation;
}

813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926

	object = (struct objc_object *)object_;

	return object->isa;
}

Class
object_setClass(id object_, Class cls)
{
	struct objc_object *object;
	Class old;

	if (object_ == nil)
		return Nil;

	object = (struct objc_object *)object_;

	old = object->isa;
	object->isa = cls;

	return old;
}

const char *
object_getClassName(id obj)
{
	return class_getName(object_getClass(obj));
}

static void
unregisterClass(Class rcls)
{
	struct objc_abi_class *cls = (struct objc_abi_class *)rcls;

	if ((rcls->info & OBJC_CLASS_INFO_SETUP) && rcls->superclass != Nil &&
	    rcls->superclass->subclassList != NULL) {
		size_t i = SIZE_MAX, count = 0;
		Class *tmp;

		for (tmp = rcls->superclass->subclassList;
		    *tmp != Nil; tmp++) {
			if (*tmp == rcls)
				i = count;

			count++;
		}

		if (count > 0 && i < SIZE_MAX) {
			tmp = rcls->superclass->subclassList;
			tmp[i] = tmp[count - 1];
			tmp[count - 1] = NULL;

			if ((tmp = realloc(rcls->superclass->subclassList,
			    count * sizeof(Class))) != NULL)
				rcls->superclass->subclassList = tmp;
		}
	}

	if (rcls->subclassList != NULL) {
		free(rcls->subclassList);
		rcls->subclassList = NULL;
	}

	if (rcls->DTable != NULL && rcls->DTable != emptyDTable)
		objc_dtable_free(rcls->DTable);

	rcls->DTable = NULL;

	if ((rcls->info & OBJC_CLASS_INFO_SETUP) && rcls->superclass != Nil)
		cls->superclass = rcls->superclass->name;

	rcls->info &= ~OBJC_CLASS_INFO_SETUP;
}

void
objc_unregister_class(Class cls)
{
	while (cls->subclassList != NULL && cls->subclassList[0] != Nil)
		objc_unregister_class(cls->subclassList[0]);

	if (cls->info & OBJC_CLASS_INFO_LOADED)
		callMethod(cls, "unload");

	objc_hashtable_delete(classes, cls->name);

	if (strcmp(class_getName(cls), "Protocol") != 0)
		classesCount--;

	unregisterClass(cls);
	unregisterClass(cls->isa);
}

void
objc_unregister_all_classes(void)
{
	if (classes == NULL)
		return;

	for (uint32_t i = 0; i < classes->size; i++) {
		if (classes->data[i] != NULL &&
		    classes->data[i] != &objc_deleted_bucket) {
			void *cls = (Class)classes->data[i]->object;

			if (cls == Nil || (uintptr_t)cls & 1)
				continue;

			objc_unregister_class(cls);

			/*
			 * The table might have been resized, so go back to the
			 * start again.
			 *
			 * Due to the i++ in the for loop, we need to set it to
			 * UINT32_MAX so that it will get increased at the end







|










|





|

|



|

|

|
|



|

|






|



|

|



|
|
|


|
|

|

|
|

|



|

|
|

|
|

|

|


|
|











|

|


|







816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929

	object = (struct objc_object *)object_;

	return object->isa;
}

Class
object_setClass(id object_, Class class)
{
	struct objc_object *object;
	Class old;

	if (object_ == nil)
		return Nil;

	object = (struct objc_object *)object_;

	old = object->isa;
	object->isa = class;

	return old;
}

const char *
object_getClassName(id object)
{
	return class_getName(object_getClass(object));
}

static void
unregisterClass(Class class)
{
	struct objc_abi_class *rawClass = (struct objc_abi_class *)class;

	if ((class->info & OBJC_CLASS_INFO_SETUP) && class->superclass != Nil &&
	    class->superclass->subclassList != NULL) {
		size_t i = SIZE_MAX, count = 0;
		Class *tmp;

		for (tmp = class->superclass->subclassList;
		    *tmp != Nil; tmp++) {
			if (*tmp == class)
				i = count;

			count++;
		}

		if (count > 0 && i < SIZE_MAX) {
			tmp = class->superclass->subclassList;
			tmp[i] = tmp[count - 1];
			tmp[count - 1] = NULL;

			if ((tmp = realloc(class->superclass->subclassList,
			    count * sizeof(Class))) != NULL)
				class->superclass->subclassList = tmp;
		}
	}

	if (class->subclassList != NULL) {
		free(class->subclassList);
		class->subclassList = NULL;
	}

	if (class->DTable != NULL && class->DTable != emptyDTable)
		objc_dtable_free(class->DTable);

	class->DTable = NULL;

	if ((class->info & OBJC_CLASS_INFO_SETUP) && class->superclass != Nil)
		rawClass->superclass = class->superclass->name;

	class->info &= ~OBJC_CLASS_INFO_SETUP;
}

void
objc_unregister_class(Class class)
{
	while (class->subclassList != NULL && class->subclassList[0] != Nil)
		objc_unregister_class(class->subclassList[0]);

	if (class->info & OBJC_CLASS_INFO_LOADED)
		callMethod(class, "unload");

	objc_hashtable_delete(classes, class->name);

	if (strcmp(class_getName(class), "Protocol") != 0)
		classesCount--;

	unregisterClass(class);
	unregisterClass(class->isa);
}

void
objc_unregister_all_classes(void)
{
	if (classes == NULL)
		return;

	for (uint32_t i = 0; i < classes->size; i++) {
		if (classes->data[i] != NULL &&
		    classes->data[i] != &objc_deleted_bucket) {
			void *class = (Class)classes->data[i]->object;

			if (class == Nil || (uintptr_t)class & 1)
				continue;

			objc_unregister_class(class);

			/*
			 * The table might have been resized, so go back to the
			 * start again.
			 *
			 * Due to the i++ in the for loop, we need to set it to
			 * UINT32_MAX so that it will get increased at the end

Modified src/runtime/dtable.m from [1fc547de44] to [f7a78b92b5].

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
	for (uint_fast16_t i = 0; i < 256; i++)
		DTable->buckets[i] = emptyLevel2;

	return DTable;
}

void
objc_dtable_copy(struct objc_dtable *dst, struct objc_dtable *src)
{
	for (uint_fast16_t i = 0; i < 256; i++) {
		if (src->buckets[i] == emptyLevel2)
			continue;

#ifdef OF_SELUID24
		for (uint_fast16_t j = 0; j < 256; j++) {
			if (src->buckets[i]->buckets[j] == emptyLevel3)
				continue;

			for (uint_fast16_t k = 0; k < 256; k++) {
				IMP obj;
				uint32_t idx;


				obj = src->buckets[i]->buckets[j]->buckets[k];

				if (obj == (IMP)0)
					continue;

				idx = (uint32_t)
				    (((uint32_t)i << 16) | (j << 8) | k);
				objc_dtable_set(dst, idx, obj);
			}
		}
#else
		for (uint_fast16_t j = 0; j < 256; j++) {
			IMP obj = src->buckets[i]->buckets[j];
			uint32_t idx;

			if (obj == (IMP)0)
				continue;

			idx = (uint32_t)((i << 8) | j);
			objc_dtable_set(dst, idx, obj);
		}
#endif
	}
}

void
objc_dtable_set(struct objc_dtable *dtable, uint32_t idx, IMP obj)
{
#ifdef OF_SELUID24
	uint8_t i = idx >> 16;
	uint8_t j = idx >> 8;
	uint8_t k = idx;
#else
	uint8_t i = idx >> 8;
	uint8_t j = idx;
#endif

	if (dtable->buckets[i] == emptyLevel2) {
		struct objc_dtable_level2 *level2 =
		    malloc(sizeof(struct objc_dtable_level2));

		if (level2 == NULL)
			OBJC_ERROR("Not enough memory to insert into dtable!");

		for (uint_fast16_t l = 0; l < 256; l++)
#ifdef OF_SELUID24
			level2->buckets[l] = emptyLevel3;
#else
			level2->buckets[l] = (IMP)0;
#endif

		dtable->buckets[i] = level2;
	}

#ifdef OF_SELUID24
	if (dtable->buckets[i]->buckets[j] == emptyLevel3) {
		struct objc_dtable_level3 *level3 =
		    malloc(sizeof(struct objc_dtable_level3));

		if (level3 == NULL)
			OBJC_ERROR("Not enough memory to insert into dtable!");

		for (uint_fast16_t l = 0; l < 256; l++)
			level3->buckets[l] = (IMP)0;

		dtable->buckets[i]->buckets[j] = level3;
	}

	dtable->buckets[i]->buckets[j]->buckets[k] = obj;
#else
	dtable->buckets[i]->buckets[j] = obj;
#endif
}

void
objc_dtable_free(struct objc_dtable *dtable)
{
	for (uint_fast16_t i = 0; i < 256; i++) {
		if (dtable->buckets[i] == emptyLevel2)
			continue;

#ifdef OF_SELUID24
		for (uint_fast16_t j = 0; j < 256; j++)
			if (dtable->buckets[i]->buckets[j] != emptyLevel3)
				free(dtable->buckets[i]->buckets[j]);
#endif

		free(dtable->buckets[i]);
	}

	free(dtable);
}

void
objc_dtable_cleanup(void)
{
	if (emptyLevel2 != NULL)
		free(emptyLevel2);







|











|


>
|

|




|




|


|



|






|










|













|



|









|


|

|




|


|




|
|


|


|







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
	for (uint_fast16_t i = 0; i < 256; i++)
		DTable->buckets[i] = emptyLevel2;

	return DTable;
}

void
objc_dtable_copy(struct objc_dtable *dest, struct objc_dtable *src)
{
	for (uint_fast16_t i = 0; i < 256; i++) {
		if (src->buckets[i] == emptyLevel2)
			continue;

#ifdef OF_SELUID24
		for (uint_fast16_t j = 0; j < 256; j++) {
			if (src->buckets[i]->buckets[j] == emptyLevel3)
				continue;

			for (uint_fast16_t k = 0; k < 256; k++) {
				IMP implementation;
				uint32_t idx;

				implementation =
				    src->buckets[i]->buckets[j]->buckets[k];

				if (implementation == (IMP)0)
					continue;

				idx = (uint32_t)
				    (((uint32_t)i << 16) | (j << 8) | k);
				objc_dtable_set(dest, idx, implementation);
			}
		}
#else
		for (uint_fast16_t j = 0; j < 256; j++) {
			IMP implementation = src->buckets[i]->buckets[j];
			uint32_t idx;

			if (implementation == (IMP)0)
				continue;

			idx = (uint32_t)((i << 8) | j);
			objc_dtable_set(dest, idx, implementation);
		}
#endif
	}
}

void
objc_dtable_set(struct objc_dtable *DTable, uint32_t idx, IMP implementation)
{
#ifdef OF_SELUID24
	uint8_t i = idx >> 16;
	uint8_t j = idx >> 8;
	uint8_t k = idx;
#else
	uint8_t i = idx >> 8;
	uint8_t j = idx;
#endif

	if (DTable->buckets[i] == emptyLevel2) {
		struct objc_dtable_level2 *level2 =
		    malloc(sizeof(struct objc_dtable_level2));

		if (level2 == NULL)
			OBJC_ERROR("Not enough memory to insert into dtable!");

		for (uint_fast16_t l = 0; l < 256; l++)
#ifdef OF_SELUID24
			level2->buckets[l] = emptyLevel3;
#else
			level2->buckets[l] = (IMP)0;
#endif

		DTable->buckets[i] = level2;
	}

#ifdef OF_SELUID24
	if (DTable->buckets[i]->buckets[j] == emptyLevel3) {
		struct objc_dtable_level3 *level3 =
		    malloc(sizeof(struct objc_dtable_level3));

		if (level3 == NULL)
			OBJC_ERROR("Not enough memory to insert into dtable!");

		for (uint_fast16_t l = 0; l < 256; l++)
			level3->buckets[l] = (IMP)0;

		DTable->buckets[i]->buckets[j] = level3;
	}

	DTable->buckets[i]->buckets[j]->buckets[k] = implementation;
#else
	DTable->buckets[i]->buckets[j] = implementation;
#endif
}

void
objc_dtable_free(struct objc_dtable *DTable)
{
	for (uint_fast16_t i = 0; i < 256; i++) {
		if (DTable->buckets[i] == emptyLevel2)
			continue;

#ifdef OF_SELUID24
		for (uint_fast16_t j = 0; j < 256; j++)
			if (DTable->buckets[i]->buckets[j] != emptyLevel3)
				free(DTable->buckets[i]->buckets[j]);
#endif

		free(DTable->buckets[i]);
	}

	free(DTable);
}

void
objc_dtable_cleanup(void)
{
	if (emptyLevel2 != NULL)
		free(emptyLevel2);

Modified src/runtime/exception.m from [3d1d7d2d6b] to [9119c3ea09].

460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
		}
	}

	return false;
#else
	uintptr_t callsiteLandingpad, callsiteAction;

	if ((intptr_t)ip < 1)
		return false;

	do {
		callsiteLandingpad = (uintptr_t)readULEB128(&ptr);
		callsiteAction = (uintptr_t)readULEB128(&ptr);
	} while (--ip > 1);

	*landingpad = callsiteLandingpad + 1;
	if (callsiteAction != 0)
		*actionRecords = LSDA->actionTable + callsiteAction - 1;

	return true;
#endif







|





|







460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
		}
	}

	return false;
#else
	uintptr_t callsiteLandingpad, callsiteAction;

	if ((intptr_t)IP < 1)
		return false;

	do {
		callsiteLandingpad = (uintptr_t)readULEB128(&ptr);
		callsiteAction = (uintptr_t)readULEB128(&ptr);
	} while (--IP > 1);

	*landingpad = callsiteLandingpad + 1;
	if (callsiteAction != 0)
		*actionRecords = LSDA->actionTable + callsiteAction - 1;

	return true;
#endif

Modified src/runtime/hashtable.m from [e12d752357] to [d1e5e6d9a8].

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
	hash ^= (hash >> 11);
	hash += (hash << 15);

	return hash;
}

bool
objc_equal_string(const void *obj1, const void *obj2)
{
	return (strcmp(obj1, obj2) == 0);
}

struct objc_hashtable *
objc_hashtable_new(uint32_t (*hash)(const void *),
    bool (*equal)(const void *, const void *), uint32_t size)
{
	struct objc_hashtable *table;







|

|







45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
	hash ^= (hash >> 11);
	hash += (hash << 15);

	return hash;
}

bool
objc_equal_string(const void *ptr1, const void *ptr2)
{
	return (strcmp(ptr1, ptr2) == 0);
}

struct objc_hashtable *
objc_hashtable_new(uint32_t (*hash)(const void *),
    bool (*equal)(const void *, const void *), uint32_t size)
{
	struct objc_hashtable *table;

Modified src/runtime/linklib/linklib.m from [f03a29b448] to [964c1adbfe].

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
Class
objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes)
{
	return objc_allocateClassPair_m68k(superclass, name, extraBytes);
}

void
objc_registerClassPair(Class cls)
{
	objc_registerClassPair_m68k(cls);
}

unsigned int
objc_getClassList(Class *buffer, unsigned int count)
{
	return objc_getClassList_m68k(buffer, count);
}

Class *
objc_copyClassList(unsigned int *length)
{
	return objc_copyClassList_m68k(length);
}

bool
class_isMetaClass(Class cls)
{
	return class_isMetaClass_m68k(cls);
}

const char *
class_getName(Class cls)
{
	return class_getName_m68k(cls);
}

Class
class_getSuperclass(Class cls)
{
	return class_getSuperclass_m68k(cls);
}

unsigned long
class_getInstanceSize(Class cls)
{
	return class_getInstanceSize_m68k(cls);
}

bool
class_respondsToSelector(Class cls, SEL selector)
{
	return class_respondsToSelector_m68k(cls, selector);
}

bool
class_conformsToProtocol(Class cls, Protocol *protocol)
{
	return class_conformsToProtocol_m68k(cls, protocol);
}

IMP
class_getMethodImplementation(Class cls, SEL selector)
{
	return class_getMethodImplementation_m68k(cls, selector);
}

IMP
class_getMethodImplementation_stret(Class cls, SEL selector)
{
	return class_getMethodImplementation_stret_m68k(cls, selector);
}

const char *
class_getMethodTypeEncoding(Class cls, SEL selector)
{
	return class_getMethodTypeEncoding_m68k(cls, selector);
}

bool
class_addMethod(Class cls, SEL selector, IMP implementation,
    const char *typeEncoding)
{
	return class_addMethod_m68k(cls, selector, implementation,
	    typeEncoding);
}

IMP
class_replaceMethod(Class cls, SEL selector, IMP implementation,
    const char *typeEncoding)
{
	return class_replaceMethod_m68k(cls, selector, implementation,
	    typeEncoding);
}

Class
object_getClass(id object)
{
	return object_getClass_m68k(object);
}

Class
object_setClass(id object, Class cls)
{
	return object_setClass_m68k(object, cls);
}

const char *
object_getClassName(id object)
{
	return object_getClassName_m68k(object);
}







|

|















|

|



|

|



|

|



|

|



|

|



|

|



|

|



|

|



|

|



|


|




|


|










|

|







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
Class
objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes)
{
	return objc_allocateClassPair_m68k(superclass, name, extraBytes);
}

void
objc_registerClassPair(Class class)
{
	objc_registerClassPair_m68k(class);
}

unsigned int
objc_getClassList(Class *buffer, unsigned int count)
{
	return objc_getClassList_m68k(buffer, count);
}

Class *
objc_copyClassList(unsigned int *length)
{
	return objc_copyClassList_m68k(length);
}

bool
class_isMetaClass(Class class)
{
	return class_isMetaClass_m68k(class);
}

const char *
class_getName(Class class)
{
	return class_getName_m68k(class);
}

Class
class_getSuperclass(Class class)
{
	return class_getSuperclass_m68k(class);
}

unsigned long
class_getInstanceSize(Class class)
{
	return class_getInstanceSize_m68k(class);
}

bool
class_respondsToSelector(Class class, SEL selector)
{
	return class_respondsToSelector_m68k(class, selector);
}

bool
class_conformsToProtocol(Class class, Protocol *protocol)
{
	return class_conformsToProtocol_m68k(class, protocol);
}

IMP
class_getMethodImplementation(Class class, SEL selector)
{
	return class_getMethodImplementation_m68k(class, selector);
}

IMP
class_getMethodImplementation_stret(Class class, SEL selector)
{
	return class_getMethodImplementation_stret_m68k(class, selector);
}

const char *
class_getMethodTypeEncoding(Class class, SEL selector)
{
	return class_getMethodTypeEncoding_m68k(class, selector);
}

bool
class_addMethod(Class class, SEL selector, IMP implementation,
    const char *typeEncoding)
{
	return class_addMethod_m68k(class, selector, implementation,
	    typeEncoding);
}

IMP
class_replaceMethod(Class class, SEL selector, IMP implementation,
    const char *typeEncoding)
{
	return class_replaceMethod_m68k(class, selector, implementation,
	    typeEncoding);
}

Class
object_getClass(id object)
{
	return object_getClass_m68k(object);
}

Class
object_setClass(id object, Class class)
{
	return object_setClass_m68k(object, class);
}

const char *
object_getClassName(id object)
{
	return object_getClassName_m68k(object);
}

Modified src/runtime/lookup.m from [5c7645fb2a] to [b612119f40].

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
	 * so don't access object directly unless it's a class!
	 */

	bool isClass =
	    object_getClass(object)->info & OBJC_CLASS_INFO_METACLASS;

	if (!(object_getClass(object)->info & OBJC_CLASS_INFO_INITIALIZED)) {
		Class cls = (isClass
		    ? (Class)object : object_getClass(object));

		objc_initialize_class(cls);

		if (!(cls->info & OBJC_CLASS_INFO_SETUP))
			OBJC_ERROR("Could not dispatch message for incomplete "
			    "class %s!", cls->name);

		/*
		 * We don't need to handle the case that super was called.
		 * The reason for this is that a call to super is not possible
		 * before a message to the class has been sent and it thus has
		 * been initialized together with its superclasses.
		 */
		return lookup(object, selector);
	}

	/* Try resolveClassMethod: / resolveInstanceMethod: */
	if (class_isMetaClass(object_getClass(object))) {
		Class cls = object_getClass(object);

		if (class_respondsToSelector(cls,
		    @selector(resolveClassMethod:)) &&
		    [object resolveClassMethod: selector]) {
			if (!class_respondsToSelector(cls, selector))
				OBJC_ERROR("[%s resolveClassMethod: %s] "
				    "returned true without adding the method!",
				    class_getName(object),
				    sel_getName(selector));

			return lookup(object, selector);
		}
	} else {
		Class cls = object_getClass(object);
		Class metaclass = object_getClass(cls);

		if (class_respondsToSelector(metaclass,
		    @selector(resolveInstanceMethod:)) &&
		    [cls resolveInstanceMethod: selector]) {
			if (!class_respondsToSelector(cls, selector))
				OBJC_ERROR("[%s resolveInstanceMethod: %s] "
				    "returned true without adding the method!",
				    class_getName(object_getClass(object)),
				    sel_getName(selector));

			return lookup(object, selector);
		}
	}







|


|

|

|












|

|


|
|







|
|



|
|
|







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
	 * so don't access object directly unless it's a class!
	 */

	bool isClass =
	    object_getClass(object)->info & OBJC_CLASS_INFO_METACLASS;

	if (!(object_getClass(object)->info & OBJC_CLASS_INFO_INITIALIZED)) {
		Class class = (isClass
		    ? (Class)object : object_getClass(object));

		objc_initialize_class(class);

		if (!(class->info & OBJC_CLASS_INFO_SETUP))
			OBJC_ERROR("Could not dispatch message for incomplete "
			    "class %s!", class_getName(class));

		/*
		 * We don't need to handle the case that super was called.
		 * The reason for this is that a call to super is not possible
		 * before a message to the class has been sent and it thus has
		 * been initialized together with its superclasses.
		 */
		return lookup(object, selector);
	}

	/* Try resolveClassMethod: / resolveInstanceMethod: */
	if (class_isMetaClass(object_getClass(object))) {
		Class class = object_getClass(object);

		if (class_respondsToSelector(class,
		    @selector(resolveClassMethod:)) &&
		    [object resolveClassMethod: selector]) {
			if (!class_respondsToSelector(class, selector))
				OBJC_ERROR("+[%s resolveClassMethod: %s] "
				    "returned true without adding the method!",
				    class_getName(object),
				    sel_getName(selector));

			return lookup(object, selector);
		}
	} else {
		Class class = object_getClass(object);
		Class metaclass = object_getClass(class);

		if (class_respondsToSelector(metaclass,
		    @selector(resolveInstanceMethod:)) &&
		    [class resolveInstanceMethod: selector]) {
			if (!class_respondsToSelector(class, selector))
				OBJC_ERROR("+[%s resolveInstanceMethod: %s] "
				    "returned true without adding the method!",
				    class_getName(object_getClass(object)),
				    sel_getName(selector));

			return lookup(object, selector);
		}
	}
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
objc_setForwardHandler(IMP forward, IMP stretForward)
{
	forwardHandler = forward;
	stretForwardHandler = stretForward;
}

bool
class_respondsToSelector(Class cls, SEL selector)
{
	if (cls == Nil)
		return false;

	return (objc_dtable_get(cls->DTable,
	    (uint32_t)selector->UID) != (IMP)0);
}

#ifndef OF_ASM_LOOKUP
static id
nilMethod(id self, SEL _cmd)
{







|

|


|







125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
objc_setForwardHandler(IMP forward, IMP stretForward)
{
	forwardHandler = forward;
	stretForwardHandler = stretForward;
}

bool
class_respondsToSelector(Class class, SEL selector)
{
	if (class == Nil)
		return false;

	return (objc_dtable_get(class->DTable,
	    (uint32_t)selector->UID) != (IMP)0);
}

#ifndef OF_ASM_LOOKUP
static id
nilMethod(id self, SEL _cmd)
{
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    IMP (*notFound)(id, SEL))
{
	IMP imp;

	if (super->self == nil)
		return (IMP)nilMethod;

	imp = objc_dtable_get(super->cls->DTable, (uint32_t)selector->UID);

	if (imp == (IMP)0)
		return notFound(super->self, selector);

	return imp;
}








|







179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    IMP (*notFound)(id, SEL))
{
	IMP imp;

	if (super->self == nil)
		return (IMP)nilMethod;

	imp = objc_dtable_get(super->class->DTable, (uint32_t)selector->UID);

	if (imp == (IMP)0)
		return notFound(super->self, selector);

	return imp;
}

Modified src/runtime/protocol.m from [5ec5a018d6] to [4cf5f39401].

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
			    protocol2))
				return true;

	return false;
}

bool
class_conformsToProtocol(Class cls, Protocol *protocol)
{
	struct objc_category **categories;

	if (cls == Nil)
		return false;

	for (struct objc_protocol_list *protocolList = cls->protocols;
	    protocolList != NULL; protocolList = protocolList->next)
		for (long i = 0; i < protocolList->count; i++)
			if (protocol_conformsToProtocol(protocolList->list[i],
			    protocol))
				return true;

	objc_global_mutex_lock();

	if ((categories = objc_categories_for_class(cls)) == NULL) {
		objc_global_mutex_unlock();
		return false;
	}

	for (long i = 0; categories[i] != NULL; i++) {
		for (struct objc_protocol_list *protocolList =
		    categories[i]->protocols; protocolList != NULL;







|



|


|








|







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
			    protocol2))
				return true;

	return false;
}

bool
class_conformsToProtocol(Class class, Protocol *protocol)
{
	struct objc_category **categories;

	if (class == Nil)
		return false;

	for (struct objc_protocol_list *protocolList = class->protocols;
	    protocolList != NULL; protocolList = protocolList->next)
		for (long i = 0; i < protocolList->count; i++)
			if (protocol_conformsToProtocol(protocolList->list[i],
			    protocol))
				return true;

	objc_global_mutex_lock();

	if ((categories = objc_categories_for_class(class)) == NULL) {
		objc_global_mutex_unlock();
		return false;
	}

	for (long i = 0; categories[i] != NULL; i++) {
		for (struct objc_protocol_list *protocolList =
		    categories[i]->protocols; protocolList != NULL;

Modified src/runtime/selector.m from [6c2deedb05] to [97ce324a27].

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
static struct objc_hashtable *selectors = NULL;
static uint32_t selectorsCount = 0;
static struct objc_sparsearray *selectorNames = NULL;
static void **freeList = NULL;
static size_t freeListCount = 0;

void
objc_register_selector(struct objc_abi_selector *sel)
{
	struct objc_selector *rsel;
	const char *name;

	if (selectorsCount > SEL_MAX)
		OBJC_ERROR("Out of selector slots!");

	if (selectors == NULL)
		selectors = objc_hashtable_new(
		    objc_hash_string, objc_equal_string, 2);
	else if ((rsel = objc_hashtable_get(selectors, sel->name)) != NULL) {

		((struct objc_selector *)sel)->UID = rsel->UID;
		return;
	}

	if (selectorNames == NULL)
		selectorNames = objc_sparsearray_new(SEL_SIZE);

	name = sel->name;
	rsel = (struct objc_selector *)sel;
	rsel->UID = selectorsCount++;

	objc_hashtable_set(selectors, name, rsel);
	objc_sparsearray_set(selectorNames, (uint32_t)rsel->UID, (void *)name);

}

SEL
sel_registerName(const char *name)
{
	const struct objc_abi_selector *rsel;
	struct objc_abi_selector *sel;

	objc_global_mutex_lock();

	if (selectors != NULL &&
	    (rsel = objc_hashtable_get(selectors, name)) != NULL) {
		objc_global_mutex_unlock();
		return (SEL)rsel;
	}

	if ((sel = malloc(sizeof(struct objc_abi_selector))) == NULL)
		OBJC_ERROR("Not enough memory to allocate selector!");

	if ((sel->name = of_strdup(name)) == NULL)
		OBJC_ERROR("Not enough memory to allocate selector!");

	sel->typeEncoding = NULL;

	if ((freeList = realloc(freeList,
	    sizeof(void *) * (freeListCount + 2))) == NULL)
		OBJC_ERROR("Not enough memory to allocate selector!");

	freeList[freeListCount++] = sel;
	freeList[freeListCount++] = (char *)sel->name;

	objc_register_selector(sel);

	objc_global_mutex_unlock();
	return (SEL)sel;
}

void
objc_register_all_selectors(struct objc_abi_symtab *symtab)
{
	struct objc_abi_selector *sel;

	if (symtab->selectorRefs == NULL)
		return;

	for (sel = symtab->selectorRefs; sel->name != NULL; sel++)

		objc_register_selector(sel);
}

const char *
sel_getName(SEL selector)
{
	const char *ret;








|

|








|
>
|






|
|
|

|
|
>





<
|




|

|


|


|


|





|
|

|


|





|




|
>
|







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
static struct objc_hashtable *selectors = NULL;
static uint32_t selectorsCount = 0;
static struct objc_sparsearray *selectorNames = NULL;
static void **freeList = NULL;
static size_t freeListCount = 0;

void
objc_register_selector(struct objc_abi_selector *rawSelector)
{
	struct objc_selector *selector;
	const char *name;

	if (selectorsCount > SEL_MAX)
		OBJC_ERROR("Out of selector slots!");

	if (selectors == NULL)
		selectors = objc_hashtable_new(
		    objc_hash_string, objc_equal_string, 2);
	else if ((selector = objc_hashtable_get(selectors,
	    rawSelector->name)) != NULL) {
		((struct objc_selector *)rawSelector)->UID = selector->UID;
		return;
	}

	if (selectorNames == NULL)
		selectorNames = objc_sparsearray_new(SEL_SIZE);

	name = rawSelector->name;
	selector = (struct objc_selector *)rawSelector;
	selector->UID = selectorsCount++;

	objc_hashtable_set(selectors, name, selector);
	objc_sparsearray_set(selectorNames, (uint32_t)selector->UID,
	    (void *)name);
}

SEL
sel_registerName(const char *name)
{

	struct objc_abi_selector *rawSelector;

	objc_global_mutex_lock();

	if (selectors != NULL &&
	    (rawSelector= objc_hashtable_get(selectors, name)) != NULL) {
		objc_global_mutex_unlock();
		return (SEL)rawSelector;
	}

	if ((rawSelector = malloc(sizeof(*rawSelector))) == NULL)
		OBJC_ERROR("Not enough memory to allocate selector!");

	if ((rawSelector->name = of_strdup(name)) == NULL)
		OBJC_ERROR("Not enough memory to allocate selector!");

	rawSelector->typeEncoding = NULL;

	if ((freeList = realloc(freeList,
	    sizeof(void *) * (freeListCount + 2))) == NULL)
		OBJC_ERROR("Not enough memory to allocate selector!");

	freeList[freeListCount++] = rawSelector;
	freeList[freeListCount++] = (char *)rawSelector->name;

	objc_register_selector(rawSelector);

	objc_global_mutex_unlock();
	return (SEL)rawSelector;
}

void
objc_register_all_selectors(struct objc_abi_symtab *symtab)
{
	struct objc_abi_selector *rawSelector;

	if (symtab->selectorRefs == NULL)
		return;

	for (rawSelector = symtab->selectorRefs; rawSelector->name != NULL;
	    rawSelector++)
		objc_register_selector(rawSelector);
}

const char *
sel_getName(SEL selector)
{
	const char *ret;

Modified src/runtime/static-instances.m from [e9f2e9fced] to [ca4604a232].

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
void
objc_init_static_instances(struct objc_abi_symtab *symtab)
{
	struct objc_abi_static_instances **staticInstances;

	/* Check if the class for a static instance became available */
	for (size_t i = 0; i < staticInstancesCount; i++) {
		Class cls = objc_lookUpClass(
		    staticInstancesList[i]->className);

		if (cls != Nil) {
			for (id *instances = staticInstancesList[i]->instances;
			    *instances != nil; instances++)
				object_setClass(*instances, cls);

			staticInstancesCount--;

			if (staticInstancesCount == 0) {
				free(staticInstancesList);
				staticInstancesList = NULL;
				break;







|


|


|







29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
void
objc_init_static_instances(struct objc_abi_symtab *symtab)
{
	struct objc_abi_static_instances **staticInstances;

	/* Check if the class for a static instance became available */
	for (size_t i = 0; i < staticInstancesCount; i++) {
		Class class = objc_lookUpClass(
		    staticInstancesList[i]->className);

		if (class != Nil) {
			for (id *instances = staticInstancesList[i]->instances;
			    *instances != nil; instances++)
				object_setClass(*instances, class);

			staticInstancesCount--;

			if (staticInstancesCount == 0) {
				free(staticInstancesList);
				staticInstancesList = NULL;
				break;
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
	staticInstances = (struct objc_abi_static_instances **)
	    symtab->defs[symtab->classDefsCount + symtab->categoryDefsCount];

	if (staticInstances == NULL)
		return;

	for (; *staticInstances != NULL; staticInstances++) {
		Class cls = objc_lookUpClass((*staticInstances)->className);

		if (cls != Nil) {
			for (id *instances = (*staticInstances)->instances;
			    *instances != nil; instances++)
				object_setClass(*instances, cls);
		} else {
			staticInstancesList = realloc(staticInstancesList,
			    sizeof(struct objc_abi_static_instances *) *
			    (staticInstancesCount + 1));

			if (staticInstancesList == NULL)
				OBJC_ERROR("Not enough memory for list of "







|

|


|







71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
	staticInstances = (struct objc_abi_static_instances **)
	    symtab->defs[symtab->classDefsCount + symtab->categoryDefsCount];

	if (staticInstances == NULL)
		return;

	for (; *staticInstances != NULL; staticInstances++) {
		Class class = objc_lookUpClass((*staticInstances)->className);

		if (class != Nil) {
			for (id *instances = (*staticInstances)->instances;
			    *instances != nil; instances++)
				object_setClass(*instances, class);
		} else {
			staticInstancesList = realloc(staticInstancesList,
			    sizeof(struct objc_abi_static_instances *) *
			    (staticInstancesCount + 1));

			if (staticInstancesList == NULL)
				OBJC_ERROR("Not enough memory for list of "