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