go-Memory Allocator

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

Consolidate two articles, Fixalloc and Mspan.


The memory model is as follows:


Fixalloc

fixalloc is not a core component that assists in implementing an underlying tool for the entire memory allocator core. The purpose of introducing fixalloc is simply to allocate mcache and mspan two specific objects, so the memory allocator has spanalloc and cachealloc two components. 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 no need to appear in this article.

list a linked list hanging on the pointer, each node of this list is a fixed-size block of memory, and the list of Cachealloc in the memory block size is sizeof (Mcache) , while the list in Spanalloc stores a memory block size of sizeof (Mspan) . chunk 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.


Mspan

Mspan and Fixalloc, are the basic tool components of the memory allocator, but not much of the intersection with Fixalloc, each of which is effective. Span (Mspan short span) is used to manage a group of page objects, first explaining that Page,page is a 4k size memory block. span is the continuous page to manage it, note is a continuous page, not the east of a west of a disorderly layout page.

The MSPAN structure is defined in the Malloc.h header file with the following code:

struct mspan{mspan*  next ;//In a span linked listmspan*  prev ;//in a span linked Listpageid  start ;//starting page numberuintptr  npages ;// Number of pages in spanmlink*  freelist ;//List of free Objectsuint32  ref ;//Number of all ocated objects in this spanint32  sizeclass ;//Size classuintptr  elemsize ;//computed From Sizeclass or from npagesuint32state;//mspaninuse etcint64 unusedsince;//first time spotted by GC in Mspanfree STA Teuintptr npreleased;//number of pages released to the osbyte*limit;//end of data in spanmtypestypes;//types of Allocat Ed objects in this span}; 

The more important fields of the span structure appear in the structure diagram above. The structure of span has pre/next two pointers, which are used to construct a doubly linked list. Span may be used in the process of assigning small objects (less than or equal to 32k) and may also be used to allocate large objects (greater than 32k), and the metadata for span management varies greatly when assigning different types of objects.

npagesIndicates the number of page (s) that are stored for this span (for example: 3 page is drawn),startCan be viewed as a page pointer, pointing to the first page, with the first page of course you can calculate the starting address of any subsequent page, because span management is always a contiguous set of page. It is important to note that the type of start is PageID, which shows that start is not the first page's starting address, but the ID value of the first page. How is this ID value calculated? Actually giving each page an ID is very simple, as long as the address of this page is divided by 4096 rounding (pseudo-code:page_addr>>20), and of course the premise is that each page is guaranteed to be 4k aligned. This way each page has an integer ID, and any memory address can be shifted to figure out which page the address belongs to.

Start is the most important field of span, and it maintains all of the page. sizeclassif it is 0, it means that span is used to allocate large objects, and the other values are allocated small objects. When assigning small objects, all the page maintained by the Start field will be cut into a contiguous block of memory, and the size of the memory block is the size of the small object, and the segmented memory blocks will be linked to a linked list to be hung on the Freelist field. When assigning large objects, freelist is useless.

Span does the work, and that's it, anyway, to manage a continuous set of page. Each page in the memory allocator will belong to a span,page and will never exist independently. The span-related APIs are:

// 初始化一个span结构,将分配的page放入到这个span中。voidruntime·MSpan_Init(MSpan *span, PageID start, uintptr npages);// 下面这些都是操作span构成的双向链表了。voidruntime·MSpanList_Init(MSpan *list);boolruntime·MSpanList_IsEmpty(MSpan *list);voidruntime·MSpanList_Insert(MSpan *list, MSpan *span);voidruntime·MSpanList_Remove(MSpan *span);

Over ...

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.