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
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
|
#include "MCAutoreleasePool.h"
#include <libetpan/libetpan.h>
#include "MCString.h"
#include "MCLog.h"
#include "MCUtils.h"
using namespace mailcore;
pthread_key_t AutoreleasePool::autoreleasePoolStackKey;
void AutoreleasePool::init()
{
static pthread_once_t once = PTHREAD_ONCE_INIT;
pthread_once(&once, initAutoreleasePoolStackKey);
}
AutoreleasePool::AutoreleasePool()
{
mPoolObjects = carray_new(4);
#if __APPLE__
mAppleAutoreleasePool = createAppleAutoreleasePool();
#endif
unsigned int idx;
carray * stack = createAutoreleasePoolStackIfNeeded();
carray_add(stack, this, &idx);
}
AutoreleasePool::~AutoreleasePool()
{
#if __APPLE__
releaseAppleAutoreleasePool(mAppleAutoreleasePool);
#endif
carray * stack = createAutoreleasePoolStackIfNeeded();
carray_delete_slow(stack, carray_count(stack) - 1);
unsigned int count = carray_count(mPoolObjects);
for(unsigned int i = 0 ; i < count ; i ++) {
Object * obj = (Object *) carray_get(mPoolObjects, i);
obj->release();
}
carray_free(mPoolObjects);
}
carray * AutoreleasePool::createAutoreleasePoolStackIfNeeded()
{
init();
carray * stack = (carray *) pthread_getspecific(autoreleasePoolStackKey);
if (stack != NULL) {
return stack;
}
stack = carray_new(4);
pthread_setspecific(autoreleasePoolStackKey, stack);
return stack;
}
void AutoreleasePool::destroyAutoreleasePoolStack(void * value)
{
carray * stack = (carray *) value;
if (carray_count(stack) != 0) {
MCLog("some autoreleasepool have not been released\n");
}
carray_free(stack);
}
void AutoreleasePool::initAutoreleasePoolStackKey()
{
pthread_key_create(&autoreleasePoolStackKey, destroyAutoreleasePoolStack);
}
AutoreleasePool * AutoreleasePool::currentAutoreleasePool()
{
init();
carray * stack;
stack = createAutoreleasePoolStackIfNeeded();
if (carray_count(stack) == 0) {
//fprintf(stderr, "no current autoreleasepool\n");
return NULL;
}
AutoreleasePool * pool;
pool = (AutoreleasePool *) carray_get(stack, carray_count(stack) - 1);
return pool;
}
void AutoreleasePool::add(Object * obj)
{
unsigned int idx;
carray_add(mPoolObjects, obj, &idx);
}
void AutoreleasePool::autorelease(Object * obj)
{
AutoreleasePool * pool = AutoreleasePool::currentAutoreleasePool();
if (pool == NULL) {
MCLog("-autorelease called with no current autoreleasepool\n");
return;
}
pool->add(obj);
}
String * AutoreleasePool::description()
{
String * result = String::string();
result->appendUTF8Format("<%p:%p ", className(), this);
unsigned int count = carray_count(mPoolObjects);
for(unsigned int i = 0 ; i < count ; i ++) {
Object * obj = (Object *) carray_get(mPoolObjects, i);
if (i != 0) {
result->appendUTF8Characters(" ");
}
result->appendString(obj->description());
}
result->appendUTF8Characters(">");
return result;
}
|