Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <string.h> |
| 3 | #include <malloc.h> |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 4 | #include <pthread.h> |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 5 | #include <unistd.h> |
| 6 | #include <assert.h> |
| 7 | |
Matthew Wilcox | 12ea653 | 2016-12-16 14:53:45 -0500 | [diff] [blame] | 8 | #include <linux/gfp.h> |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 9 | #include <linux/poison.h> |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 10 | #include <linux/slab.h> |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 11 | #include <linux/radix-tree.h> |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 12 | #include <urcu/uatomic.h> |
| 13 | |
| 14 | int nr_allocated; |
Matthew Wilcox | 847d357 | 2016-12-14 15:08:02 -0800 | [diff] [blame] | 15 | int preempt_count; |
Matthew Wilcox | 5eeb2d2 | 2016-12-24 07:49:18 -0500 | [diff] [blame] | 16 | int kmalloc_verbose; |
Rehas Sachdeva | 73bc029 | 2017-01-04 11:55:00 -0500 | [diff] [blame] | 17 | int test_verbose; |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 18 | |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 19 | struct kmem_cache { |
| 20 | pthread_mutex_t lock; |
| 21 | int size; |
| 22 | int nr_objs; |
| 23 | void *objs; |
| 24 | void (*ctor)(void *); |
| 25 | }; |
| 26 | |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 27 | void *kmem_cache_alloc(struct kmem_cache *cachep, int flags) |
| 28 | { |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 29 | struct radix_tree_node *node; |
Matthew Wilcox | 31023cd | 2016-12-14 15:07:59 -0800 | [diff] [blame] | 30 | |
| 31 | if (flags & __GFP_NOWARN) |
| 32 | return NULL; |
| 33 | |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 34 | pthread_mutex_lock(&cachep->lock); |
| 35 | if (cachep->nr_objs) { |
| 36 | cachep->nr_objs--; |
| 37 | node = cachep->objs; |
Matthew Wilcox | 1293d5c | 2017-01-16 16:41:29 -0500 | [diff] [blame] | 38 | cachep->objs = node->parent; |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 39 | pthread_mutex_unlock(&cachep->lock); |
Matthew Wilcox | 1293d5c | 2017-01-16 16:41:29 -0500 | [diff] [blame] | 40 | node->parent = NULL; |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 41 | } else { |
| 42 | pthread_mutex_unlock(&cachep->lock); |
| 43 | node = malloc(cachep->size); |
| 44 | if (cachep->ctor) |
| 45 | cachep->ctor(node); |
| 46 | } |
| 47 | |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 48 | uatomic_inc(&nr_allocated); |
Matthew Wilcox | 5eeb2d2 | 2016-12-24 07:49:18 -0500 | [diff] [blame] | 49 | if (kmalloc_verbose) |
| 50 | printf("Allocating %p from slab\n", node); |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 51 | return node; |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void kmem_cache_free(struct kmem_cache *cachep, void *objp) |
| 55 | { |
| 56 | assert(objp); |
| 57 | uatomic_dec(&nr_allocated); |
Matthew Wilcox | 5eeb2d2 | 2016-12-24 07:49:18 -0500 | [diff] [blame] | 58 | if (kmalloc_verbose) |
| 59 | printf("Freeing %p to slab\n", objp); |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 60 | 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 Wilcox | 1293d5c | 2017-01-16 16:41:29 -0500 | [diff] [blame] | 67 | node->parent = cachep->objs; |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 68 | cachep->objs = node; |
| 69 | } |
| 70 | pthread_mutex_unlock(&cachep->lock); |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Matthew Wilcox | de1af8f | 2016-12-14 15:09:25 -0800 | [diff] [blame] | 73 | void *kmalloc(size_t size, gfp_t gfp) |
| 74 | { |
| 75 | void *ret = malloc(size); |
| 76 | uatomic_inc(&nr_allocated); |
Matthew Wilcox | 5eeb2d2 | 2016-12-24 07:49:18 -0500 | [diff] [blame] | 77 | if (kmalloc_verbose) |
| 78 | printf("Allocating %p from malloc\n", ret); |
Matthew Wilcox | de1af8f | 2016-12-14 15:09:25 -0800 | [diff] [blame] | 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | void kfree(void *p) |
| 83 | { |
| 84 | if (!p) |
| 85 | return; |
| 86 | uatomic_dec(&nr_allocated); |
Matthew Wilcox | 5eeb2d2 | 2016-12-24 07:49:18 -0500 | [diff] [blame] | 87 | if (kmalloc_verbose) |
| 88 | printf("Freeing %p to malloc\n", p); |
Matthew Wilcox | de1af8f | 2016-12-14 15:09:25 -0800 | [diff] [blame] | 89 | free(p); |
| 90 | } |
| 91 | |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 92 | struct kmem_cache * |
| 93 | kmem_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 Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 98 | pthread_mutex_init(&ret->lock, NULL); |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 99 | ret->size = size; |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 100 | ret->nr_objs = 0; |
| 101 | ret->objs = NULL; |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 102 | ret->ctor = ctor; |
| 103 | return ret; |
| 104 | } |