]>
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; | |
847d3576 | 12 | int preempt_count; |
1366c37e MW |
13 | |
14 | void *mempool_alloc(mempool_t *pool, int gfp_mask) | |
15 | { | |
16 | return pool->alloc(gfp_mask, pool->data); | |
17 | } | |
18 | ||
19 | void mempool_free(void *element, mempool_t *pool) | |
20 | { | |
21 | pool->free(element, pool->data); | |
22 | } | |
23 | ||
24 | mempool_t *mempool_create(int min_nr, mempool_alloc_t *alloc_fn, | |
25 | mempool_free_t *free_fn, void *pool_data) | |
26 | { | |
27 | mempool_t *ret = malloc(sizeof(*ret)); | |
28 | ||
29 | ret->alloc = alloc_fn; | |
30 | ret->free = free_fn; | |
31 | ret->data = pool_data; | |
32 | return ret; | |
33 | } | |
34 | ||
35 | void *kmem_cache_alloc(struct kmem_cache *cachep, int flags) | |
36 | { | |
31023cd6 MW |
37 | void *ret; |
38 | ||
39 | if (flags & __GFP_NOWARN) | |
40 | return NULL; | |
41 | ||
42 | ret = malloc(cachep->size); | |
1366c37e MW |
43 | if (cachep->ctor) |
44 | cachep->ctor(ret); | |
45 | uatomic_inc(&nr_allocated); | |
46 | return ret; | |
47 | } | |
48 | ||
49 | void kmem_cache_free(struct kmem_cache *cachep, void *objp) | |
50 | { | |
51 | assert(objp); | |
52 | uatomic_dec(&nr_allocated); | |
53 | memset(objp, 0, cachep->size); | |
54 | free(objp); | |
55 | } | |
56 | ||
de1af8f6 MW |
57 | void *kmalloc(size_t size, gfp_t gfp) |
58 | { | |
59 | void *ret = malloc(size); | |
60 | uatomic_inc(&nr_allocated); | |
61 | return ret; | |
62 | } | |
63 | ||
64 | void kfree(void *p) | |
65 | { | |
66 | if (!p) | |
67 | return; | |
68 | uatomic_dec(&nr_allocated); | |
69 | free(p); | |
70 | } | |
71 | ||
1366c37e MW |
72 | struct kmem_cache * |
73 | kmem_cache_create(const char *name, size_t size, size_t offset, | |
74 | unsigned long flags, void (*ctor)(void *)) | |
75 | { | |
76 | struct kmem_cache *ret = malloc(sizeof(*ret)); | |
77 | ||
78 | ret->size = size; | |
79 | ret->ctor = ctor; | |
80 | return ret; | |
81 | } |