"UNIX" kernel management of kernel space memory and heap memory distribution to user processes

Source: Internet
Author: User


Common kernel memory allocation functions


1)_get_free_pages is the most primitive memory allocation, to level two gets the original page box from the partner system, and the return value is the starting address of the first page box. _get_free_pages only encapsulates the alloc_pages function on the implementation , and alloc_pages The allotted length is 1<

        2) kmem_cache_alloc slab Allocator is a memory allocation method that is suitable for repeatedly allocating the same size of memory to release the occasion. First Use kmem_cache_alloc Gets a new block of memory from the cache area. In 2.6 The kernel can apply up to Span style= "Font-family:times New Roman" >1<< 5*4kb 128kb continuous physical memory

3)Kmalloc is a common memory allocation method in the kernel, they are implemented by calling the kmem_cache_alloc function,kmalloc Apply up to 128KB of contiguous physical memory at a time and compile if it is greater than 128KB

4)Vmalloc in front of several distribution methods are physical continuous, can guarantee a lower average access time. However, in some cases, the request of the internal storage area is not very frequent, the higher memory access can be accepted, this can allocate a linear continuous, physical discontinuous address, the benefit is that can allocate large memory, for Vmalloc Allocated memory space is generally unlimited, but the general memory size should not be greater than 1GB

5)dma_alloc_coherent ,Dma is a hardware mechanism that allows io data to be transferred directly between the peripherals and main memory without CPU the participation, use DMA mechanism can significantly increase the throughput of communication with the device

6)ioremap

Void *ioremap(unsigned long offset ,unsigned long size )

Ioremap is a more straightforward way of allocating memory, specifying the physical start address and the amount of memory to allocate, and then mapping the physical address of that segment to the kernel address space,ioremap The physical address used in advance is known to determine , and the above several ways, not to allocate a new physical address memory,ioremap More for the device driver, to achieve the physical address and virtual address of the mutual transformation , this mechanism plays an important role in the Linux kernel device driver. allows the CPU to access the IO space of an external device indirectly through access to the virtual address .

Distribution of heap memory for user processes

extern void *malloc (unsigned int num_bytes);

Malloc assigns a specified size to the system request bytes of memory space . Is the return type void*? Type . void* A pointer that represents an indeterminate type. C Specifies thatthevoid* type can be cast to any other type of pointer.

Function: Allocates a memory block of length num_bytes bytes

Return value: Returns a pointer to the allocated memory if the assignment was successful ( the initial value in this store is indeterminate ), otherwise a null pointer is returned null. Use the Free () function to release memory blocks when memory is no longer in use. The pointer returned by the function must be properly aligned so that it can be used with any data object.

As you can see from the function declaration. malloc and the New There are at least two different : New returns a pointer of the specified type, and can automatically calculate the desired size. For example:

int *p;

p = new int; The return type is the int* type ( integer pointer ), and the allocation size is sizeof (int);

Or:

Int* Parr;

Parr = new int [100]; The return type is the int* type ( integer pointer ), and the allocation size is sizeof (int) *;

malloc , however, has to be counted by us and forcibly converted to a pointer of the actual type after the return.

int* p;

p = (int *) malloc (sizeof (int) *128);// allocate An integer storage unit (which can replace the value according to actual need) and The first address of a contiguous integer storage unit is stored in the pointer variable p

Double *pd= (double *) malloc (sizeof (double) *12);// allocation of double -type storage units , and store the first address in the pointer variable PD

Allocation mechanism

The real body of the malloc function now, it has a so-called idle list that connects the available memory blocks to a long listing. Call malloc function, it looks for a block of memory along the join table that is large enough to satisfy the user's request. The memory block is then split in two (the size of the chunk is equal to the size of the user request, and the other is the remaining bytes). Next, pass the memory allocated to the user to the user and return the rest of the block (if any) to the connection table . Called Freefunction, it connects the memory blocks freed by the user to the idle chain. In the end, the idle chain will be cut into a lot of small memory fragments, if the user requests a large memory fragment, then the idle chain may not be able to meet the user requirements of the fragment. So,mallocThe function requests the delay, and begins to rummage through the memory fragments on the idle chain, organizes them, and merges the adjacent small free blocks into larger chunks of memory. If you cannot obtain a block of memory that meets the requirements,mallocfunction will returnNULLpointer, so the callmallocwhen requesting a memory block dynamically, be sure to make a judgment return value.

"UNIX" kernel management of kernel space memory and heap memory distribution to user processes

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.