2023-09-01 16:03:20 -03:00
|
|
|
// The PyObject_ memory family: high-level object memory interfaces.
|
|
|
|
// See pymem.h for the low-level PyMem_ family.
|
1991-02-19 08:39:46 -04:00
|
|
|
|
2000-07-08 21:55:06 -03:00
|
|
|
#ifndef Py_OBJIMPL_H
|
|
|
|
#define Py_OBJIMPL_H
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2002-04-28 01:11:46 -03:00
|
|
|
/* BEWARE:
|
|
|
|
|
|
|
|
Each interface exports both functions and macros. Extension modules should
|
|
|
|
use the functions, to ensure binary compatibility across Python versions.
|
|
|
|
Because the Python implementation is free to change internal details, and
|
|
|
|
the macros may (or may not) expose details for speed, if you do use the
|
|
|
|
macros you must recompile your extensions with each Python release.
|
|
|
|
|
|
|
|
Never mix calls to PyObject_ memory functions with calls to the platform
|
|
|
|
malloc/realloc/ calloc/free, or with calls to PyMem_.
|
|
|
|
*/
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/*
|
2000-05-03 20:44:39 -03:00
|
|
|
Functions and macros for modules that implement new object types.
|
2002-04-28 01:11:46 -03:00
|
|
|
|
|
|
|
- PyObject_New(type, typeobj) allocates memory for a new object of the given
|
|
|
|
type, and initializes part of it. 'type' must be the C structure type used
|
|
|
|
to represent the object, and 'typeobj' the address of the corresponding
|
|
|
|
type object. Reference count and type pointer are filled in; the rest of
|
|
|
|
the bytes of the object are *undefined*! The resulting expression type is
|
|
|
|
'type *'. The size of the object is determined by the tp_basicsize field
|
|
|
|
of the type object.
|
|
|
|
|
|
|
|
- PyObject_NewVar(type, typeobj, n) is similar but allocates a variable-size
|
|
|
|
object with room for n items. In addition to the refcount and type pointer
|
|
|
|
fields, this also fills in the ob_size field.
|
|
|
|
|
2020-12-01 05:37:39 -04:00
|
|
|
- PyObject_Free(op) releases the memory allocated for an object. It does not
|
2023-12-07 09:24:11 -04:00
|
|
|
run a destructor -- it only frees the memory.
|
2002-04-28 01:11:46 -03:00
|
|
|
|
|
|
|
- PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) don't
|
|
|
|
allocate memory. Instead of a 'type' parameter, they take a pointer to a
|
|
|
|
new object (allocated by an arbitrary allocator), and initialize its object
|
|
|
|
header fields.
|
|
|
|
|
|
|
|
Note that objects created with PyObject_{New, NewVar} are allocated using the
|
|
|
|
specialized Python allocator (implemented in obmalloc.c), if WITH_PYMALLOC is
|
2021-04-29 05:47:47 -03:00
|
|
|
enabled. In addition, a special debugging allocator is used if Py_DEBUG
|
|
|
|
macro is also defined.
|
2002-04-28 01:11:46 -03:00
|
|
|
|
|
|
|
In case a specific form of memory management is needed (for example, if you
|
|
|
|
must use the platform malloc heap(s), or shared memory, or C++ local storage or
|
|
|
|
operator new), you must first allocate the object with your custom allocator,
|
|
|
|
then pass its pointer to PyObject_{Init, InitVar} for filling in its Python-
|
|
|
|
specific fields: reference count, type pointer, possibly others. You should
|
2018-02-06 19:07:30 -04:00
|
|
|
be aware that Python has no control over these objects because they don't
|
2002-04-28 01:11:46 -03:00
|
|
|
cooperate with the Python memory manager. Such objects may not be eligible
|
|
|
|
for automatic garbage collection and you have to make sure that they are
|
|
|
|
released accordingly whenever their destructor gets called (cf. the specific
|
2000-05-03 20:44:39 -03:00
|
|
|
form of memory management you're using).
|
|
|
|
|
2002-04-28 01:11:46 -03:00
|
|
|
Unless you have specific memory management requirements, use
|
|
|
|
PyObject_{New, NewVar, Del}.
|
|
|
|
*/
|
2000-05-03 20:44:39 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Raw object memory interface
|
|
|
|
* ===========================
|
|
|
|
*/
|
|
|
|
|
2002-04-12 02:21:34 -03:00
|
|
|
/* Functions to call the same malloc/realloc/free as used by Python's
|
|
|
|
object allocator. If WITH_PYMALLOC is enabled, these may differ from
|
|
|
|
the platform malloc/realloc/free. The Python object allocator is
|
|
|
|
designed for fast, cache-conscious allocation of many "small" objects,
|
2002-04-28 01:11:46 -03:00
|
|
|
and with low hidden memory overhead.
|
|
|
|
|
|
|
|
PyObject_Malloc(0) returns a unique non-NULL pointer if possible.
|
|
|
|
|
|
|
|
PyObject_Realloc(NULL, n) acts like PyObject_Malloc(n).
|
|
|
|
PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory
|
|
|
|
at p.
|
|
|
|
|
|
|
|
Returned pointers must be checked for NULL explicitly; no action is
|
|
|
|
performed on failure other than to return NULL (no warning it printed, no
|
|
|
|
exception is set, etc).
|
|
|
|
|
|
|
|
For allocating objects, use PyObject_{New, NewVar} instead whenever
|
|
|
|
possible. The PyObject_{Malloc, Realloc, Free} family is exposed
|
|
|
|
so that you can exploit Python's small-block allocator for non-object
|
|
|
|
uses. If you must use these routines to allocate object memory, make sure
|
|
|
|
the object gets initialized via PyObject_{Init, InitVar} after obtaining
|
|
|
|
the raw memory.
|
|
|
|
*/
|
2013-07-06 21:05:46 -03:00
|
|
|
PyAPI_FUNC(void *) PyObject_Malloc(size_t size);
|
2016-12-27 08:57:39 -04:00
|
|
|
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
|
2014-05-02 17:31:14 -03:00
|
|
|
PyAPI_FUNC(void *) PyObject_Calloc(size_t nelem, size_t elsize);
|
2016-12-27 08:57:39 -04:00
|
|
|
#endif
|
2013-07-06 21:05:46 -03:00
|
|
|
PyAPI_FUNC(void *) PyObject_Realloc(void *ptr, size_t new_size);
|
|
|
|
PyAPI_FUNC(void) PyObject_Free(void *ptr);
|
2000-05-03 20:44:39 -03:00
|
|
|
|
2013-07-06 21:05:46 -03:00
|
|
|
|
2020-12-01 04:56:42 -04:00
|
|
|
// Deprecated aliases only kept for backward compatibility.
|
2020-12-01 05:37:39 -04:00
|
|
|
// PyObject_Del and PyObject_DEL are defined with no parameter to be able to
|
|
|
|
// use them as function pointers (ex: tp_free = PyObject_Del).
|
2010-05-09 12:52:27 -03:00
|
|
|
#define PyObject_MALLOC PyObject_Malloc
|
|
|
|
#define PyObject_REALLOC PyObject_Realloc
|
|
|
|
#define PyObject_FREE PyObject_Free
|
|
|
|
#define PyObject_Del PyObject_Free
|
2013-07-06 21:05:46 -03:00
|
|
|
#define PyObject_DEL PyObject_Free
|
|
|
|
|
2002-04-11 23:38:45 -03:00
|
|
|
|
2000-05-03 20:44:39 -03:00
|
|
|
/*
|
|
|
|
* Generic object allocator interface
|
|
|
|
* ==================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Functions */
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
|
|
|
|
PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,
|
2020-06-15 20:28:07 -03:00
|
|
|
PyTypeObject *, Py_ssize_t);
|
|
|
|
|
|
|
|
#define PyObject_INIT(op, typeobj) \
|
|
|
|
PyObject_Init(_PyObject_CAST(op), (typeobj))
|
|
|
|
#define PyObject_INIT_VAR(op, typeobj, size) \
|
|
|
|
PyObject_InitVar(_PyVarObject_CAST(op), (typeobj), (size))
|
|
|
|
|
|
|
|
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);
|
2006-02-15 13:27:45 -04:00
|
|
|
PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
|
2000-05-03 20:44:39 -03:00
|
|
|
|
2020-04-07 19:38:15 -03:00
|
|
|
#define PyObject_New(type, typeobj) ((type *)_PyObject_New(typeobj))
|
|
|
|
|
|
|
|
// Alias to PyObject_New(). In Python 3.8, PyObject_NEW() called directly
|
|
|
|
// PyObject_MALLOC() with _PyObject_SIZE().
|
2022-06-26 07:40:17 -03:00
|
|
|
#define PyObject_NEW(type, typeobj) PyObject_New(type, (typeobj))
|
2020-04-07 19:38:15 -03:00
|
|
|
|
2000-05-03 20:44:39 -03:00
|
|
|
#define PyObject_NewVar(type, typeobj, n) \
|
2010-05-09 12:52:27 -03:00
|
|
|
( (type *) _PyObject_NewVar((typeobj), (n)) )
|
2000-05-03 20:44:39 -03:00
|
|
|
|
2020-12-01 04:56:42 -04:00
|
|
|
// Alias to PyObject_NewVar(). In Python 3.8, PyObject_NEW_VAR() called
|
|
|
|
// directly PyObject_MALLOC() with _PyObject_VAR_SIZE().
|
2022-06-26 07:40:17 -03:00
|
|
|
#define PyObject_NEW_VAR(type, typeobj, n) PyObject_NewVar(type, (typeobj), (n))
|
2001-10-06 18:27:34 -03:00
|
|
|
|
2020-02-05 08:12:19 -04:00
|
|
|
|
2000-06-30 02:02:53 -03:00
|
|
|
/*
|
|
|
|
* Garbage Collection Support
|
|
|
|
* ==========================
|
|
|
|
*/
|
2000-06-23 16:37:02 -03:00
|
|
|
|
2021-04-28 13:12:16 -03:00
|
|
|
/* C equivalent of gc.collect(). */
|
2006-03-04 16:01:53 -04:00
|
|
|
PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
|
2021-04-28 13:12:16 -03:00
|
|
|
/* C API for controlling the state of the garbage collector */
|
|
|
|
PyAPI_FUNC(int) PyGC_Enable(void);
|
|
|
|
PyAPI_FUNC(int) PyGC_Disable(void);
|
|
|
|
PyAPI_FUNC(int) PyGC_IsEnabled(void);
|
2003-04-17 14:29:22 -03:00
|
|
|
|
2023-03-13 22:35:54 -03:00
|
|
|
|
|
|
|
#if !defined(Py_LIMITED_API)
|
|
|
|
/* Visit all live GC-capable objects, similar to gc.get_objects(None). The
|
|
|
|
* supplied callback is called on every such object with the void* arg set
|
|
|
|
* to the supplied arg. Returning 0 from the callback ends iteration, returning
|
|
|
|
* 1 allows iteration to continue. Returning any other value may result in
|
|
|
|
* undefined behaviour.
|
|
|
|
*
|
|
|
|
* If new objects are (de)allocated by the callback it is undefined if they
|
|
|
|
* will be visited.
|
|
|
|
|
|
|
|
* Garbage collection is disabled during operation. Explicitly running a
|
|
|
|
* collection in the callback may lead to undefined behaviour e.g. visiting the
|
|
|
|
* same objects multiple times or not at all.
|
|
|
|
*/
|
|
|
|
typedef int (*gcvisitobjects_t)(PyObject*, void*);
|
|
|
|
PyAPI_FUNC(void) PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void* arg);
|
|
|
|
#endif
|
|
|
|
|
2001-08-29 20:49:28 -03:00
|
|
|
/* Test if a type has a GC head */
|
|
|
|
#define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC)
|
2000-06-30 02:02:53 -03:00
|
|
|
|
2006-02-16 10:56:14 -04:00
|
|
|
PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
|
2001-08-29 20:49:28 -03:00
|
|
|
#define PyObject_GC_Resize(type, op, n) \
|
2018-11-21 21:57:29 -04:00
|
|
|
( (type *) _PyObject_GC_Resize(_PyVarObject_CAST(op), (n)) )
|
2000-06-30 02:02:53 -03:00
|
|
|
|
2018-11-13 07:52:18 -04:00
|
|
|
|
2018-07-10 05:19:53 -03:00
|
|
|
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);
|
2006-02-15 13:27:45 -04:00
|
|
|
PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t);
|
2018-11-13 07:52:18 -04:00
|
|
|
|
|
|
|
/* Tell the GC to track this object.
|
|
|
|
*
|
|
|
|
* See also private _PyObject_GC_TRACK() macro. */
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(void) PyObject_GC_Track(void *);
|
2018-11-13 07:52:18 -04:00
|
|
|
|
|
|
|
/* Tell the GC to stop tracking this object.
|
|
|
|
*
|
|
|
|
* See also private _PyObject_GC_UNTRACK() macro. */
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);
|
2018-11-13 07:52:18 -04:00
|
|
|
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(void) PyObject_GC_Del(void *);
|
2001-08-29 20:49:28 -03:00
|
|
|
|
|
|
|
#define PyObject_GC_New(type, typeobj) \
|
2022-05-03 11:37:06 -03:00
|
|
|
_Py_CAST(type*, _PyObject_GC_New(typeobj))
|
2001-08-29 20:49:28 -03:00
|
|
|
#define PyObject_GC_NewVar(type, typeobj, n) \
|
2022-05-03 11:37:06 -03:00
|
|
|
_Py_CAST(type*, _PyObject_GC_NewVar((typeobj), (n)))
|
2002-04-11 23:38:45 -03:00
|
|
|
|
2020-04-10 21:21:54 -03:00
|
|
|
PyAPI_FUNC(int) PyObject_GC_IsTracked(PyObject *);
|
|
|
|
PyAPI_FUNC(int) PyObject_GC_IsFinalized(PyObject *);
|
2001-08-29 20:49:28 -03:00
|
|
|
|
2004-07-15 01:05:59 -03:00
|
|
|
/* Utility macro to help write tp_traverse functions.
|
|
|
|
* To use this macro, the tp_traverse function must name its arguments
|
|
|
|
* "visit" and "arg". This is intended to keep tp_traverse functions
|
|
|
|
* looking as much alike as possible.
|
|
|
|
*/
|
2010-05-09 12:52:27 -03:00
|
|
|
#define Py_VISIT(op) \
|
|
|
|
do { \
|
|
|
|
if (op) { \
|
2018-11-21 21:57:29 -04:00
|
|
|
int vret = visit(_PyObject_CAST(op), arg); \
|
2010-05-09 12:52:27 -03:00
|
|
|
if (vret) \
|
|
|
|
return vret; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2004-07-14 16:08:17 -03:00
|
|
|
|
2018-06-09 15:32:25 -03:00
|
|
|
#ifndef Py_LIMITED_API
|
2018-11-23 12:00:00 -04:00
|
|
|
# define Py_CPYTHON_OBJIMPL_H
|
2021-10-14 20:50:04 -03:00
|
|
|
# include "cpython/objimpl.h"
|
2018-11-23 12:00:00 -04:00
|
|
|
# undef Py_CPYTHON_OBJIMPL_H
|
2018-06-09 15:32:25 -03:00
|
|
|
#endif
|
2001-02-01 01:27:45 -04:00
|
|
|
|
1993-07-28 06:05:47 -03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
2023-09-01 16:03:20 -03:00
|
|
|
#endif // !Py_OBJIMPL_H
|