forked from fritz0705/dhcpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.c
52 lines (35 loc) · 939 Bytes
/
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
#include <stdlib.h>
#include <assert.h>
#include "pool.h"
struct pool *pool_create(uint32_t size) {
struct pool *pool;
pool = (struct pool*)calloc(1, sizeof(struct pool));
pool_resize(pool, size);
return pool;
}
void pool_destroy(struct pool *pool) {
assert(pool != NULL);
free(pool->a);
free(pool);
}
struct pool_entry *pool_get(struct pool *pool) {
struct pool_entry *entry;
if (pool->size == 0)
return NULL;
entry = (struct pool_entry*)malloc(sizeof(struct pool_entry));
*entry = pool->a[--pool->size];
return entry;
}
bool pool_add(struct pool *pool, struct pool_entry *entry) {
if (pool->limit < pool->size + 1)
return false;
pool->a[pool->size++] = *entry;
return true;
}
void pool_resize(struct pool *pool, uint32_t limit) {
pool->a = (struct pool_entry*)realloc(pool->a,
sizeof(struct pool_entry) * limit);
pool->limit = limit;
if (pool->size > pool->limit)
pool->size = pool->limit;
}