Go Language Memory Splitter-fixalloc

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

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.

struct FixAlloc{uintptr size;void *(*alloc)(uintptr);void (*first)(void *arg, byte *p);// called first time p is returnedvoid *arg;MLink *list;byte *chunk;uint32 nchunk;uintptr inuse;// in-use bytes nowuintptr sys;// bytes obtained from system};

FIXALLOC memory structure diagram, a look is very simple, simple to not appear in this article is necessary.

listA 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) . chunkThe 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, the memory allocator does not directly use the malloc class function to apply to the system, but to go 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, the focus is shifted to chunk, if there is enough space in the 128k chunk memory, cut a piece of memory to return to use, if chunk memory does not have the remaining memory, then from the operating system to apply for 128k memory replacement 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 is, the number of underlying threads, but the span object is not necessarily so stable, so the fixalloc memory may increase the factor is too many objects of 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.

Related Article

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.