From Python's source code to parse the Freeblock under Python

Source: Internet
Author: User
1 Introduction

In Python memory management, there is a block concept. It is more similar to the SGI sub-space Configurator.
First, apply a large space (4KB) and cut it into a small portion (8, 161 until 512).
When there is a request for memory, the simple process is to find the block according to the size, and then give it a copy on the Freeblock.

2 questions

The whole process is a more natural way of slab distribution. But when I read this piece of code, I was wondering:

Static void* _pyobject_malloc (void* ctx, size_t nbytes) {    ...  Pool->freeblock = (block*) pool + pool->nextoffset;    Pool->nextoffset + = index2size (size);    * (Block * *) (Pool->freeblock) = NULL; [1]    ...}

Freeblock points to the idle list and assigns it a good understanding. But why add code 1 to that sentence!
It is easy to see the effect of a child's shoe that is familiar to C, which is assigned a value of NULL for *freeblock.

But why did you do it?
Until you see the code for Memory recycling:

static void _pyobject_free (void* ctx, void*p) {  ...  * (block**) p = lastfree = pool->freeblock;  Pool->freeblock = (block*) p;  ...}

Recall the SGI secondary space configuration, which requires a linked list that points to the small chunks available in the block. Because these are fast and discrete, you can index it only with pointers.
In SGI's secondary space configuration, a union was used to achieve space-saving purposes: when there is data, it stores real data, and when there is no data, it becomes a pointer to the next piece of available memory:

Union __obj {  Union __obj* free_list_link;  Char client_data[];};

The problem becomes obvious when you think about it. Freeblock points to a linked list, and the next field of the list is indexed by itself.
In _pyobject_free, the memory p is to be recycled, it should be plugged into the Freeblock's list header, Freeblock is updated to point to it. At the same time, p points to the original Freeblock point, which is a very simple list insert operation.
In this way, we can use Freeblock = * Freeblock to work in the loop.
As shown in the following:

  • 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.