Python source code analysis Reading Notes: Chapter 2-integer object

Source: Internet
Author: User
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.

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.