ObjFW  Check-in [c50d483262]

Overview
Comment:It can never happen that we don't have threads and atomic ops.
If we don't have threads, everything is atomic.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c50d48326249765d6600aa644ea6015af75beec0b4e4cf20c0624ecae320d90d
User & Date: js on 2011-01-14 11:01:27
Other Links: manifest | tags
Context
2011-01-14
12:22
Add OFURL class. check-in: 164c76c0cf user: js tags: trunk
11:01
It can never happen that we don't have threads and atomic ops.
If we don't have threads, everything is atomic.
check-in: c50d483262 user: js tags: trunk
2011-01-13
14:24
Update .xcodeproj. check-in: f38b4705e3 user: js tags: trunk
Changes

Modified src/OFBlock.m from [3f030b240f] to [f29296dab5].

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
		return copy;
	}

	if (block->isa == (Class)&_NSConcreteMallocBlock) {
#if defined(OF_ATOMIC_OPS)
		of_atomic_inc_int(&block->flags);
#else
# ifdef OF_THREADS
		unsigned hash = SPINLOCK_HASH(block);

		assert(of_spinlock_lock(&spinlocks[hash]));
# endif

		block->flags++;

# ifdef OF_THREADS
		assert(of_spinlock_unlock(&spinlocks[hash]));
# endif
#endif
	}

	return block;
}

void







<



<
<

<
<

<







177
178
179
180
181
182
183

184
185
186


187


188

189
190
191
192
193
194
195
		return copy;
	}

	if (block->isa == (Class)&_NSConcreteMallocBlock) {
#if defined(OF_ATOMIC_OPS)
		of_atomic_inc_int(&block->flags);
#else

		unsigned hash = SPINLOCK_HASH(block);

		assert(of_spinlock_lock(&spinlocks[hash]));


		block->flags++;


		assert(of_spinlock_unlock(&spinlocks[hash]));

#endif
	}

	return block;
}

void
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
	if ((of_atomic_dec_int(&block->flags) & OF_BLOCK_REFCOUNT_MASK) == 0) {
		if (block->flags & OF_BLOCK_HAS_COPY_DISPOSE)
			block->descriptor->dispose_helper(block);

		free(block);
	}
#else
# ifdef OF_THREADS
	unsigned hash = SPINLOCK_HASH(block);

	assert(of_spinlock_lock(&spinlocks[hash]));
# endif

	if ((--block->flags & OF_BLOCK_REFCOUNT_MASK) == 0) {
# ifdef OF_THREADS
		assert(of_spinlock_unlock(&spinlocks[hash]));
# endif

		if (block->flags & OF_BLOCK_HAS_COPY_DISPOSE)
			block->descriptor->dispose_helper(block);

		free(block);
	}

# ifdef OF_THREADS
	assert(of_spinlock_unlock(&spinlocks[hash]));
# endif
#endif
}

void
_Block_object_assign(void *dst_, const void *src_, const int flags_)
{
	int flags = flags_ & (OF_BLOCK_FIELD_IS_BLOCK |







<



<
<

<

<






<
<

<







204
205
206
207
208
209
210

211
212
213


214

215

216
217
218
219
220
221


222

223
224
225
226
227
228
229
	if ((of_atomic_dec_int(&block->flags) & OF_BLOCK_REFCOUNT_MASK) == 0) {
		if (block->flags & OF_BLOCK_HAS_COPY_DISPOSE)
			block->descriptor->dispose_helper(block);

		free(block);
	}
#else

	unsigned hash = SPINLOCK_HASH(block);

	assert(of_spinlock_lock(&spinlocks[hash]));


	if ((--block->flags & OF_BLOCK_REFCOUNT_MASK) == 0) {

		assert(of_spinlock_unlock(&spinlocks[hash]));


		if (block->flags & OF_BLOCK_HAS_COPY_DISPOSE)
			block->descriptor->dispose_helper(block);

		free(block);
	}


	assert(of_spinlock_unlock(&spinlocks[hash]));

#endif
}

void
_Block_object_assign(void *dst_, const void *src_, const int flags_)
{
	int flags = flags_ & (OF_BLOCK_FIELD_IS_BLOCK |
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
		@throw [OFInitializationFailedException newWithClass: self];
	memcpy(&_NSConcreteMallocBlock, tmp, sizeof(_NSConcreteMallocBlock));
	free(tmp);
	objc_registerClassPair((Class)&_NSConcreteMallocBlock);
}
#endif

#if !defined(OF_ATOMIC_OPS) && defined(OF_THREADS)
+ (void)initialize
{
	size_t i;

	for (i = 0; i < NUM_SPINLOCKS; i++)
		if (!of_spinlock_new(&spinlocks[i]))
			@throw [OFInitializationFailedException







|







317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
		@throw [OFInitializationFailedException newWithClass: self];
	memcpy(&_NSConcreteMallocBlock, tmp, sizeof(_NSConcreteMallocBlock));
	free(tmp);
	objc_registerClassPair((Class)&_NSConcreteMallocBlock);
}
#endif

#if !defined(OF_ATOMIC_OPS)
+ (void)initialize
{
	size_t i;

	for (i = 0; i < NUM_SPINLOCKS; i++)
		if (!of_spinlock_new(&spinlocks[i]))
			@throw [OFInitializationFailedException

Modified src/OFObject.m from [07b4e58e79] to [a02b47cd7d].

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# define class_getSuperclass class_get_super_class
#endif

struct pre_ivar {
	void	      **memchunks;
	size_t	      memchunks_size;
	int32_t	      retain_count;
#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))







|







56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# define class_getSuperclass class_get_super_class
#endif

struct pre_ivar {
	void	      **memchunks;
	size_t	      memchunks_size;
	int32_t	      retain_count;
#if !defined(OF_ATOMIC_OPS)
	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))
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
		@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;

#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);







|







144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
		@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;

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

	instance = (OFObject*)((char*)instance + PRE_IVAR_ALIGN);
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
						      pointer: ptr];
}

- retain
{
#if defined(OF_ATOMIC_OPS)
	of_atomic_inc_32(&PRE_IVAR->retain_count);
#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
{
#if defined(OF_ATOMIC_OPS)
	if (of_atomic_dec_32(&PRE_IVAR->retain_count) <= 0)
		[self dealloc];
#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







|



<
<
















|








<
<
<







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
						      pointer: ptr];
}

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


#endif

	return self;
}

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

- (void)release
{
#if defined(OF_ATOMIC_OPS)
	if (of_atomic_dec_32(&PRE_IVAR->retain_count) <= 0)
		[self dealloc];
#else
	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];



#endif
}

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