Tim Peters | 1221c0a | 2002-03-23 00:20:15 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 3 | /* Python's malloc wrappers (see pymem.h) */ |
| 4 | |
| 5 | #ifdef PYMALLOC_DEBUG /* WITH_PYMALLOC && PYMALLOC_DEBUG */ |
| 6 | /* Forward declaration */ |
| 7 | static void* _PyMem_DebugMalloc(void *ctx, size_t size); |
| 8 | static void _PyMem_DebugFree(void *ctx, void *p); |
| 9 | static void* _PyMem_DebugRealloc(void *ctx, void *ptr, size_t size); |
| 10 | |
| 11 | static void _PyObject_DebugDumpAddress(const void *p); |
| 12 | static void _PyMem_DebugCheckAddress(char api_id, const void *p); |
| 13 | #endif |
| 14 | |
Nick Coghlan | 6ba64f4 | 2013-09-29 00:28:55 +1000 | [diff] [blame] | 15 | #if defined(__has_feature) /* Clang */ |
| 16 | #if __has_feature(address_sanitizer) /* is ASAN enabled? */ |
| 17 | #define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS \ |
| 18 | __attribute__((no_address_safety_analysis)) \ |
| 19 | __attribute__ ((noinline)) |
| 20 | #else |
| 21 | #define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS |
| 22 | #endif |
| 23 | #else |
| 24 | #if defined(__SANITIZE_ADDRESS__) /* GCC 4.8.x, is ASAN enabled? */ |
| 25 | #define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS \ |
| 26 | __attribute__((no_address_safety_analysis)) \ |
| 27 | __attribute__ ((noinline)) |
| 28 | #else |
| 29 | #define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS |
| 30 | #endif |
| 31 | #endif |
| 32 | |
Tim Peters | 1221c0a | 2002-03-23 00:20:15 +0000 | [diff] [blame] | 33 | #ifdef WITH_PYMALLOC |
| 34 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 35 | #ifdef MS_WINDOWS |
| 36 | # include <windows.h> |
| 37 | #elif defined(HAVE_MMAP) |
| 38 | # include <sys/mman.h> |
| 39 | # ifdef MAP_ANONYMOUS |
| 40 | # define ARENAS_USE_MMAP |
| 41 | # endif |
Antoine Pitrou | 6f26be0 | 2011-05-03 18:18:59 +0200 | [diff] [blame] | 42 | #endif |
| 43 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 44 | /* Forward declaration */ |
| 45 | static void* _PyObject_Malloc(void *ctx, size_t size); |
| 46 | static void _PyObject_Free(void *ctx, void *p); |
| 47 | static void* _PyObject_Realloc(void *ctx, void *ptr, size_t size); |
Martin v. Löwis | cd83fa8 | 2013-06-27 12:23:29 +0200 | [diff] [blame] | 48 | #endif |
| 49 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 50 | |
| 51 | static void * |
| 52 | _PyMem_RawMalloc(void *ctx, size_t size) |
| 53 | { |
| 54 | /* PyMem_Malloc(0) means malloc(1). Some systems would return NULL |
| 55 | for malloc(0), which would be treated as an error. Some platforms would |
| 56 | return a pointer with no memory behind it, which would break pymalloc. |
| 57 | To solve these problems, allocate an extra byte. */ |
| 58 | if (size == 0) |
| 59 | size = 1; |
| 60 | return malloc(size); |
| 61 | } |
| 62 | |
| 63 | static void * |
| 64 | _PyMem_RawRealloc(void *ctx, void *ptr, size_t size) |
| 65 | { |
| 66 | if (size == 0) |
| 67 | size = 1; |
| 68 | return realloc(ptr, size); |
| 69 | } |
| 70 | |
| 71 | static void |
| 72 | _PyMem_RawFree(void *ctx, void *ptr) |
| 73 | { |
| 74 | free(ptr); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | #ifdef MS_WINDOWS |
| 79 | static void * |
| 80 | _PyObject_ArenaVirtualAlloc(void *ctx, size_t size) |
| 81 | { |
| 82 | return VirtualAlloc(NULL, size, |
| 83 | MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); |
| 84 | } |
| 85 | |
| 86 | static void |
| 87 | _PyObject_ArenaVirtualFree(void *ctx, void *ptr, size_t size) |
| 88 | { |
Victor Stinner | 725e668 | 2013-07-07 03:06:16 +0200 | [diff] [blame] | 89 | VirtualFree(ptr, 0, MEM_RELEASE); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | #elif defined(ARENAS_USE_MMAP) |
| 93 | static void * |
| 94 | _PyObject_ArenaMmap(void *ctx, size_t size) |
| 95 | { |
| 96 | void *ptr; |
| 97 | ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, |
| 98 | MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 99 | if (ptr == MAP_FAILED) |
| 100 | return NULL; |
| 101 | assert(ptr != NULL); |
| 102 | return ptr; |
| 103 | } |
| 104 | |
| 105 | static void |
| 106 | _PyObject_ArenaMunmap(void *ctx, void *ptr, size_t size) |
| 107 | { |
| 108 | munmap(ptr, size); |
| 109 | } |
| 110 | |
| 111 | #else |
| 112 | static void * |
| 113 | _PyObject_ArenaMalloc(void *ctx, size_t size) |
| 114 | { |
| 115 | return malloc(size); |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | _PyObject_ArenaFree(void *ctx, void *ptr, size_t size) |
| 120 | { |
| 121 | free(ptr); |
| 122 | } |
| 123 | #endif |
| 124 | |
| 125 | |
| 126 | #define PYRAW_FUNCS _PyMem_RawMalloc, _PyMem_RawRealloc, _PyMem_RawFree |
| 127 | #ifdef WITH_PYMALLOC |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 128 | # define PYOBJ_FUNCS _PyObject_Malloc, _PyObject_Realloc, _PyObject_Free |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 129 | #else |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 130 | # define PYOBJ_FUNCS PYRAW_FUNCS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 131 | #endif |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 132 | #define PYMEM_FUNCS PYRAW_FUNCS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 133 | |
| 134 | #ifdef PYMALLOC_DEBUG |
| 135 | typedef struct { |
| 136 | /* We tag each block with an API ID in order to tag API violations */ |
| 137 | char api_id; |
| 138 | PyMemAllocator alloc; |
| 139 | } debug_alloc_api_t; |
| 140 | static struct { |
| 141 | debug_alloc_api_t raw; |
| 142 | debug_alloc_api_t mem; |
| 143 | debug_alloc_api_t obj; |
| 144 | } _PyMem_Debug = { |
| 145 | {'r', {NULL, PYRAW_FUNCS}}, |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 146 | {'m', {NULL, PYMEM_FUNCS}}, |
| 147 | {'o', {NULL, PYOBJ_FUNCS}} |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 148 | }; |
| 149 | |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 150 | #define PYDBG_FUNCS _PyMem_DebugMalloc, _PyMem_DebugRealloc, _PyMem_DebugFree |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 151 | #endif |
| 152 | |
| 153 | static PyMemAllocator _PyMem_Raw = { |
| 154 | #ifdef PYMALLOC_DEBUG |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 155 | &_PyMem_Debug.raw, PYDBG_FUNCS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 156 | #else |
| 157 | NULL, PYRAW_FUNCS |
| 158 | #endif |
| 159 | }; |
| 160 | |
| 161 | static PyMemAllocator _PyMem = { |
| 162 | #ifdef PYMALLOC_DEBUG |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 163 | &_PyMem_Debug.mem, PYDBG_FUNCS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 164 | #else |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 165 | NULL, PYMEM_FUNCS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 166 | #endif |
| 167 | }; |
| 168 | |
| 169 | static PyMemAllocator _PyObject = { |
| 170 | #ifdef PYMALLOC_DEBUG |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 171 | &_PyMem_Debug.obj, PYDBG_FUNCS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 172 | #else |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 173 | NULL, PYOBJ_FUNCS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 174 | #endif |
| 175 | }; |
| 176 | |
| 177 | #undef PYRAW_FUNCS |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 178 | #undef PYMEM_FUNCS |
| 179 | #undef PYOBJ_FUNCS |
| 180 | #undef PYDBG_FUNCS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 181 | |
| 182 | static PyObjectArenaAllocator _PyObject_Arena = {NULL, |
| 183 | #ifdef MS_WINDOWS |
| 184 | _PyObject_ArenaVirtualAlloc, _PyObject_ArenaVirtualFree |
| 185 | #elif defined(ARENAS_USE_MMAP) |
| 186 | _PyObject_ArenaMmap, _PyObject_ArenaMunmap |
| 187 | #else |
| 188 | _PyObject_ArenaMalloc, _PyObject_ArenaFree |
| 189 | #endif |
| 190 | }; |
| 191 | |
| 192 | void |
| 193 | PyMem_SetupDebugHooks(void) |
| 194 | { |
| 195 | #ifdef PYMALLOC_DEBUG |
| 196 | PyMemAllocator alloc; |
| 197 | |
| 198 | alloc.malloc = _PyMem_DebugMalloc; |
| 199 | alloc.realloc = _PyMem_DebugRealloc; |
| 200 | alloc.free = _PyMem_DebugFree; |
| 201 | |
| 202 | if (_PyMem_Raw.malloc != _PyMem_DebugMalloc) { |
| 203 | alloc.ctx = &_PyMem_Debug.raw; |
| 204 | PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &_PyMem_Debug.raw.alloc); |
| 205 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc); |
| 206 | } |
| 207 | |
| 208 | if (_PyMem.malloc != _PyMem_DebugMalloc) { |
| 209 | alloc.ctx = &_PyMem_Debug.mem; |
| 210 | PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &_PyMem_Debug.mem.alloc); |
| 211 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc); |
| 212 | } |
| 213 | |
| 214 | if (_PyObject.malloc != _PyMem_DebugMalloc) { |
| 215 | alloc.ctx = &_PyMem_Debug.obj; |
| 216 | PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &_PyMem_Debug.obj.alloc); |
| 217 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc); |
| 218 | } |
| 219 | #endif |
| 220 | } |
| 221 | |
| 222 | void |
| 223 | PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocator *allocator) |
| 224 | { |
| 225 | switch(domain) |
| 226 | { |
| 227 | case PYMEM_DOMAIN_RAW: *allocator = _PyMem_Raw; break; |
| 228 | case PYMEM_DOMAIN_MEM: *allocator = _PyMem; break; |
| 229 | case PYMEM_DOMAIN_OBJ: *allocator = _PyObject; break; |
| 230 | default: |
| 231 | /* unknown domain */ |
| 232 | allocator->ctx = NULL; |
| 233 | allocator->malloc = NULL; |
| 234 | allocator->realloc = NULL; |
| 235 | allocator->free = NULL; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void |
| 240 | PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocator *allocator) |
| 241 | { |
| 242 | switch(domain) |
| 243 | { |
| 244 | case PYMEM_DOMAIN_RAW: _PyMem_Raw = *allocator; break; |
| 245 | case PYMEM_DOMAIN_MEM: _PyMem = *allocator; break; |
| 246 | case PYMEM_DOMAIN_OBJ: _PyObject = *allocator; break; |
| 247 | /* ignore unknown domain */ |
| 248 | } |
| 249 | |
| 250 | } |
| 251 | |
| 252 | void |
| 253 | PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator) |
| 254 | { |
| 255 | *allocator = _PyObject_Arena; |
| 256 | } |
| 257 | |
| 258 | void |
| 259 | PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator) |
| 260 | { |
| 261 | _PyObject_Arena = *allocator; |
| 262 | } |
| 263 | |
| 264 | void * |
| 265 | PyMem_RawMalloc(size_t size) |
| 266 | { |
| 267 | /* |
| 268 | * Limit ourselves to PY_SSIZE_T_MAX bytes to prevent security holes. |
| 269 | * Most python internals blindly use a signed Py_ssize_t to track |
| 270 | * things without checking for overflows or negatives. |
| 271 | * As size_t is unsigned, checking for size < 0 is not required. |
| 272 | */ |
| 273 | if (size > (size_t)PY_SSIZE_T_MAX) |
| 274 | return NULL; |
| 275 | |
| 276 | return _PyMem_Raw.malloc(_PyMem_Raw.ctx, size); |
| 277 | } |
| 278 | |
| 279 | void* |
| 280 | PyMem_RawRealloc(void *ptr, size_t new_size) |
| 281 | { |
| 282 | /* see PyMem_RawMalloc() */ |
| 283 | if (new_size > (size_t)PY_SSIZE_T_MAX) |
| 284 | return NULL; |
| 285 | return _PyMem_Raw.realloc(_PyMem_Raw.ctx, ptr, new_size); |
| 286 | } |
| 287 | |
| 288 | void PyMem_RawFree(void *ptr) |
| 289 | { |
| 290 | _PyMem_Raw.free(_PyMem_Raw.ctx, ptr); |
| 291 | } |
| 292 | |
| 293 | void * |
| 294 | PyMem_Malloc(size_t size) |
| 295 | { |
| 296 | /* see PyMem_RawMalloc() */ |
| 297 | if (size > (size_t)PY_SSIZE_T_MAX) |
| 298 | return NULL; |
| 299 | return _PyMem.malloc(_PyMem.ctx, size); |
| 300 | } |
| 301 | |
| 302 | void * |
| 303 | PyMem_Realloc(void *ptr, size_t new_size) |
| 304 | { |
| 305 | /* see PyMem_RawMalloc() */ |
| 306 | if (new_size > (size_t)PY_SSIZE_T_MAX) |
| 307 | return NULL; |
| 308 | return _PyMem.realloc(_PyMem.ctx, ptr, new_size); |
| 309 | } |
| 310 | |
| 311 | void |
| 312 | PyMem_Free(void *ptr) |
| 313 | { |
| 314 | _PyMem.free(_PyMem.ctx, ptr); |
| 315 | } |
| 316 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 317 | char * |
| 318 | _PyMem_RawStrdup(const char *str) |
| 319 | { |
| 320 | size_t size; |
| 321 | char *copy; |
| 322 | |
| 323 | size = strlen(str) + 1; |
| 324 | copy = PyMem_RawMalloc(size); |
| 325 | if (copy == NULL) |
| 326 | return NULL; |
| 327 | memcpy(copy, str, size); |
| 328 | return copy; |
| 329 | } |
| 330 | |
| 331 | char * |
| 332 | _PyMem_Strdup(const char *str) |
| 333 | { |
| 334 | size_t size; |
| 335 | char *copy; |
| 336 | |
| 337 | size = strlen(str) + 1; |
| 338 | copy = PyMem_Malloc(size); |
| 339 | if (copy == NULL) |
| 340 | return NULL; |
| 341 | memcpy(copy, str, size); |
| 342 | return copy; |
| 343 | } |
| 344 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 345 | void * |
| 346 | PyObject_Malloc(size_t size) |
| 347 | { |
| 348 | /* see PyMem_RawMalloc() */ |
| 349 | if (size > (size_t)PY_SSIZE_T_MAX) |
| 350 | return NULL; |
| 351 | return _PyObject.malloc(_PyObject.ctx, size); |
| 352 | } |
| 353 | |
| 354 | void * |
| 355 | PyObject_Realloc(void *ptr, size_t new_size) |
| 356 | { |
| 357 | /* see PyMem_RawMalloc() */ |
| 358 | if (new_size > (size_t)PY_SSIZE_T_MAX) |
| 359 | return NULL; |
| 360 | return _PyObject.realloc(_PyObject.ctx, ptr, new_size); |
| 361 | } |
| 362 | |
| 363 | void |
| 364 | PyObject_Free(void *ptr) |
| 365 | { |
| 366 | _PyObject.free(_PyObject.ctx, ptr); |
| 367 | } |
| 368 | |
| 369 | |
| 370 | #ifdef WITH_PYMALLOC |
| 371 | |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 372 | #ifdef WITH_VALGRIND |
| 373 | #include <valgrind/valgrind.h> |
| 374 | |
| 375 | /* If we're using GCC, use __builtin_expect() to reduce overhead of |
| 376 | the valgrind checks */ |
| 377 | #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) |
| 378 | # define UNLIKELY(value) __builtin_expect((value), 0) |
| 379 | #else |
| 380 | # define UNLIKELY(value) (value) |
| 381 | #endif |
| 382 | |
| 383 | /* -1 indicates that we haven't checked that we're running on valgrind yet. */ |
| 384 | static int running_on_valgrind = -1; |
| 385 | #endif |
| 386 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 387 | /* An object allocator for Python. |
| 388 | |
| 389 | Here is an introduction to the layers of the Python memory architecture, |
| 390 | showing where the object allocator is actually used (layer +2), It is |
| 391 | called for every object allocation and deallocation (PyObject_New/Del), |
| 392 | unless the object-specific allocators implement a proprietary allocation |
| 393 | scheme (ex.: ints use a simple free list). This is also the place where |
| 394 | the cyclic garbage collector operates selectively on container objects. |
| 395 | |
| 396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 397 | Object-specific allocators |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 398 | _____ ______ ______ ________ |
| 399 | [ int ] [ dict ] [ list ] ... [ string ] Python core | |
| 400 | +3 | <----- Object-specific memory -----> | <-- Non-object memory --> | |
| 401 | _______________________________ | | |
| 402 | [ Python's object allocator ] | | |
| 403 | +2 | ####### Object memory ####### | <------ Internal buffers ------> | |
| 404 | ______________________________________________________________ | |
| 405 | [ Python's raw memory allocator (PyMem_ API) ] | |
| 406 | +1 | <----- Python memory (under PyMem manager's control) ------> | | |
| 407 | __________________________________________________________________ |
| 408 | [ Underlying general-purpose allocator (ex: C library malloc) ] |
| 409 | 0 | <------ Virtual memory allocated for the python process -------> | |
| 410 | |
| 411 | ========================================================================= |
| 412 | _______________________________________________________________________ |
| 413 | [ OS-specific Virtual Memory Manager (VMM) ] |
| 414 | -1 | <--- Kernel dynamic storage allocation & management (page-based) ---> | |
| 415 | __________________________________ __________________________________ |
| 416 | [ ] [ ] |
| 417 | -2 | <-- Physical memory: ROM/RAM --> | | <-- Secondary storage (swap) --> | |
| 418 | |
| 419 | */ |
| 420 | /*==========================================================================*/ |
| 421 | |
| 422 | /* A fast, special-purpose memory allocator for small blocks, to be used |
| 423 | on top of a general-purpose malloc -- heavily based on previous art. */ |
| 424 | |
| 425 | /* Vladimir Marangozov -- August 2000 */ |
| 426 | |
| 427 | /* |
| 428 | * "Memory management is where the rubber meets the road -- if we do the wrong |
| 429 | * thing at any level, the results will not be good. And if we don't make the |
| 430 | * levels work well together, we are in serious trouble." (1) |
| 431 | * |
| 432 | * (1) Paul R. Wilson, Mark S. Johnstone, Michael Neely, and David Boles, |
| 433 | * "Dynamic Storage Allocation: A Survey and Critical Review", |
| 434 | * in Proc. 1995 Int'l. Workshop on Memory Management, September 1995. |
| 435 | */ |
| 436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | /* #undef WITH_MEMORY_LIMITS */ /* disable mem limit checks */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 438 | |
| 439 | /*==========================================================================*/ |
| 440 | |
| 441 | /* |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 442 | * Allocation strategy abstract: |
| 443 | * |
| 444 | * For small requests, the allocator sub-allocates <Big> blocks of memory. |
Antoine Pitrou | 6f26be0 | 2011-05-03 18:18:59 +0200 | [diff] [blame] | 445 | * Requests greater than SMALL_REQUEST_THRESHOLD bytes are routed to the |
| 446 | * system's allocator. |
Tim Peters | ce7fb9b | 2002-03-23 00:28:57 +0000 | [diff] [blame] | 447 | * |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 448 | * Small requests are grouped in size classes spaced 8 bytes apart, due |
| 449 | * to the required valid alignment of the returned address. Requests of |
| 450 | * a particular size are serviced from memory pools of 4K (one VMM page). |
| 451 | * Pools are fragmented on demand and contain free lists of blocks of one |
| 452 | * particular size class. In other words, there is a fixed-size allocator |
| 453 | * for each size class. Free pools are shared by the different allocators |
| 454 | * thus minimizing the space reserved for a particular size class. |
| 455 | * |
| 456 | * This allocation strategy is a variant of what is known as "simple |
| 457 | * segregated storage based on array of free lists". The main drawback of |
| 458 | * simple segregated storage is that we might end up with lot of reserved |
| 459 | * memory for the different free lists, which degenerate in time. To avoid |
| 460 | * this, we partition each free list in pools and we share dynamically the |
| 461 | * reserved space between all free lists. This technique is quite efficient |
| 462 | * for memory intensive programs which allocate mainly small-sized blocks. |
| 463 | * |
| 464 | * For small requests we have the following table: |
| 465 | * |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | * Request in bytes Size of allocated block Size class idx |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 467 | * ---------------------------------------------------------------- |
| 468 | * 1-8 8 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | * 9-16 16 1 |
| 470 | * 17-24 24 2 |
| 471 | * 25-32 32 3 |
| 472 | * 33-40 40 4 |
| 473 | * 41-48 48 5 |
| 474 | * 49-56 56 6 |
| 475 | * 57-64 64 7 |
| 476 | * 65-72 72 8 |
| 477 | * ... ... ... |
Antoine Pitrou | 6f26be0 | 2011-05-03 18:18:59 +0200 | [diff] [blame] | 478 | * 497-504 504 62 |
| 479 | * 505-512 512 63 |
Tim Peters | ce7fb9b | 2002-03-23 00:28:57 +0000 | [diff] [blame] | 480 | * |
Antoine Pitrou | 6f26be0 | 2011-05-03 18:18:59 +0200 | [diff] [blame] | 481 | * 0, SMALL_REQUEST_THRESHOLD + 1 and up: routed to the underlying |
| 482 | * allocator. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 483 | */ |
| 484 | |
| 485 | /*==========================================================================*/ |
| 486 | |
| 487 | /* |
| 488 | * -- Main tunable settings section -- |
| 489 | */ |
| 490 | |
| 491 | /* |
| 492 | * Alignment of addresses returned to the user. 8-bytes alignment works |
| 493 | * on most current architectures (with 32-bit or 64-bit address busses). |
| 494 | * The alignment value is also used for grouping small requests in size |
| 495 | * classes spaced ALIGNMENT bytes apart. |
| 496 | * |
| 497 | * You shouldn't change this unless you know what you are doing. |
| 498 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | #define ALIGNMENT 8 /* must be 2^N */ |
| 500 | #define ALIGNMENT_SHIFT 3 |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 501 | |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 502 | /* Return the number of bytes in size class I, as a uint. */ |
| 503 | #define INDEX2SIZE(I) (((uint)(I) + 1) << ALIGNMENT_SHIFT) |
| 504 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 505 | /* |
| 506 | * Max size threshold below which malloc requests are considered to be |
| 507 | * small enough in order to use preallocated memory pools. You can tune |
| 508 | * this value according to your application behaviour and memory needs. |
| 509 | * |
Antoine Pitrou | 6f26be0 | 2011-05-03 18:18:59 +0200 | [diff] [blame] | 510 | * Note: a size threshold of 512 guarantees that newly created dictionaries |
| 511 | * will be allocated from preallocated memory pools on 64-bit. |
| 512 | * |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 513 | * The following invariants must hold: |
Antoine Pitrou | 6f26be0 | 2011-05-03 18:18:59 +0200 | [diff] [blame] | 514 | * 1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 512 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 515 | * 2) SMALL_REQUEST_THRESHOLD is evenly divisible by ALIGNMENT |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 516 | * |
| 517 | * Although not required, for better performance and space efficiency, |
| 518 | * it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2. |
| 519 | */ |
Antoine Pitrou | 6f26be0 | 2011-05-03 18:18:59 +0200 | [diff] [blame] | 520 | #define SMALL_REQUEST_THRESHOLD 512 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | #define NB_SMALL_SIZE_CLASSES (SMALL_REQUEST_THRESHOLD / ALIGNMENT) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 522 | |
| 523 | /* |
| 524 | * The system's VMM page size can be obtained on most unices with a |
| 525 | * getpagesize() call or deduced from various header files. To make |
| 526 | * things simpler, we assume that it is 4K, which is OK for most systems. |
| 527 | * It is probably better if this is the native page size, but it doesn't |
Tim Peters | ecc6e6a | 2005-07-10 22:30:55 +0000 | [diff] [blame] | 528 | * have to be. In theory, if SYSTEM_PAGE_SIZE is larger than the native page |
| 529 | * size, then `POOL_ADDR(p)->arenaindex' could rarely cause a segmentation |
| 530 | * violation fault. 4K is apparently OK for all the platforms that python |
Martin v. Löwis | 8c14028 | 2002-10-26 15:01:53 +0000 | [diff] [blame] | 531 | * currently targets. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 532 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | #define SYSTEM_PAGE_SIZE (4 * 1024) |
| 534 | #define SYSTEM_PAGE_SIZE_MASK (SYSTEM_PAGE_SIZE - 1) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 535 | |
| 536 | /* |
| 537 | * Maximum amount of memory managed by the allocator for small requests. |
| 538 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 539 | #ifdef WITH_MEMORY_LIMITS |
| 540 | #ifndef SMALL_MEMORY_LIMIT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | #define SMALL_MEMORY_LIMIT (64 * 1024 * 1024) /* 64 MB -- more? */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 542 | #endif |
| 543 | #endif |
| 544 | |
| 545 | /* |
| 546 | * The allocator sub-allocates <Big> blocks of memory (called arenas) aligned |
| 547 | * on a page boundary. This is a reserved virtual address space for the |
Antoine Pitrou | f0effe6 | 2011-11-26 01:11:02 +0100 | [diff] [blame] | 548 | * current process (obtained through a malloc()/mmap() call). In no way this |
| 549 | * means that the memory arenas will be used entirely. A malloc(<Big>) is |
| 550 | * usually an address range reservation for <Big> bytes, unless all pages within |
| 551 | * this space are referenced subsequently. So malloc'ing big blocks and not |
| 552 | * using them does not mean "wasting memory". It's an addressable range |
| 553 | * wastage... |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 554 | * |
Antoine Pitrou | f0effe6 | 2011-11-26 01:11:02 +0100 | [diff] [blame] | 555 | * Arenas are allocated with mmap() on systems supporting anonymous memory |
| 556 | * mappings to reduce heap fragmentation. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 557 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 558 | #define ARENA_SIZE (256 << 10) /* 256KB */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 559 | |
| 560 | #ifdef WITH_MEMORY_LIMITS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | #define MAX_ARENAS (SMALL_MEMORY_LIMIT / ARENA_SIZE) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 562 | #endif |
| 563 | |
| 564 | /* |
| 565 | * Size of the pools used for small blocks. Should be a power of 2, |
Tim Peters | c2ce91a | 2002-03-30 21:36:04 +0000 | [diff] [blame] | 566 | * between 1K and SYSTEM_PAGE_SIZE, that is: 1k, 2k, 4k. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 567 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 568 | #define POOL_SIZE SYSTEM_PAGE_SIZE /* must be 2^N */ |
| 569 | #define POOL_SIZE_MASK SYSTEM_PAGE_SIZE_MASK |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 570 | |
| 571 | /* |
| 572 | * -- End of tunable settings section -- |
| 573 | */ |
| 574 | |
| 575 | /*==========================================================================*/ |
| 576 | |
| 577 | /* |
| 578 | * Locking |
| 579 | * |
| 580 | * To reduce lock contention, it would probably be better to refine the |
| 581 | * crude function locking with per size class locking. I'm not positive |
| 582 | * however, whether it's worth switching to such locking policy because |
| 583 | * of the performance penalty it might introduce. |
| 584 | * |
| 585 | * The following macros describe the simplest (should also be the fastest) |
| 586 | * lock object on a particular platform and the init/fini/lock/unlock |
| 587 | * operations on it. The locks defined here are not expected to be recursive |
| 588 | * because it is assumed that they will always be called in the order: |
| 589 | * INIT, [LOCK, UNLOCK]*, FINI. |
| 590 | */ |
| 591 | |
| 592 | /* |
| 593 | * Python's threads are serialized, so object malloc locking is disabled. |
| 594 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 595 | #define SIMPLELOCK_DECL(lock) /* simple lock declaration */ |
| 596 | #define SIMPLELOCK_INIT(lock) /* allocate (if needed) and initialize */ |
| 597 | #define SIMPLELOCK_FINI(lock) /* free/destroy an existing lock */ |
| 598 | #define SIMPLELOCK_LOCK(lock) /* acquire released lock */ |
| 599 | #define SIMPLELOCK_UNLOCK(lock) /* release acquired lock */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 600 | |
| 601 | /* |
| 602 | * Basic types |
| 603 | * I don't care if these are defined in <sys/types.h> or elsewhere. Axiom. |
| 604 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 605 | #undef uchar |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | #define uchar unsigned char /* assuming == 8 bits */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 607 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 608 | #undef uint |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | #define uint unsigned int /* assuming >= 16 bits */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 610 | |
| 611 | #undef ulong |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | #define ulong unsigned long /* assuming >= 32 bits */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 613 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 614 | #undef uptr |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | #define uptr Py_uintptr_t |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 616 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 617 | /* When you say memory, my mind reasons in terms of (pointers to) blocks */ |
| 618 | typedef uchar block; |
| 619 | |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 620 | /* Pool for small blocks. */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 621 | struct pool_header { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | union { block *_padding; |
Stefan Krah | 735bb12 | 2010-11-26 10:54:09 +0000 | [diff] [blame] | 623 | uint count; } ref; /* number of allocated blocks */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | block *freeblock; /* pool's free list head */ |
| 625 | struct pool_header *nextpool; /* next pool of this size class */ |
| 626 | struct pool_header *prevpool; /* previous pool "" */ |
| 627 | uint arenaindex; /* index into arenas of base adr */ |
| 628 | uint szidx; /* block size class index */ |
| 629 | uint nextoffset; /* bytes to virgin block */ |
| 630 | uint maxnextoffset; /* largest valid nextoffset */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 631 | }; |
| 632 | |
| 633 | typedef struct pool_header *poolp; |
| 634 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 635 | /* Record keeping for arenas. */ |
| 636 | struct arena_object { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 637 | /* The address of the arena, as returned by malloc. Note that 0 |
| 638 | * will never be returned by a successful malloc, and is used |
| 639 | * here to mark an arena_object that doesn't correspond to an |
| 640 | * allocated arena. |
| 641 | */ |
| 642 | uptr address; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 644 | /* Pool-aligned pointer to the next pool to be carved off. */ |
| 645 | block* pool_address; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | /* The number of available pools in the arena: free pools + never- |
| 648 | * allocated pools. |
| 649 | */ |
| 650 | uint nfreepools; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | /* The total number of pools in the arena, whether or not available. */ |
| 653 | uint ntotalpools; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | /* Singly-linked list of available pools. */ |
| 656 | struct pool_header* freepools; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | /* Whenever this arena_object is not associated with an allocated |
| 659 | * arena, the nextarena member is used to link all unassociated |
| 660 | * arena_objects in the singly-linked `unused_arena_objects` list. |
| 661 | * The prevarena member is unused in this case. |
| 662 | * |
| 663 | * When this arena_object is associated with an allocated arena |
| 664 | * with at least one available pool, both members are used in the |
| 665 | * doubly-linked `usable_arenas` list, which is maintained in |
| 666 | * increasing order of `nfreepools` values. |
| 667 | * |
| 668 | * Else this arena_object is associated with an allocated arena |
| 669 | * all of whose pools are in use. `nextarena` and `prevarena` |
| 670 | * are both meaningless in this case. |
| 671 | */ |
| 672 | struct arena_object* nextarena; |
| 673 | struct arena_object* prevarena; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 674 | }; |
| 675 | |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 676 | #define POOL_OVERHEAD _Py_SIZE_ROUND_UP(sizeof(struct pool_header), ALIGNMENT) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 678 | #define DUMMY_SIZE_IDX 0xffff /* size class of newly cached pools */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 679 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 680 | /* Round pointer P down to the closest pool-aligned address <= P, as a poolp */ |
Antoine Pitrou | ca8aa4a | 2012-09-20 20:56:47 +0200 | [diff] [blame] | 681 | #define POOL_ADDR(P) ((poolp)_Py_ALIGN_DOWN((P), POOL_SIZE)) |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 682 | |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 683 | /* Return total number of blocks in pool of size index I, as a uint. */ |
| 684 | #define NUMBLOCKS(I) ((uint)(POOL_SIZE - POOL_OVERHEAD) / INDEX2SIZE(I)) |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 685 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 686 | /*==========================================================================*/ |
| 687 | |
| 688 | /* |
| 689 | * This malloc lock |
| 690 | */ |
Jeremy Hylton | d1fedb6 | 2002-07-18 18:49:52 +0000 | [diff] [blame] | 691 | SIMPLELOCK_DECL(_malloc_lock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | #define LOCK() SIMPLELOCK_LOCK(_malloc_lock) |
| 693 | #define UNLOCK() SIMPLELOCK_UNLOCK(_malloc_lock) |
| 694 | #define LOCK_INIT() SIMPLELOCK_INIT(_malloc_lock) |
| 695 | #define LOCK_FINI() SIMPLELOCK_FINI(_malloc_lock) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 696 | |
| 697 | /* |
Tim Peters | 1e16db6 | 2002-03-31 01:05:22 +0000 | [diff] [blame] | 698 | * Pool table -- headed, circular, doubly-linked lists of partially used pools. |
| 699 | |
| 700 | This is involved. For an index i, usedpools[i+i] is the header for a list of |
| 701 | all partially used pools holding small blocks with "size class idx" i. So |
| 702 | usedpools[0] corresponds to blocks of size 8, usedpools[2] to blocks of size |
| 703 | 16, and so on: index 2*i <-> blocks of size (i+1)<<ALIGNMENT_SHIFT. |
| 704 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 705 | Pools are carved off an arena's highwater mark (an arena_object's pool_address |
| 706 | member) as needed. Once carved off, a pool is in one of three states forever |
| 707 | after: |
Tim Peters | 1e16db6 | 2002-03-31 01:05:22 +0000 | [diff] [blame] | 708 | |
Tim Peters | 338e010 | 2002-04-01 19:23:44 +0000 | [diff] [blame] | 709 | used == partially used, neither empty nor full |
| 710 | At least one block in the pool is currently allocated, and at least one |
| 711 | block in the pool is not currently allocated (note this implies a pool |
| 712 | has room for at least two blocks). |
| 713 | This is a pool's initial state, as a pool is created only when malloc |
| 714 | needs space. |
| 715 | The pool holds blocks of a fixed size, and is in the circular list headed |
| 716 | at usedpools[i] (see above). It's linked to the other used pools of the |
| 717 | same size class via the pool_header's nextpool and prevpool members. |
| 718 | If all but one block is currently allocated, a malloc can cause a |
| 719 | transition to the full state. If all but one block is not currently |
| 720 | allocated, a free can cause a transition to the empty state. |
Tim Peters | 1e16db6 | 2002-03-31 01:05:22 +0000 | [diff] [blame] | 721 | |
Tim Peters | 338e010 | 2002-04-01 19:23:44 +0000 | [diff] [blame] | 722 | full == all the pool's blocks are currently allocated |
| 723 | On transition to full, a pool is unlinked from its usedpools[] list. |
| 724 | It's not linked to from anything then anymore, and its nextpool and |
| 725 | prevpool members are meaningless until it transitions back to used. |
| 726 | A free of a block in a full pool puts the pool back in the used state. |
| 727 | Then it's linked in at the front of the appropriate usedpools[] list, so |
| 728 | that the next allocation for its size class will reuse the freed block. |
| 729 | |
| 730 | empty == all the pool's blocks are currently available for allocation |
| 731 | On transition to empty, a pool is unlinked from its usedpools[] list, |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 732 | and linked to the front of its arena_object's singly-linked freepools list, |
Tim Peters | 338e010 | 2002-04-01 19:23:44 +0000 | [diff] [blame] | 733 | via its nextpool member. The prevpool member has no meaning in this case. |
| 734 | Empty pools have no inherent size class: the next time a malloc finds |
| 735 | an empty list in usedpools[], it takes the first pool off of freepools. |
| 736 | If the size class needed happens to be the same as the size class the pool |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 737 | last had, some pool initialization can be skipped. |
Tim Peters | 338e010 | 2002-04-01 19:23:44 +0000 | [diff] [blame] | 738 | |
| 739 | |
| 740 | Block Management |
| 741 | |
| 742 | Blocks within pools are again carved out as needed. pool->freeblock points to |
| 743 | the start of a singly-linked list of free blocks within the pool. When a |
| 744 | block is freed, it's inserted at the front of its pool's freeblock list. Note |
| 745 | that the available blocks in a pool are *not* linked all together when a pool |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 746 | is initialized. Instead only "the first two" (lowest addresses) blocks are |
| 747 | set up, returning the first such block, and setting pool->freeblock to a |
| 748 | one-block list holding the second such block. This is consistent with that |
| 749 | pymalloc strives at all levels (arena, pool, and block) never to touch a piece |
| 750 | of memory until it's actually needed. |
| 751 | |
| 752 | So long as a pool is in the used state, we're certain there *is* a block |
Tim Peters | 52aefc8 | 2002-04-11 06:36:45 +0000 | [diff] [blame] | 753 | available for allocating, and pool->freeblock is not NULL. If pool->freeblock |
| 754 | points to the end of the free list before we've carved the entire pool into |
| 755 | blocks, that means we simply haven't yet gotten to one of the higher-address |
| 756 | blocks. The offset from the pool_header to the start of "the next" virgin |
| 757 | block is stored in the pool_header nextoffset member, and the largest value |
| 758 | of nextoffset that makes sense is stored in the maxnextoffset member when a |
| 759 | pool is initialized. All the blocks in a pool have been passed out at least |
| 760 | once when and only when nextoffset > maxnextoffset. |
Tim Peters | 338e010 | 2002-04-01 19:23:44 +0000 | [diff] [blame] | 761 | |
Tim Peters | 1e16db6 | 2002-03-31 01:05:22 +0000 | [diff] [blame] | 762 | |
| 763 | Major obscurity: While the usedpools vector is declared to have poolp |
| 764 | entries, it doesn't really. It really contains two pointers per (conceptual) |
| 765 | poolp entry, the nextpool and prevpool members of a pool_header. The |
| 766 | excruciating initialization code below fools C so that |
| 767 | |
| 768 | usedpool[i+i] |
| 769 | |
| 770 | "acts like" a genuine poolp, but only so long as you only reference its |
| 771 | nextpool and prevpool members. The "- 2*sizeof(block *)" gibberish is |
| 772 | compensating for that a pool_header's nextpool and prevpool members |
| 773 | immediately follow a pool_header's first two members: |
| 774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | union { block *_padding; |
Stefan Krah | 735bb12 | 2010-11-26 10:54:09 +0000 | [diff] [blame] | 776 | uint count; } ref; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 777 | block *freeblock; |
Tim Peters | 1e16db6 | 2002-03-31 01:05:22 +0000 | [diff] [blame] | 778 | |
| 779 | each of which consume sizeof(block *) bytes. So what usedpools[i+i] really |
| 780 | contains is a fudged-up pointer p such that *if* C believes it's a poolp |
| 781 | pointer, then p->nextpool and p->prevpool are both p (meaning that the headed |
| 782 | circular list is empty). |
| 783 | |
| 784 | It's unclear why the usedpools setup is so convoluted. It could be to |
| 785 | minimize the amount of cache required to hold this heavily-referenced table |
| 786 | (which only *needs* the two interpool pointer members of a pool_header). OTOH, |
| 787 | referencing code has to remember to "double the index" and doing so isn't |
| 788 | free, usedpools[0] isn't a strictly legal pointer, and we're crucially relying |
| 789 | on that C doesn't insert any padding anywhere in a pool_header at or before |
| 790 | the prevpool member. |
| 791 | **************************************************************************** */ |
| 792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | #define PTA(x) ((poolp )((uchar *)&(usedpools[2*(x)]) - 2*sizeof(block *))) |
| 794 | #define PT(x) PTA(x), PTA(x) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 795 | |
| 796 | static poolp usedpools[2 * ((NB_SMALL_SIZE_CLASSES + 7) / 8) * 8] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 797 | PT(0), PT(1), PT(2), PT(3), PT(4), PT(5), PT(6), PT(7) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 798 | #if NB_SMALL_SIZE_CLASSES > 8 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 799 | , PT(8), PT(9), PT(10), PT(11), PT(12), PT(13), PT(14), PT(15) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 800 | #if NB_SMALL_SIZE_CLASSES > 16 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 801 | , PT(16), PT(17), PT(18), PT(19), PT(20), PT(21), PT(22), PT(23) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 802 | #if NB_SMALL_SIZE_CLASSES > 24 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | , PT(24), PT(25), PT(26), PT(27), PT(28), PT(29), PT(30), PT(31) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 804 | #if NB_SMALL_SIZE_CLASSES > 32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 805 | , PT(32), PT(33), PT(34), PT(35), PT(36), PT(37), PT(38), PT(39) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 806 | #if NB_SMALL_SIZE_CLASSES > 40 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | , PT(40), PT(41), PT(42), PT(43), PT(44), PT(45), PT(46), PT(47) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 808 | #if NB_SMALL_SIZE_CLASSES > 48 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 809 | , PT(48), PT(49), PT(50), PT(51), PT(52), PT(53), PT(54), PT(55) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 810 | #if NB_SMALL_SIZE_CLASSES > 56 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 811 | , PT(56), PT(57), PT(58), PT(59), PT(60), PT(61), PT(62), PT(63) |
Antoine Pitrou | 6f26be0 | 2011-05-03 18:18:59 +0200 | [diff] [blame] | 812 | #if NB_SMALL_SIZE_CLASSES > 64 |
| 813 | #error "NB_SMALL_SIZE_CLASSES should be less than 64" |
| 814 | #endif /* NB_SMALL_SIZE_CLASSES > 64 */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 815 | #endif /* NB_SMALL_SIZE_CLASSES > 56 */ |
| 816 | #endif /* NB_SMALL_SIZE_CLASSES > 48 */ |
| 817 | #endif /* NB_SMALL_SIZE_CLASSES > 40 */ |
| 818 | #endif /* NB_SMALL_SIZE_CLASSES > 32 */ |
| 819 | #endif /* NB_SMALL_SIZE_CLASSES > 24 */ |
| 820 | #endif /* NB_SMALL_SIZE_CLASSES > 16 */ |
| 821 | #endif /* NB_SMALL_SIZE_CLASSES > 8 */ |
| 822 | }; |
| 823 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 824 | /*========================================================================== |
| 825 | Arena management. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 826 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 827 | `arenas` is a vector of arena_objects. It contains maxarenas entries, some of |
| 828 | which may not be currently used (== they're arena_objects that aren't |
| 829 | currently associated with an allocated arena). Note that arenas proper are |
| 830 | separately malloc'ed. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 831 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 832 | Prior to Python 2.5, arenas were never free()'ed. Starting with Python 2.5, |
| 833 | we do try to free() arenas, and use some mild heuristic strategies to increase |
| 834 | the likelihood that arenas eventually can be freed. |
| 835 | |
| 836 | unused_arena_objects |
| 837 | |
| 838 | This is a singly-linked list of the arena_objects that are currently not |
| 839 | being used (no arena is associated with them). Objects are taken off the |
| 840 | head of the list in new_arena(), and are pushed on the head of the list in |
| 841 | PyObject_Free() when the arena is empty. Key invariant: an arena_object |
| 842 | is on this list if and only if its .address member is 0. |
| 843 | |
| 844 | usable_arenas |
| 845 | |
| 846 | This is a doubly-linked list of the arena_objects associated with arenas |
| 847 | that have pools available. These pools are either waiting to be reused, |
| 848 | or have not been used before. The list is sorted to have the most- |
| 849 | allocated arenas first (ascending order based on the nfreepools member). |
| 850 | This means that the next allocation will come from a heavily used arena, |
| 851 | which gives the nearly empty arenas a chance to be returned to the system. |
| 852 | In my unscientific tests this dramatically improved the number of arenas |
| 853 | that could be freed. |
| 854 | |
| 855 | Note that an arena_object associated with an arena all of whose pools are |
| 856 | currently in use isn't on either list. |
| 857 | */ |
| 858 | |
| 859 | /* Array of objects used to track chunks of memory (arenas). */ |
| 860 | static struct arena_object* arenas = NULL; |
| 861 | /* Number of slots currently allocated in the `arenas` vector. */ |
Tim Peters | 1d99af8 | 2002-03-30 10:35:09 +0000 | [diff] [blame] | 862 | static uint maxarenas = 0; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 863 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 864 | /* The head of the singly-linked, NULL-terminated list of available |
| 865 | * arena_objects. |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 866 | */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 867 | static struct arena_object* unused_arena_objects = NULL; |
| 868 | |
| 869 | /* The head of the doubly-linked, NULL-terminated at each end, list of |
| 870 | * arena_objects associated with arenas that have pools available. |
| 871 | */ |
| 872 | static struct arena_object* usable_arenas = NULL; |
| 873 | |
| 874 | /* How many arena_objects do we initially allocate? |
| 875 | * 16 = can allocate 16 arenas = 16 * ARENA_SIZE = 4MB before growing the |
| 876 | * `arenas` vector. |
| 877 | */ |
| 878 | #define INITIAL_ARENA_OBJECTS 16 |
| 879 | |
| 880 | /* Number of arenas allocated that haven't been free()'d. */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 881 | static size_t narenas_currently_allocated = 0; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 882 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 883 | /* Total number of times malloc() called to allocate an arena. */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 884 | static size_t ntimes_arena_allocated = 0; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 885 | /* High water mark (max value ever seen) for narenas_currently_allocated. */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 886 | static size_t narenas_highwater = 0; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 887 | |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 888 | static Py_ssize_t _Py_AllocatedBlocks = 0; |
| 889 | |
| 890 | Py_ssize_t |
| 891 | _Py_GetAllocatedBlocks(void) |
| 892 | { |
| 893 | return _Py_AllocatedBlocks; |
| 894 | } |
| 895 | |
| 896 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 897 | /* Allocate a new arena. If we run out of memory, return NULL. Else |
| 898 | * allocate a new arena, and return the address of an arena_object |
| 899 | * describing the new arena. It's expected that the caller will set |
| 900 | * `usable_arenas` to the return value. |
| 901 | */ |
| 902 | static struct arena_object* |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 903 | new_arena(void) |
| 904 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | struct arena_object* arenaobj; |
| 906 | uint excess; /* number of bytes above pool alignment */ |
Victor Stinner | ba10882 | 2012-03-10 00:21:44 +0100 | [diff] [blame] | 907 | void *address; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 908 | |
Tim Peters | 0e87118 | 2002-04-13 08:29:14 +0000 | [diff] [blame] | 909 | #ifdef PYMALLOC_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | if (Py_GETENV("PYTHONMALLOCSTATS")) |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 911 | _PyObject_DebugMallocStats(stderr); |
Tim Peters | 0e87118 | 2002-04-13 08:29:14 +0000 | [diff] [blame] | 912 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 913 | if (unused_arena_objects == NULL) { |
| 914 | uint i; |
| 915 | uint numarenas; |
| 916 | size_t nbytes; |
Tim Peters | 0e87118 | 2002-04-13 08:29:14 +0000 | [diff] [blame] | 917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 918 | /* Double the number of arena objects on each allocation. |
| 919 | * Note that it's possible for `numarenas` to overflow. |
| 920 | */ |
| 921 | numarenas = maxarenas ? maxarenas << 1 : INITIAL_ARENA_OBJECTS; |
| 922 | if (numarenas <= maxarenas) |
| 923 | return NULL; /* overflow */ |
Martin v. Löwis | 5aca882 | 2008-09-11 06:55:48 +0000 | [diff] [blame] | 924 | #if SIZEOF_SIZE_T <= SIZEOF_INT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | if (numarenas > PY_SIZE_MAX / sizeof(*arenas)) |
| 926 | return NULL; /* overflow */ |
Martin v. Löwis | 5aca882 | 2008-09-11 06:55:48 +0000 | [diff] [blame] | 927 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | nbytes = numarenas * sizeof(*arenas); |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 929 | arenaobj = (struct arena_object *)PyMem_RawRealloc(arenas, nbytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | if (arenaobj == NULL) |
| 931 | return NULL; |
| 932 | arenas = arenaobj; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 933 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 934 | /* We might need to fix pointers that were copied. However, |
| 935 | * new_arena only gets called when all the pages in the |
| 936 | * previous arenas are full. Thus, there are *no* pointers |
| 937 | * into the old array. Thus, we don't have to worry about |
| 938 | * invalid pointers. Just to be sure, some asserts: |
| 939 | */ |
| 940 | assert(usable_arenas == NULL); |
| 941 | assert(unused_arena_objects == NULL); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 943 | /* Put the new arenas on the unused_arena_objects list. */ |
| 944 | for (i = maxarenas; i < numarenas; ++i) { |
| 945 | arenas[i].address = 0; /* mark as unassociated */ |
| 946 | arenas[i].nextarena = i < numarenas - 1 ? |
| 947 | &arenas[i+1] : NULL; |
| 948 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | /* Update globals. */ |
| 951 | unused_arena_objects = &arenas[maxarenas]; |
| 952 | maxarenas = numarenas; |
| 953 | } |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 954 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 955 | /* Take the next available arena object off the head of the list. */ |
| 956 | assert(unused_arena_objects != NULL); |
| 957 | arenaobj = unused_arena_objects; |
| 958 | unused_arena_objects = arenaobj->nextarena; |
| 959 | assert(arenaobj->address == 0); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 960 | address = _PyObject_Arena.alloc(_PyObject_Arena.ctx, ARENA_SIZE); |
| 961 | if (address == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | /* The allocation failed: return NULL after putting the |
| 963 | * arenaobj back. |
| 964 | */ |
| 965 | arenaobj->nextarena = unused_arena_objects; |
| 966 | unused_arena_objects = arenaobj; |
| 967 | return NULL; |
| 968 | } |
Victor Stinner | ba10882 | 2012-03-10 00:21:44 +0100 | [diff] [blame] | 969 | arenaobj->address = (uptr)address; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | ++narenas_currently_allocated; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | ++ntimes_arena_allocated; |
| 973 | if (narenas_currently_allocated > narenas_highwater) |
| 974 | narenas_highwater = narenas_currently_allocated; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | arenaobj->freepools = NULL; |
| 976 | /* pool_address <- first pool-aligned address in the arena |
| 977 | nfreepools <- number of whole pools that fit after alignment */ |
| 978 | arenaobj->pool_address = (block*)arenaobj->address; |
| 979 | arenaobj->nfreepools = ARENA_SIZE / POOL_SIZE; |
| 980 | assert(POOL_SIZE * arenaobj->nfreepools == ARENA_SIZE); |
| 981 | excess = (uint)(arenaobj->address & POOL_SIZE_MASK); |
| 982 | if (excess != 0) { |
| 983 | --arenaobj->nfreepools; |
| 984 | arenaobj->pool_address += POOL_SIZE - excess; |
| 985 | } |
| 986 | arenaobj->ntotalpools = arenaobj->nfreepools; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 987 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | return arenaobj; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 991 | /* |
| 992 | Py_ADDRESS_IN_RANGE(P, POOL) |
| 993 | |
| 994 | Return true if and only if P is an address that was allocated by pymalloc. |
| 995 | POOL must be the pool address associated with P, i.e., POOL = POOL_ADDR(P) |
| 996 | (the caller is asked to compute this because the macro expands POOL more than |
| 997 | once, and for efficiency it's best for the caller to assign POOL_ADDR(P) to a |
| 998 | variable and pass the latter to the macro; because Py_ADDRESS_IN_RANGE is |
| 999 | called on every alloc/realloc/free, micro-efficiency is important here). |
| 1000 | |
| 1001 | Tricky: Let B be the arena base address associated with the pool, B = |
| 1002 | arenas[(POOL)->arenaindex].address. Then P belongs to the arena if and only if |
| 1003 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1004 | B <= P < B + ARENA_SIZE |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1005 | |
| 1006 | Subtracting B throughout, this is true iff |
| 1007 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | 0 <= P-B < ARENA_SIZE |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1009 | |
| 1010 | By using unsigned arithmetic, the "0 <=" half of the test can be skipped. |
| 1011 | |
| 1012 | Obscure: A PyMem "free memory" function can call the pymalloc free or realloc |
| 1013 | before the first arena has been allocated. `arenas` is still NULL in that |
| 1014 | case. We're relying on that maxarenas is also 0 in that case, so that |
| 1015 | (POOL)->arenaindex < maxarenas must be false, saving us from trying to index |
| 1016 | into a NULL arenas. |
| 1017 | |
| 1018 | Details: given P and POOL, the arena_object corresponding to P is AO = |
| 1019 | arenas[(POOL)->arenaindex]. Suppose obmalloc controls P. Then (barring wild |
| 1020 | stores, etc), POOL is the correct address of P's pool, AO.address is the |
| 1021 | correct base address of the pool's arena, and P must be within ARENA_SIZE of |
| 1022 | AO.address. In addition, AO.address is not 0 (no arena can start at address 0 |
| 1023 | (NULL)). Therefore Py_ADDRESS_IN_RANGE correctly reports that obmalloc |
| 1024 | controls P. |
| 1025 | |
| 1026 | Now suppose obmalloc does not control P (e.g., P was obtained via a direct |
| 1027 | call to the system malloc() or realloc()). (POOL)->arenaindex may be anything |
| 1028 | in this case -- it may even be uninitialized trash. If the trash arenaindex |
| 1029 | is >= maxarenas, the macro correctly concludes at once that obmalloc doesn't |
| 1030 | control P. |
| 1031 | |
| 1032 | Else arenaindex is < maxarena, and AO is read up. If AO corresponds to an |
| 1033 | allocated arena, obmalloc controls all the memory in slice AO.address : |
| 1034 | AO.address+ARENA_SIZE. By case assumption, P is not controlled by obmalloc, |
| 1035 | so P doesn't lie in that slice, so the macro correctly reports that P is not |
| 1036 | controlled by obmalloc. |
| 1037 | |
| 1038 | Finally, if P is not controlled by obmalloc and AO corresponds to an unused |
| 1039 | arena_object (one not currently associated with an allocated arena), |
| 1040 | AO.address is 0, and the second test in the macro reduces to: |
| 1041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | P < ARENA_SIZE |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1043 | |
| 1044 | If P >= ARENA_SIZE (extremely likely), the macro again correctly concludes |
| 1045 | that P is not controlled by obmalloc. However, if P < ARENA_SIZE, this part |
| 1046 | of the test still passes, and the third clause (AO.address != 0) is necessary |
| 1047 | to get the correct result: AO.address is 0 in this case, so the macro |
| 1048 | correctly reports that P is not controlled by obmalloc (despite that P lies in |
| 1049 | slice AO.address : AO.address + ARENA_SIZE). |
| 1050 | |
| 1051 | Note: The third (AO.address != 0) clause was added in Python 2.5. Before |
| 1052 | 2.5, arenas were never free()'ed, and an arenaindex < maxarena always |
| 1053 | corresponded to a currently-allocated arena, so the "P is not controlled by |
| 1054 | obmalloc, AO corresponds to an unused arena_object, and P < ARENA_SIZE" case |
| 1055 | was impossible. |
| 1056 | |
| 1057 | Note that the logic is excruciating, and reading up possibly uninitialized |
| 1058 | memory when P is not controlled by obmalloc (to get at (POOL)->arenaindex) |
| 1059 | creates problems for some memory debuggers. The overwhelming advantage is |
| 1060 | that this test determines whether an arbitrary address is controlled by |
| 1061 | obmalloc in a small constant time, independent of the number of arenas |
| 1062 | obmalloc controls. Since this test is needed at every entry point, it's |
| 1063 | extremely desirable that it be this fast. |
Antoine Pitrou | b7fb2e2 | 2011-01-07 21:43:59 +0000 | [diff] [blame] | 1064 | |
| 1065 | Since Py_ADDRESS_IN_RANGE may be reading from memory which was not allocated |
| 1066 | by Python, it is important that (POOL)->arenaindex is read only once, as |
| 1067 | another thread may be concurrently modifying the value without holding the |
| 1068 | GIL. To accomplish this, the arenaindex_temp variable is used to store |
| 1069 | (POOL)->arenaindex for the duration of the Py_ADDRESS_IN_RANGE macro's |
| 1070 | execution. The caller of the macro is responsible for declaring this |
| 1071 | variable. |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1072 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | #define Py_ADDRESS_IN_RANGE(P, POOL) \ |
Antoine Pitrou | b7fb2e2 | 2011-01-07 21:43:59 +0000 | [diff] [blame] | 1074 | ((arenaindex_temp = (POOL)->arenaindex) < maxarenas && \ |
| 1075 | (uptr)(P) - arenas[arenaindex_temp].address < (uptr)ARENA_SIZE && \ |
| 1076 | arenas[arenaindex_temp].address != 0) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1077 | |
Neal Norwitz | 7eb3c91 | 2004-06-06 19:20:22 +0000 | [diff] [blame] | 1078 | |
| 1079 | /* This is only useful when running memory debuggers such as |
| 1080 | * Purify or Valgrind. Uncomment to use. |
| 1081 | * |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1082 | #define Py_USING_MEMORY_DEBUGGER |
Martin v. Löwis | 6fea233 | 2008-09-25 04:15:27 +0000 | [diff] [blame] | 1083 | */ |
Neal Norwitz | 7eb3c91 | 2004-06-06 19:20:22 +0000 | [diff] [blame] | 1084 | |
| 1085 | #ifdef Py_USING_MEMORY_DEBUGGER |
| 1086 | |
| 1087 | /* Py_ADDRESS_IN_RANGE may access uninitialized memory by design |
| 1088 | * This leads to thousands of spurious warnings when using |
| 1089 | * Purify or Valgrind. By making a function, we can easily |
| 1090 | * suppress the uninitialized memory reads in this one function. |
| 1091 | * So we won't ignore real errors elsewhere. |
| 1092 | * |
| 1093 | * Disable the macro and use a function. |
| 1094 | */ |
| 1095 | |
| 1096 | #undef Py_ADDRESS_IN_RANGE |
| 1097 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1098 | #if defined(__GNUC__) && ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) || \ |
Stefan Krah | 735bb12 | 2010-11-26 10:54:09 +0000 | [diff] [blame] | 1099 | (__GNUC__ >= 4)) |
Neal Norwitz | e5e5aa4 | 2005-11-13 18:55:39 +0000 | [diff] [blame] | 1100 | #define Py_NO_INLINE __attribute__((__noinline__)) |
| 1101 | #else |
| 1102 | #define Py_NO_INLINE |
| 1103 | #endif |
| 1104 | |
| 1105 | /* Don't make static, to try to ensure this isn't inlined. */ |
| 1106 | int Py_ADDRESS_IN_RANGE(void *P, poolp pool) Py_NO_INLINE; |
| 1107 | #undef Py_NO_INLINE |
Neal Norwitz | 7eb3c91 | 2004-06-06 19:20:22 +0000 | [diff] [blame] | 1108 | #endif |
Tim Peters | 338e010 | 2002-04-01 19:23:44 +0000 | [diff] [blame] | 1109 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1110 | /*==========================================================================*/ |
| 1111 | |
Tim Peters | 84c1b97 | 2002-04-04 04:44:32 +0000 | [diff] [blame] | 1112 | /* malloc. Note that nbytes==0 tries to return a non-NULL pointer, distinct |
| 1113 | * from all other currently live pointers. This may not be possible. |
| 1114 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1115 | |
| 1116 | /* |
| 1117 | * The basic blocks are ordered by decreasing execution frequency, |
| 1118 | * which minimizes the number of jumps in the most common cases, |
| 1119 | * improves branching prediction and instruction scheduling (small |
| 1120 | * block allocations typically result in a couple of instructions). |
| 1121 | * Unless the optimizer reorders everything, being too smart... |
| 1122 | */ |
| 1123 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1124 | static void * |
| 1125 | _PyObject_Malloc(void *ctx, size_t nbytes) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1126 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1127 | block *bp; |
| 1128 | poolp pool; |
| 1129 | poolp next; |
| 1130 | uint size; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1131 | |
Antoine Pitrou | 0aaaa62 | 2013-04-06 01:15:30 +0200 | [diff] [blame] | 1132 | _Py_AllocatedBlocks++; |
| 1133 | |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 1134 | #ifdef WITH_VALGRIND |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | if (UNLIKELY(running_on_valgrind == -1)) |
| 1136 | running_on_valgrind = RUNNING_ON_VALGRIND; |
| 1137 | if (UNLIKELY(running_on_valgrind)) |
| 1138 | goto redirect; |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 1139 | #endif |
| 1140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1141 | /* |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1142 | * This implicitly redirects malloc(0). |
| 1143 | */ |
| 1144 | if ((nbytes - 1) < SMALL_REQUEST_THRESHOLD) { |
| 1145 | LOCK(); |
| 1146 | /* |
| 1147 | * Most frequent paths first |
| 1148 | */ |
| 1149 | size = (uint)(nbytes - 1) >> ALIGNMENT_SHIFT; |
| 1150 | pool = usedpools[size + size]; |
| 1151 | if (pool != pool->nextpool) { |
| 1152 | /* |
| 1153 | * There is a used pool for this size class. |
| 1154 | * Pick up the head block of its free list. |
| 1155 | */ |
| 1156 | ++pool->ref.count; |
| 1157 | bp = pool->freeblock; |
| 1158 | assert(bp != NULL); |
| 1159 | if ((pool->freeblock = *(block **)bp) != NULL) { |
| 1160 | UNLOCK(); |
| 1161 | return (void *)bp; |
| 1162 | } |
| 1163 | /* |
| 1164 | * Reached the end of the free list, try to extend it. |
| 1165 | */ |
| 1166 | if (pool->nextoffset <= pool->maxnextoffset) { |
| 1167 | /* There is room for another block. */ |
| 1168 | pool->freeblock = (block*)pool + |
| 1169 | pool->nextoffset; |
| 1170 | pool->nextoffset += INDEX2SIZE(size); |
| 1171 | *(block **)(pool->freeblock) = NULL; |
| 1172 | UNLOCK(); |
| 1173 | return (void *)bp; |
| 1174 | } |
| 1175 | /* Pool is full, unlink from used pools. */ |
| 1176 | next = pool->nextpool; |
| 1177 | pool = pool->prevpool; |
| 1178 | next->prevpool = pool; |
| 1179 | pool->nextpool = next; |
| 1180 | UNLOCK(); |
| 1181 | return (void *)bp; |
| 1182 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 | /* There isn't a pool of the right size class immediately |
| 1185 | * available: use a free pool. |
| 1186 | */ |
| 1187 | if (usable_arenas == NULL) { |
| 1188 | /* No arena has a free pool: allocate a new arena. */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1189 | #ifdef WITH_MEMORY_LIMITS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1190 | if (narenas_currently_allocated >= MAX_ARENAS) { |
| 1191 | UNLOCK(); |
| 1192 | goto redirect; |
| 1193 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1194 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1195 | usable_arenas = new_arena(); |
| 1196 | if (usable_arenas == NULL) { |
| 1197 | UNLOCK(); |
| 1198 | goto redirect; |
| 1199 | } |
| 1200 | usable_arenas->nextarena = |
| 1201 | usable_arenas->prevarena = NULL; |
| 1202 | } |
| 1203 | assert(usable_arenas->address != 0); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1205 | /* Try to get a cached free pool. */ |
| 1206 | pool = usable_arenas->freepools; |
| 1207 | if (pool != NULL) { |
| 1208 | /* Unlink from cached pools. */ |
| 1209 | usable_arenas->freepools = pool->nextpool; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | /* This arena already had the smallest nfreepools |
| 1212 | * value, so decreasing nfreepools doesn't change |
| 1213 | * that, and we don't need to rearrange the |
| 1214 | * usable_arenas list. However, if the arena has |
| 1215 | * become wholly allocated, we need to remove its |
| 1216 | * arena_object from usable_arenas. |
| 1217 | */ |
| 1218 | --usable_arenas->nfreepools; |
| 1219 | if (usable_arenas->nfreepools == 0) { |
| 1220 | /* Wholly allocated: remove. */ |
| 1221 | assert(usable_arenas->freepools == NULL); |
| 1222 | assert(usable_arenas->nextarena == NULL || |
| 1223 | usable_arenas->nextarena->prevarena == |
| 1224 | usable_arenas); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1226 | usable_arenas = usable_arenas->nextarena; |
| 1227 | if (usable_arenas != NULL) { |
| 1228 | usable_arenas->prevarena = NULL; |
| 1229 | assert(usable_arenas->address != 0); |
| 1230 | } |
| 1231 | } |
| 1232 | else { |
| 1233 | /* nfreepools > 0: it must be that freepools |
| 1234 | * isn't NULL, or that we haven't yet carved |
| 1235 | * off all the arena's pools for the first |
| 1236 | * time. |
| 1237 | */ |
| 1238 | assert(usable_arenas->freepools != NULL || |
| 1239 | usable_arenas->pool_address <= |
| 1240 | (block*)usable_arenas->address + |
| 1241 | ARENA_SIZE - POOL_SIZE); |
| 1242 | } |
| 1243 | init_pool: |
| 1244 | /* Frontlink to used pools. */ |
| 1245 | next = usedpools[size + size]; /* == prev */ |
| 1246 | pool->nextpool = next; |
| 1247 | pool->prevpool = next; |
| 1248 | next->nextpool = pool; |
| 1249 | next->prevpool = pool; |
| 1250 | pool->ref.count = 1; |
| 1251 | if (pool->szidx == size) { |
| 1252 | /* Luckily, this pool last contained blocks |
| 1253 | * of the same size class, so its header |
| 1254 | * and free list are already initialized. |
| 1255 | */ |
| 1256 | bp = pool->freeblock; |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 1257 | assert(bp != NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1258 | pool->freeblock = *(block **)bp; |
| 1259 | UNLOCK(); |
| 1260 | return (void *)bp; |
| 1261 | } |
| 1262 | /* |
| 1263 | * Initialize the pool header, set up the free list to |
| 1264 | * contain just the second block, and return the first |
| 1265 | * block. |
| 1266 | */ |
| 1267 | pool->szidx = size; |
| 1268 | size = INDEX2SIZE(size); |
| 1269 | bp = (block *)pool + POOL_OVERHEAD; |
| 1270 | pool->nextoffset = POOL_OVERHEAD + (size << 1); |
| 1271 | pool->maxnextoffset = POOL_SIZE - size; |
| 1272 | pool->freeblock = bp + size; |
| 1273 | *(block **)(pool->freeblock) = NULL; |
| 1274 | UNLOCK(); |
| 1275 | return (void *)bp; |
| 1276 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1278 | /* Carve off a new pool. */ |
| 1279 | assert(usable_arenas->nfreepools > 0); |
| 1280 | assert(usable_arenas->freepools == NULL); |
| 1281 | pool = (poolp)usable_arenas->pool_address; |
| 1282 | assert((block*)pool <= (block*)usable_arenas->address + |
| 1283 | ARENA_SIZE - POOL_SIZE); |
| 1284 | pool->arenaindex = usable_arenas - arenas; |
| 1285 | assert(&arenas[pool->arenaindex] == usable_arenas); |
| 1286 | pool->szidx = DUMMY_SIZE_IDX; |
| 1287 | usable_arenas->pool_address += POOL_SIZE; |
| 1288 | --usable_arenas->nfreepools; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1290 | if (usable_arenas->nfreepools == 0) { |
| 1291 | assert(usable_arenas->nextarena == NULL || |
| 1292 | usable_arenas->nextarena->prevarena == |
| 1293 | usable_arenas); |
| 1294 | /* Unlink the arena: it is completely allocated. */ |
| 1295 | usable_arenas = usable_arenas->nextarena; |
| 1296 | if (usable_arenas != NULL) { |
| 1297 | usable_arenas->prevarena = NULL; |
| 1298 | assert(usable_arenas->address != 0); |
| 1299 | } |
| 1300 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1302 | goto init_pool; |
| 1303 | } |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | /* The small block allocator ends here. */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1306 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 1307 | redirect: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1308 | /* Redirect the original request to the underlying (libc) allocator. |
| 1309 | * We jump here on bigger requests, on error in the code above (as a |
| 1310 | * last chance to serve the request) or when the max memory limit |
| 1311 | * has been reached. |
| 1312 | */ |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 1313 | { |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 1314 | void *result = PyMem_RawMalloc(nbytes); |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 1315 | if (!result) |
| 1316 | _Py_AllocatedBlocks--; |
| 1317 | return result; |
| 1318 | } |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | /* free */ |
| 1322 | |
Nick Coghlan | 6ba64f4 | 2013-09-29 00:28:55 +1000 | [diff] [blame] | 1323 | ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1324 | static void |
| 1325 | _PyObject_Free(void *ctx, void *p) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1326 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | poolp pool; |
| 1328 | block *lastfree; |
| 1329 | poolp next, prev; |
| 1330 | uint size; |
Antoine Pitrou | b7fb2e2 | 2011-01-07 21:43:59 +0000 | [diff] [blame] | 1331 | #ifndef Py_USING_MEMORY_DEBUGGER |
| 1332 | uint arenaindex_temp; |
| 1333 | #endif |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | if (p == NULL) /* free(NULL) has no effect */ |
| 1336 | return; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1337 | |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 1338 | _Py_AllocatedBlocks--; |
| 1339 | |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 1340 | #ifdef WITH_VALGRIND |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | if (UNLIKELY(running_on_valgrind > 0)) |
| 1342 | goto redirect; |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 1343 | #endif |
| 1344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | pool = POOL_ADDR(p); |
| 1346 | if (Py_ADDRESS_IN_RANGE(p, pool)) { |
| 1347 | /* We allocated this address. */ |
| 1348 | LOCK(); |
| 1349 | /* Link p to the start of the pool's freeblock list. Since |
| 1350 | * the pool had at least the p block outstanding, the pool |
| 1351 | * wasn't empty (so it's already in a usedpools[] list, or |
| 1352 | * was full and is in no list -- it's not in the freeblocks |
| 1353 | * list in any case). |
| 1354 | */ |
| 1355 | assert(pool->ref.count > 0); /* else it was empty */ |
| 1356 | *(block **)p = lastfree = pool->freeblock; |
| 1357 | pool->freeblock = (block *)p; |
| 1358 | if (lastfree) { |
| 1359 | struct arena_object* ao; |
| 1360 | uint nf; /* ao->nfreepools */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1362 | /* freeblock wasn't NULL, so the pool wasn't full, |
| 1363 | * and the pool is in a usedpools[] list. |
| 1364 | */ |
| 1365 | if (--pool->ref.count != 0) { |
| 1366 | /* pool isn't empty: leave it in usedpools */ |
| 1367 | UNLOCK(); |
| 1368 | return; |
| 1369 | } |
| 1370 | /* Pool is now empty: unlink from usedpools, and |
| 1371 | * link to the front of freepools. This ensures that |
| 1372 | * previously freed pools will be allocated later |
| 1373 | * (being not referenced, they are perhaps paged out). |
| 1374 | */ |
| 1375 | next = pool->nextpool; |
| 1376 | prev = pool->prevpool; |
| 1377 | next->prevpool = prev; |
| 1378 | prev->nextpool = next; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | /* Link the pool to freepools. This is a singly-linked |
| 1381 | * list, and pool->prevpool isn't used there. |
| 1382 | */ |
| 1383 | ao = &arenas[pool->arenaindex]; |
| 1384 | pool->nextpool = ao->freepools; |
| 1385 | ao->freepools = pool; |
| 1386 | nf = ++ao->nfreepools; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1388 | /* All the rest is arena management. We just freed |
| 1389 | * a pool, and there are 4 cases for arena mgmt: |
| 1390 | * 1. If all the pools are free, return the arena to |
| 1391 | * the system free(). |
| 1392 | * 2. If this is the only free pool in the arena, |
| 1393 | * add the arena back to the `usable_arenas` list. |
| 1394 | * 3. If the "next" arena has a smaller count of free |
| 1395 | * pools, we have to "slide this arena right" to |
| 1396 | * restore that usable_arenas is sorted in order of |
| 1397 | * nfreepools. |
| 1398 | * 4. Else there's nothing more to do. |
| 1399 | */ |
| 1400 | if (nf == ao->ntotalpools) { |
| 1401 | /* Case 1. First unlink ao from usable_arenas. |
| 1402 | */ |
| 1403 | assert(ao->prevarena == NULL || |
| 1404 | ao->prevarena->address != 0); |
| 1405 | assert(ao ->nextarena == NULL || |
| 1406 | ao->nextarena->address != 0); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1408 | /* Fix the pointer in the prevarena, or the |
| 1409 | * usable_arenas pointer. |
| 1410 | */ |
| 1411 | if (ao->prevarena == NULL) { |
| 1412 | usable_arenas = ao->nextarena; |
| 1413 | assert(usable_arenas == NULL || |
| 1414 | usable_arenas->address != 0); |
| 1415 | } |
| 1416 | else { |
| 1417 | assert(ao->prevarena->nextarena == ao); |
| 1418 | ao->prevarena->nextarena = |
| 1419 | ao->nextarena; |
| 1420 | } |
| 1421 | /* Fix the pointer in the nextarena. */ |
| 1422 | if (ao->nextarena != NULL) { |
| 1423 | assert(ao->nextarena->prevarena == ao); |
| 1424 | ao->nextarena->prevarena = |
| 1425 | ao->prevarena; |
| 1426 | } |
| 1427 | /* Record that this arena_object slot is |
| 1428 | * available to be reused. |
| 1429 | */ |
| 1430 | ao->nextarena = unused_arena_objects; |
| 1431 | unused_arena_objects = ao; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1433 | /* Free the entire arena. */ |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1434 | _PyObject_Arena.free(_PyObject_Arena.ctx, |
| 1435 | (void *)ao->address, ARENA_SIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1436 | ao->address = 0; /* mark unassociated */ |
| 1437 | --narenas_currently_allocated; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1439 | UNLOCK(); |
| 1440 | return; |
| 1441 | } |
| 1442 | if (nf == 1) { |
| 1443 | /* Case 2. Put ao at the head of |
| 1444 | * usable_arenas. Note that because |
| 1445 | * ao->nfreepools was 0 before, ao isn't |
| 1446 | * currently on the usable_arenas list. |
| 1447 | */ |
| 1448 | ao->nextarena = usable_arenas; |
| 1449 | ao->prevarena = NULL; |
| 1450 | if (usable_arenas) |
| 1451 | usable_arenas->prevarena = ao; |
| 1452 | usable_arenas = ao; |
| 1453 | assert(usable_arenas->address != 0); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1454 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1455 | UNLOCK(); |
| 1456 | return; |
| 1457 | } |
| 1458 | /* If this arena is now out of order, we need to keep |
| 1459 | * the list sorted. The list is kept sorted so that |
| 1460 | * the "most full" arenas are used first, which allows |
| 1461 | * the nearly empty arenas to be completely freed. In |
| 1462 | * a few un-scientific tests, it seems like this |
| 1463 | * approach allowed a lot more memory to be freed. |
| 1464 | */ |
| 1465 | if (ao->nextarena == NULL || |
| 1466 | nf <= ao->nextarena->nfreepools) { |
| 1467 | /* Case 4. Nothing to do. */ |
| 1468 | UNLOCK(); |
| 1469 | return; |
| 1470 | } |
| 1471 | /* Case 3: We have to move the arena towards the end |
| 1472 | * of the list, because it has more free pools than |
| 1473 | * the arena to its right. |
| 1474 | * First unlink ao from usable_arenas. |
| 1475 | */ |
| 1476 | if (ao->prevarena != NULL) { |
| 1477 | /* ao isn't at the head of the list */ |
| 1478 | assert(ao->prevarena->nextarena == ao); |
| 1479 | ao->prevarena->nextarena = ao->nextarena; |
| 1480 | } |
| 1481 | else { |
| 1482 | /* ao is at the head of the list */ |
| 1483 | assert(usable_arenas == ao); |
| 1484 | usable_arenas = ao->nextarena; |
| 1485 | } |
| 1486 | ao->nextarena->prevarena = ao->prevarena; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1487 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1488 | /* Locate the new insertion point by iterating over |
| 1489 | * the list, using our nextarena pointer. |
| 1490 | */ |
| 1491 | while (ao->nextarena != NULL && |
| 1492 | nf > ao->nextarena->nfreepools) { |
| 1493 | ao->prevarena = ao->nextarena; |
| 1494 | ao->nextarena = ao->nextarena->nextarena; |
| 1495 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1497 | /* Insert ao at this point. */ |
| 1498 | assert(ao->nextarena == NULL || |
| 1499 | ao->prevarena == ao->nextarena->prevarena); |
| 1500 | assert(ao->prevarena->nextarena == ao->nextarena); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1501 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1502 | ao->prevarena->nextarena = ao; |
| 1503 | if (ao->nextarena != NULL) |
| 1504 | ao->nextarena->prevarena = ao; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1506 | /* Verify that the swaps worked. */ |
| 1507 | assert(ao->nextarena == NULL || |
| 1508 | nf <= ao->nextarena->nfreepools); |
| 1509 | assert(ao->prevarena == NULL || |
| 1510 | nf > ao->prevarena->nfreepools); |
| 1511 | assert(ao->nextarena == NULL || |
| 1512 | ao->nextarena->prevarena == ao); |
| 1513 | assert((usable_arenas == ao && |
| 1514 | ao->prevarena == NULL) || |
| 1515 | ao->prevarena->nextarena == ao); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1516 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1517 | UNLOCK(); |
| 1518 | return; |
| 1519 | } |
| 1520 | /* Pool was full, so doesn't currently live in any list: |
| 1521 | * link it to the front of the appropriate usedpools[] list. |
| 1522 | * This mimics LRU pool usage for new allocations and |
| 1523 | * targets optimal filling when several pools contain |
| 1524 | * blocks of the same size class. |
| 1525 | */ |
| 1526 | --pool->ref.count; |
| 1527 | assert(pool->ref.count > 0); /* else the pool is empty */ |
| 1528 | size = pool->szidx; |
| 1529 | next = usedpools[size + size]; |
| 1530 | prev = next->prevpool; |
| 1531 | /* insert pool before next: prev <-> pool <-> next */ |
| 1532 | pool->nextpool = next; |
| 1533 | pool->prevpool = prev; |
| 1534 | next->prevpool = pool; |
| 1535 | prev->nextpool = pool; |
| 1536 | UNLOCK(); |
| 1537 | return; |
| 1538 | } |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1539 | |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 1540 | #ifdef WITH_VALGRIND |
| 1541 | redirect: |
| 1542 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1543 | /* We didn't allocate this address. */ |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 1544 | PyMem_RawFree(p); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Tim Peters | 84c1b97 | 2002-04-04 04:44:32 +0000 | [diff] [blame] | 1547 | /* realloc. If p is NULL, this acts like malloc(nbytes). Else if nbytes==0, |
| 1548 | * then as the Python docs promise, we do not treat this like free(p), and |
| 1549 | * return a non-NULL result. |
| 1550 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1551 | |
Nick Coghlan | 6ba64f4 | 2013-09-29 00:28:55 +1000 | [diff] [blame] | 1552 | ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1553 | static void * |
| 1554 | _PyObject_Realloc(void *ctx, void *p, size_t nbytes) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1555 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1556 | void *bp; |
| 1557 | poolp pool; |
| 1558 | size_t size; |
Antoine Pitrou | b7fb2e2 | 2011-01-07 21:43:59 +0000 | [diff] [blame] | 1559 | #ifndef Py_USING_MEMORY_DEBUGGER |
| 1560 | uint arenaindex_temp; |
| 1561 | #endif |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1563 | if (p == NULL) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1564 | return _PyObject_Malloc(ctx, nbytes); |
Georg Brandl | d492ad8 | 2008-07-23 16:13:07 +0000 | [diff] [blame] | 1565 | |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 1566 | #ifdef WITH_VALGRIND |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1567 | /* Treat running_on_valgrind == -1 the same as 0 */ |
| 1568 | if (UNLIKELY(running_on_valgrind > 0)) |
| 1569 | goto redirect; |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 1570 | #endif |
| 1571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1572 | pool = POOL_ADDR(p); |
| 1573 | if (Py_ADDRESS_IN_RANGE(p, pool)) { |
| 1574 | /* We're in charge of this block */ |
| 1575 | size = INDEX2SIZE(pool->szidx); |
| 1576 | if (nbytes <= size) { |
| 1577 | /* The block is staying the same or shrinking. If |
| 1578 | * it's shrinking, there's a tradeoff: it costs |
| 1579 | * cycles to copy the block to a smaller size class, |
| 1580 | * but it wastes memory not to copy it. The |
| 1581 | * compromise here is to copy on shrink only if at |
| 1582 | * least 25% of size can be shaved off. |
| 1583 | */ |
| 1584 | if (4 * nbytes > 3 * size) { |
| 1585 | /* It's the same, |
| 1586 | * or shrinking and new/old > 3/4. |
| 1587 | */ |
| 1588 | return p; |
| 1589 | } |
| 1590 | size = nbytes; |
| 1591 | } |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1592 | bp = _PyObject_Malloc(ctx, nbytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1593 | if (bp != NULL) { |
| 1594 | memcpy(bp, p, size); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1595 | _PyObject_Free(ctx, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1596 | } |
| 1597 | return bp; |
| 1598 | } |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 1599 | #ifdef WITH_VALGRIND |
| 1600 | redirect: |
| 1601 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1602 | /* We're not managing this block. If nbytes <= |
| 1603 | * SMALL_REQUEST_THRESHOLD, it's tempting to try to take over this |
| 1604 | * block. However, if we do, we need to copy the valid data from |
| 1605 | * the C-managed block to one of our blocks, and there's no portable |
| 1606 | * way to know how much of the memory space starting at p is valid. |
| 1607 | * As bug 1185883 pointed out the hard way, it's possible that the |
| 1608 | * C-managed block is "at the end" of allocated VM space, so that |
| 1609 | * a memory fault can occur if we try to copy nbytes bytes starting |
| 1610 | * at p. Instead we punt: let C continue to manage this block. |
| 1611 | */ |
| 1612 | if (nbytes) |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 1613 | return PyMem_RawRealloc(p, nbytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1614 | /* C doesn't define the result of realloc(p, 0) (it may or may not |
| 1615 | * return NULL then), but Python's docs promise that nbytes==0 never |
| 1616 | * returns NULL. We don't pass 0 to realloc(), to avoid that endcase |
| 1617 | * to begin with. Even then, we can't be sure that realloc() won't |
| 1618 | * return NULL. |
| 1619 | */ |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 1620 | bp = PyMem_RawRealloc(p, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1621 | return bp ? bp : p; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1624 | #else /* ! WITH_PYMALLOC */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1625 | |
| 1626 | /*==========================================================================*/ |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1627 | /* pymalloc not enabled: Redirect the entry points to malloc. These will |
| 1628 | * only be used by extensions that are compiled with pymalloc enabled. */ |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 1629 | |
Antoine Pitrou | 9284053 | 2012-12-17 23:05:59 +0100 | [diff] [blame] | 1630 | Py_ssize_t |
| 1631 | _Py_GetAllocatedBlocks(void) |
| 1632 | { |
| 1633 | return 0; |
| 1634 | } |
| 1635 | |
Tim Peters | 1221c0a | 2002-03-23 00:20:15 +0000 | [diff] [blame] | 1636 | #endif /* WITH_PYMALLOC */ |
| 1637 | |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1638 | #ifdef PYMALLOC_DEBUG |
| 1639 | /*==========================================================================*/ |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 1640 | /* A x-platform debugging allocator. This doesn't manage memory directly, |
| 1641 | * it wraps a real allocator, adding extra debugging info to the memory blocks. |
| 1642 | */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1643 | |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1644 | /* Special bytes broadcast into debug memory blocks at appropriate times. |
| 1645 | * Strings of these are unlikely to be valid addresses, floats, ints or |
| 1646 | * 7-bit ASCII. |
| 1647 | */ |
| 1648 | #undef CLEANBYTE |
| 1649 | #undef DEADBYTE |
| 1650 | #undef FORBIDDENBYTE |
| 1651 | #define CLEANBYTE 0xCB /* clean (newly allocated) memory */ |
Tim Peters | 889f61d | 2002-07-10 19:29:49 +0000 | [diff] [blame] | 1652 | #define DEADBYTE 0xDB /* dead (newly freed) memory */ |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1653 | #define FORBIDDENBYTE 0xFB /* untouchable bytes at each end of a block */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1655 | static size_t serialno = 0; /* incremented on each debug {m,re}alloc */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1656 | |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 1657 | /* serialno is always incremented via calling this routine. The point is |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1658 | * to supply a single place to set a breakpoint. |
| 1659 | */ |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 1660 | static void |
Neil Schemenauer | bd02b14 | 2002-03-28 21:05:38 +0000 | [diff] [blame] | 1661 | bumpserialno(void) |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 1662 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1663 | ++serialno; |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1666 | #define SST SIZEOF_SIZE_T |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 1667 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1668 | /* Read sizeof(size_t) bytes at p as a big-endian size_t. */ |
| 1669 | static size_t |
| 1670 | read_size_t(const void *p) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1671 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1672 | const uchar *q = (const uchar *)p; |
| 1673 | size_t result = *q++; |
| 1674 | int i; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1676 | for (i = SST; --i > 0; ++q) |
| 1677 | result = (result << 8) | *q; |
| 1678 | return result; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1681 | /* Write n as a big-endian size_t, MSB at address p, LSB at |
| 1682 | * p + sizeof(size_t) - 1. |
| 1683 | */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1684 | static void |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1685 | write_size_t(void *p, size_t n) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1686 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1687 | uchar *q = (uchar *)p + SST - 1; |
| 1688 | int i; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1690 | for (i = SST; --i >= 0; --q) { |
| 1691 | *q = (uchar)(n & 0xff); |
| 1692 | n >>= 8; |
| 1693 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
Tim Peters | 08d8215 | 2002-04-18 22:25:03 +0000 | [diff] [blame] | 1696 | #ifdef Py_DEBUG |
| 1697 | /* Is target in the list? The list is traversed via the nextpool pointers. |
| 1698 | * The list may be NULL-terminated, or circular. Return 1 if target is in |
| 1699 | * list, else 0. |
| 1700 | */ |
| 1701 | static int |
| 1702 | pool_is_in_list(const poolp target, poolp list) |
| 1703 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1704 | poolp origlist = list; |
| 1705 | assert(target != NULL); |
| 1706 | if (list == NULL) |
| 1707 | return 0; |
| 1708 | do { |
| 1709 | if (target == list) |
| 1710 | return 1; |
| 1711 | list = list->nextpool; |
| 1712 | } while (list != NULL && list != origlist); |
| 1713 | return 0; |
Tim Peters | 08d8215 | 2002-04-18 22:25:03 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | #else |
| 1717 | #define pool_is_in_list(X, Y) 1 |
| 1718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | #endif /* Py_DEBUG */ |
Tim Peters | 08d8215 | 2002-04-18 22:25:03 +0000 | [diff] [blame] | 1720 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1721 | /* Let S = sizeof(size_t). The debug malloc asks for 4*S extra bytes and |
| 1722 | fills them with useful stuff, here calling the underlying malloc's result p: |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1723 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1724 | p[0: S] |
| 1725 | Number of bytes originally asked for. This is a size_t, big-endian (easier |
| 1726 | to read in a memory dump). |
Georg Brandl | 7cba5fd | 2013-09-25 09:04:23 +0200 | [diff] [blame] | 1727 | p[S] |
Tim Peters | df099f5 | 2013-09-19 21:06:37 -0500 | [diff] [blame] | 1728 | API ID. See PEP 445. This is a character, but seems undocumented. |
| 1729 | p[S+1: 2*S] |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1730 | Copies of FORBIDDENBYTE. Used to catch under- writes and reads. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1731 | p[2*S: 2*S+n] |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1732 | The requested memory, filled with copies of CLEANBYTE. |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1733 | Used to catch reference to uninitialized memory. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1734 | &p[2*S] is returned. Note that this is 8-byte aligned if pymalloc |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1735 | handled the request itself. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1736 | p[2*S+n: 2*S+n+S] |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1737 | Copies of FORBIDDENBYTE. Used to catch over- writes and reads. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1738 | p[2*S+n+S: 2*S+n+2*S] |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1739 | A serial number, incremented by 1 on each call to _PyMem_DebugMalloc |
| 1740 | and _PyMem_DebugRealloc. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1741 | This is a big-endian size_t. |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1742 | If "bad memory" is detected later, the serial number gives an |
| 1743 | excellent way to set a breakpoint on the next run, to capture the |
| 1744 | instant at which this block was passed out. |
| 1745 | */ |
| 1746 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1747 | static void * |
| 1748 | _PyMem_DebugMalloc(void *ctx, size_t nbytes) |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1749 | { |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1750 | debug_alloc_api_t *api = (debug_alloc_api_t *)ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1751 | uchar *p; /* base address of malloc'ed block */ |
| 1752 | uchar *tail; /* p + 2*SST + nbytes == pointer to tail pad bytes */ |
| 1753 | size_t total; /* nbytes + 4*SST */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1754 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1755 | bumpserialno(); |
| 1756 | total = nbytes + 4*SST; |
| 1757 | if (total < nbytes) |
| 1758 | /* overflow: can't represent total as a size_t */ |
| 1759 | return NULL; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1760 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1761 | p = (uchar *)api->alloc.malloc(api->alloc.ctx, total); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1762 | if (p == NULL) |
| 1763 | return NULL; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1764 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1765 | /* at p, write size (SST bytes), id (1 byte), pad (SST-1 bytes) */ |
| 1766 | write_size_t(p, nbytes); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1767 | p[SST] = (uchar)api->api_id; |
| 1768 | memset(p + SST + 1, FORBIDDENBYTE, SST-1); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1769 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1770 | if (nbytes > 0) |
| 1771 | memset(p + 2*SST, CLEANBYTE, nbytes); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1773 | /* at tail, write pad (SST bytes) and serialno (SST bytes) */ |
| 1774 | tail = p + 2*SST + nbytes; |
| 1775 | memset(tail, FORBIDDENBYTE, SST); |
| 1776 | write_size_t(tail + SST, serialno); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1778 | return p + 2*SST; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1781 | /* The debug free first checks the 2*SST bytes on each end for sanity (in |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1782 | particular, that the FORBIDDENBYTEs with the api ID are still intact). |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1783 | Then fills the original bytes with DEADBYTE. |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1784 | Then calls the underlying free. |
| 1785 | */ |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1786 | static void |
| 1787 | _PyMem_DebugFree(void *ctx, void *p) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1788 | { |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1789 | debug_alloc_api_t *api = (debug_alloc_api_t *)ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1790 | uchar *q = (uchar *)p - 2*SST; /* address returned from malloc */ |
| 1791 | size_t nbytes; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1793 | if (p == NULL) |
| 1794 | return; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1795 | _PyMem_DebugCheckAddress(api->api_id, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1796 | nbytes = read_size_t(q); |
| 1797 | nbytes += 4*SST; |
| 1798 | if (nbytes > 0) |
| 1799 | memset(q, DEADBYTE, nbytes); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1800 | api->alloc.free(api->alloc.ctx, q); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1803 | static void * |
| 1804 | _PyMem_DebugRealloc(void *ctx, void *p, size_t nbytes) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1805 | { |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1806 | debug_alloc_api_t *api = (debug_alloc_api_t *)ctx; |
Victor Stinner | c426636 | 2013-07-09 00:44:43 +0200 | [diff] [blame] | 1807 | uchar *q = (uchar *)p, *oldq; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | uchar *tail; |
| 1809 | size_t total; /* nbytes + 4*SST */ |
| 1810 | size_t original_nbytes; |
| 1811 | int i; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1813 | if (p == NULL) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1814 | return _PyMem_DebugMalloc(ctx, nbytes); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1815 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1816 | _PyMem_DebugCheckAddress(api->api_id, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1817 | bumpserialno(); |
| 1818 | original_nbytes = read_size_t(q - 2*SST); |
| 1819 | total = nbytes + 4*SST; |
| 1820 | if (total < nbytes) |
| 1821 | /* overflow: can't represent total as a size_t */ |
| 1822 | return NULL; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1823 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1824 | /* Resize and add decorations. We may get a new pointer here, in which |
| 1825 | * case we didn't get the chance to mark the old memory with DEADBYTE, |
| 1826 | * but we live with that. |
| 1827 | */ |
Victor Stinner | c426636 | 2013-07-09 00:44:43 +0200 | [diff] [blame] | 1828 | oldq = q; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1829 | q = (uchar *)api->alloc.realloc(api->alloc.ctx, q - 2*SST, total); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1830 | if (q == NULL) |
| 1831 | return NULL; |
Tim Peters | 85cc1c4 | 2002-04-12 08:52:50 +0000 | [diff] [blame] | 1832 | |
Victor Stinner | c426636 | 2013-07-09 00:44:43 +0200 | [diff] [blame] | 1833 | if (q == oldq && nbytes < original_nbytes) { |
| 1834 | /* shrinking: mark old extra memory dead */ |
| 1835 | memset(q + nbytes, DEADBYTE, original_nbytes - nbytes); |
| 1836 | } |
| 1837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1838 | write_size_t(q, nbytes); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1839 | assert(q[SST] == (uchar)api->api_id); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1840 | for (i = 1; i < SST; ++i) |
| 1841 | assert(q[SST + i] == FORBIDDENBYTE); |
| 1842 | q += 2*SST; |
Victor Stinner | c426636 | 2013-07-09 00:44:43 +0200 | [diff] [blame] | 1843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1844 | tail = q + nbytes; |
| 1845 | memset(tail, FORBIDDENBYTE, SST); |
| 1846 | write_size_t(tail + SST, serialno); |
Tim Peters | 85cc1c4 | 2002-04-12 08:52:50 +0000 | [diff] [blame] | 1847 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1848 | if (nbytes > original_nbytes) { |
| 1849 | /* growing: mark new extra memory clean */ |
| 1850 | memset(q + original_nbytes, CLEANBYTE, |
Stefan Krah | 735bb12 | 2010-11-26 10:54:09 +0000 | [diff] [blame] | 1851 | nbytes - original_nbytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1852 | } |
Tim Peters | 85cc1c4 | 2002-04-12 08:52:50 +0000 | [diff] [blame] | 1853 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | return q; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1857 | /* Check the forbidden bytes on both ends of the memory allocated for p. |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1858 | * If anything is wrong, print info to stderr via _PyObject_DebugDumpAddress, |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1859 | * and call Py_FatalError to kill the program. |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1860 | * The API id, is also checked. |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1861 | */ |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1862 | static void |
| 1863 | _PyMem_DebugCheckAddress(char api, const void *p) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1864 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1865 | const uchar *q = (const uchar *)p; |
| 1866 | char msgbuf[64]; |
| 1867 | char *msg; |
| 1868 | size_t nbytes; |
| 1869 | const uchar *tail; |
| 1870 | int i; |
| 1871 | char id; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1872 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1873 | if (p == NULL) { |
| 1874 | msg = "didn't expect a NULL pointer"; |
| 1875 | goto error; |
| 1876 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1877 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1878 | /* Check the API id */ |
| 1879 | id = (char)q[-SST]; |
| 1880 | if (id != api) { |
| 1881 | msg = msgbuf; |
| 1882 | snprintf(msg, sizeof(msgbuf), "bad ID: Allocated using API '%c', verified using API '%c'", id, api); |
| 1883 | msgbuf[sizeof(msgbuf)-1] = 0; |
| 1884 | goto error; |
| 1885 | } |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1887 | /* Check the stuff at the start of p first: if there's underwrite |
| 1888 | * corruption, the number-of-bytes field may be nuts, and checking |
| 1889 | * the tail could lead to a segfault then. |
| 1890 | */ |
| 1891 | for (i = SST-1; i >= 1; --i) { |
| 1892 | if (*(q-i) != FORBIDDENBYTE) { |
| 1893 | msg = "bad leading pad byte"; |
| 1894 | goto error; |
| 1895 | } |
| 1896 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 | nbytes = read_size_t(q - 2*SST); |
| 1899 | tail = q + nbytes; |
| 1900 | for (i = 0; i < SST; ++i) { |
| 1901 | if (tail[i] != FORBIDDENBYTE) { |
| 1902 | msg = "bad trailing pad byte"; |
| 1903 | goto error; |
| 1904 | } |
| 1905 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1907 | return; |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 1908 | |
| 1909 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | _PyObject_DebugDumpAddress(p); |
| 1911 | Py_FatalError(msg); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1914 | /* Display info to stderr about the memory block at p. */ |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1915 | static void |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1916 | _PyObject_DebugDumpAddress(const void *p) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1917 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1918 | const uchar *q = (const uchar *)p; |
| 1919 | const uchar *tail; |
| 1920 | size_t nbytes, serial; |
| 1921 | int i; |
| 1922 | int ok; |
| 1923 | char id; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1924 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1925 | fprintf(stderr, "Debug memory block at address p=%p:", p); |
| 1926 | if (p == NULL) { |
| 1927 | fprintf(stderr, "\n"); |
| 1928 | return; |
| 1929 | } |
| 1930 | id = (char)q[-SST]; |
| 1931 | fprintf(stderr, " API '%c'\n", id); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1933 | nbytes = read_size_t(q - 2*SST); |
| 1934 | fprintf(stderr, " %" PY_FORMAT_SIZE_T "u bytes originally " |
| 1935 | "requested\n", nbytes); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1936 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1937 | /* In case this is nuts, check the leading pad bytes first. */ |
| 1938 | fprintf(stderr, " The %d pad bytes at p-%d are ", SST-1, SST-1); |
| 1939 | ok = 1; |
| 1940 | for (i = 1; i <= SST-1; ++i) { |
| 1941 | if (*(q-i) != FORBIDDENBYTE) { |
| 1942 | ok = 0; |
| 1943 | break; |
| 1944 | } |
| 1945 | } |
| 1946 | if (ok) |
| 1947 | fputs("FORBIDDENBYTE, as expected.\n", stderr); |
| 1948 | else { |
| 1949 | fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n", |
| 1950 | FORBIDDENBYTE); |
| 1951 | for (i = SST-1; i >= 1; --i) { |
| 1952 | const uchar byte = *(q-i); |
| 1953 | fprintf(stderr, " at p-%d: 0x%02x", i, byte); |
| 1954 | if (byte != FORBIDDENBYTE) |
| 1955 | fputs(" *** OUCH", stderr); |
| 1956 | fputc('\n', stderr); |
| 1957 | } |
Tim Peters | 449b5a8 | 2002-04-28 06:14:45 +0000 | [diff] [blame] | 1958 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1959 | fputs(" Because memory is corrupted at the start, the " |
| 1960 | "count of bytes requested\n" |
| 1961 | " may be bogus, and checking the trailing pad " |
| 1962 | "bytes may segfault.\n", stderr); |
| 1963 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1965 | tail = q + nbytes; |
| 1966 | fprintf(stderr, " The %d pad bytes at tail=%p are ", SST, tail); |
| 1967 | ok = 1; |
| 1968 | for (i = 0; i < SST; ++i) { |
| 1969 | if (tail[i] != FORBIDDENBYTE) { |
| 1970 | ok = 0; |
| 1971 | break; |
| 1972 | } |
| 1973 | } |
| 1974 | if (ok) |
| 1975 | fputs("FORBIDDENBYTE, as expected.\n", stderr); |
| 1976 | else { |
| 1977 | fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n", |
Stefan Krah | 735bb12 | 2010-11-26 10:54:09 +0000 | [diff] [blame] | 1978 | FORBIDDENBYTE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1979 | for (i = 0; i < SST; ++i) { |
| 1980 | const uchar byte = tail[i]; |
| 1981 | fprintf(stderr, " at tail+%d: 0x%02x", |
Stefan Krah | 735bb12 | 2010-11-26 10:54:09 +0000 | [diff] [blame] | 1982 | i, byte); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1983 | if (byte != FORBIDDENBYTE) |
| 1984 | fputs(" *** OUCH", stderr); |
| 1985 | fputc('\n', stderr); |
| 1986 | } |
| 1987 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1988 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1989 | serial = read_size_t(tail + SST); |
| 1990 | fprintf(stderr, " The block was made by call #%" PY_FORMAT_SIZE_T |
| 1991 | "u to debug malloc/realloc.\n", serial); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1992 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1993 | if (nbytes > 0) { |
| 1994 | i = 0; |
| 1995 | fputs(" Data at p:", stderr); |
| 1996 | /* print up to 8 bytes at the start */ |
| 1997 | while (q < tail && i < 8) { |
| 1998 | fprintf(stderr, " %02x", *q); |
| 1999 | ++i; |
| 2000 | ++q; |
| 2001 | } |
| 2002 | /* and up to 8 at the end */ |
| 2003 | if (q < tail) { |
| 2004 | if (tail - q > 8) { |
| 2005 | fputs(" ...", stderr); |
| 2006 | q = tail - 8; |
| 2007 | } |
| 2008 | while (q < tail) { |
| 2009 | fprintf(stderr, " %02x", *q); |
| 2010 | ++q; |
| 2011 | } |
| 2012 | } |
| 2013 | fputc('\n', stderr); |
| 2014 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2017 | #endif /* PYMALLOC_DEBUG */ |
| 2018 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 2019 | static size_t |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2020 | printone(FILE *out, const char* msg, size_t value) |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 2021 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2022 | int i, k; |
| 2023 | char buf[100]; |
| 2024 | size_t origvalue = value; |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 2025 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2026 | fputs(msg, out); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2027 | for (i = (int)strlen(msg); i < 35; ++i) |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2028 | fputc(' ', out); |
| 2029 | fputc('=', out); |
Tim Peters | 49f2681 | 2002-04-06 01:45:35 +0000 | [diff] [blame] | 2030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2031 | /* Write the value with commas. */ |
| 2032 | i = 22; |
| 2033 | buf[i--] = '\0'; |
| 2034 | buf[i--] = '\n'; |
| 2035 | k = 3; |
| 2036 | do { |
| 2037 | size_t nextvalue = value / 10; |
Benjamin Peterson | 2dba1ee | 2013-02-20 16:54:30 -0500 | [diff] [blame] | 2038 | unsigned int digit = (unsigned int)(value - nextvalue * 10); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2039 | value = nextvalue; |
| 2040 | buf[i--] = (char)(digit + '0'); |
| 2041 | --k; |
| 2042 | if (k == 0 && value && i >= 0) { |
| 2043 | k = 3; |
| 2044 | buf[i--] = ','; |
| 2045 | } |
| 2046 | } while (value && i >= 0); |
Tim Peters | 49f2681 | 2002-04-06 01:45:35 +0000 | [diff] [blame] | 2047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2048 | while (i >= 0) |
| 2049 | buf[i--] = ' '; |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2050 | fputs(buf, out); |
Tim Peters | 49f2681 | 2002-04-06 01:45:35 +0000 | [diff] [blame] | 2051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2052 | return origvalue; |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2055 | void |
| 2056 | _PyDebugAllocatorStats(FILE *out, |
| 2057 | const char *block_name, int num_blocks, size_t sizeof_block) |
| 2058 | { |
| 2059 | char buf1[128]; |
| 2060 | char buf2[128]; |
| 2061 | PyOS_snprintf(buf1, sizeof(buf1), |
Tim Peters | eaa3bcc | 2013-09-05 22:57:04 -0500 | [diff] [blame] | 2062 | "%d %ss * %" PY_FORMAT_SIZE_T "d bytes each", |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2063 | num_blocks, block_name, sizeof_block); |
| 2064 | PyOS_snprintf(buf2, sizeof(buf2), |
| 2065 | "%48s ", buf1); |
| 2066 | (void)printone(out, buf2, num_blocks * sizeof_block); |
| 2067 | } |
| 2068 | |
| 2069 | #ifdef WITH_PYMALLOC |
| 2070 | |
| 2071 | /* Print summary info to "out" about the state of pymalloc's structures. |
Tim Peters | 08d8215 | 2002-04-18 22:25:03 +0000 | [diff] [blame] | 2072 | * In Py_DEBUG mode, also perform some expensive internal consistency |
| 2073 | * checks. |
| 2074 | */ |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 2075 | void |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2076 | _PyObject_DebugMallocStats(FILE *out) |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 2077 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2078 | uint i; |
| 2079 | const uint numclasses = SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT; |
| 2080 | /* # of pools, allocated blocks, and free blocks per class index */ |
| 2081 | size_t numpools[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT]; |
| 2082 | size_t numblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT]; |
| 2083 | size_t numfreeblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT]; |
| 2084 | /* total # of allocated bytes in used and full pools */ |
| 2085 | size_t allocated_bytes = 0; |
| 2086 | /* total # of available bytes in used pools */ |
| 2087 | size_t available_bytes = 0; |
| 2088 | /* # of free pools + pools not yet carved out of current arena */ |
| 2089 | uint numfreepools = 0; |
| 2090 | /* # of bytes for arena alignment padding */ |
| 2091 | size_t arena_alignment = 0; |
| 2092 | /* # of bytes in used and full pools used for pool_headers */ |
| 2093 | size_t pool_header_bytes = 0; |
| 2094 | /* # of bytes in used and full pools wasted due to quantization, |
| 2095 | * i.e. the necessarily leftover space at the ends of used and |
| 2096 | * full pools. |
| 2097 | */ |
| 2098 | size_t quantization = 0; |
| 2099 | /* # of arenas actually allocated. */ |
| 2100 | size_t narenas = 0; |
| 2101 | /* running total -- should equal narenas * ARENA_SIZE */ |
| 2102 | size_t total; |
| 2103 | char buf[128]; |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 2104 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2105 | fprintf(out, "Small block threshold = %d, in %u size classes.\n", |
Stefan Krah | 735bb12 | 2010-11-26 10:54:09 +0000 | [diff] [blame] | 2106 | SMALL_REQUEST_THRESHOLD, numclasses); |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 2107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2108 | for (i = 0; i < numclasses; ++i) |
| 2109 | numpools[i] = numblocks[i] = numfreeblocks[i] = 0; |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 2110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2111 | /* Because full pools aren't linked to from anything, it's easiest |
| 2112 | * to march over all the arenas. If we're lucky, most of the memory |
| 2113 | * will be living in full pools -- would be a shame to miss them. |
| 2114 | */ |
| 2115 | for (i = 0; i < maxarenas; ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2116 | uint j; |
| 2117 | uptr base = arenas[i].address; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2119 | /* Skip arenas which are not allocated. */ |
| 2120 | if (arenas[i].address == (uptr)NULL) |
| 2121 | continue; |
| 2122 | narenas += 1; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2124 | numfreepools += arenas[i].nfreepools; |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 2125 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2126 | /* round up to pool alignment */ |
| 2127 | if (base & (uptr)POOL_SIZE_MASK) { |
| 2128 | arena_alignment += POOL_SIZE; |
| 2129 | base &= ~(uptr)POOL_SIZE_MASK; |
| 2130 | base += POOL_SIZE; |
| 2131 | } |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 2132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2133 | /* visit every pool in the arena */ |
| 2134 | assert(base <= (uptr) arenas[i].pool_address); |
| 2135 | for (j = 0; |
| 2136 | base < (uptr) arenas[i].pool_address; |
| 2137 | ++j, base += POOL_SIZE) { |
| 2138 | poolp p = (poolp)base; |
| 2139 | const uint sz = p->szidx; |
| 2140 | uint freeblocks; |
Tim Peters | 08d8215 | 2002-04-18 22:25:03 +0000 | [diff] [blame] | 2141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2142 | if (p->ref.count == 0) { |
| 2143 | /* currently unused */ |
| 2144 | assert(pool_is_in_list(p, arenas[i].freepools)); |
| 2145 | continue; |
| 2146 | } |
| 2147 | ++numpools[sz]; |
| 2148 | numblocks[sz] += p->ref.count; |
| 2149 | freeblocks = NUMBLOCKS(sz) - p->ref.count; |
| 2150 | numfreeblocks[sz] += freeblocks; |
Tim Peters | 08d8215 | 2002-04-18 22:25:03 +0000 | [diff] [blame] | 2151 | #ifdef Py_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2152 | if (freeblocks > 0) |
| 2153 | assert(pool_is_in_list(p, usedpools[sz + sz])); |
Tim Peters | 08d8215 | 2002-04-18 22:25:03 +0000 | [diff] [blame] | 2154 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2155 | } |
| 2156 | } |
| 2157 | assert(narenas == narenas_currently_allocated); |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 2158 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2159 | fputc('\n', out); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2160 | fputs("class size num pools blocks in use avail blocks\n" |
| 2161 | "----- ---- --------- ------------- ------------\n", |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2162 | out); |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 2163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 | for (i = 0; i < numclasses; ++i) { |
| 2165 | size_t p = numpools[i]; |
| 2166 | size_t b = numblocks[i]; |
| 2167 | size_t f = numfreeblocks[i]; |
| 2168 | uint size = INDEX2SIZE(i); |
| 2169 | if (p == 0) { |
| 2170 | assert(b == 0 && f == 0); |
| 2171 | continue; |
| 2172 | } |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2173 | fprintf(out, "%5u %6u " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2174 | "%11" PY_FORMAT_SIZE_T "u " |
| 2175 | "%15" PY_FORMAT_SIZE_T "u " |
| 2176 | "%13" PY_FORMAT_SIZE_T "u\n", |
Stefan Krah | 735bb12 | 2010-11-26 10:54:09 +0000 | [diff] [blame] | 2177 | i, size, p, b, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2178 | allocated_bytes += b * size; |
| 2179 | available_bytes += f * size; |
| 2180 | pool_header_bytes += p * POOL_OVERHEAD; |
| 2181 | quantization += p * ((POOL_SIZE - POOL_OVERHEAD) % size); |
| 2182 | } |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2183 | fputc('\n', out); |
| 2184 | #ifdef PYMALLOC_DEBUG |
| 2185 | (void)printone(out, "# times object malloc called", serialno); |
| 2186 | #endif |
| 2187 | (void)printone(out, "# arenas allocated total", ntimes_arena_allocated); |
| 2188 | (void)printone(out, "# arenas reclaimed", ntimes_arena_allocated - narenas); |
| 2189 | (void)printone(out, "# arenas highwater mark", narenas_highwater); |
| 2190 | (void)printone(out, "# arenas allocated current", narenas); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2192 | PyOS_snprintf(buf, sizeof(buf), |
| 2193 | "%" PY_FORMAT_SIZE_T "u arenas * %d bytes/arena", |
| 2194 | narenas, ARENA_SIZE); |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2195 | (void)printone(out, buf, narenas * ARENA_SIZE); |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 2196 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2197 | fputc('\n', out); |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 2198 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2199 | total = printone(out, "# bytes in allocated blocks", allocated_bytes); |
| 2200 | total += printone(out, "# bytes in available blocks", available_bytes); |
Tim Peters | 49f2681 | 2002-04-06 01:45:35 +0000 | [diff] [blame] | 2201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2202 | PyOS_snprintf(buf, sizeof(buf), |
| 2203 | "%u unused pools * %d bytes", numfreepools, POOL_SIZE); |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2204 | total += printone(out, buf, (size_t)numfreepools * POOL_SIZE); |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 2205 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2206 | total += printone(out, "# bytes lost to pool headers", pool_header_bytes); |
| 2207 | total += printone(out, "# bytes lost to quantization", quantization); |
| 2208 | total += printone(out, "# bytes lost to arena alignment", arena_alignment); |
| 2209 | (void)printone(out, "Total", total); |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 2212 | #endif /* #ifdef WITH_PYMALLOC */ |
Neal Norwitz | 7eb3c91 | 2004-06-06 19:20:22 +0000 | [diff] [blame] | 2213 | |
| 2214 | #ifdef Py_USING_MEMORY_DEBUGGER |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 2215 | /* Make this function last so gcc won't inline it since the definition is |
| 2216 | * after the reference. |
| 2217 | */ |
Neal Norwitz | 7eb3c91 | 2004-06-06 19:20:22 +0000 | [diff] [blame] | 2218 | int |
| 2219 | Py_ADDRESS_IN_RANGE(void *P, poolp pool) |
| 2220 | { |
Antoine Pitrou | b7fb2e2 | 2011-01-07 21:43:59 +0000 | [diff] [blame] | 2221 | uint arenaindex_temp = pool->arenaindex; |
| 2222 | |
| 2223 | return arenaindex_temp < maxarenas && |
| 2224 | (uptr)P - arenas[arenaindex_temp].address < (uptr)ARENA_SIZE && |
| 2225 | arenas[arenaindex_temp].address != 0; |
Neal Norwitz | 7eb3c91 | 2004-06-06 19:20:22 +0000 | [diff] [blame] | 2226 | } |
| 2227 | #endif |