︙ | | |
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
-
+
-
+
|
};
struct pre_mem {
struct pre_mem *prev, *next;
id owner;
};
#define PRE_IVAR_ALIGN ((sizeof(struct pre_ivar) + \
#define PRE_IVARS_ALIGN ((sizeof(struct pre_ivar) + \
(__BIGGEST_ALIGNMENT__ - 1)) & ~(__BIGGEST_ALIGNMENT__ - 1))
#define PRE_IVAR ((struct pre_ivar*)(void*)((char*)self - PRE_IVAR_ALIGN))
#define PRE_IVARS ((struct pre_ivar*)(void*)((char*)self - PRE_IVARS_ALIGN))
#define PRE_MEM_ALIGN ((sizeof(struct pre_mem) + \
(__BIGGEST_ALIGNMENT__ - 1)) & ~(__BIGGEST_ALIGNMENT__ - 1))
#define PRE_MEM(mem) ((struct pre_mem*)(void*)((char*)mem - PRE_MEM_ALIGN))
static struct {
Class isa;
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
|
instanceSize = class_getInstanceSize(class);
if OF_UNLIKELY (extraAlignment > 0)
extraAlignment = ((instanceSize + extraAlignment - 1) &
~(extraAlignment - 1)) - extraAlignment;
instance = malloc(PRE_IVAR_ALIGN + instanceSize +
instance = malloc(PRE_IVARS_ALIGN + instanceSize +
extraAlignment + extraSize);
if OF_UNLIKELY (instance == nil) {
alloc_failed_exception.isa = [OFAllocFailedException class];
@throw (id)&alloc_failed_exception;
}
((struct pre_ivar*)instance)->retainCount = 1;
((struct pre_ivar*)instance)->firstMem = NULL;
((struct pre_ivar*)instance)->lastMem = NULL;
#if !defined(OF_ATOMIC_OPS) && defined(OF_THREADS)
if OF_UNLIKELY (!of_spinlock_new(
&((struct pre_ivar*)instance)->retainCountSpinlock)) {
free(instance);
@throw [OFInitializationFailedException
exceptionWithClass: class];
}
#endif
instance = (OFObject*)((char*)instance + PRE_IVAR_ALIGN);
instance = (OFObject*)((char*)instance + PRE_IVARS_ALIGN);
memset(instance, 0, instanceSize);
if (!objc_constructInstance(class, instance)) {
free((char*)instance - PRE_IVAR_ALIGN);
free((char*)instance - PRE_IVARS_ALIGN);
@throw [OFInitializationFailedException
exceptionWithClass: class];
}
if OF_UNLIKELY (extra != NULL)
*extra = (char*)instance + instanceSize + extraAlignment;
|
︙ | | |
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
|
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
|
-
+
-
+
-
-
+
+
-
-
-
+
+
+
|
}
- (void*)allocMemoryWithSize: (size_t)size
{
void *pointer;
struct pre_mem *preMem;
if OF_UNLIKELY (size > SIZE_MAX - PRE_IVAR_ALIGN)
if OF_UNLIKELY (size > SIZE_MAX - PRE_IVARS_ALIGN)
@throw [OFOutOfRangeException exceptionWithClass: [self class]];
if OF_UNLIKELY ((pointer = malloc(PRE_MEM_ALIGN + size)) == NULL)
@throw [OFOutOfMemoryException exceptionWithClass: [self class]
requestedSize: size];
preMem = pointer;
preMem->owner = self;
preMem->prev = PRE_IVAR->lastMem;
preMem->prev = PRE_IVARS->lastMem;
preMem->next = NULL;
if OF_LIKELY (PRE_IVAR->lastMem != NULL)
PRE_IVAR->lastMem->next = preMem;
if OF_LIKELY (PRE_IVARS->lastMem != NULL)
PRE_IVARS->lastMem->next = preMem;
if OF_UNLIKELY (PRE_IVAR->firstMem == NULL)
PRE_IVAR->firstMem = preMem;
PRE_IVAR->lastMem = preMem;
if OF_UNLIKELY (PRE_IVARS->firstMem == NULL)
PRE_IVARS->firstMem = preMem;
PRE_IVARS->lastMem = preMem;
return (char*)pointer + PRE_MEM_ALIGN;
}
- (void*)allocMemoryWithSize: (size_t)size
count: (size_t)count
{
|
︙ | | |
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
|
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
|
-
-
-
-
+
+
+
+
|
if OF_UNLIKELY (preMem != PRE_MEM(pointer)) {
if OF_LIKELY (preMem->prev != NULL)
preMem->prev->next = preMem;
if OF_LIKELY (preMem->next != NULL)
preMem->next->prev = preMem;
if OF_UNLIKELY (PRE_IVAR->firstMem == PRE_MEM(pointer))
PRE_IVAR->firstMem = preMem;
if OF_UNLIKELY (PRE_IVAR->lastMem == PRE_MEM(pointer))
PRE_IVAR->lastMem = preMem;
if OF_UNLIKELY (PRE_IVARS->firstMem == PRE_MEM(pointer))
PRE_IVARS->firstMem = preMem;
if OF_UNLIKELY (PRE_IVARS->lastMem == PRE_MEM(pointer))
PRE_IVARS->lastMem = preMem;
}
return (char*)new + PRE_MEM_ALIGN;
}
- (void*)resizeMemory: (void*)pointer
size: (size_t)size
|
︙ | | |
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
|
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
|
-
-
-
-
+
+
+
+
-
+
-
-
-
+
+
+
-
+
-
-
+
+
-
+
-
-
-
+
+
+
-
+
|
pointer: pointer];
if OF_LIKELY (PRE_MEM(pointer)->prev != NULL)
PRE_MEM(pointer)->prev->next = PRE_MEM(pointer)->next;
if OF_LIKELY (PRE_MEM(pointer)->next != NULL)
PRE_MEM(pointer)->next->prev = PRE_MEM(pointer)->prev;
if OF_UNLIKELY (PRE_IVAR->firstMem == PRE_MEM(pointer))
PRE_IVAR->firstMem = PRE_MEM(pointer)->next;
if OF_UNLIKELY (PRE_IVAR->lastMem == PRE_MEM(pointer))
PRE_IVAR->lastMem = PRE_MEM(pointer)->prev;
if OF_UNLIKELY (PRE_IVARS->firstMem == PRE_MEM(pointer))
PRE_IVARS->firstMem = PRE_MEM(pointer)->next;
if OF_UNLIKELY (PRE_IVARS->lastMem == PRE_MEM(pointer))
PRE_IVARS->lastMem = PRE_MEM(pointer)->prev;
/* To detect double-free */
PRE_MEM(pointer)->owner = nil;
free(PRE_MEM(pointer));
}
- (id)forwardingTargetForSelector: (SEL)selector
{
return nil;
}
- retain
{
#if defined(OF_ATOMIC_OPS)
of_atomic_inc_32(&PRE_IVAR->retainCount);
of_atomic_inc_32(&PRE_IVARS->retainCount);
#elif defined(OF_THREADS)
OF_ENSURE(of_spinlock_lock(&PRE_IVAR->retainCountSpinlock));
PRE_IVAR->retainCount++;
OF_ENSURE(of_spinlock_unlock(&PRE_IVAR->retainCountSspinlock));
OF_ENSURE(of_spinlock_lock(&PRE_IVARS->retainCountSpinlock));
PRE_IVARS->retainCount++;
OF_ENSURE(of_spinlock_unlock(&PRE_IVARS->retainCountSspinlock));
#else
PRE_IVAR->retainCount++;
PRE_IVARS->retainCount++;
#endif
return self;
}
- (unsigned int)retainCount
{
assert(PRE_IVAR->retainCount >= 0);
return PRE_IVAR->retainCount;
assert(PRE_IVARS->retainCount >= 0);
return PRE_IVARS->retainCount;
}
- (void)release
{
#if defined(OF_ATOMIC_OPS)
if (of_atomic_dec_32(&PRE_IVAR->retainCount) <= 0)
if (of_atomic_dec_32(&PRE_IVARS->retainCount) <= 0)
[self dealloc];
#elif defined(OF_THREADS)
size_t c;
OF_ENSURE(of_spinlock_lock(&PRE_IVAR->retainCountSpinlock));
c = --PRE_IVAR->retainCount;
OF_ENSURE(of_spinlock_unlock(&PRE_IVAR->retainCountSpinlock));
OF_ENSURE(of_spinlock_lock(&PRE_IVARS->retainCountSpinlock));
c = --PRE_IVARS->retainCount;
OF_ENSURE(of_spinlock_unlock(&PRE_IVARS->retainCountSpinlock));
if (c == 0)
[self dealloc];
#else
if (--PRE_IVAR->retainCount == 0)
if (--PRE_IVARS->retainCount == 0)
[self dealloc];
#endif
}
- autorelease
{
return _objc_rootAutorelease(self);
|
︙ | | |
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
|
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
|
-
+
-
+
|
- (void)dealloc
{
struct pre_mem *iter;
objc_destructInstance(self);
iter = PRE_IVAR->firstMem;
iter = PRE_IVARS->firstMem;
while (iter != NULL) {
struct pre_mem *next = iter->next;
/*
* We can use owner as a sentinel to prevent exploitation in
* case there is a buffer underflow somewhere.
*/
if OF_UNLIKELY (iter->owner != self)
abort();
free(iter);
iter = next;
}
free((char*)self - PRE_IVAR_ALIGN);
free((char*)self - PRE_IVARS_ALIGN);
}
/* Required to use properties with the Apple runtime */
- copyWithZone: (void*)zone
{
if OF_UNLIKELY (zone != NULL)
@throw [OFNotImplementedException
|
︙ | | |