Debian Codegraph @

pypy/module/cpyext/src/pymem.c

raw symbols SCIP
1 #include <Python.h>
2
3 #ifdef _WIN32
4 # include <Windows.h>
5 #endif
6
7
8 void *
9 PyMem_RawMalloc(size_t size)
10 {
11 /*
12 * Limit ourselves to PY_SSIZE_T_MAX bytes to prevent security holes.
13 * Most python internals blindly use a signed Py_ssize_t to track
14 * things without checking for overflows or negatives.
15 * As size_t is unsigned, checking for size < 0 is not required.
16 */
17 if (size > (size_t)PY_SSIZE_T_MAX)
18 return NULL;
19 if (size == 0)
20 size = 1;
21 return malloc(size);
22 }
23
24 void *
25 PyMem_RawCalloc(size_t nelem, size_t elsize)
26 {
27 /* see PyMem_RawMalloc() */
28 if (elsize != 0 && nelem > (size_t)PY_SSIZE_T_MAX / elsize)
29 return NULL;
30 /* PyMem_RawCalloc(0, 0) means calloc(1, 1). Some systems would return NULL
31 for calloc(0, 0), which would be treated as an error. Some platforms
32 would return a pointer with no memory behind it, which would break
33 pymalloc. To solve these problems, allocate an extra byte. */
34 if (nelem == 0 || elsize == 0) {
35 nelem = 1;
36 elsize = 1;
37 }
38 return calloc(nelem, elsize);
39 }
40
41 void*
42 PyMem_RawRealloc(void *ptr, size_t size)
43 {
44 /* see PyMem_RawMalloc() */
45 if (size > (size_t)PY_SSIZE_T_MAX)
46 return NULL;
47 if (size == 0)
48 size = 1;
49 return realloc(ptr, size);
50 }
51
52 void PyMem_RawFree(void *ptr)
53 {
54 free(ptr);
55 }
56
57
58 /* the PyMem_Xxx functions are the same as PyMem_RawXxx in PyPy, for now */
59 void *PyMem_Malloc(size_t size)
60 {
61 if (size > (size_t)PY_SSIZE_T_MAX)
62 return NULL;
63 if (size == 0)
64 size = 1;
65 return malloc(size);
66 }
67
68 void *PyMem_Calloc(size_t nelem, size_t elsize)
69 {
70 if (elsize != 0 && nelem > (size_t)PY_SSIZE_T_MAX / elsize)
71 return NULL;
72 if (nelem == 0 || elsize == 0) {
73 nelem = 1;
74 elsize = 1;
75 }
76 return calloc(nelem, elsize);
77 }
78
79 void* PyMem_Realloc(void *ptr, size_t size)
80 {
81 if (size > (size_t)PY_SSIZE_T_MAX)
82 return NULL;
83 if (size == 0)
84 size = 1;
85 return realloc(ptr, size);
86 }
87
88 void PyMem_Free(void *ptr)
89 {
90 free(ptr);
91 }
92
93 int PyTraceMalloc_Track(unsigned int domain,
94 uintptr_t ptr, size_t size)
95 {
96 /* to avoid acquiring/releasing the GIL too often, only do it
97 if the total reported size exceeds 64KB. */
98 static volatile long unreported_size = 0;
99 long prev, next, report;
100
101 size += sizeof(long);
102 /* ^^^ to account for some alignment. Important, otherwise we'd
103 * collect sizes of, say, 1-bytes mallocs in 1-bytes increment */
104
105 retry:
106 report = 0;
107 prev = unreported_size;
108 next = prev + size;
109 if (next >= 65536) {
110 report = next;
111 next = 0;
112 }
113 if (prev != next) {
114 #ifdef _WIN32
115 if (InterlockedCompareExchange(&unreported_size, next, prev) != prev)
116 goto retry;
117 #else
118 if (!__sync_bool_compare_and_swap(&unreported_size, prev, next))
119 goto retry;
120 #endif
121 }
122
123 if (report) {
124 PyGILState_STATE state = PyGILState_Ensure();
125 _PyPyGC_AddMemoryPressure(report);
126 PyGILState_Release(state);
127 }
128
129 /* Should we return -2 or 0? In theory it should be -2, because
130 we're not using the info to really track the allocations.
131 But I'm sure someone is too clever somewhere and stops calling
132 PyTraceMalloc_Track() if it returns -2. On the other hand,
133 returning 0 might lead to expectations that importing
134 'tracemalloc' works on Python 3. Oh well, in that case we'll
135 just crash with ImportError during 'import tracemalloc'.
136 */
137 return 0;
138 }
139
140 int PyTraceMalloc_Untrack(unsigned int domain, uintptr_t ptr)
141 {
142 /* nothing to do */
143 return 0;
144 }