forked from Villemoes/rvutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmppool.h
52 lines (43 loc) · 1.44 KB
/
tmppool.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
#ifndef TMPPOOL_H
#define TMPPOOL_H
#include <sys/queue.h>
#include <pthread.h>
#include "ass.h"
struct tmppool_obj;
struct tmppool_list {
pthread_mutex_t mtx;
SLIST_HEAD(, tmppool_obj) head;
};
#define TMPPOOL_LIST_INITIALIZER {.mtx = PTHREAD_MUTEX_INITIALIZER, .head = SLIST_HEAD_INITIALIZER() }
struct tmppool {
int (*init_obj)(void *obj, unsigned size);
void (*destroy_obj)(void *obj, unsigned size);
unsigned mask;
unsigned obj_size;
struct tmppool_list list[];
};
/*
* I'd like to avoid using the gcc extension allowing static
* initialization of a flexible array members, but I don't know how to
* achieve this any other way. I really don't want to force the user
* to allocate and initialize the tmppool at runtime (this is library
* code meant for other libraries to build on).
*
*/
#define TMPPOOL_DECLARE(name, N, init, destroy, objsize) \
struct tmppool name = { \
.init_obj = (init), \
.destroy_obj = (destroy), \
.mask = ((N)-1) + \
static_assert_zero(((N)&((N)-1)) == 0, \
N_must_be_power_of_2), \
.obj_size = (objsize), \
.list = { \
[0 ... ((N)-1)] = TMPPOOL_LIST_INITIALIZER, \
}, \
}
/* Passing NULL to _put as second argument is safe and does nothing. */
void *tmppool_get(struct tmppool *pool);
void tmppool_put(struct tmppool *pool, void *obj);
void tmppool_release(struct tmppool *pool);
#endif /* TMPPOOL_H */