In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.