Kmalloc vmalloc kzalloc malloc and Get_free_page ()

Source: Internet
Author: User

kmalloc vmalloc kzalloc get_free_page () is a kernel space request memory space function

malloc is the user space request memory function

One, the difference between Kmalloc () and Kfree () and Get_free_page

1, for small, contiguous physical memory: Using a memory allocator slab a small piece. The requested memory is in the mapped area of physical memory. Its true physical address is only a fixed offset.

You can use these two macros to simply convert __PA (address) {Virt_to_phys ()} and __va (address) {Phys_to_virt ()}

Get_free_page () The requested memory is an entire page, the size of a page is generally 128K. The difference between them is only this, the others are the same.

Essentially, the Kmalloc () and Get_free_page () final invocation implementations are the same, except that the flag are different when the final function is invoked.

2. void *kmalloc (size_t size, int flags) allocated memory physical address continuous, virtual address is also continuous

3. Gfp_mask Logo:

Case corresponding Flag
Process context, you can sleep Gfp_kernel
Process context, can not sleep gfp_atomic
Interrupt Handler Gfp_atomic
Soft Interrupt Gfp_atomic
Tasklet gfp_atomic
For DMA memory, you can sleep GFP_DMA | Gfp_kernel
Memory for DMA, no sleep GFP_DMA | Gfp_atomic

4. void Kfree (const void *PTR)

Frees the memory blocks allocated by Kmalloc ()

two, Vmalloc () and Vfree ()

Used to request larger memory space, virtual memory is contiguous: The application memory is located between the Vmalloc_start-----vmalloc_end, and there is no simple conversion relationship with physical memory. The physics does not require continuous.

1. Allocate in bytes, in <linux/vmalloc.h>

2. void *vmalloc (unsigned long size) the memory virtual address is allocated continuously, the physical address is discontinuous

3. In general, only hardware devices need physical address continuous memory, because hardware devices often exist outside the MMU, do not understand the virtual address, but for performance considerations, the kernel generally use kmalloc (), and only if you need to get large chunks of memory to use Vmalloc (), For example, when a module is dynamically loaded into the kernel, it loads the module onto the memory allocated by Vmalloc ().

4.void vfree (void *addr), this function can sleep, and therefore cannot be invoked from the interrupt context.

Three, malloc (), Vmalloc () and Kmalloc () difference

[1]kmalloc and Vmalloc are allocated kernel memory, malloc allocates the user's memory

[2]kmalloc guarantee that the distribution within the existence of the physical is continuous, Vmalloc guaranteed in the virtual address space, malloc and Vmalloc, is also a virtual connection, things are not necessarily connected.

[3]kmalloc can be allocated a limited size, vmalloc and malloc can be allocated relatively large size

[4] memory is required to be physically contiguous only when it is being accessed by DMA

[5]vmalloc is slower than Kmalloc

Four, Kzalloc

Kzalloc implements the functions of Kmalloc and memset, a function that functions as two functions.

Five, currently Kzalloc will replace Kmalloc and memset functions.

later in the kernel, the ratio of seeing this function will be higher.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.