-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathISObject.c
119 lines (97 loc) · 2.51 KB
/
ISObject.c
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
/* Created By: Justin Meiners (2013) */
#include "ISObject.h"
struct ISAutoreleasePool
{
ISObjectRef* objectBuffer;
int bufferSize;
int objectCount;
};
#define MAX_AUTORELEASE_STACK_DEPTH 128
#define START_AUTORELEASE_BUFFER_SIZE 256
static struct ISAutoreleasePool _poolStatck[MAX_AUTORELEASE_STACK_DEPTH];
static int _poolDepth = 0;
void ISAutoreleasePoolPush()
{
_poolDepth++;
assert(_poolDepth < MAX_AUTORELEASE_STACK_DEPTH);
_poolStatck[_poolDepth].bufferSize = START_AUTORELEASE_BUFFER_SIZE;
_poolStatck[_poolDepth].objectBuffer = malloc(sizeof(ISObjectRef) * _poolStatck[_poolDepth].bufferSize);
_poolStatck[_poolDepth].objectCount = 0;
}
void ISAutoreleasePoolPop()
{
struct ISAutoreleasePool* pool = &_poolStatck[_poolDepth];
int i;
for (i = 0; i < pool->objectCount; i ++)
{
ISRelease(pool->objectBuffer[i]);
}
free(pool->objectBuffer);
_poolDepth--;
assert(_poolDepth >= 0);
}
static void _ISAutorelease(struct ISAutoreleasePool* pool, ISObjectRef object)
{
/* no autorelease pool */
assert(_poolDepth > 0);
if (pool->objectCount + 1 >= pool->bufferSize)
{
pool->bufferSize = ((pool->bufferSize + 1) * 3) / 2;
pool->objectBuffer = realloc(pool->objectBuffer, sizeof(ISObjectRef) * pool->bufferSize);
}
pool->objectBuffer[pool->objectCount] = object;
pool->objectCount++;
}
void ISObjectBaseInit(ISObjectBase* base, ISObjectClass* objClass)
{
base->retainCount = 1;
base->objectClass = objClass;
}
ISObjectRef ISRetain(ISObjectRef object)
{
if (!object)
{
return NULL;
}
ISObjectBase* base = (ISObjectBase*)object;
base->retainCount++;
return object;
}
void ISRelease(ISObjectRef object)
{
if (object)
{
ISObjectBase* base = (ISObjectBase*)object;
base->retainCount--;
if (base->retainCount < 1)
{
base->objectClass->_deallocFunc(object);
}
}
}
ISObjectRef ISAutorelease(ISObjectRef object)
{
if (object)
{
_ISAutorelease(&_poolStatck[_poolDepth], object);
}
return object;
}
int ISObjectRetainCount(ISObjectRef object)
{
if (!object)
{
return 0;
}
ISObjectBase* base = (ISObjectBase*)object;
return base->retainCount;
}
ISObjectRef ISCopy(ISObjectRef object)
{
if (!object)
{
return NULL;
}
ISObjectBase* base = (ISObjectBase*)object;
return base->objectClass->_copyFunc(object);
}