140090Smsmith// SPDX-License-Identifier: GPL-2.0 240090Smsmith 340090Smsmith#include <stdio.h> 440090Smsmith#include <string.h> 540090Smsmith 640090Smsmith#include <urcu/uatomic.h> 740090Smsmith#include <linux/slab.h> 840090Smsmith#include <malloc.h> 940090Smsmith#include <linux/gfp.h> 1040090Smsmith 1140090Smsmithint kmalloc_nr_allocated; 1240090Smsmithint kmalloc_verbose; 1340090Smsmith 1440090Smsmithvoid *kmalloc(size_t size, gfp_t gfp) 1540090Smsmith{ 1640090Smsmith void *ret; 1740090Smsmith 1840090Smsmith if (!(gfp & __GFP_DIRECT_RECLAIM)) 1940090Smsmith return NULL; 2040090Smsmith 2140090Smsmith ret = malloc(size); 2240090Smsmith uatomic_inc(&kmalloc_nr_allocated); 2340090Smsmith if (kmalloc_verbose) 2440090Smsmith printf("Allocating %p from malloc\n", ret); 2540090Smsmith if (gfp & __GFP_ZERO) 2640090Smsmith memset(ret, 0, size); 27116182Sobrien return ret; 28116182Sobrien} 29116182Sobrien 3040090Smsmithvoid kfree(void *p) 3140090Smsmith{ 3240090Smsmith if (!p) 3340090Smsmith return; 3440090Smsmith uatomic_dec(&kmalloc_nr_allocated); 3540090Smsmith if (kmalloc_verbose) 3640090Smsmith printf("Freeing %p to malloc\n", p); 3740090Smsmith free(p); 38218494Smarcel} 39218494Smarcel