123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- extern "C" {
- PyAPI_FUNC(void *) PyObject_Malloc(size_t size);
- PyAPI_FUNC(void *) PyObject_Calloc(size_t nelem, size_t elsize);
- PyAPI_FUNC(void *) PyObject_Realloc(void *ptr, size_t new_size);
- PyAPI_FUNC(void) PyObject_Free(void *ptr);
- PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void);
- PyAPI_FUNC(void) _PyObject_DebugMallocStats(FILE *out);
- 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)) )
- _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \
- (nitems)*(typeobj)->tp_itemsize, \
- SIZEOF_VOID_P)
- #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
- */
- typedef struct {
-
- void *ctx;
-
- void* (*alloc) (void *ctx, size_t size);
-
- void (*free) (void *ctx, void *ptr, size_t size);
- } PyObjectArenaAllocator;
- PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator);
- PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
- PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
- PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(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)) )
- /* GC information is stored BEFORE the object structure. */
- #ifndef Py_LIMITED_API
- typedef union _gc_head {
- struct {
- union _gc_head *gc_next;
- union _gc_head *gc_prev;
- Py_ssize_t gc_refs;
- } gc;
- double dummy; /* force worst-case alignment */
- } PyGC_Head;
- extern PyGC_Head *_PyGC_generation0;
- #define _Py_AS_GC(o) ((PyGC_Head *)(o)-1)
- /* Bit 0 is set when tp_finalize is called */
- #define _PyGC_REFS_MASK_FINALIZED (1 << 0)
- /* The (N-1) most significant bits contain the gc state / refcount */
- #define _PyGC_REFS_SHIFT (1)
- #define _PyGC_REFS_MASK (((size_t) -1) << _PyGC_REFS_SHIFT)
- #define _PyGCHead_REFS(g) ((g)->gc.gc_refs >> _PyGC_REFS_SHIFT)
- #define _PyGCHead_SET_REFS(g, v) do { \
- (g)->gc.gc_refs = ((g)->gc.gc_refs & ~_PyGC_REFS_MASK) \
- | (((size_t)(v)) << _PyGC_REFS_SHIFT); \
- } while (0)
- #define _PyGCHead_DECREF(g) ((g)->gc.gc_refs -= 1 << _PyGC_REFS_SHIFT)
- #define _PyGCHead_FINALIZED(g) (((g)->gc.gc_refs & _PyGC_REFS_MASK_FINALIZED) != 0)
- #define _PyGCHead_SET_FINALIZED(g, v) do { \
- (g)->gc.gc_refs = ((g)->gc.gc_refs & ~_PyGC_REFS_MASK_FINALIZED) \
- | (v != 0); \
- } while (0)
- #define _PyGC_FINALIZED(o) _PyGCHead_FINALIZED(_Py_AS_GC(o))
- #define _PyGC_SET_FINALIZED(o, v) _PyGCHead_SET_FINALIZED(_Py_AS_GC(o), v)
- #define _PyGC_REFS(o) _PyGCHead_REFS(_Py_AS_GC(o))
- #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 (_PyGCHead_REFS(g) != _PyGC_REFS_UNTRACKED) \
- Py_FatalError("GC object already tracked"); \
- _PyGCHead_SET_REFS(g, _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(_PyGCHead_REFS(g) != _PyGC_REFS_UNTRACKED); \
- _PyGCHead_SET_REFS(g, _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);
- (_PyGC_REFS(o) != _PyGC_REFS_UNTRACKED)
- (PyObject_IS_GC(obj) && \
- (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj)))
- PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size);
- PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size);
- 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 *);
- ( (type *) _PyObject_GC_New(typeobj) )
- ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
- do { \
- if (op) { \
- int vret = visit((PyObject *)(op), arg); \
- if (vret) \
- return vret; \
- } \
- } while (0)
- ((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset))
- #ifdef __cplusplus
- }
- #endif
- #endif /* !Py_OBJIMPL_H */
|