This is a creation in Article, where the information may have evolved or changed.
Go Language Memory Splitter-fixalloc
OctoberSkoo Read 918 times 1 people like 0Comment Collection
Yesterday wrote a go language memory dispenser design, recording the general structure of the memory allocator. Before introducing the core implementation of the memory allocator, this article first introduces a tool component--fixalloc in the memory allocator. Fixalloc is not a core component, it is just a basic tool to assist in implementing the core of the entire memory allocator, and it can be seen that fixalloc is a relatively important component. The purpose of introducing fixalloc is only to allocate MCache
and MSpan
two specific objects, so there are two components in the memory allocator spanalloc
cachealloc
(see diagram of the Go language memory dispenser design). The two structures of Mcache and mspan are defined in malloc.h.
The FIXALLOC structure defined in the Malloc.h file is as follows, the key three fields are alloc, list, and chunk, and the other fields are mostly used to count the state data, such as how much memory is allocated.
from system};
FIXALLOC memory structure diagram, a look is very simple, simple to not appear in this article is necessary.
list
A linked list is hung on the pointer, and each node of the list is a fixed-size block of memory, and the list in Cachealloc stores the size of the memory block sizeof(MCache)
, while the Spanalloc list stores the memory block size sizeof(MSpan)
. chunk
The pointer is always mounted as a 128k large block of memory.
Fixalloc provides three APIs, namely runtime Fixalloc_init, Runtime Fixalloc_alloc and runtime Fixalloc_free.
Assign a pseudo-code of Mcache and Mspan:
MCache *mcache;mcache = (MCache *) runtime·FixAlloc_Alloc(cachealloc);MSpan *mspan;mspan = (MSpan *) runtime·FixAlloc_Alloc(spanalloc);
This pseudo-code shows the allocation of a Mcache and Mspan object, memoryDispenserInstead of directly using the malloc class function to apply to the system, it went fixalloc. When using Fixalloc to assign Mcache and Mspan objects, the first is to find the list of Fixalloc lists, and if the list is not empty, take a block of memory directly back to use; If the list is empty, shift the focus to chunk, if there is enough space in the 128k chunk memory,CuttingA piece of memory comes back to use, if chunk memory has no memory left,Operating SystemThen apply 128k memory instead of the old chunk. Fixalloc's fixed object allocation logic is so simple that the release logic is simpler, freeing the object to be placed directly in the list and not returned to the operating system. Of course, the number of Mcache is basically stable, that isBottomThe number of threads, but the span object is not necessarily so stable, so the fixalloc memory may grow because there are too many objects in the span.
Fixalloc implementation is located in the mfixalloc.c file, the code is currently less than 100 lines, it is too simple. Originally planned this article together introduced Fixalloc and Mspan two basic components, today's body uncomfortable, a small cold, no energy to write Mspan.
Note: This article is based on the Go1.1.2 version.
Lying on the bed, with the millet box while watching the boring TV, while holding a computer to write technical articles, it seems to be able to let oneself in a very relaxed state, the premise may be that they need to write things at home, no longer need to go through the code bar.