Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the Linux kernel memory allocation functions kzalloc and kcalloc

Shulou Source: shulou.com Published: 2022-06-01 21:17:16 09月28日 Update

This article mainly explains "Linux kernel memory allocation function kzalloc and kcalloc how to use", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "Linux kernel memory allocation function kzalloc and kcalloc how to use" it!

1. Kzalloc

File: include/linux/slab.h, defined as follows:

/ * kzalloc-allocate memory. The memory is set to zero. * @ size: how many bytes of memory are required. * @ flags: the type of memory to allocate (see kmalloc). * / static inline void * kzalloc (size_t size, gfp_t flags) {return kmalloc (size, flags | _ _ GFP_ZERO);}

The kzalloc () function is the same as kmalloc (). Difference: zero after successful memory allocation.

Every time you use kzalloc (), there must be a corresponding memory release function kfree ().

For example:

Static int rockchip_drm_open (struct drm_device * dev, struct drm_file * file) {. File_priv = kzalloc (sizeof (* file_priv), GFP_KERNEL); Kfree (file_priv); file_priv = NULL;.} II, kcalloc

File: include/linux/slab.h, defined as follows:

/ * kmalloc_array-allocate memory for an array. * @ n: number of elements. * @ size: element size. * @ flags: the type of memory to allocate (see kmalloc). * / static inline void * kmalloc_array (size_t n, size_t size, gfp_t flags) {if (size! = 0 & & n > SIZE_MAX / size) return NULL; return _ kmalloc (n * size, flags);} / * * kcalloc-allocate memory for an array. The memory is set to zero. * @ n: number of elements. * @ size: element size. * @ flags: the type of memory to allocate (see kmalloc). * / static inline void * kcalloc (size_t n, size_t size, gfp_t flags) {return kmalloc_array (n, size, flags | _ _ GFP_ZERO);}

The kcalloc () function allocates memory to the array, sizes n*size, and zeros the allocated memory. The final implementation of this function is similar to the kmalloc () function.

Every time you use kcalloc (), there must be a corresponding memory release function kfree ().

For example:

Struct drm_clip_rect {unsigned short x 1; unsigned short y 1; unsigned short x 2; unsigned short y 2;}; int drm_mode_dirtyfb_ioctl (struct drm_device * dev, void * data, struct drm_file * file_priv) {. Struct drm_clip_rect * clips = NULL;... Clips = kcalloc (num_clips, sizeof (* clips), GFP_KERNEL); Kfree (clips);.} Thank you for your reading. The above is the content of "how to use the Linux kernel memory allocation functions kzalloc and kcalloc". After the study of this article, I believe you have a deeper understanding of how to use the Linux kernel memory allocation functions kzalloc and kcalloc, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Function memory allocation kernel learning content file success number function size that is ideas situations articles more knowledge knowledge points articles follow Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Tech Info Shulou Information Shulou Technology MariaDB