Debian Codegraph @

Include/internal/pycore_list.h

raw
1 #ifndef Py_INTERNAL_LIST_H
2 #define Py_INTERNAL_LIST_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6
7 #ifndef Py_BUILD_CORE
8 # error "this header requires Py_BUILD_CORE define"
9 #endif
10
11 #ifdef Py_GIL_DISABLED
12 #include "pycore_stackref.h"
13 #endif
14
15 PyAPI_FUNC(PyObject*) _PyList_Extend(PyListObject *, PyObject *);
16 PyAPI_FUNC(PyObject) *_PyList_SliceSubscript(PyObject*, PyObject*);
17 extern void _PyList_DebugMallocStats(FILE *out);
18 // _PyList_GetItemRef should be used only when the object is known as a list
19 // because it doesn't raise TypeError when the object is not a list, whereas PyList_GetItemRef does.
20 extern PyObject* _PyList_GetItemRef(PyListObject *, Py_ssize_t i);
21
22
23 #ifdef Py_GIL_DISABLED
24 // Returns -1 in case of races with other threads.
25 extern int _PyList_GetItemRefNoLock(PyListObject *, Py_ssize_t, _PyStackRef *);
26 #endif
27
28 #define _PyList_ITEMS(op) _Py_RVALUE(_PyList_CAST(op)->ob_item)
29
30 PyAPI_FUNC(int)
31 _PyList_AppendTakeRefListResize(PyListObject *self, PyObject *newitem);
32
33 // In free-threaded build: self should be locked by the caller, if it should be thread-safe.
34 static inline int
35 _PyList_AppendTakeRef(PyListObject *self, PyObject *newitem)
36 {
37 assert(self != NULL && newitem != NULL);
38 assert(PyList_Check(self));
39 Py_ssize_t len = Py_SIZE(self);
40 Py_ssize_t allocated = self->allocated;
41 assert((size_t)len + 1 < PY_SSIZE_T_MAX);
42 if (allocated > len) {
43 #ifdef Py_GIL_DISABLED
44 _Py_atomic_store_ptr_release(&self->ob_item[len], newitem);
45 #else
46 PyList_SET_ITEM(self, len, newitem);
47 #endif
48 Py_SET_SIZE(self, len + 1);
49 return 0;
50 }
51 return _PyList_AppendTakeRefListResize(self, newitem);
52 }
53
54 // Repeat the bytes of a buffer in place
55 static inline void
56 _Py_memory_repeat(char* dest, Py_ssize_t len_dest, Py_ssize_t len_src)
57 {
58 assert(len_src > 0);
59 Py_ssize_t copied = len_src;
60 while (copied < len_dest) {
61 Py_ssize_t bytes_to_copy = Py_MIN(copied, len_dest - copied);
62 memcpy(dest + copied, dest, (size_t)bytes_to_copy);
63 copied += bytes_to_copy;
64 }
65 }
66
67 typedef struct {
68 PyObject_HEAD
69 Py_ssize_t it_index;
70 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
71 } _PyListIterObject;
72
73 union _PyStackRef;
74
75 PyAPI_FUNC(PyObject *)_PyList_FromStackRefStealOnSuccess(const union _PyStackRef *src, Py_ssize_t n);
76 PyAPI_FUNC(PyObject *)_PyList_AsTupleAndClear(PyListObject *v);
77
78 #ifdef __cplusplus
79 }
80 #endif
81 #endif /* !Py_INTERNAL_LIST_H */