ObjFW  Diff

Differences From Artifact [72f139fc0b]:

To Artifact [b33c6cd1e8]:


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







-
+

-
+














-
+








#ifdef _WIN32
# include <windows.h>
#endif

#import "OFString.h"

#ifdef OF_ATOMIC_OPS
#if defined(OF_ATOMIC_OPS)
# import "atomic.h"
#else
#elif defined(OF_THREADS)
# import "threading.h"
#endif

/* A few macros to reduce #ifdefs */
#ifdef OF_OLD_GNU_RUNTIME
# define class_getInstanceSize class_get_instance_size
# define class_getName class_get_class_name
# define class_getSuperclass class_get_super_class
#endif

struct pre_ivar {
	void	      **memchunks;
	size_t	      memchunks_size;
	int32_t	      retain_count;
#ifndef OF_ATOMIC_OPS
#if !defined(OF_ATOMIC_OPS) && defined(OF_THREADS)
	of_spinlock_t retain_spinlock;
#endif
};

/* Hopefully no arch needs more than 16 bytes padding */
#define PRE_IVAR_ALIGN ((sizeof(struct pre_ivar) + 15) & ~15)
#define PRE_IVAR ((struct pre_ivar*)((char*)self - PRE_IVAR_ALIGN))
139
140
141
142
143
144
145
146

147
148
149
150
151
152
153
139
140
141
142
143
144
145

146
147
148
149
150
151
152
153







-
+







		@throw (OFAllocFailedException*)&alloc_failed_exception;
	}

	((struct pre_ivar*)instance)->memchunks = NULL;
	((struct pre_ivar*)instance)->memchunks_size = 0;
	((struct pre_ivar*)instance)->retain_count = 1;

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

	instance = (OFObject*)((char*)instance + PRE_IVAR_ALIGN);
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
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







-
+

-
+



+
+













-
+


-
+








+
+
+








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

- retain
{
#ifdef OF_ATOMIC_OPS
#if defined(OF_ATOMIC_OPS)
	of_atomic_inc_32(&PRE_IVAR->retain_count);
#else
#elif defined(OF_THREADS)
	assert(of_spinlock_lock(&PRE_IVAR->retain_spinlock));
	PRE_IVAR->retain_count++;
	assert(of_spinlock_unlock(&PRE_IVAR->retain_spinlock));
#else
	PRE_IVAR->retain_count++;
#endif

	return self;
}

- (size_t)retainCount
{
	assert(PRE_IVAR->retain_count >= 0);
	return (size_t)PRE_IVAR->retain_count;
}

- (void)release
{
#ifdef OF_ATOMIC_OPS
#if defined(OF_ATOMIC_OPS)
	if (of_atomic_dec_32(&PRE_IVAR->retain_count) <= 0)
		[self dealloc];
#else
#elif defined(OF_THREADS)
	size_t c;

	assert(of_spinlock_lock(&PRE_IVAR->retain_spinlock));
	c = --PRE_IVAR->retain_count;
	assert(of_spinlock_unlock(&PRE_IVAR->retain_spinlock));

	if (!c)
		[self dealloc];
#else
	if (--PRE_IVAR->retain_count <= 0)
		[self dealloc];
#endif
}

- autorelease
{
	/*
	 * Cache OFAutoreleasePool since class lookups are expensive with the