ObjFW  Diff

Differences From Artifact [d6164e1afa]:

To Artifact [edc6b04857]:


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







-
+






-
+










-
-
+
+











-
+







    void **extra)
{
	OFObject *instance;
	size_t instanceSize;

	instanceSize = class_getInstanceSize(class);

	if (OF_UNLIKELY(extraAlignment > 0))
	if OF_UNLIKELY (extraAlignment > 0)
		extraAlignment = ((instanceSize + extraAlignment - 1) &
		    ~(extraAlignment - 1)) - extraAlignment;

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

	if (OF_UNLIKELY(instance == nil)) {
	if OF_UNLIKELY (instance == nil) {
		object_setClass((id)&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))) {
	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);

	memset(instance, 0, instanceSize);
	object_setClass(instance, class);

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

	return instance;
}

const char*
_NSPrintForDebugger(id object)
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
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







-
+


-
+








-
+


-
+









-
+


-
+











-
+


-
+




-
+




+
-
+




-
-
+
+

-
+


-
+

-
+










-
+



-
+




-
+








-
+


-
+




-
+

-
+


-
+

-
+







}

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

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

	if ((pointer = malloc(PRE_MEM_ALIGN + size)) == NULL)
	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->next = NULL;

	if (PRE_IVAR->lastMem != NULL)
	if OF_LIKELY (PRE_IVAR->lastMem != NULL)
		PRE_IVAR->lastMem->next = preMem;

	if (PRE_IVAR->firstMem == NULL)
	if OF_UNLIKELY (PRE_IVAR->firstMem == NULL)
		PRE_IVAR->firstMem = preMem;
	PRE_IVAR->lastMem = preMem;

	return (char*)pointer + PRE_MEM_ALIGN;
}

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

	if (count > SIZE_MAX / size)
	if OF_UNLIKELY (count > SIZE_MAX / size)
		@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)
	if OF_UNLIKELY (pointer == NULL)
		return [self allocMemoryWithSize: size];

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

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

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

	if (preMem != PRE_MEM(pointer)) {
		if (preMem->prev != NULL)
	if OF_UNLIKELY (preMem != PRE_MEM(pointer)) {
		if OF_LIKELY (preMem->prev != NULL)
			preMem->prev->next = preMem;
		if (preMem->next != NULL)
		if OF_LIKELY (preMem->next != NULL)
			preMem->next->prev = preMem;

		if (PRE_IVAR->firstMem == PRE_MEM(pointer))
		if OF_UNLIKELY (PRE_IVAR->firstMem == PRE_MEM(pointer))
			PRE_IVAR->firstMem = preMem;
		if (PRE_IVAR->lastMem == PRE_MEM(pointer))
		if OF_UNLIKELY (PRE_IVAR->lastMem == PRE_MEM(pointer))
			PRE_IVAR->lastMem = preMem;
	}

	return (char*)new + PRE_MEM_ALIGN;
}

- (void*)resizeMemory: (void*)pointer
		 size: (size_t)size
		count: (size_t)count
{
	if (pointer == NULL)
	if OF_UNLIKELY (pointer == NULL)
		return [self allocMemoryWithSize: size
					   count: count];

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

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

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

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

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

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

	if (PRE_IVAR->firstMem == PRE_MEM(pointer))
	if OF_UNLIKELY (PRE_IVAR->firstMem == PRE_MEM(pointer))
		PRE_IVAR->firstMem = PRE_MEM(pointer)->next;
	if (PRE_IVAR->lastMem == PRE_MEM(pointer))
	if OF_UNLIKELY (PRE_IVAR->lastMem == PRE_MEM(pointer))
		PRE_IVAR->lastMem = PRE_MEM(pointer)->prev;

	/* To detect double-free */
	PRE_MEM(pointer)->owner = nil;

	free(PRE_MEM(pointer));
}
764
765
766
767
768
769
770
771

772
773
774
775
776
777
778
765
766
767
768
769
770
771

772
773
774
775
776
777
778
779







-
+







#endif
}

- autorelease
{
	/*
	 * Cache OFAutoreleasePool since class lookups are expensive with the
	 * GNU ABI.
	 * GNU ABI used by GCC.
	 */
	if (autoreleasePool == Nil)
		autoreleasePool = [OFAutoreleasePool class];

	return [autoreleasePool addObject: self];
}

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







-
+













-
+









-
+







	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 (iter->owner != self)
		if OF_UNLIKELY (iter->owner != self)
			abort();

		free(iter);

		iter = next;
	}

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

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

	return [(id)self copy];
}

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

	return [(id)self mutableCopy];
}