-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSObject.m
More file actions
2682 lines (2381 loc) · 70.5 KB
/
NSObject.m
File metadata and controls
2682 lines (2381 loc) · 70.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
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
809
810
811
812
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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** Implementation of NSObject for GNUStep
Copyright (C) 1994-2017 Free Software Foundation, Inc.
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
Date: August 1994
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
<title>NSObject class reference</title>
$Date$ $Revision$
*/
/* On some versions of mingw we need to work around bad function declarations
* by defining them away and doing the declarations ourself later.
*/
#ifndef _WIN64
#define InterlockedIncrement BadInterlockedIncrement
#define InterlockedDecrement BadInterlockedDecrement
#endif
#import "common.h"
#include <objc/Protocol.h>
#import "Foundation/NSMethodSignature.h"
#import "Foundation/NSInvocation.h"
#import "Foundation/NSLock.h"
#import "Foundation/NSAutoreleasePool.h"
#import "Foundation/NSArray.h"
#import "Foundation/NSException.h"
#import "Foundation/NSHashTable.h"
#import "Foundation/NSPortCoder.h"
#import "Foundation/NSDistantObject.h"
#import "Foundation/NSThread.h"
#import "Foundation/NSNotification.h"
#import "Foundation/NSMapTable.h"
#import "Foundation/NSUserDefaults.h"
#import "GNUstepBase/GSLocale.h"
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#import "GSPThread.h"
#if defined(HAVE_SYS_SIGNAL_H)
# include <sys/signal.h>
#elif defined(HAVE_SIGNAL_H)
# include <signal.h>
#endif
#if __GNUC__ >= 4
#if defined(__FreeBSD__)
#include <fenv.h>
#endif
#endif // __GNUC__
#define IN_NSOBJECT_M 1
#import "GSPrivate.h"
#ifdef __GNUSTEP_RUNTIME__
#include <objc/capabilities.h>
#include <objc/hooks.h>
#ifdef OBJC_CAP_ARC
#include <objc/objc-arc.h>
#endif
#endif
/* platforms which do not support weak */
#if defined (__WIN32)
#define WEAK_ATTRIBUTE
#else
/* all platforms which support weak */
#define WEAK_ATTRIBUTE __attribute__((weak))
#endif
/* When this is `YES', every call to release/autorelease, checks to
make sure isn't being set up to release itself too many times.
This does not need mutex protection. */
static BOOL double_release_check_enabled = NO;
/* The Class responsible for handling autorelease's. This does not
need mutex protection, since it is simply a pointer that gets read
and set. */
static id autorelease_class = nil;
static SEL autorelease_sel;
static IMP autorelease_imp;
static SEL finalize_sel;
static IMP finalize_imp;
static Class NSConstantStringClass;
@class NSDataMalloc;
@class NSMutableDataMalloc;
GS_ROOT_CLASS @interface NSZombie
{
Class isa;
}
- (Class) class;
- (void) forwardInvocation: (NSInvocation*)anInvocation;
- (NSMethodSignature*) methodSignatureForSelector: (SEL)aSelector;
@end
@interface GSContentAccessingProxy : NSProxy
{
NSObject<NSDiscardableContent> *object;
}
- (id) initWithObject: (id)anObject;
@end
/* allocationLock is needed when for protecting the map table of zombie
* information and if atomic operations are not available.
*/
static pthread_mutex_t allocationLock = PTHREAD_MUTEX_INITIALIZER;
BOOL NSZombieEnabled = NO;
BOOL NSDeallocateZombies = NO;
@class NSZombie;
static Class zombieClass = Nil;
static NSMapTable *zombieMap = 0;
#ifndef OBJC_CAP_ARC
static void GSMakeZombie(NSObject *o, Class c)
{
object_setClass(o, zombieClass);
if (0 != zombieMap)
{
pthread_mutex_lock(&allocationLock);
if (0 != zombieMap)
{
NSMapInsert(zombieMap, (void*)o, (void*)c);
}
pthread_mutex_unlock(&allocationLock);
}
}
#endif
static void GSLogZombie(id o, SEL sel)
{
Class c = 0;
if (0 != zombieMap)
{
pthread_mutex_lock(&allocationLock);
if (0 != zombieMap)
{
c = NSMapGet(zombieMap, (void*)o);
}
pthread_mutex_unlock(&allocationLock);
}
if (c == 0)
{
NSLog(@"*** -[??? %@]: message sent to deallocated instance %p",
NSStringFromSelector(sel), o);
}
else
{
NSLog(@"*** -[%@ %@]: message sent to deallocated instance %p",
c, NSStringFromSelector(sel), o);
}
if (GSPrivateEnvironmentFlag("CRASH_ON_ZOMBIE", NO) == YES)
{
abort();
}
}
/*
* Reference count and memory management
* Reference counts for object are stored
* with the object.
* The zone in which an object has been
* allocated is stored with the object.
*/
/* Now, if we are on a platform where we know how to do atomic
* read, increment, and decrement, then we define the GSATOMICREAD
* macro and macros or functions to increment/decrement.
* The presence of the GSATOMICREAD macro is used later to determine
* whether to attempt atomic operations or to use locking for the
* retain/release mechanism.
* The GSAtomicIncrement() and GSAtomicDecrement() functions take a
* pointer to a 32bit integer as an argument, increment/decrement the
* value pointed to, and return the result.
*/
#ifdef GSATOMICREAD
#undef GSATOMICREAD
#endif
/* Traditionally, GNUstep has been using a 32bit reference count in front
* of the object. The automatic reference counting implementation in
* libobjc2 uses an intptr_t instead, so NSObject will only be compatible
* with ARC if either of the following apply:
*
* a) sizeof(intptr_t) == sizeof(int32_t)
* b) we can provide atomic operations on pointer sized values, allowing
* us to extend the refcount to intptr_t.
*/
#ifdef GS_ARC_COMPATIBLE
#undef GS_ARC_COMPATIBLE
#endif
#if defined(__llvm__) || (defined(USE_ATOMIC_BUILTINS) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)))
/* Use the GCC atomic operations with recent GCC versions */
typedef intptr_t volatile *gsatomic_t;
typedef intptr_t gsrefcount_t;
#define GSATOMICREAD(X) (*(X))
#define GSAtomicIncrement(X) __sync_add_and_fetch(X, 1)
#define GSAtomicDecrement(X) __sync_sub_and_fetch(X, 1)
#define GS_ARC_COMPATIBLE 1
#elif defined(_WIN32)
/* Set up atomic read, increment and decrement for mswindows
*/
typedef int32_t volatile *gsatomic_t;
typedef int32_t gsrefcount_t;
#ifndef _WIN64
#undef InterlockedIncrement
#undef InterlockedDecrement
LONG WINAPI InterlockedIncrement(LONG volatile *);
LONG WINAPI InterlockedDecrement(LONG volatile *);
#endif
#define GSATOMICREAD(X) (*(X))
#define GSAtomicIncrement(X) InterlockedIncrement(X)
#define GSAtomicDecrement(X) InterlockedDecrement(X)
#elif defined(__linux__) && (defined(__i386__) || defined(__x86_64__))
/* Set up atomic read, increment and decrement for intel style linux
*/
typedef int32_t volatile *gsatomic_t;
typedef int32_t gsrefcount_t;
#define GSATOMICREAD(X) (*(X))
static __inline__ int32_t
GSAtomicIncrement(gsatomic_t X)
{
register int32_t tmp;
__asm__ __volatile__ (
"movl $1, %0\n"
"lock xaddl %0, %1"
:"=r" (tmp), "=m" (*X)
:"r" (tmp), "m" (*X)
:"memory" );
return tmp + 1;
}
static __inline__ int32_t
GSAtomicDecrement(gsatomic_t X)
{
register int32_t tmp;
__asm__ __volatile__ (
"movl $1, %0\n"
"negl %0\n"
"lock xaddl %0, %1"
:"=r" (tmp), "=m" (*X)
:"r" (tmp), "m" (*X)
:"memory" );
return tmp - 1;
}
#elif defined(__PPC__) || defined(__POWERPC__)
typedef int32_t volatile *gsatomic_t;
typedef int32_t gsrefcount_t;
#define GSATOMICREAD(X) (*(X))
static __inline__ int32_t
GSAtomicIncrement(gsatomic_t X)
{
int32_t tmp;
__asm__ __volatile__ (
"0:"
"lwarx %0,0,%1 \n"
"addic %0,%0,1 \n"
"stwcx. %0,0,%1 \n"
"bne- 0b \n"
:"=&r" (tmp)
:"r" (X)
:"cc", "memory");
return tmp;
}
static __inline__ int32_t
GSAtomicDecrement(gsatomic_t X)
{
int32_t tmp;
__asm__ __volatile__ (
"0:"
"lwarx %0,0,%1 \n"
"addic %0,%0,-1 \n"
"stwcx. %0,0,%1 \n"
"bne- 0b \n"
:"=&r" (tmp)
:"r" (X)
:"cc", "memory");
return tmp;
}
#elif defined(__m68k__)
typedef int32_t volatile *gsatomic_t;
typedef int32_t gsrefcount_t;
#define GSATOMICREAD(X) (*(X))
static __inline__ int32_t
GSAtomicIncrement(gsatomic_t X)
{
__asm__ __volatile__ (
"addq%.l %#1, %0"
:"=m" (*X));
return *X;
}
static __inline__ int32_t
GSAtomicDecrement(gsatomic_t X)
{
__asm__ __volatile__ (
"subq%.l %#1, %0"
:"=m" (*X));
return *X;
}
#elif defined(__mips__)
typedef int32_t volatile *gsatomic_t;
typedef int32_t gsrefcount_t;
#define GSATOMICREAD(X) (*(X))
static __inline__ int32_t
GSAtomicIncrement(gsatomic_t X)
{
int32_t tmp;
__asm__ __volatile__ (
#if !defined(__mips64)
" .set mips2 \n"
#endif
"0: ll %0, %1 \n"
" addiu %0, 1 \n"
" sc %0, %1 \n"
" beqz %0, 0b \n"
:"=&r" (tmp), "=m" (*X));
return tmp;
}
static __inline__ int32_t
GSAtomicDecrement(gsatomic_t X)
{
int32_t tmp;
__asm__ __volatile__ (
#if !defined(__mips64)
" .set mips2 \n"
#endif
"0: ll %0, %1 \n"
" addiu %0, -1 \n"
" sc %0, %1 \n"
" beqz %0, 0b \n"
:"=&r" (tmp), "=m" (*X));
return tmp;
}
#endif
#if !defined(GSATOMICREAD)
typedef int gsrefcount_t; // No atomics, use a simple integer
/* Having just one allocationLock for all leads to lock contention
* if there are lots of threads doing lots of retain/release calls.
* To alleviate this, instead of a single
* allocationLock for all objects, we divide the object space into
* chunks, each with its own lock. The chunk is selected by shifting
* off the low-order ALIGNBITS of the object's pointer (these bits
* are presumably always zero) and take
* the low-order LOCKBITS of the result to index into a table of locks.
*/
#define LOCKBITS 5
#define LOCKCOUNT (1<<LOCKBITS)
#define LOCKMASK (LOCKCOUNT-1)
#define ALIGNBITS 3
static pthread_mutex_t allocationLocks[LOCKCOUNT];
static inline pthread_mutex_t *GSAllocationLockForObject(id p)
{
NSUInteger i = ((((NSUInteger)(uintptr_t)p) >> ALIGNBITS) & LOCKMASK);
return &allocationLocks[i];
}
#endif
#ifndef GS_ARC_COMPATIBLE
/*
* If we haven't previously declared that we can work in fast-ARC mode,
* check whether a point is 32bit (4 bytes) wide, which also enables ARC
* integration.
*/
# if __SIZEOF_POINTER__ == 4
# define GS_ARC_COMPATIBLE 1
# endif
#endif
#if defined(__GNUC__) && __GNUC__ < 4
#define __builtin_offsetof(s, f) (uintptr_t)(&(((s*)0)->f))
#endif
#define alignof(type) __builtin_offsetof(struct { const char c; type member; }, member)
/*
* Define a structure to hold information that is held locally
* (before the start) in each object.
*/
typedef struct obj_layout_unpadded {
gsrefcount_t retained;
} unp;
#define UNP sizeof(unp)
/* GCC provides a defined value for the largest alignment required on a
* machine, and we must lay objects out to that alignment.
* For compilers that don't define it, we try to pick a likely value.
*/
#ifndef __BIGGEST_ALIGNMENT__
#define __BIGGEST_ALIGNMENT__ (SIZEOF_VOIDP * 2)
#endif
/*
* Now do the REAL version - using the other version to determine
* what padding (if any) is required to get the alignment of the
* structure correct.
*/
struct obj_layout {
char padding[__BIGGEST_ALIGNMENT__ - ((UNP % __BIGGEST_ALIGNMENT__)
? (UNP % __BIGGEST_ALIGNMENT__) : __BIGGEST_ALIGNMENT__)];
gsrefcount_t retained;
};
typedef struct obj_layout *obj;
/*
* These symbols are provided by newer versions of the GNUstep Objective-C
* runtime. When linked against an older version, we will use our internal
* versions.
*/
GS_IMPORT WEAK_ATTRIBUTE
BOOL objc_release_fast_no_destroy_np(id anObject);
GS_IMPORT WEAK_ATTRIBUTE
void objc_release_fast_np(id anObject);
GS_IMPORT WEAK_ATTRIBUTE
size_t object_getRetainCount_np(id anObject);
GS_IMPORT WEAK_ATTRIBUTE
id objc_retain_fast_np(id anObject);
static BOOL objc_release_fast_no_destroy_internal(id anObject)
{
if (double_release_check_enabled)
{
NSUInteger release_count;
NSUInteger retain_count = [anObject retainCount];
release_count = [autorelease_class autoreleaseCountForObject: anObject];
if (release_count >= retain_count)
[NSException raise: NSGenericException
format: @"Release would release object too many times."];
}
{
#if defined(GSATOMICREAD)
gsrefcount_t result;
result = GSAtomicDecrement((gsatomic_t)&(((obj)anObject)[-1].retained));
if (result < 0)
{
if (result != -1)
{
[NSException raise: NSInternalInconsistencyException
format: @"NSDecrementExtraRefCount() decremented too far"];
}
/* The counter has become negative so it must have been zero.
* We reset it and return YES ... in a correctly operating
* process we know we can safely reset back to zero without
* worrying about atomicity, since there can be no other
* thread accessing the object (or its reference count would
* have been greater than zero)
*/
(((obj)anObject)[-1].retained) = 0;
# ifdef OBJC_CAP_ARC
objc_delete_weak_refs(anObject);
# endif
return YES;
}
#else /* GSATOMICREAD */
pthread_mutex_t *theLock = GSAllocationLockForObject(anObject);
pthread_mutex_lock(theLock);
if (((obj)anObject)[-1].retained == 0)
{
# ifdef OBJC_CAP_ARC
objc_delete_weak_refs(anObject);
# endif
pthread_mutex_unlock(theLock);
return YES;
}
else
{
((obj)anObject)[-1].retained--;
pthread_mutex_unlock(theLock);
return NO;
}
#endif /* GSATOMICREAD */
}
return NO;
}
static BOOL release_fast_no_destroy(id anObject)
{
#ifdef __GNUSTEP_RUNTIME__
if (objc_release_fast_no_destroy_np)
{
return objc_release_fast_no_destroy_np(anObject);
}
else
#endif
{
return objc_release_fast_no_destroy_internal(anObject);
}
}
static void objc_release_fast_np_internal(id anObject)
{
if (release_fast_no_destroy(anObject))
{
[anObject dealloc];
}
}
static void release_fast(id anObject)
{
#ifdef __GNUSTEP_RUNTIME__
if (objc_release_fast_np)
{
objc_release_fast_np(anObject);
}
else
#endif
{
objc_release_fast_np_internal(anObject);
}
}
/**
* Examines the extra reference count for the object and, if non-zero
* decrements it, otherwise leaves it unchanged.<br />
* Returns a flag to say whether the count was zero
* (and hence whether the extra reference count was decremented).<br />
*/
inline BOOL
NSDecrementExtraRefCountWasZero(id anObject)
{
return release_fast_no_destroy(anObject);
}
size_t object_getRetainCount_np_internal(id anObject)
{
return ((obj)anObject)[-1].retained + 1;
}
size_t getRetainCount(id anObject)
{
#ifdef __GNUSTEP_RUNTIME__
if (object_getRetainCount_np)
{
return object_getRetainCount_np(anObject);
}
else
#endif
{
return object_getRetainCount_np_internal(anObject);
}
}
/**
* Return the extra reference count of anObject (a value in the range
* from 0 to the maximum unsigned integer value minus one).<br />
* The retain count for an object is this value plus one.
*/
inline NSUInteger
NSExtraRefCount(id anObject)
{
return getRetainCount(anObject) - 1;
}
/**
* Increments the extra reference count for anObject.<br />
* The GNUstep version raises an exception if the reference count
* would be incremented to too large a value.<br />
* This is used by the [NSObject-retain] method.
*/
static id objc_retain_fast_np_internal(id anObject)
{
BOOL tooFar = NO;
#if defined(GSATOMICREAD)
/* I've seen comments saying that some platforms only support up to
* 24 bits in atomic locking, so raise an exception if we try to
* go beyond 0xfffffe.
*/
if (GSAtomicIncrement((gsatomic_t)&(((obj)anObject)[-1].retained))
> 0xfffffe)
{
tooFar = YES;
}
#else /* GSATOMICREAD */
pthread_mutex_t *theLock = GSAllocationLockForObject(anObject);
pthread_mutex_lock(theLock);
if (((obj)anObject)[-1].retained > 0xfffffe)
{
tooFar = YES;
}
else
{
((obj)anObject)[-1].retained++;
}
pthread_mutex_unlock(theLock);
#endif /* GSATOMICREAD */
if (YES == tooFar)
{
static NSHashTable *overrun = nil;
/* We store this instance in a hash table so that we will only raise
* an exception for it once (and can therefore expect to log the instance
* as part of the exception derscription without recursion).
* NB. The hash table does not retain the object, so the code in the
* lock protected region below should be safe anyway.
*/
[gnustep_global_lock lock];
if (nil == overrun)
{
overrun = NSCreateHashTable(NSNonRetainedObjectHashCallBacks, 0);
}
if (0 == NSHashGet(overrun, anObject))
{
NSHashInsert(overrun, anObject);
}
else
{
tooFar = NO;
}
[gnustep_global_lock lock];
if (YES == tooFar)
{
NSString *base;
base = [NSString stringWithFormat: @"<%s: %p>",
class_getName([anObject class]), anObject];
[NSException raise: NSInternalInconsistencyException
format: @"NSIncrementExtraRefCount() asked to increment too far"
@" for %@ - %@", base, anObject];
}
}
return anObject;
}
static id retain_fast(id anObject)
{
#ifdef __GNUSTEP_RUNTIME__
if (objc_retain_fast_np)
{
return objc_retain_fast_np(anObject);
}
else
#endif
{
return objc_retain_fast_np_internal(anObject);
}
}
/**
* Increments the extra reference count for anObject.<br />
* The GNUstep version raises an exception if the reference count
* would be incremented to too large a value.<br />
* This is used by the [NSObject-retain] method.
*/
inline void
NSIncrementExtraRefCount(id anObject)
{
retain_fast(anObject);
}
#ifndef NDEBUG
#define AADD(c, o) GSDebugAllocationAdd(c, o)
#define AREM(c, o) GSDebugAllocationRemove(c, o)
#else
#define AADD(c, o)
#define AREM(c, o)
#endif
#ifndef OBJC_CAP_ARC
static SEL cxx_construct, cxx_destruct;
/**
* Calls the C++ constructors for this object, starting with the ones declared
* in aClass. The compiler generates two methods on Objective-C++ classes that
* static instances of C++ classes as ivars. These are -.cxx_construct and
* -.cxx_destruct. The -.cxx_construct methods must be called in order from
* the root class to all subclasses, to ensure that subclass ivars are
* initialised after superclass ones. This must be done in reverse for
* destruction.
*
* This function first calls itself recursively on the superclass, to get the
* IMP for the constructor function in the superclass. It then compares the
* construct method for this class with the one that's already been called,
* and calls it if it's new.
*/
static IMP
callCXXConstructors(Class aClass, id anObject)
{
IMP constructor = 0;
if (class_respondsToSelector(aClass, cxx_construct))
{
IMP calledConstructor =
callCXXConstructors(class_getSuperclass(aClass), anObject);
constructor = class_getMethodImplementation(aClass, cxx_construct);
if (calledConstructor != constructor)
{
constructor(anObject, cxx_construct);
}
}
return constructor;
}
#endif
/*
* Now do conditional compilation of memory allocation functions
* depending on what information (if any) we are storing before
* the start of each object.
*/
// FIXME rewrite object allocation to use class_createInstance when we
// are using libobjc2.
inline id
NSAllocateObject(Class aClass, NSUInteger extraBytes, NSZone *zone)
{
id new;
#ifdef OBJC_CAP_ARC
if ((new = class_createInstance(aClass, extraBytes)) != nil)
{
AADD(aClass, new);
}
#else
int size;
NSCAssert((!class_isMetaClass(aClass)), @"Bad class for new object");
size = class_getInstanceSize(aClass) + extraBytes + sizeof(struct obj_layout);
if (zone == 0)
{
zone = NSDefaultMallocZone();
}
new = NSZoneMalloc(zone, size);
if (new != nil)
{
memset (new, 0, size);
new = (id)&((obj)new)[1];
object_setClass(new, aClass);
AADD(aClass, new);
}
/* Don't bother doing this in a thread-safe way, because the cost of locking
* will be a lot more than the cost of doing the same call in two threads.
* The returned selector will persist and the runtime will ensure that both
* calls return the same selector, so we don't need to bother doing it
* ourselves.
*/
if (0 == cxx_construct)
{
cxx_construct = sel_registerName(".cxx_construct");
cxx_destruct = sel_registerName(".cxx_destruct");
}
callCXXConstructors(aClass, new);
#endif
return new;
}
inline void
NSDeallocateObject(id anObject)
{
Class aClass = object_getClass(anObject);
if ((anObject != nil) && !class_isMetaClass(aClass))
{
#ifndef OBJC_CAP_ARC
obj o = &((obj)anObject)[-1];
NSZone *z = NSZoneFromPointer(o);
#endif
/* Call the default finalizer to handle C++ destructors.
*/
(*finalize_imp)(anObject, finalize_sel);
AREM(aClass, (id)anObject);
if (NSZombieEnabled == YES)
{
#ifdef OBJC_CAP_ARC
if (0 != zombieMap)
{
pthread_mutex_lock(&allocationLock);
if (0 != zombieMap)
{
NSMapInsert(zombieMap, (void*)anObject, (void*)aClass);
}
pthread_mutex_unlock(&allocationLock);
}
if (NSDeallocateZombies == YES)
{
object_dispose(anObject);
}
else
{
object_setClass(anObject, zombieClass);
}
#else
GSMakeZombie(anObject, aClass);
if (NSDeallocateZombies == YES)
{
NSZoneFree(z, o);
}
#endif
}
else
{
#ifdef OBJC_CAP_ARC
object_dispose(anObject);
#else
object_setClass((id)anObject, (Class)(void*)0xdeadface);
NSZoneFree(z, o);
#endif
}
}
return;
}
BOOL
NSShouldRetainWithZone (NSObject *anObject, NSZone *requestedZone)
{
return (!requestedZone || requestedZone == NSDefaultMallocZone()
|| [anObject zone] == requestedZone);
}
/**
* <p>
* <code>NSObject</code> is the root class (a root class is
* a class with no superclass) of the GNUstep base library
* class hierarchy, so all classes normally inherit from
* <code>NSObject</code>. There is an exception though:
* <code>NSProxy</code> (which is used for remote messaging)
* does not inherit from <code>NSObject</code>.
* </p>
* <p>
* Unless you are really sure of what you are doing, all
* your own classes should inherit (directly or indirectly)
* from <code>NSObject</code> (or in special cases from
* <code>NSProxy</code>). <code>NSObject</code> provides
* the basic common functionality shared by all GNUstep
* classes and objects.
* </p>
* <p>
* The essential methods which must be implemented by all
* classes for their instances to be usable within GNUstep
* are declared in a separate protocol, which is the
* <code>NSObject</code> protocol. Both
* <code>NSObject</code> and <code>NSProxy</code> conform to
* this protocol, which means all objects in a GNUstep
* application will conform to this protocol (btw, if you
* don't find a method of <code>NSObject</code> you are
* looking for in this documentation, make sure you also
* look into the documentation for the <code>NSObject</code>
* protocol).
* </p>
* <p>
* Theoretically, in special cases you might need to
* implement a new root class. If you do, you need to make
* sure that your root class conforms (at least) to the
* <code>NSObject</code> protocol, otherwise it will not
* interact correctly with the GNUstep framework. Said
* that, I must note that I have never seen a case in which
* a new root class is needed.
* </p>
* <p>
* <code>NSObject</code> is a root class, which implies that
* instance methods of <code>NSObject</code> are treated in
* a special way by the Objective-C runtime. This is an
* exception to the normal way messaging works with class
* and instance methods: if the Objective-C runtime can't
* find a class method for a class object, as a last resort
* it looks for an instance method of the root class with
* the same name, and executes it if it finds it. This
* means that instance methods of the root class (such as
* <code>NSObject</code>) can be performed by class objects
* which inherit from that root class ! This can only
* happen if the class doesn't have a class method with the
* same name, otherwise that method - of course - takes the
* precedence. Because of this exception,
* <code>NSObject</code>'s instance methods are written in
* such a way that they work both on <code>NSObject</code>'s
* instances and on class objects.
* </p>
*/
@implementation NSObject
#if defined(GS_ARC_COMPATIBLE)
- (void)_ARCCompliantRetainRelease {}
#endif
/**
* Semi-private function in libobjc2 that initialises the classes used for
* blocks.
*/
extern BOOL
objc_create_block_classes_as_subclasses_of(Class super);
#ifdef OBJC_CAP_ARC
static id gs_weak_load(id obj)
{
return [obj retainCount] > 0 ? obj : nil;
}
#endif
+ (void) load
{
#ifdef OBJC_CAP_ARC
_objc_weak_load = gs_weak_load;
#endif
objc_create_block_classes_as_subclasses_of(self);
}
+ (void) initialize
{
if (self == [NSObject class])
{
#ifdef _WIN32
{
// See libgnustep-base-entry.m
extern void gnustep_base_socket_init(void);
gnustep_base_socket_init();
}
#else /* _WIN32 */
#ifdef SIGPIPE
/*
* If SIGPIPE is not handled or ignored, we will abort on any attempt
* to write to a pipe/socket that has been closed by the other end!
* We therefore need to ignore the signal if nothing else is already
* handling it.
*/
#ifdef HAVE_SIGACTION
{
struct sigaction act;
if (sigaction(SIGPIPE, 0, &act) == 0)
{
if (act.sa_handler == SIG_DFL)