Go Language Memory Splitter-mspan

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

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. In order to feel the visual image of span, or to draw a map, graphics is the best communication language.

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 listPageIDstart;// starting page numberuintptrnpages;// number of pages in spanMLink*freelist;// list of free objectsuint32ref;// number of allocated objects in this spanint32sizeclass;// size classuintptrelemsize;// computed from sizeclass or from npagesuint32state;// MSpanInUse etcint64   unusedsince;// First time spotted by GC in MSpanFree stateuintptr npreleased;// number of pages released to the OSbyte*limit;// end of data in spanMTypestypes;// types of allocated objects in this span};

The more important fields of the span structure appear in the structure diagram above, which is not to say that the other fields are unimportant. The structure of span has pre/next two pointers, which can be guessed as a way to construct a doubly linked list. Yes, in actual use, span does appear in a two-way loop 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) stored in this span (for example: 3 page is drawn), start can be regarded as a page pointer, point to the first page, with the first page of course you can figure out any of the following page start address, 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? In fact, to each page to calculate an ID, is very simple thing, as long as the page's address divided by 4096 rounding (pseudo code: page_addr>>20 ) can, of course, the premise is to ensure that each page by 4k alignment. It's very subtle, so every page has an integer ID, and any memory address can be shifted to figure out which page this address belongs to, which is important.

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, of course, allocated small objects. When assigning small objects, the Start field maintains all the page, and the last one is cut into a contiguous block of memory, the size of the memory block is, of course, the size of the small object, and the segmented chunks of memory 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);

Span is written here first, and then the Mcache, Mcentral, mheap and other core components are also involved in how to use span.

Note: This article is based on the Go1.1.2 version code


***

Write here, I feel that I did not write a clear span, write source Analysis technology article is really more than read the source of the challenge, I try to use the smallest code show to tell the principle clearly. It's not enough to find your own foundation.

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.