blob: 25a867f139cc7a387c95bd4936c7d629f0e0e5bd [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _allocating-objects:
4
5Allocating Objects on the Heap
6==============================
7
8
Georg Brandl60203b42010-10-06 10:11:56 +00009.. c:function:: PyObject* _PyObject_New(PyTypeObject *type)
Georg Brandl54a3faa2008-01-20 09:30:57 +000010
11
Georg Brandl60203b42010-10-06 10:11:56 +000012.. c:function:: PyVarObject* _PyObject_NewVar(PyTypeObject *type, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +000013
14
Georg Brandl60203b42010-10-06 10:11:56 +000015.. c:function:: PyObject* PyObject_Init(PyObject *op, PyTypeObject *type)
Georg Brandl54a3faa2008-01-20 09:30:57 +000016
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000017 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 Brandl54a3faa2008-01-20 09:30:57 +000022
23
Georg Brandl60203b42010-10-06 10:11:56 +000024.. c:function:: PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +000025
Georg Brandl60203b42010-10-06 10:11:56 +000026 This does everything :c:func:`PyObject_Init` does, and also initializes the
Georg Brandl54a3faa2008-01-20 09:30:57 +000027 length information for a variable-size object.
28
29
Georg Brandl60203b42010-10-06 10:11:56 +000030.. c:function:: TYPE* PyObject_New(TYPE, PyTypeObject *type)
Georg Brandl54a3faa2008-01-20 09:30:57 +000031
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000032 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
Antoine Pitrou39668f52013-08-01 21:12:45 +020035 the memory allocation is determined from the :c:member:`~PyTypeObject.tp_basicsize` field of
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000036 the type object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000037
38
Georg Brandl60203b42010-10-06 10:11:56 +000039.. c:function:: TYPE* PyObject_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +000040
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000041 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
Antoine Pitrou39668f52013-08-01 21:12:45 +020044 plus *size* fields of the size given by the :c:member:`~PyTypeObject.tp_itemsize` field of
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000045 *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 Brandl54a3faa2008-01-20 09:30:57 +000049
50
Georg Brandl60203b42010-10-06 10:11:56 +000051.. c:function:: void PyObject_Del(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +000052
Georg Brandl60203b42010-10-06 10:11:56 +000053 Releases memory allocated to an object using :c:func:`PyObject_New` or
54 :c:func:`PyObject_NewVar`. This is normally called from the
Antoine Pitrou39668f52013-08-01 21:12:45 +020055 :c:member:`~PyTypeObject.tp_dealloc` handler specified in the object's type. The fields of
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000056 the object should not be accessed after this call as the memory is no
57 longer a valid Python object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000058
59
Georg Brandl60203b42010-10-06 10:11:56 +000060.. c:var:: PyObject _Py_NoneStruct
Georg Brandl54a3faa2008-01-20 09:30:57 +000061
62 Object which is visible in Python as ``None``. This should only be accessed
Georg Brandl60203b42010-10-06 10:11:56 +000063 using the :c:macro:`Py_None` macro, which evaluates to a pointer to this
Georg Brandl54a3faa2008-01-20 09:30:57 +000064 object.
Georg Brandle69cdf92009-01-04 23:20:14 +000065
66
67.. seealso::
68
Georg Brandl60203b42010-10-06 10:11:56 +000069 :c:func:`PyModule_Create`
Georg Brandle69cdf92009-01-04 23:20:14 +000070 To allocate and create extension modules.
71