Python Basic Teaching List Object time: 2014-01-19 Source: Server home Contributor: Root
1. Pylistobject Object
typedef struct {
Pyobject_var_head
Pyobject **ob_item;
py_ssize_t allocated;
} Pylistobject;
The obsize in Pyobject_var_head indicates the number of elements that the list object contains.
The allocated represents the memory space occupied by the list object.
Ob_item actually points to an array of pointers, each of which points to the true pyobject.
2. Pylistobject Object Use
When created, you can specify the size to open up the required memory for the element list.
Op->ob_item = (pyobject * *) Pymem_malloc (nbytes);
memset (op->ob_item, 0, nbytes);
After you create a new list object, you can insert, add, and delete elements. If the allocated is insufficient, the memory space can be automatically applied.
The memory growth model for list in Python is as follows:
0, 4, 8, 16, 25, 35, 46, 58, 72, 88
The implementation is as follows:
new_allocated = (newsize >> 3) + (NewSize < 9? 3:6);
When you delete a list object
i = py_size (OP);
while (---->= 0) {//reduces references to real pyobject objects individually
Py_xdecref (Op->ob_item[i]);
}
Pymem_free (Op->ob_item); Then release the element pointer list
and whether the list object is destroyed, as follows.
3. Pylistobject Object Buffer Pool
#define PYLIST_MAXFREELIST 80
Static Pylistobject *free_list[pylist_maxfreelist];
Free_list is an array of pointers that point to the list object.
When you create a list object:
if (numfree) {//If free_list has idle, point directly to
numfree--;
op = free_list[numfree];
_py_newreference ((Pyobject *) op);
} else {//otherwise new open memory space
op = pyobject_gc_new (Pylistobject, &pylist_type);
}
When you delete a list object:
if (Numfree < pylist_maxfreelist)//If you have free space, put the List object in Free_list
free_list[numfree++] = op;
else//otherwise directly destroyed
Py_type (OP)->tp_free ((Pyobject *) op);
Python basic Teaching List Object Goto