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