From the python source to parse the Freeblock under Python

Source: Internet
Author: User

This article mainly introduces the source code of Python to parse the freeblock under Python, including the memory space allocation and so on knowledge, needs the friend to be possible to refer to under

1 Introduction

In Python memory management, there is a block concept. It is similar to the SGI secondary space configurator.

First apply a large space (4KB) and cut it into a small portion (8, 161 until 512).

When a memory request is requested, the simple process is to find the corresponding block based on size and then give it a copy on the Freeblock.

2 questions

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

?

1 2 3 4 5 6 7 8 9 Static void* _pyobject_malloc (void* ctx, size_t nbytes) {... pool->freeblock = (block*) pool + pool->nextoffset; Pool->nextoffset + + index2size (size); * (Block * *) = NULL; pool->freeblock; [1] ...}

Freeblock points to an idle list, and it is well understood that it is assigned a value. But why add code 1 to that sentence!

It is easy to see the role of a child shoe that is familiar to C, and it is assigned null for *freeblock.

But why would you do that?

Until you see the code for the memory recycle:

?

1 2 3 4 5 6 7 static void _pyobject_free (void* ctx, void*p) {... * (block**) p = lastfree = pool->freeblock; Pool->freeblock = (blo ck*) p; ... }

Recall the SGI secondary space configuration, which requires a linked list pointing to the small blocks available in the block. Because these are fast, are discrete, only use a pointer to index it.

In the SGI secondary space configuration, a union is used to achieve space-saving purposes: when there is data, it stores the real data; when there is no data, it becomes a pointer to the next available memory:

?

1 2 3 4 Union __obj {Union __obj* free_list_link; char client_data[];

In this way, the problem becomes obvious. 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 reclaimed, it should be inserted in the freeblock of the linked table header, freeblock be updated to point to it. At the same time, p points to what the original Freeblock pointed to, which is a very simple list insertion operation.

In this way, we can use Freeblock = * Freeblock to work in the traversal.

As shown in the following illustration:

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.