整數(shù)對(duì)象 PyIntObject
PyIntObject 是一個(gè)值不可變對(duì)象
定義
typedef struct {
PyObject_HEAD
long ob_ival;
} PyIntObject;
相應(yīng)的與整數(shù)類型相對(duì)應(yīng)的類型對(duì)象為PyInt_Type
PyTypeObject PyInt_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"int",
sizeof(PyIntObject),
0,
(destructor)int_dealloc, /* tp_dealloc */
(printfunc)int_print, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
(cmpfunc)int_compare, /* tp_compare */
(reprfunc)int_to_decimal_string, /* tp_repr */
&int_as_number, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
(hashfunc)int_hash, /* tp_hash */
0, /* tp_call */
(reprfunc)int_to_decimal_string, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_INT_SUBCLASS, /* tp_flags */
int_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
int_methods, /* tp_methods */
0, /* tp_members */
int_getset, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
int_new, /* tp_new */
};
Attribute | Operation |
---|---|
int_dealloc | PyIntObject 對(duì)象的析構(gòu)操作 |
int_free | 對(duì)象的釋放操作 |
int_repr | 轉(zhuǎn)化為PyStringObject對(duì)象 |
int_hash | 獲得hash值 |
int_print | 打印 |
int_compare | 比較 |
int_as_number | 數(shù)值操作集合 |
int_methods | 成員函數(shù)集合 |
其中含義
Attribute | Operation |
---|---|
int_dealloc | PyIntObject 對(duì)象的析構(gòu)操作 |
int_free | 對(duì)象的釋放操作 |
int_repr | 轉(zhuǎn)化為PyStringObject對(duì)象 |
int_hash | 獲得hash值 |
int_print | 打印 |
int_compare | 比較 |
int_as_number | 數(shù)值操作集合 |
int_methods | 成員函數(shù)集合 |
創(chuàng)建與維護(hù)
創(chuàng)建
在python的內(nèi)部,python為這些內(nèi)建的對(duì)象提供了特定的C API
創(chuàng)建一個(gè)PyIntObject
可以由下面三種方式:
PyObject *PyInt_FromLong(long ival)
PyObject* PyInt_FromString(char *s, char **pend, int base)
#ifdef Py_USING_UNICODE
PyObject*PyInt_FromUnicode(Py_UNICODE *s, int length, int base)
#endif
小整數(shù)
由于整數(shù)在程序中非常頻繁的使用,尤其是小整數(shù)场航,那么為了確保效率渠牲,便不可能頻繁的申請(qǐng)新內(nèi)存來創(chuàng)建整數(shù)對(duì)象辖众,不斷的釋放就內(nèi)存昏鹃。因此外冀,為提高效率,python對(duì)小整數(shù)使用了內(nèi)存池技術(shù)媳溺。
[intobject.c]
#ifndef NSMALLPOSINTS
#define NSMALLPOSINTS 257
#endif
#ifndef NSMALLNEGINTS
#define NSMALLNEGINTS 5
#endif
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
/* References to small integers are saved in this array so that they
can be shared.
The integers that are saved are those in the range
-NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
*/
static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
#endif
(可以通過以上源碼的方式來修改小整數(shù)的范圍)
在上面代碼中還定義了small_ints
來作為小整數(shù)的對(duì)象池
大整數(shù)
對(duì)于大整數(shù)月幌,python運(yùn)行環(huán)境提供了一塊內(nèi)存,來給這些大整數(shù)輪流使用
[intobject.c]
#define BLOCK_SIZE 1000 /* 1K less typical malloc overhead */
#define BHEAD_SIZE 8 /* Enough for a 64-bit pointer */
#define N_INTOBJECTS ((BLOCK_SIZE - BHEAD_SIZE) / sizeof(PyIntObject))
struct _intblock {
struct _intblock *next;
PyIntObject objects[N_INTOBJECTS];
};
typedef struct _intblock PyIntBlock;
static PyIntBlock *block_list = NULL;
static PyIntObject *free_list = NULL;
(上述代碼顯示了python通過單鏈表的方式來維護(hù)這塊內(nèi)存)
添加和刪除
- 添加悬蔽,創(chuàng)建PyIntObject對(duì)象
[intobject.c]
PyObject *
PyInt_FromLong(long ival)
{
register PyIntObject *v;
// 小整數(shù)池是否被激活
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
// 判斷是否在小整數(shù)池的范圍內(nèi)
if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
// 獲得相應(yīng)值的小整數(shù)對(duì)象
v = small_ints[ival + NSMALLNEGINTS];
// 引用計(jì)數(shù)加一
Py_INCREF(v);
#ifdef COUNT_ALLOCS
if (ival >= 0)
quick_int_allocs++;
else
quick_neg_int_allocs++;
#endif
return (PyObject *) v;
}
#endif
//為通用整數(shù)對(duì)象池申請(qǐng)新內(nèi)存空間
//第一次運(yùn)行時(shí)free_list為NULL,(已用完扯躺,也會(huì)為NULL)
if (free_list == NULL) {
if ((free_list = fill_free_list()) == NULL)
return NULL;
}
/* Inline PyObject_New */
v = free_list;
free_list = (PyIntObject *)Py_TYPE(v);
(void)PyObject_INIT(v, &PyInt_Type);
v->ob_ival = ival;
return (PyObject *) v;
}
關(guān)于內(nèi)存池的分配fill_free_list
[intobject.c]
static PyIntObject *
fill_free_list(void)
{
PyIntObject *p, *q;
/* Python's object allocator isn't appropriate for large blocks. */
p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
if (p == NULL)
return (PyIntObject *) PyErr_NoMemory();
((PyIntBlock *)p)->next = block_list;
block_list = (PyIntBlock *)p;
/* Link the int objects together, from rear to front, then return
the address of the last int object in the block. */
p = &((PyIntBlock *)p)->objects[0];
q = p + N_INTOBJECTS;
//連接為鏈表
while (--q > p)
Py_TYPE(q) = (struct _typeobject *)(q-1);
Py_TYPE(q) = NULL;
return p + N_INTOBJECTS - 1;
}
(注意,這里使用了PyObject中的ob_type指針作為連接指針)
連接為鏈表
- 刪除蝎困,釋放PyIntObject對(duì)象
[intobject.c]
static void
int_dealloc(PyIntObject *v)
{
//檢查對(duì)象是否是PyIntObject(避免對(duì)其派生類進(jìn)行內(nèi)存操作)
if (PyInt_CheckExact(v)) {
//是PyIntObject類型
Py_TYPE(v) = (struct _typeobject *)free_list;
free_list = v;
}
else
//不是PyIntObject類型录语,是其派生類
Py_TYPE(v)->tp_free((PyObject *)v);
}
(在釋放時(shí),將釋放掉的對(duì)象內(nèi)存加到了free_list鏈表禾乘,以備生成時(shí)重復(fù)使用)
- 小整數(shù)對(duì)象池的初始化
[intobject.c]
int
_PyInt_Init(void)
{
PyIntObject *v;
int ival;
// 小整數(shù)池是否被激活
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
for (ival = -NSMALLNEGINTS; ival < NSMALLPOSINTS; ival++) {
if (!free_list && (free_list = fill_free_list()) == NULL)
return 0;
/* PyObject_New is inlined */
v = free_list;
free_list = (PyIntObject *)Py_TYPE(v);
(void)PyObject_INIT(v, &PyInt_Type);
v->ob_ival = ival;
small_ints[ival + NSMALLNEGINTS] = v;
}
#endif
return 1;
}
(上述代碼顯示澎埠,其實(shí)小整數(shù)內(nèi)存池也是通過block_list
來管理的)
參考
《Python 源碼剖析》