"Glibc source code analysis ptmalloc memory management" Memory Management Overview

Memory management design assumptions

1. The large memory allocation with a long life cycle using mmap.
2. Special large memory allocation always use mmap.
3. Memory allocation using a short life cycle BRK, because with anonymous mmap map page, an exception occurs when a missing page, linux kernel allocates a new physical page is missing page and the physical page is cleared, a memory block mmap need to map multiple physical pages, resulting in multiple cleared operation, it is a waste of system resources, so the introduction of mmap allocation threshold dynamic adjustment mechanism to ensure that only use mmap to allocate memory if necessary.
4. Try to free small memory blocks cached only for temporary use, for large blocks of memory or long life cycle of a large block of memory at the time of release are directly returned to the operating system.
5. Command free small memory blocks are combined in malloc and free time, free memory blocks may be placed in the free pool, the necessarily returned to the operating system.
6. contracted condition stack current free block size can be incorporated before and after adding the chunk size is larger than 64KB, and the size of the top of the stack reaches the threshold value, possible contraction of the stack, the stack top of the operating system is returned to the free memory.
7. The need to maintain long-term storage is unsuitable for use ptmalloc to manage memory.
8. To support multiple threads, multiple threads can allocate memory from the same distribution zone (Arena) in, ptamlloc assumed that a thread A freed memory, thread B will be applied for a similar amount of memory, but the release of A with B requires memory the memory is not necessarily completely equal, there may be a small error, it is necessary to keep the memory block for cutting merging, this process may fragment memory.

Memory management data structures Overview

Main_arena 与 non_main_arena

1. The main distribution area and the non-main distribution area endless chain management.
2. Each distribution zone using a mutex (the mutex) to make the thread exclusive access to the distribution area
3. Each process has only one main distribution area, but there may be a plurality of non-main distribution area, and the number of zones assigned once increase will not reduce.
4. The main distribution area accessible area and a heap process mmap mapping region (and the sbrk mmap), the main distribution area can only access the non-process mmap mapping region (mmap)

Chunk of space multiplexing

1. When a chunk is in use, then its next chunk of prev_size field is invalid, the current chunk can be used.

Free chunk container

Fast Bins

Is not greater than 1. max_fast chunk was released after the first will be placed in the fast bins, fast bins in the chunk does not change the using labeled P.
2. At a particular time, ptmalloc will traverse fast bins in the chunk, will be merged with adjacent free chunk, chunk and the combined addition of the unsorted bin, and then unsorted bin bins in the chunk join in.

Unsorted Bin

1. If the user releases the chunk is greater than max_fast, fast or after the free chunk bins combined, these first chunk unsorted bin will be placed in the queue.
2. If the unsorted bin does not meet the requirements allocation, malloc will be added to the unsorted bin bins in the chunk.

Top chunk

1.Top chunk always considered after the fast bins and bins in the distribution.

mmaped chunk

1. When the fast bins, bins and Top chunk allocation requirements are not met, then use mmap to use memory-mapped directly to map pages into the process space.

Last remainder

1. When need to allocate a chunk Small, but could not find a suitable small bins in the chunk, if the size of the last chunk is greater than the required REMAINDER Small size chunk, REMAINDER last chunk chunk is split into two, one of which is returned to the chunk user, another chunk becomes the new last remainder chunk.

Memory Allocation Overview

In Example 32 the system
1 acquires the lock allocation area, in order to prevent simultaneous access to a plurality of threads assigned area, the need to obtain a lock prior to dispensing the allocation area. Thread first check whether there is already a thread private area allocated example, if there are attempts to lock the distribution area, if the lock is successful, the use of the allocated memory area allocation, otherwise, the thread assigned search area circular list trying to get a free distribution area. If all zones have been assigned shackles, then ptmalloc will open up a new distribution area, the area added to the distribution list and thread global distribution cycle area and private instance lock, and then use the assigned zone dispensing operation. Opened up a new area allocated certain non-main distribution area, because the main distribution area is the parent process had inherited. Calls mmap to create a sub-heap when open non-main distribution area, and set top chunk.
2. converts the user's request for the chunk size of the actual space to be assigned.
3. Analyzing the desired size of the chunk allocation meets chunk_size <= max_fast, if yes, then go to the next step, otherwise go to step 5.
4. The first attempt to take a desired size in the chunk allocated to the user in a fast bins. If found, the assignment ends, otherwise go to the next step.
The determination whether the desired size in small bins, i.e. determines chunk_size <512B is established. If the chunk size in small bins, then take the next step, otherwise go to step 6.
The size of the chunk of the required distribution, find the specific location of a small bin, the bin removal from the tail of a just meet chunk size. If successful, the assignment ends, otherwise, go to the next step.
7. At this step, the need is to allocate a large memory, or small bins can not find a suitable chunk. So when, ptmalloc first goes through fast bins in the chunk, chunk merge adjacent, and link to the unsorted bin, and then traverse the unsorted bin of the chunk, if unsorted bin is only one chunk, and the chunk allocated in the last was used, and the required size of the allocated chunk belongs small bins, and the size of the chunk is greater than equal to the required size distribution, directly to the chunk cutting this case, the end of the distribution, otherwise according to the size of the chunk of space placed in small bins or in large bins, after the completion of traverse, into the next step.
8. At this step, the need is to allocate a large memory, or small bins and the unsorted bin can not find a suitable chunk, and fast bins and unsorted bin in the chunk are all cleaned up. From the large bins in accordance with the "smallest-first, best-fit " principle, to find a suitable chunk, chunk from dividing a desired size, and the rest to link back to the bins. If the operation is successful, the assignment ends, otherwise go to the next step.
9. If the search fast bins and bins are not suitable chunk is found, then you need to operate top chunk allocated. Top chunk size is determined to meet the required size of the chunk, if it is, separated from in a chunk to Top. Otherwise, go to the next step.
10. At this step, can not be described top chunk allocation meet requirements, so there are two options: If the main distribution area, the sbrk call, increasing the top chunk size; if the non-primary distribution zone, to allocate a new call mmap the sub-heap, increasing the size of the top chunk; mmap directly or dispensing. Here, the need to rely on the size of the chunk to decide which method to use in the end. Determining whether the desired chunk size distribution greater than or equal mmap assigned threshold, and if yes, then go to the next step, call mmap distribution, denied skip to step 12, increasing the size of the chunk Top.
11. Use mmap system call to map the memory space of the program a chunk_size align 4KB size space, and then the memory pointer returned to the user.
12 first determines whether the call malloc time, if the main distribution area, the initialization sequence is required, a size distribution (chunk_size + 128KB) align 4KB size space as the initial heap. If already been initialized, the primary partition is called sbrk increased heap space, the main distribution area partition cut out a chunk in the chunk Top, so as to meet the demand distribution, and a memory pointer back to the user.

Memory recall Overview

1.free first need to get the same function lock allocation area to ensure thread safety.
2. The judgment passed pointer is 0, if 0, do nothing, direct return. Otherwise, go to the next step.
3. To determine whether the required release chunk mmaped chunk, and if so, call munmap release mmaped chunk, lift the memory map, the space is no longer valid. If turned dynamic adjustment mechanism mmap assignment threshold, and the current recovered chunk size is larger than the threshold value mmap assigned the mmap allocation threshold is set to the size of the chunk, the mmap shrink threshold is set at twice the mmap assignment threshold, is released complete, otherwise skip to the next step.
4. The position and size of the chunk is determined which, if chunk_size <= max_fast, and the chunk is not at the top of the heap, that is not adjacent to the Top chunk, then jump to the next step, otherwise jump to the first 6 steps.
5. The fast bins into the chunk, the chunk fast bins placed in use does not modify the status bit chunk P. Nor with the adjacent chunk. Knowledge into them, that's all. This step is done after the release will be the end, the program returns from the free function.
6. Before determining whether a chunk is in use, if the first block is a free block, the merge. And take the next step.
7. The current release is determined whether the chunk is a chunk Top, if yes, then go to Step 9, otherwise go to the next step.
8. next determines whether a chunk is in use, if the next chunk is idle, then combined and placed in unsorted bin chunk combined. Note that here in the merger process, the size of the chunk to be updated to reflect the size of the chunk of the merger. And go to step 10.
9. If you perform this step, indicating that the release of a top chunk and an adjacent chunk. Regardless of how big it is, it will merge with the top chunk, and update the size of the top chunk of information. Take the next step.
10 determines whether the size of the chunk is greater than the combined FASTBIN_CONSOLIDATION_THRESHOLD, if so, the merge operation is triggered fsat bins, fast bins in the chunk is traversed, and to merge adjacent free chunk, chunk merged unsroted bin will be placed in. fast bins are empty, the next step after the transfer operation is completed.
11. The top chunk size is determined whether the threshold value is greater than a mmap contraction, and if so, the main distribution area, it tries to return a portion of the top chunk in the operating system. However, the first space is not allocated 128kb returned, ptmalloc will always manage this memory, in response to a request for allocation; non main distribution area if, for sub-heap will shrink, the return portion of the top chunk to the operating system, if the top chunk for the entire sub-heap, put the entire sub-heap back to the operating system. After doing this step, the release of quits from the free function. It can be seen that contraction of the stack of the current free
longitudinal chunk size can be incorporated together with the chunk size is greater than 64K, and to top chunk size to reach the threshold mmap contraction, possible shrinkage of the stack.

Content Sources

"Glibc memory management ptmalloc source code analysis"

Guess you like

Origin www.cnblogs.com/luoleqi/p/12335965.html