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 #include "pycore_freelist.h" // _PyFreeListState
12
13 PyAPI_FUNC(PyObject*) _PyList_Extend(PyListObject *, PyObject *);
14 extern void _PyList_DebugMallocStats(FILE *out);
15
16 #define _PyList_ITEMS(op) _Py_RVALUE(_PyList_CAST(op)->ob_item)
17
18 PyAPI_FUNC(int)
19 _PyList_AppendTakeRefListResize(PyListObject *self, PyObject *newitem);
20
21 // In free-threaded build: self should be locked by the caller, if it should be thread-safe.
22 static inline int
_PyList_AppendTakeRef(PyListObject * self,PyObject * newitem)23 _PyList_AppendTakeRef(PyListObject *self, PyObject *newitem)
24 {
25 assert(self != NULL && newitem != NULL);
26 assert(PyList_Check(self));
27 Py_ssize_t len = Py_SIZE(self);
28 Py_ssize_t allocated = self->allocated;
29 assert((size_t)len + 1 < PY_SSIZE_T_MAX);
30 if (allocated > len) {
31 #ifdef Py_GIL_DISABLED
32 _Py_atomic_store_ptr_release(&self->ob_item[len], newitem);
33 #else
34 PyList_SET_ITEM(self, len, newitem);
35 #endif
36 Py_SET_SIZE(self, len + 1);
37 return 0;
38 }
39 return _PyList_AppendTakeRefListResize(self, newitem);
40 }
41
42 // Repeat the bytes of a buffer in place
43 static inline void
_Py_memory_repeat(char * dest,Py_ssize_t len_dest,Py_ssize_t len_src)44 _Py_memory_repeat(char* dest, Py_ssize_t len_dest, Py_ssize_t len_src)
45 {
46 assert(len_src > 0);
47 Py_ssize_t copied = len_src;
48 while (copied < len_dest) {
49 Py_ssize_t bytes_to_copy = Py_MIN(copied, len_dest - copied);
50 memcpy(dest + copied, dest, bytes_to_copy);
51 copied += bytes_to_copy;
52 }
53 }
54
55 typedef struct {
56 PyObject_HEAD
57 Py_ssize_t it_index;
58 PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
59 } _PyListIterObject;
60
61 PyAPI_FUNC(PyObject *)_PyList_FromArraySteal(PyObject *const *src, Py_ssize_t n);
62
63 #ifdef __cplusplus
64 }
65 #endif
66 #endif /* !Py_INTERNAL_LIST_H */
67