Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _allocating-objects: |
| 4 | |
| 5 | Allocating Objects on the Heap |
| 6 | ============================== |
| 7 | |
| 8 | |
| 9 | .. cfunction:: PyObject* _PyObject_New(PyTypeObject *type) |
| 10 | |
| 11 | |
| 12 | .. cfunction:: PyVarObject* _PyObject_NewVar(PyTypeObject *type, Py_ssize_t size) |
| 13 | |
| 14 | |
| 15 | .. cfunction:: PyObject* PyObject_Init(PyObject *op, PyTypeObject *type) |
| 16 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 17 | Initialize a newly-allocated object *op* with its type and initial |
| 18 | reference. Returns the initialized object. If *type* indicates that the |
| 19 | object participates in the cyclic garbage detector, it is added to the |
| 20 | detector's set of observed objects. Other fields of the object are not |
| 21 | affected. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 22 | |
| 23 | |
| 24 | .. cfunction:: PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size) |
| 25 | |
| 26 | This does everything :cfunc:`PyObject_Init` does, and also initializes the |
| 27 | length information for a variable-size object. |
| 28 | |
| 29 | |
| 30 | .. cfunction:: TYPE* PyObject_New(TYPE, PyTypeObject *type) |
| 31 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 32 | Allocate a new Python object using the C structure type *TYPE* and the |
| 33 | Python type object *type*. Fields not defined by the Python object header |
| 34 | are not initialized; the object's reference count will be one. The size of |
| 35 | the memory allocation is determined from the :attr:`tp_basicsize` field of |
| 36 | the type object. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | .. cfunction:: TYPE* PyObject_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size) |
| 40 | |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 41 | Allocate a new Python object using the C structure type *TYPE* and the |
| 42 | Python type object *type*. Fields not defined by the Python object header |
| 43 | are not initialized. The allocated memory allows for the *TYPE* structure |
| 44 | plus *size* fields of the size given by the :attr:`tp_itemsize` field of |
| 45 | *type*. This is useful for implementing objects like tuples, which are |
| 46 | able to determine their size at construction time. Embedding the array of |
| 47 | fields into the same allocation decreases the number of allocations, |
| 48 | improving the memory management efficiency. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 49 | |
| 50 | |
| 51 | .. cfunction:: void PyObject_Del(PyObject *op) |
| 52 | |
| 53 | Releases memory allocated to an object using :cfunc:`PyObject_New` or |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 54 | :cfunc:`PyObject_NewVar`. This is normally called from the |
| 55 | :attr:`tp_dealloc` handler specified in the object's type. The fields of |
| 56 | the object should not be accessed after this call as the memory is no |
| 57 | longer a valid Python object. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 58 | |
| 59 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 60 | .. cvar:: PyObject _Py_NoneStruct |
| 61 | |
| 62 | Object which is visible in Python as ``None``. This should only be accessed |
| 63 | using the :cmacro:`Py_None` macro, which evaluates to a pointer to this |
| 64 | object. |
Georg Brandl | e69cdf9 | 2009-01-04 23:20:14 +0000 | [diff] [blame] | 65 | |
| 66 | |
| 67 | .. seealso:: |
| 68 | |
| 69 | :cfunc:`PyModule_Create` |
| 70 | To allocate and create extension modules. |
| 71 | |