Chapter 2-integer object
Integer object definition:
Typedef struct {
Pyobject_head
Long ob_ival;
} Py1_bject;
We can see that only one long field ob_ival is added to save the integer. Long is used because the highest sign bit of Long is used as the overflow flag.
Memory Management Mechanism of integer objects:
1. small INTEGER: the integer in the range [-nsmallnegints, nsmallposints) is defined as a small integer, Which is cached in the object pool and will not be destroyed during Python running, for quick reference (for example, when used for loop variables ). The value range can be specified manually, but it must be re-compiled.Code
2. Common INTEGER: Except for integers in the small Integer Range, All integers are common integers and are stored in the object pool linked by the block_list linked list.
Struct _ intblock {
Struct _ intblock * next;
Py1_bject objects [n_1_bjects];
};
Typedef struct _ intblock pyintblock;
Static py1_bject * free_list = NULL;
The idle linked list of free_list links unused Objects Memory. Note: In free_list, The ob_type field acts as a pointer to the next node, and the type security is obviously abandoned here. The table header is at the end of the array.
Each time you need to apply for an integer object, extract an object from the free_list header, and then point free_list to the next element.
When the memory of an object is recycled, the object is re-linked to free_list.
The small integer is located at the end of the block_list linked list because it is initialized first.