ObjFW  Check-in [dfc10dbeb8]

Overview
Comment:Use OFList and OFArray in OFAutoreleasePool.
This reduces code duplication and looks far better.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: dfc10dbeb86a9c0718200875fb594ac02edaaed29236448560bfb11109a0ff6d
User & Date: js on 2009-01-06 22:38:25
Other Links: manifest | tags
Context
2009-01-07
17:10
Also release the pools when we release the pool list.
We need to do that manually as we disabled retain / release for the
list.
check-in: 03618ea87b user: js tags: trunk
2009-01-06
22:38
Use OFList and OFArray in OFAutoreleasePool.
This reduces code duplication and looks far better.
check-in: dfc10dbeb8 user: js tags: trunk
2009-01-05
22:18
Reworked OFList. check-in: 8ab6561840 user: js tags: trunk
Changes

Modified src/OFArray.h from [02a145515d] to [e7a8933e40].

66
67
68
69
70
71
72
73

74
75
76
77
78
79
80
66
67
68
69
70
71
72

73
74
75
76
77
78
79
80







-
+







 * \return The last item of the OFArray
 */
- (void*)last;

/**
 * Adds an item to the OFArray.
 *
 * \param item An arbitrary item
 * \param item A pointer to an arbitrary item
 */
- add: (void*)item;

/**
 * Adds items from a C array to the OFArray.
 *
 * \param nitems The number of items to add

Modified src/OFAutoreleasePool.h from [b23ad42b20] to [d4653e60d0].

1
2
3
4
5
6
7
8
9
10
11
12


13
14
15
16
17
18
19
20
21
22


23
24
25
26
27
28
29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22


23
24
25
26
27
28
29
30
31












+
+








-
-
+
+







/*
 * Copyright (c) 2008 - 2009
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFObject.h"
#import "OFArray.h"
#import "OFList.h"

/**
 * 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
{
	OFObject **objects;
	size_t	 size;
	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
37
38
39
40
41
42
43
44

45
46
47
48
49
50
39
40
41
42
43
44
45

46





47







-
+
-
-
-
-
-

 * \param obj The object to add to the autorelease pool
 */
- addToPool: (OFObject*)obj;

/**
 * Releases all objects in the autorelease pool.
 */
- release;
- releaseObjects;

/**
 * \returns All objects in the autorelease pool
 */
- (OFObject**)objects;
@end

Modified src/OFAutoreleasePool.m from [e28fa49a4f] to [8a91012795].

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
196
197
198
199
200
201
202
203
204
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







+






+
-
-
+
+

+
+
-
+
-




-
+

-
-
-
-
-
-
+
-
-







-
-
-
+
-

-


-
+

-
-
-
-
-





-
-
+

-
-
-
-
-
-
-
-
+
-

-
+
-
-
-
-
-
-
-


-
+


-
+




-
-
+
-


-
+
-

-
-
-
-
-
+
-
-

-
+
-
-
+
-
-
-
-
+
-
-
-
-
-
+
-


-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-







-
+






-
-
+
+

-
+

-
-
-
-
+
+
+
-
-
-
-
+
+
+
-
-

-
+


-
+

-
+
+

-
+
-
-
+

-
-
+
+
+
+
+

+
-
+
-



-
-
-
-
-


#ifndef _WIN32
#import <pthread.h>
#endif

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

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

#ifndef _WIN32
#define get_tls(t) pthread_getspecific(pool_list_key)
static pthread_key_t pool_stack_key;
static pthread_key_t pool_index_key;
#define set_tls(t, v) pthread_setspecific(t, v)
static pthread_key_t pool_list_key;
#else
#define get_tls(t) TlsGetValue(t)
#define set_tls(t, v) TlsSetValue(t, v)
static DWORD pool_stack_key;
static DWORD pool_list_key;
static DWORD pool_index_key;
#endif

#ifndef _WIN32
static void
free_each(void *ptr)
release_obj(void *obj)
{
	OFAutoreleasePool **p;
	OFObject **o;

	for (p = (OFAutoreleasePool**)ptr; *p != nil; p++) {
		for (o = [*p objects]; *o != nil; o++)
			[*o release];
	[(OFObject*)obj release];
		[*p free];
	}
}
#endif

@implementation OFAutoreleasePool
+ (void)initialize
{
#ifndef _WIN32
	if (pthread_key_create(&pool_stack_key, free_each))
		@throw [OFInitializationFailedException newWithClass: self];
	if (pthread_key_create(&pool_index_key, free)) {
	if (pthread_key_create(&pool_list_key, release_obj))
		pthread_key_delete(pool_stack_key);
		@throw [OFInitializationFailedException newWithClass: self];
	}
#else
	/* FIXME: Free stuff when thread is terminated! */
	if ((pool_stack_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
	if ((pool_list_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
		@throw [OFInitializationFailedException newWithClass: self];
	if ((pool_index_key = TlsAlloc()) == TLS_OUT_OF_INDEXES) {
		TlsFree(pool_stack_key);
		@throw [OFInitializationFailedException newWithClass: self];
	}

#endif
}

+ (void)addToPool: (OFObject*)obj
{
	OFAutoreleasePool **pool_stack;
	int *pool_index;
	OFList *pool_list = get_tls(pool_list_key);

#ifndef _WIN32
	pool_stack = pthread_getspecific(pool_stack_key);
	pool_index = pthread_getspecific(pool_index_key);
#else
	pool_stack = TlsGetValue(pool_stack_key);
	pool_index = TlsGetValue(pool_index_key);
#endif

	if (pool_list == nil) {
	if (pool_stack == NULL || pool_index == NULL) {
		[[self alloc] init];

		pool_list = get_tls(pool_list_key);
#ifndef _WIN32
		pool_stack = pthread_getspecific(pool_stack_key);
		pool_index = pthread_getspecific(pool_index_key);
#else
		pool_stack = TlsGetValue(pool_stack_key);
		pool_index = TlsGetValue(pool_index_key);
#endif
	}

	if (*pool_stack == nil || *pool_index == -1)
	if (pool_list == nil || [pool_list last] == NULL)
		@throw [OFInitializationFailedException newWithClass: self];

	[pool_stack[*pool_index] addToPool: obj];
	[[pool_list last]->object addToPool: obj];
}

- init
{
	OFAutoreleasePool **pool_stack, **pool_stack2;
	int *pool_index;
	OFList *pool_list;
	Class c;

	if ((self = [super init])) {
		objects = NULL;
		objects = nil;
		size = 0;

#ifndef _WIN32
		pool_stack = pthread_getspecific(pool_stack_key);
		pool_index = pthread_getspecific(pool_index_key);
#else
		pool_stack = TlsGetValue(pool_stack_key);
		pool_list = get_tls(pool_list_key);
		pool_index = TlsGetValue(pool_index_key);
#endif

		if (pool_index == NULL) {
		if (pool_list == nil) {
			if ((pool_index = malloc(sizeof(int))) == NULL) {
				c = [self class];
			pool_list = [[OFList alloc]
				[super free];
				@throw [OFNoMemException newWithClass: c];
			}

			    initWithRetainAndReleaseEnabled: NO];
			*pool_index = -1;
#ifndef _WIN32
			pthread_setspecific(pool_index_key, pool_index);
#else
			TlsSetValue(pool_index_key, pool_index);
			set_tls(pool_list_key, pool_list);
#endif
		}

		if ((pool_stack2 = realloc(pool_stack,
		    (*pool_index + 3) * sizeof(OFAutoreleasePool*))) == NULL) {
			c = [self class];
			[super free];
			@throw [OFNoMemException newWithClass: c];
		}
		pool_stack = pool_stack2;
#ifndef _WIN32
		pthread_setspecific(pool_stack_key, pool_stack);
#else
		TlsSetValue(pool_stack_key, pool_stack);
#endif
		(*pool_index)++;

		listobj = [pool_list append: self];
		pool_stack[*pool_index] = self;
		pool_stack[*pool_index + 1] = nil;
	}

	return self;
}

- free
{
	[self release];
	[(OFList*)get_tls(pool_list_key) remove: listobj];

	return [super free];
}

- addToPool: (OFObject*)obj
{
	OFObject **objects2;
	size_t size2;
	if (objects == nil)
		objects = [OFArray newWithItemSize: sizeof(char*)];

	size2 = size + 1;
	[objects add: &obj];

	if (SIZE_MAX - size < 1 || size2 > SIZE_MAX / sizeof(OFObject*))
		@throw [OFOutOfRangeException newWithClass: [self class]];

	if ((objects2 = realloc(objects, size2 * sizeof(OFObject*))) == NULL)
	return self;
}

		@throw [OFNoMemException newWithClass: [self class]
					      andSize: size2];

	objects = objects2;
- release
{
	[self releaseObjects];
	objects[size] = obj;
	size = size2;

	return self;
	return [super release];
}

- release
- releaseObjects
{
	size_t i;
	size_t i, size;
	IMP get_item;

	if (objects != NULL) {
	if (objects == nil)
		for (i = 0; size < i; i++)
			[objects[i] release];
		return self;

		free(objects);
	}
	size = [objects items];
	get_item = [objects methodFor: @selector(item:)];

	for (i = 0; i < size; i++)
		[*((OFObject**)get_item(objects, @selector(item:), i)) release];

	[objects release];
	objects = NULL;
	objects = nil;
	size = 0;

	return self;
}

- (OFObject**)objects
{
	return objects;
}
@end

Modified src/OFList.h from [8e66167c8d] to [439fd52763].

21
22
23
24
25
26
27

28
29









30
31
32
33
34
35
36
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







+


+
+
+
+
+
+
+
+
+







/**
 * The OFList class provides easy to use double-linked lists.
 */
@interface OFList: OFObject
{
	of_list_object_t *first;
	of_list_object_t *last;
	BOOL		 retain_and_release;
}

/**
 * Initializes an already allocated OFList.
 *
 * \param enabled Whether release / retain should be called when an object is
 *	  added / removed. Default: YES
 * \return An initialized OFList
 */
- initWithRetainAndReleaseEnabled: (BOOL)enabled;

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

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

Modified src/OFList.m from [55395f28ca] to [f248439ac0].

14
15
16
17
18
19
20
21













22
23
24
25
26
27
28
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







-
+
+
+
+
+
+
+
+
+
+
+
+
+







#import "OFList.h"

@implementation OFList
- init
{
	if ((self = [super init])) {
		first = NULL;
		last  = NULL;
		last = NULL;
		retain_and_release = YES;
	}

	return self;
}

- initWithRetainAndReleaseEnabled: (BOOL)enabled
{
	if ((self = [super init])) {
		first = NULL;
		last = NULL;
		retain_and_release = enabled;
	}

	return self;
}

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







+
-
+
+


















+
-
+
+




















+
-
+
+




















+
-
+
+















+
+
+

+



	if (last != NULL)
		last->next = o;

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

	if (retain_and_release)
	[obj retain];
		[obj retain];

	return o;
}

- (of_list_object_t*)prepend: (id)obj
{
	of_list_object_t *o = [self getMemWithSize: sizeof(of_list_object_t)];

	o->object = obj;
	o->next = first;
	o->prev = NULL;

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

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

	if (retain_and_release)
	[obj retain];
		[obj retain];

	return o;
}

- (of_list_object_t*)insert: (id)obj
		     before: (of_list_object_t*)listobj
{
	of_list_object_t *o = [self getMemWithSize: sizeof(of_list_object_t)];

	o->object = obj;
	o->next = listobj;
	o->prev = listobj->prev;

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

	listobj->prev = o;

	if (listobj == first)
		first = o;

	if (retain_and_release)
	[obj retain];
		[obj retain];

	return o;
}

- (of_list_object_t*)insert: (id)obj
		      after: (of_list_object_t*)listobj
{
	of_list_object_t *o = [self getMemWithSize: sizeof(of_list_object_t)];

	o->object = obj;
	o->next = listobj->next;
	o->prev = listobj;

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

	listobj->next = o;

	if (listobj == last)
		last = o;

	if (retain_and_release)
	[obj retain];
		[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;

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

	[self freeMem: listobj];

	return self;
}
@end

Modified src/OFObject.h from [743bd758c4] to [8a674581d8].

42
43
44
45
46
47
48





49
50
51
52
53
54
55
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60







+
+
+
+
+







- release;

/**
 * Adds the object to the autorelease pool that is on top of the thread's stack.
 */
- autorelease;

/**
 * \return The retain count
 */
- (size_t)retainCount;

/**
 * Adds a pointer to the memory pool.
 * This is useful to add memory allocated by functions such as asprintf to the
 * pool so it gets freed automatically when the object is freed.
 *
 * \param ptr A pointer to add to the memory pool
 */

Modified src/OFObject.m from [c1018bac67] to [f1ae449a76].

62
63
64
65
66
67
68





69
70
71
72
73
74
75
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80







+
+
+
+
+








- autorelease
{
	[OFAutoreleasePool addToPool: self];

	return self;
}

- (size_t)retainCount
{
	return __retain_count;
}

- addToMemoryPool: (void*)ptr
{
	void **memchunks;
	size_t memchunks_size;

	memchunks_size = __memchunks_size + 1;

Modified tests/OFArray/OFArray.m from [f659184241] to [211a3acfce].

95
96
97
98
99
100
101
102

103
104
105
106
107
108
109
95
96
97
98
99
100
101

102
103
104
105
106
107
108
109







-
+







									\
	[a free];							\
									\
	puts("Creating new array and using it to build a string...");	\
	a = [type newWithItemSize: 1];					\
									\
	for (i = 0; i < strlen(str); i++)				\
		[a add: (void*)(str + i)];				\
		[a add: (void*)&str[i]];				\
	[a add: ""];							\
									\
	if (!strcmp([a data], str))					\
		puts("Built string matches!");				\
	else {								\
		puts("Built string does not match!");			\
		abort();						\

Modified tests/OFAutoreleasePool/OFAutoreleasePool.m from [a5d1f5a43b] to [389325218a].

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







+
-
+
+
+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+



+
+







-
+











 * the packaging of this file.
 */

#import "config.h"

#import "OFAutoreleasePool.h"

#import <stdio.h>
/* FIXME: Just crashtests */

#ifndef _WIN32
#define ZD "%zd"
#else
#define ZD "%u"
#endif

@interface TestObject: OFObject
- init;
- retain;
- release;
@end

@implementation TestObject
- init
{
	id ret;
       
	ret = [super init];
	printf("New %s with retain cnt " ZD "\n", [self name],
	    [ret retainCount]);

	return ret;
}

- retain
{
	id ret;

	ret = [super retain];
	printf("Retaining %s to " ZD "\n", [self name], [ret retainCount]);

	return ret;
}

- release
{
	printf("Releasing %s to " ZD "\n", [self name], [self retainCount] - 1);

	return [super release];
}
@end

int
main()
{
	[TestObject poseAs: [OFObject class]];

	OFObject *o1, *o2, *o3;
	OFAutoreleasePool *pool1, *pool2;

	o1 = [[OFObject new] autorelease];

	pool1 = [OFAutoreleasePool new];
	o2 = [[OFObject new] autorelease];
	[pool1 release];
	[pool1 releaseObjects];

	o2 = [[OFObject new] autorelease];

	pool2 = [OFAutoreleasePool new];
	o3 = [[OFObject new] autorelease];

	[pool1 release];
	[o3 free];

	return 0;
}