forked from zwdzwd/utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmempool.h
52 lines (45 loc) · 1.3 KB
/
mempool.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
/**************************
* A memory pool of reads *
**************************
* Similar to the ones in samtools.
* Note memory pool doesn't keep track
* of allocation, it only keep track of recycles.
*
* This setting slightly improve the performance
* of memory allocation.
**************************/
typedef struct __mempool_t {
int cnt; /* number of prima facie allocations */
int n; /* number of recycles */
int max; /* maximum possible recycles */
rnode_t **recycle_stack; /* address of recycled */
} mempool_t;
static mempool_t *mp_init(void) {
mempool_t *mp;
mp = (mempool_t*) calloc(1, sizeof(mempool_t));
return mp;
}
static void mp_destroy(mempool_t *mp) {
/* clean recycled nodes */
int k;
for (k = 0; k < mp->n; ++k) {
free(mp->buf[k]->b.data);
free(mp->buf[k]);
}
free(mp->buf);
free(mp);
}
static inline rnode_t *mp_alloc(mempool_t *mp)
{
++mp->cnt;
if (mp->n == 0) return (rnode_t*)calloc(1, sizeof(rnode_t));
else return mp->buf[--mp->n];
}
static inline void mp_free(mempool_t *mp, rnode_t *p) {
--mp->cnt;
if (mp->n == mp->max) {
mp->max = mp->max? mp->max<<1 : 256;
mp->buf = (rnode_t**)realloc(mp->buf, sizeof(rnode_t*) * mp->max);
}
mp->buf[mp->n++] = p;
}