forked from fantaisie-software/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.c
243 lines (218 loc) · 5.2 KB
/
pool.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
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
240
241
242
/* pool - a container of recycleable objects
* Copyright (c) 2002 Michael B. Allen <mba2000 ioplex.com>
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include "mba/msgno.h"
#include "mba/allocator.h"
#include "mba/iterator.h"
#include "mba/stack.h"
#include "mba/bitset.h"
#include "mba/pool.h"
int
pool_create(struct pool *p,
unsigned int max_size,
new_fn object_new,
del_fn object_del,
rst_fn object_rst,
void *context,
size_t size,
int flags,
struct allocator *al)
{
if (p == NULL || object_new == NULL) {
PMNO(errno = EINVAL);
return -1;
}
if (max_size == 0 || max_size > POOL_SIZE_MAX) {
max_size = POOL_SIZE_MAX;
}
p->al = al;
if ((p->bitset = allocator_alloc(p->al, max_size / 8 + 1, 1)) == NULL ||
stack_init(&p->stk, max_size, p->al) == -1) {
PMNO(errno);
allocator_free(p->al, p->bitset);
return -1;
}
p->object_new = object_new;
p->object_del = object_del;
p->object_rst = object_rst;
p->context = context;
p->size = size;
p->flags = flags;
p->max_size = max_size;
p->unused = 0;
return 0;
}
int
pool_destroy(struct pool *p)
{
if (p && (stack_deinit(&p->stk, p->object_del, p->context) +
allocator_free(p->al, p->bitset)) != 0) {
AMSG("");
return -1;
}
return 0;
}
struct pool *
pool_new(unsigned int max_size,
new_fn object_new,
del_fn object_del,
rst_fn object_rst,
void *context,
size_t size,
int flags,
struct allocator *al)
{
struct pool *p;
if ((p = allocator_alloc(al, sizeof *p, 0)) == NULL ||
pool_create(p, max_size, object_new, object_del, object_rst,
context, size, flags, al) == -1) {
AMSG("");
return NULL;
}
return p;
}
int
pool_del(struct pool *p)
{
int ret = 0;
if (p) {
ret += pool_destroy(p);
ret += allocator_free(p->al, p);
}
return ret ? -1 : 0;
}
void *
pool_get(struct pool *p)
{
unsigned int lim, n;
void *obj;
if (p == NULL) {
PMNO(errno = ERANGE);
return NULL;
}
if (p->unused == 0 && stack_size(&p->stk) == p->max_size) {
PMNF(errno = ERANGE, ": %d", p->max_size);
return NULL;
}
lim = p->max_size / 8 + 1;
if ((n = bitset_find_first(p->bitset, p->bitset + lim, 0)) == (unsigned int)-1) {
PMNO(errno = ERANGE);
return NULL;
}
if ((n == stack_size(&p->stk))) {
size_t size = p->size == (size_t)-1 ? n : p->size;
/* If p->size is -1 then 'size' is really the index. This is
* only used by svsem.
*/
if ((obj = p->object_new(p->context, size, p->flags)) == NULL) {
AMSG("");
return NULL;
}
if (stack_push(&p->stk, obj) == -1) {
AMSG("");
p->object_del(p->context, obj);
return NULL;
}
} else {
if ((obj = stack_get(&p->stk, n)) == NULL) {
AMSG("");
return NULL;
}
if (p->object_rst && p->object_rst(p->context, obj) == -1) {
AMSG("");
return NULL;
}
p->unused--;
}
bitset_set(p->bitset, n);
return obj;
}
int
pool_release(struct pool *p, void *data)
{
if (!data) return 0;
if (p) {
iter_t iter;
int n;
void *d;
stack_iterate(&p->stk, &iter);
for (n = 0; (d = stack_next(&p->stk, &iter)) != NULL; n++) {
if (d == data) {
bitset_unset(p->bitset, n);
p->unused++;
return 0;
}
}
}
PMNO(errno = EINVAL);
return -1;
}
unsigned int
pool_size(struct pool *p)
{
return p == NULL ? 0 : stack_size(&p->stk);
}
unsigned int
pool_unused(struct pool *p)
{
return p == NULL ? 0 : p->unused;
}
void
pool_iterate(void *p, iter_t *iter)
{
if (p) { /* should we bother to check? */
struct pool *p0 = p;
stack_iterate(&p0->stk, iter);
}
}
void *
pool_next(void *_p, iter_t *iter)
{
struct pool *p = _p;
return stack_next(&p->stk, iter);
}
int
pool_clean(struct pool *p)
{
int idx;
idx = stack_size(&p->stk);
if (idx-- && p->object_del) {
int count = 0;
while (!bitset_isset(p->bitset, idx--)) {
if (p->object_del(p->context, stack_pop(&p->stk)) == -1) {
AMSG("");
return -1;
}
count++;
}
if (stack_clean(&p->stk) == -1) {
AMSG("");
return -1;
}
return count;
}
return 0;
}