Coverage Report

Created: 2026-12-01 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython-install/include/python3.16/cpython/objimpl.h
Line
Count
Source
1
#ifndef Py_CPYTHON_OBJIMPL_H
2
#  error "this header file must not be included directly"
3
#endif
4
5
0
static inline size_t _PyObject_SIZE(PyTypeObject *type) {
6
0
    return _Py_STATIC_CAST(size_t, type->tp_basicsize);
7
0
}
8
9
/* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a
10
   vrbl-size object with nitems items, exclusive of gc overhead (if any).  The
11
   value is rounded up to the closest multiple of sizeof(void *), in order to
12
   ensure that pointer fields at the end of the object are correctly aligned
13
   for the platform (this is of special importance for subclasses of, e.g.,
14
   str or int, so that pointers can be stored after the embedded data).
15
16
   Note that there's no memory wastage in doing this, as malloc has to
17
   return (at worst) pointer-aligned memory anyway.
18
19
   Return SIZE_MAX on integer overflow (if the result would be greater than
20
   PY_SSIZE_T_MAX).
21
*/
22
#if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0
23
#   error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2"
24
#endif
25
26
static inline size_t
27
_PyObject_VAR_SIZE(PyTypeObject *type, Py_ssize_t nitems_signed)
28
0
{
29
0
    size_t basicsize;
30
0
    size_t size;
31
0
32
0
    assert(type->tp_itemsize >= 0);
33
0
    basicsize = _Py_STATIC_CAST(size_t, type->tp_basicsize);
34
0
35
0
    if (type->tp_itemsize != 0) {
36
0
        size_t nitems = _Py_STATIC_CAST(size_t, nitems_signed);
37
0
        size_t itemsize = _Py_STATIC_CAST(size_t, type->tp_itemsize);
38
0
        if (nitems > ((size_t)PY_SSIZE_T_MAX - basicsize) / itemsize) {
39
0
            return SIZE_MAX;
40
0
        }
41
0
        size = basicsize + nitems * itemsize;
42
0
    }
43
0
    else {
44
0
        size = basicsize;
45
0
    }
46
0
47
0
    size = _Py_SIZE_ROUND_UP(size, SIZEOF_VOID_P);
48
0
    if (size > (size_t)PY_SSIZE_T_MAX) {
49
0
        return SIZE_MAX;
50
0
    }
51
0
    return size;
52
0
}
53
54
55
/* This example code implements an object constructor with a custom
56
   allocator, where PyObject_New is inlined, and shows the important
57
   distinction between two steps (at least):
58
       1) the actual allocation of the object storage;
59
       2) the initialization of the Python specific fields
60
      in this storage with PyObject_{Init, InitVar}.
61
62
   PyObject *
63
   YourObject_New(...)
64
   {
65
       PyObject *op;
66
67
       op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct));
68
       if (op == NULL) {
69
           return PyErr_NoMemory();
70
       }
71
72
       PyObject_Init(op, &YourTypeStruct);
73
74
       op->ob_field = value;
75
       ...
76
       return op;
77
   }
78
79
   Note that in C++, the use of the new operator usually implies that
80
   the 1st step is performed automatically for you, so in a C++ class
81
   constructor you would start directly with PyObject_Init/InitVar. */
82
83
84
typedef struct {
85
    /* user context passed as the first argument to the 2 functions */
86
    void *ctx;
87
88
    /* allocate an arena of size bytes */
89
    void* (*alloc) (void *ctx, size_t size);
90
91
    /* free an arena */
92
    void (*free) (void *ctx, void *ptr, size_t size);
93
} PyObjectArenaAllocator;
94
95
/* Get the arena allocator. */
96
PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator);
97
98
/* Set the arena allocator. */
99
PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator);
100
101
102
/* Test if an object implements the garbage collector protocol */
103
PyAPI_FUNC(int) PyObject_IS_GC(PyObject *obj);
104
105
106
// Test if a type supports weak references
107
PyAPI_FUNC(int) PyType_SUPPORTS_WEAKREFS(PyTypeObject *type);
108
109
PyAPI_FUNC(PyObject **) PyObject_GET_WEAKREFS_LISTPTR(PyObject *op);
110
111
PyAPI_FUNC(PyObject *) PyUnstable_Object_GC_NewWithExtraData(PyTypeObject *,
112
                                                             size_t);
113
114
115
/* Visit all live GC-capable objects, similar to gc.get_objects(None). The
116
 * supplied callback is called on every such object with the void* arg set
117
 * to the supplied arg. Returning 0 from the callback ends iteration, returning
118
 * 1 allows iteration to continue. Returning any other value may result in
119
 * undefined behaviour.
120
 *
121
 * If new objects are (de)allocated by the callback it is undefined if they
122
 * will be visited.
123
124
 * Garbage collection is disabled during operation. Explicitly running a
125
 * collection in the callback may lead to undefined behaviour e.g. visiting the
126
 * same objects multiple times or not at all.
127
 */
128
typedef int (*gcvisitobjects_t)(PyObject*, void*);
129
PyAPI_FUNC(void) PyUnstable_GC_VisitObjects(gcvisitobjects_t callback, void* arg);