ObjFW  Diff

Differences From Artifact [64a1864a0a]:

To Artifact [470fa11181]:


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







+
-
-
+
+

















-
+
-
-
+







		extraAlignment = ((instanceSize + extraAlignment - 1) &
		    ~(extraAlignment - 1)) - extraAlignment;

	instance = malloc(PRE_IVAR_ALIGN + instanceSize +
	    extraAlignment + extraSize);

	if (OF_UNLIKELY(instance == nil)) {
		object_setClass((id)&alloc_failed_exception,
		alloc_failed_exception.isa = [OFAllocFailedException class];
		@throw (OFAllocFailedException*)&alloc_failed_exception;
		    [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->isa = class;
	memset(instance, 0, instanceSize);
	memset((char*)instance + sizeof(instance->isa), 0,
	    instanceSize - sizeof(instance->isa));
	object_setClass(instance, class);

	if (OF_UNLIKELY(extra != NULL))
		*extra = (char*)instance + instanceSize + extraAlignment;

	return instance;
}

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







-
+














-
+







	    typeEncoding);
}

+ (IMP)replaceClassMethod: (SEL)selector
       withImplementation: (IMP)implementation
	     typeEncoding: (const char*)typeEncoding
{
	return class_replaceMethod(((OFObject*)self)->isa, selector,
	return class_replaceMethod(object_getClass(self), selector,
	    implementation, typeEncoding);
}

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

	if ([self isSubclassOfClass: class])
		return;

#if defined(OF_APPLE_RUNTIME)
	Method *methodList;
	unsigned i, count;

	methodList = class_copyMethodList(((OFObject*)class)->isa, &count);
	methodList = class_copyMethodList(object_getClass(class), &count);
	@try {
		for (i = 0; i < count; i++) {
			SEL selector = method_getName(methodList[i]);

			/*
			 * Don't replace methods implemented in receiving class.
			 */
418
419
420
421
422
423
424
425

426
427
428
429
430
431
432
418
419
420
421
422
423
424

425
426
427
428
429
430
431
432







-
+







		}
	} @finally {
		free(methodList);
	}
#elif defined(OF_OBJFW_RUNTIME)
	struct objc_method_list *methodlist;

	for (methodlist = class->isa->methodlist;
	for (methodlist = object_getClass(class)->methodlist;
	    methodlist != NULL; methodlist = methodlist->next) {
		int i;

		for (i = 0; i < methodlist->count; i++) {
			SEL selector = (SEL)&methodlist->methods[i].sel;

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







+
-
+

















-
+




-
+







+
-
+








-
+




-
+




-
+




-
+







}

- init
{
	Class class;
	void (*last)(id, SEL) = NULL;

	for (class = object_getClass(self); class != Nil;
	for (class = isa; class != Nil; class = class_getSuperclass(class)) {
	    class = class_getSuperclass(class)) {
		void (*construct)(id, SEL);

		if ([class instancesRespondToSelector: cxx_construct]) {
			if ((construct = (void(*)(id, SEL))[class
			    instanceMethodForSelector: cxx_construct]) != last)
				construct(self, cxx_construct);

			last = construct;
		} else
			break;
	}

	return self;
}

- (Class)class
{
	return isa;
	return object_getClass(self);
}

- (OFString*)className
{
	return [OFString stringWithCString: class_getName(isa)
	return [OFString stringWithCString: object_getClassName(self)
				  encoding: OF_STRING_ENCODING_ASCII];
}

- (BOOL)isKindOfClass: (Class)class
{
	Class iter;

	for (iter = object_getClass(self); iter != Nil;
	for (iter = isa; iter != Nil; iter = class_getSuperclass(iter))
	    iter = class_getSuperclass(iter))
		if (iter == class)
			return YES;

	return NO;
}

- (BOOL)isMemberOfClass: (Class)class
{
	return (isa == class);
	return (object_getClass(self) == class);
}

- (BOOL)respondsToSelector: (SEL)selector
{
	return class_respondsToSelector(isa, selector);
	return class_respondsToSelector(object_getClass(self), selector);
}

- (BOOL)conformsToProtocol: (Protocol*)protocol
{
	return [isa conformsToProtocol: protocol];
	return [object_getClass(self) conformsToProtocol: protocol];
}

- (IMP)methodForSelector: (SEL)selector
{
	return class_getMethodImplementation(isa, selector);
	return class_getMethodImplementation(object_getClass(self), selector);
}

- (id)performSelector: (SEL)selector
{
	id (*imp)(id, SEL) = (id(*)(id, SEL))[self methodForSelector: selector];

	return imp(self, selector);
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
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







-
-
-
+
+
+
+
+






-
-
-
-
+
+
+
+
+







}

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

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

	return ret;
#else
	Method m;
	const char *ret;

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

	return ret;
#endif
}

- (BOOL)isEqual: (id)object
{
596
597
598
599
600
601
602
603

604
605
606

607
608
609
610
611
612
613
601
602
603
604
605
606
607

608
609
610

611
612
613
614
615
616
617
618







-
+


-
+








- (void*)allocMemoryWithSize: (size_t)size
{
	void *pointer;
	struct pre_mem *preMem;

	if (size > SIZE_MAX - PRE_IVAR_ALIGN)
		@throw [OFOutOfRangeException exceptionWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	if ((pointer = malloc(PRE_MEM_ALIGN + size)) == NULL)
		@throw [OFOutOfMemoryException exceptionWithClass: isa
		@throw [OFOutOfMemoryException exceptionWithClass: [self class]
						    requestedSize: size];
	preMem = pointer;

	preMem->owner = self;
	preMem->prev = PRE_IVAR->lastMem;
	preMem->next = NULL;

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







-
+




















-
+



-
+







- (void*)allocMemoryWithSize: (size_t)size
		       count: (size_t)count
{
	if (size == 0 || count == 0)
		return NULL;

	if (count > SIZE_MAX / size)
		@throw [OFOutOfRangeException exceptionWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	return [self allocMemoryWithSize: size * count];
}

- (void*)resizeMemory: (void*)pointer
		 size: (size_t)size
{
	void *new;
	struct pre_mem *preMem;

	if (pointer == NULL)
		return [self allocMemoryWithSize: size];

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

	if (PRE_MEM(pointer)->owner != self)
		@throw [OFMemoryNotPartOfObjectException
		    exceptionWithClass: isa
		    exceptionWithClass: [self class]
			       pointer: pointer];

	if ((new = realloc(PRE_MEM(pointer), PRE_MEM_ALIGN + size)) == NULL)
		@throw [OFOutOfMemoryException exceptionWithClass: isa
		@throw [OFOutOfMemoryException exceptionWithClass: [self class]
						    requestedSize: size];
	preMem = new;

	if (preMem != PRE_MEM(pointer)) {
		if (preMem->prev != NULL)
			preMem->prev->next = preMem;
		if (preMem->next != NULL)
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
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







-
+












-
+








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

	if (count > SIZE_MAX / size)
		@throw [OFOutOfRangeException exceptionWithClass: isa];
		@throw [OFOutOfRangeException exceptionWithClass: [self class]];

	return [self resizeMemory: pointer
			     size: size * count];
}

- (void)freeMemory: (void*)pointer
{
	if (pointer == NULL)
		return;

	if (PRE_MEM(pointer)->owner != self)
		@throw [OFMemoryNotPartOfObjectException
		    exceptionWithClass: isa
		    exceptionWithClass: [self class]
			       pointer: pointer];

	if (PRE_MEM(pointer)->prev != NULL)
		PRE_MEM(pointer)->prev->next = PRE_MEM(pointer)->next;
	if (PRE_MEM(pointer)->next != NULL)
		PRE_MEM(pointer)->next->prev = PRE_MEM(pointer)->prev;

783
784
785
786
787
788
789

790

791
792
793
794
795
796
797
788
789
790
791
792
793
794
795

796
797
798
799
800
801
802
803







+
-
+








- (void)dealloc
{
	Class class;
	void (*last)(id, SEL) = NULL;
	struct pre_mem *iter;

	for (class = object_getClass(self); class != Nil;
	for (class = isa; class != Nil; class = class_getSuperclass(class)) {
	    class = class_getSuperclass(class)) {
		void (*destruct)(id, SEL);

		if ([class instancesRespondToSelector: cxx_destruct]) {
			if ((destruct = (void(*)(id, SEL))[class
			    instanceMethodForSelector: cxx_destruct]) != last)
				destruct(self, cxx_destruct);

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







-
-
+
+
+







-
-
+
+
+







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

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

	return [(id)self copy];
}

- mutableCopyWithZone: (void*)zone
{
	if (zone != NULL)
		@throw [OFNotImplementedException exceptionWithClass: isa
							    selector: _cmd];
		@throw [OFNotImplementedException
		    exceptionWithClass: [self class]
			      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.