"Glibc Memory Management" notes DAY2

Ptmalloc memory management design

  • Large memory allocation with a long life cycle using mmap.
  • Particularly large memory allocation always use mmap.
  • 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 needs to be mapped mmap a plurality of 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.
  • Try just Cache free small memory blocks temporary use of a large block of memory at the time of the release of large blocks of memory are returned directly or long life cycle of the operating system.
  • Small memory blocks for free only in malloc and free time to merge, when the free block of free memory may be placed in the pool, not necessarily return to the operating system.
  • Heap shrinkage conditions 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.
  • The need to maintain long-term storage is unsuitable for use ptmalloc to manage memory.
  • After To support multithreading, multiple threads can allocate memory from the same distribution area (arena) in, ptmalloc suppose thread A freed block of memory, thread B will apply for a similar amount of memory, but the memory with the release of A B memory needed not necessarily completely equal, there may be a small error, it is necessary to keep the memory block for cutting and merging, this process may fragment memory.

Main_arena 与 non_main_arena

  • Main distribution area and the non-main distribution area managed endless chain.
  • Each distribution zone using a mutex (the mutex) to make the thread mutually exclusive access to the distribution area.
  • Each process has only one main distribution area, but there may be more non-main distribution area.
  • Main distribution area can access the process heap area and mmap mapped area; non-main distribution area can only access the process mmap mapped area.
  • If the main memory area is assigned) to the dispensing system through the mmap (, when the free memory, the main distribution area will be called directly munmap () returned to the system memory.
  • When a thread needs to call malloc () to allocate memory space, the thread to see if there is already a thread private variable distribution area, if there is, try to lock the distribution area, if the lock is successful, the use of the distribution area distribution memory, if it fails, the thread loop search list trying to get a distribution area is not locked. If all of the distribution area have been locked, then malloc () will open up a new distribution area, assigned to the area to join the global distribution chain and lock loop area, then use the allocated memory area allocation operation. In the release operation, the thread is also trying to get a lock where the distribution area of ​​the memory block to be released, if the distribution area is being used by another thread, we need to wait before they can be released operating until another thread releases the mutex after the allocation area .

chunk of organization

  • Whether memory is allocated where, what allocation method, the spatial distribution of user requests use a chunk in ptmalloc to represent. Users call free () function freed memory is also not immediately return to the operating system, on the contrary, they will be represented as a chunk.
  • size域AMP
    • A: represents a chunk belonging to the main distribution area or a non-main distribution area, if they are non-main distribution area, the position is 1, otherwise set to zero.
    • M: indicates the current chunk of memory area which is obtained from the virtual memory. M 1 indicates that the chunk is allocated from mmap mapping area, otherwise it is allocated from the heap area.
    • P: represents a previous block is in use, a front P 0 indicates a chunk is idle, when P is 1, represents a chunk before being used.
  • The first block is always allocated to P 1, to prevent the program by reference to the area does not exist. :

Free chunk container

  • Bins: Ptmalloc maintains a total of 128 bin, and use an array to store the bin
    • unsorted bin: a first array of bins, if the user is released chunk is greater than max_fast, bins or combined fast idle chunk, the first chunk unsorted bin will be placed in the queue, the operation is performed when the malloc, if did not find the right chunk in the fast bins, you will first find the appropriate ptmalloc free chunk in unsorted bin, and then only find the bins. If the unsorted bin can not meet the distribution requirements. malloc will be added to the unsorted bin bins in the chunk. And then proceed to find and distribution process from the bins. Can be seen from this process, unsorted bin bins can be seen as a buffer, increasing it's just to speed up the allocation rate.
    • small bins: an array from the front 64 of the bin 2 are numbered the same in a small bin chunk having the same size. chunk size in two adjacent small bin difference 8bytes. small bins in the chunk in accordance with the recent order of use, and finally released chunk is linked to the head of the list, while applying chunk is from the beginning of the tail of the list, so that each has an equal chance to be a chunk ptmalloc selected.
    • large bins: behind Small bins bin, large bins each of which contains a chunk each bin within a given range, wherein the size of the chunk are arranged in order. Chunk of the same size are arranged in the same order of most recently used. ptmalloc using the "smallest-first, best-fit" principle to locate a suitable chunk in large bins in idle.
    • When idle is linked to bin chunk of time, ptmalloc will indicate that the chunk is in use flag P is set to 0 (note that this flag is actually in the next chunk in), while it also checks before and after ptmalloc whether the chunk is free, and if so, ptmalloc will first merge them into one large chunk, chunk and then merged into unstored bin in. It should be noted that not all of the chunk to be released immediately after being placed in the bin. In order to increase the speed distribution ptmalloc, a small chunk will first called the fast bins into the container.
    • Fast Bins: the general situation is that the program at run time will often need to apply and release some of the smaller memory space. When the dispenser incorporates several small chunk of adjacent, it may be just another small memory requests, so they need from a large distributor of free memory in the separation of a cut, which undoubtedly is a relatively inefficient , therefore, ptmalloc introduced fast bins during dispensing, no greater than max_fast (64B is the default value) of the chunk was released after the first fast bins will be placed in the fast bins chunk flag does not change its use P. Thus it can not combine them when necessary to a chunk assigned by the user is less than or equal to max_fast, ptmalloc first looks for a free block in the respective bins FAST, and then only to find empty bins in the chunk. 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 usortedbin bins in the chunk join in.
  • chunk of tissue other than Bins
    • Top chunk: top chunk assigned to the main area and the non-main distribution area is not the same.
      • Non-main distribution area: pre-allocates the non-main distribution area from a larger area free mmap analog memory sub-heap, in response to the needs of users by managing the sub-heap, because the memory is allocated from low to high address carried by , the highest point in the free memory, there must be a free chunk, called the top chunk. When the bins and fast bins can not meet the needs of the distribution, ptmalloc will try to separate the block of memory to the user in the top chunk, if the top chunk itself is not big enough, the distribution program will reallocate a sub-heap, and migrate to the top chunk a new sub-heap, the new sub-heap with the existing sub-heap connected singly linked together, and then allocate the required memory on the new top chunk allocated to meet the needs, in fact, in the top chunk when the distribution is always considered after the fast bins and bins, so, no matter how much topchunk, it will not be placed in the fast bins or bins. Top chunk size is constantly changing as the allocation and recovery, if the chunk allocated from memory causes Top Top chunk reduced, if the recovered exactly adjacent Top chunk chunk, then the two will be merged into a new chunk top chunk, so that the top chunk becomes large. If recovered in the free time of the memory is greater than a certain threshold, and the size of the top chunk also exceeds the collapse threshold, ptmalloc shrinks sub-heap, if the top-chunk contains the entire sub-heap, ptmalloc calls munmap the entire sub-heap memory returned to the operating system.
      • Main distribution area: since the main distribution zone allocation area is the only area of ​​the heap mapping process, or it can be increased by shrinking the size of the heap process sbrk (), ptmalloc pre-allocates a larger free memory at the start ( so-called heap), top chunk of the main distribution area are allocated on a first call malloc (chunk_size + 128KB) align 4KB size as the initial heap space, when the user allocate memory from the top chunk, can be directly taken out one memory to the user. In the recovery of memory, recovered memory coincided with the top chunk adjacent merge into a new top chunk, when the times recovered the free memory reaches a certain threshold, and the size of the top chunk also exceeded the contraction threshold, performs memory contraction reducing the size of the top chunk, but at least you want to keep a page size of free memory, so the memory is returned to the operating system. If the application is the main distribution area to the top chunk of memory, while the top chunk no free memory, ptmalloc calls sbrk () move on to the process heap boundary brk, and then modify the size of the top chunk.
    • mmaped chunk: When you need to allocate a chunk large enough and fast bins and bins can not meet the requirements, even top chunk itself can not meet the needs of the time allocated, ptmalloc use mmap to use memory-mapped directly to map pages into the process space. Such allocation chunk directly unmapped when it is free, so it will be returned to the operating system memory, a reference to this memory area will once again result in segmentation fault error. This chunk will not be included in any bin.
    • Last remainder: Last remainder is another special chunk, like the top chunk and mmaped chunk, it would not be found in any bins in this chunk. When the 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, wherein a chunk returned to the user, another chunk becomes the new last remainder chuk.

sbrk and mmap

  • ptmalloc started, if the requested space is less than the threshold value assigned mmap (mmap threshold, the default value is 128KB)
    • Main distribution area: call the sbrk space 4KB () increasing a size (128KB + chunk_size) align as a heap.
    • Non-main distribution area: mapping a call mmap size HEAP_MAX_SIZE (on 32-bit system defaults to 1MB, the default is 64MB 64-bit system) space as sub-heap.
  • ptmalloc maintenance space allocation: when a user requests memory allocation, will find a suitable first chunk to the user in this area. When the user releases the heap in the chunk, ptmalloc will use fastbins and bins to organize free chunk. Primary distribution to prepare the user. If desired allocated chunk size is less than mmap allocation threshold, and heap space is not, at this time the main distribution area will increase the heap size by the sbrk () call, the non-main distribution area calls mmap mapping a new sub-heap, but also is to increase the size of the top chunk, the value of each heap are aligned increased to 4KB.
  • When the time of the user request exceeds the mmap allocation threshold, and the main distribution area using the sbrk () allocation failure, or a non-main distribution area can not be allocated to the memory required in the top chunk in, ptmalloc attempts to use mmap () directly mapping a memory to process memory space. Use mmap () chunk direct mapped directly mapped to lift when released, and no longer belongs to the memory space of the process. Any access to the memory segment will generate an error. The space allocated in the heap or sub-heap in you may remain in the process memory space, it can also quoted again (of course is very dangerous).
  • When ptmalloc munmap chunk, if the recovered chunk of space size is larger than mmap current value allocation threshold and less than DEFAULT_MMAP_THRESHOLD_MAX (32-bit system defaults to 512KB, 64-bit system default is 32MB), ptmalloc will mmap assignment threshold is adjusted to the current recovery the size of the chunk, and the systolic threshold mmap (mmap trim threshold) is set to twice mmap assignment threshold. This is the dynamic adjustment mechanism ptmalloc mmap assignment threshold, which mechanism is enabled by default, of course, also be used mallopt () to close the mechanism.

Memory Allocation Overview

  • Allocation algorithm, an example system 32, similar to the 64-bit system.
    • 64 bytes or less: The pool allocation algorithm.
    • Between 64 to 512 bytes: Take a suitable dispensing best match algorithms and pool assignment algorithm.
    • Greater than or equal to 512 bytes: allocation algorithm with the best match.
    • Greater than or equal mmap allocation threshold (default 128KB): mmap allocated according to the allocation policy settings, if not open the dynamic adjustment mechanism mmap allocation threshold of greater than or equal to 128KB direct call mmap allocation. Otherwise, when the allocation threshold greater than or equal to directly call mmap mmap () allocation.
  • ptmalloc specific steps in response to the memory allocation required by the user
    1 acquires the lock allocation area, in order to prevent multiple threads to access the same distribution zone, the need to obtain a lock prior to dispensing the allocated area.

    2. converts the user's request for the chunk size of the actual space to be assigned.

    3. Analyzing the desired size chunk allocation meets chunk_size <= max_fast (max_fast 64B is default), and if so, 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 distribution end. Otherwise, go to the next step.

    The desired size is determined 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 7.

    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 the allocated size needs to be equal, to direct the chunk cutting this case, the end of the distribution, or the size of the chunk according to the space into 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, from
    the division of a desired chunk size, and link back to the rest of 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, instructions can not meet the top chunk allocation requirements, therefore, so there are two options: If the main distribution area, call the sbrk (), to increase the size of the top chunk; non-primary if the distribution zone, to call mmap assigns a new sub-heap, increasing the size of the top chunk; or using mmap () directly assigned. Here, the need to rely on the size of the chunk to decide which method to use in the end. Determining whether the chunk size distribution is not less than the desired mmap allocation threshold, if yes, then go to the next step, mmap calls assigned, otherwise skip to step 12, increasing the size of the chunk Top.

    11. Use mmap system call to map a chunk_size align 4kB size of memory space for the program. The memory pointer is then returned to the user.

    12 first determines whether the call malloc time, if the main distribution area, once initialization is required, a size distribution (chunk_size + 128KB) align 4KB size space as the initial heap. If already been initialized, the main distribution area is called the 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

  free () function accepts a pointer to the allocated area of ​​the pointer as an argument, releasing the pointer points chunk. The method of releasing the specific position of the chunk is located and the size of the chunk is watching.

  • free () function has the following work steps
    1.free () function is also first need to acquire a 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 greater than mmap allocation threshold, a mmap allocation threshold is set to the size of the chunk, the mmap shrink threshold is set at twice the mmap assignment threshold, release 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, go to next step, otherwise go to Step 6 . (Because the adjacent small chunk chunk Top and Top chunk also be combined, so there is necessary to determine not only the size, but also need to determine where adjacent)

    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. Just 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. Analyzing the current chunk released next block is a Top chunk, 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 (default 64KB), and if so, the merge operation is triggered fast bins, fast bins in the chunk is traversed, and with the adjacent free chunk, chunk will be merged into the unsorted bin. fast bins becomes empty, the next step after the transfer operation is completed.

    11 determines whether the size of the top chunk mmap shrinkage greater than a threshold value (default 128KB), and if so, the main distribution area, it tries to return a portion of the top chunk in the operating system. However, space is allocated first to 128KB not returned, ptmalloc will always manage this memory, in response to a user request for dispensing; if a non-primary distribution zone will be sub-heap shrinkage, a portion of the top chunk returned to operating system, if the top chunk for the entire sub-heap, the entire sub-heap will also return to the operating system. After doing this step, the release of the end () function exits from free. As can be seen, the contraction of the stack before and after the current condition of the free chunk size can be incorporated together with the chunk size is larger than 64K, and to top chunk size to reach the threshold mmap contraction, possible shrinkage of the stack.

Boundary labeling

  Ptmalloc achieved using a chunk of memory management, the management of the chunk boundaries based on the unique labeling

  • In different platforms, the minimum size of each chunk, the address alignment is different, dependent ptmalloc size_t defined platform length, for the 32-bit platform, size_t length of 4 bytes, 64-bit platform, the length may be size_t 4 bytes, 8 bytes is also possible, size_t is 8 bytes on Linux X86_64
#ifndef INTERNAL_SIZE_T
#define INTERNAL_SIZE_T size_t
#endif
/* The corresponding word size */
#define SIZE_SZ (sizeof(INTERNAL_SIZE_T))
/*
 MALLOC_ALIGNMENT is the minimum alignment for malloc'ed chunks.
 It must be a power of two at least 2 * SIZE_SZ, even on machines
 for which smaller alignments would suffice. It may be defined as
 larger than this though. Note however that code and data structures
 are optimized for the case of 8-byte alignment.
*/
#ifndef MALLOC_ALIGNMENT
#define MALLOC_ALIGNMENT (2 * SIZE_SZ)
#endif
/* The corresponding bit mask value */
#define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1)
  • SIZE_SZ: size_t size is defined as the bit 4 in byte 32-bit platform, 8-bit bytes, or 4 bytes in the 64-bit platform.
  • 8 bytes in the chunk address alignment platform 32, the platform 64 by 8 bytes or 16-byte aligned on it.
  • Ptmalloc boundary marking method using the memory is divided into many pieces, so that the memory allocation and recovery management. Ptmalloc implementation in source code definition of the structure described malloc_chunk these blocks, and use a macro encapsulates each chunk of the field read, modify, parity, etc. traversing. malloc_chunk defined as follows:
struct malloc_chunk {
 INTERNAL_SIZE_T prev_size; /* Size of previous chunk (if free). */
 INTERNAL_SIZE_T size; /* Size in bytes, including overhead. */
 struct malloc_chunk* fd; /* double links -- used only if free. */
 struct malloc_chunk* bk;
 /* Only used for large blocks: pointer to next larger size. */
 struct malloc_chunk* fd_nextsize; /* double links -- used only if free. */
 struct malloc_chunk* bk_nextsize;
};
  • fd and bk: bk pointer fd and only when this chunk is present only free blocks, which is used to effect a corresponding block is added to the free chunk free chunk list unified management block, if the block chunk is assigned to the application use, so there is no use two pointers (the chunk block has been removed from the free chain), and it is also used as a space of the application, and not waste.
  • fd_nextsize and bk_nextsize: When the current chunk is present in large bins in, large bins in the free chunk is sorted according to size, but the same-sized chunk may have more, an increase of these two fields can speed up the traversal free chunk, and look to meet the needs of free chunk, fd_nextsize points to the next than the current size of a large chunk of the first free chunk, a former bk_nextszie point to the first free chunk large chunk than the current small. If the block chunk is assigned to the application used, then there would be no use two pointers (the block chunk size has been removed from the chain), and it is also used as a space of the application, and not waste.

Content Sources

Zhuangming Jiang teacher "Glibc Memory Management"

Guess you like

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