~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/radix-tree/linux.c

Version: ~ [ linux-5.1-rc5 ] ~ [ linux-5.0.7 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.34 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.111 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.168 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.178 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.138 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.65 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <malloc.h>
  5 #include <pthread.h>
  6 #include <unistd.h>
  7 #include <assert.h>
  8 
  9 #include <linux/gfp.h>
 10 #include <linux/poison.h>
 11 #include <linux/slab.h>
 12 #include <linux/radix-tree.h>
 13 #include <urcu/uatomic.h>
 14 
 15 int nr_allocated;
 16 int preempt_count;
 17 int kmalloc_verbose;
 18 int test_verbose;
 19 
 20 struct kmem_cache {
 21         pthread_mutex_t lock;
 22         int size;
 23         int nr_objs;
 24         void *objs;
 25         void (*ctor)(void *);
 26 };
 27 
 28 void *kmem_cache_alloc(struct kmem_cache *cachep, int flags)
 29 {
 30         struct radix_tree_node *node;
 31 
 32         if (!(flags & __GFP_DIRECT_RECLAIM))
 33                 return NULL;
 34 
 35         pthread_mutex_lock(&cachep->lock);
 36         if (cachep->nr_objs) {
 37                 cachep->nr_objs--;
 38                 node = cachep->objs;
 39                 cachep->objs = node->parent;
 40                 pthread_mutex_unlock(&cachep->lock);
 41                 node->parent = NULL;
 42         } else {
 43                 pthread_mutex_unlock(&cachep->lock);
 44                 node = malloc(cachep->size);
 45                 if (cachep->ctor)
 46                         cachep->ctor(node);
 47         }
 48 
 49         uatomic_inc(&nr_allocated);
 50         if (kmalloc_verbose)
 51                 printf("Allocating %p from slab\n", node);
 52         return node;
 53 }
 54 
 55 void kmem_cache_free(struct kmem_cache *cachep, void *objp)
 56 {
 57         assert(objp);
 58         uatomic_dec(&nr_allocated);
 59         if (kmalloc_verbose)
 60                 printf("Freeing %p to slab\n", objp);
 61         pthread_mutex_lock(&cachep->lock);
 62         if (cachep->nr_objs > 10) {
 63                 memset(objp, POISON_FREE, cachep->size);
 64                 free(objp);
 65         } else {
 66                 struct radix_tree_node *node = objp;
 67                 cachep->nr_objs++;
 68                 node->parent = cachep->objs;
 69                 cachep->objs = node;
 70         }
 71         pthread_mutex_unlock(&cachep->lock);
 72 }
 73 
 74 void *kmalloc(size_t size, gfp_t gfp)
 75 {
 76         void *ret;
 77 
 78         if (!(gfp & __GFP_DIRECT_RECLAIM))
 79                 return NULL;
 80 
 81         ret = malloc(size);
 82         uatomic_inc(&nr_allocated);
 83         if (kmalloc_verbose)
 84                 printf("Allocating %p from malloc\n", ret);
 85         if (gfp & __GFP_ZERO)
 86                 memset(ret, 0, size);
 87         return ret;
 88 }
 89 
 90 void kfree(void *p)
 91 {
 92         if (!p)
 93                 return;
 94         uatomic_dec(&nr_allocated);
 95         if (kmalloc_verbose)
 96                 printf("Freeing %p to malloc\n", p);
 97         free(p);
 98 }
 99 
100 struct kmem_cache *
101 kmem_cache_create(const char *name, size_t size, size_t offset,
102         unsigned long flags, void (*ctor)(void *))
103 {
104         struct kmem_cache *ret = malloc(sizeof(*ret));
105 
106         pthread_mutex_init(&ret->lock, NULL);
107         ret->size = size;
108         ret->nr_objs = 0;
109         ret->objs = NULL;
110         ret->ctor = ctor;
111         return ret;
112 }
113 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp