File: weak.m

package info (click to toggle)
gnustep-base 1.31.1-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 26,460 kB
  • sloc: objc: 239,505; ansic: 36,519; sh: 135; cpp: 122; makefile: 100; xml: 32
file content (446 lines) | stat: -rw-r--r-- 10,036 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446

/* Emulation of ARC runtime support for weak references based on the gnustep
 * runtime implementation.
 */

#import "common.h"
#import "Foundation/Foundation.h"
#import "../GSPrivate.h"
#import "../GSPThread.h"

static Class	persistentClasses[1];
static int	persistentClassCount = sizeof(persistentClasses)/sizeof(Class);

/* This function needs to identify objects which should NOT be handled by
 * weak references.
 * Nil is a special case which can not be stored as a weak reference because
 * it indicates the absence of an object etc.
 * Persistent objects do not need any sort of weak (or strong) reference and
 * if they are immutable then trying to mark them as referenced would crash.
 */
__attribute__((always_inline))
static inline BOOL
isPersistentObject(id obj)
{
  Class	c;
  int	i;

  if (nil == obj)
    {
      return YES;
    }

  /* If the alignment of the object does not match that needed for a
   * pointer (to the class of the object) then the object must be a
   * special one of some sort and we assume it's persistent.
   */
#if	ALIGNOF_OBJC_OBJECT > 1
  if ((intptr_t)obj & (ALIGNOF_OBJC_OBJECT - 1))
    {
      return YES;
    }
#endif

  c = object_getClass(obj);
  if (class_isMetaClass(c))
    {
      return YES;	// obj was a class rather than an instance
    }

  for (i = 0; i < persistentClassCount; i++)
    {
      if (persistentClasses[i] == c)
	{
	  return YES;	// the object is a persistent instance
	}
    }
  return NO;
}

static int  WeakRefClass = 0;

#define GSI_MAP_NODE_CLASS  (&WeakRefClass)
#define GSI_MAP_CLEAR_KEY(M, X) ((*X).obj = nil)       
#define GSI_MAP_HASH(M, X)	((NSUInteger)(X.obj) >> 2)
#define GSI_MAP_EQUAL(M, X, Y)	(X.obj == Y.obj)
#define GSI_MAP_RETAIN_KEY(M, X)        
#define GSI_MAP_RELEASE_KEY(M, X)       
#define GSI_MAP_RETAIN_VAL(M, X)        
#define GSI_MAP_RELEASE_VAL(M, X)       
#define GSI_MAP_KTYPES  GSUNION_OBJ
#define GSI_MAP_VTYPES  GSUNION_NSINT

#include "GNUstepBase/GSIMap.h"

typedef	GSIMapNode_t	WeakRef;

static gs_mutex_t  	weakLock = GS_MUTEX_INIT_STATIC;

/* The weakRefs table contains weak references (nodes) for weak references
 * to any active objects.
 */
static GSIMapTable_t	weakRefs = { 0 };

/* The deallocated list contains the weak references (nodes) for objects
 * which have already been deallocated (so the references are now to nil).
 */
static WeakRef		*deallocated = NULL;


/* This must be called on startup before any weak references are taken.
 */
void
GSWeakInit()
{
  GS_MUTEX_LOCK(weakLock);
  if (0 == weakRefs.increment)
    {
      GSIMapInitWithZoneAndCapacity(
	&weakRefs, NSDefaultMallocZone(), 1024);
      persistentClasses[0] = [NXConstantString class];
    }
  GS_MUTEX_UNLOCK(weakLock);
}

/* Load from a weak pointer and return whether this really was a weak
 * reference or a strong (not deallocatable) object in a weak pointer.
 * The object will be stored in 'obj' and the weak reference in 'ref',
 * if one exists.
 */
inline static BOOL
loadWeakPointer(id *addr, id *obj, WeakRef **ref)
{
  id	oldObj = *addr;

  if (nil == oldObj)
    {
      *ref = NULL;
      *obj = nil;
      return NO;
    }
  if (*(void**)oldObj == (void*)&WeakRefClass)
    {
      *ref = (WeakRef*)oldObj;
      *obj = (*ref)->key.obj;
      return YES;
    }
  *ref = NULL;
  *obj = oldObj;
  return NO;
}

__attribute__((always_inline))
static inline BOOL
weakRefRelease(WeakRef *ref)
{
  ref->value.nsi--;
  if (ref->value.nsi == 0)
    {
      if (nil == ref->key.obj)
	{
	  /* The object was already deallocated so we must remove this
	   * reference from the deallocated list.
	   */
	  if (deallocated == ref)
	    {
	      deallocated = ref->nextInBucket;
	    }
	  else
	    {
	      WeakRef	*tmp = deallocated;

	      while (tmp->nextInBucket != 0)
		{
		  if (tmp->nextInBucket == ref)
		    {
		      tmp->nextInBucket = ref->nextInBucket;
		      break;
		    }
		  tmp = tmp->nextInBucket;
		}
	    }
	  ref->nextInBucket = weakRefs.freeNodes;
          weakRefs.freeNodes = ref;
	}
      else
	{
	  GSIMapBucket  bucket = GSIMapBucketForKey(&weakRefs, ref->key);

	  GSIMapRemoveNodeFromMap(&weakRefs, bucket, ref);
	  GSIMapFreeNode(&weakRefs, ref);
	}
      return YES;
    }
  return NO;
}

/* We should record the fact that the object has weak references (unless
 * it is a persistent one).
 * Return YES if the object is persistent and should not have weak references,
 * NO otherwise.
 */
static BOOL
setObjectHasWeakRefs(id obj)
{
  BOOL isPersistent = isPersistentObject(obj);

  if (NO == isPersistent)
    {
      /* FIXME ... for performance we should mark the object as having
       * weak references and we should check that in objc_delete_weak_refs()
       */
    }
  return isPersistent;
}

static WeakRef *
incrementWeakRefCount(id obj)
{       
  GSIMapKey	key;
  GSIMapBucket  bucket;
  WeakRef 	*ref;

  /* Mark the instance as having had a weak reference at some point.
   * This information is used when the instance is deallocated.
   */
  GSPrivateMarkedWeak(obj, YES);
  
  key.obj = obj;
  bucket = GSIMapBucketForKey(&weakRefs, key);
  ref = GSIMapNodeForKeyInBucket(&weakRefs, bucket, key);
  if (NULL == ref)
    {
      ref = GSIMapGetNode(&weakRefs);

      ref->key.obj = obj;
      ref->value.nsi = 1;
      GSIMapAddNodeToBucket(bucket, ref);
      weakRefs.nodeCount++;
      return ref;
    }
  ref->value.nsi++;

  return ref;
}

id
objc_storeWeak(id *addr, id obj)
{
  WeakRef	*oldRef;
  id 		old;
  BOOL 		isGlobalObject;

  GS_MUTEX_LOCK(weakLock);
  loadWeakPointer(addr, &old, &oldRef);
  /* If the old and new values are the same (and we are not setting a nil
   * value to destroy an existing weak reference), then we don't need to
   * do anything.
   */
  if ((obj != nil || oldRef == NULL) && old == obj)
    {
      GS_MUTEX_UNLOCK(weakLock);
      return obj;
    }
  isGlobalObject = setObjectHasWeakRefs(obj);

  /* If we old ref exists, decrement its reference count.  This may also
   * delete the weak reference from the map.
   */
  if (oldRef != NULL)
    {
      weakRefRelease(oldRef);
    }

  /* If we're storing nil, then just write a null pointer.
   */
  if (nil == obj)
    {
      *addr = obj;
    }
  else if (isGlobalObject)
    {
      /* If this is a global object, it's never deallocated,
       * so we don't make this a weak reference.
       */
      *addr = obj;
    }
  else if ([obj retainCount] == 0)
    {
      /* The object is being deallocated ... we must store nil.
       */
      *addr = obj = nil;
    }
  else
    {
      *addr = (id)incrementWeakRefCount(obj);
    }
  GS_MUTEX_UNLOCK(weakLock);
  return obj;
}

/* Function called when objects are deallocated
 */
BOOL
objc_delete_weak_refs(id obj)
{
  GSIMapKey	key;
  GSIMapBucket  bucket;
  WeakRef 	*ref;

  /* For performance we should have marked the object as having
   * weak references and we check that in order to avoid the cost
   * of the map table lookup when it's not needed.
   */
  if (NO == GSPrivateMarkedWeak(obj, NO))
    {
      return NO;
    }

  key.obj = obj;
  GS_MUTEX_LOCK(weakLock);
  bucket = GSIMapBucketForKey(&weakRefs, key);
  ref = GSIMapNodeForKeyInBucket(&weakRefs, bucket, key);
  if (ref)
    {
      GSIMapRemoveNodeFromBucket(bucket, ref);
      ref->key.obj = nil;
      weakRefs.nodeCount--;
      /* The object is deallocated but there are still weak references
       * to it so we put the weak reference node in the deallocated list.
       */
      ref->nextInBucket = deallocated;
      deallocated = ref;
    }
  GS_MUTEX_UNLOCK(weakLock);
  return YES;
}

id
objc_loadWeakRetained(id *addr)
{
  id 		obj;
  WeakRef 	*ref;

  GS_MUTEX_LOCK(weakLock);

  /* If this is not actually a weak pointer, return the object directly.
   */
  if (!loadWeakPointer(addr, &obj, &ref))
    {
      GS_MUTEX_UNLOCK(weakLock);
      return obj;
    }

  if (nil == obj)
    {
      /* The object has been destroed so we should remove the weak
       * reference to it.
       */
      if (ref != NULL)
	{
	  weakRefRelease(ref);
	  *addr = nil;
	}
      GS_MUTEX_UNLOCK(weakLock);
      return nil;
    }

  obj = [obj retain];
  GS_MUTEX_UNLOCK(weakLock);
  return obj;
}

id
objc_loadWeak(id *object)
{
  return [objc_loadWeakRetained(object) autorelease];
}

void
objc_copyWeak(id *dest, id *src)
{
  /* Don't retain or release.
   * 'src' is a valid pointer to a __weak pointer or nil.
   * 'dest' is a valid pointer to uninitialised memory.
   * After this operation, 'dest' should contain whatever 'src' contained.
   */
  id 		obj;
  WeakRef 	*srcRef;

  GS_MUTEX_LOCK(weakLock);
  loadWeakPointer(src, &obj, &srcRef);
  *dest = *src;
  if (srcRef)
    {
      srcRef->value.nsi++;
    }
  GS_MUTEX_UNLOCK(weakLock);
}

void
objc_moveWeak(id *dest, id *src)
{
  /* Don't retain or release.
   * 'dest' is a valid pointer to uninitialized memory.
   * 'src' is a valid pointer to a __weak pointer.
   * This operation moves from *src to *dest and must be atomic with respect
   * to other stores to *src via 'objc_storeWeak'.
   */
  GS_MUTEX_LOCK(weakLock);
  *dest = *src;
  *src = nil;
  GS_MUTEX_UNLOCK(weakLock);
}

void
objc_destroyWeak(id *obj)
{
  WeakRef	*oldRef;
  id 		old;

  GS_MUTEX_LOCK(weakLock);
  loadWeakPointer(obj, &old, &oldRef);
  /* If the old ref exists, decrement its reference count.
   * This may also remove the weak reference from the map.
   */
  if (oldRef != NULL)
    {
      weakRefRelease(oldRef);
    }
  GS_MUTEX_UNLOCK(weakLock);
}

id
objc_initWeak(id *addr, id obj)
{
  BOOL 		isGlobalObject;

  if (nil == obj)
    {
      *addr = nil;
      return nil;
    }

  GS_MUTEX_LOCK(weakLock);
  isGlobalObject = setObjectHasWeakRefs(obj);
  if (isGlobalObject)
    {
      *addr = obj;
      GS_MUTEX_UNLOCK(weakLock);
      return obj;
    }

  if ([obj retainCount] == 0)
    {
      *addr = nil;
      GS_MUTEX_UNLOCK(weakLock);
      return nil;
    }
  if (nil != obj)
    {
      *(WeakRef**)addr = incrementWeakRefCount(obj);
    }
  GS_MUTEX_UNLOCK(weakLock);
  return obj;
}