Commit | Line | Data |
---|---|---|
1366c37e MW |
1 | #include <stdlib.h> |
2 | #include <string.h> | |
3 | #include <malloc.h> | |
4 | #include <unistd.h> | |
5 | #include <assert.h> | |
6 | ||
7 | #include <linux/mempool.h> | |
8 | #include <linux/slab.h> | |
9 | #include <urcu/uatomic.h> | |
10 | ||
11 | int nr_allocated; | |
12 | ||
13 | void *mempool_alloc(mempool_t *pool, int gfp_mask) | |
14 | { | |
15 | return pool->alloc(gfp_mask, pool->data); | |
16 | } | |
17 | ||
18 | void mempool_free(void *element, mempool_t *pool) | |
19 | { | |
20 | pool->free(element, pool->data); | |
21 | } | |
22 | ||
23 | mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn, | |
24 | mempool_free_t *free_fn, void *pool_data) | |
25 | { | |
26 | mempool_t *ret = malloc(sizeof(*ret)); | |
27 | ||
28 | ret->alloc = alloc_fn; | |
29 | ret->free = free_fn; | |
30 | ret->data = pool_data; | |
31 | return ret; | |
32 | } | |
33 | ||
34 | void *kmem_cache_alloc(struct kmem_cache *cachep, int flags) | |
35 | { | |
31023cd6 MW |
36 | void *ret; |
37 | ||
38 | if (flags & __GFP_NOWARN) | |
39 | return NULL; | |
40 | ||
41 | ret = malloc(cachep->size); | |
1366c37e MW |
42 | if (cachep->ctor) |
43 | cachep->ctor(ret); | |
44 | uatomic_inc(&nr_allocated); | |
45 | return ret; | |
46 | } | |
47 | ||
48 | void kmem_cache_free(struct kmem_cache *cachep, void *objp) | |
49 | { | |
50 | assert(objp); | |
51 | uatomic_dec(&nr_allocated); | |
52 | memset(objp, 0, cachep->size); | |
53 | free(objp); | |
54 | } | |
55 | ||
56 | struct kmem_cache * | |
57 | kmem_cache_create(const char *name, size_t size, size_t offset, | |
58 | unsigned long flags, void (*ctor)(void *)) | |
59 | { | |
60 | struct kmem_cache *ret = malloc(sizeof(*ret)); | |
61 | ||
62 | ret->size = size; | |
63 | ret->ctor = ctor; | |
64 | return ret; | |
65 | } |