Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:49

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