blob: fbc947806908fe594869263e93b103937b7c9678 [file] [log] [blame]
Tim Peters1221c0a2002-03-23 00:20:15 +00001#include "Python.h"
2
Benjamin Peterson3924f932016-09-18 19:12:48 -07003#include <stdbool.h>
4
Victor Stinner0611c262016-03-15 22:22:13 +01005
6/* Defined in tracemalloc.c */
7extern void _PyMem_DumpTraceback(int fd, const void *ptr);
8
9
Victor Stinner0507bf52013-07-07 02:05:46 +020010/* Python's malloc wrappers (see pymem.h) */
11
Victor Stinner34be807c2016-03-14 12:04:26 +010012#undef uint
13#define uint unsigned int /* assuming >= 16 bits */
14
Victor Stinner0507bf52013-07-07 02:05:46 +020015/* Forward declaration */
Victor Stinnerc4aec362016-03-14 22:26:53 +010016static void* _PyMem_DebugRawMalloc(void *ctx, size_t size);
17static void* _PyMem_DebugRawCalloc(void *ctx, size_t nelem, size_t elsize);
18static void* _PyMem_DebugRawRealloc(void *ctx, void *ptr, size_t size);
Victor Stinner9ed83c42017-10-31 12:18:10 -070019static void _PyMem_DebugRawFree(void *ctx, void *ptr);
Victor Stinnerc4aec362016-03-14 22:26:53 +010020
Victor Stinner0507bf52013-07-07 02:05:46 +020021static void* _PyMem_DebugMalloc(void *ctx, size_t size);
Victor Stinnerdb067af2014-05-02 22:31:14 +020022static void* _PyMem_DebugCalloc(void *ctx, size_t nelem, size_t elsize);
Victor Stinner0507bf52013-07-07 02:05:46 +020023static void* _PyMem_DebugRealloc(void *ctx, void *ptr, size_t size);
Victor Stinnerc4aec362016-03-14 22:26:53 +010024static void _PyMem_DebugFree(void *ctx, void *p);
Victor Stinner0507bf52013-07-07 02:05:46 +020025
26static void _PyObject_DebugDumpAddress(const void *p);
27static void _PyMem_DebugCheckAddress(char api_id, const void *p);
Victor Stinner0507bf52013-07-07 02:05:46 +020028
Victor Stinner5d39e042017-11-29 17:20:38 +010029static void _PyMem_SetupDebugHooksDomain(PyMemAllocatorDomain domain);
30
Nick Coghlan6ba64f42013-09-29 00:28:55 +100031#if defined(__has_feature) /* Clang */
32 #if __has_feature(address_sanitizer) /* is ASAN enabled? */
33 #define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS \
Benjamin Peterson3924f932016-09-18 19:12:48 -070034 __attribute__((no_address_safety_analysis))
Nick Coghlan6ba64f42013-09-29 00:28:55 +100035 #else
36 #define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS
37 #endif
38#else
39 #if defined(__SANITIZE_ADDRESS__) /* GCC 4.8.x, is ASAN enabled? */
40 #define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS \
Benjamin Peterson3924f932016-09-18 19:12:48 -070041 __attribute__((no_address_safety_analysis))
Nick Coghlan6ba64f42013-09-29 00:28:55 +100042 #else
43 #define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS
44 #endif
45#endif
46
Tim Peters1221c0a2002-03-23 00:20:15 +000047#ifdef WITH_PYMALLOC
48
Victor Stinner0507bf52013-07-07 02:05:46 +020049#ifdef MS_WINDOWS
50# include <windows.h>
51#elif defined(HAVE_MMAP)
52# include <sys/mman.h>
53# ifdef MAP_ANONYMOUS
54# define ARENAS_USE_MMAP
55# endif
Antoine Pitrou6f26be02011-05-03 18:18:59 +020056#endif
57
Victor Stinner0507bf52013-07-07 02:05:46 +020058/* Forward declaration */
59static void* _PyObject_Malloc(void *ctx, size_t size);
Victor Stinnerdb067af2014-05-02 22:31:14 +020060static void* _PyObject_Calloc(void *ctx, size_t nelem, size_t elsize);
Victor Stinner0507bf52013-07-07 02:05:46 +020061static void _PyObject_Free(void *ctx, void *p);
62static void* _PyObject_Realloc(void *ctx, void *ptr, size_t size);
Martin v. Löwiscd83fa82013-06-27 12:23:29 +020063#endif
64
Victor Stinner0507bf52013-07-07 02:05:46 +020065
Victor Stinner9e00e802018-10-25 13:31:16 +020066/* bpo-35053: Declare tracemalloc configuration here rather than
67 Modules/_tracemalloc.c because _tracemalloc can be compiled as dynamic
68 library, whereas _Py_NewReference() requires it. */
69struct _PyTraceMalloc_Config _Py_tracemalloc_config = _PyTraceMalloc_Config_INIT;
70
71
Victor Stinner0507bf52013-07-07 02:05:46 +020072static void *
73_PyMem_RawMalloc(void *ctx, size_t size)
74{
Victor Stinnerdb067af2014-05-02 22:31:14 +020075 /* PyMem_RawMalloc(0) means malloc(1). Some systems would return NULL
Victor Stinner0507bf52013-07-07 02:05:46 +020076 for malloc(0), which would be treated as an error. Some platforms would
77 return a pointer with no memory behind it, which would break pymalloc.
78 To solve these problems, allocate an extra byte. */
79 if (size == 0)
80 size = 1;
81 return malloc(size);
82}
83
84static void *
Victor Stinnerdb067af2014-05-02 22:31:14 +020085_PyMem_RawCalloc(void *ctx, size_t nelem, size_t elsize)
86{
87 /* PyMem_RawCalloc(0, 0) means calloc(1, 1). Some systems would return NULL
88 for calloc(0, 0), which would be treated as an error. Some platforms
89 would return a pointer with no memory behind it, which would break
90 pymalloc. To solve these problems, allocate an extra byte. */
91 if (nelem == 0 || elsize == 0) {
92 nelem = 1;
93 elsize = 1;
94 }
95 return calloc(nelem, elsize);
96}
97
98static void *
Victor Stinner0507bf52013-07-07 02:05:46 +020099_PyMem_RawRealloc(void *ctx, void *ptr, size_t size)
100{
101 if (size == 0)
102 size = 1;
103 return realloc(ptr, size);
104}
105
106static void
107_PyMem_RawFree(void *ctx, void *ptr)
108{
109 free(ptr);
110}
111
112
113#ifdef MS_WINDOWS
114static void *
115_PyObject_ArenaVirtualAlloc(void *ctx, size_t size)
116{
117 return VirtualAlloc(NULL, size,
118 MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
119}
120
121static void
122_PyObject_ArenaVirtualFree(void *ctx, void *ptr, size_t size)
123{
Victor Stinner725e6682013-07-07 03:06:16 +0200124 VirtualFree(ptr, 0, MEM_RELEASE);
Victor Stinner0507bf52013-07-07 02:05:46 +0200125}
126
127#elif defined(ARENAS_USE_MMAP)
128static void *
129_PyObject_ArenaMmap(void *ctx, size_t size)
130{
131 void *ptr;
132 ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
133 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
134 if (ptr == MAP_FAILED)
135 return NULL;
136 assert(ptr != NULL);
137 return ptr;
138}
139
140static void
141_PyObject_ArenaMunmap(void *ctx, void *ptr, size_t size)
142{
143 munmap(ptr, size);
144}
145
146#else
147static void *
148_PyObject_ArenaMalloc(void *ctx, size_t size)
149{
150 return malloc(size);
151}
152
153static void
154_PyObject_ArenaFree(void *ctx, void *ptr, size_t size)
155{
156 free(ptr);
157}
158#endif
159
Victor Stinner5d39e042017-11-29 17:20:38 +0100160#define MALLOC_ALLOC {NULL, _PyMem_RawMalloc, _PyMem_RawCalloc, _PyMem_RawRealloc, _PyMem_RawFree}
Victor Stinner0507bf52013-07-07 02:05:46 +0200161#ifdef WITH_PYMALLOC
Victor Stinner5d39e042017-11-29 17:20:38 +0100162# define PYMALLOC_ALLOC {NULL, _PyObject_Malloc, _PyObject_Calloc, _PyObject_Realloc, _PyObject_Free}
Victor Stinner0507bf52013-07-07 02:05:46 +0200163#endif
Victor Stinner5d39e042017-11-29 17:20:38 +0100164
165#define PYRAW_ALLOC MALLOC_ALLOC
166#ifdef WITH_PYMALLOC
167# define PYOBJ_ALLOC PYMALLOC_ALLOC
168#else
169# define PYOBJ_ALLOC MALLOC_ALLOC
170#endif
171#define PYMEM_ALLOC PYOBJ_ALLOC
Victor Stinner0507bf52013-07-07 02:05:46 +0200172
Victor Stinner0507bf52013-07-07 02:05:46 +0200173typedef struct {
174 /* We tag each block with an API ID in order to tag API violations */
175 char api_id;
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200176 PyMemAllocatorEx alloc;
Victor Stinner0507bf52013-07-07 02:05:46 +0200177} debug_alloc_api_t;
178static struct {
179 debug_alloc_api_t raw;
180 debug_alloc_api_t mem;
181 debug_alloc_api_t obj;
182} _PyMem_Debug = {
Victor Stinner5d39e042017-11-29 17:20:38 +0100183 {'r', PYRAW_ALLOC},
184 {'m', PYMEM_ALLOC},
185 {'o', PYOBJ_ALLOC}
Victor Stinner0507bf52013-07-07 02:05:46 +0200186 };
187
Victor Stinner5d39e042017-11-29 17:20:38 +0100188#define PYDBGRAW_ALLOC \
189 {&_PyMem_Debug.raw, _PyMem_DebugRawMalloc, _PyMem_DebugRawCalloc, _PyMem_DebugRawRealloc, _PyMem_DebugRawFree}
190#define PYDBGMEM_ALLOC \
191 {&_PyMem_Debug.mem, _PyMem_DebugMalloc, _PyMem_DebugCalloc, _PyMem_DebugRealloc, _PyMem_DebugFree}
192#define PYDBGOBJ_ALLOC \
193 {&_PyMem_Debug.obj, _PyMem_DebugMalloc, _PyMem_DebugCalloc, _PyMem_DebugRealloc, _PyMem_DebugFree}
Victor Stinner0507bf52013-07-07 02:05:46 +0200194
Victor Stinner9e87e772017-11-24 12:09:24 +0100195#ifdef Py_DEBUG
Victor Stinner5d39e042017-11-29 17:20:38 +0100196static PyMemAllocatorEx _PyMem_Raw = PYDBGRAW_ALLOC;
197static PyMemAllocatorEx _PyMem = PYDBGMEM_ALLOC;
198static PyMemAllocatorEx _PyObject = PYDBGOBJ_ALLOC;
Victor Stinner9e87e772017-11-24 12:09:24 +0100199#else
Victor Stinner5d39e042017-11-29 17:20:38 +0100200static PyMemAllocatorEx _PyMem_Raw = PYRAW_ALLOC;
201static PyMemAllocatorEx _PyMem = PYMEM_ALLOC;
202static PyMemAllocatorEx _PyObject = PYOBJ_ALLOC;
Victor Stinner9e87e772017-11-24 12:09:24 +0100203#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600204
Victor Stinner0507bf52013-07-07 02:05:46 +0200205
Victor Stinner5d39e042017-11-29 17:20:38 +0100206static int
207pymem_set_default_allocator(PyMemAllocatorDomain domain, int debug,
208 PyMemAllocatorEx *old_alloc)
209{
210 if (old_alloc != NULL) {
211 PyMem_GetAllocator(domain, old_alloc);
212 }
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800213
Victor Stinner5d39e042017-11-29 17:20:38 +0100214
215 PyMemAllocatorEx new_alloc;
216 switch(domain)
217 {
218 case PYMEM_DOMAIN_RAW:
219 new_alloc = (PyMemAllocatorEx)PYRAW_ALLOC;
220 break;
221 case PYMEM_DOMAIN_MEM:
222 new_alloc = (PyMemAllocatorEx)PYMEM_ALLOC;
223 break;
224 case PYMEM_DOMAIN_OBJ:
225 new_alloc = (PyMemAllocatorEx)PYOBJ_ALLOC;
226 break;
227 default:
228 /* unknown domain */
229 return -1;
230 }
231 PyMem_SetAllocator(domain, &new_alloc);
232 if (debug) {
233 _PyMem_SetupDebugHooksDomain(domain);
234 }
235 return 0;
236}
237
238
239int
240_PyMem_SetDefaultAllocator(PyMemAllocatorDomain domain,
241 PyMemAllocatorEx *old_alloc)
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800242{
Victor Stinnerccb04422017-11-16 03:20:31 -0800243#ifdef Py_DEBUG
Victor Stinner5d39e042017-11-29 17:20:38 +0100244 const int debug = 1;
Victor Stinnerccb04422017-11-16 03:20:31 -0800245#else
Victor Stinner5d39e042017-11-29 17:20:38 +0100246 const int debug = 0;
Victor Stinnerccb04422017-11-16 03:20:31 -0800247#endif
Victor Stinner5d39e042017-11-29 17:20:38 +0100248 return pymem_set_default_allocator(domain, debug, old_alloc);
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800249}
Victor Stinner0507bf52013-07-07 02:05:46 +0200250
Victor Stinner5d39e042017-11-29 17:20:38 +0100251
Victor Stinner34be807c2016-03-14 12:04:26 +0100252int
253_PyMem_SetupAllocators(const char *opt)
254{
255 if (opt == NULL || *opt == '\0') {
256 /* PYTHONMALLOC is empty or is not set or ignored (-E/-I command line
Victor Stinner5d39e042017-11-29 17:20:38 +0100257 options): use default memory allocators */
258 opt = "default";
Victor Stinner34be807c2016-03-14 12:04:26 +0100259 }
260
Victor Stinner5d39e042017-11-29 17:20:38 +0100261 if (strcmp(opt, "default") == 0) {
262 (void)_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, NULL);
263 (void)_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_MEM, NULL);
264 (void)_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_OBJ, NULL);
Victor Stinner34be807c2016-03-14 12:04:26 +0100265 }
Victor Stinner5d39e042017-11-29 17:20:38 +0100266 else if (strcmp(opt, "debug") == 0) {
267 (void)pymem_set_default_allocator(PYMEM_DOMAIN_RAW, 1, NULL);
268 (void)pymem_set_default_allocator(PYMEM_DOMAIN_MEM, 1, NULL);
269 (void)pymem_set_default_allocator(PYMEM_DOMAIN_OBJ, 1, NULL);
Victor Stinner34be807c2016-03-14 12:04:26 +0100270 }
271#ifdef WITH_PYMALLOC
Victor Stinner5d39e042017-11-29 17:20:38 +0100272 else if (strcmp(opt, "pymalloc") == 0 || strcmp(opt, "pymalloc_debug") == 0) {
273 PyMemAllocatorEx malloc_alloc = MALLOC_ALLOC;
274 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &malloc_alloc);
Victor Stinner34be807c2016-03-14 12:04:26 +0100275
Victor Stinner5d39e042017-11-29 17:20:38 +0100276 PyMemAllocatorEx pymalloc = PYMALLOC_ALLOC;
277 PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &pymalloc);
278 PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &pymalloc);
Victor Stinner34be807c2016-03-14 12:04:26 +0100279
Victor Stinner5d39e042017-11-29 17:20:38 +0100280 if (strcmp(opt, "pymalloc_debug") == 0) {
Victor Stinner34be807c2016-03-14 12:04:26 +0100281 PyMem_SetupDebugHooks();
Victor Stinner5d39e042017-11-29 17:20:38 +0100282 }
Victor Stinner34be807c2016-03-14 12:04:26 +0100283 }
284#endif
Victor Stinner5d39e042017-11-29 17:20:38 +0100285 else if (strcmp(opt, "malloc") == 0 || strcmp(opt, "malloc_debug") == 0) {
286 PyMemAllocatorEx malloc_alloc = MALLOC_ALLOC;
287 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &malloc_alloc);
288 PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &malloc_alloc);
289 PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &malloc_alloc);
290
291 if (strcmp(opt, "malloc_debug") == 0) {
292 PyMem_SetupDebugHooks();
293 }
294 }
Victor Stinner34be807c2016-03-14 12:04:26 +0100295 else {
296 /* unknown allocator */
297 return -1;
298 }
299 return 0;
300}
301
Victor Stinner5d39e042017-11-29 17:20:38 +0100302
303static int
304pymemallocator_eq(PyMemAllocatorEx *a, PyMemAllocatorEx *b)
305{
306 return (memcmp(a, b, sizeof(PyMemAllocatorEx)) == 0);
307}
308
309
310const char*
311_PyMem_GetAllocatorsName(void)
312{
313 PyMemAllocatorEx malloc_alloc = MALLOC_ALLOC;
314#ifdef WITH_PYMALLOC
315 PyMemAllocatorEx pymalloc = PYMALLOC_ALLOC;
316#endif
317
318 if (pymemallocator_eq(&_PyMem_Raw, &malloc_alloc) &&
319 pymemallocator_eq(&_PyMem, &malloc_alloc) &&
320 pymemallocator_eq(&_PyObject, &malloc_alloc))
321 {
322 return "malloc";
323 }
324#ifdef WITH_PYMALLOC
325 if (pymemallocator_eq(&_PyMem_Raw, &malloc_alloc) &&
326 pymemallocator_eq(&_PyMem, &pymalloc) &&
327 pymemallocator_eq(&_PyObject, &pymalloc))
328 {
329 return "pymalloc";
330 }
331#endif
332
333 PyMemAllocatorEx dbg_raw = PYDBGRAW_ALLOC;
334 PyMemAllocatorEx dbg_mem = PYDBGMEM_ALLOC;
335 PyMemAllocatorEx dbg_obj = PYDBGOBJ_ALLOC;
336
337 if (pymemallocator_eq(&_PyMem_Raw, &dbg_raw) &&
338 pymemallocator_eq(&_PyMem, &dbg_mem) &&
339 pymemallocator_eq(&_PyObject, &dbg_obj))
340 {
341 /* Debug hooks installed */
342 if (pymemallocator_eq(&_PyMem_Debug.raw.alloc, &malloc_alloc) &&
343 pymemallocator_eq(&_PyMem_Debug.mem.alloc, &malloc_alloc) &&
344 pymemallocator_eq(&_PyMem_Debug.obj.alloc, &malloc_alloc))
345 {
346 return "malloc_debug";
347 }
348#ifdef WITH_PYMALLOC
349 if (pymemallocator_eq(&_PyMem_Debug.raw.alloc, &malloc_alloc) &&
350 pymemallocator_eq(&_PyMem_Debug.mem.alloc, &pymalloc) &&
351 pymemallocator_eq(&_PyMem_Debug.obj.alloc, &pymalloc))
352 {
353 return "pymalloc_debug";
354 }
355#endif
356 }
357 return NULL;
358}
359
360
361#undef MALLOC_ALLOC
362#undef PYMALLOC_ALLOC
363#undef PYRAW_ALLOC
364#undef PYMEM_ALLOC
365#undef PYOBJ_ALLOC
366#undef PYDBGRAW_ALLOC
367#undef PYDBGMEM_ALLOC
368#undef PYDBGOBJ_ALLOC
369
Victor Stinner0507bf52013-07-07 02:05:46 +0200370
Victor Stinner9e87e772017-11-24 12:09:24 +0100371static PyObjectArenaAllocator _PyObject_Arena = {NULL,
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800372#ifdef MS_WINDOWS
Victor Stinner9e87e772017-11-24 12:09:24 +0100373 _PyObject_ArenaVirtualAlloc, _PyObject_ArenaVirtualFree
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800374#elif defined(ARENAS_USE_MMAP)
Victor Stinner9e87e772017-11-24 12:09:24 +0100375 _PyObject_ArenaMmap, _PyObject_ArenaMunmap
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800376#else
Victor Stinner9e87e772017-11-24 12:09:24 +0100377 _PyObject_ArenaMalloc, _PyObject_ArenaFree
Victor Stinnerf7e5b562017-11-15 15:48:08 -0800378#endif
379 };
380
Victor Stinner0621e0e2016-04-19 17:02:55 +0200381#ifdef WITH_PYMALLOC
Victor Stinner34be807c2016-03-14 12:04:26 +0100382static int
383_PyMem_DebugEnabled(void)
384{
385 return (_PyObject.malloc == _PyMem_DebugMalloc);
386}
387
Victor Stinner6bf992a2017-12-06 17:26:10 +0100388static int
Victor Stinner34be807c2016-03-14 12:04:26 +0100389_PyMem_PymallocEnabled(void)
390{
391 if (_PyMem_DebugEnabled()) {
392 return (_PyMem_Debug.obj.alloc.malloc == _PyObject_Malloc);
393 }
394 else {
395 return (_PyObject.malloc == _PyObject_Malloc);
396 }
397}
398#endif
399
Victor Stinner5d39e042017-11-29 17:20:38 +0100400
401static void
402_PyMem_SetupDebugHooksDomain(PyMemAllocatorDomain domain)
Victor Stinner0507bf52013-07-07 02:05:46 +0200403{
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200404 PyMemAllocatorEx alloc;
Victor Stinner0507bf52013-07-07 02:05:46 +0200405
Victor Stinner5d39e042017-11-29 17:20:38 +0100406 if (domain == PYMEM_DOMAIN_RAW) {
407 if (_PyMem_Raw.malloc == _PyMem_DebugRawMalloc) {
408 return;
409 }
Victor Stinner34be807c2016-03-14 12:04:26 +0100410
Victor Stinner0507bf52013-07-07 02:05:46 +0200411 PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &_PyMem_Debug.raw.alloc);
Victor Stinner5d39e042017-11-29 17:20:38 +0100412 alloc.ctx = &_PyMem_Debug.raw;
413 alloc.malloc = _PyMem_DebugRawMalloc;
414 alloc.calloc = _PyMem_DebugRawCalloc;
415 alloc.realloc = _PyMem_DebugRawRealloc;
416 alloc.free = _PyMem_DebugRawFree;
Victor Stinner0507bf52013-07-07 02:05:46 +0200417 PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc);
418 }
Victor Stinner5d39e042017-11-29 17:20:38 +0100419 else if (domain == PYMEM_DOMAIN_MEM) {
420 if (_PyMem.malloc == _PyMem_DebugMalloc) {
421 return;
422 }
Victor Stinner0507bf52013-07-07 02:05:46 +0200423
Victor Stinnerad524372016-03-16 12:12:53 +0100424 PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &_PyMem_Debug.mem.alloc);
Victor Stinner5d39e042017-11-29 17:20:38 +0100425 alloc.ctx = &_PyMem_Debug.mem;
426 alloc.malloc = _PyMem_DebugMalloc;
427 alloc.calloc = _PyMem_DebugCalloc;
428 alloc.realloc = _PyMem_DebugRealloc;
429 alloc.free = _PyMem_DebugFree;
Victor Stinnerad524372016-03-16 12:12:53 +0100430 PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc);
431 }
Victor Stinner5d39e042017-11-29 17:20:38 +0100432 else if (domain == PYMEM_DOMAIN_OBJ) {
433 if (_PyObject.malloc == _PyMem_DebugMalloc) {
434 return;
435 }
Victor Stinnerad524372016-03-16 12:12:53 +0100436
Victor Stinner0507bf52013-07-07 02:05:46 +0200437 PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &_PyMem_Debug.obj.alloc);
Victor Stinner5d39e042017-11-29 17:20:38 +0100438 alloc.ctx = &_PyMem_Debug.obj;
439 alloc.malloc = _PyMem_DebugMalloc;
440 alloc.calloc = _PyMem_DebugCalloc;
441 alloc.realloc = _PyMem_DebugRealloc;
442 alloc.free = _PyMem_DebugFree;
Victor Stinner0507bf52013-07-07 02:05:46 +0200443 PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc);
444 }
Victor Stinner0507bf52013-07-07 02:05:46 +0200445}
446
Victor Stinner5d39e042017-11-29 17:20:38 +0100447
448void
449PyMem_SetupDebugHooks(void)
450{
451 _PyMem_SetupDebugHooksDomain(PYMEM_DOMAIN_RAW);
452 _PyMem_SetupDebugHooksDomain(PYMEM_DOMAIN_MEM);
453 _PyMem_SetupDebugHooksDomain(PYMEM_DOMAIN_OBJ);
454}
455
Victor Stinner0507bf52013-07-07 02:05:46 +0200456void
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200457PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
Victor Stinner0507bf52013-07-07 02:05:46 +0200458{
459 switch(domain)
460 {
461 case PYMEM_DOMAIN_RAW: *allocator = _PyMem_Raw; break;
462 case PYMEM_DOMAIN_MEM: *allocator = _PyMem; break;
463 case PYMEM_DOMAIN_OBJ: *allocator = _PyObject; break;
464 default:
Victor Stinnerdb067af2014-05-02 22:31:14 +0200465 /* unknown domain: set all attributes to NULL */
Victor Stinner0507bf52013-07-07 02:05:46 +0200466 allocator->ctx = NULL;
467 allocator->malloc = NULL;
Victor Stinnerdb067af2014-05-02 22:31:14 +0200468 allocator->calloc = NULL;
Victor Stinner0507bf52013-07-07 02:05:46 +0200469 allocator->realloc = NULL;
470 allocator->free = NULL;
471 }
472}
473
474void
Victor Stinnerd8f0d922014-06-02 21:57:10 +0200475PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
Victor Stinner0507bf52013-07-07 02:05:46 +0200476{
477 switch(domain)
478 {
479 case PYMEM_DOMAIN_RAW: _PyMem_Raw = *allocator; break;
480 case PYMEM_DOMAIN_MEM: _PyMem = *allocator; break;
481 case PYMEM_DOMAIN_OBJ: _PyObject = *allocator; break;
482 /* ignore unknown domain */
483 }
Victor Stinner0507bf52013-07-07 02:05:46 +0200484}
485
486void
487PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator)
488{
Victor Stinner9e87e772017-11-24 12:09:24 +0100489 *allocator = _PyObject_Arena;
Victor Stinner0507bf52013-07-07 02:05:46 +0200490}
491
492void
493PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator)
494{
Victor Stinner9e87e772017-11-24 12:09:24 +0100495 _PyObject_Arena = *allocator;
Victor Stinner0507bf52013-07-07 02:05:46 +0200496}
497
498void *
499PyMem_RawMalloc(size_t size)
500{
501 /*
502 * Limit ourselves to PY_SSIZE_T_MAX bytes to prevent security holes.
503 * Most python internals blindly use a signed Py_ssize_t to track
504 * things without checking for overflows or negatives.
505 * As size_t is unsigned, checking for size < 0 is not required.
506 */
507 if (size > (size_t)PY_SSIZE_T_MAX)
508 return NULL;
Victor Stinner0507bf52013-07-07 02:05:46 +0200509 return _PyMem_Raw.malloc(_PyMem_Raw.ctx, size);
510}
511
Victor Stinnerdb067af2014-05-02 22:31:14 +0200512void *
513PyMem_RawCalloc(size_t nelem, size_t elsize)
514{
515 /* see PyMem_RawMalloc() */
516 if (elsize != 0 && nelem > (size_t)PY_SSIZE_T_MAX / elsize)
517 return NULL;
518 return _PyMem_Raw.calloc(_PyMem_Raw.ctx, nelem, elsize);
519}
520
Victor Stinner0507bf52013-07-07 02:05:46 +0200521void*
522PyMem_RawRealloc(void *ptr, size_t new_size)
523{
524 /* see PyMem_RawMalloc() */
525 if (new_size > (size_t)PY_SSIZE_T_MAX)
526 return NULL;
527 return _PyMem_Raw.realloc(_PyMem_Raw.ctx, ptr, new_size);
528}
529
Victor Stinner9e87e772017-11-24 12:09:24 +0100530void PyMem_RawFree(void *ptr)
Victor Stinner0507bf52013-07-07 02:05:46 +0200531{
532 _PyMem_Raw.free(_PyMem_Raw.ctx, ptr);
533}
534
Victor Stinner9ed83c42017-10-31 12:18:10 -0700535
Victor Stinner0507bf52013-07-07 02:05:46 +0200536void *
537PyMem_Malloc(size_t size)
538{
539 /* see PyMem_RawMalloc() */
540 if (size > (size_t)PY_SSIZE_T_MAX)
541 return NULL;
542 return _PyMem.malloc(_PyMem.ctx, size);
543}
544
545void *
Victor Stinnerdb067af2014-05-02 22:31:14 +0200546PyMem_Calloc(size_t nelem, size_t elsize)
547{
548 /* see PyMem_RawMalloc() */
549 if (elsize != 0 && nelem > (size_t)PY_SSIZE_T_MAX / elsize)
550 return NULL;
551 return _PyMem.calloc(_PyMem.ctx, nelem, elsize);
552}
553
554void *
Victor Stinner0507bf52013-07-07 02:05:46 +0200555PyMem_Realloc(void *ptr, size_t new_size)
556{
557 /* see PyMem_RawMalloc() */
558 if (new_size > (size_t)PY_SSIZE_T_MAX)
559 return NULL;
560 return _PyMem.realloc(_PyMem.ctx, ptr, new_size);
561}
562
563void
564PyMem_Free(void *ptr)
565{
566 _PyMem.free(_PyMem.ctx, ptr);
567}
568
Victor Stinner9ed83c42017-10-31 12:18:10 -0700569
Victor Stinner46972b72017-11-24 22:55:40 +0100570wchar_t*
571_PyMem_RawWcsdup(const wchar_t *str)
572{
Victor Stinnerb64de462017-12-01 18:27:09 +0100573 assert(str != NULL);
574
Victor Stinner46972b72017-11-24 22:55:40 +0100575 size_t len = wcslen(str);
576 if (len > (size_t)PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) {
577 return NULL;
578 }
579
580 size_t size = (len + 1) * sizeof(wchar_t);
581 wchar_t *str2 = PyMem_RawMalloc(size);
582 if (str2 == NULL) {
583 return NULL;
584 }
585
586 memcpy(str2, str, size);
587 return str2;
588}
589
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200590char *
591_PyMem_RawStrdup(const char *str)
592{
Victor Stinnerb64de462017-12-01 18:27:09 +0100593 assert(str != NULL);
594 size_t size = strlen(str) + 1;
595 char *copy = PyMem_RawMalloc(size);
596 if (copy == NULL) {
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200597 return NULL;
Victor Stinnerb64de462017-12-01 18:27:09 +0100598 }
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200599 memcpy(copy, str, size);
600 return copy;
601}
602
603char *
604_PyMem_Strdup(const char *str)
605{
Victor Stinnerb64de462017-12-01 18:27:09 +0100606 assert(str != NULL);
607 size_t size = strlen(str) + 1;
608 char *copy = PyMem_Malloc(size);
609 if (copy == NULL) {
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200610 return NULL;
Victor Stinnerb64de462017-12-01 18:27:09 +0100611 }
Victor Stinner49fc8ec2013-07-07 23:30:24 +0200612 memcpy(copy, str, size);
613 return copy;
614}
615
Victor Stinner0507bf52013-07-07 02:05:46 +0200616void *
617PyObject_Malloc(size_t size)
618{
619 /* see PyMem_RawMalloc() */
620 if (size > (size_t)PY_SSIZE_T_MAX)
621 return NULL;
622 return _PyObject.malloc(_PyObject.ctx, size);
623}
624
625void *
Victor Stinnerdb067af2014-05-02 22:31:14 +0200626PyObject_Calloc(size_t nelem, size_t elsize)
627{
628 /* see PyMem_RawMalloc() */
629 if (elsize != 0 && nelem > (size_t)PY_SSIZE_T_MAX / elsize)
630 return NULL;
631 return _PyObject.calloc(_PyObject.ctx, nelem, elsize);
632}
633
634void *
Victor Stinner0507bf52013-07-07 02:05:46 +0200635PyObject_Realloc(void *ptr, size_t new_size)
636{
637 /* see PyMem_RawMalloc() */
638 if (new_size > (size_t)PY_SSIZE_T_MAX)
639 return NULL;
640 return _PyObject.realloc(_PyObject.ctx, ptr, new_size);
641}
642
643void
644PyObject_Free(void *ptr)
645{
646 _PyObject.free(_PyObject.ctx, ptr);
647}
648
649
650#ifdef WITH_PYMALLOC
651
Benjamin Peterson05159c42009-12-03 03:01:27 +0000652#ifdef WITH_VALGRIND
653#include <valgrind/valgrind.h>
654
655/* If we're using GCC, use __builtin_expect() to reduce overhead of
656 the valgrind checks */
657#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
658# define UNLIKELY(value) __builtin_expect((value), 0)
659#else
660# define UNLIKELY(value) (value)
661#endif
662
663/* -1 indicates that we haven't checked that we're running on valgrind yet. */
664static int running_on_valgrind = -1;
665#endif
666
Victor Stinner9ed83c42017-10-31 12:18:10 -0700667
Victor Stinner9e87e772017-11-24 12:09:24 +0100668/* An object allocator for Python.
669
670 Here is an introduction to the layers of the Python memory architecture,
671 showing where the object allocator is actually used (layer +2), It is
672 called for every object allocation and deallocation (PyObject_New/Del),
673 unless the object-specific allocators implement a proprietary allocation
674 scheme (ex.: ints use a simple free list). This is also the place where
675 the cyclic garbage collector operates selectively on container objects.
676
677
678 Object-specific allocators
679 _____ ______ ______ ________
680 [ int ] [ dict ] [ list ] ... [ string ] Python core |
681+3 | <----- Object-specific memory -----> | <-- Non-object memory --> |
682 _______________________________ | |
683 [ Python's object allocator ] | |
684+2 | ####### Object memory ####### | <------ Internal buffers ------> |
685 ______________________________________________________________ |
686 [ Python's raw memory allocator (PyMem_ API) ] |
687+1 | <----- Python memory (under PyMem manager's control) ------> | |
688 __________________________________________________________________
689 [ Underlying general-purpose allocator (ex: C library malloc) ]
690 0 | <------ Virtual memory allocated for the python process -------> |
691
692 =========================================================================
693 _______________________________________________________________________
694 [ OS-specific Virtual Memory Manager (VMM) ]
695-1 | <--- Kernel dynamic storage allocation & management (page-based) ---> |
696 __________________________________ __________________________________
697 [ ] [ ]
698-2 | <-- Physical memory: ROM/RAM --> | | <-- Secondary storage (swap) --> |
699
700*/
701/*==========================================================================*/
702
703/* A fast, special-purpose memory allocator for small blocks, to be used
704 on top of a general-purpose malloc -- heavily based on previous art. */
705
706/* Vladimir Marangozov -- August 2000 */
707
708/*
709 * "Memory management is where the rubber meets the road -- if we do the wrong
710 * thing at any level, the results will not be good. And if we don't make the
711 * levels work well together, we are in serious trouble." (1)
712 *
713 * (1) Paul R. Wilson, Mark S. Johnstone, Michael Neely, and David Boles,
714 * "Dynamic Storage Allocation: A Survey and Critical Review",
715 * in Proc. 1995 Int'l. Workshop on Memory Management, September 1995.
716 */
717
718/* #undef WITH_MEMORY_LIMITS */ /* disable mem limit checks */
719
720/*==========================================================================*/
721
722/*
723 * Allocation strategy abstract:
724 *
725 * For small requests, the allocator sub-allocates <Big> blocks of memory.
726 * Requests greater than SMALL_REQUEST_THRESHOLD bytes are routed to the
727 * system's allocator.
728 *
729 * Small requests are grouped in size classes spaced 8 bytes apart, due
730 * to the required valid alignment of the returned address. Requests of
731 * a particular size are serviced from memory pools of 4K (one VMM page).
732 * Pools are fragmented on demand and contain free lists of blocks of one
733 * particular size class. In other words, there is a fixed-size allocator
734 * for each size class. Free pools are shared by the different allocators
735 * thus minimizing the space reserved for a particular size class.
736 *
737 * This allocation strategy is a variant of what is known as "simple
738 * segregated storage based on array of free lists". The main drawback of
739 * simple segregated storage is that we might end up with lot of reserved
740 * memory for the different free lists, which degenerate in time. To avoid
741 * this, we partition each free list in pools and we share dynamically the
742 * reserved space between all free lists. This technique is quite efficient
743 * for memory intensive programs which allocate mainly small-sized blocks.
744 *
745 * For small requests we have the following table:
746 *
747 * Request in bytes Size of allocated block Size class idx
748 * ----------------------------------------------------------------
749 * 1-8 8 0
750 * 9-16 16 1
751 * 17-24 24 2
752 * 25-32 32 3
753 * 33-40 40 4
754 * 41-48 48 5
755 * 49-56 56 6
756 * 57-64 64 7
757 * 65-72 72 8
758 * ... ... ...
759 * 497-504 504 62
760 * 505-512 512 63
761 *
762 * 0, SMALL_REQUEST_THRESHOLD + 1 and up: routed to the underlying
763 * allocator.
764 */
765
766/*==========================================================================*/
767
768/*
769 * -- Main tunable settings section --
770 */
771
772/*
773 * Alignment of addresses returned to the user. 8-bytes alignment works
774 * on most current architectures (with 32-bit or 64-bit address busses).
775 * The alignment value is also used for grouping small requests in size
776 * classes spaced ALIGNMENT bytes apart.
777 *
778 * You shouldn't change this unless you know what you are doing.
779 */
780#define ALIGNMENT 8 /* must be 2^N */
781#define ALIGNMENT_SHIFT 3
782
783/* Return the number of bytes in size class I, as a uint. */
784#define INDEX2SIZE(I) (((uint)(I) + 1) << ALIGNMENT_SHIFT)
785
786/*
787 * Max size threshold below which malloc requests are considered to be
788 * small enough in order to use preallocated memory pools. You can tune
789 * this value according to your application behaviour and memory needs.
790 *
791 * Note: a size threshold of 512 guarantees that newly created dictionaries
792 * will be allocated from preallocated memory pools on 64-bit.
793 *
794 * The following invariants must hold:
795 * 1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 512
796 * 2) SMALL_REQUEST_THRESHOLD is evenly divisible by ALIGNMENT
797 *
798 * Although not required, for better performance and space efficiency,
799 * it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2.
800 */
801#define SMALL_REQUEST_THRESHOLD 512
802#define NB_SMALL_SIZE_CLASSES (SMALL_REQUEST_THRESHOLD / ALIGNMENT)
803
804/*
805 * The system's VMM page size can be obtained on most unices with a
806 * getpagesize() call or deduced from various header files. To make
807 * things simpler, we assume that it is 4K, which is OK for most systems.
808 * It is probably better if this is the native page size, but it doesn't
809 * have to be. In theory, if SYSTEM_PAGE_SIZE is larger than the native page
810 * size, then `POOL_ADDR(p)->arenaindex' could rarely cause a segmentation
811 * violation fault. 4K is apparently OK for all the platforms that python
812 * currently targets.
813 */
814#define SYSTEM_PAGE_SIZE (4 * 1024)
815#define SYSTEM_PAGE_SIZE_MASK (SYSTEM_PAGE_SIZE - 1)
816
817/*
818 * Maximum amount of memory managed by the allocator for small requests.
819 */
820#ifdef WITH_MEMORY_LIMITS
821#ifndef SMALL_MEMORY_LIMIT
822#define SMALL_MEMORY_LIMIT (64 * 1024 * 1024) /* 64 MB -- more? */
823#endif
824#endif
825
826/*
827 * The allocator sub-allocates <Big> blocks of memory (called arenas) aligned
828 * on a page boundary. This is a reserved virtual address space for the
829 * current process (obtained through a malloc()/mmap() call). In no way this
830 * means that the memory arenas will be used entirely. A malloc(<Big>) is
831 * usually an address range reservation for <Big> bytes, unless all pages within
832 * this space are referenced subsequently. So malloc'ing big blocks and not
833 * using them does not mean "wasting memory". It's an addressable range
834 * wastage...
835 *
836 * Arenas are allocated with mmap() on systems supporting anonymous memory
837 * mappings to reduce heap fragmentation.
838 */
839#define ARENA_SIZE (256 << 10) /* 256KB */
840
841#ifdef WITH_MEMORY_LIMITS
842#define MAX_ARENAS (SMALL_MEMORY_LIMIT / ARENA_SIZE)
843#endif
844
845/*
846 * Size of the pools used for small blocks. Should be a power of 2,
847 * between 1K and SYSTEM_PAGE_SIZE, that is: 1k, 2k, 4k.
848 */
849#define POOL_SIZE SYSTEM_PAGE_SIZE /* must be 2^N */
850#define POOL_SIZE_MASK SYSTEM_PAGE_SIZE_MASK
851
852/*
853 * -- End of tunable settings section --
854 */
855
856/*==========================================================================*/
857
Victor Stinner9e87e772017-11-24 12:09:24 +0100858/* When you say memory, my mind reasons in terms of (pointers to) blocks */
859typedef uint8_t block;
860
861/* Pool for small blocks. */
862struct pool_header {
863 union { block *_padding;
864 uint count; } ref; /* number of allocated blocks */
865 block *freeblock; /* pool's free list head */
866 struct pool_header *nextpool; /* next pool of this size class */
867 struct pool_header *prevpool; /* previous pool "" */
868 uint arenaindex; /* index into arenas of base adr */
869 uint szidx; /* block size class index */
870 uint nextoffset; /* bytes to virgin block */
871 uint maxnextoffset; /* largest valid nextoffset */
872};
873
874typedef struct pool_header *poolp;
875
876/* Record keeping for arenas. */
877struct arena_object {
878 /* The address of the arena, as returned by malloc. Note that 0
879 * will never be returned by a successful malloc, and is used
880 * here to mark an arena_object that doesn't correspond to an
881 * allocated arena.
882 */
883 uintptr_t address;
884
885 /* Pool-aligned pointer to the next pool to be carved off. */
886 block* pool_address;
887
888 /* The number of available pools in the arena: free pools + never-
889 * allocated pools.
890 */
891 uint nfreepools;
892
893 /* The total number of pools in the arena, whether or not available. */
894 uint ntotalpools;
895
896 /* Singly-linked list of available pools. */
897 struct pool_header* freepools;
898
899 /* Whenever this arena_object is not associated with an allocated
900 * arena, the nextarena member is used to link all unassociated
901 * arena_objects in the singly-linked `unused_arena_objects` list.
902 * The prevarena member is unused in this case.
903 *
904 * When this arena_object is associated with an allocated arena
905 * with at least one available pool, both members are used in the
906 * doubly-linked `usable_arenas` list, which is maintained in
907 * increasing order of `nfreepools` values.
908 *
909 * Else this arena_object is associated with an allocated arena
910 * all of whose pools are in use. `nextarena` and `prevarena`
911 * are both meaningless in this case.
912 */
913 struct arena_object* nextarena;
914 struct arena_object* prevarena;
915};
916
917#define POOL_OVERHEAD _Py_SIZE_ROUND_UP(sizeof(struct pool_header), ALIGNMENT)
918
919#define DUMMY_SIZE_IDX 0xffff /* size class of newly cached pools */
920
921/* Round pointer P down to the closest pool-aligned address <= P, as a poolp */
922#define POOL_ADDR(P) ((poolp)_Py_ALIGN_DOWN((P), POOL_SIZE))
923
924/* Return total number of blocks in pool of size index I, as a uint. */
925#define NUMBLOCKS(I) ((uint)(POOL_SIZE - POOL_OVERHEAD) / INDEX2SIZE(I))
926
927/*==========================================================================*/
928
929/*
Victor Stinner9e87e772017-11-24 12:09:24 +0100930 * Pool table -- headed, circular, doubly-linked lists of partially used pools.
931
932This is involved. For an index i, usedpools[i+i] is the header for a list of
933all partially used pools holding small blocks with "size class idx" i. So
934usedpools[0] corresponds to blocks of size 8, usedpools[2] to blocks of size
93516, and so on: index 2*i <-> blocks of size (i+1)<<ALIGNMENT_SHIFT.
936
937Pools are carved off an arena's highwater mark (an arena_object's pool_address
938member) as needed. Once carved off, a pool is in one of three states forever
939after:
940
941used == partially used, neither empty nor full
942 At least one block in the pool is currently allocated, and at least one
943 block in the pool is not currently allocated (note this implies a pool
944 has room for at least two blocks).
945 This is a pool's initial state, as a pool is created only when malloc
946 needs space.
947 The pool holds blocks of a fixed size, and is in the circular list headed
948 at usedpools[i] (see above). It's linked to the other used pools of the
949 same size class via the pool_header's nextpool and prevpool members.
950 If all but one block is currently allocated, a malloc can cause a
951 transition to the full state. If all but one block is not currently
952 allocated, a free can cause a transition to the empty state.
953
954full == all the pool's blocks are currently allocated
955 On transition to full, a pool is unlinked from its usedpools[] list.
956 It's not linked to from anything then anymore, and its nextpool and
957 prevpool members are meaningless until it transitions back to used.
958 A free of a block in a full pool puts the pool back in the used state.
959 Then it's linked in at the front of the appropriate usedpools[] list, so
960 that the next allocation for its size class will reuse the freed block.
961
962empty == all the pool's blocks are currently available for allocation
963 On transition to empty, a pool is unlinked from its usedpools[] list,
964 and linked to the front of its arena_object's singly-linked freepools list,
965 via its nextpool member. The prevpool member has no meaning in this case.
966 Empty pools have no inherent size class: the next time a malloc finds
967 an empty list in usedpools[], it takes the first pool off of freepools.
968 If the size class needed happens to be the same as the size class the pool
969 last had, some pool initialization can be skipped.
970
971
972Block Management
973
974Blocks within pools are again carved out as needed. pool->freeblock points to
975the start of a singly-linked list of free blocks within the pool. When a
976block is freed, it's inserted at the front of its pool's freeblock list. Note
977that the available blocks in a pool are *not* linked all together when a pool
978is initialized. Instead only "the first two" (lowest addresses) blocks are
979set up, returning the first such block, and setting pool->freeblock to a
980one-block list holding the second such block. This is consistent with that
981pymalloc strives at all levels (arena, pool, and block) never to touch a piece
982of memory until it's actually needed.
983
984So long as a pool is in the used state, we're certain there *is* a block
985available for allocating, and pool->freeblock is not NULL. If pool->freeblock
986points to the end of the free list before we've carved the entire pool into
987blocks, that means we simply haven't yet gotten to one of the higher-address
988blocks. The offset from the pool_header to the start of "the next" virgin
989block is stored in the pool_header nextoffset member, and the largest value
990of nextoffset that makes sense is stored in the maxnextoffset member when a
991pool is initialized. All the blocks in a pool have been passed out at least
992once when and only when nextoffset > maxnextoffset.
993
994
995Major obscurity: While the usedpools vector is declared to have poolp
996entries, it doesn't really. It really contains two pointers per (conceptual)
997poolp entry, the nextpool and prevpool members of a pool_header. The
998excruciating initialization code below fools C so that
999
1000 usedpool[i+i]
1001
1002"acts like" a genuine poolp, but only so long as you only reference its
1003nextpool and prevpool members. The "- 2*sizeof(block *)" gibberish is
1004compensating for that a pool_header's nextpool and prevpool members
1005immediately follow a pool_header's first two members:
1006
1007 union { block *_padding;
1008 uint count; } ref;
1009 block *freeblock;
1010
1011each of which consume sizeof(block *) bytes. So what usedpools[i+i] really
1012contains is a fudged-up pointer p such that *if* C believes it's a poolp
1013pointer, then p->nextpool and p->prevpool are both p (meaning that the headed
1014circular list is empty).
1015
1016It's unclear why the usedpools setup is so convoluted. It could be to
1017minimize the amount of cache required to hold this heavily-referenced table
1018(which only *needs* the two interpool pointer members of a pool_header). OTOH,
1019referencing code has to remember to "double the index" and doing so isn't
1020free, usedpools[0] isn't a strictly legal pointer, and we're crucially relying
1021on that C doesn't insert any padding anywhere in a pool_header at or before
1022the prevpool member.
1023**************************************************************************** */
1024
1025#define PTA(x) ((poolp )((uint8_t *)&(usedpools[2*(x)]) - 2*sizeof(block *)))
1026#define PT(x) PTA(x), PTA(x)
1027
1028static poolp usedpools[2 * ((NB_SMALL_SIZE_CLASSES + 7) / 8) * 8] = {
1029 PT(0), PT(1), PT(2), PT(3), PT(4), PT(5), PT(6), PT(7)
1030#if NB_SMALL_SIZE_CLASSES > 8
1031 , PT(8), PT(9), PT(10), PT(11), PT(12), PT(13), PT(14), PT(15)
1032#if NB_SMALL_SIZE_CLASSES > 16
1033 , PT(16), PT(17), PT(18), PT(19), PT(20), PT(21), PT(22), PT(23)
1034#if NB_SMALL_SIZE_CLASSES > 24
1035 , PT(24), PT(25), PT(26), PT(27), PT(28), PT(29), PT(30), PT(31)
1036#if NB_SMALL_SIZE_CLASSES > 32
1037 , PT(32), PT(33), PT(34), PT(35), PT(36), PT(37), PT(38), PT(39)
1038#if NB_SMALL_SIZE_CLASSES > 40
1039 , PT(40), PT(41), PT(42), PT(43), PT(44), PT(45), PT(46), PT(47)
1040#if NB_SMALL_SIZE_CLASSES > 48
1041 , PT(48), PT(49), PT(50), PT(51), PT(52), PT(53), PT(54), PT(55)
1042#if NB_SMALL_SIZE_CLASSES > 56
1043 , PT(56), PT(57), PT(58), PT(59), PT(60), PT(61), PT(62), PT(63)
1044#if NB_SMALL_SIZE_CLASSES > 64
1045#error "NB_SMALL_SIZE_CLASSES should be less than 64"
1046#endif /* NB_SMALL_SIZE_CLASSES > 64 */
1047#endif /* NB_SMALL_SIZE_CLASSES > 56 */
1048#endif /* NB_SMALL_SIZE_CLASSES > 48 */
1049#endif /* NB_SMALL_SIZE_CLASSES > 40 */
1050#endif /* NB_SMALL_SIZE_CLASSES > 32 */
1051#endif /* NB_SMALL_SIZE_CLASSES > 24 */
1052#endif /* NB_SMALL_SIZE_CLASSES > 16 */
1053#endif /* NB_SMALL_SIZE_CLASSES > 8 */
1054};
1055
1056/*==========================================================================
1057Arena management.
1058
1059`arenas` is a vector of arena_objects. It contains maxarenas entries, some of
1060which may not be currently used (== they're arena_objects that aren't
1061currently associated with an allocated arena). Note that arenas proper are
1062separately malloc'ed.
1063
1064Prior to Python 2.5, arenas were never free()'ed. Starting with Python 2.5,
1065we do try to free() arenas, and use some mild heuristic strategies to increase
1066the likelihood that arenas eventually can be freed.
1067
1068unused_arena_objects
1069
1070 This is a singly-linked list of the arena_objects that are currently not
1071 being used (no arena is associated with them). Objects are taken off the
1072 head of the list in new_arena(), and are pushed on the head of the list in
1073 PyObject_Free() when the arena is empty. Key invariant: an arena_object
1074 is on this list if and only if its .address member is 0.
1075
1076usable_arenas
1077
1078 This is a doubly-linked list of the arena_objects associated with arenas
1079 that have pools available. These pools are either waiting to be reused,
1080 or have not been used before. The list is sorted to have the most-
1081 allocated arenas first (ascending order based on the nfreepools member).
1082 This means that the next allocation will come from a heavily used arena,
1083 which gives the nearly empty arenas a chance to be returned to the system.
1084 In my unscientific tests this dramatically improved the number of arenas
1085 that could be freed.
1086
1087Note that an arena_object associated with an arena all of whose pools are
1088currently in use isn't on either list.
1089*/
1090
1091/* Array of objects used to track chunks of memory (arenas). */
1092static struct arena_object* arenas = NULL;
1093/* Number of slots currently allocated in the `arenas` vector. */
1094static uint maxarenas = 0;
1095
1096/* The head of the singly-linked, NULL-terminated list of available
1097 * arena_objects.
1098 */
1099static struct arena_object* unused_arena_objects = NULL;
1100
1101/* The head of the doubly-linked, NULL-terminated at each end, list of
1102 * arena_objects associated with arenas that have pools available.
1103 */
1104static struct arena_object* usable_arenas = NULL;
1105
1106/* How many arena_objects do we initially allocate?
1107 * 16 = can allocate 16 arenas = 16 * ARENA_SIZE = 4MB before growing the
1108 * `arenas` vector.
1109 */
1110#define INITIAL_ARENA_OBJECTS 16
1111
1112/* Number of arenas allocated that haven't been free()'d. */
1113static size_t narenas_currently_allocated = 0;
1114
1115/* Total number of times malloc() called to allocate an arena. */
1116static size_t ntimes_arena_allocated = 0;
1117/* High water mark (max value ever seen) for narenas_currently_allocated. */
1118static size_t narenas_highwater = 0;
1119
1120static Py_ssize_t _Py_AllocatedBlocks = 0;
1121
Antoine Pitrouf9d0b122012-12-09 14:28:26 +01001122Py_ssize_t
1123_Py_GetAllocatedBlocks(void)
1124{
Victor Stinner9e87e772017-11-24 12:09:24 +01001125 return _Py_AllocatedBlocks;
Antoine Pitrouf9d0b122012-12-09 14:28:26 +01001126}
1127
1128
Thomas Woutersa9773292006-04-21 09:43:23 +00001129/* Allocate a new arena. If we run out of memory, return NULL. Else
1130 * allocate a new arena, and return the address of an arena_object
1131 * describing the new arena. It's expected that the caller will set
1132 * `usable_arenas` to the return value.
1133 */
1134static struct arena_object*
Tim Petersd97a1c02002-03-30 06:09:22 +00001135new_arena(void)
1136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 struct arena_object* arenaobj;
1138 uint excess; /* number of bytes above pool alignment */
Victor Stinnerba108822012-03-10 00:21:44 +01001139 void *address;
Victor Stinner34be807c2016-03-14 12:04:26 +01001140 static int debug_stats = -1;
Tim Petersd97a1c02002-03-30 06:09:22 +00001141
Victor Stinner34be807c2016-03-14 12:04:26 +01001142 if (debug_stats == -1) {
Serhiy Storchaka4ae06c52017-12-12 13:55:04 +02001143 const char *opt = Py_GETENV("PYTHONMALLOCSTATS");
Victor Stinner34be807c2016-03-14 12:04:26 +01001144 debug_stats = (opt != NULL && *opt != '\0');
1145 }
1146 if (debug_stats)
David Malcolm49526f42012-06-22 14:55:41 -04001147 _PyObject_DebugMallocStats(stderr);
Victor Stinner34be807c2016-03-14 12:04:26 +01001148
Victor Stinner9e87e772017-11-24 12:09:24 +01001149 if (unused_arena_objects == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 uint i;
1151 uint numarenas;
1152 size_t nbytes;
Tim Peters0e871182002-04-13 08:29:14 +00001153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 /* Double the number of arena objects on each allocation.
1155 * Note that it's possible for `numarenas` to overflow.
1156 */
Victor Stinner9e87e772017-11-24 12:09:24 +01001157 numarenas = maxarenas ? maxarenas << 1 : INITIAL_ARENA_OBJECTS;
1158 if (numarenas <= maxarenas)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 return NULL; /* overflow */
Martin v. Löwis5aca8822008-09-11 06:55:48 +00001160#if SIZEOF_SIZE_T <= SIZEOF_INT
Victor Stinner9e87e772017-11-24 12:09:24 +01001161 if (numarenas > SIZE_MAX / sizeof(*arenas))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 return NULL; /* overflow */
Martin v. Löwis5aca8822008-09-11 06:55:48 +00001163#endif
Victor Stinner9e87e772017-11-24 12:09:24 +01001164 nbytes = numarenas * sizeof(*arenas);
1165 arenaobj = (struct arena_object *)PyMem_RawRealloc(arenas, nbytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 if (arenaobj == NULL)
1167 return NULL;
Victor Stinner9e87e772017-11-24 12:09:24 +01001168 arenas = arenaobj;
Thomas Woutersa9773292006-04-21 09:43:23 +00001169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 /* We might need to fix pointers that were copied. However,
1171 * new_arena only gets called when all the pages in the
1172 * previous arenas are full. Thus, there are *no* pointers
1173 * into the old array. Thus, we don't have to worry about
1174 * invalid pointers. Just to be sure, some asserts:
1175 */
Victor Stinner9e87e772017-11-24 12:09:24 +01001176 assert(usable_arenas == NULL);
1177 assert(unused_arena_objects == NULL);
Thomas Woutersa9773292006-04-21 09:43:23 +00001178
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 /* Put the new arenas on the unused_arena_objects list. */
Victor Stinner9e87e772017-11-24 12:09:24 +01001180 for (i = maxarenas; i < numarenas; ++i) {
1181 arenas[i].address = 0; /* mark as unassociated */
1182 arenas[i].nextarena = i < numarenas - 1 ?
1183 &arenas[i+1] : NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 }
Thomas Woutersa9773292006-04-21 09:43:23 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 /* Update globals. */
Victor Stinner9e87e772017-11-24 12:09:24 +01001187 unused_arena_objects = &arenas[maxarenas];
1188 maxarenas = numarenas;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 }
Tim Petersd97a1c02002-03-30 06:09:22 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 /* Take the next available arena object off the head of the list. */
Victor Stinner9e87e772017-11-24 12:09:24 +01001192 assert(unused_arena_objects != NULL);
1193 arenaobj = unused_arena_objects;
1194 unused_arena_objects = arenaobj->nextarena;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 assert(arenaobj->address == 0);
Victor Stinner9e87e772017-11-24 12:09:24 +01001196 address = _PyObject_Arena.alloc(_PyObject_Arena.ctx, ARENA_SIZE);
Victor Stinner0507bf52013-07-07 02:05:46 +02001197 if (address == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 /* The allocation failed: return NULL after putting the
1199 * arenaobj back.
1200 */
Victor Stinner9e87e772017-11-24 12:09:24 +01001201 arenaobj->nextarena = unused_arena_objects;
1202 unused_arena_objects = arenaobj;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 return NULL;
1204 }
Benjamin Peterson5d4b09c2016-09-18 19:24:52 -07001205 arenaobj->address = (uintptr_t)address;
Tim Petersd97a1c02002-03-30 06:09:22 +00001206
Victor Stinner9e87e772017-11-24 12:09:24 +01001207 ++narenas_currently_allocated;
1208 ++ntimes_arena_allocated;
1209 if (narenas_currently_allocated > narenas_highwater)
1210 narenas_highwater = narenas_currently_allocated;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 arenaobj->freepools = NULL;
1212 /* pool_address <- first pool-aligned address in the arena
1213 nfreepools <- number of whole pools that fit after alignment */
Victor Stinner9e87e772017-11-24 12:09:24 +01001214 arenaobj->pool_address = (block*)arenaobj->address;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 arenaobj->nfreepools = ARENA_SIZE / POOL_SIZE;
1216 assert(POOL_SIZE * arenaobj->nfreepools == ARENA_SIZE);
1217 excess = (uint)(arenaobj->address & POOL_SIZE_MASK);
1218 if (excess != 0) {
1219 --arenaobj->nfreepools;
1220 arenaobj->pool_address += POOL_SIZE - excess;
1221 }
1222 arenaobj->ntotalpools = arenaobj->nfreepools;
Thomas Woutersa9773292006-04-21 09:43:23 +00001223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 return arenaobj;
Tim Petersd97a1c02002-03-30 06:09:22 +00001225}
1226
Victor Stinner9ed83c42017-10-31 12:18:10 -07001227
Thomas Woutersa9773292006-04-21 09:43:23 +00001228/*
Benjamin Peterson3924f932016-09-18 19:12:48 -07001229address_in_range(P, POOL)
Thomas Woutersa9773292006-04-21 09:43:23 +00001230
1231Return true if and only if P is an address that was allocated by pymalloc.
1232POOL must be the pool address associated with P, i.e., POOL = POOL_ADDR(P)
1233(the caller is asked to compute this because the macro expands POOL more than
1234once, and for efficiency it's best for the caller to assign POOL_ADDR(P) to a
Benjamin Peterson3924f932016-09-18 19:12:48 -07001235variable and pass the latter to the macro; because address_in_range is
Thomas Woutersa9773292006-04-21 09:43:23 +00001236called on every alloc/realloc/free, micro-efficiency is important here).
1237
1238Tricky: Let B be the arena base address associated with the pool, B =
1239arenas[(POOL)->arenaindex].address. Then P belongs to the arena if and only if
1240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 B <= P < B + ARENA_SIZE
Thomas Woutersa9773292006-04-21 09:43:23 +00001242
1243Subtracting B throughout, this is true iff
1244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 0 <= P-B < ARENA_SIZE
Thomas Woutersa9773292006-04-21 09:43:23 +00001246
1247By using unsigned arithmetic, the "0 <=" half of the test can be skipped.
1248
1249Obscure: A PyMem "free memory" function can call the pymalloc free or realloc
1250before the first arena has been allocated. `arenas` is still NULL in that
1251case. We're relying on that maxarenas is also 0 in that case, so that
1252(POOL)->arenaindex < maxarenas must be false, saving us from trying to index
1253into a NULL arenas.
1254
1255Details: given P and POOL, the arena_object corresponding to P is AO =
1256arenas[(POOL)->arenaindex]. Suppose obmalloc controls P. Then (barring wild
1257stores, etc), POOL is the correct address of P's pool, AO.address is the
1258correct base address of the pool's arena, and P must be within ARENA_SIZE of
1259AO.address. In addition, AO.address is not 0 (no arena can start at address 0
Benjamin Peterson3924f932016-09-18 19:12:48 -07001260(NULL)). Therefore address_in_range correctly reports that obmalloc
Thomas Woutersa9773292006-04-21 09:43:23 +00001261controls P.
1262
1263Now suppose obmalloc does not control P (e.g., P was obtained via a direct
1264call to the system malloc() or realloc()). (POOL)->arenaindex may be anything
1265in this case -- it may even be uninitialized trash. If the trash arenaindex
1266is >= maxarenas, the macro correctly concludes at once that obmalloc doesn't
1267control P.
1268
1269Else arenaindex is < maxarena, and AO is read up. If AO corresponds to an
1270allocated arena, obmalloc controls all the memory in slice AO.address :
1271AO.address+ARENA_SIZE. By case assumption, P is not controlled by obmalloc,
1272so P doesn't lie in that slice, so the macro correctly reports that P is not
1273controlled by obmalloc.
1274
1275Finally, if P is not controlled by obmalloc and AO corresponds to an unused
1276arena_object (one not currently associated with an allocated arena),
1277AO.address is 0, and the second test in the macro reduces to:
1278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 P < ARENA_SIZE
Thomas Woutersa9773292006-04-21 09:43:23 +00001280
1281If P >= ARENA_SIZE (extremely likely), the macro again correctly concludes
1282that P is not controlled by obmalloc. However, if P < ARENA_SIZE, this part
1283of the test still passes, and the third clause (AO.address != 0) is necessary
1284to get the correct result: AO.address is 0 in this case, so the macro
1285correctly reports that P is not controlled by obmalloc (despite that P lies in
1286slice AO.address : AO.address + ARENA_SIZE).
1287
1288Note: The third (AO.address != 0) clause was added in Python 2.5. Before
12892.5, arenas were never free()'ed, and an arenaindex < maxarena always
1290corresponded to a currently-allocated arena, so the "P is not controlled by
1291obmalloc, AO corresponds to an unused arena_object, and P < ARENA_SIZE" case
1292was impossible.
1293
1294Note that the logic is excruciating, and reading up possibly uninitialized
1295memory when P is not controlled by obmalloc (to get at (POOL)->arenaindex)
1296creates problems for some memory debuggers. The overwhelming advantage is
1297that this test determines whether an arbitrary address is controlled by
1298obmalloc in a small constant time, independent of the number of arenas
1299obmalloc controls. Since this test is needed at every entry point, it's
1300extremely desirable that it be this fast.
1301*/
Thomas Woutersa9773292006-04-21 09:43:23 +00001302
Benjamin Peterson3924f932016-09-18 19:12:48 -07001303static bool ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS
1304address_in_range(void *p, poolp pool)
1305{
1306 // Since address_in_range may be reading from memory which was not allocated
1307 // by Python, it is important that pool->arenaindex is read only once, as
1308 // another thread may be concurrently modifying the value without holding
1309 // the GIL. The following dance forces the compiler to read pool->arenaindex
1310 // only once.
1311 uint arenaindex = *((volatile uint *)&pool->arenaindex);
Victor Stinner9e87e772017-11-24 12:09:24 +01001312 return arenaindex < maxarenas &&
1313 (uintptr_t)p - arenas[arenaindex].address < ARENA_SIZE &&
1314 arenas[arenaindex].address != 0;
Benjamin Peterson3924f932016-09-18 19:12:48 -07001315}
Tim Peters338e0102002-04-01 19:23:44 +00001316
Victor Stinner9ed83c42017-10-31 12:18:10 -07001317
Neil Schemenauera35c6882001-02-27 04:45:05 +00001318/*==========================================================================*/
1319
Victor Stinner9ed83c42017-10-31 12:18:10 -07001320/* pymalloc allocator
Neil Schemenauera35c6882001-02-27 04:45:05 +00001321
Victor Stinner9ed83c42017-10-31 12:18:10 -07001322 The basic blocks are ordered by decreasing execution frequency,
1323 which minimizes the number of jumps in the most common cases,
1324 improves branching prediction and instruction scheduling (small
1325 block allocations typically result in a couple of instructions).
1326 Unless the optimizer reorders everything, being too smart...
Neil Schemenauera35c6882001-02-27 04:45:05 +00001327
Victor Stinner9ed83c42017-10-31 12:18:10 -07001328 Return 1 if pymalloc allocated memory and wrote the pointer into *ptr_p.
1329
1330 Return 0 if pymalloc failed to allocate the memory block: on bigger
1331 requests, on error in the code below (as a last chance to serve the request)
1332 or when the max memory limit has been reached. */
1333static int
1334pymalloc_alloc(void *ctx, void **ptr_p, size_t nbytes)
Neil Schemenauera35c6882001-02-27 04:45:05 +00001335{
Victor Stinner9e87e772017-11-24 12:09:24 +01001336 block *bp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 poolp pool;
1338 poolp next;
1339 uint size;
Neil Schemenauera35c6882001-02-27 04:45:05 +00001340
Benjamin Peterson05159c42009-12-03 03:01:27 +00001341#ifdef WITH_VALGRIND
Victor Stinner9ed83c42017-10-31 12:18:10 -07001342 if (UNLIKELY(running_on_valgrind == -1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 running_on_valgrind = RUNNING_ON_VALGRIND;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001344 }
1345 if (UNLIKELY(running_on_valgrind)) {
1346 return 0;
1347 }
Benjamin Peterson05159c42009-12-03 03:01:27 +00001348#endif
1349
Victor Stinner9ed83c42017-10-31 12:18:10 -07001350 if (nbytes == 0) {
1351 return 0;
1352 }
1353 if (nbytes > SMALL_REQUEST_THRESHOLD) {
1354 return 0;
1355 }
T. Wouters06bb4872017-03-31 10:10:19 -07001356
Victor Stinner9ed83c42017-10-31 12:18:10 -07001357 /*
1358 * Most frequent paths first
1359 */
1360 size = (uint)(nbytes - 1) >> ALIGNMENT_SHIFT;
Victor Stinner9e87e772017-11-24 12:09:24 +01001361 pool = usedpools[size + size];
Victor Stinner9ed83c42017-10-31 12:18:10 -07001362 if (pool != pool->nextpool) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 /*
Victor Stinner9ed83c42017-10-31 12:18:10 -07001364 * There is a used pool for this size class.
1365 * Pick up the head block of its free list.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 */
Victor Stinner9ed83c42017-10-31 12:18:10 -07001367 ++pool->ref.count;
1368 bp = pool->freeblock;
1369 assert(bp != NULL);
Victor Stinner9e87e772017-11-24 12:09:24 +01001370 if ((pool->freeblock = *(block **)bp) != NULL) {
Victor Stinner9ed83c42017-10-31 12:18:10 -07001371 goto success;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 }
Thomas Woutersa9773292006-04-21 09:43:23 +00001373
Victor Stinner9ed83c42017-10-31 12:18:10 -07001374 /*
1375 * Reached the end of the free list, try to extend it.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 */
Victor Stinner9ed83c42017-10-31 12:18:10 -07001377 if (pool->nextoffset <= pool->maxnextoffset) {
1378 /* There is room for another block. */
Victor Stinner9e87e772017-11-24 12:09:24 +01001379 pool->freeblock = (block*)pool +
Victor Stinner9ed83c42017-10-31 12:18:10 -07001380 pool->nextoffset;
1381 pool->nextoffset += INDEX2SIZE(size);
Victor Stinner9e87e772017-11-24 12:09:24 +01001382 *(block **)(pool->freeblock) = NULL;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001383 goto success;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 }
Thomas Woutersa9773292006-04-21 09:43:23 +00001385
Victor Stinner9ed83c42017-10-31 12:18:10 -07001386 /* Pool is full, unlink from used pools. */
1387 next = pool->nextpool;
1388 pool = pool->prevpool;
1389 next->prevpool = pool;
1390 pool->nextpool = next;
1391 goto success;
1392 }
Thomas Woutersa9773292006-04-21 09:43:23 +00001393
Victor Stinner9ed83c42017-10-31 12:18:10 -07001394 /* There isn't a pool of the right size class immediately
1395 * available: use a free pool.
1396 */
Victor Stinner9e87e772017-11-24 12:09:24 +01001397 if (usable_arenas == NULL) {
Victor Stinner9ed83c42017-10-31 12:18:10 -07001398 /* No arena has a free pool: allocate a new arena. */
1399#ifdef WITH_MEMORY_LIMITS
Victor Stinner9e87e772017-11-24 12:09:24 +01001400 if (narenas_currently_allocated >= MAX_ARENAS) {
Victor Stinner9ed83c42017-10-31 12:18:10 -07001401 goto failed;
1402 }
1403#endif
Victor Stinner9e87e772017-11-24 12:09:24 +01001404 usable_arenas = new_arena();
1405 if (usable_arenas == NULL) {
Victor Stinner9ed83c42017-10-31 12:18:10 -07001406 goto failed;
1407 }
Victor Stinner9e87e772017-11-24 12:09:24 +01001408 usable_arenas->nextarena =
1409 usable_arenas->prevarena = NULL;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001410 }
Victor Stinner9e87e772017-11-24 12:09:24 +01001411 assert(usable_arenas->address != 0);
Victor Stinner9ed83c42017-10-31 12:18:10 -07001412
1413 /* Try to get a cached free pool. */
Victor Stinner9e87e772017-11-24 12:09:24 +01001414 pool = usable_arenas->freepools;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001415 if (pool != NULL) {
1416 /* Unlink from cached pools. */
Victor Stinner9e87e772017-11-24 12:09:24 +01001417 usable_arenas->freepools = pool->nextpool;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001418
1419 /* This arena already had the smallest nfreepools
1420 * value, so decreasing nfreepools doesn't change
1421 * that, and we don't need to rearrange the
1422 * usable_arenas list. However, if the arena has
1423 * become wholly allocated, we need to remove its
1424 * arena_object from usable_arenas.
1425 */
Victor Stinner9e87e772017-11-24 12:09:24 +01001426 --usable_arenas->nfreepools;
1427 if (usable_arenas->nfreepools == 0) {
Victor Stinner9ed83c42017-10-31 12:18:10 -07001428 /* Wholly allocated: remove. */
Victor Stinner9e87e772017-11-24 12:09:24 +01001429 assert(usable_arenas->freepools == NULL);
1430 assert(usable_arenas->nextarena == NULL ||
1431 usable_arenas->nextarena->prevarena ==
1432 usable_arenas);
Victor Stinner9ed83c42017-10-31 12:18:10 -07001433
Victor Stinner9e87e772017-11-24 12:09:24 +01001434 usable_arenas = usable_arenas->nextarena;
1435 if (usable_arenas != NULL) {
1436 usable_arenas->prevarena = NULL;
1437 assert(usable_arenas->address != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 }
1439 }
Victor Stinner9ed83c42017-10-31 12:18:10 -07001440 else {
1441 /* nfreepools > 0: it must be that freepools
1442 * isn't NULL, or that we haven't yet carved
1443 * off all the arena's pools for the first
1444 * time.
1445 */
Victor Stinner9e87e772017-11-24 12:09:24 +01001446 assert(usable_arenas->freepools != NULL ||
1447 usable_arenas->pool_address <=
1448 (block*)usable_arenas->address +
Victor Stinner9ed83c42017-10-31 12:18:10 -07001449 ARENA_SIZE - POOL_SIZE);
1450 }
Thomas Woutersa9773292006-04-21 09:43:23 +00001451
Victor Stinner9ed83c42017-10-31 12:18:10 -07001452 init_pool:
1453 /* Frontlink to used pools. */
Victor Stinner9e87e772017-11-24 12:09:24 +01001454 next = usedpools[size + size]; /* == prev */
Victor Stinner9ed83c42017-10-31 12:18:10 -07001455 pool->nextpool = next;
1456 pool->prevpool = next;
1457 next->nextpool = pool;
1458 next->prevpool = pool;
1459 pool->ref.count = 1;
1460 if (pool->szidx == size) {
1461 /* Luckily, this pool last contained blocks
1462 * of the same size class, so its header
1463 * and free list are already initialized.
1464 */
1465 bp = pool->freeblock;
1466 assert(bp != NULL);
Victor Stinner9e87e772017-11-24 12:09:24 +01001467 pool->freeblock = *(block **)bp;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001468 goto success;
1469 }
1470 /*
1471 * Initialize the pool header, set up the free list to
1472 * contain just the second block, and return the first
1473 * block.
1474 */
1475 pool->szidx = size;
1476 size = INDEX2SIZE(size);
Victor Stinner9e87e772017-11-24 12:09:24 +01001477 bp = (block *)pool + POOL_OVERHEAD;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001478 pool->nextoffset = POOL_OVERHEAD + (size << 1);
1479 pool->maxnextoffset = POOL_SIZE - size;
1480 pool->freeblock = bp + size;
Victor Stinner9e87e772017-11-24 12:09:24 +01001481 *(block **)(pool->freeblock) = NULL;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001482 goto success;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 }
Neil Schemenauera35c6882001-02-27 04:45:05 +00001484
Victor Stinner9ed83c42017-10-31 12:18:10 -07001485 /* Carve off a new pool. */
Victor Stinner9e87e772017-11-24 12:09:24 +01001486 assert(usable_arenas->nfreepools > 0);
1487 assert(usable_arenas->freepools == NULL);
1488 pool = (poolp)usable_arenas->pool_address;
1489 assert((block*)pool <= (block*)usable_arenas->address +
Victor Stinner9ed83c42017-10-31 12:18:10 -07001490 ARENA_SIZE - POOL_SIZE);
Victor Stinner9e87e772017-11-24 12:09:24 +01001491 pool->arenaindex = (uint)(usable_arenas - arenas);
1492 assert(&arenas[pool->arenaindex] == usable_arenas);
Victor Stinner9ed83c42017-10-31 12:18:10 -07001493 pool->szidx = DUMMY_SIZE_IDX;
Victor Stinner9e87e772017-11-24 12:09:24 +01001494 usable_arenas->pool_address += POOL_SIZE;
1495 --usable_arenas->nfreepools;
Neil Schemenauera35c6882001-02-27 04:45:05 +00001496
Victor Stinner9e87e772017-11-24 12:09:24 +01001497 if (usable_arenas->nfreepools == 0) {
1498 assert(usable_arenas->nextarena == NULL ||
1499 usable_arenas->nextarena->prevarena ==
1500 usable_arenas);
Victor Stinner9ed83c42017-10-31 12:18:10 -07001501 /* Unlink the arena: it is completely allocated. */
Victor Stinner9e87e772017-11-24 12:09:24 +01001502 usable_arenas = usable_arenas->nextarena;
1503 if (usable_arenas != NULL) {
1504 usable_arenas->prevarena = NULL;
1505 assert(usable_arenas->address != 0);
Victor Stinner9ed83c42017-10-31 12:18:10 -07001506 }
Antoine Pitrouf9d0b122012-12-09 14:28:26 +01001507 }
Victor Stinner9ed83c42017-10-31 12:18:10 -07001508
1509 goto init_pool;
1510
1511success:
Victor Stinner9ed83c42017-10-31 12:18:10 -07001512 assert(bp != NULL);
1513 *ptr_p = (void *)bp;
1514 return 1;
1515
1516failed:
Victor Stinner9ed83c42017-10-31 12:18:10 -07001517 return 0;
Neil Schemenauera35c6882001-02-27 04:45:05 +00001518}
1519
Victor Stinner9ed83c42017-10-31 12:18:10 -07001520
Victor Stinnerdb067af2014-05-02 22:31:14 +02001521static void *
1522_PyObject_Malloc(void *ctx, size_t nbytes)
1523{
Victor Stinner9ed83c42017-10-31 12:18:10 -07001524 void* ptr;
1525 if (pymalloc_alloc(ctx, &ptr, nbytes)) {
Victor Stinner9e87e772017-11-24 12:09:24 +01001526 _Py_AllocatedBlocks++;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001527 return ptr;
1528 }
1529
1530 ptr = PyMem_RawMalloc(nbytes);
1531 if (ptr != NULL) {
Victor Stinner9e87e772017-11-24 12:09:24 +01001532 _Py_AllocatedBlocks++;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001533 }
1534 return ptr;
Victor Stinnerdb067af2014-05-02 22:31:14 +02001535}
1536
Victor Stinner9ed83c42017-10-31 12:18:10 -07001537
Victor Stinnerdb067af2014-05-02 22:31:14 +02001538static void *
1539_PyObject_Calloc(void *ctx, size_t nelem, size_t elsize)
1540{
Victor Stinner9ed83c42017-10-31 12:18:10 -07001541 void* ptr;
1542
1543 assert(elsize == 0 || nelem <= (size_t)PY_SSIZE_T_MAX / elsize);
1544 size_t nbytes = nelem * elsize;
1545
1546 if (pymalloc_alloc(ctx, &ptr, nbytes)) {
1547 memset(ptr, 0, nbytes);
Victor Stinner9e87e772017-11-24 12:09:24 +01001548 _Py_AllocatedBlocks++;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001549 return ptr;
1550 }
1551
1552 ptr = PyMem_RawCalloc(nelem, elsize);
1553 if (ptr != NULL) {
Victor Stinner9e87e772017-11-24 12:09:24 +01001554 _Py_AllocatedBlocks++;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001555 }
1556 return ptr;
Victor Stinnerdb067af2014-05-02 22:31:14 +02001557}
1558
Neil Schemenauera35c6882001-02-27 04:45:05 +00001559
Victor Stinner9ed83c42017-10-31 12:18:10 -07001560/* Free a memory block allocated by pymalloc_alloc().
1561 Return 1 if it was freed.
1562 Return 0 if the block was not allocated by pymalloc_alloc(). */
1563static int
1564pymalloc_free(void *ctx, void *p)
Neil Schemenauera35c6882001-02-27 04:45:05 +00001565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 poolp pool;
Victor Stinner9e87e772017-11-24 12:09:24 +01001567 block *lastfree;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 poolp next, prev;
1569 uint size;
Neil Schemenauera35c6882001-02-27 04:45:05 +00001570
Victor Stinner9ed83c42017-10-31 12:18:10 -07001571 assert(p != NULL);
Antoine Pitrouf9d0b122012-12-09 14:28:26 +01001572
Benjamin Peterson05159c42009-12-03 03:01:27 +00001573#ifdef WITH_VALGRIND
Victor Stinner9ed83c42017-10-31 12:18:10 -07001574 if (UNLIKELY(running_on_valgrind > 0)) {
1575 return 0;
1576 }
Benjamin Peterson05159c42009-12-03 03:01:27 +00001577#endif
1578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 pool = POOL_ADDR(p);
Victor Stinner9ed83c42017-10-31 12:18:10 -07001580 if (!address_in_range(p, pool)) {
1581 return 0;
1582 }
1583 /* We allocated this address. */
Thomas Woutersa9773292006-04-21 09:43:23 +00001584
Victor Stinner9ed83c42017-10-31 12:18:10 -07001585 /* Link p to the start of the pool's freeblock list. Since
1586 * the pool had at least the p block outstanding, the pool
1587 * wasn't empty (so it's already in a usedpools[] list, or
1588 * was full and is in no list -- it's not in the freeblocks
1589 * list in any case).
1590 */
1591 assert(pool->ref.count > 0); /* else it was empty */
Victor Stinner9e87e772017-11-24 12:09:24 +01001592 *(block **)p = lastfree = pool->freeblock;
1593 pool->freeblock = (block *)p;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001594 if (!lastfree) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 /* Pool was full, so doesn't currently live in any list:
1596 * link it to the front of the appropriate usedpools[] list.
1597 * This mimics LRU pool usage for new allocations and
1598 * targets optimal filling when several pools contain
1599 * blocks of the same size class.
1600 */
1601 --pool->ref.count;
1602 assert(pool->ref.count > 0); /* else the pool is empty */
1603 size = pool->szidx;
Victor Stinner9e87e772017-11-24 12:09:24 +01001604 next = usedpools[size + size];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 prev = next->prevpool;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 /* insert pool before next: prev <-> pool <-> next */
1608 pool->nextpool = next;
1609 pool->prevpool = prev;
1610 next->prevpool = pool;
1611 prev->nextpool = pool;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001612 goto success;
1613 }
1614
1615 struct arena_object* ao;
1616 uint nf; /* ao->nfreepools */
1617
1618 /* freeblock wasn't NULL, so the pool wasn't full,
1619 * and the pool is in a usedpools[] list.
1620 */
1621 if (--pool->ref.count != 0) {
1622 /* pool isn't empty: leave it in usedpools */
1623 goto success;
1624 }
1625 /* Pool is now empty: unlink from usedpools, and
1626 * link to the front of freepools. This ensures that
1627 * previously freed pools will be allocated later
1628 * (being not referenced, they are perhaps paged out).
1629 */
1630 next = pool->nextpool;
1631 prev = pool->prevpool;
1632 next->prevpool = prev;
1633 prev->nextpool = next;
1634
1635 /* Link the pool to freepools. This is a singly-linked
1636 * list, and pool->prevpool isn't used there.
1637 */
Victor Stinner9e87e772017-11-24 12:09:24 +01001638 ao = &arenas[pool->arenaindex];
Victor Stinner9ed83c42017-10-31 12:18:10 -07001639 pool->nextpool = ao->freepools;
1640 ao->freepools = pool;
1641 nf = ++ao->nfreepools;
1642
1643 /* All the rest is arena management. We just freed
1644 * a pool, and there are 4 cases for arena mgmt:
1645 * 1. If all the pools are free, return the arena to
1646 * the system free().
1647 * 2. If this is the only free pool in the arena,
1648 * add the arena back to the `usable_arenas` list.
1649 * 3. If the "next" arena has a smaller count of free
1650 * pools, we have to "slide this arena right" to
1651 * restore that usable_arenas is sorted in order of
1652 * nfreepools.
1653 * 4. Else there's nothing more to do.
1654 */
1655 if (nf == ao->ntotalpools) {
1656 /* Case 1. First unlink ao from usable_arenas.
1657 */
1658 assert(ao->prevarena == NULL ||
1659 ao->prevarena->address != 0);
1660 assert(ao ->nextarena == NULL ||
1661 ao->nextarena->address != 0);
1662
1663 /* Fix the pointer in the prevarena, or the
1664 * usable_arenas pointer.
1665 */
1666 if (ao->prevarena == NULL) {
Victor Stinner9e87e772017-11-24 12:09:24 +01001667 usable_arenas = ao->nextarena;
1668 assert(usable_arenas == NULL ||
1669 usable_arenas->address != 0);
Victor Stinner9ed83c42017-10-31 12:18:10 -07001670 }
1671 else {
1672 assert(ao->prevarena->nextarena == ao);
1673 ao->prevarena->nextarena =
1674 ao->nextarena;
1675 }
1676 /* Fix the pointer in the nextarena. */
1677 if (ao->nextarena != NULL) {
1678 assert(ao->nextarena->prevarena == ao);
1679 ao->nextarena->prevarena =
1680 ao->prevarena;
1681 }
1682 /* Record that this arena_object slot is
1683 * available to be reused.
1684 */
Victor Stinner9e87e772017-11-24 12:09:24 +01001685 ao->nextarena = unused_arena_objects;
1686 unused_arena_objects = ao;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001687
1688 /* Free the entire arena. */
Victor Stinner9e87e772017-11-24 12:09:24 +01001689 _PyObject_Arena.free(_PyObject_Arena.ctx,
Victor Stinner9ed83c42017-10-31 12:18:10 -07001690 (void *)ao->address, ARENA_SIZE);
1691 ao->address = 0; /* mark unassociated */
Victor Stinner9e87e772017-11-24 12:09:24 +01001692 --narenas_currently_allocated;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001693
1694 goto success;
1695 }
1696
1697 if (nf == 1) {
1698 /* Case 2. Put ao at the head of
1699 * usable_arenas. Note that because
1700 * ao->nfreepools was 0 before, ao isn't
1701 * currently on the usable_arenas list.
1702 */
Victor Stinner9e87e772017-11-24 12:09:24 +01001703 ao->nextarena = usable_arenas;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001704 ao->prevarena = NULL;
Victor Stinner9e87e772017-11-24 12:09:24 +01001705 if (usable_arenas)
1706 usable_arenas->prevarena = ao;
1707 usable_arenas = ao;
1708 assert(usable_arenas->address != 0);
Victor Stinner9ed83c42017-10-31 12:18:10 -07001709
1710 goto success;
1711 }
1712
1713 /* If this arena is now out of order, we need to keep
1714 * the list sorted. The list is kept sorted so that
1715 * the "most full" arenas are used first, which allows
1716 * the nearly empty arenas to be completely freed. In
1717 * a few un-scientific tests, it seems like this
1718 * approach allowed a lot more memory to be freed.
1719 */
1720 if (ao->nextarena == NULL ||
1721 nf <= ao->nextarena->nfreepools) {
1722 /* Case 4. Nothing to do. */
1723 goto success;
1724 }
1725 /* Case 3: We have to move the arena towards the end
1726 * of the list, because it has more free pools than
1727 * the arena to its right.
1728 * First unlink ao from usable_arenas.
1729 */
1730 if (ao->prevarena != NULL) {
1731 /* ao isn't at the head of the list */
1732 assert(ao->prevarena->nextarena == ao);
1733 ao->prevarena->nextarena = ao->nextarena;
1734 }
1735 else {
1736 /* ao is at the head of the list */
Victor Stinner9e87e772017-11-24 12:09:24 +01001737 assert(usable_arenas == ao);
1738 usable_arenas = ao->nextarena;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001739 }
1740 ao->nextarena->prevarena = ao->prevarena;
1741
1742 /* Locate the new insertion point by iterating over
1743 * the list, using our nextarena pointer.
1744 */
1745 while (ao->nextarena != NULL && nf > ao->nextarena->nfreepools) {
1746 ao->prevarena = ao->nextarena;
1747 ao->nextarena = ao->nextarena->nextarena;
1748 }
1749
1750 /* Insert ao at this point. */
1751 assert(ao->nextarena == NULL || ao->prevarena == ao->nextarena->prevarena);
1752 assert(ao->prevarena->nextarena == ao->nextarena);
1753
1754 ao->prevarena->nextarena = ao;
1755 if (ao->nextarena != NULL) {
1756 ao->nextarena->prevarena = ao;
1757 }
1758
1759 /* Verify that the swaps worked. */
1760 assert(ao->nextarena == NULL || nf <= ao->nextarena->nfreepools);
1761 assert(ao->prevarena == NULL || nf > ao->prevarena->nfreepools);
1762 assert(ao->nextarena == NULL || ao->nextarena->prevarena == ao);
Victor Stinner9e87e772017-11-24 12:09:24 +01001763 assert((usable_arenas == ao && ao->prevarena == NULL)
Victor Stinner9ed83c42017-10-31 12:18:10 -07001764 || ao->prevarena->nextarena == ao);
1765
1766 goto success;
1767
1768success:
Victor Stinner9ed83c42017-10-31 12:18:10 -07001769 return 1;
1770}
1771
1772
1773static void
1774_PyObject_Free(void *ctx, void *p)
1775{
1776 /* PyObject_Free(NULL) has no effect */
1777 if (p == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 return;
1779 }
Neil Schemenauera35c6882001-02-27 04:45:05 +00001780
Victor Stinner9e87e772017-11-24 12:09:24 +01001781 _Py_AllocatedBlocks--;
Victor Stinner9ed83c42017-10-31 12:18:10 -07001782 if (!pymalloc_free(ctx, p)) {
1783 /* pymalloc didn't allocate this address */
1784 PyMem_RawFree(p);
1785 }
Neil Schemenauera35c6882001-02-27 04:45:05 +00001786}
1787
Neil Schemenauera35c6882001-02-27 04:45:05 +00001788
Victor Stinner9ed83c42017-10-31 12:18:10 -07001789/* pymalloc realloc.
1790
1791 If nbytes==0, then as the Python docs promise, we do not treat this like
1792 free(p), and return a non-NULL result.
1793
1794 Return 1 if pymalloc reallocated memory and wrote the new pointer into
1795 newptr_p.
1796
1797 Return 0 if pymalloc didn't allocated p. */
1798static int
1799pymalloc_realloc(void *ctx, void **newptr_p, void *p, size_t nbytes)
Neil Schemenauera35c6882001-02-27 04:45:05 +00001800{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 void *bp;
1802 poolp pool;
1803 size_t size;
Neil Schemenauera35c6882001-02-27 04:45:05 +00001804
Victor Stinner9ed83c42017-10-31 12:18:10 -07001805 assert(p != NULL);
Georg Brandld492ad82008-07-23 16:13:07 +00001806
Benjamin Peterson05159c42009-12-03 03:01:27 +00001807#ifdef WITH_VALGRIND
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 /* Treat running_on_valgrind == -1 the same as 0 */
Victor Stinner9ed83c42017-10-31 12:18:10 -07001809 if (UNLIKELY(running_on_valgrind > 0)) {
1810 return 0;
1811 }
Benjamin Peterson05159c42009-12-03 03:01:27 +00001812#endif
1813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 pool = POOL_ADDR(p);
Victor Stinner9ed83c42017-10-31 12:18:10 -07001815 if (!address_in_range(p, pool)) {
1816 /* pymalloc is not managing this block.
1817
1818 If nbytes <= SMALL_REQUEST_THRESHOLD, it's tempting to try to take
1819 over this block. However, if we do, we need to copy the valid data
1820 from the C-managed block to one of our blocks, and there's no
1821 portable way to know how much of the memory space starting at p is
1822 valid.
1823
1824 As bug 1185883 pointed out the hard way, it's possible that the
1825 C-managed block is "at the end" of allocated VM space, so that a
1826 memory fault can occur if we try to copy nbytes bytes starting at p.
1827 Instead we punt: let C continue to manage this block. */
1828 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 }
Victor Stinner9ed83c42017-10-31 12:18:10 -07001830
1831 /* pymalloc is in charge of this block */
1832 size = INDEX2SIZE(pool->szidx);
1833 if (nbytes <= size) {
1834 /* The block is staying the same or shrinking.
1835
1836 If it's shrinking, there's a tradeoff: it costs cycles to copy the
1837 block to a smaller size class, but it wastes memory not to copy it.
1838
1839 The compromise here is to copy on shrink only if at least 25% of
1840 size can be shaved off. */
1841 if (4 * nbytes > 3 * size) {
1842 /* It's the same, or shrinking and new/old > 3/4. */
1843 *newptr_p = p;
1844 return 1;
1845 }
1846 size = nbytes;
1847 }
1848
1849 bp = _PyObject_Malloc(ctx, nbytes);
1850 if (bp != NULL) {
1851 memcpy(bp, p, size);
1852 _PyObject_Free(ctx, p);
1853 }
1854 *newptr_p = bp;
1855 return 1;
1856}
1857
1858
1859static void *
1860_PyObject_Realloc(void *ctx, void *ptr, size_t nbytes)
1861{
1862 void *ptr2;
1863
1864 if (ptr == NULL) {
1865 return _PyObject_Malloc(ctx, nbytes);
1866 }
1867
1868 if (pymalloc_realloc(ctx, &ptr2, ptr, nbytes)) {
1869 return ptr2;
1870 }
1871
1872 return PyMem_RawRealloc(ptr, nbytes);
Neil Schemenauera35c6882001-02-27 04:45:05 +00001873}
1874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875#else /* ! WITH_PYMALLOC */
Tim Petersddea2082002-03-23 10:03:50 +00001876
1877/*==========================================================================*/
Neil Schemenauerd2560cd2002-04-12 03:10:20 +00001878/* pymalloc not enabled: Redirect the entry points to malloc. These will
1879 * only be used by extensions that are compiled with pymalloc enabled. */
Tim Peters62c06ba2002-03-23 22:28:18 +00001880
Antoine Pitrou92840532012-12-17 23:05:59 +01001881Py_ssize_t
1882_Py_GetAllocatedBlocks(void)
1883{
1884 return 0;
1885}
1886
Tim Peters1221c0a2002-03-23 00:20:15 +00001887#endif /* WITH_PYMALLOC */
1888
Victor Stinner34be807c2016-03-14 12:04:26 +01001889
Tim Petersddea2082002-03-23 10:03:50 +00001890/*==========================================================================*/
Tim Peters62c06ba2002-03-23 22:28:18 +00001891/* A x-platform debugging allocator. This doesn't manage memory directly,
1892 * it wraps a real allocator, adding extra debugging info to the memory blocks.
1893 */
Tim Petersddea2082002-03-23 10:03:50 +00001894
Tim Petersf6fb5012002-04-12 07:38:53 +00001895/* Special bytes broadcast into debug memory blocks at appropriate times.
1896 * Strings of these are unlikely to be valid addresses, floats, ints or
1897 * 7-bit ASCII.
1898 */
1899#undef CLEANBYTE
1900#undef DEADBYTE
1901#undef FORBIDDENBYTE
1902#define CLEANBYTE 0xCB /* clean (newly allocated) memory */
Tim Peters889f61d2002-07-10 19:29:49 +00001903#define DEADBYTE 0xDB /* dead (newly freed) memory */
Tim Petersf6fb5012002-04-12 07:38:53 +00001904#define FORBIDDENBYTE 0xFB /* untouchable bytes at each end of a block */
Tim Petersddea2082002-03-23 10:03:50 +00001905
Victor Stinner9e87e772017-11-24 12:09:24 +01001906static size_t serialno = 0; /* incremented on each debug {m,re}alloc */
1907
Tim Peterse0850172002-03-24 00:34:21 +00001908/* serialno is always incremented via calling this routine. The point is
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001909 * to supply a single place to set a breakpoint.
1910 */
Tim Peterse0850172002-03-24 00:34:21 +00001911static void
Neil Schemenauerbd02b142002-03-28 21:05:38 +00001912bumpserialno(void)
Tim Peterse0850172002-03-24 00:34:21 +00001913{
Victor Stinner9e87e772017-11-24 12:09:24 +01001914 ++serialno;
Tim Peterse0850172002-03-24 00:34:21 +00001915}
1916
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001917#define SST SIZEOF_SIZE_T
Tim Peterse0850172002-03-24 00:34:21 +00001918
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001919/* Read sizeof(size_t) bytes at p as a big-endian size_t. */
1920static size_t
1921read_size_t(const void *p)
Tim Petersddea2082002-03-23 10:03:50 +00001922{
Benjamin Peterson19517e42016-09-18 19:22:22 -07001923 const uint8_t *q = (const uint8_t *)p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 size_t result = *q++;
1925 int i;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 for (i = SST; --i > 0; ++q)
1928 result = (result << 8) | *q;
1929 return result;
Tim Petersddea2082002-03-23 10:03:50 +00001930}
1931
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001932/* Write n as a big-endian size_t, MSB at address p, LSB at
1933 * p + sizeof(size_t) - 1.
1934 */
Tim Petersddea2082002-03-23 10:03:50 +00001935static void
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001936write_size_t(void *p, size_t n)
Tim Petersddea2082002-03-23 10:03:50 +00001937{
Benjamin Peterson19517e42016-09-18 19:22:22 -07001938 uint8_t *q = (uint8_t *)p + SST - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 int i;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 for (i = SST; --i >= 0; --q) {
Benjamin Peterson19517e42016-09-18 19:22:22 -07001942 *q = (uint8_t)(n & 0xff);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 n >>= 8;
1944 }
Tim Petersddea2082002-03-23 10:03:50 +00001945}
1946
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001947/* Let S = sizeof(size_t). The debug malloc asks for 4*S extra bytes and
1948 fills them with useful stuff, here calling the underlying malloc's result p:
Tim Petersddea2082002-03-23 10:03:50 +00001949
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001950p[0: S]
1951 Number of bytes originally asked for. This is a size_t, big-endian (easier
1952 to read in a memory dump).
Georg Brandl7cba5fd2013-09-25 09:04:23 +02001953p[S]
Tim Petersdf099f52013-09-19 21:06:37 -05001954 API ID. See PEP 445. This is a character, but seems undocumented.
1955p[S+1: 2*S]
Tim Petersf6fb5012002-04-12 07:38:53 +00001956 Copies of FORBIDDENBYTE. Used to catch under- writes and reads.
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001957p[2*S: 2*S+n]
Tim Petersf6fb5012002-04-12 07:38:53 +00001958 The requested memory, filled with copies of CLEANBYTE.
Tim Petersddea2082002-03-23 10:03:50 +00001959 Used to catch reference to uninitialized memory.
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001960 &p[2*S] is returned. Note that this is 8-byte aligned if pymalloc
Tim Petersddea2082002-03-23 10:03:50 +00001961 handled the request itself.
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001962p[2*S+n: 2*S+n+S]
Tim Petersf6fb5012002-04-12 07:38:53 +00001963 Copies of FORBIDDENBYTE. Used to catch over- writes and reads.
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001964p[2*S+n+S: 2*S+n+2*S]
Victor Stinner0507bf52013-07-07 02:05:46 +02001965 A serial number, incremented by 1 on each call to _PyMem_DebugMalloc
1966 and _PyMem_DebugRealloc.
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001967 This is a big-endian size_t.
Tim Petersddea2082002-03-23 10:03:50 +00001968 If "bad memory" is detected later, the serial number gives an
1969 excellent way to set a breakpoint on the next run, to capture the
1970 instant at which this block was passed out.
1971*/
1972
Victor Stinner0507bf52013-07-07 02:05:46 +02001973static void *
Victor Stinnerc4aec362016-03-14 22:26:53 +01001974_PyMem_DebugRawAlloc(int use_calloc, void *ctx, size_t nbytes)
Kristján Valur Jónssonae4cfb12009-09-28 13:45:02 +00001975{
Victor Stinner0507bf52013-07-07 02:05:46 +02001976 debug_alloc_api_t *api = (debug_alloc_api_t *)ctx;
Serhiy Storchaka3cc4c532017-11-07 12:46:42 +02001977 uint8_t *p; /* base address of malloc'ed pad block */
Victor Stinner9ed83c42017-10-31 12:18:10 -07001978 uint8_t *data; /* p + 2*SST == pointer to data bytes */
1979 uint8_t *tail; /* data + nbytes == pointer to tail pad bytes */
1980 size_t total; /* 2 * SST + nbytes + 2 * SST */
1981
1982 if (nbytes > (size_t)PY_SSIZE_T_MAX - 4 * SST) {
1983 /* integer overflow: can't represent total as a Py_ssize_t */
1984 return NULL;
1985 }
1986 total = nbytes + 4 * SST;
1987
1988 /* Layout: [SSSS IFFF CCCC...CCCC FFFF NNNN]
1989 * ^--- p ^--- data ^--- tail
1990 S: nbytes stored as size_t
1991 I: API identifier (1 byte)
1992 F: Forbidden bytes (size_t - 1 bytes before, size_t bytes after)
1993 C: Clean bytes used later to store actual data
1994 N: Serial number stored as size_t */
1995
1996 if (use_calloc) {
1997 p = (uint8_t *)api->alloc.calloc(api->alloc.ctx, 1, total);
1998 }
1999 else {
2000 p = (uint8_t *)api->alloc.malloc(api->alloc.ctx, total);
2001 }
2002 if (p == NULL) {
2003 return NULL;
2004 }
2005 data = p + 2*SST;
Tim Petersddea2082002-03-23 10:03:50 +00002006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 bumpserialno();
Tim Petersddea2082002-03-23 10:03:50 +00002008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002009 /* at p, write size (SST bytes), id (1 byte), pad (SST-1 bytes) */
2010 write_size_t(p, nbytes);
Benjamin Peterson19517e42016-09-18 19:22:22 -07002011 p[SST] = (uint8_t)api->api_id;
Victor Stinner0507bf52013-07-07 02:05:46 +02002012 memset(p + SST + 1, FORBIDDENBYTE, SST-1);
Tim Petersddea2082002-03-23 10:03:50 +00002013
Victor Stinner9ed83c42017-10-31 12:18:10 -07002014 if (nbytes > 0 && !use_calloc) {
2015 memset(data, CLEANBYTE, nbytes);
2016 }
Tim Petersddea2082002-03-23 10:03:50 +00002017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 /* at tail, write pad (SST bytes) and serialno (SST bytes) */
Victor Stinner9ed83c42017-10-31 12:18:10 -07002019 tail = data + nbytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 memset(tail, FORBIDDENBYTE, SST);
Victor Stinner9e87e772017-11-24 12:09:24 +01002021 write_size_t(tail + SST, serialno);
Tim Petersddea2082002-03-23 10:03:50 +00002022
Victor Stinner9ed83c42017-10-31 12:18:10 -07002023 return data;
Tim Petersddea2082002-03-23 10:03:50 +00002024}
2025
Victor Stinnerdb067af2014-05-02 22:31:14 +02002026static void *
Victor Stinnerc4aec362016-03-14 22:26:53 +01002027_PyMem_DebugRawMalloc(void *ctx, size_t nbytes)
Victor Stinnerdb067af2014-05-02 22:31:14 +02002028{
Victor Stinnerc4aec362016-03-14 22:26:53 +01002029 return _PyMem_DebugRawAlloc(0, ctx, nbytes);
Victor Stinnerdb067af2014-05-02 22:31:14 +02002030}
2031
2032static void *
Victor Stinnerc4aec362016-03-14 22:26:53 +01002033_PyMem_DebugRawCalloc(void *ctx, size_t nelem, size_t elsize)
Victor Stinnerdb067af2014-05-02 22:31:14 +02002034{
2035 size_t nbytes;
Victor Stinner9ed83c42017-10-31 12:18:10 -07002036 assert(elsize == 0 || nelem <= (size_t)PY_SSIZE_T_MAX / elsize);
Victor Stinnerdb067af2014-05-02 22:31:14 +02002037 nbytes = nelem * elsize;
Victor Stinnerc4aec362016-03-14 22:26:53 +01002038 return _PyMem_DebugRawAlloc(1, ctx, nbytes);
Victor Stinnerdb067af2014-05-02 22:31:14 +02002039}
2040
Victor Stinner9ed83c42017-10-31 12:18:10 -07002041
Victor Stinner82af0b62018-10-23 17:39:40 +02002042/* Heuristic checking if the memory has been freed. Rely on the debug hooks on
2043 Python memory allocators which fills the memory with DEADBYTE (0xDB) when
2044 memory is deallocated. */
2045int
2046_PyMem_IsFreed(void *ptr, size_t size)
2047{
2048 unsigned char *bytes = ptr;
2049 for (size_t i=0; i < size; i++) {
2050 if (bytes[i] != DEADBYTE) {
2051 return 0;
2052 }
2053 }
2054 return 1;
2055}
2056
2057
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002058/* The debug free first checks the 2*SST bytes on each end for sanity (in
Kristján Valur Jónssonae4cfb12009-09-28 13:45:02 +00002059 particular, that the FORBIDDENBYTEs with the api ID are still intact).
Tim Petersf6fb5012002-04-12 07:38:53 +00002060 Then fills the original bytes with DEADBYTE.
Tim Petersddea2082002-03-23 10:03:50 +00002061 Then calls the underlying free.
2062*/
Victor Stinner0507bf52013-07-07 02:05:46 +02002063static void
Victor Stinnerc4aec362016-03-14 22:26:53 +01002064_PyMem_DebugRawFree(void *ctx, void *p)
Tim Petersddea2082002-03-23 10:03:50 +00002065{
Victor Stinner9ed83c42017-10-31 12:18:10 -07002066 /* PyMem_Free(NULL) has no effect */
2067 if (p == NULL) {
2068 return;
2069 }
2070
Victor Stinner0507bf52013-07-07 02:05:46 +02002071 debug_alloc_api_t *api = (debug_alloc_api_t *)ctx;
Benjamin Peterson19517e42016-09-18 19:22:22 -07002072 uint8_t *q = (uint8_t *)p - 2*SST; /* address returned from malloc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 size_t nbytes;
Tim Petersddea2082002-03-23 10:03:50 +00002074
Victor Stinner0507bf52013-07-07 02:05:46 +02002075 _PyMem_DebugCheckAddress(api->api_id, p);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 nbytes = read_size_t(q);
Victor Stinner9ed83c42017-10-31 12:18:10 -07002077 nbytes += 4 * SST;
2078 memset(q, DEADBYTE, nbytes);
Victor Stinner0507bf52013-07-07 02:05:46 +02002079 api->alloc.free(api->alloc.ctx, q);
Tim Petersddea2082002-03-23 10:03:50 +00002080}
2081
Victor Stinner9ed83c42017-10-31 12:18:10 -07002082
Victor Stinner0507bf52013-07-07 02:05:46 +02002083static void *
Victor Stinnerc4aec362016-03-14 22:26:53 +01002084_PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
Tim Petersddea2082002-03-23 10:03:50 +00002085{
Victor Stinner9ed83c42017-10-31 12:18:10 -07002086 if (p == NULL) {
2087 return _PyMem_DebugRawAlloc(0, ctx, nbytes);
2088 }
2089
Victor Stinner0507bf52013-07-07 02:05:46 +02002090 debug_alloc_api_t *api = (debug_alloc_api_t *)ctx;
Serhiy Storchaka3cc4c532017-11-07 12:46:42 +02002091 uint8_t *head; /* base address of malloc'ed pad block */
2092 uint8_t *data; /* pointer to data bytes */
2093 uint8_t *r;
Victor Stinner9ed83c42017-10-31 12:18:10 -07002094 uint8_t *tail; /* data + nbytes == pointer to tail pad bytes */
2095 size_t total; /* 2 * SST + nbytes + 2 * SST */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 size_t original_nbytes;
Victor Stinner9e87e772017-11-24 12:09:24 +01002097 size_t block_serialno;
Serhiy Storchaka3cc4c532017-11-07 12:46:42 +02002098#define ERASED_SIZE 64
2099 uint8_t save[2*ERASED_SIZE]; /* A copy of erased bytes. */
Tim Petersddea2082002-03-23 10:03:50 +00002100
Victor Stinner0507bf52013-07-07 02:05:46 +02002101 _PyMem_DebugCheckAddress(api->api_id, p);
Victor Stinner9ed83c42017-10-31 12:18:10 -07002102
Serhiy Storchaka3cc4c532017-11-07 12:46:42 +02002103 data = (uint8_t *)p;
2104 head = data - 2*SST;
2105 original_nbytes = read_size_t(head);
Victor Stinner9ed83c42017-10-31 12:18:10 -07002106 if (nbytes > (size_t)PY_SSIZE_T_MAX - 4*SST) {
2107 /* integer overflow: can't represent total as a Py_ssize_t */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 return NULL;
Victor Stinner9ed83c42017-10-31 12:18:10 -07002109 }
2110 total = nbytes + 4*SST;
Tim Petersddea2082002-03-23 10:03:50 +00002111
Serhiy Storchaka3cc4c532017-11-07 12:46:42 +02002112 tail = data + original_nbytes;
Victor Stinner9e87e772017-11-24 12:09:24 +01002113 block_serialno = read_size_t(tail + SST);
Serhiy Storchaka3cc4c532017-11-07 12:46:42 +02002114 /* Mark the header, the trailer, ERASED_SIZE bytes at the begin and
2115 ERASED_SIZE bytes at the end as dead and save the copy of erased bytes.
2116 */
2117 if (original_nbytes <= sizeof(save)) {
2118 memcpy(save, data, original_nbytes);
2119 memset(data - 2*SST, DEADBYTE, original_nbytes + 4*SST);
2120 }
2121 else {
2122 memcpy(save, data, ERASED_SIZE);
2123 memset(head, DEADBYTE, ERASED_SIZE + 2*SST);
2124 memcpy(&save[ERASED_SIZE], tail - ERASED_SIZE, ERASED_SIZE);
2125 memset(tail - ERASED_SIZE, DEADBYTE, ERASED_SIZE + 2*SST);
Victor Stinner9ed83c42017-10-31 12:18:10 -07002126 }
Tim Peters85cc1c42002-04-12 08:52:50 +00002127
Serhiy Storchaka3cc4c532017-11-07 12:46:42 +02002128 /* Resize and add decorations. */
2129 r = (uint8_t *)api->alloc.realloc(api->alloc.ctx, head, total);
2130 if (r == NULL) {
2131 nbytes = original_nbytes;
Victor Stinner9ed83c42017-10-31 12:18:10 -07002132 }
Serhiy Storchaka3cc4c532017-11-07 12:46:42 +02002133 else {
2134 head = r;
2135 bumpserialno();
Victor Stinner9e87e772017-11-24 12:09:24 +01002136 block_serialno = serialno;
Serhiy Storchaka3cc4c532017-11-07 12:46:42 +02002137 }
2138
2139 write_size_t(head, nbytes);
2140 head[SST] = (uint8_t)api->api_id;
2141 memset(head + SST + 1, FORBIDDENBYTE, SST-1);
2142 data = head + 2*SST;
Victor Stinnerc4266362013-07-09 00:44:43 +02002143
Victor Stinner9ed83c42017-10-31 12:18:10 -07002144 tail = data + nbytes;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 memset(tail, FORBIDDENBYTE, SST);
Victor Stinner9e87e772017-11-24 12:09:24 +01002146 write_size_t(tail + SST, block_serialno);
Serhiy Storchaka3cc4c532017-11-07 12:46:42 +02002147
2148 /* Restore saved bytes. */
2149 if (original_nbytes <= sizeof(save)) {
2150 memcpy(data, save, Py_MIN(nbytes, original_nbytes));
2151 }
2152 else {
2153 size_t i = original_nbytes - ERASED_SIZE;
2154 memcpy(data, save, Py_MIN(nbytes, ERASED_SIZE));
2155 if (nbytes > i) {
2156 memcpy(data + i, &save[ERASED_SIZE],
2157 Py_MIN(nbytes - i, ERASED_SIZE));
2158 }
2159 }
2160
2161 if (r == NULL) {
2162 return NULL;
2163 }
Tim Peters85cc1c42002-04-12 08:52:50 +00002164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 if (nbytes > original_nbytes) {
2166 /* growing: mark new extra memory clean */
Serhiy Storchaka3cc4c532017-11-07 12:46:42 +02002167 memset(data + original_nbytes, CLEANBYTE, nbytes - original_nbytes);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 }
Tim Peters85cc1c42002-04-12 08:52:50 +00002169
Victor Stinner9ed83c42017-10-31 12:18:10 -07002170 return data;
Tim Petersddea2082002-03-23 10:03:50 +00002171}
2172
Victor Stinnerc4aec362016-03-14 22:26:53 +01002173static void
2174_PyMem_DebugCheckGIL(void)
2175{
Victor Stinnerc4aec362016-03-14 22:26:53 +01002176 if (!PyGILState_Check())
2177 Py_FatalError("Python memory allocator called "
2178 "without holding the GIL");
Victor Stinnerc4aec362016-03-14 22:26:53 +01002179}
2180
2181static void *
2182_PyMem_DebugMalloc(void *ctx, size_t nbytes)
2183{
2184 _PyMem_DebugCheckGIL();
2185 return _PyMem_DebugRawMalloc(ctx, nbytes);
2186}
2187
2188static void *
2189_PyMem_DebugCalloc(void *ctx, size_t nelem, size_t elsize)
2190{
2191 _PyMem_DebugCheckGIL();
2192 return _PyMem_DebugRawCalloc(ctx, nelem, elsize);
2193}
2194
Victor Stinner9ed83c42017-10-31 12:18:10 -07002195
Victor Stinnerc4aec362016-03-14 22:26:53 +01002196static void
2197_PyMem_DebugFree(void *ctx, void *ptr)
2198{
2199 _PyMem_DebugCheckGIL();
Victor Stinner0aed3a42016-03-23 11:30:43 +01002200 _PyMem_DebugRawFree(ctx, ptr);
Victor Stinnerc4aec362016-03-14 22:26:53 +01002201}
2202
Victor Stinner9ed83c42017-10-31 12:18:10 -07002203
Victor Stinnerc4aec362016-03-14 22:26:53 +01002204static void *
2205_PyMem_DebugRealloc(void *ctx, void *ptr, size_t nbytes)
2206{
2207 _PyMem_DebugCheckGIL();
2208 return _PyMem_DebugRawRealloc(ctx, ptr, nbytes);
2209}
2210
Tim Peters7ccfadf2002-04-01 06:04:21 +00002211/* Check the forbidden bytes on both ends of the memory allocated for p.
Neil Schemenauerd2560cd2002-04-12 03:10:20 +00002212 * If anything is wrong, print info to stderr via _PyObject_DebugDumpAddress,
Tim Peters7ccfadf2002-04-01 06:04:21 +00002213 * and call Py_FatalError to kill the program.
Kristján Valur Jónssonae4cfb12009-09-28 13:45:02 +00002214 * The API id, is also checked.
Tim Peters7ccfadf2002-04-01 06:04:21 +00002215 */
Victor Stinner0507bf52013-07-07 02:05:46 +02002216static void
2217_PyMem_DebugCheckAddress(char api, const void *p)
Tim Petersddea2082002-03-23 10:03:50 +00002218{
Benjamin Peterson19517e42016-09-18 19:22:22 -07002219 const uint8_t *q = (const uint8_t *)p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 char msgbuf[64];
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02002221 const char *msg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 size_t nbytes;
Benjamin Peterson19517e42016-09-18 19:22:22 -07002223 const uint8_t *tail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 int i;
2225 char id;
Tim Petersddea2082002-03-23 10:03:50 +00002226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 if (p == NULL) {
2228 msg = "didn't expect a NULL pointer";
2229 goto error;
2230 }
Tim Petersddea2082002-03-23 10:03:50 +00002231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 /* Check the API id */
2233 id = (char)q[-SST];
2234 if (id != api) {
2235 msg = msgbuf;
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02002236 snprintf(msgbuf, sizeof(msgbuf), "bad ID: Allocated using API '%c', verified using API '%c'", id, api);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 msgbuf[sizeof(msgbuf)-1] = 0;
2238 goto error;
2239 }
Kristján Valur Jónssonae4cfb12009-09-28 13:45:02 +00002240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 /* Check the stuff at the start of p first: if there's underwrite
2242 * corruption, the number-of-bytes field may be nuts, and checking
2243 * the tail could lead to a segfault then.
2244 */
2245 for (i = SST-1; i >= 1; --i) {
2246 if (*(q-i) != FORBIDDENBYTE) {
2247 msg = "bad leading pad byte";
2248 goto error;
2249 }
2250 }
Tim Petersddea2082002-03-23 10:03:50 +00002251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 nbytes = read_size_t(q - 2*SST);
2253 tail = q + nbytes;
2254 for (i = 0; i < SST; ++i) {
2255 if (tail[i] != FORBIDDENBYTE) {
2256 msg = "bad trailing pad byte";
2257 goto error;
2258 }
2259 }
Tim Petersddea2082002-03-23 10:03:50 +00002260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 return;
Tim Petersd1139e02002-03-28 07:32:11 +00002262
2263error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 _PyObject_DebugDumpAddress(p);
2265 Py_FatalError(msg);
Tim Petersddea2082002-03-23 10:03:50 +00002266}
2267
Tim Peters7ccfadf2002-04-01 06:04:21 +00002268/* Display info to stderr about the memory block at p. */
Victor Stinner0507bf52013-07-07 02:05:46 +02002269static void
Neil Schemenauerd2560cd2002-04-12 03:10:20 +00002270_PyObject_DebugDumpAddress(const void *p)
Tim Petersddea2082002-03-23 10:03:50 +00002271{
Benjamin Peterson19517e42016-09-18 19:22:22 -07002272 const uint8_t *q = (const uint8_t *)p;
2273 const uint8_t *tail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 size_t nbytes, serial;
2275 int i;
2276 int ok;
2277 char id;
Tim Petersddea2082002-03-23 10:03:50 +00002278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 fprintf(stderr, "Debug memory block at address p=%p:", p);
2280 if (p == NULL) {
2281 fprintf(stderr, "\n");
2282 return;
2283 }
2284 id = (char)q[-SST];
2285 fprintf(stderr, " API '%c'\n", id);
Tim Petersddea2082002-03-23 10:03:50 +00002286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 nbytes = read_size_t(q - 2*SST);
2288 fprintf(stderr, " %" PY_FORMAT_SIZE_T "u bytes originally "
2289 "requested\n", nbytes);
Tim Petersddea2082002-03-23 10:03:50 +00002290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 /* In case this is nuts, check the leading pad bytes first. */
2292 fprintf(stderr, " The %d pad bytes at p-%d are ", SST-1, SST-1);
2293 ok = 1;
2294 for (i = 1; i <= SST-1; ++i) {
2295 if (*(q-i) != FORBIDDENBYTE) {
2296 ok = 0;
2297 break;
2298 }
2299 }
2300 if (ok)
2301 fputs("FORBIDDENBYTE, as expected.\n", stderr);
2302 else {
2303 fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n",
2304 FORBIDDENBYTE);
2305 for (i = SST-1; i >= 1; --i) {
Benjamin Peterson19517e42016-09-18 19:22:22 -07002306 const uint8_t byte = *(q-i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 fprintf(stderr, " at p-%d: 0x%02x", i, byte);
2308 if (byte != FORBIDDENBYTE)
2309 fputs(" *** OUCH", stderr);
2310 fputc('\n', stderr);
2311 }
Tim Peters449b5a82002-04-28 06:14:45 +00002312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 fputs(" Because memory is corrupted at the start, the "
2314 "count of bytes requested\n"
2315 " may be bogus, and checking the trailing pad "
2316 "bytes may segfault.\n", stderr);
2317 }
Tim Petersddea2082002-03-23 10:03:50 +00002318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 tail = q + nbytes;
2320 fprintf(stderr, " The %d pad bytes at tail=%p are ", SST, tail);
2321 ok = 1;
2322 for (i = 0; i < SST; ++i) {
2323 if (tail[i] != FORBIDDENBYTE) {
2324 ok = 0;
2325 break;
2326 }
2327 }
2328 if (ok)
2329 fputs("FORBIDDENBYTE, as expected.\n", stderr);
2330 else {
2331 fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n",
Stefan Krah735bb122010-11-26 10:54:09 +00002332 FORBIDDENBYTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 for (i = 0; i < SST; ++i) {
Benjamin Peterson19517e42016-09-18 19:22:22 -07002334 const uint8_t byte = tail[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002335 fprintf(stderr, " at tail+%d: 0x%02x",
Stefan Krah735bb122010-11-26 10:54:09 +00002336 i, byte);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337 if (byte != FORBIDDENBYTE)
2338 fputs(" *** OUCH", stderr);
2339 fputc('\n', stderr);
2340 }
2341 }
Tim Petersddea2082002-03-23 10:03:50 +00002342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 serial = read_size_t(tail + SST);
2344 fprintf(stderr, " The block was made by call #%" PY_FORMAT_SIZE_T
2345 "u to debug malloc/realloc.\n", serial);
Tim Petersddea2082002-03-23 10:03:50 +00002346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 if (nbytes > 0) {
2348 i = 0;
2349 fputs(" Data at p:", stderr);
2350 /* print up to 8 bytes at the start */
2351 while (q < tail && i < 8) {
2352 fprintf(stderr, " %02x", *q);
2353 ++i;
2354 ++q;
2355 }
2356 /* and up to 8 at the end */
2357 if (q < tail) {
2358 if (tail - q > 8) {
2359 fputs(" ...", stderr);
2360 q = tail - 8;
2361 }
2362 while (q < tail) {
2363 fprintf(stderr, " %02x", *q);
2364 ++q;
2365 }
2366 }
2367 fputc('\n', stderr);
2368 }
Victor Stinner0611c262016-03-15 22:22:13 +01002369 fputc('\n', stderr);
2370
2371 fflush(stderr);
2372 _PyMem_DumpTraceback(fileno(stderr), p);
Tim Petersddea2082002-03-23 10:03:50 +00002373}
2374
David Malcolm49526f42012-06-22 14:55:41 -04002375
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002376static size_t
David Malcolm49526f42012-06-22 14:55:41 -04002377printone(FILE *out, const char* msg, size_t value)
Tim Peters16bcb6b2002-04-05 05:45:31 +00002378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 int i, k;
2380 char buf[100];
2381 size_t origvalue = value;
Tim Peters16bcb6b2002-04-05 05:45:31 +00002382
David Malcolm49526f42012-06-22 14:55:41 -04002383 fputs(msg, out);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002384 for (i = (int)strlen(msg); i < 35; ++i)
David Malcolm49526f42012-06-22 14:55:41 -04002385 fputc(' ', out);
2386 fputc('=', out);
Tim Peters49f26812002-04-06 01:45:35 +00002387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 /* Write the value with commas. */
2389 i = 22;
2390 buf[i--] = '\0';
2391 buf[i--] = '\n';
2392 k = 3;
2393 do {
2394 size_t nextvalue = value / 10;
Benjamin Peterson2dba1ee2013-02-20 16:54:30 -05002395 unsigned int digit = (unsigned int)(value - nextvalue * 10);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 value = nextvalue;
2397 buf[i--] = (char)(digit + '0');
2398 --k;
2399 if (k == 0 && value && i >= 0) {
2400 k = 3;
2401 buf[i--] = ',';
2402 }
2403 } while (value && i >= 0);
Tim Peters49f26812002-04-06 01:45:35 +00002404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 while (i >= 0)
2406 buf[i--] = ' ';
David Malcolm49526f42012-06-22 14:55:41 -04002407 fputs(buf, out);
Tim Peters49f26812002-04-06 01:45:35 +00002408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 return origvalue;
Tim Peters16bcb6b2002-04-05 05:45:31 +00002410}
2411
David Malcolm49526f42012-06-22 14:55:41 -04002412void
2413_PyDebugAllocatorStats(FILE *out,
2414 const char *block_name, int num_blocks, size_t sizeof_block)
2415{
2416 char buf1[128];
2417 char buf2[128];
2418 PyOS_snprintf(buf1, sizeof(buf1),
Tim Peterseaa3bcc2013-09-05 22:57:04 -05002419 "%d %ss * %" PY_FORMAT_SIZE_T "d bytes each",
David Malcolm49526f42012-06-22 14:55:41 -04002420 num_blocks, block_name, sizeof_block);
2421 PyOS_snprintf(buf2, sizeof(buf2),
2422 "%48s ", buf1);
2423 (void)printone(out, buf2, num_blocks * sizeof_block);
2424}
2425
Victor Stinner34be807c2016-03-14 12:04:26 +01002426
David Malcolm49526f42012-06-22 14:55:41 -04002427#ifdef WITH_PYMALLOC
2428
Victor Stinner34be807c2016-03-14 12:04:26 +01002429#ifdef Py_DEBUG
2430/* Is target in the list? The list is traversed via the nextpool pointers.
2431 * The list may be NULL-terminated, or circular. Return 1 if target is in
2432 * list, else 0.
2433 */
2434static int
2435pool_is_in_list(const poolp target, poolp list)
2436{
2437 poolp origlist = list;
2438 assert(target != NULL);
2439 if (list == NULL)
2440 return 0;
2441 do {
2442 if (target == list)
2443 return 1;
2444 list = list->nextpool;
2445 } while (list != NULL && list != origlist);
2446 return 0;
2447}
2448#endif
2449
David Malcolm49526f42012-06-22 14:55:41 -04002450/* Print summary info to "out" about the state of pymalloc's structures.
Tim Peters08d82152002-04-18 22:25:03 +00002451 * In Py_DEBUG mode, also perform some expensive internal consistency
2452 * checks.
Victor Stinner6bf992a2017-12-06 17:26:10 +01002453 *
2454 * Return 0 if the memory debug hooks are not installed or no statistics was
Leo Ariasc3d95082018-02-03 18:36:10 -06002455 * written into out, return 1 otherwise.
Tim Peters08d82152002-04-18 22:25:03 +00002456 */
Victor Stinner6bf992a2017-12-06 17:26:10 +01002457int
David Malcolm49526f42012-06-22 14:55:41 -04002458_PyObject_DebugMallocStats(FILE *out)
Tim Peters7ccfadf2002-04-01 06:04:21 +00002459{
Victor Stinner6bf992a2017-12-06 17:26:10 +01002460 if (!_PyMem_PymallocEnabled()) {
2461 return 0;
2462 }
2463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 uint i;
2465 const uint numclasses = SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT;
2466 /* # of pools, allocated blocks, and free blocks per class index */
2467 size_t numpools[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT];
2468 size_t numblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT];
2469 size_t numfreeblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT];
2470 /* total # of allocated bytes in used and full pools */
2471 size_t allocated_bytes = 0;
2472 /* total # of available bytes in used pools */
2473 size_t available_bytes = 0;
2474 /* # of free pools + pools not yet carved out of current arena */
2475 uint numfreepools = 0;
2476 /* # of bytes for arena alignment padding */
2477 size_t arena_alignment = 0;
2478 /* # of bytes in used and full pools used for pool_headers */
2479 size_t pool_header_bytes = 0;
2480 /* # of bytes in used and full pools wasted due to quantization,
2481 * i.e. the necessarily leftover space at the ends of used and
2482 * full pools.
2483 */
2484 size_t quantization = 0;
2485 /* # of arenas actually allocated. */
2486 size_t narenas = 0;
2487 /* running total -- should equal narenas * ARENA_SIZE */
2488 size_t total;
2489 char buf[128];
Tim Peters7ccfadf2002-04-01 06:04:21 +00002490
David Malcolm49526f42012-06-22 14:55:41 -04002491 fprintf(out, "Small block threshold = %d, in %u size classes.\n",
Stefan Krah735bb122010-11-26 10:54:09 +00002492 SMALL_REQUEST_THRESHOLD, numclasses);
Tim Peters7ccfadf2002-04-01 06:04:21 +00002493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 for (i = 0; i < numclasses; ++i)
2495 numpools[i] = numblocks[i] = numfreeblocks[i] = 0;
Tim Peters7ccfadf2002-04-01 06:04:21 +00002496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 /* Because full pools aren't linked to from anything, it's easiest
2498 * to march over all the arenas. If we're lucky, most of the memory
2499 * will be living in full pools -- would be a shame to miss them.
2500 */
Victor Stinner9e87e772017-11-24 12:09:24 +01002501 for (i = 0; i < maxarenas; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 uint j;
Victor Stinner9e87e772017-11-24 12:09:24 +01002503 uintptr_t base = arenas[i].address;
Thomas Woutersa9773292006-04-21 09:43:23 +00002504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 /* Skip arenas which are not allocated. */
Victor Stinner9e87e772017-11-24 12:09:24 +01002506 if (arenas[i].address == (uintptr_t)NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 continue;
2508 narenas += 1;
Thomas Woutersa9773292006-04-21 09:43:23 +00002509
Victor Stinner9e87e772017-11-24 12:09:24 +01002510 numfreepools += arenas[i].nfreepools;
Tim Peters7ccfadf2002-04-01 06:04:21 +00002511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 /* round up to pool alignment */
Benjamin Peterson5d4b09c2016-09-18 19:24:52 -07002513 if (base & (uintptr_t)POOL_SIZE_MASK) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 arena_alignment += POOL_SIZE;
Benjamin Peterson5d4b09c2016-09-18 19:24:52 -07002515 base &= ~(uintptr_t)POOL_SIZE_MASK;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 base += POOL_SIZE;
2517 }
Tim Peters7ccfadf2002-04-01 06:04:21 +00002518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 /* visit every pool in the arena */
Victor Stinner9e87e772017-11-24 12:09:24 +01002520 assert(base <= (uintptr_t) arenas[i].pool_address);
2521 for (j = 0; base < (uintptr_t) arenas[i].pool_address;
Benjamin Peterson19517e42016-09-18 19:22:22 -07002522 ++j, base += POOL_SIZE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 poolp p = (poolp)base;
2524 const uint sz = p->szidx;
2525 uint freeblocks;
Tim Peters08d82152002-04-18 22:25:03 +00002526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 if (p->ref.count == 0) {
2528 /* currently unused */
Victor Stinner34be807c2016-03-14 12:04:26 +01002529#ifdef Py_DEBUG
Victor Stinner9e87e772017-11-24 12:09:24 +01002530 assert(pool_is_in_list(p, arenas[i].freepools));
Victor Stinner34be807c2016-03-14 12:04:26 +01002531#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 continue;
2533 }
2534 ++numpools[sz];
2535 numblocks[sz] += p->ref.count;
2536 freeblocks = NUMBLOCKS(sz) - p->ref.count;
2537 numfreeblocks[sz] += freeblocks;
Tim Peters08d82152002-04-18 22:25:03 +00002538#ifdef Py_DEBUG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539 if (freeblocks > 0)
Victor Stinner9e87e772017-11-24 12:09:24 +01002540 assert(pool_is_in_list(p, usedpools[sz + sz]));
Tim Peters08d82152002-04-18 22:25:03 +00002541#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002542 }
2543 }
Victor Stinner9e87e772017-11-24 12:09:24 +01002544 assert(narenas == narenas_currently_allocated);
Tim Peters7ccfadf2002-04-01 06:04:21 +00002545
David Malcolm49526f42012-06-22 14:55:41 -04002546 fputc('\n', out);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 fputs("class size num pools blocks in use avail blocks\n"
2548 "----- ---- --------- ------------- ------------\n",
David Malcolm49526f42012-06-22 14:55:41 -04002549 out);
Tim Peters7ccfadf2002-04-01 06:04:21 +00002550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002551 for (i = 0; i < numclasses; ++i) {
2552 size_t p = numpools[i];
2553 size_t b = numblocks[i];
2554 size_t f = numfreeblocks[i];
2555 uint size = INDEX2SIZE(i);
2556 if (p == 0) {
2557 assert(b == 0 && f == 0);
2558 continue;
2559 }
David Malcolm49526f42012-06-22 14:55:41 -04002560 fprintf(out, "%5u %6u "
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 "%11" PY_FORMAT_SIZE_T "u "
2562 "%15" PY_FORMAT_SIZE_T "u "
2563 "%13" PY_FORMAT_SIZE_T "u\n",
Stefan Krah735bb122010-11-26 10:54:09 +00002564 i, size, p, b, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 allocated_bytes += b * size;
2566 available_bytes += f * size;
2567 pool_header_bytes += p * POOL_OVERHEAD;
2568 quantization += p * ((POOL_SIZE - POOL_OVERHEAD) % size);
2569 }
David Malcolm49526f42012-06-22 14:55:41 -04002570 fputc('\n', out);
Victor Stinner34be807c2016-03-14 12:04:26 +01002571 if (_PyMem_DebugEnabled())
Victor Stinner9e87e772017-11-24 12:09:24 +01002572 (void)printone(out, "# times object malloc called", serialno);
2573 (void)printone(out, "# arenas allocated total", ntimes_arena_allocated);
2574 (void)printone(out, "# arenas reclaimed", ntimes_arena_allocated - narenas);
2575 (void)printone(out, "# arenas highwater mark", narenas_highwater);
David Malcolm49526f42012-06-22 14:55:41 -04002576 (void)printone(out, "# arenas allocated current", narenas);
Thomas Woutersa9773292006-04-21 09:43:23 +00002577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 PyOS_snprintf(buf, sizeof(buf),
2579 "%" PY_FORMAT_SIZE_T "u arenas * %d bytes/arena",
2580 narenas, ARENA_SIZE);
David Malcolm49526f42012-06-22 14:55:41 -04002581 (void)printone(out, buf, narenas * ARENA_SIZE);
Tim Peters16bcb6b2002-04-05 05:45:31 +00002582
David Malcolm49526f42012-06-22 14:55:41 -04002583 fputc('\n', out);
Tim Peters16bcb6b2002-04-05 05:45:31 +00002584
David Malcolm49526f42012-06-22 14:55:41 -04002585 total = printone(out, "# bytes in allocated blocks", allocated_bytes);
2586 total += printone(out, "# bytes in available blocks", available_bytes);
Tim Peters49f26812002-04-06 01:45:35 +00002587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 PyOS_snprintf(buf, sizeof(buf),
2589 "%u unused pools * %d bytes", numfreepools, POOL_SIZE);
David Malcolm49526f42012-06-22 14:55:41 -04002590 total += printone(out, buf, (size_t)numfreepools * POOL_SIZE);
Tim Peters16bcb6b2002-04-05 05:45:31 +00002591
David Malcolm49526f42012-06-22 14:55:41 -04002592 total += printone(out, "# bytes lost to pool headers", pool_header_bytes);
2593 total += printone(out, "# bytes lost to quantization", quantization);
2594 total += printone(out, "# bytes lost to arena alignment", arena_alignment);
2595 (void)printone(out, "Total", total);
Victor Stinner6bf992a2017-12-06 17:26:10 +01002596 return 1;
Tim Peters7ccfadf2002-04-01 06:04:21 +00002597}
2598
David Malcolm49526f42012-06-22 14:55:41 -04002599#endif /* #ifdef WITH_PYMALLOC */