ObjFW  Diff

Differences From Artifact [684035ffa6]:

To Artifact [63edddc2b7]:


11
12
13
14
15
16
17

18
19
20
21
22















23
24
25
26
27
28

29
30
31
32
33
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
66
67
68
69
70
71
72

73
74
75

76
77
78
79
80
81
82

#include "config.h"

#import <objc/objc.h>

#import "OFExceptions.h"


#import "atomic.h"

#define NUM_SPINLOCKS 8	/* needs to be a power of 2 */
#define SPINLOCK_HASH(p) ((uintptr_t)p >> 4) & (NUM_SPINLOCKS - 1)
static of_spinlock_t spinlocks[NUM_SPINLOCKS] = {};
















id
objc_getProperty(id self, SEL _cmd, ptrdiff_t offset, BOOL atomic)
{
	if (atomic) {
		id *ptr = (id*)((char*)self + offset);

		unsigned hash = SPINLOCK_HASH(ptr);

		of_spinlock_lock(spinlocks[hash]);

		@try {
			return [[*ptr retain] autorelease];
		} @finally {
			of_spinlock_unlock(spinlocks[hash]);
		}



	}

	return *(id*)((char*)self + offset);
}

void
objc_setProperty(id self, SEL _cmd, ptrdiff_t offset, id value, BOOL atomic,
    BOOL copy)
{
	if (atomic) {
		id *ptr = (id*)((char*)self + offset);

		unsigned hash = SPINLOCK_HASH(ptr);

		of_spinlock_lock(spinlocks[hash]);

		@try {

			id old = *ptr;

			switch (copy) {
			case 0:
				*ptr = [value retain];
				break;
			case 2:
				/*
				 * Apple uses this to indicate that the copy
				 * should be mutable. Please hit them for
				 * abusing a poor BOOL!
				 */
				*ptr = [value mutableCopy];
				break;
			default:
				*ptr = [value copy];
			}

			[old release];

		} @finally {
			of_spinlock_unlock(spinlocks[hash]);
		}


		return;
	}

	id *ptr = (id*)((char*)self + offset);
	id old = *ptr;








>
|



|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>






>


|




|

>
>
>











>


|


>



















>

|

>







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

#include "config.h"

#import <objc/objc.h>

#import "OFExceptions.h"

#ifdef OF_THREADS
#import "threading.h"

#define NUM_SPINLOCKS 8	/* needs to be a power of 2 */
#define SPINLOCK_HASH(p) ((uintptr_t)p >> 4) & (NUM_SPINLOCKS - 1)
static of_spinlock_t spinlocks[NUM_SPINLOCKS];
#endif

BOOL
objc_properties_init()
{
#ifdef OF_THREADS
	size_t i;

	for (i = 0; i < NUM_SPINLOCKS; i++)
		if (!of_spinlock_new(&spinlocks[i]))
			return NO;
#endif

	return YES;
}

id
objc_getProperty(id self, SEL _cmd, ptrdiff_t offset, BOOL atomic)
{
	if (atomic) {
		id *ptr = (id*)((char*)self + offset);
#ifdef OF_THREADS
		unsigned hash = SPINLOCK_HASH(ptr);

		of_spinlock_lock(&spinlocks[hash]);

		@try {
			return [[*ptr retain] autorelease];
		} @finally {
			of_spinlock_unlock(&spinlocks[hash]);
		}
#else
		return [[*ptr retain] autorelease];
#endif
	}

	return *(id*)((char*)self + offset);
}

void
objc_setProperty(id self, SEL _cmd, ptrdiff_t offset, id value, BOOL atomic,
    BOOL copy)
{
	if (atomic) {
		id *ptr = (id*)((char*)self + offset);
#ifdef OF_THREADS
		unsigned hash = SPINLOCK_HASH(ptr);

		of_spinlock_lock(&spinlocks[hash]);

		@try {
#endif
			id old = *ptr;

			switch (copy) {
			case 0:
				*ptr = [value retain];
				break;
			case 2:
				/*
				 * Apple uses this to indicate that the copy
				 * should be mutable. Please hit them for
				 * abusing a poor BOOL!
				 */
				*ptr = [value mutableCopy];
				break;
			default:
				*ptr = [value copy];
			}

			[old release];
#ifdef OF_THREADS
		} @finally {
			of_spinlock_unlock(&spinlocks[hash]);
		}
#endif

		return;
	}

	id *ptr = (id*)((char*)self + offset);
	id old = *ptr;