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: