ObjFW  Check-in [79011c5f56]

Overview
Comment:runtime: Move all globals into a single struct

This is required to create a .library on MorphOS.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 79011c5f5699e3adb6846dbe0dff313cba4991009d55740f92422fc806abb2ba
User & Date: js on 2017-06-07 21:38:21
Other Links: manifest | tags
Context
2017-06-10
10:00
Rename OFDeflateStream back to OFInflateStream check-in: f56a50ee50 user: js tags: trunk
2017-06-07
21:38
runtime: Move all globals into a single struct check-in: 79011c5f56 user: js tags: trunk
20:22
OFBlock: Fix a type mismatch check-in: c12d5c986f user: js tags: trunk
Changes

Modified src/runtime/arc.m from [a8a136e91e] to [d1270d3d56].

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







-
-
-
+
+
+
-















-
+


-
+







#import "OFBlock.h"

struct weak_ref {
	id **locations;
	size_t count;
};

static struct objc_hashtable *hashtable;
#ifdef OF_HAVE_THREADS
static of_spinlock_t spinlock;
#import "globals.h"
#define weak_refs objc_globals.weak_refs
#define weak_refs_lock objc_globals.weak_refs_lock
#endif

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

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

OF_CONSTRUCTOR()
{
	hashtable = objc_hashtable_new(obj_hash, obj_equal, 2);
	weak_refs = objc_hashtable_new(obj_hash, obj_equal, 2);

#ifdef OF_HAVE_THREADS
	if (!of_spinlock_new(&spinlock))
	if (!of_spinlock_new(&weak_refs_lock))
		OBJC_ERROR("Failed to create spinlock!")
#endif
}

id
objc_retain(id object)
{
120
121
122
123
124
125
126
127

128
129
130
131
132

133
134
135
136

137
138
139
140
141
142
143
119
120
121
122
123
124
125

126
127
128
129
130

131
132
133
134

135
136
137
138
139
140
141
142







-
+




-
+



-
+








id
objc_storeWeak(id *object, id value)
{
	struct weak_ref *old;

#ifdef OF_HAVE_THREADS
	if (!of_spinlock_lock(&spinlock))
	if (!of_spinlock_lock(&weak_refs_lock))
		OBJC_ERROR("Failed to lock spinlock!")
#endif

	if (*object != nil &&
	    (old = objc_hashtable_get(hashtable, *object)) != NULL) {
	    (old = objc_hashtable_get(weak_refs, *object)) != NULL) {
		for (size_t i = 0; i < old->count; i++) {
			if (old->locations[i] == object) {
				if (--old->count == 0) {
					objc_hashtable_delete(hashtable,
					objc_hashtable_delete(weak_refs,
					    *object);
					free(old->locations);
					free(old);
				} else {
					id **locations;

					old->locations[i] =
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
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







-
+






-
+














-
+













-
+



-
+



-
+







				break;
			}
		}
	}

	if (value != nil && class_respondsToSelector(object_getClass(value),
	    @selector(allowsWeakReference)) && [value allowsWeakReference]) {
		struct weak_ref *ref = objc_hashtable_get(hashtable, value);
		struct weak_ref *ref = objc_hashtable_get(weak_refs, value);

		if (ref == NULL) {
			if ((ref = calloc(1, sizeof(*ref))) == NULL)
				OBJC_ERROR("Not enough memory to allocate weak "
				    "reference!");

			objc_hashtable_set(hashtable, value, ref);
			objc_hashtable_set(weak_refs, value, ref);
		}

		if ((ref->locations = realloc(ref->locations,
		    (ref->count + 1) * sizeof(id *))) == NULL)
			OBJC_ERROR("Not enough memory to allocate weak "
			    "reference!")

		ref->locations[ref->count++] = object;
	} else
		value = nil;

	*object = value;

#ifdef OF_HAVE_THREADS
	if (!of_spinlock_unlock(&spinlock))
	if (!of_spinlock_unlock(&weak_refs_lock))
		OBJC_ERROR("Failed to unlock spinlock!")
#endif

	return value;
}

id
objc_loadWeakRetained(id *object)
{
	id value = nil;
	struct weak_ref *ref;

#ifdef OF_HAVE_THREADS
	if (!of_spinlock_lock(&spinlock))
	if (!of_spinlock_lock(&weak_refs_lock))
		OBJC_ERROR("Failed to lock spinlock!")
#endif

	if ((ref = objc_hashtable_get(hashtable, *object)) != NULL)
	if ((ref = objc_hashtable_get(weak_refs, *object)) != NULL)
		value = *object;

#ifdef OF_HAVE_THREADS
	if (!of_spinlock_unlock(&spinlock))
	if (!of_spinlock_unlock(&weak_refs_lock))
		OBJC_ERROR("Failed to unlock spinlock!")
#endif

	if (class_respondsToSelector(object_getClass(value),
	    @selector(retainWeakReference)) && [value retainWeakReference])
		return value;

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







-
+



-
+












-
+










-
+



-
+



-
+





-
+




void
objc_moveWeak(id *dest, id *src)
{
	struct weak_ref *ref;

#ifdef OF_HAVE_THREADS
	if (!of_spinlock_lock(&spinlock))
	if (!of_spinlock_lock(&weak_refs_lock))
		OBJC_ERROR("Failed to lock spinlock!")
#endif

	if ((ref = objc_hashtable_get(hashtable, *src)) != NULL) {
	if ((ref = objc_hashtable_get(weak_refs, *src)) != NULL) {
		for (size_t i = 0; i < ref->count; i++) {
			if (ref->locations[i] == src) {
				ref->locations[i] = dest;
				break;
			}
		}
	}

	*dest = *src;
	*src = nil;

#ifdef OF_HAVE_THREADS
	if (!of_spinlock_unlock(&spinlock))
	if (!of_spinlock_unlock(&weak_refs_lock))
		OBJC_ERROR("Failed to unlock spinlock!")
#endif
}

void
objc_zero_weak_references(id value)
{
	struct weak_ref *ref;

#ifdef OF_HAVE_THREADS
	if (!of_spinlock_lock(&spinlock))
	if (!of_spinlock_lock(&weak_refs_lock))
		OBJC_ERROR("Failed to lock spinlock!")
#endif

	if ((ref = objc_hashtable_get(hashtable, value)) != NULL) {
	if ((ref = objc_hashtable_get(weak_refs, value)) != NULL) {
		for (size_t i = 0; i < ref->count; i++)
			*ref->locations[i] = nil;

		objc_hashtable_delete(hashtable, value);
		objc_hashtable_delete(weak_refs, value);
		free(ref->locations);
		free(ref);
	}

#ifdef OF_HAVE_THREADS
	if (!of_spinlock_unlock(&spinlock))
	if (!of_spinlock_unlock(&weak_refs_lock))
		OBJC_ERROR("Failed to unlock spinlock!")
#endif
}

Modified src/runtime/category.m from [768ffeb962] to [84e94f572f].

19
20
21
22
23
24
25

26

27
28
29
30
31
32
33
19
20
21
22
23
24
25
26

27
28
29
30
31
32
33
34







+
-
+







#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#import "runtime.h"
#import "runtime-private.h"

#import "globals.h"
static struct objc_hashtable *categories = NULL;
#define categories objc_globals.categories

static void
register_selectors(struct objc_abi_category *cat)
{
	for (struct objc_abi_method_list *ml = cat->instance_methods;
	    ml != NULL; ml = ml->next)
		for (unsigned int i = 0; i < ml->count; i++)

Modified src/runtime/class.m from [aedf6b0f5f] to [de3e6ed813].

22
23
24
25
26
27
28

29
30
31
32
33
34
35







36
37
38
39
40
41
42
22
23
24
25
26
27
28
29







30
31
32
33
34
35
36
37
38
39
40
41
42
43







+
-
-
-
-
-
-
-
+
+
+
+
+
+
+







#include <limits.h>

#include <assert.h>

#import "runtime.h"
#import "runtime-private.h"

#import "globals.h"
static struct objc_hashtable *classes = NULL;
static unsigned classes_cnt = 0;
static Class *load_queue = NULL;
static size_t load_queue_cnt = 0;
static struct objc_dtable *empty_dtable = NULL;
static unsigned lookups_till_fast_path = 128;
static struct objc_sparsearray *fast_path = NULL;
#define classes objc_globals.classes
#define classes_cnt objc_globals.classes_cnt
#define load_queue objc_globals.load_queue
#define load_queue_cnt objc_globals.load_queue_cnt
#define empty_dtable objc_globals.empty_dtable
#define lookups_till_fast_path objc_globals.lookups_till_fast_path
#define fast_path objc_globals.fast_path

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

Modified src/runtime/dtable.m from [6f5c36ea25] to [da0a13a473].

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







+
-
+
-
-
+
-




-
-
+
+



-
-
+
+





-
-
+
+



-
+









-
+


-
+







-
+








-
+




-
+








#include <stdio.h>
#include <stdlib.h>

#import "runtime.h"
#import "runtime-private.h"

#import "globals.h"
static struct objc_dtable_level2 *empty_level2 = NULL;
#define empty_dtable_level2 objc_globals.empty_dtable_level2
#ifdef OF_SELUID24
static struct objc_dtable_level3 *empty_level3 = NULL;
#define empty_dtable_level3 objc_globals.empty_dtable_level3
#endif

static void
init(void)
{
	empty_level2 = malloc(sizeof(struct objc_dtable_level2));
	if (empty_level2 == NULL)
	empty_dtable_level2 = malloc(sizeof(struct objc_dtable_level2));
	if (empty_dtable_level2 == NULL)
		OBJC_ERROR("Not enough memory to allocate dtable!");

#ifdef OF_SELUID24
	empty_level3 = malloc(sizeof(struct objc_dtable_level3));
	if (empty_level3 == NULL)
	empty_dtable_level3 = malloc(sizeof(struct objc_dtable_level3));
	if (empty_dtable_level3 == NULL)
		OBJC_ERROR("Not enough memory to allocate dtable!");
#endif

#ifdef OF_SELUID24
	for (uint_fast16_t i = 0; i < 256; i++) {
		empty_level2->buckets[i] = empty_level3;
		empty_level3->buckets[i] = (IMP)0;
		empty_dtable_level2->buckets[i] = empty_dtable_level3;
		empty_dtable_level3->buckets[i] = (IMP)0;
	}
#else
	for (uint_fast16_t i = 0; i < 256; i++)
		empty_level2->buckets[i] = (IMP)0;
		empty_dtable_level2->buckets[i] = (IMP)0;
#endif
}

struct objc_dtable *
objc_dtable_new(void)
{
	struct objc_dtable *dtable;

#ifdef OF_SELUID24
	if (empty_level2 == NULL || empty_level3 == NULL)
	if (empty_dtable_level2 == NULL || empty_dtable_level3 == NULL)
		init();
#else
	if (empty_level2 == NULL)
	if (empty_dtable_level2 == NULL)
		init();
#endif

	if ((dtable = malloc(sizeof(struct objc_dtable))) == NULL)
		OBJC_ERROR("Not enough memory to allocate dtable!");

	for (uint_fast16_t i = 0; i < 256; i++)
		dtable->buckets[i] = empty_level2;
		dtable->buckets[i] = empty_dtable_level2;

	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] == empty_level2)
		if (src->buckets[i] == empty_dtable_level2)
			continue;

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

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

				obj = src->buckets[i]->buckets[j]->buckets[k];
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
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







-
+








-
+








-
+







	uint8_t j = idx >> 8;
	uint8_t k = idx;
#else
	uint8_t i = idx >> 8;
	uint8_t j = idx;
#endif

	if (dtable->buckets[i] == empty_level2) {
	if (dtable->buckets[i] == empty_dtable_level2) {
		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] = empty_level3;
			level2->buckets[l] = empty_dtable_level3;
#else
			level2->buckets[l] = (IMP)0;
#endif

		dtable->buckets[i] = level2;
	}

#ifdef OF_SELUID24
	if (dtable->buckets[i]->buckets[j] == empty_level3) {
	if (dtable->buckets[i]->buckets[j] == empty_dtable_level3) {
		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++)
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
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







-
+




-
+
+












-
-
+
+

-
-
+
+


-
+

-
+


#endif
}

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

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

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

	free(dtable);
}

void
objc_dtable_cleanup(void)
{
	if (empty_level2 != NULL)
		free(empty_level2);
	if (empty_dtable_level2 != NULL)
		free(empty_dtable_level2);
#ifdef OF_SELUID24
	if (empty_level3 != NULL)
		free(empty_level3);
	if (empty_dtable_level3 != NULL)
		free(empty_dtable_level3);
#endif

	empty_level2 = NULL;
	empty_dtable_level2 = NULL;
#ifdef OF_SELUID24
	empty_level3 = NULL;
	empty_dtable_level3 = NULL;
#endif
}

Added src/runtime/globals.h version [4d94664fb8].






























































































1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
 *   Jonathan Schleifer <js@heap.zone>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "runtime-private.h"

#ifdef OF_HAVE_THREADS
# import "threading.h"
#endif

/* All globals used by the runtime */
struct objc_globals {
	/* arc.m */
	struct objc_hashtable *weak_refs;
#ifdef OF_HAVE_THREADS
	of_spinlock_t weak_refs_lock;
#endif

	/* category.m */
	struct objc_hashtable *categories;

	/* class.m */
	struct objc_hashtable *classes;
	unsigned classes_cnt;
	Class *load_queue;
	size_t load_queue_cnt;
	struct objc_dtable *empty_dtable;
	unsigned lookups_till_fast_path;
	struct objc_sparsearray *fast_path;

	/* dtable.m */
	struct objc_dtable_level2 *empty_dtable_level2;
#ifdef OF_SELUID24
	struct objc_dtable_level3 *empty_dtable_level3;
#endif

	/* lookup.m */
	IMP forward_handler;
	IMP forward_handler_stret;

	/* misc.m */
	objc_enumeration_mutation_handler enumeration_mutation_handler;

	/* property.m */
#ifdef OF_HAVE_THREADS
# define NUM_PROPERTY_LOCKS 8	/* needs to be a power of 2 */
	of_spinlock_t property_locks[NUM_PROPERTY_LOCKS];
#endif

	/* selector.m */
	struct objc_hashtable *selectors;
	uint32_t selectors_cnt;
	struct objc_sparsearray *selector_names;
	void **ptrs_to_free;
	size_t ptrs_to_free_cnt;

	/* static-instances.m */
	struct objc_abi_static_instances **static_instances;
	size_t static_instances_cnt;

	/* synchronized.m */
#ifdef OF_HAVE_THREADS
	struct synchronized_lock {
		id object;
		int count;
		of_rmutex_t rmutex;
		struct synchronized_lock *next;
	} *synchronized_locks;
	of_mutex_t synchronized_locks_lock;
#endif

	/* threading.m */
#ifdef OF_HAVE_THREADS
	of_rmutex_t global_mutex;
	of_once_t global_once_control;
#endif
};

extern struct objc_globals objc_globals;

Modified src/runtime/init.m from [56c8216488] to [631c262625].

14
15
16
17
18
19
20









21
22
23
24
25
26
27
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36







+
+
+
+
+
+
+
+
+







 * file.
 */

#include "config.h"

#import "runtime.h"
#import "runtime-private.h"

#import "globals.h"

struct objc_globals objc_globals = {
#ifdef OF_HAVE_THREADS
	.global_once_control = OF_ONCE_INIT,
#endif
	.lookups_till_fast_path = 128
};

void
__objc_exec_class(void *module_)
{
	struct objc_abi_module *module = module_;

	objc_global_mutex_lock();

Modified src/runtime/lookup.m from [b142ca0e6c] to [5175096ba7].

28
29
30
31
32
33
34

35
36


37
38
39
40
41
42
43
28
29
30
31
32
33
34
35


36
37
38
39
40
41
42
43
44







+
-
-
+
+







	Class isa;
}

+ (BOOL)resolveClassMethod: (SEL)selector;
+ (BOOL)resolveInstanceMethod: (SEL)selector;
@end

#import "globals.h"
static IMP forward_handler = (IMP)0;
static IMP forward_handler_stret = (IMP)0;
#define forward_handler objc_globals.forward_handler
#define forward_handler_stret objc_globals.forward_handler_stret

static IMP
common_method_not_found(id obj, SEL sel, IMP (*lookup)(id, SEL), IMP forward)
{
	/*
	 * obj might be a dummy object (see class_getMethodImplementation), so
	 * don't access obj directly unless it's a class!

Modified src/runtime/misc.m from [8f9e858d65] to [14423bcc6c].

18
19
20
21
22
23
24

25

26
27
28
29
30
31
32
18
19
20
21
22
23
24
25

26
27
28
29
30
31
32
33







+
-
+








#include <stdio.h>
#include <stdlib.h>

#include "runtime.h"
#include "runtime-private.h"

#import "globals.h"
static objc_enumeration_mutation_handler enumeration_mutation_handler = NULL;
#define enumeration_mutation_handler objc_globals.enumeration_mutation_handler

void
objc_enumerationMutation(id obj)
{
	if (enumeration_mutation_handler != NULL)
		enumeration_mutation_handler(obj);
	else

Modified src/runtime/property.m from [f610db3f7e] to [63a32b597a].

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







+
+
+


-
-
-
+
+
-

-


-
-
+
+












-
+



-
+


















-
+


















-
+







#include <string.h>

#import "runtime.h"
#import "runtime-private.h"

#import "OFObject.h"

#import "globals.h"
#define property_locks objc_globals.property_locks

#ifdef OF_HAVE_THREADS
# import "threading.h"
# define NUM_SPINLOCKS 8	/* needs to be a power of 2 */
# define SPINLOCK_HASH(p) ((unsigned)((uintptr_t)p >> 4) & (NUM_SPINLOCKS - 1))
static of_spinlock_t spinlocks[NUM_SPINLOCKS];
# define SPINLOCK_HASH(p) \
    ((unsigned)((uintptr_t)p >> 4) & (NUM_PROPERTY_LOCKS - 1))
#endif

#ifdef OF_HAVE_THREADS
OF_CONSTRUCTOR()
{
	for (size_t i = 0; i < NUM_SPINLOCKS; i++)
		if (!of_spinlock_new(&spinlocks[i]))
	for (size_t i = 0; i < NUM_PROPERTY_LOCKS; i++)
		if (!of_spinlock_new(&property_locks[i]))
			OBJC_ERROR("Failed to initialize spinlocks!")
}
#endif

id
objc_getProperty(id self, SEL _cmd, ptrdiff_t offset, BOOL atomic)
{
	if (atomic) {
		id *ptr = (id *)(void *)((char *)self + offset);
#ifdef OF_HAVE_THREADS
		unsigned hash = SPINLOCK_HASH(ptr);

		OF_ENSURE(of_spinlock_lock(&spinlocks[hash]));
		OF_ENSURE(of_spinlock_lock(&property_locks[hash]));
		@try {
			return [[*ptr retain] autorelease];
		} @finally {
			OF_ENSURE(of_spinlock_unlock(&spinlocks[hash]));
			OF_ENSURE(of_spinlock_unlock(&property_locks[hash]));
		}
#else
		return [[*ptr retain] autorelease];
#endif
	}

	return *(id *)(void *)((char *)self + offset);
}

void
objc_setProperty(id self, SEL _cmd, ptrdiff_t offset, id value, BOOL atomic,
    signed char copy)
{
	if (atomic) {
		id *ptr = (id *)(void *)((char *)self + offset);
#ifdef OF_HAVE_THREADS
		unsigned hash = SPINLOCK_HASH(ptr);

		OF_ENSURE(of_spinlock_lock(&spinlocks[hash]));
		OF_ENSURE(of_spinlock_lock(&property_locks[hash]));
		@try {
#endif
			id old = *ptr;

			switch (copy) {
			case 0:
				*ptr = [value retain];
				break;
			case 2:
				*ptr = [value mutableCopy];
				break;
			default:
				*ptr = [value copy];
			}

			[old release];
#ifdef OF_HAVE_THREADS
		} @finally {
			OF_ENSURE(of_spinlock_unlock(&spinlocks[hash]));
			OF_ENSURE(of_spinlock_unlock(&property_locks[hash]));
		}
#endif

		return;
	}

	id *ptr = (id *)(void *)((char *)self + offset);
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
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







-
+



-
+
















-
+



-
+







objc_getPropertyStruct(void *dest, const void *src, ptrdiff_t size, BOOL atomic,
    BOOL strong)
{
	if (atomic) {
#ifdef OF_HAVE_THREADS
		unsigned hash = SPINLOCK_HASH(src);

		OF_ENSURE(of_spinlock_lock(&spinlocks[hash]));
		OF_ENSURE(of_spinlock_lock(&property_locks[hash]));
#endif
		memcpy(dest, src, size);
#ifdef OF_HAVE_THREADS
		OF_ENSURE(of_spinlock_unlock(&spinlocks[hash]));
		OF_ENSURE(of_spinlock_unlock(&property_locks[hash]));
#endif

		return;
	}

	memcpy(dest, src, size);
}

void
objc_setPropertyStruct(void *dest, const void *src, ptrdiff_t size, BOOL atomic,
    BOOL strong)
{
	if (atomic) {
#ifdef OF_HAVE_THREADS
		unsigned hash = SPINLOCK_HASH(src);

		OF_ENSURE(of_spinlock_lock(&spinlocks[hash]));
		OF_ENSURE(of_spinlock_lock(&property_locks[hash]));
#endif
		memcpy(dest, src, size);
#ifdef OF_HAVE_THREADS
		OF_ENSURE(of_spinlock_unlock(&spinlocks[hash]));
		OF_ENSURE(of_spinlock_unlock(&property_locks[hash]));
#endif

		return;
	}

	memcpy(dest, src, size);
}

Modified src/runtime/selector.m from [57debffc40] to [f5a125315c].

29
30
31
32
33
34
35

36
37
38
39





40
41
42
43
44
45
46
47
29
30
31
32
33
34
35
36




37
38
39
40
41

42
43
44
45
46
47
48







+
-
-
-
-
+
+
+
+
+
-







# define SEL_MAX 0xFFFFFF
# define SEL_SIZE 3
#else
# define SEL_MAX 0xFFFF
# define SEL_SIZE 2
#endif

#import "globals.h"
static struct objc_hashtable *selectors = NULL;
static uint32_t selectors_cnt = 0;
static struct objc_sparsearray *selector_names = NULL;
static void **free_list = NULL;
#define selectors objc_globals.selectors
#define selectors_cnt objc_globals.selectors_cnt
#define selector_names objc_globals.selector_names
#define ptrs_to_free objc_globals.ptrs_to_free
#define ptrs_to_free_cnt objc_globals.ptrs_to_free_cnt
static size_t free_list_cnt = 0;

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

85
86
87
88
89
90
91
92
93


94
95
96
97


98
99
100
101
102
103
104
86
87
88
89
90
91
92


93
94
95
96


97
98
99
100
101
102
103
104
105







-
-
+
+


-
-
+
+







		OBJC_ERROR("Not enough memory to allocate selector!");

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

	sel->types = NULL;

	if ((free_list = realloc(free_list,
	    sizeof(void *) * (free_list_cnt + 2))) == NULL)
	if ((ptrs_to_free = realloc(ptrs_to_free,
	    sizeof(void *) * (ptrs_to_free_cnt + 2))) == NULL)
		OBJC_ERROR("Not enough memory to allocate selector!");

	free_list[free_list_cnt++] = sel;
	free_list[free_list_cnt++] = (char *)sel->name;
	ptrs_to_free[ptrs_to_free_cnt++] = sel;
	ptrs_to_free[ptrs_to_free_cnt++] = (char *)sel->name;

	objc_register_selector(sel);

	objc_global_mutex_unlock();
	return (SEL)sel;
}

134
135
136
137
138
139
140
141
142
143



144
145

146
147
148
149
150
151
152


153
135
136
137
138
139
140
141



142
143
144
145

146
147
148
149
150
151


152
153
154







-
-
-
+
+
+

-
+





-
-
+
+


void
objc_unregister_all_selectors(void)
{
	objc_hashtable_free(selectors);
	objc_sparsearray_free(selector_names);

	if (free_list != NULL) {
		for (size_t i = 0; i < free_list_cnt; i++)
			free(free_list[i]);
	if (ptrs_to_free != NULL) {
		for (size_t i = 0; i < ptrs_to_free_cnt; i++)
			free(ptrs_to_free[i]);

		free(free_list);
		free(ptrs_to_free);
	}

	selectors = NULL;
	selectors_cnt = 0;
	selector_names = NULL;
	free_list = NULL;
	free_list_cnt = 0;
	ptrs_to_free = NULL;
	ptrs_to_free_cnt = 0;
}

Modified src/runtime/static-instances.m from [e637d2916b] to [f2156324ce].

18
19
20
21
22
23
24

25
26


27
28
29
30
31
32
33
18
19
20
21
22
23
24
25


26
27
28
29
30
31
32
33
34







+
-
-
+
+








#include <stdio.h>
#include <stdlib.h>

#import "runtime.h"
#import "runtime-private.h"

#import "globals.h"
static struct objc_abi_static_instances **static_instances = NULL;
static size_t static_instances_cnt = 0;
#define static_instances objc_globals.static_instances
#define static_instances_cnt objc_globals.static_instances_cnt

void
objc_init_static_instances(struct objc_abi_symtab *symtab)
{
	struct objc_abi_static_instances **si;

	/* Check if the class for a static instance became available */

Modified src/runtime/synchronized.m from [5afa8ac0c0] to [aae42e3525].

18
19
20
21
22
23
24




25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

40
41
42
43
44
45
46
47
48
49
50
51

52
53

54
55
56
57

58
59
60
61
62
63

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

82
83

84
85

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

103
104

105
106
107

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123


124
125
126
127
128

129
130
131
132
133
134
135
136
137
138
18
19
20
21
22
23
24
25
26
27
28
29
30
31









32
33

34
35
36
37
38
39
40
41
42
43
44
45

46
47

48
49
50
51

52
53
54
55
56
57

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

76
77

78
79

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

97
98

99
100
101

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116


117
118
119
120
121
122

123
124
125
126
127
128
129
130
131
132
133







+
+
+
+



-
-
-
-
-
-
-
-
-


-
+











-
+

-
+



-
+





-
+

















-
+

-
+

-
+
















-
+

-
+


-
+














-
-
+
+




-
+











#include <stdio.h>
#include <stdlib.h>

#import "runtime.h"
#import "runtime-private.h"

#import "globals.h"
#define synchronized_locks objc_globals.synchronized_locks
#define synchronized_locks_lock objc_globals.synchronized_locks_lock

#ifdef OF_HAVE_THREADS
# import "threading.h"

static struct lock_s {
	id	      object;
	int	      count;
	of_rmutex_t   rmutex;
	struct lock_s *next;
} *locks = NULL;

static of_mutex_t mutex;

OF_CONSTRUCTOR()
{
	if (!of_mutex_new(&mutex))
	if (!of_mutex_new(&synchronized_locks_lock))
		OBJC_ERROR("Failed to create mutex!")
}
#endif

int
objc_sync_enter(id object)
{
	if (object == nil)
		return 0;

#ifdef OF_HAVE_THREADS
	struct lock_s *lock;
	struct synchronized_lock *lock;

	if (!of_mutex_lock(&mutex))
	if (!of_mutex_lock(&synchronized_locks_lock))
		OBJC_ERROR("Failed to lock mutex!");

	/* Look if we already have a lock */
	for (lock = locks; lock != NULL; lock = lock->next) {
	for (lock = synchronized_locks; lock != NULL; lock = lock->next) {
		if (lock->object != object)
			continue;

		lock->count++;

		if (!of_mutex_unlock(&mutex))
		if (!of_mutex_unlock(&synchronized_locks_lock))
			OBJC_ERROR("Failed to unlock mutex!");

		if (!of_rmutex_lock(&lock->rmutex))
			OBJC_ERROR("Failed to lock mutex!");

		return 0;
	}

	/* Create a new lock */
	if ((lock = malloc(sizeof(*lock))) == NULL)
		OBJC_ERROR("Failed to allocate memory for mutex!");

	if (!of_rmutex_new(&lock->rmutex))
		OBJC_ERROR("Failed to create mutex!");

	lock->object = object;
	lock->count = 1;
	lock->next = locks;
	lock->next = synchronized_locks;

	locks = lock;
	synchronized_locks = lock;

	if (!of_mutex_unlock(&mutex))
	if (!of_mutex_unlock(&synchronized_locks_lock))
		OBJC_ERROR("Failed to unlock mutex!");

	if (!of_rmutex_lock(&lock->rmutex))
		OBJC_ERROR("Failed to lock mutex!");
#endif

	return 0;
}

int
objc_sync_exit(id object)
{
	if (object == nil)
		return 0;

#ifdef OF_HAVE_THREADS
	struct lock_s *lock, *last = NULL;
	struct synchronized_lock *lock, *last = NULL;

	if (!of_mutex_lock(&mutex))
	if (!of_mutex_lock(&synchronized_locks_lock))
		OBJC_ERROR("Failed to lock mutex!");

	for (lock = locks; lock != NULL; lock = lock->next) {
	for (lock = synchronized_locks; lock != NULL; lock = lock->next) {
		if (lock->object != object) {
			last = lock;
			continue;
		}

		if (!of_rmutex_unlock(&lock->rmutex))
			OBJC_ERROR("Failed to unlock mutex!");

		if (--lock->count == 0) {
			if (!of_rmutex_free(&lock->rmutex))
				OBJC_ERROR("Failed to destroy mutex!");

			if (last != NULL)
				last->next = lock->next;
			if (locks == lock)
				locks = lock->next;
			if (synchronized_locks == lock)
				synchronized_locks = lock->next;

			free(lock);
		}

		if (!of_mutex_unlock(&mutex))
		if (!of_mutex_unlock(&synchronized_locks_lock))
			OBJC_ERROR("Failed to unlock mutex!");

		return 0;
	}

	OBJC_ERROR("objc_sync_exit() was called for an object not locked!");
#else
	return 0;
#endif
}

Modified src/runtime/threading.m from [1fbcc9555d] to [e057f37307].

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







+
-
-
+
+











-
+











#include <stdio.h>
#include <stdlib.h>

#import "runtime.h"
#import "runtime-private.h"
#import "threading.h"

#import "globals.h"
static of_rmutex_t global_mutex;
static of_once_t once_control = OF_ONCE_INIT;
#define global_mutex objc_globals.global_mutex
#define global_once_control objc_globals.global_once_control

static void
init(void)
{
	if (!of_rmutex_new(&global_mutex))
		OBJC_ERROR("Failed to create global mutex!");
}

void
objc_global_mutex_lock(void)
{
	of_once(&once_control, init);
	of_once(&global_once_control, init);

	if (!of_rmutex_lock(&global_mutex))
		OBJC_ERROR("Failed to lock global mutex!");
}

void
objc_global_mutex_unlock(void)
{
	if (!of_rmutex_unlock(&global_mutex))
		OBJC_ERROR("Failed to unlock global mutex!");
}