Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include <stdio.h>
0004 #include <string.h>
0005 
0006 #include <urcu/uatomic.h>
0007 #include <linux/slab.h>
0008 #include <malloc.h>
0009 #include <linux/gfp.h>
0010 
0011 int kmalloc_nr_allocated;
0012 int kmalloc_verbose;
0013 
0014 void *kmalloc(size_t size, gfp_t gfp)
0015 {
0016     void *ret;
0017 
0018     if (!(gfp & __GFP_DIRECT_RECLAIM))
0019         return NULL;
0020 
0021     ret = malloc(size);
0022     uatomic_inc(&kmalloc_nr_allocated);
0023     if (kmalloc_verbose)
0024         printf("Allocating %p from malloc\n", ret);
0025     if (gfp & __GFP_ZERO)
0026         memset(ret, 0, size);
0027     return ret;
0028 }
0029 
0030 void kfree(void *p)
0031 {
0032     if (!p)
0033         return;
0034     uatomic_dec(&kmalloc_nr_allocated);
0035     if (kmalloc_verbose)
0036         printf("Freeing %p to malloc\n", p);
0037     free(p);
0038 }