17db96d56Sopenharmony_ci.. highlight:: c 27db96d56Sopenharmony_ci 37db96d56Sopenharmony_ci.. _allocating-objects: 47db96d56Sopenharmony_ci 57db96d56Sopenharmony_ciAllocating Objects on the Heap 67db96d56Sopenharmony_ci============================== 77db96d56Sopenharmony_ci 87db96d56Sopenharmony_ci 97db96d56Sopenharmony_ci.. c:function:: PyObject* _PyObject_New(PyTypeObject *type) 107db96d56Sopenharmony_ci 117db96d56Sopenharmony_ci 127db96d56Sopenharmony_ci.. c:function:: PyVarObject* _PyObject_NewVar(PyTypeObject *type, Py_ssize_t size) 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ci 157db96d56Sopenharmony_ci.. c:function:: PyObject* PyObject_Init(PyObject *op, PyTypeObject *type) 167db96d56Sopenharmony_ci 177db96d56Sopenharmony_ci Initialize a newly allocated object *op* with its type and initial 187db96d56Sopenharmony_ci reference. Returns the initialized object. If *type* indicates that the 197db96d56Sopenharmony_ci object participates in the cyclic garbage detector, it is added to the 207db96d56Sopenharmony_ci detector's set of observed objects. Other fields of the object are not 217db96d56Sopenharmony_ci affected. 227db96d56Sopenharmony_ci 237db96d56Sopenharmony_ci 247db96d56Sopenharmony_ci.. c:function:: PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size) 257db96d56Sopenharmony_ci 267db96d56Sopenharmony_ci This does everything :c:func:`PyObject_Init` does, and also initializes the 277db96d56Sopenharmony_ci length information for a variable-size object. 287db96d56Sopenharmony_ci 297db96d56Sopenharmony_ci 307db96d56Sopenharmony_ci.. c:function:: TYPE* PyObject_New(TYPE, PyTypeObject *type) 317db96d56Sopenharmony_ci 327db96d56Sopenharmony_ci Allocate a new Python object using the C structure type *TYPE* and the 337db96d56Sopenharmony_ci Python type object *type*. Fields not defined by the Python object header 347db96d56Sopenharmony_ci are not initialized; the object's reference count will be one. The size of 357db96d56Sopenharmony_ci the memory allocation is determined from the :c:member:`~PyTypeObject.tp_basicsize` field of 367db96d56Sopenharmony_ci the type object. 377db96d56Sopenharmony_ci 387db96d56Sopenharmony_ci 397db96d56Sopenharmony_ci.. c:function:: TYPE* PyObject_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size) 407db96d56Sopenharmony_ci 417db96d56Sopenharmony_ci Allocate a new Python object using the C structure type *TYPE* and the 427db96d56Sopenharmony_ci Python type object *type*. Fields not defined by the Python object header 437db96d56Sopenharmony_ci are not initialized. The allocated memory allows for the *TYPE* structure 447db96d56Sopenharmony_ci plus *size* fields of the size given by the :c:member:`~PyTypeObject.tp_itemsize` field of 457db96d56Sopenharmony_ci *type*. This is useful for implementing objects like tuples, which are 467db96d56Sopenharmony_ci able to determine their size at construction time. Embedding the array of 477db96d56Sopenharmony_ci fields into the same allocation decreases the number of allocations, 487db96d56Sopenharmony_ci improving the memory management efficiency. 497db96d56Sopenharmony_ci 507db96d56Sopenharmony_ci 517db96d56Sopenharmony_ci.. c:function:: void PyObject_Del(void *op) 527db96d56Sopenharmony_ci 537db96d56Sopenharmony_ci Releases memory allocated to an object using :c:func:`PyObject_New` or 547db96d56Sopenharmony_ci :c:func:`PyObject_NewVar`. This is normally called from the 557db96d56Sopenharmony_ci :c:member:`~PyTypeObject.tp_dealloc` handler specified in the object's type. The fields of 567db96d56Sopenharmony_ci the object should not be accessed after this call as the memory is no 577db96d56Sopenharmony_ci longer a valid Python object. 587db96d56Sopenharmony_ci 597db96d56Sopenharmony_ci 607db96d56Sopenharmony_ci.. c:var:: PyObject _Py_NoneStruct 617db96d56Sopenharmony_ci 627db96d56Sopenharmony_ci Object which is visible in Python as ``None``. This should only be accessed 637db96d56Sopenharmony_ci using the :c:macro:`Py_None` macro, which evaluates to a pointer to this 647db96d56Sopenharmony_ci object. 657db96d56Sopenharmony_ci 667db96d56Sopenharmony_ci 677db96d56Sopenharmony_ci.. seealso:: 687db96d56Sopenharmony_ci 697db96d56Sopenharmony_ci :c:func:`PyModule_Create` 707db96d56Sopenharmony_ci To allocate and create extension modules. 717db96d56Sopenharmony_ci 72