ObjFW  Diff

Differences From Artifact [e5c0e6c88e]:

To Artifact [6a6eeb3258]:

  • File src/OFObject.m — part of check-in [e1e7ffa903] at 2011-09-22 23:25:42 on branch trunk — Exceptions are now autoreleased.

    This is safe as an "exception loop" can't happen, since if allocating
    an exception fails, it throws an OFAllocFailedException which is
    preallocated and can always be thrown.

    So, the worst case would be that an autorelease of an exception fails,
    triggering an OFOutOfMemoryException for which there is no memory,
    resulting in an OFAllocFailedException to be thrown. (user: js, size: 27831) [annotate] [blame] [check-ins using]


97
98
99
100
101
102
103
104
105



106
107
108
109
110
111
112
97
98
99
100
101
102
103


104
105
106
107
108
109
110
111
112
113







-
-
+
+
+







#ifdef NEED_OBJC_PROPERTIES_INIT
extern BOOL objc_properties_init();
#endif

static void
enumeration_mutation_handler(id object)
{
	@throw [OFEnumerationMutationException newWithClass: [object class]
						     object: object];
	@throw [OFEnumerationMutationException
	    exceptionWithClass: [object class]
			object: object];
}

#ifndef HAVE_OBJC_ENUMERATIONMUTATION
void
objc_enumerationMutation(id object)
{
	enumeration_mutation_handler(object);
193
194
195
196
197
198
199
200


201
202
203
204
205
206
207
194
195
196
197
198
199
200

201
202
203
204
205
206
207
208
209







-
+
+







	((struct pre_ivar*)instance)->memoryChunksSize = 0;
	((struct pre_ivar*)instance)->retainCount = 1;

#if !defined(OF_ATOMIC_OPS)
	if (!of_spinlock_new(
	    &((struct pre_ivar*)instance)->retainCountSpinlock)) {
		free(instance);
		@throw [OFInitializationFailedException newWithClass: self];
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
	}
#endif

	instance = (OFObject*)((char*)instance + PRE_IVAR_ALIGN);
	memset(instance, 0, instanceSize);
	instance->isa = self;

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







-
-
+
+







-
-
+
+








-
-
+
+















-
-
+
+









-
-
+
+








+ (const char*)typeEncodingForInstanceSelector: (SEL)selector
{
#if defined(OF_OBJFW_RUNTIME)
	const char *ret;

	if ((ret = objc_get_type_encoding(self, selector)) == NULL)
		@throw [OFNotImplementedException newWithClass: self
						      selector: selector];
		@throw [OFNotImplementedException exceptionWithClass: self
							    selector: selector];

	return ret;
#elif defined(OF_OLD_GNU_RUNTIME)
	Method_t m;

	if ((m = class_get_instance_method(self, selector)) == NULL ||
	    m->method_types == NULL)
		@throw [OFNotImplementedException newWithClass: self
						      selector: selector];
		@throw [OFNotImplementedException exceptionWithClass: self
							    selector: selector];

	return m->method_types;
#else
	Method m;
	const char *ret;

	if ((m = class_getInstanceMethod(self, selector)) == NULL ||
	    (ret = method_getTypeEncoding(m)) == NULL)
		@throw [OFNotImplementedException newWithClass: self
						      selector: selector];
		@throw [OFNotImplementedException exceptionWithClass: self
							    selector: selector];

	return ret;
#endif
}

+ (OFString*)description
{
	return [self className];
}

+ (IMP)setImplementation: (IMP)newImp
	  forClassMethod: (SEL)selector
{
#if defined(OF_OBJFW_RUNTIME)
	if (newImp == (IMP)0 || !class_respondsToSelector(self->isa, selector))
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];
		@throw [OFInvalidArgumentException exceptionWithClass: self
							     selector: _cmd];

	return objc_replace_class_method(self, selector, newImp);
#elif defined(OF_OLD_GNU_RUNTIME)
	Method_t method;
	MethodList_t iter;

	method = class_get_class_method(self->class_pointer, selector);

	if (newImp == (IMP)0 || method == METHOD_NULL)
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];
		@throw [OFInvalidArgumentException exceptionWithClass: self
							     selector: _cmd];

	for (iter = ((Class)self->class_pointer)->methods; iter != NULL;
	    iter = iter->method_next) {
		int i;

		for (i = 0; i < iter->method_count; i++)
			if (sel_eq(iter->method_list[i].method_name,
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
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







-
-
+
+
















-
-
+
+












-
-
+
+









-
-
+
+








	return (IMP)0;
#else
	Method method;

	if (newImp == (IMP)0 ||
	    (method = class_getClassMethod(self, selector)) == NULL)
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];
		@throw [OFInvalidArgumentException exceptionWithClass: self
							     selector: _cmd];

	/*
	 * Cast needed because it's isa in the Apple runtime, but class_pointer
	 * in the GNU runtime.
	 */
	return class_replaceMethod(((OFObject*)self)->isa, selector, newImp,
	    method_getTypeEncoding(method));
#endif
}

+ (IMP)replaceClassMethod: (SEL)selector
      withMethodFromClass: (Class)class
{
	IMP newImp;

	if (![class isSubclassOfClass: self])
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];
		@throw [OFInvalidArgumentException exceptionWithClass: self
							     selector: _cmd];

	newImp = [class methodForSelector: selector];

	return [self setImplementation: newImp
			forClassMethod: selector];
}

+ (IMP)setImplementation: (IMP)newImp
       forInstanceMethod: (SEL)selector
{
#if defined(OF_OBJFW_RUNTIME)
	if (newImp == (IMP)0 || !class_respondsToSelector(self, selector))
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];
		@throw [OFInvalidArgumentException exceptionWithClass: self
							     selector: _cmd];

	return objc_replace_instance_method(self, selector, newImp);
#elif defined(OF_OLD_GNU_RUNTIME)
	Method_t method;
	MethodList_t iter;

	method = class_get_instance_method(self, selector);

	if (newImp == (IMP)0 || method == METHOD_NULL)
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];
		@throw [OFInvalidArgumentException exceptionWithClass: self
							     selector: _cmd];

	for (iter = ((Class)self)->methods; iter != NULL;
	    iter = iter->method_next) {
		int i;

		for (i = 0; i < iter->method_count; i++)
			if (sel_eq(iter->method_list[i].method_name,
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
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







-
-
+
+












-
-
+
+








	return (IMP)0;
#else
	Method method;

	if (newImp == (IMP)0 ||
	    (method = class_getInstanceMethod(self, selector)) == NULL)
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];
		@throw [OFInvalidArgumentException exceptionWithClass: self
							     selector: _cmd];

	return class_replaceMethod(self, selector, newImp,
	    method_getTypeEncoding(method));
#endif
}

+ (IMP)replaceInstanceMethod: (SEL)selector
	 withMethodFromClass: (Class)class
{
	IMP newImp;

	if (![class isSubclassOfClass: self])
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];
		@throw [OFInvalidArgumentException exceptionWithClass: self
							     selector: _cmd];

	newImp = [class instanceMethodForSelector: selector];

	return [self setImplementation: newImp
		     forInstanceMethod: selector];
}

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







-
-
+
+














-
-
+
+







			if (sel_eq(methodList->method_list[i].method_name,
			    selector))
				return NO;
	}

	if ((methodList = malloc(sizeof(*methodList))) == NULL)
		@throw [OFOutOfMemoryException
		    newWithClass: self
		   requestedSize: sizeof(*methodList)];
		    exceptionWithClass: self
			 requestedSize: sizeof(*methodList)];

	methodList->method_next = ((Class)self)->methods;
	methodList->method_count = 1;

	methodList->method_list[0].method_name = selector;
	methodList->method_list[0].method_types = typeEncoding;
	methodList->method_list[0].method_imp = implementation;

	((Class)self)->methods = methodList;

	__objc_update_dispatch_table_for_class(self);

	return YES;
#else
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
	@throw [OFNotImplementedException exceptionWithClass: self
						    selector: _cmd];
#endif
}

+ (BOOL)addClassMethod: (SEL)selector
      withTypeEncoding: (const char*)typeEncoding
	implementation: (IMP)implementation
{
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
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







-
-
+
+














-
-
+
+







			if (sel_eq(methodList->method_list[i].method_name,
			    selector))
				return NO;
	}

	if ((methodList = malloc(sizeof(*methodList))) == NULL)
		@throw [OFOutOfMemoryException
		    newWithClass: self
		   requestedSize: sizeof(*methodList)];
		    exceptionWithClass: self
			 requestedSize: sizeof(*methodList)];

	methodList->method_next = ((Class)self->class_pointer)->methods;
	methodList->method_count = 1;

	methodList->method_list[0].method_name = selector;
	methodList->method_list[0].method_types = typeEncoding;
	methodList->method_list[0].method_imp = implementation;

	((Class)self->class_pointer)->methods = methodList;

	__objc_update_dispatch_table_for_class((Class)self->class_pointer);

	return YES;
#else
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
	@throw [OFNotImplementedException exceptionWithClass: self
						    selector: _cmd];
#endif
}

+ (void)inheritMethodsFromClass: (Class)class
{
	Class superclass = [self superclass];

696
697
698
699
700
701
702
703
704


705
706
707
708
709
710
711
698
699
700
701
702
703
704


705
706
707
708
709
710
711
712
713







-
-
+
+







				[self addInstanceMethod: selector
				       withTypeEncoding: typeEncoding
					 implementation: implementation];
			}
		}
	}
#else
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
	@throw [OFNotImplementedException exceptionWithClass: self
						    selector: _cmd];
#endif

	[self inheritMethodsFromClass: [class superclass]];
}

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







-
-
+
+







-
-
+
+








-
-
+
+








- (const char*)typeEncodingForSelector: (SEL)selector
{
#if defined(OF_OBJFW_RUNTIME)
	const char *ret;

	if ((ret = objc_get_type_encoding(isa, selector)) == NULL)
		@throw [OFNotImplementedException newWithClass: isa
						      selector: selector];
		@throw [OFNotImplementedException exceptionWithClass: isa
							    selector: selector];

	return ret;
#elif defined(OF_OLD_GNU_RUNTIME)
	Method_t m;

	if ((m = class_get_instance_method(isa, selector)) == NULL ||
	    m->method_types == NULL)
		@throw [OFNotImplementedException newWithClass: isa
						      selector: selector];
		@throw [OFNotImplementedException exceptionWithClass: isa
							    selector: selector];

	return m->method_types;
#else
	Method m;
	const char *ret;

	if ((m = class_getInstanceMethod(isa, selector)) == NULL ||
	    (ret = method_getTypeEncoding(m)) == NULL)
		@throw [OFNotImplementedException newWithClass: isa
						      selector: selector];
		@throw [OFNotImplementedException exceptionWithClass: isa
							    selector: selector];

	return ret;
#endif
}

- (BOOL)isEqual: (id)object
{
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
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







-
+



-
-
+
+
+


















-
+


-
-
+
+




-
-
+
+
+
















-
+







	void **memoryChunks;
	unsigned int memoryChunksSize;

	memoryChunksSize = PRE_IVAR->memoryChunksSize + 1;

	if (UINT_MAX - PRE_IVAR->memoryChunksSize < 1 ||
	    memoryChunksSize > UINT_MAX / sizeof(void*))
		@throw [OFOutOfRangeException newWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	if ((memoryChunks = realloc(PRE_IVAR->memoryChunks,
	    memoryChunksSize * sizeof(void*))) == NULL)
		@throw [OFOutOfMemoryException newWithClass: isa
					      requestedSize: memoryChunksSize];
		@throw [OFOutOfMemoryException
		    exceptionWithClass: isa
			 requestedSize: memoryChunksSize];

	PRE_IVAR->memoryChunks = memoryChunks;
	PRE_IVAR->memoryChunks[PRE_IVAR->memoryChunksSize] = pointer;
	PRE_IVAR->memoryChunksSize = memoryChunksSize;
}

- (void*)allocMemoryWithSize: (size_t)size
{
	void *pointer, **memoryChunks;
	unsigned int memoryChunksSize;

	if (size == 0)
		return NULL;

	memoryChunksSize = PRE_IVAR->memoryChunksSize + 1;

	if (UINT_MAX - PRE_IVAR->memoryChunksSize == 0 ||
	    memoryChunksSize > UINT_MAX / sizeof(void*))
		@throw [OFOutOfRangeException newWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	if ((pointer = malloc(size)) == NULL)
		@throw [OFOutOfMemoryException newWithClass: isa
					      requestedSize: size];
		@throw [OFOutOfMemoryException exceptionWithClass: isa
						    requestedSize: size];

	if ((memoryChunks = realloc(PRE_IVAR->memoryChunks,
	    memoryChunksSize * sizeof(void*))) == NULL) {
		free(pointer);
		@throw [OFOutOfMemoryException newWithClass: isa
					      requestedSize: memoryChunksSize];
		@throw [OFOutOfMemoryException
		    exceptionWithClass: isa
			 requestedSize: memoryChunksSize];
	}

	PRE_IVAR->memoryChunks = memoryChunks;
	PRE_IVAR->memoryChunks[PRE_IVAR->memoryChunksSize] = pointer;
	PRE_IVAR->memoryChunksSize = memoryChunksSize;

	return pointer;
}

- (void*)allocMemoryForNItems: (size_t)nItems
		       ofSize: (size_t)size
{
	if (nItems == 0 || size == 0)
		return NULL;

	if (nItems > SIZE_MAX / size)
		@throw [OFOutOfRangeException newWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	return [self allocMemoryWithSize: nItems * size];
}

- (void*)resizeMemory: (void*)pointer
	       toSize: (size_t)size
{
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
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







-
-
+
+






-
-
+
+
















-
+







	iter = PRE_IVAR->memoryChunks + PRE_IVAR->memoryChunksSize;

	while (iter-- > PRE_IVAR->memoryChunks) {
		if (OF_UNLIKELY(*iter == pointer)) {
			if (OF_UNLIKELY((pointer = realloc(pointer,
			    size)) == NULL))
				@throw [OFOutOfMemoryException
				     newWithClass: isa
				    requestedSize: size];
				     exceptionWithClass: isa
					  requestedSize: size];

			*iter = pointer;
			return pointer;
		}
	}

	@throw [OFMemoryNotPartOfObjectException newWithClass: isa
						      pointer: pointer];
	@throw [OFMemoryNotPartOfObjectException exceptionWithClass: isa
							    pointer: pointer];
}

- (void*)resizeMemory: (void*)pointer
	     toNItems: (size_t)nItems
	       ofSize: (size_t)size
{
	if (pointer == NULL)
		return [self allocMemoryForNItems: nItems
					   ofSize: size];

	if (nItems == 0 || size == 0) {
		[self freeMemory: pointer];
		return NULL;
	}

	if (nItems > SIZE_MAX / size)
		@throw [OFOutOfRangeException newWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: isa];

	return [self resizeMemory: pointer
			   toSize: nItems * size];
}

- (void)freeMemory: (void*)pointer
{
995
996
997
998
999
1000
1001
1002
1003


1004
1005
1006
1007
1008
1009
1010
999
1000
1001
1002
1003
1004
1005


1006
1007
1008
1009
1010
1011
1012
1013
1014







-
-
+
+








			PRE_IVAR->memoryChunks = memoryChunks;

			return;
		}
	}

	@throw [OFMemoryNotPartOfObjectException newWithClass: isa
						      pointer: pointer];
	@throw [OFMemoryNotPartOfObjectException exceptionWithClass: isa
							    pointer: pointer];
}

- retain
{
#if defined(OF_ATOMIC_OPS)
	of_atomic_inc_32(&PRE_IVAR->retainCount);
#else
1087
1088
1089
1090
1091
1092
1093
1094
1095


1096
1097
1098
1099
1100
1101
1102
1103
1104


1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116


1117
1118
1119
1120
1121
1122


1123
1124
1125
1126
1127
1128
1129


1130
1131
1132
1133
1134
1135
1136


1137
1138
1139
1140
1141
1142
1143
1144


1145
1146
1147
1148
1149
1150


1151
1152
1153
1154
1155
1156
1157
1091
1092
1093
1094
1095
1096
1097


1098
1099
1100
1101
1102
1103
1104
1105
1106


1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118


1119
1120
1121
1122
1123
1124


1125
1126
1127
1128
1129
1130
1131


1132
1133
1134
1135
1136
1137
1138


1139
1140
1141
1142
1143
1144
1145
1146


1147
1148
1149
1150
1151
1152


1153
1154
1155
1156
1157
1158
1159
1160
1161







-
-
+
+







-
-
+
+










-
-
+
+




-
-
+
+





-
-
+
+





-
-
+
+






-
-
+
+




-
-
+
+







	free((char*)self - PRE_IVAR_ALIGN);
}

/* Required to use properties with the Apple runtime */
- copyWithZone: (void*)zone
{
	if (zone != NULL)
		@throw [OFNotImplementedException newWithClass: isa
						      selector: _cmd];
		@throw [OFNotImplementedException exceptionWithClass: isa
							    selector: _cmd];

	return [(id)self copy];
}

- mutableCopyWithZone: (void*)zone
{
	if (zone != NULL)
		@throw [OFNotImplementedException newWithClass: isa
						      selector: _cmd];
		@throw [OFNotImplementedException exceptionWithClass: isa
							    selector: _cmd];

	return [(id)self mutableCopy];
}

/*
 * Those are needed as the root class is the superclass of the root class's
 * metaclass and thus instance methods can be sent to class objects as well.
 */
+ (void)addMemoryToPool: (void*)pointer
{
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
	@throw [OFNotImplementedException exceptionWithClass: self
						    selector: _cmd];
}

+ (void*)allocMemoryWithSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
	@throw [OFNotImplementedException exceptionWithClass: self
						    selector: _cmd];
}

+ (void*)allocMemoryForNItems: (size_t)nItems
		       ofSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
	@throw [OFNotImplementedException exceptionWithClass: self
						    selector: _cmd];
}

+ (void*)resizeMemory: (void*)pointer
	       toSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
	@throw [OFNotImplementedException exceptionWithClass: self
						    selector: _cmd];
}

+ (void*)resizeMemory: (void*)pointer
	     toNItems: (size_t)nItems
	      ofSize: (size_t)size
{
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
	@throw [OFNotImplementedException exceptionWithClass: self
						    selector: _cmd];
}

+ (void)freeMemory: (void*)pointer
{
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
	@throw [OFNotImplementedException exceptionWithClass: self
						    selector: _cmd];
}

+ retain
{
	return self;
}

1167
1168
1169
1170
1171
1172
1173
1174
1175


1176
1177
1178
1179
1180
1181


1182
1183
1184
1185
1186
1187


1188
1189
1171
1172
1173
1174
1175
1176
1177


1178
1179
1180
1181
1182
1183


1184
1185
1186
1187
1188
1189


1190
1191
1192
1193







-
-
+
+




-
-
+
+




-
-
+
+



+ (void)release
{
}

+ (void)dealloc
{
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
	@throw [OFNotImplementedException exceptionWithClass: self
						    selector: _cmd];
}

+ copyWithZone: (void*)zone
{
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
	@throw [OFNotImplementedException exceptionWithClass: self
						    selector: _cmd];
}

+ mutableCopyWithZone: (void*)zone
{
	@throw [OFNotImplementedException newWithClass: self
					      selector: _cmd];
	@throw [OFNotImplementedException exceptionWithClass: self
						    selector: _cmd];
}
@end