objimpl.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef Py_CPYTHON_OBJIMPL_H
  2. # error "this header file must not be included directly"
  3. #endif
  4. #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize )
  5. /* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
  6. vrbl-size object with nitems items, exclusive of gc overhead (if any). The
  7. value is rounded up to the closest multiple of sizeof(void *), in order to
  8. ensure that pointer fields at the end of the object are correctly aligned
  9. for the platform (this is of special importance for subclasses of, e.g.,
  10. str or int, so that pointers can be stored after the embedded data).
  11. Note that there's no memory wastage in doing this, as malloc has to
  12. return (at worst) pointer-aligned memory anyway.
  13. */
  14. #if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
  15. # error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
  16. #endif
  17. #define _PyObject_VAR_SIZE(typeobj, nitems) \
  18. _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \
  19. (nitems)*(typeobj)->tp_itemsize, \
  20. SIZEOF_VOID_P)
  21. /* This example code implements an object constructor with a custom
  22. allocator, where PyObject_New is inlined, and shows the important
  23. distinction between two steps (at least):
  24. 1) the actual allocation of the object storage;
  25. 2) the initialization of the Python specific fields
  26. in this storage with PyObject_{Init, InitVar}.
  27. PyObject *
  28. YourObject_New(...)
  29. {
  30. PyObject *op;
  31. op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
  32. if (op == NULL) {
  33. return PyErr_NoMemory();
  34. }
  35. PyObject_Init(op, &YourTypeStruct);
  36. op->ob_field = value;
  37. ...
  38. return op;
  39. }
  40. Note that in C++, the use of the new operator usually implies that
  41. the 1st step is performed automatically for you, so in a C++ class
  42. constructor you would start directly with PyObject_Init/InitVar. */
  43. /* This function returns the number of allocated memory blocks, regardless of size */
  44. PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void);
  45. /* Macros */
  46. #ifdef WITH_PYMALLOC
  47. PyAPI_FUNC(int) _PyObject_DebugMallocStats(FILE *out);
  48. #endif
  49. typedef struct {
  50. /* user context passed as the first argument to the 2 functions */
  51. void *ctx;
  52. /* allocate an arena of size bytes */
  53. void* (*alloc) (void *ctx, size_t size);
  54. /* free an arena */
  55. void (*free) (void *ctx, void *ptr, size_t size);
  56. } PyObjectArenaAllocator;
  57. /* Get the arena allocator. */
  58. PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator);
  59. /* Set the arena allocator. */
  60. PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
  61. /* Test if an object implements the garbage collector protocol */
  62. PyAPI_FUNC(int) PyObject_IS_GC(PyObject *obj);
  63. /* Code built with Py_BUILD_CORE must include pycore_gc.h instead which
  64. defines a different _PyGC_FINALIZED() macro. */
  65. #ifndef Py_BUILD_CORE
  66. // Kept for backward compatibility with Python 3.8
  67. # define _PyGC_FINALIZED(o) PyObject_GC_IsFinalized(o)
  68. #endif
  69. PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t size);
  70. PyAPI_FUNC(PyObject *) _PyObject_GC_Calloc(size_t size);
  71. /* Test if a type supports weak references */
  72. #define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0)
  73. PyAPI_FUNC(PyObject **) PyObject_GET_WEAKREFS_LISTPTR(PyObject *op);