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
|
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
|
-
-
-
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
+
-
-
-
+
+
-
+
+
+
-
+
-
+
+
+
+
-
+
-
+
+
+
+
-
+
+
-
+
|
* the packaging of this file.
*/
#import "config.h"
#include <stdlib.h>
#ifndef _WIN32
#include <pthread.h>
#endif
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "OFList.h"
#import "OFList.h"
#import "OFThread.h"
#import "OFExceptions.h"
#ifdef _WIN32
#include <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;
static OFTLSKey *pool_list_key;
#endif
#ifndef _WIN32
static void
release_list(void *list)
{
of_list_object_t *first, *iter;
IMP release;
if ((first = [(OFList*)list first]) != NULL)
release = [first->object methodFor: @selector(release)];
for (iter = first; iter != NULL; iter = iter->next)
release(iter->object, @selector(release));
[(OFList*)list release];
}
#endif
@implementation OFAutoreleasePool
+ initialize
{
#ifndef _WIN32
if (pthread_key_create(&pool_list_key, release_list))
@throw [OFInitializationFailedException newWithClass: self];
#else
/* FIXME: Free stuff when thread is terminated! */
if ((pool_list_key = TlsAlloc()) == TLS_OUT_OF_INDEXES)
pool_list_key = [[OFTLSKey alloc] initWithDestructor: release_list];
@throw [OFInitializationFailedException newWithClass: self];
#endif
return self;
}
+ (void)addToPool: (OFObject*)obj
{
OFList *pool_list = get_tls(pool_list_key);
OFList *pool_list;
@try {
if (pool_list == nil || [pool_list last] == NULL) {
pool_list = [OFThread objectForTLSKey: pool_list_key];
} @catch (OFNotInSetException *e) {
[e free];
[[self alloc] init];
pool_list = get_tls(pool_list_key);
pool_list = [OFThread objectForTLSKey: pool_list_key];
}
if (pool_list == nil || [pool_list last] == NULL)
if ([pool_list last] == NULL)
[[self alloc] init];
if ([pool_list last] == NULL)
@throw [OFInitializationFailedException newWithClass: self];
[[pool_list last]->object addToPool: obj];
}
- init
{
OFList *pool_list;
self = [super init];
objects = nil;
pool_list = get_tls(pool_list_key);
@try {
if (pool_list == nil) {
pool_list = [OFThread objectForTLSKey: pool_list_key];
} @catch (OFNotInSetException *e) {
[e free];
pool_list = [[OFList alloc] initWithoutRetainAndRelease];
[OFThread setObject: pool_list
set_tls(pool_list_key, pool_list);
forTLSKey: pool_list_key];
[pool_list release];
}
listobj = [pool_list append: self];
return self;
}
- free
{
[(OFList*)get_tls(pool_list_key) remove: listobj];
[[OFThread objectForTLSKey: pool_list_key] remove: listobj];
return [super free];
}
- addToPool: (OFObject*)obj
{
if (objects == nil)
|