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