(root)/
Python-3.11.7/
Include/
cpython/
objimpl.h
       1  #ifndef Py_CPYTHON_OBJIMPL_H
       2  #  error "this header file must not be included directly"
       3  #endif
       4  
       5  #define _PyObject_SIZE(typeobj) ( (typeobj)->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  
      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  
      21  #define _PyObject_VAR_SIZE(typeobj, nitems)     \
      22      _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \
      23          (nitems)*(typeobj)->tp_itemsize,        \
      24          SIZEOF_VOID_P)
      25  
      26  
      27  /* This example code implements an object constructor with a custom
      28     allocator, where PyObject_New is inlined, and shows the important
      29     distinction between two steps (at least):
      30         1) the actual allocation of the object storage;
      31         2) the initialization of the Python specific fields
      32        in this storage with PyObject_{Init, InitVar}.
      33  
      34     PyObject *
      35     YourObject_New(...)
      36     {
      37         PyObject *op;
      38  
      39         op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
      40         if (op == NULL) {
      41             return PyErr_NoMemory();
      42         }
      43  
      44         PyObject_Init(op, &YourTypeStruct);
      45  
      46         op->ob_field = value;
      47         ...
      48         return op;
      49     }
      50  
      51     Note that in C++, the use of the new operator usually implies that
      52     the 1st step is performed automatically for you, so in a C++ class
      53     constructor you would start directly with PyObject_Init/InitVar. */
      54  
      55  
      56  typedef struct {
      57      /* user context passed as the first argument to the 2 functions */
      58      void *ctx;
      59  
      60      /* allocate an arena of size bytes */
      61      void* (*alloc) (void *ctx, size_t size);
      62  
      63      /* free an arena */
      64      void (*free) (void *ctx, void *ptr, size_t size);
      65  } PyObjectArenaAllocator;
      66  
      67  /* Get the arena allocator. */
      68  PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator);
      69  
      70  /* Set the arena allocator. */
      71  PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
      72  
      73  
      74  /* Test if an object implements the garbage collector protocol */
      75  PyAPI_FUNC(int) PyObject_IS_GC(PyObject *obj);
      76  
      77  
      78  /* Code built with Py_BUILD_CORE must include pycore_gc.h instead which
      79     defines a different _PyGC_FINALIZED() macro. */
      80  #ifndef Py_BUILD_CORE
      81     // Kept for backward compatibility with Python 3.8
      82  #  define _PyGC_FINALIZED(o) PyObject_GC_IsFinalized(o)
      83  #endif
      84  
      85  
      86  // Test if a type supports weak references
      87  PyAPI_FUNC(int) PyType_SUPPORTS_WEAKREFS(PyTypeObject *type);
      88  
      89  PyAPI_FUNC(PyObject **) PyObject_GET_WEAKREFS_LISTPTR(PyObject *op);