-
Notifications
You must be signed in to change notification settings - Fork 1
/
pool.c
134 lines (114 loc) · 2.57 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
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include "pool.h"
typedef struct pool {
void *pool;
struct pool *next;
} pool;
struct poolset {
pool *pools; /* link to first pool */
pool *active; /* link to active pool */
size_t length; /* size of each pool */
size_t atom_size; /* size of each element within a pool */
size_t last_i; /* allocation index */
int (*empty)(void *); /* callback to check if an element is empty/unused */
};
poolset *new_poolset(uint64_t pool_length, uint64_t atom_size)
{
poolset *ps;
if (pool_length == 0 || atom_size == 0) {
return NULL;
}
ps = (poolset *)calloc(1, sizeof(poolset));
if (!ps) {
return NULL;
}
ps->length = pool_length;
ps->atom_size = atom_size;
return ps;
}
void delete_poolset(poolset *ps)
{
pool *node, *tmp;
for (node = ps->pools; node;) {
free(node->pool);
tmp = node->next;
free(node);
node = tmp;
}
free(ps);
}
static int alloc_pool(poolset *ps)
{
void *tmp;
pool *node;
int rc;
if (!ps) {
return -1;
}
node = (pool *)malloc(sizeof(pool));
if (!node) {
rc = -errno;
return rc;
}
node->next = NULL;
assert(ps->length && ps->atom_size);
tmp = calloc(ps->length, ps->atom_size);
if (!tmp) {
rc = -errno;
free(node);
return rc;
}
node->pool = tmp;
if (ps->pools == NULL) {
ps->pools = node;
} else {
assert(ps->active);
ps->active->next = node;
}
ps->active = node;
ps->last_i = 0;
return 0;
}
static int empty_atom(poolset *ps, char *element)
{
if (ps->empty == NULL) {
char *buff = (char *)element;
int i = ps->atom_size;
while (i--) {
if (*buff++ != '\0') {
return 0;
}
}
return 1;
} else {
return ps->empty(element);
}
}
void *pool_get_atom(poolset *ps)
{
char *atom;
int i, j;
if (!ps) {
return NULL;
}
if (!ps->pools) {
alloc_pool(ps);
}
/* (1) Check active pool */
for (i = 0; i < ps->length; i++) {
j = (i + ps->last_i) % ps->length;
atom = (char *)ps->active->pool + (j * ps->atom_size);
if (empty_atom(ps, atom)) {
ps->last_i = (j + 1) % ps->length;
return (void *)atom;
}
}
/* (2) Create a new pool, and try again */
if (alloc_pool(ps)) {
return NULL;
}
return pool_get_atom(ps);
}