9 #include <linux/poison.h>
10 #include <linux/slab.h>
11 #include <linux/radix-tree.h>
12 #include <urcu/uatomic.h>
27 void *kmem_cache_alloc(struct kmem_cache *cachep, int flags)
29 struct radix_tree_node *node;
31 if (flags & __GFP_NOWARN)
34 pthread_mutex_lock(&cachep->lock);
35 if (cachep->nr_objs) {
38 cachep->objs = node->private_data;
39 pthread_mutex_unlock(&cachep->lock);
40 node->private_data = NULL;
42 pthread_mutex_unlock(&cachep->lock);
43 node = malloc(cachep->size);
48 uatomic_inc(&nr_allocated);
50 printf("Allocating %p from slab\n", node);
54 void kmem_cache_free(struct kmem_cache *cachep, void *objp)
57 uatomic_dec(&nr_allocated);
59 printf("Freeing %p to slab\n", objp);
60 pthread_mutex_lock(&cachep->lock);
61 if (cachep->nr_objs > 10) {
62 memset(objp, POISON_FREE, cachep->size);
65 struct radix_tree_node *node = objp;
67 node->private_data = cachep->objs;
70 pthread_mutex_unlock(&cachep->lock);
73 void *kmalloc(size_t size, gfp_t gfp)
75 void *ret = malloc(size);
76 uatomic_inc(&nr_allocated);
78 printf("Allocating %p from malloc\n", ret);
86 uatomic_dec(&nr_allocated);
88 printf("Freeing %p to malloc\n", p);
93 kmem_cache_create(const char *name, size_t size, size_t offset,
94 unsigned long flags, void (*ctor)(void *))
96 struct kmem_cache *ret = malloc(sizeof(*ret));
98 pthread_mutex_init(&ret->lock, NULL);