-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathobj_pool.h
239 lines (208 loc) · 4.46 KB
/
obj_pool.h
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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// obj_pool.h
// A general-purpose object pool
//
//
// 2013-08-28 shaneyu Created
// 2013-10-12 shaneyu Use pointer in set<> instead of object
// 2014-02-04 shaneyu Use pointer to replace iterator
// 2014-03-15 shaneyu Remove the use of vector
//
#include <stdint.h>
#include <string.h>
#include "obj_base.h"
extern "C"
{
#include "Attr_API.h"
}
#ifndef __UC_OBJ_POOL__
#define __UC_OBJ_POOL__
using namespace std;
//
// 动态管理多个对象集合的对象池
//
// 通过指针实现
// 对 obj_type 的约束:
// 1, obj_type必须包含int类型的curr和next成员变量
// 2, obj_type必须实现ReleaseObject()成员函数,回收本对象的资源
//
template<class obj_type> class ObjPool
{
public:
ObjPool() : obj_freelist(NULL) { }
~ObjPool() { while(obj_freelist) { obj_type *next=(obj_type *)obj_freelist->next; delete obj_freelist; obj_freelist = next; } }
obj_type *New(); // 创建独立对象
int Delete(obj_type *obj); // 删除独立对象obj
obj_type *New(obj_type *&first); // 创建对象并插到first指向的链表尾部
obj_type *NewFront(obj_type *&first); // 创建对象并插到first指向的链表头部
int Delete(obj_type *&first, obj_type *obj); // 从first指向的对象链表中删除对象obj
int Detach(obj_type *&first, obj_type *obj); // 从first指向的对象链表中移除对象obj,用户需要调用Delete(obj)将其释放
int DeleteAll(obj_type *&first); // 删除first指向的整个链表
int AddToFreeList(obj_type *first);
private:
obj_type *obj_freelist; // 释放后的obj放这里
};
//
// 实现部分
//
template<class obj_type> inline obj_type *ObjPool<obj_type>::New()
{
obj_type *o;
if(obj_freelist)
{
o = obj_freelist;
obj_freelist = (obj_type *)obj_freelist->next;
}
else
{
Attr_API(390992, 1);
try {
o = new obj_type();
}catch(...) {
Attr_API(391020, 1);
return NULL;
}
}
// 插入到obj链表
o->next = NULL;
o->prev = o;
return o;
}
template<class obj_type> inline int ObjPool<obj_type>::Delete(obj_type *obj)
{
obj->ReleaseObject(); // NOTE: ReleaseObject() should not change prev/next pointers
// 将obj放到free链表
obj->next = obj_freelist;
obj_freelist = obj;
return 0;
}
template<class obj_type> inline obj_type *ObjPool<obj_type>::New(obj_type *&first)
{
obj_type *o;
if(obj_freelist)
{
o = obj_freelist;
obj_freelist = (obj_type *)obj_freelist->next;
}
else
{
Attr_API(390992, 1);
try {
o = new obj_type();
}catch(...) {
Attr_API(391020, 1);
return NULL;
}
}
// 插入到obj链表
o->next = NULL;
if(first)
{
obj_type *lst = (obj_type *)first->prev; // must not be null
lst->next = o;
o->prev = lst;
first->prev = o;
}
else
{
first = o;
o->prev = o;
}
return o;
}
template<class obj_type> inline obj_type *ObjPool<obj_type>::NewFront(obj_type *&first)
{
obj_type *o;
if(obj_freelist)
{
o = obj_freelist;
obj_freelist = (obj_type *)obj_freelist->next;
}
else
{
Attr_API(390992, 1);
try {
o = new obj_type();
}catch(...) {
Attr_API(391020, 1);
return NULL;
}
}
// 插入到obj链表头部
o->next = first;
if(first)
{
o->prev = first->prev;
first->prev = o;
}
else
{
o->prev = o;
}
first = o;
return o;
}
template<class obj_type> inline int ObjPool<obj_type>::Delete(obj_type *&first, obj_type *obj)
{
if(obj==first)
{
if(obj->next) // new first
((obj_type *)obj->next)->prev = first->prev;
first = (obj_type *)obj->next;
}
else
{
obj_type *p = (obj_type *)obj->prev; // must not null
obj_type *n = (obj_type *)obj->next;
p->next = n;
if(n)
n->prev = p;
else // obj is the last
first->prev = p;
}
obj->ReleaseObject();
obj->next = obj_freelist;
obj_freelist = obj;
return 0;
}
template<class obj_type> inline int ObjPool<obj_type>::Detach(obj_type *&first, obj_type *obj)
{
if(obj==first)
{
if(obj->next) // new first
((obj_type *)obj->next)->prev = first->prev;
first = (obj_type *)obj->next;
}
else
{
obj_type *p = (obj_type *)obj->prev; // must not null
obj_type *n = (obj_type *)obj->next;
p->next = n;
if(n)
n->prev = p;
else // obj is the last
first->prev = p;
}
obj->next = obj->prev = NULL;
return 0;
}
template<class obj_type> inline int ObjPool<obj_type>::AddToFreeList(obj_type *obj)
{
((obj_type *)obj->prev)->next = obj_freelist;
obj_freelist = obj;
return 0;
}
template<class obj_type> inline int ObjPool<obj_type>::DeleteAll(obj_type *&first)
{
obj_type *o = first;
while(o)
{
o->ReleaseObject(); // NOTE: ReleaseObject() should not change prev/next pointers
o = (obj_type *)o->next;
}
// 将obj放到free链表
AddToFreeList(first);
first = NULL;
return 0;
}
#endif