]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - tools/testing/radix-tree/linux.c
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[mirror_ubuntu-bionic-kernel.git] / tools / testing / radix-tree / linux.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1366c37e
MW
2#include <stdlib.h>
3#include <string.h>
4#include <malloc.h>
bbe9d71f 5#include <pthread.h>
1366c37e
MW
6#include <unistd.h>
7#include <assert.h>
8
12ea6539 9#include <linux/gfp.h>
bbe9d71f 10#include <linux/poison.h>
1366c37e 11#include <linux/slab.h>
bbe9d71f 12#include <linux/radix-tree.h>
1366c37e
MW
13#include <urcu/uatomic.h>
14
15int nr_allocated;
847d3576 16int preempt_count;
5eeb2d23 17int kmalloc_verbose;
73bc029b 18int test_verbose;
1366c37e 19
bbe9d71f
MW
20struct kmem_cache {
21 pthread_mutex_t lock;
22 int size;
23 int nr_objs;
24 void *objs;
25 void (*ctor)(void *);
26};
27
1366c37e
MW
28void *kmem_cache_alloc(struct kmem_cache *cachep, int flags)
29{
bbe9d71f 30 struct radix_tree_node *node;
31023cd6
MW
31
32 if (flags & __GFP_NOWARN)
33 return NULL;
34
bbe9d71f
MW
35 pthread_mutex_lock(&cachep->lock);
36 if (cachep->nr_objs) {
37 cachep->nr_objs--;
38 node = cachep->objs;
1293d5c5 39 cachep->objs = node->parent;
bbe9d71f 40 pthread_mutex_unlock(&cachep->lock);
1293d5c5 41 node->parent = NULL;
bbe9d71f
MW
42 } else {
43 pthread_mutex_unlock(&cachep->lock);
44 node = malloc(cachep->size);
45 if (cachep->ctor)
46 cachep->ctor(node);
47 }
48
1366c37e 49 uatomic_inc(&nr_allocated);
5eeb2d23
MW
50 if (kmalloc_verbose)
51 printf("Allocating %p from slab\n", node);
bbe9d71f 52 return node;
1366c37e
MW
53}
54
55void kmem_cache_free(struct kmem_cache *cachep, void *objp)
56{
57 assert(objp);
58 uatomic_dec(&nr_allocated);
5eeb2d23
MW
59 if (kmalloc_verbose)
60 printf("Freeing %p to slab\n", objp);
bbe9d71f
MW
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++;
1293d5c5 68 node->parent = cachep->objs;
bbe9d71f
MW
69 cachep->objs = node;
70 }
71 pthread_mutex_unlock(&cachep->lock);
1366c37e
MW
72}
73
de1af8f6
MW
74void *kmalloc(size_t size, gfp_t gfp)
75{
76 void *ret = malloc(size);
77 uatomic_inc(&nr_allocated);
5eeb2d23
MW
78 if (kmalloc_verbose)
79 printf("Allocating %p from malloc\n", ret);
de1af8f6
MW
80 return ret;
81}
82
83void kfree(void *p)
84{
85 if (!p)
86 return;
87 uatomic_dec(&nr_allocated);
5eeb2d23
MW
88 if (kmalloc_verbose)
89 printf("Freeing %p to malloc\n", p);
de1af8f6
MW
90 free(p);
91}
92
1366c37e
MW
93struct kmem_cache *
94kmem_cache_create(const char *name, size_t size, size_t offset,
95 unsigned long flags, void (*ctor)(void *))
96{
97 struct kmem_cache *ret = malloc(sizeof(*ret));
98
bbe9d71f 99 pthread_mutex_init(&ret->lock, NULL);
1366c37e 100 ret->size = size;
bbe9d71f
MW
101 ret->nr_objs = 0;
102 ret->objs = NULL;
1366c37e
MW
103 ret->ctor = ctor;
104 return ret;
105}