| 1 | |
| 2 |
|
| 3 | #include <stdio.h>
|
| 4 | #include <string.h>
|
| 5 |
|
| 6 | #include <urcu/uatomic.h>
|
| 7 | #include <linux/slab.h>
|
| 8 | #include <malloc.h>
|
| 9 | #include <linux/gfp.h>
|
| 10 |
|
| 11 | int kmalloc_nr_allocated;
|
| 12 | int kmalloc_verbose;
|
| 13 |
|
| 14 | void *kmalloc(size_t size, gfp_t gfp)
|
| 15 | {
|
| 16 | void *ret;
|
| 17 |
|
| 18 | if (!(gfp & __GFP_DIRECT_RECLAIM))
|
| 19 | return NULL;
|
| 20 |
|
| 21 | ret = malloc(size);
|
| 22 | uatomic_inc(&kmalloc_nr_allocated);
|
| 23 | if (kmalloc_verbose)
|
| 24 | printf("Allocating %p from malloc\n", ret);
|
| 25 | if (gfp & __GFP_ZERO)
|
| 26 | memset(ret, 0, size);
|
| 27 | return ret;
|
| 28 | }
|
| 29 |
|
| 30 | void kfree(void *p)
|
| 31 | {
|
| 32 | if (!p)
|
| 33 | return;
|
| 34 | uatomic_dec(&kmalloc_nr_allocated);
|
| 35 | if (kmalloc_verbose)
|
| 36 | printf("Freeing %p to malloc\n", p);
|
| 37 | free(p);
|
| 38 | }
|
| 39 |
|
| 40 | void *kmalloc_array(size_t n, size_t size, gfp_t gfp)
|
| 41 | {
|
| 42 | void *ret;
|
| 43 |
|
| 44 | if (!(gfp & __GFP_DIRECT_RECLAIM))
|
| 45 | return NULL;
|
| 46 |
|
| 47 | ret = calloc(n, size);
|
| 48 | uatomic_inc(&kmalloc_nr_allocated);
|
| 49 | if (kmalloc_verbose)
|
| 50 | printf("Allocating %p from calloc\n", ret);
|
| 51 | if (gfp & __GFP_ZERO)
|
| 52 | memset(ret, 0, n * size);
|
| 53 | return ret;
|
| 54 | }
|