objimpl.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
  8. /* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
  9. vrbl-size object with nitems items, exclusive of gc overhead (if any). The
  10. value is rounded up to the closest multiple of sizeof(void *), in order to
  11. ensure that pointer fields at the end of the object are correctly aligned
  12. for the platform (this is of special importance for subclasses of, e.g.,
  13. str or int, so that pointers can be stored after the embedded data).
  14. Note that there's no memory wastage in doing this, as malloc has to
  15. return (at worst) pointer-aligned memory anyway.
  16. */
  17. #if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
  18. # error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
  19. #endif
  20. #define _PyObject_VAR_SIZE(typeobj, nitems) \
  21. _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \
  22. (nitems)*(typeobj)->tp_itemsize, \
  23. SIZEOF_VOID_P)
  24. /* This example code implements an object constructor with a custom
  25. allocator, where PyObject_New is inlined, and shows the important
  26. distinction between two steps (at least):
  27. 1) the actual allocation of the object storage;
  28. 2) the initialization of the Python specific fields
  29. in this storage with PyObject_{Init, InitVar}.
  30. PyObject *
  31. YourObject_New(...)
  32. {
  33. PyObject *op;
  34. op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
  35. if (op == NULL)
  36. return PyErr_NoMemory();
  37. PyObject_Init(op, &YourTypeStruct);
  38. op->ob_field = value;
  39. ...
  40. return op;
  41. }
  42. Note that in C++, the use of the new operator usually implies that
  43. the 1st step is performed automatically for you, so in a C++ class
  44. constructor you would start directly with PyObject_Init/InitVar. */
  45. /* Inline functions trading binary compatibility for speed:
  46. PyObject_INIT() is the fast version of PyObject_Init(), and
  47. PyObject_INIT_VAR() is the fast version of PyObject_InitVar().
  48. These inline functions must not be called with op=NULL. */
  49. static inline PyObject*
  50. _PyObject_INIT(PyObject *op, PyTypeObject *typeobj)
  51. {
  52. assert(op != NULL);
  53. Py_SET_TYPE(op, typeobj);
  54. if (PyType_GetFlags(typeobj) & Py_TPFLAGS_HEAPTYPE) {
  55. Py_INCREF(typeobj);
  56. }
  57. _Py_NewReference(op);
  58. return op;
  59. }
  60. #define PyObject_INIT(op, typeobj) \
  61. _PyObject_INIT(_PyObject_CAST(op), (typeobj))
  62. static inline PyVarObject*
  63. _PyObject_INIT_VAR(PyVarObject *op, PyTypeObject *typeobj, Py_ssize_t size)
  64. {
  65. assert(op != NULL);
  66. Py_SET_SIZE(op, size);
  67. PyObject_INIT((PyObject *)op, typeobj);
  68. return op;
  69. }
  70. #define PyObject_INIT_VAR(op, typeobj, size) \
  71. _PyObject_INIT_VAR(_PyVarObject_CAST(op), (typeobj), (size))
  72. /* This function returns the number of allocated memory blocks, regardless of size */
  73. PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void);
  74. /* Macros */
  75. #ifdef WITH_PYMALLOC
  76. PyAPI_FUNC(int) _PyObject_DebugMallocStats(FILE *out);
  77. #endif
  78. typedef struct {
  79. /* user context passed as the first argument to the 2 functions */
  80. void *ctx;
  81. /* allocate an arena of size bytes */
  82. void* (*alloc) (void *ctx, size_t size);
  83. /* free an arena */
  84. void (*free) (void *ctx, void *ptr, size_t size);
  85. } PyObjectArenaAllocator;
  86. /* Get the arena allocator. */
  87. PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator);
  88. /* Set the arena allocator. */
  89. PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
  90. PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void);
  91. PyAPI_FUNC(Py_ssize_t) _PyGC_CollectIfEnabled(void);
  92. /* Test if an object implements the garbage collector protocol */
  93. PyAPI_FUNC(int) PyObject_IS_GC(PyObject *obj);
  94. /* Code built with Py_BUILD_CORE must include pycore_gc.h instead which
  95. defines a different _PyGC_FINALIZED() macro. */
  96. #ifndef Py_BUILD_CORE
  97. // Kept for backward compatibility with Python 3.8
  98. # define _PyGC_FINALIZED(o) PyObject_GC_IsFinalized(o)
  99. #endif
  100. PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size);
  101. PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size);
  102. /* Test if a type supports weak references */
  103. #define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0)
  104. PyAPI_FUNC(PyObject **) PyObject_GET_WEAKREFS_LISTPTR(PyObject *op);
  105. #ifdef __cplusplus
  106. }
  107. #endif