In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use the devm_kmalloc and devm_kzalloc kernel memory allocation functions in Linux. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
1. Devm_kmalloc
File: drivers/base/devres.c, defined as follows:
/ * devm_kmalloc-Resource-managed kmalloc * @ dev: Device to allocate memory for * @ size: Allocation size * @ gfp: Allocation gfp flags * * Managed kmalloc. Memory allocated with this function is * automatically freed on driver detach. Like all other devres * resources, guaranteed alignment is unsigned long long. * RETURNS: * Pointer to allocated memory on success, NULL on failure. * / void * devm_kmalloc (struct device * dev, size_t size, gfp_t gfp) {struct devres * dr; / * use raw alloc_dr for kmalloc caller tracing * / dr = alloc_dr (devm_kmalloc_release, size, gfp, dev_to_node (dev); Set_node_dbginfo (& dr- > node, "devm_kzalloc_release", size); devres_add (dev, dr- > data); return dr- > data;}
The devm_kmalloc () function applies for device resources through alloc_dr (). The process of calling alloc_dr () is as follows:
Alloc_dr ()-> # # devres.c kmalloc_node_track_caller ()-> # # slab.h kmalloc_track_caller ()-> # # slab.h _ _ kmalloc_track_caller ()-> # # salb.c _ do_kmalloc () # # similar to kmalloc implementation
It is then added to the list of device resources through devres_add ().
It is described in detail later in equipment Resource Management (device resource management).
II. Devm_kzalloc
File: include/linux/device.h, defined as follows:
Static inline void * devm_kzalloc (struct device * dev, size_t size, gfp_t gfp) {return devm_kmalloc (dev, size, gfp | _ _ GFP_ZERO);}
The devm_kzalloc () function takes the same arguments and return values as devm_kmalloc (). Difference: zero after successful memory allocation.
Note:
1) devm_kmalloc () and devm_kzalloc () are kmalloc () and kzalloc () with resource management. Associated with the owning device and used in the probe function. When the device driver is uninstalled, the memory is automatically freed, or it can be freed directly using the devm_kfree () function.
2) memory requested by kmalloc () and kzalloc () must be freed by calling kfree ().
III. Devm_kfree
File: drivers/base/devres.c, defined as follows:
/ * * devm_kfree-Resource-managed kfree * @ dev: Device this memory belongs to * @ p: Memory to free * * Free memory allocated with devm_kmalloc (). * / void devm_kfree (struct device * dev, void * p) {int rc; rc = devres_destroy (dev, devm_kmalloc_release, devm_kmalloc_match, p); WARN_ON (rc);} after reading the above, do you have any further understanding of how to use the devm_kmalloc and devm_kzalloc kernel memory allocation functions in Linux? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.