How to use kmalloc Kernel memory allocation function in Linux
This article is about how to use the kmalloc kernel memory allocation function in Linux. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
Define static _ _ always_inline void * kmalloc (size_t size, gfp_t flags) {if (_ _ builtin_constant_p (size)) {if (size > KMALLOC_MAX_CACHE_SIZE) return kmalloc_large (size, flags); # ifndef CONFIG_SLOB if (! (flags & GFP_DMA)) {int index = kmalloc_index (size) If (! index) return ZERO_SIZE_PTR; return kmem_cache_alloc_trace (kmalloc_caches [index], flags, size);} # endif} return _ kmalloc (size, flags);}
Note:
1) the _ _ builtin_constant_p compiler inlines the function to determine whether the incoming parameter is constant. If it is a variable, call the _ _ kmalloc function directly.
2) KMALLOC_MAX_CACHE_SIZE means that the maximum value of slab cache created by the system is 8K, which is defined as follows:
# # include/linux/slab.h/* Maximum size for which we actually use a slab cache * / # define KMALLOC_MAX_CACHE_SIZE (1UL