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; |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 17 | |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 18 | struct kmem_cache { |
| 19 | pthread_mutex_t lock; |
| 20 | int size; |
| 21 | int nr_objs; |
| 22 | void *objs; |
| 23 | void (*ctor)(void *); |
| 24 | }; |
| 25 | |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 26 | void *kmem_cache_alloc(struct kmem_cache *cachep, int flags) |
| 27 | { |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 28 | struct radix_tree_node *node; |
Matthew Wilcox | 31023cd | 2016-12-14 15:07:59 -0800 | [diff] [blame] | 29 | |
| 30 | if (flags & __GFP_NOWARN) |
| 31 | return NULL; |
| 32 | |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 33 | pthread_mutex_lock(&cachep->lock); |
| 34 | if (cachep->nr_objs) { |
| 35 | cachep->nr_objs--; |
| 36 | node = cachep->objs; |
| 37 | cachep->objs = node->private_data; |
| 38 | pthread_mutex_unlock(&cachep->lock); |
| 39 | node->private_data = NULL; |
| 40 | } else { |
| 41 | pthread_mutex_unlock(&cachep->lock); |
| 42 | node = malloc(cachep->size); |
| 43 | if (cachep->ctor) |
| 44 | cachep->ctor(node); |
| 45 | } |
| 46 | |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 47 | uatomic_inc(&nr_allocated); |
Matthew Wilcox | 5eeb2d2 | 2016-12-24 07:49:18 -0500 | [diff] [blame^] | 48 | if (kmalloc_verbose) |
| 49 | printf("Allocating %p from slab\n", node); |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 50 | return node; |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void kmem_cache_free(struct kmem_cache *cachep, void *objp) |
| 54 | { |
| 55 | assert(objp); |
| 56 | uatomic_dec(&nr_allocated); |
Matthew Wilcox | 5eeb2d2 | 2016-12-24 07:49:18 -0500 | [diff] [blame^] | 57 | if (kmalloc_verbose) |
| 58 | printf("Freeing %p to slab\n", objp); |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 59 | pthread_mutex_lock(&cachep->lock); |
| 60 | if (cachep->nr_objs > 10) { |
| 61 | memset(objp, POISON_FREE, cachep->size); |
| 62 | free(objp); |
| 63 | } else { |
| 64 | struct radix_tree_node *node = objp; |
| 65 | cachep->nr_objs++; |
| 66 | node->private_data = cachep->objs; |
| 67 | cachep->objs = node; |
| 68 | } |
| 69 | pthread_mutex_unlock(&cachep->lock); |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Matthew Wilcox | de1af8f | 2016-12-14 15:09:25 -0800 | [diff] [blame] | 72 | void *kmalloc(size_t size, gfp_t gfp) |
| 73 | { |
| 74 | void *ret = malloc(size); |
| 75 | uatomic_inc(&nr_allocated); |
Matthew Wilcox | 5eeb2d2 | 2016-12-24 07:49:18 -0500 | [diff] [blame^] | 76 | if (kmalloc_verbose) |
| 77 | printf("Allocating %p from malloc\n", ret); |
Matthew Wilcox | de1af8f | 2016-12-14 15:09:25 -0800 | [diff] [blame] | 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | void kfree(void *p) |
| 82 | { |
| 83 | if (!p) |
| 84 | return; |
| 85 | uatomic_dec(&nr_allocated); |
Matthew Wilcox | 5eeb2d2 | 2016-12-24 07:49:18 -0500 | [diff] [blame^] | 86 | if (kmalloc_verbose) |
| 87 | printf("Freeing %p to malloc\n", p); |
Matthew Wilcox | de1af8f | 2016-12-14 15:09:25 -0800 | [diff] [blame] | 88 | free(p); |
| 89 | } |
| 90 | |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 91 | struct kmem_cache * |
| 92 | kmem_cache_create(const char *name, size_t size, size_t offset, |
| 93 | unsigned long flags, void (*ctor)(void *)) |
| 94 | { |
| 95 | struct kmem_cache *ret = malloc(sizeof(*ret)); |
| 96 | |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 97 | pthread_mutex_init(&ret->lock, NULL); |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 98 | ret->size = size; |
Matthew Wilcox | bbe9d71 | 2016-12-14 15:09:28 -0800 | [diff] [blame] | 99 | ret->nr_objs = 0; |
| 100 | ret->objs = NULL; |
Matthew Wilcox | 1366c37 | 2016-03-17 14:21:45 -0700 | [diff] [blame] | 101 | ret->ctor = ctor; |
| 102 | return ret; |
| 103 | } |