Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Include/internal/pycore_list.h
12 views
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 "listobject.h" // _PyList_CAST()
12
13
14
/* runtime lifecycle */
15
16
extern void _PyList_Fini(PyInterpreterState *);
17
18
19
/* other API */
20
21
#ifndef WITH_FREELISTS
22
// without freelists
23
# define PyList_MAXFREELIST 0
24
#endif
25
26
/* Empty list reuse scheme to save calls to malloc and free */
27
#ifndef PyList_MAXFREELIST
28
# define PyList_MAXFREELIST 80
29
#endif
30
31
struct _Py_list_state {
32
#if PyList_MAXFREELIST > 0
33
PyListObject *free_list[PyList_MAXFREELIST];
34
int numfree;
35
#endif
36
};
37
38
#define _PyList_ITEMS(op) _Py_RVALUE(_PyList_CAST(op)->ob_item)
39
40
extern int
41
_PyList_AppendTakeRefListResize(PyListObject *self, PyObject *newitem);
42
43
static inline int
44
_PyList_AppendTakeRef(PyListObject *self, PyObject *newitem)
45
{
46
assert(self != NULL && newitem != NULL);
47
assert(PyList_Check(self));
48
Py_ssize_t len = PyList_GET_SIZE(self);
49
Py_ssize_t allocated = self->allocated;
50
assert((size_t)len + 1 < PY_SSIZE_T_MAX);
51
if (allocated > len) {
52
Py_SET_SIZE(self, len + 1);
53
PyList_SET_ITEM(self, len, newitem);
54
return 0;
55
}
56
return _PyList_AppendTakeRefListResize(self, newitem);
57
}
58
59
// Repeat the bytes of a buffer in place
60
static inline void
61
_Py_memory_repeat(char* dest, Py_ssize_t len_dest, Py_ssize_t len_src)
62
{
63
assert(len_src > 0);
64
Py_ssize_t copied = len_src;
65
while (copied < len_dest) {
66
Py_ssize_t bytes_to_copy = Py_MIN(copied, len_dest - copied);
67
memcpy(dest + copied, dest, bytes_to_copy);
68
copied += bytes_to_copy;
69
}
70
}
71
72
typedef struct {
73
PyObject_HEAD
74
Py_ssize_t it_index;
75
PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
76
} _PyListIterObject;
77
78
extern PyObject *_PyList_FromArraySteal(PyObject *const *src, Py_ssize_t n);
79
80
#ifdef __cplusplus
81
}
82
#endif
83
#endif /* !Py_INTERNAL_LIST_H */
84
85