forked from ceu-lang/ceu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathceu_pool.c
More file actions
59 lines (51 loc) · 1.25 KB
/
Copy pathceu_pool.c
File metadata and controls
59 lines (51 loc) · 1.25 KB
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
/*
* Ceu pool.c is based on Contiki and TinyOS pools:
* https://github.com/contiki-os/contiki/blob/master/core/lib/memb.c
* https://github.com/tinyos/tinyos-main/blob/master/tos/system/PoolP.nc
*/
#ifndef _CEU_POOL_C
#define _CEU_POOL_C
#include <stdlib.h>
#include "ceu_pool.h"
void ceu_pool_init (tceu_pool* pool, int size, int unit,
char** queue, char* mem)
{
int i;
pool->size = size;
pool->free = size;
pool->index = 0;
pool->unit = unit;
pool->queue = queue;
pool->mem = mem;
for (i=0; i<size; i++) {
queue[i] = &mem[i*unit];
}
}
char* ceu_pool_alloc (tceu_pool* pool) {
char* ret;
if (pool->free == 0) {
return NULL;
}
pool->free--;
ret = pool->queue[pool->index];
pool->queue[pool->index++] = NULL;
if (pool->index == pool->size) {
pool->index = 0;
}
return ret;
}
void ceu_pool_free (tceu_pool* pool, char* val) {
int empty = pool->index + pool->free;
if (empty >= pool->size) {
empty -= pool->size;
}
pool->queue[empty] = val;
pool->free++;
}
/*
int ceu_pool_inside (tceu_pool* pool, char* val) {
return ((char*)val >= pool->mem)
&& ((char*)val < pool->mem+(pool->size*pool->unit));
}
*/
#endif