objimpl.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #ifndef Py_CPYTHON_OBJIMPL_H
  2. # error "this header file must not be included directly"
  3. #endif
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. /* This function returns the number of allocated memory blocks, regardless of size */
  8. PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void);
  9. /* Macros */
  10. #ifdef WITH_PYMALLOC
  11. PyAPI_FUNC(int) _PyObject_DebugMallocStats(FILE *out);
  12. #endif
  13. typedef struct {
  14. /* user context passed as the first argument to the 2 functions */
  15. void *ctx;
  16. /* allocate an arena of size bytes */
  17. void* (*alloc) (void *ctx, size_t size);
  18. /* free an arena */
  19. void (*free) (void *ctx, void *ptr, size_t size);
  20. } PyObjectArenaAllocator;
  21. /* Get the arena allocator. */
  22. PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator);
  23. /* Set the arena allocator. */
  24. PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
  25. PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void);
  26. PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void);
  27. /* Test if an object has a GC head */
  28. #define PyObject_IS_GC(o) \
  29. (PyType_IS_GC(Py_TYPE(o)) \
  30. && (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o)))
  31. /* GC information is stored BEFORE the object structure. */
  32. typedef struct {
  33. // Pointer to next object in the list.
  34. // 0 means the object is not tracked
  35. uintptr_t _gc_next;
  36. // Pointer to previous object in the list.
  37. // Lowest two bits are used for flags documented later.
  38. uintptr_t _gc_prev;
  39. } PyGC_Head;
  40. #define _Py_AS_GC(o) ((PyGC_Head *)(o)-1)
  41. /* True if the object is currently tracked by the GC. */
  42. #define _PyObject_GC_IS_TRACKED(o) (_Py_AS_GC(o)->_gc_next != 0)
  43. /* True if the object may be tracked by the GC in the future, or already is.
  44. This can be useful to implement some optimizations. */
  45. #define _PyObject_GC_MAY_BE_TRACKED(obj) \
  46. (PyObject_IS_GC(obj) && \
  47. (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj)))
  48. /* Bit flags for _gc_prev */
  49. /* Bit 0 is set when tp_finalize is called */
  50. #define _PyGC_PREV_MASK_FINALIZED (1)
  51. /* Bit 1 is set when the object is in generation which is GCed currently. */
  52. #define _PyGC_PREV_MASK_COLLECTING (2)
  53. /* The (N-2) most significant bits contain the real address. */
  54. #define _PyGC_PREV_SHIFT (2)
  55. #define _PyGC_PREV_MASK (((uintptr_t) -1) << _PyGC_PREV_SHIFT)
  56. // Lowest bit of _gc_next is used for flags only in GC.
  57. // But it is always 0 for normal code.
  58. #define _PyGCHead_NEXT(g) ((PyGC_Head*)(g)->_gc_next)
  59. #define _PyGCHead_SET_NEXT(g, p) ((g)->_gc_next = (uintptr_t)(p))
  60. // Lowest two bits of _gc_prev is used for _PyGC_PREV_MASK_* flags.
  61. #define _PyGCHead_PREV(g) ((PyGC_Head*)((g)->_gc_prev & _PyGC_PREV_MASK))
  62. #define _PyGCHead_SET_PREV(g, p) do { \
  63. assert(((uintptr_t)p & ~_PyGC_PREV_MASK) == 0); \
  64. (g)->_gc_prev = ((g)->_gc_prev & ~_PyGC_PREV_MASK) \
  65. | ((uintptr_t)(p)); \
  66. } while (0)
  67. #define _PyGCHead_FINALIZED(g) \
  68. (((g)->_gc_prev & _PyGC_PREV_MASK_FINALIZED) != 0)
  69. #define _PyGCHead_SET_FINALIZED(g) \
  70. ((g)->_gc_prev |= _PyGC_PREV_MASK_FINALIZED)
  71. #define _PyGC_FINALIZED(o) \
  72. _PyGCHead_FINALIZED(_Py_AS_GC(o))
  73. #define _PyGC_SET_FINALIZED(o) \
  74. _PyGCHead_SET_FINALIZED(_Py_AS_GC(o))
  75. PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size);
  76. PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size);
  77. /* Test if a type supports weak references */
  78. #define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0)
  79. #define PyObject_GET_WEAKREFS_LISTPTR(o) \
  80. ((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset))
  81. #ifdef __cplusplus
  82. }
  83. #endif