ObjFW  Check-in [a99f512a4a]

Overview
Comment:Rework OFAutoreleasePool and remove now unnecessary hack from OFList.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a99f512a4aacfd32fc1474a81919cafac2ad7add4c5c78523a57a69e29ce19eb
User & Date: js on 2009-09-16 16:22:27
Other Links: manifest | tags
Context
2009-09-16
17:22
Completely remove tests/OFAutoreleasePool, it's for the old version. check-in: f8005a79c1 user: js tags: trunk
16:22
Rework OFAutoreleasePool and remove now unnecessary hack from OFList. check-in: a99f512a4a user: js tags: trunk
2009-09-15
14:17
+[stringWithCString:length:] requires a \0 at the end of the string. check-in: 16db39752c user: js tags: trunk
Changes

Modified src/OFAutoreleasePool.h from [9729efb348] to [33de3af070].

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * The OFAutoreleasePool class provides a class that keeps track of objects
 * that will be released when the autorelease pool is released.
 * Every thread has its own stack of autorelease pools.
 */
@interface OFAutoreleasePool: OFObject
{
	OFArray		 *objects;
	of_list_object_t *listobj;
}

/**
 * Adds an object to the autorelease pool at the top of the thread-specific
 * stack.
 *
 * \param obj The object to add to the autorelease pool







|
|







16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * The OFAutoreleasePool class provides a class that keeps track of objects
 * that will be released when the autorelease pool is released.
 * Every thread has its own stack of autorelease pools.
 */
@interface OFAutoreleasePool: OFObject
{
	OFArray		  *objects;
	OFAutoreleasePool *next, *prev;
}

/**
 * Adds an object to the autorelease pool at the top of the thread-specific
 * stack.
 *
 * \param obj The object to add to the autorelease pool

Modified src/OFAutoreleasePool.m from [d23a1326bd] to [b3f1509666].

8
9
10
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120






121




122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#include "config.h"

#include <stdlib.h>







#import "OFAutoreleasePool.h"
#import "OFList.h"
#import "OFThread.h"
#import "OFExceptions.h"





static OFTLSKey *pool_list_key;






static void
release_list(void *list)
{

	of_list_object_t *first, *iter;
	IMP release;

	if ((first = [(OFList*)list first]) != NULL)
		release = [first->object methodForSelector: @selector(release)];

	for (iter = first; iter != NULL; iter = iter->next)
		release(iter->object, @selector(release));


	[(OFList*)list release];
}

@implementation OFAutoreleasePool
+ (void)initialize
{
	if (self != [OFAutoreleasePool class])
		return;





	pool_list_key = [[OFTLSKey alloc] initWithDestructor: release_list];




}

+ (void)addObjectToTopmostPool: (OFObject*)obj
{

	OFList *pool_list = [OFThread objectForTLSKey: pool_list_key];




	if (pool_list == nil || [pool_list last] == NULL) {
		@try {
			[[self alloc] init];
			pool_list = [OFThread objectForTLSKey: pool_list_key];
		} @catch (OFException *e) {
			[obj release];
			@throw e;
		}
	}






	if (pool_list == nil || [pool_list last] == NULL) {

		[obj release];
		@throw [OFInitializationFailedException newWithClass: self];
	}

	@try {
		[[pool_list last]->object addObject: obj];
	} @catch (OFException *e) {
		[obj release];
		@throw e;
	}
}

- init
{
	OFList *pool_list;

	self = [super init];

	if ((pool_list = [OFThread objectForTLSKey: pool_list_key]) == nil) {
		@try {




			pool_list = [[OFList alloc]
			    initWithoutRetainAndRelease];
		} @catch (OFException *e) {






			[self dealloc];
			@throw e;
		}









		@try {
			[OFThread setObject: pool_list

				  forTLSKey: pool_list_key];

		} @catch (OFException *e) {
			[self dealloc];
			@throw e;
		} @finally {
			[pool_list release];
		}
	}

	@try {
		listobj = [pool_list append: self];
	} @catch (OFException *e) {
		[self dealloc];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	/*
	 * FIXME:
	 * Maybe we should get the objects ourself, release them and then
	 * release the pool without calling its release method? This way,
	 * there wouldn't be a recursion.
	 */
	if (listobj->next != NULL)
		[listobj->next->object release];







	[self releaseObjects];





	[[OFThread objectForTLSKey: pool_list_key] remove: listobj];

	[super dealloc];
}

- addObject: (OFObject*)obj
{
	if (objects == nil)
		objects = [[OFMutableArray alloc] init];

	[objects addObject: obj];
	[obj release];

	return self;
}

- releaseObjects
{
	if (listobj->next != NULL)
		[listobj->next->object releaseObjects];

	if (objects == nil)
		return self;

	[objects release];
	objects = nil;








>
>
>
>
>
>






>
>
>
>
|
>
>
>
>
>


|

>
|
|
|
<
<
|
<
<

>
|








>
>
>
>
|
>
>
>
>




>
|
>
>
>

|


<




|
>
>
>
>
>
|
|
>





|








<
<


<
|
>
>
>
>
|
|
|
>
>
>
>
>
>
|
|
|
>
>
>
>
>
>
>
>

|
|
>
|
>
|
|
|
<
|



<
|
|
|
<







<
<
<
<
<
<
|
<

>
>
>
>
>
>
|
>
>
>
>

|

















<
|







8
9
10
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
107

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
175
176
177
178
179
180
181
182
183
184
185
186
187

188
189
190
191
192
193
194
195
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#include "config.h"

#include <stdlib.h>

#ifndef _WIN32
#include <pthread.h>
#else
#include <windows.h>
#endif

#import "OFAutoreleasePool.h"
#import "OFList.h"
#import "OFThread.h"
#import "OFExceptions.h"

/*
 * Pay special attention to NULL and nil in this file, they might be different!
 * Use NULL for TLS values and nil for instance variables.
 */

#ifndef _WIN32
static pthread_key_t first_key, last_key;
#else
static DWORD first_key, last_key;
#endif

static void
release_all(void *list)
{
#ifndef _WIN32
	void *first = pthread_getspecific(first_key);
#else
	void *first = TlsGetValue(first_key);


#endif



	if (first != NULL)
		[(OFAutoreleasePool*)first release];
}

@implementation OFAutoreleasePool
+ (void)initialize
{
	if (self != [OFAutoreleasePool class])
		return;

#ifndef _WIN32
	if (pthread_key_create(&first_key, release_all) ||
	    pthread_key_create(&last_key, NULL))
#else
	/* FIXME: Call destructor */
	if ((first_key = TlsAlloc()) == TLS_OUT_OF_INDEXES ||
	    (last_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
#endif
		@throw [OFInitializationFailedException newWithClass: self];
}

+ (void)addObjectToTopmostPool: (OFObject*)obj
{
#ifndef _WIN32
	void *last = pthread_getspecific(last_key);
#else
	void *last = TlsGetValue(last_key);
#endif

	if (last == NULL) {
		@try {
			[[self alloc] init];

		} @catch (OFException *e) {
			[obj release];
			@throw e;
		}

#ifndef _WIN32
		last = pthread_getspecific(last_key);
#else
		last = TlsGetValue(last_key);
#endif
	}

	if (last == NULL) {
		[obj release];
		@throw [OFInitializationFailedException newWithClass: self];
	}

	@try {
		[(OFAutoreleasePool*)last addObject: obj];
	} @catch (OFException *e) {
		[obj release];
		@throw e;
	}
}

- init
{


	self = [super init];


#ifndef _WIN32
	void *first = pthread_getspecific(first_key);
	void *last = pthread_getspecific(last_key);
#else
	void *first = TlsGetValue(first_key);
	void *last = TlsGetValue(last_key);
#endif

#ifndef _WIN32
	if (pthread_setspecific(last_key, self)) {
#else
	if (!TlsSetValue(last_key, self)) {
#endif
		Class c = isa;
		[super dealloc];
		@throw [OFInitializationFailedException newWithClass: c];
	}

	if (first == NULL) {
#ifndef _WIN32
		if (pthread_setspecific(first_key, self)) {
#else
		if (!TlsSetValue(first_key, self)) {
#endif
			Class c = isa;

#ifndef _WIN32
			pthread_setspecific(last_key, last);
#else
			TlsSetValue(last_key, last);
#endif

			[super dealloc];
			@throw [OFInitializationFailedException

			    newWithClass: c];
		}
	}


	if (last != NULL) {
		prev = (OFAutoreleasePool*)last;
		prev->next = self;

	}

	return self;
}

- (void)dealloc
{






	[next dealloc];


	if (prev != nil)
		prev->next = nil;
#ifndef _WIN32
	pthread_setspecific(last_key, (prev != nil ? prev : NULL));
	if (pthread_getspecific(first_key) == self)
		pthread_setspecific(first_key, NULL);
#else
	TlsSetValue(last_key, (prev != nil ? prev : NULL));
	if (TlsGetValue(first_key) == self)
		TlsSetValue(first_key, NULL);
#endif

	[objects release];

	[super dealloc];
}

- addObject: (OFObject*)obj
{
	if (objects == nil)
		objects = [[OFMutableArray alloc] init];

	[objects addObject: obj];
	[obj release];

	return self;
}

- releaseObjects
{

	[next releaseObjects];

	if (objects == nil)
		return self;

	[objects release];
	objects = nil;

Modified src/OFList.h from [d42967ddf0] to [677855ce18].

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
 * The OFList class provides easy to use double-linked lists.
 */
@interface OFList: OFObject <OFCopying>
{
	of_list_object_t *first;
	of_list_object_t *last;
	size_t		 count;
	BOOL		 retain_and_release;
}

/**
 * \return A new autoreleased OFList
 */
+ list;

/**
 * Initializes an already allocated OFList that does not retain/release objects
 * added to it.
 *
 * \return An initialized OFList
 */
- initWithoutRetainAndRelease;

/**
 * \return The first list object in the list
 */
- (of_list_object_t*)first;

/**
 * \return The last list object in the list







<







<
<
<
<
<
<
<
<







29
30
31
32
33
34
35

36
37
38
39
40
41
42








43
44
45
46
47
48
49
 * The OFList class provides easy to use double-linked lists.
 */
@interface OFList: OFObject <OFCopying>
{
	of_list_object_t *first;
	of_list_object_t *last;
	size_t		 count;

}

/**
 * \return A new autoreleased OFList
 */
+ list;









/**
 * \return The first list object in the list
 */
- (of_list_object_t*)first;

/**
 * \return The last list object in the list

Modified src/OFList.m from [18ec5ebfcc] to [b22035331b].

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
	return [[[self alloc] init] autorelease];
}

- init
{
	self = [super init];

	first = NULL;
	last = NULL;
	retain_and_release = YES;

	return self;
}

- initWithoutRetainAndRelease
{
	self = [super init];

	first = NULL;
	last = NULL;

	return self;
}

- (void)dealloc







<
<
<
<
<
<
<
<
<
<
<







22
23
24
25
26
27
28











29
30
31
32
33
34
35
	return [[[self alloc] init] autorelease];
}

- init
{
	self = [super init];












	first = NULL;
	last = NULL;

	return self;
}

- (void)dealloc
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

	last = o;
	if (first == NULL)
		first = o;

	count++;

	if (retain_and_release)
		[obj retain];

	return o;
}

- (of_list_object_t*)prepend: (id)obj
{
	of_list_object_t *o;







<
|







66
67
68
69
70
71
72

73
74
75
76
77
78
79
80

	last = o;
	if (first == NULL)
		first = o;

	count++;


	[obj retain];

	return o;
}

- (of_list_object_t*)prepend: (id)obj
{
	of_list_object_t *o;
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

	first = o;
	if (last == NULL)
		last = o;

	count++;

	if (retain_and_release)
		[obj retain];

	return o;
}

- (of_list_object_t*)insert: (id)obj
		     before: (of_list_object_t*)listobj
{







<
|







89
90
91
92
93
94
95

96
97
98
99
100
101
102
103

	first = o;
	if (last == NULL)
		last = o;

	count++;


	[obj retain];

	return o;
}

- (of_list_object_t*)insert: (id)obj
		     before: (of_list_object_t*)listobj
{
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
	listobj->prev = o;

	if (listobj == first)
		first = o;

	count++;

	if (retain_and_release)
		[obj retain];

	return o;
}

- (of_list_object_t*)insert: (id)obj
		      after: (of_list_object_t*)listobj
{







<
|







114
115
116
117
118
119
120

121
122
123
124
125
126
127
128
	listobj->prev = o;

	if (listobj == first)
		first = o;

	count++;


	[obj retain];

	return o;
}

- (of_list_object_t*)insert: (id)obj
		      after: (of_list_object_t*)listobj
{
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
	listobj->next = o;

	if (listobj == last)
		last = o;

	count++;

	if (retain_and_release)
		[obj retain];

	return o;
}

- remove: (of_list_object_t*)listobj
{
	if (listobj->prev != NULL)
		listobj->prev->next = listobj->next;
	if (listobj->next != NULL)
		listobj->next->prev = listobj->prev;

	if (first == listobj)
		first = listobj->next;
	if (last == listobj)
		last = listobj->prev;

	count--;

	if (retain_and_release)
		[listobj->object release];

	[self freeMemory: listobj];

	return self;
}

- (size_t)count







<
|


















<
|







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
	listobj->next = o;

	if (listobj == last)
		last = o;

	count++;


	[obj retain];

	return o;
}

- remove: (of_list_object_t*)listobj
{
	if (listobj->prev != NULL)
		listobj->prev->next = listobj->next;
	if (listobj->next != NULL)
		listobj->next->prev = listobj->prev;

	if (first == listobj)
		first = listobj->next;
	if (last == listobj)
		last = listobj->prev;

	count--;


	[listobj->object release];

	[self freeMemory: listobj];

	return self;
}

- (size_t)count
209
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
244
245
246
247
248
249
250
251
252
253
254
255
	assert(iter == NULL && iter2 == NULL);

	return YES;
}

- (id)copy
{
	OFList *new;
	of_list_object_t *iter, *o, *prev;

	if (retain_and_release)
		new = [[OFList alloc] init];
	else
		new = [[OFList alloc] initWithoutRetainAndRelease];

	o = NULL;
	prev = NULL;

	@try {
		for (iter = first; iter != NULL; iter = iter->next) {
			o = [new allocMemoryWithSize: sizeof(of_list_object_t)];
			o->object = iter->object;
			o->next = NULL;
			o->prev = prev;

			if (new->first == NULL)
				new->first = o;
			if (prev != NULL)
				prev->next = o;

			new->count++;

			if (retain_and_release)
				[o->object retain];

			prev = o;
		}
	} @catch (OFException *e) {
		[new release];
		@throw e;
	}

	new->last = o;

	return new;
}
@end







|


<
<
<
<
<

















<
|













193
194
195
196
197
198
199
200
201
202





203
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
230
231
232
233
	assert(iter == NULL && iter2 == NULL);

	return YES;
}

- (id)copy
{
	OFList *new = [[OFList alloc] init];
	of_list_object_t *iter, *o, *prev;






	o = NULL;
	prev = NULL;

	@try {
		for (iter = first; iter != NULL; iter = iter->next) {
			o = [new allocMemoryWithSize: sizeof(of_list_object_t)];
			o->object = iter->object;
			o->next = NULL;
			o->prev = prev;

			if (new->first == NULL)
				new->first = o;
			if (prev != NULL)
				prev->next = o;

			new->count++;


			[o->object retain];

			prev = o;
		}
	} @catch (OFException *e) {
		[new release];
		@throw e;
	}

	new->last = o;

	return new;
}
@end

Modified tests/Makefile from [2533f0275e] to [c768dbff65].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
include ../extra.mk

SUBDIRS = OFAutoreleasePool	\
	  OFDataArray		\
	  OFArray		\
	  OFDictionary		\
	  OFHashes		\
	  ${OFPLUGIN}		\
	  OFTCPSocket		\
	  OFThread		\
	  OFList		\
	  OFXMLElement		\
	  OFXMLParser		\
	  ${OBJC_SYNC}

include ../buildsys.mk


<
|












1
2

3
4
5
6
7
8
9
10
11
12
13
14
15
include ../extra.mk


SUBDIRS = OFDataArray		\
	  OFArray		\
	  OFDictionary		\
	  OFHashes		\
	  ${OFPLUGIN}		\
	  OFTCPSocket		\
	  OFThread		\
	  OFList		\
	  OFXMLElement		\
	  OFXMLParser		\
	  ${OBJC_SYNC}

include ../buildsys.mk

Modified tests_new/main.m from [f0c02f400c] to [3bad531e54].

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "config.h"

#ifdef STDOUT
#include <stdio.h>
#endif
#include <stdlib.h>

#import <OFString.h>
#import <OFAutoreleasePool.h>

extern void object_tests();
extern void string_tests();

static int fails = 0;

static void







|
|







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "config.h"

#ifdef STDOUT
#include <stdio.h>
#endif
#include <stdlib.h>

#import "OFString.h"
#import "OFAutoreleasePool.h"

extern void object_tests();
extern void string_tests();

static int fails = 0;

static void