123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- extern "C" {
- PyAPI_FUNC(void *) PyObject_Malloc(size_t);
- PyAPI_FUNC(void *) PyObject_Realloc(void *, size_t);
- PyAPI_FUNC(void) PyObject_Free(void *);
- PyAPI_FUNC(void *) _PyObject_DebugMalloc(size_t nbytes);
- PyAPI_FUNC(void *) _PyObject_DebugRealloc(void *p, size_t nbytes);
- PyAPI_FUNC(void) _PyObject_DebugFree(void *p);
- PyAPI_FUNC(void) _PyObject_DebugDumpAddress(const void *p);
- PyAPI_FUNC(void) _PyObject_DebugCheckAddress(const void *p);
- PyAPI_FUNC(void) _PyObject_DebugMallocStats(void);
- PyAPI_FUNC(void *) _PyObject_DebugMallocApi(char api, size_t nbytes);
- PyAPI_FUNC(void *) _PyObject_DebugReallocApi(char api, void *p, size_t nbytes);
- PyAPI_FUNC(void) _PyObject_DebugFreeApi(char api, void *p);
- PyAPI_FUNC(void) _PyObject_DebugCheckAddressApi(char api, const void *p);
- PyAPI_FUNC(void *) _PyMem_DebugMalloc(size_t nbytes);
- PyAPI_FUNC(void *) _PyMem_DebugRealloc(void *p, size_t nbytes);
- PyAPI_FUNC(void) _PyMem_DebugFree(void *p);
- PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
- PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *,
- PyTypeObject *, Py_ssize_t);
- PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *);
- PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
- ( (type *) _PyObject_New(typeobj) )
- ( (type *) _PyObject_NewVar((typeobj), (n)) )
- ( Py_TYPE(op) = (typeobj), _Py_NewReference((PyObject *)(op)), (op) )
- ( Py_SIZE(op) = (size), PyObject_INIT((op), (typeobj)) )
- (size_t) \
- ( ( (typeobj)->tp_basicsize + \
- (nitems)*(typeobj)->tp_itemsize + \
- (SIZEOF_VOID_P - 1) \
- ) & ~(SIZEOF_VOID_P - 1) \
- )
- #define PyObject_NEW(type, typeobj) \
- ( (type *) PyObject_Init( \
- (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) )
- #define PyObject_NEW_VAR(type, typeobj, n) \
- ( (type *) PyObject_InitVar( \
- (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\
- (typeobj), (n)) )
- /* This example code implements an object constructor with a custom
- allocator, where PyObject_New is inlined, and shows the important
- distinction between two steps (at least):
- 1) the actual allocation of the object storage;
- 2) the initialization of the Python specific fields
- in this storage with PyObject_{Init, InitVar}.
- PyObject *
- YourObject_New(...)
- {
- PyObject *op;
- op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
- if (op == NULL)
- return PyErr_NoMemory();
- PyObject_Init(op, &YourTypeStruct);
- op->ob_field = value;
- ...
- return op;
- }
- Note that in C++, the use of the new operator usually implies that
- the 1st step is performed automatically for you, so in a C++ class
- constructor you would start directly with PyObject_Init/InitVar
- */
- PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
- (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o)))
- PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t);
- #define PyObject_GC_Resize(type, op, n) \
- ( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) )
- /* for source compatibility with 2.2 */
- #define _PyObject_GC_Del PyObject_GC_Del
- /* GC information is stored BEFORE the object structure. */
- typedef union _gc_head {
- struct {
- union _gc_head *gc_next;
- union _gc_head *gc_prev;
- Py_ssize_t gc_refs;
- } gc;
- long double dummy; /* force worst-case alignment */
- } PyGC_Head;
- extern PyGC_Head *_PyGC_generation0;
- #define _Py_AS_GC(o) ((PyGC_Head *)(o)-1)
- #define _PyGC_REFS_UNTRACKED (-2)
- #define _PyGC_REFS_REACHABLE (-3)
- #define _PyGC_REFS_TENTATIVELY_UNREACHABLE (-4)
- /* Tell the GC to track this object. NB: While the object is tracked the
- * collector it must be safe to call the ob_traverse method. */
- #define _PyObject_GC_TRACK(o) do { \
- PyGC_Head *g = _Py_AS_GC(o); \
- if (g->gc.gc_refs != _PyGC_REFS_UNTRACKED) \
- Py_FatalError("GC object already tracked"); \
- g->gc.gc_refs = _PyGC_REFS_REACHABLE; \
- g->gc.gc_next = _PyGC_generation0; \
- g->gc.gc_prev = _PyGC_generation0->gc.gc_prev; \
- g->gc.gc_prev->gc.gc_next = g; \
- _PyGC_generation0->gc.gc_prev = g; \
- } while (0);
- PyGC_Head *g = _Py_AS_GC(o); \
- assert(g->gc.gc_refs != _PyGC_REFS_UNTRACKED); \
- g->gc.gc_refs = _PyGC_REFS_UNTRACKED; \
- g->gc.gc_prev->gc.gc_next = g->gc.gc_next; \
- g->gc.gc_next->gc.gc_prev = g->gc.gc_prev; \
- g->gc.gc_next = NULL; \
- } while (0);
- ((_Py_AS_GC(o))->gc.gc_refs != _PyGC_REFS_UNTRACKED)
- /* True if the object may be tracked by the GC in the future, or already is.
- This can be useful to implement some optimizations. */
- #define _PyObject_GC_MAY_BE_TRACKED(obj) \
- (PyObject_IS_GC(obj) && \
- (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj)))
- PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t);
- PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *);
- PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t);
- PyAPI_FUNC(void) PyObject_GC_Track(void *);
- PyAPI_FUNC(void) PyObject_GC_UnTrack(void *);
- PyAPI_FUNC(void) PyObject_GC_Del(void *);
- #define PyObject_GC_New(type, typeobj) \
- ( (type *) _PyObject_GC_New(typeobj) )
- #define PyObject_GC_NewVar(type, typeobj, n) \
- ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
- /* 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.
- */
- #define Py_VISIT(op) \
- do { \
- if (op) { \
- int vret = visit((PyObject *)(op), arg); \
- if (vret) \
- return vret; \
- } \
- } while (0)
- /* This is here for the sake of backwards compatibility. Extensions that
- * use the old GC API will still compile but the objects will not be
- * tracked by the GC. */
- #define PyGC_HEAD_SIZE 0
- #define PyObject_GC_Init(op)
- #define PyObject_GC_Fini(op)
- #define PyObject_AS_GC(op) (op)
- #define PyObject_FROM_GC(op) (op)
- /* Test if a type supports weak references */
- #define PyType_SUPPORTS_WEAKREFS(t) \
- (PyType_HasFeature((t), Py_TPFLAGS_HAVE_WEAKREFS) \
- && ((t)->tp_weaklistoffset > 0))
- #define PyObject_GET_WEAKREFS_LISTPTR(o) \
- ((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset))
- #ifdef __cplusplus
- }
- #endif
- #endif /* !Py_OBJIMPL_H */
|