Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-19 09:50:54

0001 // The PyObject_ memory family:  high-level object memory interfaces.
0002 // See pymem.h for the low-level PyMem_ family.
0003 
0004 #ifndef Py_OBJIMPL_H
0005 #define Py_OBJIMPL_H
0006 #ifdef __cplusplus
0007 extern "C" {
0008 #endif
0009 
0010 /* BEWARE:
0011 
0012    Each interface exports both functions and macros.  Extension modules should
0013    use the functions, to ensure binary compatibility across Python versions.
0014    Because the Python implementation is free to change internal details, and
0015    the macros may (or may not) expose details for speed, if you do use the
0016    macros you must recompile your extensions with each Python release.
0017 
0018    Never mix calls to PyObject_ memory functions with calls to the platform
0019    malloc/realloc/ calloc/free, or with calls to PyMem_.
0020 */
0021 
0022 /*
0023 Functions and macros for modules that implement new object types.
0024 
0025  - PyObject_New(type, typeobj) allocates memory for a new object of the given
0026    type, and initializes part of it.  'type' must be the C structure type used
0027    to represent the object, and 'typeobj' the address of the corresponding
0028    type object.  Reference count and type pointer are filled in; the rest of
0029    the bytes of the object are *undefined*!  The resulting expression type is
0030    'type *'.  The size of the object is determined by the tp_basicsize field
0031    of the type object.
0032 
0033  - PyObject_NewVar(type, typeobj, n) is similar but allocates a variable-size
0034    object with room for n items.  In addition to the refcount and type pointer
0035    fields, this also fills in the ob_size field.
0036 
0037  - PyObject_Free(op) releases the memory allocated for an object.  It does not
0038    run a destructor -- it only frees the memory.
0039 
0040  - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) don't
0041    allocate memory.  Instead of a 'type' parameter, they take a pointer to a
0042    new object (allocated by an arbitrary allocator), and initialize its object
0043    header fields.
0044 
0045 Note that objects created with PyObject_{New, NewVar} are allocated using the
0046 specialized Python allocator (implemented in obmalloc.c), if WITH_PYMALLOC is
0047 enabled.  In addition, a special debugging allocator is used if Py_DEBUG
0048 macro is also defined.
0049 
0050 In case a specific form of memory management is needed (for example, if you
0051 must use the platform malloc heap(s), or shared memory, or C++ local storage or
0052 operator new), you must first allocate the object with your custom allocator,
0053 then pass its pointer to PyObject_{Init, InitVar} for filling in its Python-
0054 specific fields:  reference count, type pointer, possibly others.  You should
0055 be aware that Python has no control over these objects because they don't
0056 cooperate with the Python memory manager.  Such objects may not be eligible
0057 for automatic garbage collection and you have to make sure that they are
0058 released accordingly whenever their destructor gets called (cf. the specific
0059 form of memory management you're using).
0060 
0061 Unless you have specific memory management requirements, use
0062 PyObject_{New, NewVar, Del}.
0063 */
0064 
0065 /*
0066  * Raw object memory interface
0067  * ===========================
0068  */
0069 
0070 /* Functions to call the same malloc/realloc/free as used by Python's
0071    object allocator.  If WITH_PYMALLOC is enabled, these may differ from
0072    the platform malloc/realloc/free.  The Python object allocator is
0073    designed for fast, cache-conscious allocation of many "small" objects,
0074    and with low hidden memory overhead.
0075 
0076    PyObject_Malloc(0) returns a unique non-NULL pointer if possible.
0077 
0078    PyObject_Realloc(NULL, n) acts like PyObject_Malloc(n).
0079    PyObject_Realloc(p != NULL, 0) does not return  NULL, or free the memory
0080    at p.
0081 
0082    Returned pointers must be checked for NULL explicitly; no action is
0083    performed on failure other than to return NULL (no warning it printed, no
0084    exception is set, etc).
0085 
0086    For allocating objects, use PyObject_{New, NewVar} instead whenever
0087    possible.  The PyObject_{Malloc, Realloc, Free} family is exposed
0088    so that you can exploit Python's small-block allocator for non-object
0089    uses.  If you must use these routines to allocate object memory, make sure
0090    the object gets initialized via PyObject_{Init, InitVar} after obtaining
0091    the raw memory.
0092 */
0093 PyAPI_FUNC(void *) PyObject_Malloc(size_t size);
0094 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
0095 PyAPI_FUNC(void *) PyObject_Calloc(size_t nelem, size_t elsize);
0096 #endif
0097 PyAPI_FUNC(void *) PyObject_Realloc(void *ptr, size_t new_size);
0098 PyAPI_FUNC(void) PyObject_Free(void *ptr);
0099 
0100 
0101 // Deprecated aliases only kept for backward compatibility.
0102 // PyObject_Del and PyObject_DEL are defined with no parameter to be able to
0103 // use them as function pointers (ex: tp_free = PyObject_Del).
0104 #define PyObject_MALLOC         PyObject_Malloc
0105 #define PyObject_REALLOC        PyObject_Realloc
0106 #define PyObject_FREE           PyObject_Free
0107 #define PyObject_Del            PyObject_Free
0108 #define PyObject_DEL            PyObject_Free
0109 
0110 
0111 /*
0112  * Generic object allocator interface
0113  * ==================================
0114  */
0115 
0116 /* Functions */
0117 PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
0118 PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,
0119                                            PyTypeObject *, Py_ssize_t);
0120 
0121 #define PyObject_INIT(op, typeobj) \
0122     PyObject_Init(_PyObject_CAST(op), (typeobj))
0123 #define PyObject_INIT_VAR(op, typeobj, size) \
0124     PyObject_InitVar(_PyVarObject_CAST(op), (typeobj), (size))
0125 
0126 
0127 PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);
0128 PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
0129 
0130 #define PyObject_New(type, typeobj) ((type *)_PyObject_New(typeobj))
0131 
0132 // Alias to PyObject_New(). In Python 3.8, PyObject_NEW() called directly
0133 // PyObject_MALLOC() with _PyObject_SIZE().
0134 #define PyObject_NEW(type, typeobj) PyObject_New(type, (typeobj))
0135 
0136 #define PyObject_NewVar(type, typeobj, n) \
0137                 ( (type *) _PyObject_NewVar((typeobj), (n)) )
0138 
0139 // Alias to PyObject_NewVar(). In Python 3.8, PyObject_NEW_VAR() called
0140 // directly PyObject_MALLOC() with _PyObject_VAR_SIZE().
0141 #define PyObject_NEW_VAR(type, typeobj, n) PyObject_NewVar(type, (typeobj), (n))
0142 
0143 
0144 /*
0145  * Garbage Collection Support
0146  * ==========================
0147  */
0148 
0149 /* C equivalent of gc.collect(). */
0150 PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
0151 /* C API for controlling the state of the garbage collector */
0152 PyAPI_FUNC(int) PyGC_Enable(void);
0153 PyAPI_FUNC(int) PyGC_Disable(void);
0154 PyAPI_FUNC(int) PyGC_IsEnabled(void);
0155 
0156 /* Test if a type has a GC head */
0157 #define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
0158 
0159 PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
0160 #define PyObject_GC_Resize(type, op, n) \
0161                 ( (type *) _PyObject_GC_Resize(_PyVarObject_CAST(op), (n)) )
0162 
0163 
0164 
0165 PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);
0166 PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t);
0167 
0168 /* Tell the GC to track this object.
0169  *
0170  * See also private _PyObject_GC_TRACK() macro. */
0171 PyAPI_FUNC(void) PyObject_GC_Track(void *);
0172 
0173 /* Tell the GC to stop tracking this object.
0174  *
0175  * See also private _PyObject_GC_UNTRACK() macro. */
0176 PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);
0177 
0178 PyAPI_FUNC(void) PyObject_GC_Del(void *);
0179 
0180 #define PyObject_GC_New(type, typeobj) \
0181     _Py_CAST(type*, _PyObject_GC_New(typeobj))
0182 #define PyObject_GC_NewVar(type, typeobj, n) \
0183     _Py_CAST(type*, _PyObject_GC_NewVar((typeobj), (n)))
0184 
0185 PyAPI_FUNC(int) PyObject_GC_IsTracked(PyObject *);
0186 PyAPI_FUNC(int) PyObject_GC_IsFinalized(PyObject *);
0187 
0188 /* Utility macro to help write tp_traverse functions.
0189  * To use this macro, the tp_traverse function must name its arguments
0190  * "visit" and "arg".  This is intended to keep tp_traverse functions
0191  * looking as much alike as possible.
0192  */
0193 #define Py_VISIT(op)                                                    \
0194     do {                                                                \
0195         if (op) {                                                       \
0196             int vret = visit(_PyObject_CAST(op), arg);                  \
0197             if (vret)                                                   \
0198                 return vret;                                            \
0199         }                                                               \
0200     } while (0)
0201 
0202 #ifndef Py_LIMITED_API
0203 #  define Py_CPYTHON_OBJIMPL_H
0204 #  include "cpython/objimpl.h"
0205 #  undef Py_CPYTHON_OBJIMPL_H
0206 #endif
0207 
0208 #ifdef __cplusplus
0209 }
0210 #endif
0211 #endif   // !Py_OBJIMPL_H