blob: cf48c8473f4869a8205a9eaa94b6f423079b9be5 [file] [log] [blame]
Matthew Wilcox1366c372016-03-17 14:21:45 -07001#include <stdlib.h>
2#include <string.h>
3#include <malloc.h>
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -08004#include <pthread.h>
Matthew Wilcox1366c372016-03-17 14:21:45 -07005#include <unistd.h>
6#include <assert.h>
7
Matthew Wilcox12ea6532016-12-16 14:53:45 -05008#include <linux/gfp.h>
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -08009#include <linux/poison.h>
Matthew Wilcox1366c372016-03-17 14:21:45 -070010#include <linux/slab.h>
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080011#include <linux/radix-tree.h>
Matthew Wilcox1366c372016-03-17 14:21:45 -070012#include <urcu/uatomic.h>
13
14int nr_allocated;
Matthew Wilcox847d3572016-12-14 15:08:02 -080015int preempt_count;
Matthew Wilcox5eeb2d22016-12-24 07:49:18 -050016int kmalloc_verbose;
Rehas Sachdeva73bc0292017-01-04 11:55:00 -050017int test_verbose;
Matthew Wilcox1366c372016-03-17 14:21:45 -070018
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080019struct kmem_cache {
20 pthread_mutex_t lock;
21 int size;
22 int nr_objs;
23 void *objs;
24 void (*ctor)(void *);
25};
26
Matthew Wilcox1366c372016-03-17 14:21:45 -070027void *kmem_cache_alloc(struct kmem_cache *cachep, int flags)
28{
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080029 struct radix_tree_node *node;
Matthew Wilcox31023cd2016-12-14 15:07:59 -080030
31 if (flags & __GFP_NOWARN)
32 return NULL;
33
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080034 pthread_mutex_lock(&cachep->lock);
35 if (cachep->nr_objs) {
36 cachep->nr_objs--;
37 node = cachep->objs;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -050038 cachep->objs = node->parent;
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080039 pthread_mutex_unlock(&cachep->lock);
Matthew Wilcox1293d5c2017-01-16 16:41:29 -050040 node->parent = NULL;
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080041 } else {
42 pthread_mutex_unlock(&cachep->lock);
43 node = malloc(cachep->size);
44 if (cachep->ctor)
45 cachep->ctor(node);
46 }
47
Matthew Wilcox1366c372016-03-17 14:21:45 -070048 uatomic_inc(&nr_allocated);
Matthew Wilcox5eeb2d22016-12-24 07:49:18 -050049 if (kmalloc_verbose)
50 printf("Allocating %p from slab\n", node);
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080051 return node;
Matthew Wilcox1366c372016-03-17 14:21:45 -070052}
53
54void kmem_cache_free(struct kmem_cache *cachep, void *objp)
55{
56 assert(objp);
57 uatomic_dec(&nr_allocated);
Matthew Wilcox5eeb2d22016-12-24 07:49:18 -050058 if (kmalloc_verbose)
59 printf("Freeing %p to slab\n", objp);
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080060 pthread_mutex_lock(&cachep->lock);
61 if (cachep->nr_objs > 10) {
62 memset(objp, POISON_FREE, cachep->size);
63 free(objp);
64 } else {
65 struct radix_tree_node *node = objp;
66 cachep->nr_objs++;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -050067 node->parent = cachep->objs;
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080068 cachep->objs = node;
69 }
70 pthread_mutex_unlock(&cachep->lock);
Matthew Wilcox1366c372016-03-17 14:21:45 -070071}
72
Matthew Wilcoxde1af8f2016-12-14 15:09:25 -080073void *kmalloc(size_t size, gfp_t gfp)
74{
75 void *ret = malloc(size);
76 uatomic_inc(&nr_allocated);
Matthew Wilcox5eeb2d22016-12-24 07:49:18 -050077 if (kmalloc_verbose)
78 printf("Allocating %p from malloc\n", ret);
Matthew Wilcoxde1af8f2016-12-14 15:09:25 -080079 return ret;
80}
81
82void kfree(void *p)
83{
84 if (!p)
85 return;
86 uatomic_dec(&nr_allocated);
Matthew Wilcox5eeb2d22016-12-24 07:49:18 -050087 if (kmalloc_verbose)
88 printf("Freeing %p to malloc\n", p);
Matthew Wilcoxde1af8f2016-12-14 15:09:25 -080089 free(p);
90}
91
Matthew Wilcox1366c372016-03-17 14:21:45 -070092struct kmem_cache *
93kmem_cache_create(const char *name, size_t size, size_t offset,
94 unsigned long flags, void (*ctor)(void *))
95{
96 struct kmem_cache *ret = malloc(sizeof(*ret));
97
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -080098 pthread_mutex_init(&ret->lock, NULL);
Matthew Wilcox1366c372016-03-17 14:21:45 -070099 ret->size = size;
Matthew Wilcoxbbe9d712016-12-14 15:09:28 -0800100 ret->nr_objs = 0;
101 ret->objs = NULL;
Matthew Wilcox1366c372016-03-17 14:21:45 -0700102 ret->ctor = ctor;
103 return ret;
104}