/* The PyObject_ memory family: high-level object memory interfaces.1See pymem.h for the low-level PyMem_ family.2*/34#ifndef Py_OBJIMPL_H5#define Py_OBJIMPL_H67#include "pymem.h"89#ifdef __cplusplus10extern "C" {11#endif1213/* BEWARE:1415Each interface exports both functions and macros. Extension modules should16use the functions, to ensure binary compatibility across Python versions.17Because the Python implementation is free to change internal details, and18the macros may (or may not) expose details for speed, if you do use the19macros you must recompile your extensions with each Python release.2021Never mix calls to PyObject_ memory functions with calls to the platform22malloc/realloc/ calloc/free, or with calls to PyMem_.23*/2425/*26Functions and macros for modules that implement new object types.2728- PyObject_New(type, typeobj) allocates memory for a new object of the given29type, and initializes part of it. 'type' must be the C structure type used30to represent the object, and 'typeobj' the address of the corresponding31type object. Reference count and type pointer are filled in; the rest of32the bytes of the object are *undefined*! The resulting expression type is33'type *'. The size of the object is determined by the tp_basicsize field34of the type object.3536- PyObject_NewVar(type, typeobj, n) is similar but allocates a variable-size37object with room for n items. In addition to the refcount and type pointer38fields, this also fills in the ob_size field.3940- PyObject_Free(op) releases the memory allocated for an object. It does not41run a destructor -- it only frees the memory. PyObject_Free is identical.4243- PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) don't44allocate memory. Instead of a 'type' parameter, they take a pointer to a45new object (allocated by an arbitrary allocator), and initialize its object46header fields.4748Note that objects created with PyObject_{New, NewVar} are allocated using the49specialized Python allocator (implemented in obmalloc.c), if WITH_PYMALLOC is50enabled. In addition, a special debugging allocator is used if Py_DEBUG51macro is also defined.5253In case a specific form of memory management is needed (for example, if you54must use the platform malloc heap(s), or shared memory, or C++ local storage or55operator new), you must first allocate the object with your custom allocator,56then pass its pointer to PyObject_{Init, InitVar} for filling in its Python-57specific fields: reference count, type pointer, possibly others. You should58be aware that Python has no control over these objects because they don't59cooperate with the Python memory manager. Such objects may not be eligible60for automatic garbage collection and you have to make sure that they are61released accordingly whenever their destructor gets called (cf. the specific62form of memory management you're using).6364Unless you have specific memory management requirements, use65PyObject_{New, NewVar, Del}.66*/6768/*69* Raw object memory interface70* ===========================71*/7273/* Functions to call the same malloc/realloc/free as used by Python's74object allocator. If WITH_PYMALLOC is enabled, these may differ from75the platform malloc/realloc/free. The Python object allocator is76designed for fast, cache-conscious allocation of many "small" objects,77and with low hidden memory overhead.7879PyObject_Malloc(0) returns a unique non-NULL pointer if possible.8081PyObject_Realloc(NULL, n) acts like PyObject_Malloc(n).82PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory83at p.8485Returned pointers must be checked for NULL explicitly; no action is86performed on failure other than to return NULL (no warning it printed, no87exception is set, etc).8889For allocating objects, use PyObject_{New, NewVar} instead whenever90possible. The PyObject_{Malloc, Realloc, Free} family is exposed91so that you can exploit Python's small-block allocator for non-object92uses. If you must use these routines to allocate object memory, make sure93the object gets initialized via PyObject_{Init, InitVar} after obtaining94the raw memory.95*/96PyAPI_FUNC(void *) PyObject_Malloc(size_t size);97#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x0305000098PyAPI_FUNC(void *) PyObject_Calloc(size_t nelem, size_t elsize);99#endif100PyAPI_FUNC(void *) PyObject_Realloc(void *ptr, size_t new_size);101PyAPI_FUNC(void) PyObject_Free(void *ptr);102103104// Deprecated aliases only kept for backward compatibility.105// PyObject_Del and PyObject_DEL are defined with no parameter to be able to106// use them as function pointers (ex: tp_free = PyObject_Del).107#define PyObject_MALLOC PyObject_Malloc108#define PyObject_REALLOC PyObject_Realloc109#define PyObject_FREE PyObject_Free110#define PyObject_Del PyObject_Free111#define PyObject_DEL PyObject_Free112113114/*115* Generic object allocator interface116* ==================================117*/118119/* Functions */120PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);121PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,122PyTypeObject *, Py_ssize_t);123124#define PyObject_INIT(op, typeobj) \125PyObject_Init(_PyObject_CAST(op), (typeobj))126#define PyObject_INIT_VAR(op, typeobj, size) \127PyObject_InitVar(_PyVarObject_CAST(op), (typeobj), (size))128129130PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);131PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);132133#define PyObject_New(type, typeobj) ((type *)_PyObject_New(typeobj))134135// Alias to PyObject_New(). In Python 3.8, PyObject_NEW() called directly136// PyObject_MALLOC() with _PyObject_SIZE().137#define PyObject_NEW(type, typeobj) PyObject_New(type, (typeobj))138139#define PyObject_NewVar(type, typeobj, n) \140( (type *) _PyObject_NewVar((typeobj), (n)) )141142// Alias to PyObject_NewVar(). In Python 3.8, PyObject_NEW_VAR() called143// directly PyObject_MALLOC() with _PyObject_VAR_SIZE().144#define PyObject_NEW_VAR(type, typeobj, n) PyObject_NewVar(type, (typeobj), (n))145146147/*148* Garbage Collection Support149* ==========================150*/151152/* C equivalent of gc.collect(). */153PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);154/* C API for controlling the state of the garbage collector */155PyAPI_FUNC(int) PyGC_Enable(void);156PyAPI_FUNC(int) PyGC_Disable(void);157PyAPI_FUNC(int) PyGC_IsEnabled(void);158159160#if !defined(Py_LIMITED_API)161/* Visit all live GC-capable objects, similar to gc.get_objects(None). The162* supplied callback is called on every such object with the void* arg set163* to the supplied arg. Returning 0 from the callback ends iteration, returning164* 1 allows iteration to continue. Returning any other value may result in165* undefined behaviour.166*167* If new objects are (de)allocated by the callback it is undefined if they168* will be visited.169170* Garbage collection is disabled during operation. Explicitly running a171* collection in the callback may lead to undefined behaviour e.g. visiting the172* same objects multiple times or not at all.173*/174typedef int (*gcvisitobjects_t)(PyObject*, void*);175PyAPI_FUNC(void) PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void* arg);176#endif177178/* Test if a type has a GC head */179#define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)180181PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);182#define PyObject_GC_Resize(type, op, n) \183( (type *) _PyObject_GC_Resize(_PyVarObject_CAST(op), (n)) )184185186187PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);188PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t);189190/* Tell the GC to track this object.191*192* See also private _PyObject_GC_TRACK() macro. */193PyAPI_FUNC(void) PyObject_GC_Track(void *);194195/* Tell the GC to stop tracking this object.196*197* See also private _PyObject_GC_UNTRACK() macro. */198PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);199200PyAPI_FUNC(void) PyObject_GC_Del(void *);201202#define PyObject_GC_New(type, typeobj) \203_Py_CAST(type*, _PyObject_GC_New(typeobj))204#define PyObject_GC_NewVar(type, typeobj, n) \205_Py_CAST(type*, _PyObject_GC_NewVar((typeobj), (n)))206207PyAPI_FUNC(int) PyObject_GC_IsTracked(PyObject *);208PyAPI_FUNC(int) PyObject_GC_IsFinalized(PyObject *);209210/* Utility macro to help write tp_traverse functions.211* To use this macro, the tp_traverse function must name its arguments212* "visit" and "arg". This is intended to keep tp_traverse functions213* looking as much alike as possible.214*/215#define Py_VISIT(op) \216do { \217if (op) { \218int vret = visit(_PyObject_CAST(op), arg); \219if (vret) \220return vret; \221} \222} while (0)223224#ifndef Py_LIMITED_API225# define Py_CPYTHON_OBJIMPL_H226# include "cpython/objimpl.h"227# undef Py_CPYTHON_OBJIMPL_H228#endif229230#ifdef __cplusplus231}232#endif233#endif /* !Py_OBJIMPL_H */234235236