ObjFW  Diff

Differences From Artifact [e28fa49a4f]:

To Artifact [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

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

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


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

#ifndef _WIN32

static pthread_key_t pool_stack_key;
static pthread_key_t pool_index_key;
#else


static DWORD pool_stack_key;
static DWORD pool_index_key;
#endif

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

	for (p = (OFAutoreleasePool**)ptr; *p != nil; p++) {
		for (o = [*p objects]; *o != nil; o++)
			[*o 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)) {
		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)
		@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;

#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 == NULL || pool_index == NULL) {
		[[self alloc] init];

#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)
		@throw [OFInitializationFailedException newWithClass: self];

	[pool_stack[*pool_index] addToPool: obj];
}

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

	if ((self = [super init])) {
		objects = NULL;
		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_index = TlsGetValue(pool_index_key);
#endif

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

			*pool_index = -1;
#ifndef _WIN32
			pthread_setspecific(pool_index_key, pool_index);
#else
			TlsSetValue(pool_index_key, pool_index);
#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)++;

		pool_stack[*pool_index] = self;
		pool_stack[*pool_index + 1] = nil;
	}

	return self;
}

- free
{
	[self release];

	return [super free];
}

- addToPool: (OFObject*)obj
{
	OFObject **objects2;
	size_t size2;

	size2 = size + 1;

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

	if ((objects2 = realloc(objects, size2 * sizeof(OFObject*))) == NULL)
		@throw [OFNoMemException newWithClass: [self class]
					      andSize: size2];


	objects = objects2;
	objects[size] = obj;
	size = size2;

	return self;
}

- release
{
	size_t i;


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

		free(objects);

	}




	objects = NULL;
	size = 0;

	return self;
}

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







>






>
|
|

>
>
|
<




|

<
<
<
<
<
|
<
<







<
<
|
<

<


|

<
<
<
<
<





<
|

<
<
<
<
<
<
<
|
<

|
<
<
<
<
<
<
<


|


|




<
|
<


|
<

<
<
<
<
|
<
<

|
<
|
<
<
<
|
<
<
<
<
|
<


<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<







|






|
|

|

<
|
|
|
<
<
>
|
|
<
<

|


|

|
>

|
<
|

|
>
|
>
>

>
|
<



<
<
<
<
<

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

#endif

#ifndef _WIN32
static void
release_obj(void *obj)
{





	[(OFObject*)obj release];


}
#endif

@implementation OFAutoreleasePool
+ (void)initialize
{
#ifndef _WIN32


	if (pthread_key_create(&pool_list_key, release_obj))

		@throw [OFInitializationFailedException newWithClass: self];

#else
	/* FIXME: Free stuff when thread is terminated! */
	if ((pool_list_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
		@throw [OFInitializationFailedException newWithClass: self];





#endif
}

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

	OFList *pool_list = get_tls(pool_list_key);








	if (pool_list == nil) {

		[[self alloc] init];
		pool_list = get_tls(pool_list_key);







	}

	if (pool_list == nil || [pool_list last] == NULL)
		@throw [OFInitializationFailedException newWithClass: self];

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

- init
{

	OFList *pool_list;


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






		pool_list = get_tls(pool_list_key);



		if (pool_list == nil) {

			pool_list = [[OFList alloc]



			    initWithRetainAndReleaseEnabled: NO];




			set_tls(pool_list_key, pool_list);

		}














		listobj = [pool_list append: self];


	}

	return self;
}

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

	return [super free];
}

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

	[objects add: &obj];


	return self;
}



- release
{
	[self releaseObjects];



	return [super release];
}

- releaseObjects
{
	size_t i, size;
	IMP get_item;

	if (objects == nil)

		return self;

	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 = nil;


	return self;
}





@end