objimpl.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #ifndef Py_CPYTHON_OBJIMPL_H
  2. # error "this header file must not be included directly"
  3. #endif
  4. static inline size_t _PyObject_SIZE(PyTypeObject *type) {
  5. return _Py_STATIC_CAST(size_t, type->tp_basicsize);
  6. }
  7. /* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
  8. vrbl-size object with nitems items, exclusive of gc overhead (if any). The
  9. value is rounded up to the closest multiple of sizeof(void *), in order to
  10. ensure that pointer fields at the end of the object are correctly aligned
  11. for the platform (this is of special importance for subclasses of, e.g.,
  12. str or int, so that pointers can be stored after the embedded data).
  13. Note that there's no memory wastage in doing this, as malloc has to
  14. return (at worst) pointer-aligned memory anyway.
  15. */
  16. #if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
  17. # error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
  18. #endif
  19. static inline size_t _PyObject_VAR_SIZE(PyTypeObject *type, Py_ssize_t nitems) {
  20. size_t size = _Py_STATIC_CAST(size_t, type->tp_basicsize);
  21. size += _Py_STATIC_CAST(size_t, nitems) * _Py_STATIC_CAST(size_t, type->tp_itemsize);
  22. return _Py_SIZE_ROUND_UP(size, SIZEOF_VOID_P);
  23. }
  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. }
  38. PyObject_Init(op, &YourTypeStruct);
  39. op->ob_field = value;
  40. ...
  41. return op;
  42. }
  43. Note that in C++, the use of the new operator usually implies that
  44. the 1st step is performed automatically for you, so in a C++ class
  45. constructor you would start directly with PyObject_Init/InitVar. */
  46. typedef struct {
  47. /* user context passed as the first argument to the 2 functions */
  48. void *ctx;
  49. /* allocate an arena of size bytes */
  50. void* (*alloc) (void *ctx, size_t size);
  51. /* free an arena */
  52. void (*free) (void *ctx, void *ptr, size_t size);
  53. } PyObjectArenaAllocator;
  54. /* Get the arena allocator. */
  55. PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator);
  56. /* Set the arena allocator. */
  57. PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
  58. /* Test if an object implements the garbage collector protocol */
  59. PyAPI_FUNC(int) PyObject_IS_GC(PyObject *obj);
  60. /* Code built with Py_BUILD_CORE must include pycore_gc.h instead which
  61. defines a different _PyGC_FINALIZED() macro. */
  62. #ifndef Py_BUILD_CORE
  63. // Kept for backward compatibility with Python 3.8
  64. # define _PyGC_FINALIZED(o) PyObject_GC_IsFinalized(o)
  65. #endif
  66. // Test if a type supports weak references
  67. PyAPI_FUNC(int) PyType_SUPPORTS_WEAKREFS(PyTypeObject *type);
  68. PyAPI_FUNC(PyObject **) PyObject_GET_WEAKREFS_LISTPTR(PyObject *op);
  69. PyAPI_FUNC(PyObject *) PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *,
  70. size_t);