Tim Peters | 1221c0a | 2002-03-23 00:20:15 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | |
Benjamin Peterson | 3924f93 | 2016-09-18 19:12:48 -0700 | [diff] [blame] | 3 | #include <stdbool.h> |
| 4 | |
Victor Stinner | 0611c26 | 2016-03-15 22:22:13 +0100 | [diff] [blame] | 5 | |
| 6 | /* Defined in tracemalloc.c */ |
| 7 | extern void _PyMem_DumpTraceback(int fd, const void *ptr); |
| 8 | |
| 9 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 10 | /* Python's malloc wrappers (see pymem.h) */ |
| 11 | |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 12 | #undef uint |
| 13 | #define uint unsigned int /* assuming >= 16 bits */ |
| 14 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 15 | /* Forward declaration */ |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 16 | static void* _PyMem_DebugRawMalloc(void *ctx, size_t size); |
| 17 | static void* _PyMem_DebugRawCalloc(void *ctx, size_t nelem, size_t elsize); |
| 18 | static void* _PyMem_DebugRawRealloc(void *ctx, void *ptr, size_t size); |
| 19 | static void _PyMem_DebugRawFree(void *ctx, void *p); |
| 20 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 21 | static void* _PyMem_DebugMalloc(void *ctx, size_t size); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 22 | static void* _PyMem_DebugCalloc(void *ctx, size_t nelem, size_t elsize); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 23 | static void* _PyMem_DebugRealloc(void *ctx, void *ptr, size_t size); |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 24 | static void _PyMem_DebugFree(void *ctx, void *p); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 25 | |
| 26 | static void _PyObject_DebugDumpAddress(const void *p); |
| 27 | static void _PyMem_DebugCheckAddress(char api_id, const void *p); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 28 | |
Nick Coghlan | 6ba64f4 | 2013-09-29 00:28:55 +1000 | [diff] [blame] | 29 | #if defined(__has_feature) /* Clang */ |
| 30 | #if __has_feature(address_sanitizer) /* is ASAN enabled? */ |
| 31 | #define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS \ |
Benjamin Peterson | 3924f93 | 2016-09-18 19:12:48 -0700 | [diff] [blame] | 32 | __attribute__((no_address_safety_analysis)) |
Nick Coghlan | 6ba64f4 | 2013-09-29 00:28:55 +1000 | [diff] [blame] | 33 | #else |
| 34 | #define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS |
| 35 | #endif |
| 36 | #else |
| 37 | #if defined(__SANITIZE_ADDRESS__) /* GCC 4.8.x, is ASAN enabled? */ |
| 38 | #define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS \ |
Benjamin Peterson | 3924f93 | 2016-09-18 19:12:48 -0700 | [diff] [blame] | 39 | __attribute__((no_address_safety_analysis)) |
Nick Coghlan | 6ba64f4 | 2013-09-29 00:28:55 +1000 | [diff] [blame] | 40 | #else |
| 41 | #define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS |
| 42 | #endif |
| 43 | #endif |
| 44 | |
Tim Peters | 1221c0a | 2002-03-23 00:20:15 +0000 | [diff] [blame] | 45 | #ifdef WITH_PYMALLOC |
| 46 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 47 | #ifdef MS_WINDOWS |
| 48 | # include <windows.h> |
| 49 | #elif defined(HAVE_MMAP) |
| 50 | # include <sys/mman.h> |
| 51 | # ifdef MAP_ANONYMOUS |
| 52 | # define ARENAS_USE_MMAP |
| 53 | # endif |
Antoine Pitrou | 6f26be0 | 2011-05-03 18:18:59 +0200 | [diff] [blame] | 54 | #endif |
| 55 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 56 | /* Forward declaration */ |
| 57 | static void* _PyObject_Malloc(void *ctx, size_t size); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 58 | static void* _PyObject_Calloc(void *ctx, size_t nelem, size_t elsize); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 59 | static void _PyObject_Free(void *ctx, void *p); |
| 60 | 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] | 61 | #endif |
| 62 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 63 | |
| 64 | static void * |
| 65 | _PyMem_RawMalloc(void *ctx, size_t size) |
| 66 | { |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 67 | /* PyMem_RawMalloc(0) means malloc(1). Some systems would return NULL |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 68 | for malloc(0), which would be treated as an error. Some platforms would |
| 69 | return a pointer with no memory behind it, which would break pymalloc. |
| 70 | To solve these problems, allocate an extra byte. */ |
| 71 | if (size == 0) |
| 72 | size = 1; |
| 73 | return malloc(size); |
| 74 | } |
| 75 | |
| 76 | static void * |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 77 | _PyMem_RawCalloc(void *ctx, size_t nelem, size_t elsize) |
| 78 | { |
| 79 | /* PyMem_RawCalloc(0, 0) means calloc(1, 1). Some systems would return NULL |
| 80 | for calloc(0, 0), which would be treated as an error. Some platforms |
| 81 | would return a pointer with no memory behind it, which would break |
| 82 | pymalloc. To solve these problems, allocate an extra byte. */ |
| 83 | if (nelem == 0 || elsize == 0) { |
| 84 | nelem = 1; |
| 85 | elsize = 1; |
| 86 | } |
| 87 | return calloc(nelem, elsize); |
| 88 | } |
| 89 | |
| 90 | static void * |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 91 | _PyMem_RawRealloc(void *ctx, void *ptr, size_t size) |
| 92 | { |
| 93 | if (size == 0) |
| 94 | size = 1; |
| 95 | return realloc(ptr, size); |
| 96 | } |
| 97 | |
| 98 | static void |
| 99 | _PyMem_RawFree(void *ctx, void *ptr) |
| 100 | { |
| 101 | free(ptr); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | #ifdef MS_WINDOWS |
| 106 | static void * |
| 107 | _PyObject_ArenaVirtualAlloc(void *ctx, size_t size) |
| 108 | { |
| 109 | return VirtualAlloc(NULL, size, |
| 110 | MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); |
| 111 | } |
| 112 | |
| 113 | static void |
| 114 | _PyObject_ArenaVirtualFree(void *ctx, void *ptr, size_t size) |
| 115 | { |
Victor Stinner | 725e668 | 2013-07-07 03:06:16 +0200 | [diff] [blame] | 116 | VirtualFree(ptr, 0, MEM_RELEASE); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | #elif defined(ARENAS_USE_MMAP) |
| 120 | static void * |
| 121 | _PyObject_ArenaMmap(void *ctx, size_t size) |
| 122 | { |
| 123 | void *ptr; |
| 124 | ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, |
| 125 | MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 126 | if (ptr == MAP_FAILED) |
| 127 | return NULL; |
| 128 | assert(ptr != NULL); |
| 129 | return ptr; |
| 130 | } |
| 131 | |
| 132 | static void |
| 133 | _PyObject_ArenaMunmap(void *ctx, void *ptr, size_t size) |
| 134 | { |
| 135 | munmap(ptr, size); |
| 136 | } |
| 137 | |
| 138 | #else |
| 139 | static void * |
| 140 | _PyObject_ArenaMalloc(void *ctx, size_t size) |
| 141 | { |
| 142 | return malloc(size); |
| 143 | } |
| 144 | |
| 145 | static void |
| 146 | _PyObject_ArenaFree(void *ctx, void *ptr, size_t size) |
| 147 | { |
| 148 | free(ptr); |
| 149 | } |
| 150 | #endif |
| 151 | |
| 152 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 153 | #define PYRAW_FUNCS _PyMem_RawMalloc, _PyMem_RawCalloc, _PyMem_RawRealloc, _PyMem_RawFree |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 154 | #ifdef WITH_PYMALLOC |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 155 | # define PYOBJ_FUNCS _PyObject_Malloc, _PyObject_Calloc, _PyObject_Realloc, _PyObject_Free |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 156 | #else |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 157 | # define PYOBJ_FUNCS PYRAW_FUNCS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 158 | #endif |
Victor Stinner | 1593259 | 2016-04-22 18:52:22 +0200 | [diff] [blame] | 159 | #define PYMEM_FUNCS PYOBJ_FUNCS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 160 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 161 | typedef struct { |
| 162 | /* We tag each block with an API ID in order to tag API violations */ |
| 163 | char api_id; |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 164 | PyMemAllocatorEx alloc; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 165 | } debug_alloc_api_t; |
| 166 | static struct { |
| 167 | debug_alloc_api_t raw; |
| 168 | debug_alloc_api_t mem; |
| 169 | debug_alloc_api_t obj; |
| 170 | } _PyMem_Debug = { |
| 171 | {'r', {NULL, PYRAW_FUNCS}}, |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 172 | {'m', {NULL, PYMEM_FUNCS}}, |
| 173 | {'o', {NULL, PYOBJ_FUNCS}} |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 174 | }; |
| 175 | |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 176 | #define PYRAWDBG_FUNCS \ |
| 177 | _PyMem_DebugRawMalloc, _PyMem_DebugRawCalloc, _PyMem_DebugRawRealloc, _PyMem_DebugRawFree |
| 178 | #define PYDBG_FUNCS \ |
| 179 | _PyMem_DebugMalloc, _PyMem_DebugCalloc, _PyMem_DebugRealloc, _PyMem_DebugFree |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 180 | |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 181 | |
| 182 | #define _PyMem_Raw _PyRuntime.mem.allocators.raw |
| 183 | static const PyMemAllocatorEx _pymem_raw = { |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 184 | #ifdef Py_DEBUG |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 185 | &_PyMem_Debug.raw, PYRAWDBG_FUNCS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 186 | #else |
| 187 | NULL, PYRAW_FUNCS |
| 188 | #endif |
| 189 | }; |
| 190 | |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 191 | #define _PyMem _PyRuntime.mem.allocators.mem |
| 192 | static const PyMemAllocatorEx _pymem = { |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 193 | #ifdef Py_DEBUG |
Victor Stinner | 1593259 | 2016-04-22 18:52:22 +0200 | [diff] [blame] | 194 | &_PyMem_Debug.mem, PYDBG_FUNCS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 195 | #else |
Victor Stinner | 1593259 | 2016-04-22 18:52:22 +0200 | [diff] [blame] | 196 | NULL, PYMEM_FUNCS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 197 | #endif |
| 198 | }; |
| 199 | |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 200 | #define _PyObject _PyRuntime.mem.allocators.obj |
| 201 | static const PyMemAllocatorEx _pyobject = { |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 202 | #ifdef Py_DEBUG |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 203 | &_PyMem_Debug.obj, PYDBG_FUNCS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 204 | #else |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 205 | NULL, PYOBJ_FUNCS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 206 | #endif |
| 207 | }; |
| 208 | |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 209 | int |
| 210 | _PyMem_SetupAllocators(const char *opt) |
| 211 | { |
| 212 | if (opt == NULL || *opt == '\0') { |
| 213 | /* PYTHONMALLOC is empty or is not set or ignored (-E/-I command line |
| 214 | options): use default allocators */ |
| 215 | #ifdef Py_DEBUG |
| 216 | # ifdef WITH_PYMALLOC |
| 217 | opt = "pymalloc_debug"; |
| 218 | # else |
| 219 | opt = "malloc_debug"; |
| 220 | # endif |
| 221 | #else |
| 222 | /* !Py_DEBUG */ |
| 223 | # ifdef WITH_PYMALLOC |
| 224 | opt = "pymalloc"; |
| 225 | # else |
| 226 | opt = "malloc"; |
| 227 | # endif |
| 228 | #endif |
| 229 | } |
| 230 | |
| 231 | if (strcmp(opt, "debug") == 0) { |
| 232 | PyMem_SetupDebugHooks(); |
| 233 | } |
| 234 | else if (strcmp(opt, "malloc") == 0 || strcmp(opt, "malloc_debug") == 0) |
| 235 | { |
| 236 | PyMemAllocatorEx alloc = {NULL, PYRAW_FUNCS}; |
| 237 | |
| 238 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc); |
| 239 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc); |
| 240 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc); |
| 241 | |
| 242 | if (strcmp(opt, "malloc_debug") == 0) |
| 243 | PyMem_SetupDebugHooks(); |
| 244 | } |
| 245 | #ifdef WITH_PYMALLOC |
| 246 | else if (strcmp(opt, "pymalloc") == 0 |
| 247 | || strcmp(opt, "pymalloc_debug") == 0) |
| 248 | { |
Victor Stinner | 1593259 | 2016-04-22 18:52:22 +0200 | [diff] [blame] | 249 | PyMemAllocatorEx raw_alloc = {NULL, PYRAW_FUNCS}; |
| 250 | PyMemAllocatorEx mem_alloc = {NULL, PYMEM_FUNCS}; |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 251 | PyMemAllocatorEx obj_alloc = {NULL, PYOBJ_FUNCS}; |
| 252 | |
Victor Stinner | 1593259 | 2016-04-22 18:52:22 +0200 | [diff] [blame] | 253 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &raw_alloc); |
| 254 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &mem_alloc); |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 255 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &obj_alloc); |
| 256 | |
| 257 | if (strcmp(opt, "pymalloc_debug") == 0) |
| 258 | PyMem_SetupDebugHooks(); |
| 259 | } |
| 260 | #endif |
| 261 | else { |
| 262 | /* unknown allocator */ |
| 263 | return -1; |
| 264 | } |
| 265 | return 0; |
| 266 | } |
| 267 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 268 | #undef PYRAW_FUNCS |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 269 | #undef PYMEM_FUNCS |
| 270 | #undef PYOBJ_FUNCS |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 271 | #undef PYRAWDBG_FUNCS |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 272 | #undef PYDBG_FUNCS |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 273 | |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 274 | static const PyObjectArenaAllocator _PyObject_Arena = {NULL, |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 275 | #ifdef MS_WINDOWS |
| 276 | _PyObject_ArenaVirtualAlloc, _PyObject_ArenaVirtualFree |
| 277 | #elif defined(ARENAS_USE_MMAP) |
| 278 | _PyObject_ArenaMmap, _PyObject_ArenaMunmap |
| 279 | #else |
| 280 | _PyObject_ArenaMalloc, _PyObject_ArenaFree |
| 281 | #endif |
| 282 | }; |
| 283 | |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 284 | void |
| 285 | _PyObject_Initialize(struct _pyobj_runtime_state *state) |
| 286 | { |
| 287 | state->allocator_arenas = _PyObject_Arena; |
| 288 | } |
| 289 | |
| 290 | void |
| 291 | _PyMem_Initialize(struct _pymem_runtime_state *state) |
| 292 | { |
| 293 | state->allocators.raw = _pymem_raw; |
| 294 | state->allocators.mem = _pymem; |
| 295 | state->allocators.obj = _pyobject; |
| 296 | |
| 297 | #ifdef WITH_PYMALLOC |
| 298 | for (int i = 0; i < 8; i++) { |
| 299 | if (NB_SMALL_SIZE_CLASSES <= i * 8) |
| 300 | break; |
| 301 | for (int j = 0; j < 8; j++) { |
| 302 | int x = i * 8 + j; |
| 303 | poolp *addr = &(state->usedpools[2*(x)]); |
| 304 | poolp val = (poolp)((uint8_t *)addr - 2*sizeof(pyblock *)); |
| 305 | state->usedpools[x * 2] = val; |
| 306 | state->usedpools[x * 2 + 1] = val; |
| 307 | }; |
| 308 | }; |
| 309 | #endif /* WITH_PYMALLOC */ |
| 310 | } |
| 311 | |
Victor Stinner | 0621e0e | 2016-04-19 17:02:55 +0200 | [diff] [blame] | 312 | #ifdef WITH_PYMALLOC |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 313 | static int |
| 314 | _PyMem_DebugEnabled(void) |
| 315 | { |
| 316 | return (_PyObject.malloc == _PyMem_DebugMalloc); |
| 317 | } |
| 318 | |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 319 | int |
| 320 | _PyMem_PymallocEnabled(void) |
| 321 | { |
| 322 | if (_PyMem_DebugEnabled()) { |
| 323 | return (_PyMem_Debug.obj.alloc.malloc == _PyObject_Malloc); |
| 324 | } |
| 325 | else { |
| 326 | return (_PyObject.malloc == _PyObject_Malloc); |
| 327 | } |
| 328 | } |
| 329 | #endif |
| 330 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 331 | void |
| 332 | PyMem_SetupDebugHooks(void) |
| 333 | { |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 334 | PyMemAllocatorEx alloc; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 335 | |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 336 | alloc.malloc = _PyMem_DebugRawMalloc; |
| 337 | alloc.calloc = _PyMem_DebugRawCalloc; |
| 338 | alloc.realloc = _PyMem_DebugRawRealloc; |
| 339 | alloc.free = _PyMem_DebugRawFree; |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 340 | |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 341 | if (_PyMem_Raw.malloc != _PyMem_DebugRawMalloc) { |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 342 | alloc.ctx = &_PyMem_Debug.raw; |
| 343 | PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &_PyMem_Debug.raw.alloc); |
| 344 | PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &alloc); |
| 345 | } |
| 346 | |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 347 | alloc.malloc = _PyMem_DebugMalloc; |
| 348 | alloc.calloc = _PyMem_DebugCalloc; |
| 349 | alloc.realloc = _PyMem_DebugRealloc; |
| 350 | alloc.free = _PyMem_DebugFree; |
| 351 | |
Victor Stinner | ad52437 | 2016-03-16 12:12:53 +0100 | [diff] [blame] | 352 | if (_PyMem.malloc != _PyMem_DebugMalloc) { |
| 353 | alloc.ctx = &_PyMem_Debug.mem; |
| 354 | PyMem_GetAllocator(PYMEM_DOMAIN_MEM, &_PyMem_Debug.mem.alloc); |
| 355 | PyMem_SetAllocator(PYMEM_DOMAIN_MEM, &alloc); |
| 356 | } |
| 357 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 358 | if (_PyObject.malloc != _PyMem_DebugMalloc) { |
| 359 | alloc.ctx = &_PyMem_Debug.obj; |
| 360 | PyMem_GetAllocator(PYMEM_DOMAIN_OBJ, &_PyMem_Debug.obj.alloc); |
| 361 | PyMem_SetAllocator(PYMEM_DOMAIN_OBJ, &alloc); |
| 362 | } |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 366 | PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 367 | { |
| 368 | switch(domain) |
| 369 | { |
| 370 | case PYMEM_DOMAIN_RAW: *allocator = _PyMem_Raw; break; |
| 371 | case PYMEM_DOMAIN_MEM: *allocator = _PyMem; break; |
| 372 | case PYMEM_DOMAIN_OBJ: *allocator = _PyObject; break; |
| 373 | default: |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 374 | /* unknown domain: set all attributes to NULL */ |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 375 | allocator->ctx = NULL; |
| 376 | allocator->malloc = NULL; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 377 | allocator->calloc = NULL; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 378 | allocator->realloc = NULL; |
| 379 | allocator->free = NULL; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void |
Victor Stinner | d8f0d92 | 2014-06-02 21:57:10 +0200 | [diff] [blame] | 384 | PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 385 | { |
| 386 | switch(domain) |
| 387 | { |
| 388 | case PYMEM_DOMAIN_RAW: _PyMem_Raw = *allocator; break; |
| 389 | case PYMEM_DOMAIN_MEM: _PyMem = *allocator; break; |
| 390 | case PYMEM_DOMAIN_OBJ: _PyObject = *allocator; break; |
| 391 | /* ignore unknown domain */ |
| 392 | } |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | void |
| 396 | PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator) |
| 397 | { |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 398 | *allocator = _PyRuntime.obj.allocator_arenas; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | void |
| 402 | PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator) |
| 403 | { |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 404 | _PyRuntime.obj.allocator_arenas = *allocator; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void * |
| 408 | PyMem_RawMalloc(size_t size) |
| 409 | { |
| 410 | /* |
| 411 | * Limit ourselves to PY_SSIZE_T_MAX bytes to prevent security holes. |
| 412 | * Most python internals blindly use a signed Py_ssize_t to track |
| 413 | * things without checking for overflows or negatives. |
| 414 | * As size_t is unsigned, checking for size < 0 is not required. |
| 415 | */ |
| 416 | if (size > (size_t)PY_SSIZE_T_MAX) |
| 417 | return NULL; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 418 | return _PyMem_Raw.malloc(_PyMem_Raw.ctx, size); |
| 419 | } |
| 420 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 421 | void * |
| 422 | PyMem_RawCalloc(size_t nelem, size_t elsize) |
| 423 | { |
| 424 | /* see PyMem_RawMalloc() */ |
| 425 | if (elsize != 0 && nelem > (size_t)PY_SSIZE_T_MAX / elsize) |
| 426 | return NULL; |
| 427 | return _PyMem_Raw.calloc(_PyMem_Raw.ctx, nelem, elsize); |
| 428 | } |
| 429 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 430 | void* |
| 431 | PyMem_RawRealloc(void *ptr, size_t new_size) |
| 432 | { |
| 433 | /* see PyMem_RawMalloc() */ |
| 434 | if (new_size > (size_t)PY_SSIZE_T_MAX) |
| 435 | return NULL; |
| 436 | return _PyMem_Raw.realloc(_PyMem_Raw.ctx, ptr, new_size); |
| 437 | } |
| 438 | |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 439 | void |
| 440 | PyMem_RawFree(void *ptr) |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 441 | { |
| 442 | _PyMem_Raw.free(_PyMem_Raw.ctx, ptr); |
| 443 | } |
| 444 | |
| 445 | void * |
| 446 | PyMem_Malloc(size_t size) |
| 447 | { |
| 448 | /* see PyMem_RawMalloc() */ |
| 449 | if (size > (size_t)PY_SSIZE_T_MAX) |
| 450 | return NULL; |
| 451 | return _PyMem.malloc(_PyMem.ctx, size); |
| 452 | } |
| 453 | |
| 454 | void * |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 455 | PyMem_Calloc(size_t nelem, size_t elsize) |
| 456 | { |
| 457 | /* see PyMem_RawMalloc() */ |
| 458 | if (elsize != 0 && nelem > (size_t)PY_SSIZE_T_MAX / elsize) |
| 459 | return NULL; |
| 460 | return _PyMem.calloc(_PyMem.ctx, nelem, elsize); |
| 461 | } |
| 462 | |
| 463 | void * |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 464 | PyMem_Realloc(void *ptr, size_t new_size) |
| 465 | { |
| 466 | /* see PyMem_RawMalloc() */ |
| 467 | if (new_size > (size_t)PY_SSIZE_T_MAX) |
| 468 | return NULL; |
| 469 | return _PyMem.realloc(_PyMem.ctx, ptr, new_size); |
| 470 | } |
| 471 | |
| 472 | void |
| 473 | PyMem_Free(void *ptr) |
| 474 | { |
| 475 | _PyMem.free(_PyMem.ctx, ptr); |
| 476 | } |
| 477 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 478 | char * |
| 479 | _PyMem_RawStrdup(const char *str) |
| 480 | { |
| 481 | size_t size; |
| 482 | char *copy; |
| 483 | |
| 484 | size = strlen(str) + 1; |
| 485 | copy = PyMem_RawMalloc(size); |
| 486 | if (copy == NULL) |
| 487 | return NULL; |
| 488 | memcpy(copy, str, size); |
| 489 | return copy; |
| 490 | } |
| 491 | |
| 492 | char * |
| 493 | _PyMem_Strdup(const char *str) |
| 494 | { |
| 495 | size_t size; |
| 496 | char *copy; |
| 497 | |
| 498 | size = strlen(str) + 1; |
| 499 | copy = PyMem_Malloc(size); |
| 500 | if (copy == NULL) |
| 501 | return NULL; |
| 502 | memcpy(copy, str, size); |
| 503 | return copy; |
| 504 | } |
| 505 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 506 | void * |
| 507 | PyObject_Malloc(size_t size) |
| 508 | { |
| 509 | /* see PyMem_RawMalloc() */ |
| 510 | if (size > (size_t)PY_SSIZE_T_MAX) |
| 511 | return NULL; |
| 512 | return _PyObject.malloc(_PyObject.ctx, size); |
| 513 | } |
| 514 | |
| 515 | void * |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 516 | PyObject_Calloc(size_t nelem, size_t elsize) |
| 517 | { |
| 518 | /* see PyMem_RawMalloc() */ |
| 519 | if (elsize != 0 && nelem > (size_t)PY_SSIZE_T_MAX / elsize) |
| 520 | return NULL; |
| 521 | return _PyObject.calloc(_PyObject.ctx, nelem, elsize); |
| 522 | } |
| 523 | |
| 524 | void * |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 525 | PyObject_Realloc(void *ptr, size_t new_size) |
| 526 | { |
| 527 | /* see PyMem_RawMalloc() */ |
| 528 | if (new_size > (size_t)PY_SSIZE_T_MAX) |
| 529 | return NULL; |
| 530 | return _PyObject.realloc(_PyObject.ctx, ptr, new_size); |
| 531 | } |
| 532 | |
| 533 | void |
| 534 | PyObject_Free(void *ptr) |
| 535 | { |
| 536 | _PyObject.free(_PyObject.ctx, ptr); |
| 537 | } |
| 538 | |
| 539 | |
| 540 | #ifdef WITH_PYMALLOC |
| 541 | |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 542 | #ifdef WITH_VALGRIND |
| 543 | #include <valgrind/valgrind.h> |
| 544 | |
| 545 | /* If we're using GCC, use __builtin_expect() to reduce overhead of |
| 546 | the valgrind checks */ |
| 547 | #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) |
| 548 | # define UNLIKELY(value) __builtin_expect((value), 0) |
| 549 | #else |
| 550 | # define UNLIKELY(value) (value) |
| 551 | #endif |
| 552 | |
| 553 | /* -1 indicates that we haven't checked that we're running on valgrind yet. */ |
| 554 | static int running_on_valgrind = -1; |
| 555 | #endif |
| 556 | |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 557 | Py_ssize_t |
| 558 | _Py_GetAllocatedBlocks(void) |
| 559 | { |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 560 | return _PyRuntime.mem.num_allocated_blocks; |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 564 | /* Allocate a new arena. If we run out of memory, return NULL. Else |
| 565 | * allocate a new arena, and return the address of an arena_object |
| 566 | * describing the new arena. It's expected that the caller will set |
| 567 | * `usable_arenas` to the return value. |
| 568 | */ |
| 569 | static struct arena_object* |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 570 | new_arena(void) |
| 571 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 572 | struct arena_object* arenaobj; |
| 573 | uint excess; /* number of bytes above pool alignment */ |
Victor Stinner | ba10882 | 2012-03-10 00:21:44 +0100 | [diff] [blame] | 574 | void *address; |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 575 | static int debug_stats = -1; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 576 | |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 577 | if (debug_stats == -1) { |
| 578 | char *opt = Py_GETENV("PYTHONMALLOCSTATS"); |
| 579 | debug_stats = (opt != NULL && *opt != '\0'); |
| 580 | } |
| 581 | if (debug_stats) |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 582 | _PyObject_DebugMallocStats(stderr); |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 583 | |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 584 | if (_PyRuntime.mem.unused_arena_objects == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | uint i; |
| 586 | uint numarenas; |
| 587 | size_t nbytes; |
Tim Peters | 0e87118 | 2002-04-13 08:29:14 +0000 | [diff] [blame] | 588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | /* Double the number of arena objects on each allocation. |
| 590 | * Note that it's possible for `numarenas` to overflow. |
| 591 | */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 592 | numarenas = _PyRuntime.mem.maxarenas ? _PyRuntime.mem.maxarenas << 1 : INITIAL_ARENA_OBJECTS; |
| 593 | if (numarenas <= _PyRuntime.mem.maxarenas) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | return NULL; /* overflow */ |
Martin v. Löwis | 5aca882 | 2008-09-11 06:55:48 +0000 | [diff] [blame] | 595 | #if SIZEOF_SIZE_T <= SIZEOF_INT |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 596 | if (numarenas > SIZE_MAX / sizeof(*_PyRuntime.mem.arenas)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | return NULL; /* overflow */ |
Martin v. Löwis | 5aca882 | 2008-09-11 06:55:48 +0000 | [diff] [blame] | 598 | #endif |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 599 | nbytes = numarenas * sizeof(*_PyRuntime.mem.arenas); |
| 600 | arenaobj = (struct arena_object *)PyMem_RawRealloc(_PyRuntime.mem.arenas, nbytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 601 | if (arenaobj == NULL) |
| 602 | return NULL; |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 603 | _PyRuntime.mem.arenas = arenaobj; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 605 | /* We might need to fix pointers that were copied. However, |
| 606 | * new_arena only gets called when all the pages in the |
| 607 | * previous arenas are full. Thus, there are *no* pointers |
| 608 | * into the old array. Thus, we don't have to worry about |
| 609 | * invalid pointers. Just to be sure, some asserts: |
| 610 | */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 611 | assert(_PyRuntime.mem.usable_arenas == NULL); |
| 612 | assert(_PyRuntime.mem.unused_arena_objects == NULL); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | /* Put the new arenas on the unused_arena_objects list. */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 615 | for (i = _PyRuntime.mem.maxarenas; i < numarenas; ++i) { |
| 616 | _PyRuntime.mem.arenas[i].address = 0; /* mark as unassociated */ |
| 617 | _PyRuntime.mem.arenas[i].nextarena = i < numarenas - 1 ? |
| 618 | &_PyRuntime.mem.arenas[i+1] : NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 620 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | /* Update globals. */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 622 | _PyRuntime.mem.unused_arena_objects = &_PyRuntime.mem.arenas[_PyRuntime.mem.maxarenas]; |
| 623 | _PyRuntime.mem.maxarenas = numarenas; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 624 | } |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 626 | /* Take the next available arena object off the head of the list. */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 627 | assert(_PyRuntime.mem.unused_arena_objects != NULL); |
| 628 | arenaobj = _PyRuntime.mem.unused_arena_objects; |
| 629 | _PyRuntime.mem.unused_arena_objects = arenaobj->nextarena; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | assert(arenaobj->address == 0); |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 631 | address = _PyRuntime.obj.allocator_arenas.alloc(_PyRuntime.obj.allocator_arenas.ctx, ARENA_SIZE); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 632 | if (address == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 633 | /* The allocation failed: return NULL after putting the |
| 634 | * arenaobj back. |
| 635 | */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 636 | arenaobj->nextarena = _PyRuntime.mem.unused_arena_objects; |
| 637 | _PyRuntime.mem.unused_arena_objects = arenaobj; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | return NULL; |
| 639 | } |
Benjamin Peterson | 5d4b09c | 2016-09-18 19:24:52 -0700 | [diff] [blame] | 640 | arenaobj->address = (uintptr_t)address; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 641 | |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 642 | ++_PyRuntime.mem.narenas_currently_allocated; |
| 643 | ++_PyRuntime.mem.ntimes_arena_allocated; |
| 644 | if (_PyRuntime.mem.narenas_currently_allocated > _PyRuntime.mem.narenas_highwater) |
| 645 | _PyRuntime.mem.narenas_highwater = _PyRuntime.mem.narenas_currently_allocated; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | arenaobj->freepools = NULL; |
| 647 | /* pool_address <- first pool-aligned address in the arena |
| 648 | nfreepools <- number of whole pools that fit after alignment */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 649 | arenaobj->pool_address = (pyblock*)arenaobj->address; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | arenaobj->nfreepools = ARENA_SIZE / POOL_SIZE; |
| 651 | assert(POOL_SIZE * arenaobj->nfreepools == ARENA_SIZE); |
| 652 | excess = (uint)(arenaobj->address & POOL_SIZE_MASK); |
| 653 | if (excess != 0) { |
| 654 | --arenaobj->nfreepools; |
| 655 | arenaobj->pool_address += POOL_SIZE - excess; |
| 656 | } |
| 657 | arenaobj->ntotalpools = arenaobj->nfreepools; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 659 | return arenaobj; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 662 | /* |
Benjamin Peterson | 3924f93 | 2016-09-18 19:12:48 -0700 | [diff] [blame] | 663 | address_in_range(P, POOL) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 664 | |
| 665 | Return true if and only if P is an address that was allocated by pymalloc. |
| 666 | POOL must be the pool address associated with P, i.e., POOL = POOL_ADDR(P) |
| 667 | (the caller is asked to compute this because the macro expands POOL more than |
| 668 | once, and for efficiency it's best for the caller to assign POOL_ADDR(P) to a |
Benjamin Peterson | 3924f93 | 2016-09-18 19:12:48 -0700 | [diff] [blame] | 669 | variable and pass the latter to the macro; because address_in_range is |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 670 | called on every alloc/realloc/free, micro-efficiency is important here). |
| 671 | |
| 672 | Tricky: Let B be the arena base address associated with the pool, B = |
| 673 | arenas[(POOL)->arenaindex].address. Then P belongs to the arena if and only if |
| 674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | B <= P < B + ARENA_SIZE |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 676 | |
| 677 | Subtracting B throughout, this is true iff |
| 678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | 0 <= P-B < ARENA_SIZE |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 680 | |
| 681 | By using unsigned arithmetic, the "0 <=" half of the test can be skipped. |
| 682 | |
| 683 | Obscure: A PyMem "free memory" function can call the pymalloc free or realloc |
| 684 | before the first arena has been allocated. `arenas` is still NULL in that |
| 685 | case. We're relying on that maxarenas is also 0 in that case, so that |
| 686 | (POOL)->arenaindex < maxarenas must be false, saving us from trying to index |
| 687 | into a NULL arenas. |
| 688 | |
| 689 | Details: given P and POOL, the arena_object corresponding to P is AO = |
| 690 | arenas[(POOL)->arenaindex]. Suppose obmalloc controls P. Then (barring wild |
| 691 | stores, etc), POOL is the correct address of P's pool, AO.address is the |
| 692 | correct base address of the pool's arena, and P must be within ARENA_SIZE of |
| 693 | AO.address. In addition, AO.address is not 0 (no arena can start at address 0 |
Benjamin Peterson | 3924f93 | 2016-09-18 19:12:48 -0700 | [diff] [blame] | 694 | (NULL)). Therefore address_in_range correctly reports that obmalloc |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 695 | controls P. |
| 696 | |
| 697 | Now suppose obmalloc does not control P (e.g., P was obtained via a direct |
| 698 | call to the system malloc() or realloc()). (POOL)->arenaindex may be anything |
| 699 | in this case -- it may even be uninitialized trash. If the trash arenaindex |
| 700 | is >= maxarenas, the macro correctly concludes at once that obmalloc doesn't |
| 701 | control P. |
| 702 | |
| 703 | Else arenaindex is < maxarena, and AO is read up. If AO corresponds to an |
| 704 | allocated arena, obmalloc controls all the memory in slice AO.address : |
| 705 | AO.address+ARENA_SIZE. By case assumption, P is not controlled by obmalloc, |
| 706 | so P doesn't lie in that slice, so the macro correctly reports that P is not |
| 707 | controlled by obmalloc. |
| 708 | |
| 709 | Finally, if P is not controlled by obmalloc and AO corresponds to an unused |
| 710 | arena_object (one not currently associated with an allocated arena), |
| 711 | AO.address is 0, and the second test in the macro reduces to: |
| 712 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | P < ARENA_SIZE |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 714 | |
| 715 | If P >= ARENA_SIZE (extremely likely), the macro again correctly concludes |
| 716 | that P is not controlled by obmalloc. However, if P < ARENA_SIZE, this part |
| 717 | of the test still passes, and the third clause (AO.address != 0) is necessary |
| 718 | to get the correct result: AO.address is 0 in this case, so the macro |
| 719 | correctly reports that P is not controlled by obmalloc (despite that P lies in |
| 720 | slice AO.address : AO.address + ARENA_SIZE). |
| 721 | |
| 722 | Note: The third (AO.address != 0) clause was added in Python 2.5. Before |
| 723 | 2.5, arenas were never free()'ed, and an arenaindex < maxarena always |
| 724 | corresponded to a currently-allocated arena, so the "P is not controlled by |
| 725 | obmalloc, AO corresponds to an unused arena_object, and P < ARENA_SIZE" case |
| 726 | was impossible. |
| 727 | |
| 728 | Note that the logic is excruciating, and reading up possibly uninitialized |
| 729 | memory when P is not controlled by obmalloc (to get at (POOL)->arenaindex) |
| 730 | creates problems for some memory debuggers. The overwhelming advantage is |
| 731 | that this test determines whether an arbitrary address is controlled by |
| 732 | obmalloc in a small constant time, independent of the number of arenas |
| 733 | obmalloc controls. Since this test is needed at every entry point, it's |
| 734 | extremely desirable that it be this fast. |
| 735 | */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 736 | |
Benjamin Peterson | 3924f93 | 2016-09-18 19:12:48 -0700 | [diff] [blame] | 737 | static bool ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS |
| 738 | address_in_range(void *p, poolp pool) |
| 739 | { |
| 740 | // Since address_in_range may be reading from memory which was not allocated |
| 741 | // by Python, it is important that pool->arenaindex is read only once, as |
| 742 | // another thread may be concurrently modifying the value without holding |
| 743 | // the GIL. The following dance forces the compiler to read pool->arenaindex |
| 744 | // only once. |
| 745 | uint arenaindex = *((volatile uint *)&pool->arenaindex); |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 746 | return arenaindex < _PyRuntime.mem.maxarenas && |
| 747 | (uintptr_t)p - _PyRuntime.mem.arenas[arenaindex].address < ARENA_SIZE && |
| 748 | _PyRuntime.mem.arenas[arenaindex].address != 0; |
Benjamin Peterson | 3924f93 | 2016-09-18 19:12:48 -0700 | [diff] [blame] | 749 | } |
Tim Peters | 338e010 | 2002-04-01 19:23:44 +0000 | [diff] [blame] | 750 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 751 | /*==========================================================================*/ |
| 752 | |
Tim Peters | 84c1b97 | 2002-04-04 04:44:32 +0000 | [diff] [blame] | 753 | /* malloc. Note that nbytes==0 tries to return a non-NULL pointer, distinct |
| 754 | * from all other currently live pointers. This may not be possible. |
| 755 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 756 | |
| 757 | /* |
| 758 | * The basic blocks are ordered by decreasing execution frequency, |
| 759 | * which minimizes the number of jumps in the most common cases, |
| 760 | * improves branching prediction and instruction scheduling (small |
| 761 | * block allocations typically result in a couple of instructions). |
| 762 | * Unless the optimizer reorders everything, being too smart... |
| 763 | */ |
| 764 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 765 | static void * |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 766 | _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] | 767 | { |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 768 | size_t nbytes; |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 769 | pyblock *bp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 770 | poolp pool; |
| 771 | poolp next; |
| 772 | uint size; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 773 | |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 774 | _PyRuntime.mem.num_allocated_blocks++; |
Antoine Pitrou | 0aaaa62 | 2013-04-06 01:15:30 +0200 | [diff] [blame] | 775 | |
T. Wouters | 06bb487 | 2017-03-31 10:10:19 -0700 | [diff] [blame] | 776 | assert(elsize == 0 || nelem <= PY_SSIZE_T_MAX / elsize); |
Victor Stinner | 3080d92 | 2014-05-06 11:32:29 +0200 | [diff] [blame] | 777 | nbytes = nelem * elsize; |
| 778 | |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 779 | #ifdef WITH_VALGRIND |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 780 | if (UNLIKELY(running_on_valgrind == -1)) |
| 781 | running_on_valgrind = RUNNING_ON_VALGRIND; |
| 782 | if (UNLIKELY(running_on_valgrind)) |
| 783 | goto redirect; |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 784 | #endif |
| 785 | |
T. Wouters | 06bb487 | 2017-03-31 10:10:19 -0700 | [diff] [blame] | 786 | if (nelem == 0 || elsize == 0) |
| 787 | goto redirect; |
| 788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 789 | if ((nbytes - 1) < SMALL_REQUEST_THRESHOLD) { |
| 790 | LOCK(); |
| 791 | /* |
| 792 | * Most frequent paths first |
| 793 | */ |
| 794 | size = (uint)(nbytes - 1) >> ALIGNMENT_SHIFT; |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 795 | pool = _PyRuntime.mem.usedpools[size + size]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 796 | if (pool != pool->nextpool) { |
| 797 | /* |
| 798 | * There is a used pool for this size class. |
| 799 | * Pick up the head block of its free list. |
| 800 | */ |
| 801 | ++pool->ref.count; |
| 802 | bp = pool->freeblock; |
| 803 | assert(bp != NULL); |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 804 | if ((pool->freeblock = *(pyblock **)bp) != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 805 | UNLOCK(); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 806 | if (use_calloc) |
| 807 | memset(bp, 0, nbytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 808 | return (void *)bp; |
| 809 | } |
| 810 | /* |
| 811 | * Reached the end of the free list, try to extend it. |
| 812 | */ |
| 813 | if (pool->nextoffset <= pool->maxnextoffset) { |
| 814 | /* There is room for another block. */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 815 | pool->freeblock = (pyblock*)pool + |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 816 | pool->nextoffset; |
| 817 | pool->nextoffset += INDEX2SIZE(size); |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 818 | *(pyblock **)(pool->freeblock) = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 819 | UNLOCK(); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 820 | if (use_calloc) |
| 821 | memset(bp, 0, nbytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | return (void *)bp; |
| 823 | } |
| 824 | /* Pool is full, unlink from used pools. */ |
| 825 | next = pool->nextpool; |
| 826 | pool = pool->prevpool; |
| 827 | next->prevpool = pool; |
| 828 | pool->nextpool = next; |
| 829 | UNLOCK(); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 830 | if (use_calloc) |
| 831 | memset(bp, 0, nbytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | return (void *)bp; |
| 833 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | /* There isn't a pool of the right size class immediately |
| 836 | * available: use a free pool. |
| 837 | */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 838 | if (_PyRuntime.mem.usable_arenas == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | /* No arena has a free pool: allocate a new arena. */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 840 | #ifdef WITH_MEMORY_LIMITS |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 841 | if (_PyRuntime.mem.narenas_currently_allocated >= MAX_ARENAS) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | UNLOCK(); |
| 843 | goto redirect; |
| 844 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 845 | #endif |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 846 | _PyRuntime.mem.usable_arenas = new_arena(); |
| 847 | if (_PyRuntime.mem.usable_arenas == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 848 | UNLOCK(); |
| 849 | goto redirect; |
| 850 | } |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 851 | _PyRuntime.mem.usable_arenas->nextarena = |
| 852 | _PyRuntime.mem.usable_arenas->prevarena = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 853 | } |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 854 | assert(_PyRuntime.mem.usable_arenas->address != 0); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 856 | /* Try to get a cached free pool. */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 857 | pool = _PyRuntime.mem.usable_arenas->freepools; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | if (pool != NULL) { |
| 859 | /* Unlink from cached pools. */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 860 | _PyRuntime.mem.usable_arenas->freepools = pool->nextpool; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 862 | /* This arena already had the smallest nfreepools |
| 863 | * value, so decreasing nfreepools doesn't change |
| 864 | * that, and we don't need to rearrange the |
| 865 | * usable_arenas list. However, if the arena has |
| 866 | * become wholly allocated, we need to remove its |
| 867 | * arena_object from usable_arenas. |
| 868 | */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 869 | --_PyRuntime.mem.usable_arenas->nfreepools; |
| 870 | if (_PyRuntime.mem.usable_arenas->nfreepools == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 871 | /* Wholly allocated: remove. */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 872 | assert(_PyRuntime.mem.usable_arenas->freepools == NULL); |
| 873 | assert(_PyRuntime.mem.usable_arenas->nextarena == NULL || |
| 874 | _PyRuntime.mem.usable_arenas->nextarena->prevarena == |
| 875 | _PyRuntime.mem.usable_arenas); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 876 | |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 877 | _PyRuntime.mem.usable_arenas = _PyRuntime.mem.usable_arenas->nextarena; |
| 878 | if (_PyRuntime.mem.usable_arenas != NULL) { |
| 879 | _PyRuntime.mem.usable_arenas->prevarena = NULL; |
| 880 | assert(_PyRuntime.mem.usable_arenas->address != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | else { |
| 884 | /* nfreepools > 0: it must be that freepools |
| 885 | * isn't NULL, or that we haven't yet carved |
| 886 | * off all the arena's pools for the first |
| 887 | * time. |
| 888 | */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 889 | assert(_PyRuntime.mem.usable_arenas->freepools != NULL || |
| 890 | _PyRuntime.mem.usable_arenas->pool_address <= |
| 891 | (pyblock*)_PyRuntime.mem.usable_arenas->address + |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 892 | ARENA_SIZE - POOL_SIZE); |
| 893 | } |
| 894 | init_pool: |
| 895 | /* Frontlink to used pools. */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 896 | next = _PyRuntime.mem.usedpools[size + size]; /* == prev */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | pool->nextpool = next; |
| 898 | pool->prevpool = next; |
| 899 | next->nextpool = pool; |
| 900 | next->prevpool = pool; |
| 901 | pool->ref.count = 1; |
| 902 | if (pool->szidx == size) { |
| 903 | /* Luckily, this pool last contained blocks |
| 904 | * of the same size class, so its header |
| 905 | * and free list are already initialized. |
| 906 | */ |
| 907 | bp = pool->freeblock; |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 908 | assert(bp != NULL); |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 909 | pool->freeblock = *(pyblock **)bp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | UNLOCK(); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 911 | if (use_calloc) |
| 912 | memset(bp, 0, nbytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 913 | return (void *)bp; |
| 914 | } |
| 915 | /* |
| 916 | * Initialize the pool header, set up the free list to |
| 917 | * contain just the second block, and return the first |
| 918 | * block. |
| 919 | */ |
| 920 | pool->szidx = size; |
| 921 | size = INDEX2SIZE(size); |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 922 | bp = (pyblock *)pool + POOL_OVERHEAD; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 923 | pool->nextoffset = POOL_OVERHEAD + (size << 1); |
| 924 | pool->maxnextoffset = POOL_SIZE - size; |
| 925 | pool->freeblock = bp + size; |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 926 | *(pyblock **)(pool->freeblock) = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | UNLOCK(); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 928 | if (use_calloc) |
| 929 | memset(bp, 0, nbytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | return (void *)bp; |
| 931 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | /* Carve off a new pool. */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 934 | assert(_PyRuntime.mem.usable_arenas->nfreepools > 0); |
| 935 | assert(_PyRuntime.mem.usable_arenas->freepools == NULL); |
| 936 | pool = (poolp)_PyRuntime.mem.usable_arenas->pool_address; |
| 937 | assert((pyblock*)pool <= (pyblock*)_PyRuntime.mem.usable_arenas->address + |
| 938 | ARENA_SIZE - POOL_SIZE); |
| 939 | pool->arenaindex = (uint)(_PyRuntime.mem.usable_arenas - _PyRuntime.mem.arenas); |
| 940 | assert(&_PyRuntime.mem.arenas[pool->arenaindex] == _PyRuntime.mem.usable_arenas); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | pool->szidx = DUMMY_SIZE_IDX; |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 942 | _PyRuntime.mem.usable_arenas->pool_address += POOL_SIZE; |
| 943 | --_PyRuntime.mem.usable_arenas->nfreepools; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 944 | |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 945 | if (_PyRuntime.mem.usable_arenas->nfreepools == 0) { |
| 946 | assert(_PyRuntime.mem.usable_arenas->nextarena == NULL || |
| 947 | _PyRuntime.mem.usable_arenas->nextarena->prevarena == |
| 948 | _PyRuntime.mem.usable_arenas); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 949 | /* Unlink the arena: it is completely allocated. */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 950 | _PyRuntime.mem.usable_arenas = _PyRuntime.mem.usable_arenas->nextarena; |
| 951 | if (_PyRuntime.mem.usable_arenas != NULL) { |
| 952 | _PyRuntime.mem.usable_arenas->prevarena = NULL; |
| 953 | assert(_PyRuntime.mem.usable_arenas->address != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 956 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 957 | goto init_pool; |
| 958 | } |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | /* The small block allocator ends here. */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 961 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 962 | redirect: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | /* Redirect the original request to the underlying (libc) allocator. |
| 964 | * We jump here on bigger requests, on error in the code above (as a |
| 965 | * last chance to serve the request) or when the max memory limit |
| 966 | * has been reached. |
| 967 | */ |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 968 | { |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 969 | void *result; |
| 970 | if (use_calloc) |
| 971 | result = PyMem_RawCalloc(nelem, elsize); |
| 972 | else |
| 973 | result = PyMem_RawMalloc(nbytes); |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 974 | if (!result) |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 975 | _PyRuntime.mem.num_allocated_blocks--; |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 976 | return result; |
| 977 | } |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 980 | static void * |
| 981 | _PyObject_Malloc(void *ctx, size_t nbytes) |
| 982 | { |
| 983 | return _PyObject_Alloc(0, ctx, 1, nbytes); |
| 984 | } |
| 985 | |
| 986 | static void * |
| 987 | _PyObject_Calloc(void *ctx, size_t nelem, size_t elsize) |
| 988 | { |
| 989 | return _PyObject_Alloc(1, ctx, nelem, elsize); |
| 990 | } |
| 991 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 992 | /* free */ |
| 993 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 994 | static void |
| 995 | _PyObject_Free(void *ctx, void *p) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 996 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 997 | poolp pool; |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 998 | pyblock *lastfree; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 999 | poolp next, prev; |
| 1000 | uint size; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | if (p == NULL) /* free(NULL) has no effect */ |
| 1003 | return; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1004 | |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1005 | _PyRuntime.mem.num_allocated_blocks--; |
Antoine Pitrou | f9d0b12 | 2012-12-09 14:28:26 +0100 | [diff] [blame] | 1006 | |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 1007 | #ifdef WITH_VALGRIND |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | if (UNLIKELY(running_on_valgrind > 0)) |
| 1009 | goto redirect; |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 1010 | #endif |
| 1011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | pool = POOL_ADDR(p); |
Benjamin Peterson | 3924f93 | 2016-09-18 19:12:48 -0700 | [diff] [blame] | 1013 | if (address_in_range(p, pool)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | /* We allocated this address. */ |
| 1015 | LOCK(); |
| 1016 | /* Link p to the start of the pool's freeblock list. Since |
| 1017 | * the pool had at least the p block outstanding, the pool |
| 1018 | * wasn't empty (so it's already in a usedpools[] list, or |
| 1019 | * was full and is in no list -- it's not in the freeblocks |
| 1020 | * list in any case). |
| 1021 | */ |
| 1022 | assert(pool->ref.count > 0); /* else it was empty */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1023 | *(pyblock **)p = lastfree = pool->freeblock; |
| 1024 | pool->freeblock = (pyblock *)p; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | if (lastfree) { |
| 1026 | struct arena_object* ao; |
| 1027 | uint nf; /* ao->nfreepools */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1028 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | /* freeblock wasn't NULL, so the pool wasn't full, |
| 1030 | * and the pool is in a usedpools[] list. |
| 1031 | */ |
| 1032 | if (--pool->ref.count != 0) { |
| 1033 | /* pool isn't empty: leave it in usedpools */ |
| 1034 | UNLOCK(); |
| 1035 | return; |
| 1036 | } |
| 1037 | /* Pool is now empty: unlink from usedpools, and |
| 1038 | * link to the front of freepools. This ensures that |
| 1039 | * previously freed pools will be allocated later |
| 1040 | * (being not referenced, they are perhaps paged out). |
| 1041 | */ |
| 1042 | next = pool->nextpool; |
| 1043 | prev = pool->prevpool; |
| 1044 | next->prevpool = prev; |
| 1045 | prev->nextpool = next; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | /* Link the pool to freepools. This is a singly-linked |
| 1048 | * list, and pool->prevpool isn't used there. |
| 1049 | */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1050 | ao = &_PyRuntime.mem.arenas[pool->arenaindex]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | pool->nextpool = ao->freepools; |
| 1052 | ao->freepools = pool; |
| 1053 | nf = ++ao->nfreepools; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | /* All the rest is arena management. We just freed |
| 1056 | * a pool, and there are 4 cases for arena mgmt: |
| 1057 | * 1. If all the pools are free, return the arena to |
| 1058 | * the system free(). |
| 1059 | * 2. If this is the only free pool in the arena, |
| 1060 | * add the arena back to the `usable_arenas` list. |
| 1061 | * 3. If the "next" arena has a smaller count of free |
| 1062 | * pools, we have to "slide this arena right" to |
| 1063 | * restore that usable_arenas is sorted in order of |
| 1064 | * nfreepools. |
| 1065 | * 4. Else there's nothing more to do. |
| 1066 | */ |
| 1067 | if (nf == ao->ntotalpools) { |
| 1068 | /* Case 1. First unlink ao from usable_arenas. |
| 1069 | */ |
| 1070 | assert(ao->prevarena == NULL || |
| 1071 | ao->prevarena->address != 0); |
| 1072 | assert(ao ->nextarena == NULL || |
| 1073 | ao->nextarena->address != 0); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | /* Fix the pointer in the prevarena, or the |
| 1076 | * usable_arenas pointer. |
| 1077 | */ |
| 1078 | if (ao->prevarena == NULL) { |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1079 | _PyRuntime.mem.usable_arenas = ao->nextarena; |
| 1080 | assert(_PyRuntime.mem.usable_arenas == NULL || |
| 1081 | _PyRuntime.mem.usable_arenas->address != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | } |
| 1083 | else { |
| 1084 | assert(ao->prevarena->nextarena == ao); |
| 1085 | ao->prevarena->nextarena = |
| 1086 | ao->nextarena; |
| 1087 | } |
| 1088 | /* Fix the pointer in the nextarena. */ |
| 1089 | if (ao->nextarena != NULL) { |
| 1090 | assert(ao->nextarena->prevarena == ao); |
| 1091 | ao->nextarena->prevarena = |
| 1092 | ao->prevarena; |
| 1093 | } |
| 1094 | /* Record that this arena_object slot is |
| 1095 | * available to be reused. |
| 1096 | */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1097 | ao->nextarena = _PyRuntime.mem.unused_arena_objects; |
| 1098 | _PyRuntime.mem.unused_arena_objects = ao; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | /* Free the entire arena. */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1101 | _PyRuntime.obj.allocator_arenas.free(_PyRuntime.obj.allocator_arenas.ctx, |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1102 | (void *)ao->address, ARENA_SIZE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | ao->address = 0; /* mark unassociated */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1104 | --_PyRuntime.mem.narenas_currently_allocated; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1106 | UNLOCK(); |
| 1107 | return; |
| 1108 | } |
| 1109 | if (nf == 1) { |
| 1110 | /* Case 2. Put ao at the head of |
| 1111 | * usable_arenas. Note that because |
| 1112 | * ao->nfreepools was 0 before, ao isn't |
| 1113 | * currently on the usable_arenas list. |
| 1114 | */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1115 | ao->nextarena = _PyRuntime.mem.usable_arenas; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1116 | ao->prevarena = NULL; |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1117 | if (_PyRuntime.mem.usable_arenas) |
| 1118 | _PyRuntime.mem.usable_arenas->prevarena = ao; |
| 1119 | _PyRuntime.mem.usable_arenas = ao; |
| 1120 | assert(_PyRuntime.mem.usable_arenas->address != 0); |
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 | UNLOCK(); |
| 1123 | return; |
| 1124 | } |
| 1125 | /* If this arena is now out of order, we need to keep |
| 1126 | * the list sorted. The list is kept sorted so that |
| 1127 | * the "most full" arenas are used first, which allows |
| 1128 | * the nearly empty arenas to be completely freed. In |
| 1129 | * a few un-scientific tests, it seems like this |
| 1130 | * approach allowed a lot more memory to be freed. |
| 1131 | */ |
| 1132 | if (ao->nextarena == NULL || |
| 1133 | nf <= ao->nextarena->nfreepools) { |
| 1134 | /* Case 4. Nothing to do. */ |
| 1135 | UNLOCK(); |
| 1136 | return; |
| 1137 | } |
| 1138 | /* Case 3: We have to move the arena towards the end |
| 1139 | * of the list, because it has more free pools than |
| 1140 | * the arena to its right. |
| 1141 | * First unlink ao from usable_arenas. |
| 1142 | */ |
| 1143 | if (ao->prevarena != NULL) { |
| 1144 | /* ao isn't at the head of the list */ |
| 1145 | assert(ao->prevarena->nextarena == ao); |
| 1146 | ao->prevarena->nextarena = ao->nextarena; |
| 1147 | } |
| 1148 | else { |
| 1149 | /* ao is at the head of the list */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1150 | assert(_PyRuntime.mem.usable_arenas == ao); |
| 1151 | _PyRuntime.mem.usable_arenas = ao->nextarena; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1152 | } |
| 1153 | ao->nextarena->prevarena = ao->prevarena; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | /* Locate the new insertion point by iterating over |
| 1156 | * the list, using our nextarena pointer. |
| 1157 | */ |
| 1158 | while (ao->nextarena != NULL && |
| 1159 | nf > ao->nextarena->nfreepools) { |
| 1160 | ao->prevarena = ao->nextarena; |
| 1161 | ao->nextarena = ao->nextarena->nextarena; |
| 1162 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1164 | /* Insert ao at this point. */ |
| 1165 | assert(ao->nextarena == NULL || |
| 1166 | ao->prevarena == ao->nextarena->prevarena); |
| 1167 | assert(ao->prevarena->nextarena == ao->nextarena); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1169 | ao->prevarena->nextarena = ao; |
| 1170 | if (ao->nextarena != NULL) |
| 1171 | ao->nextarena->prevarena = ao; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | /* Verify that the swaps worked. */ |
| 1174 | assert(ao->nextarena == NULL || |
| 1175 | nf <= ao->nextarena->nfreepools); |
| 1176 | assert(ao->prevarena == NULL || |
| 1177 | nf > ao->prevarena->nfreepools); |
| 1178 | assert(ao->nextarena == NULL || |
| 1179 | ao->nextarena->prevarena == ao); |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1180 | assert((_PyRuntime.mem.usable_arenas == ao && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1181 | ao->prevarena == NULL) || |
| 1182 | ao->prevarena->nextarena == ao); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 | UNLOCK(); |
| 1185 | return; |
| 1186 | } |
| 1187 | /* Pool was full, so doesn't currently live in any list: |
| 1188 | * link it to the front of the appropriate usedpools[] list. |
| 1189 | * This mimics LRU pool usage for new allocations and |
| 1190 | * targets optimal filling when several pools contain |
| 1191 | * blocks of the same size class. |
| 1192 | */ |
| 1193 | --pool->ref.count; |
| 1194 | assert(pool->ref.count > 0); /* else the pool is empty */ |
| 1195 | size = pool->szidx; |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1196 | next = _PyRuntime.mem.usedpools[size + size]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1197 | prev = next->prevpool; |
| 1198 | /* insert pool before next: prev <-> pool <-> next */ |
| 1199 | pool->nextpool = next; |
| 1200 | pool->prevpool = prev; |
| 1201 | next->prevpool = pool; |
| 1202 | prev->nextpool = pool; |
| 1203 | UNLOCK(); |
| 1204 | return; |
| 1205 | } |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1206 | |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 1207 | #ifdef WITH_VALGRIND |
| 1208 | redirect: |
| 1209 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1210 | /* We didn't allocate this address. */ |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 1211 | PyMem_RawFree(p); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Tim Peters | 84c1b97 | 2002-04-04 04:44:32 +0000 | [diff] [blame] | 1214 | /* realloc. If p is NULL, this acts like malloc(nbytes). Else if nbytes==0, |
| 1215 | * then as the Python docs promise, we do not treat this like free(p), and |
| 1216 | * return a non-NULL result. |
| 1217 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1218 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1219 | static void * |
| 1220 | _PyObject_Realloc(void *ctx, void *p, size_t nbytes) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1221 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1222 | void *bp; |
| 1223 | poolp pool; |
| 1224 | size_t size; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1226 | if (p == NULL) |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 1227 | return _PyObject_Alloc(0, ctx, 1, nbytes); |
Georg Brandl | d492ad8 | 2008-07-23 16:13:07 +0000 | [diff] [blame] | 1228 | |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 1229 | #ifdef WITH_VALGRIND |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | /* Treat running_on_valgrind == -1 the same as 0 */ |
| 1231 | if (UNLIKELY(running_on_valgrind > 0)) |
| 1232 | goto redirect; |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 1233 | #endif |
| 1234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | pool = POOL_ADDR(p); |
Benjamin Peterson | 3924f93 | 2016-09-18 19:12:48 -0700 | [diff] [blame] | 1236 | if (address_in_range(p, pool)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | /* We're in charge of this block */ |
| 1238 | size = INDEX2SIZE(pool->szidx); |
| 1239 | if (nbytes <= size) { |
| 1240 | /* The block is staying the same or shrinking. If |
| 1241 | * it's shrinking, there's a tradeoff: it costs |
| 1242 | * cycles to copy the block to a smaller size class, |
| 1243 | * but it wastes memory not to copy it. The |
| 1244 | * compromise here is to copy on shrink only if at |
| 1245 | * least 25% of size can be shaved off. |
| 1246 | */ |
| 1247 | if (4 * nbytes > 3 * size) { |
| 1248 | /* It's the same, |
| 1249 | * or shrinking and new/old > 3/4. |
| 1250 | */ |
| 1251 | return p; |
| 1252 | } |
| 1253 | size = nbytes; |
| 1254 | } |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 1255 | bp = _PyObject_Alloc(0, ctx, 1, nbytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | if (bp != NULL) { |
| 1257 | memcpy(bp, p, size); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1258 | _PyObject_Free(ctx, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1259 | } |
| 1260 | return bp; |
| 1261 | } |
Benjamin Peterson | 05159c4 | 2009-12-03 03:01:27 +0000 | [diff] [blame] | 1262 | #ifdef WITH_VALGRIND |
| 1263 | redirect: |
| 1264 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1265 | /* We're not managing this block. If nbytes <= |
| 1266 | * SMALL_REQUEST_THRESHOLD, it's tempting to try to take over this |
| 1267 | * block. However, if we do, we need to copy the valid data from |
| 1268 | * the C-managed block to one of our blocks, and there's no portable |
| 1269 | * way to know how much of the memory space starting at p is valid. |
| 1270 | * As bug 1185883 pointed out the hard way, it's possible that the |
| 1271 | * C-managed block is "at the end" of allocated VM space, so that |
| 1272 | * a memory fault can occur if we try to copy nbytes bytes starting |
| 1273 | * at p. Instead we punt: let C continue to manage this block. |
| 1274 | */ |
| 1275 | if (nbytes) |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 1276 | return PyMem_RawRealloc(p, nbytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1277 | /* C doesn't define the result of realloc(p, 0) (it may or may not |
| 1278 | * return NULL then), but Python's docs promise that nbytes==0 never |
| 1279 | * returns NULL. We don't pass 0 to realloc(), to avoid that endcase |
| 1280 | * to begin with. Even then, we can't be sure that realloc() won't |
| 1281 | * return NULL. |
| 1282 | */ |
Victor Stinner | 6cf185d | 2013-10-10 15:58:42 +0200 | [diff] [blame] | 1283 | bp = PyMem_RawRealloc(p, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1284 | return bp ? bp : p; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1287 | #else /* ! WITH_PYMALLOC */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1288 | |
| 1289 | /*==========================================================================*/ |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1290 | /* pymalloc not enabled: Redirect the entry points to malloc. These will |
| 1291 | * only be used by extensions that are compiled with pymalloc enabled. */ |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 1292 | |
Antoine Pitrou | 9284053 | 2012-12-17 23:05:59 +0100 | [diff] [blame] | 1293 | Py_ssize_t |
| 1294 | _Py_GetAllocatedBlocks(void) |
| 1295 | { |
| 1296 | return 0; |
| 1297 | } |
| 1298 | |
Tim Peters | 1221c0a | 2002-03-23 00:20:15 +0000 | [diff] [blame] | 1299 | #endif /* WITH_PYMALLOC */ |
| 1300 | |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 1301 | |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1302 | /*==========================================================================*/ |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 1303 | /* A x-platform debugging allocator. This doesn't manage memory directly, |
| 1304 | * it wraps a real allocator, adding extra debugging info to the memory blocks. |
| 1305 | */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1306 | |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1307 | /* Special bytes broadcast into debug memory blocks at appropriate times. |
| 1308 | * Strings of these are unlikely to be valid addresses, floats, ints or |
| 1309 | * 7-bit ASCII. |
| 1310 | */ |
| 1311 | #undef CLEANBYTE |
| 1312 | #undef DEADBYTE |
| 1313 | #undef FORBIDDENBYTE |
| 1314 | #define CLEANBYTE 0xCB /* clean (newly allocated) memory */ |
Tim Peters | 889f61d | 2002-07-10 19:29:49 +0000 | [diff] [blame] | 1315 | #define DEADBYTE 0xDB /* dead (newly freed) memory */ |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1316 | #define FORBIDDENBYTE 0xFB /* untouchable bytes at each end of a block */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1317 | |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 1318 | /* serialno is always incremented via calling this routine. The point is |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1319 | * to supply a single place to set a breakpoint. |
| 1320 | */ |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 1321 | static void |
Neil Schemenauer | bd02b14 | 2002-03-28 21:05:38 +0000 | [diff] [blame] | 1322 | bumpserialno(void) |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 1323 | { |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1324 | ++_PyRuntime.mem.serialno; |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1327 | #define SST SIZEOF_SIZE_T |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 1328 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1329 | /* Read sizeof(size_t) bytes at p as a big-endian size_t. */ |
| 1330 | static size_t |
| 1331 | read_size_t(const void *p) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1332 | { |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1333 | const uint8_t *q = (const uint8_t *)p; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1334 | size_t result = *q++; |
| 1335 | int i; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1337 | for (i = SST; --i > 0; ++q) |
| 1338 | result = (result << 8) | *q; |
| 1339 | return result; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1342 | /* Write n as a big-endian size_t, MSB at address p, LSB at |
| 1343 | * p + sizeof(size_t) - 1. |
| 1344 | */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1345 | static void |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1346 | write_size_t(void *p, size_t n) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1347 | { |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1348 | uint8_t *q = (uint8_t *)p + SST - 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1349 | int i; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1351 | for (i = SST; --i >= 0; --q) { |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1352 | *q = (uint8_t)(n & 0xff); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | n >>= 8; |
| 1354 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1357 | /* Let S = sizeof(size_t). The debug malloc asks for 4*S extra bytes and |
| 1358 | 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] | 1359 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1360 | p[0: S] |
| 1361 | Number of bytes originally asked for. This is a size_t, big-endian (easier |
| 1362 | to read in a memory dump). |
Georg Brandl | 7cba5fd | 2013-09-25 09:04:23 +0200 | [diff] [blame] | 1363 | p[S] |
Tim Peters | df099f5 | 2013-09-19 21:06:37 -0500 | [diff] [blame] | 1364 | API ID. See PEP 445. This is a character, but seems undocumented. |
| 1365 | p[S+1: 2*S] |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1366 | Copies of FORBIDDENBYTE. Used to catch under- writes and reads. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1367 | p[2*S: 2*S+n] |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1368 | The requested memory, filled with copies of CLEANBYTE. |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1369 | Used to catch reference to uninitialized memory. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1370 | &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] | 1371 | handled the request itself. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1372 | p[2*S+n: 2*S+n+S] |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1373 | Copies of FORBIDDENBYTE. Used to catch over- writes and reads. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1374 | p[2*S+n+S: 2*S+n+2*S] |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1375 | A serial number, incremented by 1 on each call to _PyMem_DebugMalloc |
| 1376 | and _PyMem_DebugRealloc. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1377 | This is a big-endian size_t. |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1378 | If "bad memory" is detected later, the serial number gives an |
| 1379 | excellent way to set a breakpoint on the next run, to capture the |
| 1380 | instant at which this block was passed out. |
| 1381 | */ |
| 1382 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1383 | static void * |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 1384 | _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] | 1385 | { |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1386 | debug_alloc_api_t *api = (debug_alloc_api_t *)ctx; |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1387 | uint8_t *p; /* base address of malloc'ed block */ |
| 1388 | uint8_t *tail; /* p + 2*SST + nbytes == pointer to tail pad bytes */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | size_t total; /* nbytes + 4*SST */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | bumpserialno(); |
| 1392 | total = nbytes + 4*SST; |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 1393 | if (nbytes > PY_SSIZE_T_MAX - 4*SST) |
| 1394 | /* overflow: can't represent total as a Py_ssize_t */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1395 | return NULL; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1396 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 1397 | if (use_calloc) |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1398 | p = (uint8_t *)api->alloc.calloc(api->alloc.ctx, 1, total); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 1399 | else |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1400 | p = (uint8_t *)api->alloc.malloc(api->alloc.ctx, total); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1401 | if (p == NULL) |
| 1402 | return NULL; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1404 | /* at p, write size (SST bytes), id (1 byte), pad (SST-1 bytes) */ |
| 1405 | write_size_t(p, nbytes); |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1406 | p[SST] = (uint8_t)api->api_id; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1407 | memset(p + SST + 1, FORBIDDENBYTE, SST-1); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1408 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 1409 | if (nbytes > 0 && !use_calloc) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1410 | memset(p + 2*SST, CLEANBYTE, nbytes); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | /* at tail, write pad (SST bytes) and serialno (SST bytes) */ |
| 1413 | tail = p + 2*SST + nbytes; |
| 1414 | memset(tail, FORBIDDENBYTE, SST); |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1415 | write_size_t(tail + SST, _PyRuntime.mem.serialno); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1417 | return p + 2*SST; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 1420 | static void * |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 1421 | _PyMem_DebugRawMalloc(void *ctx, size_t nbytes) |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 1422 | { |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 1423 | return _PyMem_DebugRawAlloc(0, ctx, nbytes); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | static void * |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 1427 | _PyMem_DebugRawCalloc(void *ctx, size_t nelem, size_t elsize) |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 1428 | { |
| 1429 | size_t nbytes; |
| 1430 | assert(elsize == 0 || nelem <= PY_SSIZE_T_MAX / elsize); |
| 1431 | nbytes = nelem * elsize; |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 1432 | return _PyMem_DebugRawAlloc(1, ctx, nbytes); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 1433 | } |
| 1434 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1435 | /* 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] | 1436 | particular, that the FORBIDDENBYTEs with the api ID are still intact). |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1437 | Then fills the original bytes with DEADBYTE. |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1438 | Then calls the underlying free. |
| 1439 | */ |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1440 | static void |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 1441 | _PyMem_DebugRawFree(void *ctx, void *p) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1442 | { |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1443 | debug_alloc_api_t *api = (debug_alloc_api_t *)ctx; |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1444 | uint8_t *q = (uint8_t *)p - 2*SST; /* address returned from malloc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1445 | size_t nbytes; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1447 | if (p == NULL) |
| 1448 | return; |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1449 | _PyMem_DebugCheckAddress(api->api_id, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | nbytes = read_size_t(q); |
| 1451 | nbytes += 4*SST; |
| 1452 | if (nbytes > 0) |
| 1453 | memset(q, DEADBYTE, nbytes); |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1454 | api->alloc.free(api->alloc.ctx, q); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1457 | static void * |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 1458 | _PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1459 | { |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1460 | debug_alloc_api_t *api = (debug_alloc_api_t *)ctx; |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1461 | uint8_t *q = (uint8_t *)p, *oldq; |
| 1462 | uint8_t *tail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1463 | size_t total; /* nbytes + 4*SST */ |
| 1464 | size_t original_nbytes; |
| 1465 | int i; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1466 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1467 | if (p == NULL) |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 1468 | return _PyMem_DebugRawAlloc(0, ctx, nbytes); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1469 | |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1470 | _PyMem_DebugCheckAddress(api->api_id, p); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1471 | bumpserialno(); |
| 1472 | original_nbytes = read_size_t(q - 2*SST); |
| 1473 | total = nbytes + 4*SST; |
Antoine Pitrou | cc23154 | 2014-11-02 18:40:09 +0100 | [diff] [blame] | 1474 | if (nbytes > PY_SSIZE_T_MAX - 4*SST) |
| 1475 | /* overflow: can't represent total as a Py_ssize_t */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1476 | return NULL; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1477 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1478 | /* Resize and add decorations. We may get a new pointer here, in which |
| 1479 | * case we didn't get the chance to mark the old memory with DEADBYTE, |
| 1480 | * but we live with that. |
| 1481 | */ |
Victor Stinner | c426636 | 2013-07-09 00:44:43 +0200 | [diff] [blame] | 1482 | oldq = q; |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1483 | q = (uint8_t *)api->alloc.realloc(api->alloc.ctx, q - 2*SST, total); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | if (q == NULL) |
| 1485 | return NULL; |
Tim Peters | 85cc1c4 | 2002-04-12 08:52:50 +0000 | [diff] [blame] | 1486 | |
Victor Stinner | c426636 | 2013-07-09 00:44:43 +0200 | [diff] [blame] | 1487 | if (q == oldq && nbytes < original_nbytes) { |
| 1488 | /* shrinking: mark old extra memory dead */ |
| 1489 | memset(q + nbytes, DEADBYTE, original_nbytes - nbytes); |
| 1490 | } |
| 1491 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1492 | write_size_t(q, nbytes); |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1493 | assert(q[SST] == (uint8_t)api->api_id); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | for (i = 1; i < SST; ++i) |
| 1495 | assert(q[SST + i] == FORBIDDENBYTE); |
| 1496 | q += 2*SST; |
Victor Stinner | c426636 | 2013-07-09 00:44:43 +0200 | [diff] [blame] | 1497 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1498 | tail = q + nbytes; |
| 1499 | memset(tail, FORBIDDENBYTE, SST); |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1500 | write_size_t(tail + SST, _PyRuntime.mem.serialno); |
Tim Peters | 85cc1c4 | 2002-04-12 08:52:50 +0000 | [diff] [blame] | 1501 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1502 | if (nbytes > original_nbytes) { |
| 1503 | /* growing: mark new extra memory clean */ |
| 1504 | memset(q + original_nbytes, CLEANBYTE, |
Stefan Krah | 735bb12 | 2010-11-26 10:54:09 +0000 | [diff] [blame] | 1505 | nbytes - original_nbytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1506 | } |
Tim Peters | 85cc1c4 | 2002-04-12 08:52:50 +0000 | [diff] [blame] | 1507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1508 | return q; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 1511 | static void |
| 1512 | _PyMem_DebugCheckGIL(void) |
| 1513 | { |
| 1514 | #ifdef WITH_THREAD |
| 1515 | if (!PyGILState_Check()) |
| 1516 | Py_FatalError("Python memory allocator called " |
| 1517 | "without holding the GIL"); |
| 1518 | #endif |
| 1519 | } |
| 1520 | |
| 1521 | static void * |
| 1522 | _PyMem_DebugMalloc(void *ctx, size_t nbytes) |
| 1523 | { |
| 1524 | _PyMem_DebugCheckGIL(); |
| 1525 | return _PyMem_DebugRawMalloc(ctx, nbytes); |
| 1526 | } |
| 1527 | |
| 1528 | static void * |
| 1529 | _PyMem_DebugCalloc(void *ctx, size_t nelem, size_t elsize) |
| 1530 | { |
| 1531 | _PyMem_DebugCheckGIL(); |
| 1532 | return _PyMem_DebugRawCalloc(ctx, nelem, elsize); |
| 1533 | } |
| 1534 | |
| 1535 | static void |
| 1536 | _PyMem_DebugFree(void *ctx, void *ptr) |
| 1537 | { |
| 1538 | _PyMem_DebugCheckGIL(); |
Victor Stinner | 0aed3a4 | 2016-03-23 11:30:43 +0100 | [diff] [blame] | 1539 | _PyMem_DebugRawFree(ctx, ptr); |
Victor Stinner | c4aec36 | 2016-03-14 22:26:53 +0100 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | static void * |
| 1543 | _PyMem_DebugRealloc(void *ctx, void *ptr, size_t nbytes) |
| 1544 | { |
| 1545 | _PyMem_DebugCheckGIL(); |
| 1546 | return _PyMem_DebugRawRealloc(ctx, ptr, nbytes); |
| 1547 | } |
| 1548 | |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1549 | /* 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] | 1550 | * If anything is wrong, print info to stderr via _PyObject_DebugDumpAddress, |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1551 | * and call Py_FatalError to kill the program. |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1552 | * The API id, is also checked. |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1553 | */ |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1554 | static void |
| 1555 | _PyMem_DebugCheckAddress(char api, const void *p) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1556 | { |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1557 | const uint8_t *q = (const uint8_t *)p; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1558 | char msgbuf[64]; |
| 1559 | char *msg; |
| 1560 | size_t nbytes; |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1561 | const uint8_t *tail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1562 | int i; |
| 1563 | char id; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1564 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1565 | if (p == NULL) { |
| 1566 | msg = "didn't expect a NULL pointer"; |
| 1567 | goto error; |
| 1568 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | /* Check the API id */ |
| 1571 | id = (char)q[-SST]; |
| 1572 | if (id != api) { |
| 1573 | msg = msgbuf; |
| 1574 | snprintf(msg, sizeof(msgbuf), "bad ID: Allocated using API '%c', verified using API '%c'", id, api); |
| 1575 | msgbuf[sizeof(msgbuf)-1] = 0; |
| 1576 | goto error; |
| 1577 | } |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1579 | /* Check the stuff at the start of p first: if there's underwrite |
| 1580 | * corruption, the number-of-bytes field may be nuts, and checking |
| 1581 | * the tail could lead to a segfault then. |
| 1582 | */ |
| 1583 | for (i = SST-1; i >= 1; --i) { |
| 1584 | if (*(q-i) != FORBIDDENBYTE) { |
| 1585 | msg = "bad leading pad byte"; |
| 1586 | goto error; |
| 1587 | } |
| 1588 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1590 | nbytes = read_size_t(q - 2*SST); |
| 1591 | tail = q + nbytes; |
| 1592 | for (i = 0; i < SST; ++i) { |
| 1593 | if (tail[i] != FORBIDDENBYTE) { |
| 1594 | msg = "bad trailing pad byte"; |
| 1595 | goto error; |
| 1596 | } |
| 1597 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1599 | return; |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 1600 | |
| 1601 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1602 | _PyObject_DebugDumpAddress(p); |
| 1603 | Py_FatalError(msg); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1606 | /* Display info to stderr about the memory block at p. */ |
Victor Stinner | 0507bf5 | 2013-07-07 02:05:46 +0200 | [diff] [blame] | 1607 | static void |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1608 | _PyObject_DebugDumpAddress(const void *p) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1609 | { |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1610 | const uint8_t *q = (const uint8_t *)p; |
| 1611 | const uint8_t *tail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1612 | size_t nbytes, serial; |
| 1613 | int i; |
| 1614 | int ok; |
| 1615 | char id; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1616 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1617 | fprintf(stderr, "Debug memory block at address p=%p:", p); |
| 1618 | if (p == NULL) { |
| 1619 | fprintf(stderr, "\n"); |
| 1620 | return; |
| 1621 | } |
| 1622 | id = (char)q[-SST]; |
| 1623 | fprintf(stderr, " API '%c'\n", id); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1625 | nbytes = read_size_t(q - 2*SST); |
| 1626 | fprintf(stderr, " %" PY_FORMAT_SIZE_T "u bytes originally " |
| 1627 | "requested\n", nbytes); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1629 | /* In case this is nuts, check the leading pad bytes first. */ |
| 1630 | fprintf(stderr, " The %d pad bytes at p-%d are ", SST-1, SST-1); |
| 1631 | ok = 1; |
| 1632 | for (i = 1; i <= SST-1; ++i) { |
| 1633 | if (*(q-i) != FORBIDDENBYTE) { |
| 1634 | ok = 0; |
| 1635 | break; |
| 1636 | } |
| 1637 | } |
| 1638 | if (ok) |
| 1639 | fputs("FORBIDDENBYTE, as expected.\n", stderr); |
| 1640 | else { |
| 1641 | fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n", |
| 1642 | FORBIDDENBYTE); |
| 1643 | for (i = SST-1; i >= 1; --i) { |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1644 | const uint8_t byte = *(q-i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1645 | fprintf(stderr, " at p-%d: 0x%02x", i, byte); |
| 1646 | if (byte != FORBIDDENBYTE) |
| 1647 | fputs(" *** OUCH", stderr); |
| 1648 | fputc('\n', stderr); |
| 1649 | } |
Tim Peters | 449b5a8 | 2002-04-28 06:14:45 +0000 | [diff] [blame] | 1650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1651 | fputs(" Because memory is corrupted at the start, the " |
| 1652 | "count of bytes requested\n" |
| 1653 | " may be bogus, and checking the trailing pad " |
| 1654 | "bytes may segfault.\n", stderr); |
| 1655 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1657 | tail = q + nbytes; |
| 1658 | fprintf(stderr, " The %d pad bytes at tail=%p are ", SST, tail); |
| 1659 | ok = 1; |
| 1660 | for (i = 0; i < SST; ++i) { |
| 1661 | if (tail[i] != FORBIDDENBYTE) { |
| 1662 | ok = 0; |
| 1663 | break; |
| 1664 | } |
| 1665 | } |
| 1666 | if (ok) |
| 1667 | fputs("FORBIDDENBYTE, as expected.\n", stderr); |
| 1668 | else { |
| 1669 | fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n", |
Stefan Krah | 735bb12 | 2010-11-26 10:54:09 +0000 | [diff] [blame] | 1670 | FORBIDDENBYTE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1671 | for (i = 0; i < SST; ++i) { |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1672 | const uint8_t byte = tail[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1673 | fprintf(stderr, " at tail+%d: 0x%02x", |
Stefan Krah | 735bb12 | 2010-11-26 10:54:09 +0000 | [diff] [blame] | 1674 | i, byte); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1675 | if (byte != FORBIDDENBYTE) |
| 1676 | fputs(" *** OUCH", stderr); |
| 1677 | fputc('\n', stderr); |
| 1678 | } |
| 1679 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1681 | serial = read_size_t(tail + SST); |
| 1682 | fprintf(stderr, " The block was made by call #%" PY_FORMAT_SIZE_T |
| 1683 | "u to debug malloc/realloc.\n", serial); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1685 | if (nbytes > 0) { |
| 1686 | i = 0; |
| 1687 | fputs(" Data at p:", stderr); |
| 1688 | /* print up to 8 bytes at the start */ |
| 1689 | while (q < tail && i < 8) { |
| 1690 | fprintf(stderr, " %02x", *q); |
| 1691 | ++i; |
| 1692 | ++q; |
| 1693 | } |
| 1694 | /* and up to 8 at the end */ |
| 1695 | if (q < tail) { |
| 1696 | if (tail - q > 8) { |
| 1697 | fputs(" ...", stderr); |
| 1698 | q = tail - 8; |
| 1699 | } |
| 1700 | while (q < tail) { |
| 1701 | fprintf(stderr, " %02x", *q); |
| 1702 | ++q; |
| 1703 | } |
| 1704 | } |
| 1705 | fputc('\n', stderr); |
| 1706 | } |
Victor Stinner | 0611c26 | 2016-03-15 22:22:13 +0100 | [diff] [blame] | 1707 | fputc('\n', stderr); |
| 1708 | |
| 1709 | fflush(stderr); |
| 1710 | _PyMem_DumpTraceback(fileno(stderr), p); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1713 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1714 | static size_t |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1715 | printone(FILE *out, const char* msg, size_t value) |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1716 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1717 | int i, k; |
| 1718 | char buf[100]; |
| 1719 | size_t origvalue = value; |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1720 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1721 | fputs(msg, out); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1722 | for (i = (int)strlen(msg); i < 35; ++i) |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1723 | fputc(' ', out); |
| 1724 | fputc('=', out); |
Tim Peters | 49f2681 | 2002-04-06 01:45:35 +0000 | [diff] [blame] | 1725 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1726 | /* Write the value with commas. */ |
| 1727 | i = 22; |
| 1728 | buf[i--] = '\0'; |
| 1729 | buf[i--] = '\n'; |
| 1730 | k = 3; |
| 1731 | do { |
| 1732 | size_t nextvalue = value / 10; |
Benjamin Peterson | 2dba1ee | 2013-02-20 16:54:30 -0500 | [diff] [blame] | 1733 | unsigned int digit = (unsigned int)(value - nextvalue * 10); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1734 | value = nextvalue; |
| 1735 | buf[i--] = (char)(digit + '0'); |
| 1736 | --k; |
| 1737 | if (k == 0 && value && i >= 0) { |
| 1738 | k = 3; |
| 1739 | buf[i--] = ','; |
| 1740 | } |
| 1741 | } while (value && i >= 0); |
Tim Peters | 49f2681 | 2002-04-06 01:45:35 +0000 | [diff] [blame] | 1742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1743 | while (i >= 0) |
| 1744 | buf[i--] = ' '; |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1745 | fputs(buf, out); |
Tim Peters | 49f2681 | 2002-04-06 01:45:35 +0000 | [diff] [blame] | 1746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1747 | return origvalue; |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1748 | } |
| 1749 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1750 | void |
| 1751 | _PyDebugAllocatorStats(FILE *out, |
| 1752 | const char *block_name, int num_blocks, size_t sizeof_block) |
| 1753 | { |
| 1754 | char buf1[128]; |
| 1755 | char buf2[128]; |
| 1756 | PyOS_snprintf(buf1, sizeof(buf1), |
Tim Peters | eaa3bcc | 2013-09-05 22:57:04 -0500 | [diff] [blame] | 1757 | "%d %ss * %" PY_FORMAT_SIZE_T "d bytes each", |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1758 | num_blocks, block_name, sizeof_block); |
| 1759 | PyOS_snprintf(buf2, sizeof(buf2), |
| 1760 | "%48s ", buf1); |
| 1761 | (void)printone(out, buf2, num_blocks * sizeof_block); |
| 1762 | } |
| 1763 | |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 1764 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1765 | #ifdef WITH_PYMALLOC |
| 1766 | |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 1767 | #ifdef Py_DEBUG |
| 1768 | /* Is target in the list? The list is traversed via the nextpool pointers. |
| 1769 | * The list may be NULL-terminated, or circular. Return 1 if target is in |
| 1770 | * list, else 0. |
| 1771 | */ |
| 1772 | static int |
| 1773 | pool_is_in_list(const poolp target, poolp list) |
| 1774 | { |
| 1775 | poolp origlist = list; |
| 1776 | assert(target != NULL); |
| 1777 | if (list == NULL) |
| 1778 | return 0; |
| 1779 | do { |
| 1780 | if (target == list) |
| 1781 | return 1; |
| 1782 | list = list->nextpool; |
| 1783 | } while (list != NULL && list != origlist); |
| 1784 | return 0; |
| 1785 | } |
| 1786 | #endif |
| 1787 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1788 | /* Print summary info to "out" about the state of pymalloc's structures. |
Tim Peters | 08d8215 | 2002-04-18 22:25:03 +0000 | [diff] [blame] | 1789 | * In Py_DEBUG mode, also perform some expensive internal consistency |
| 1790 | * checks. |
| 1791 | */ |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1792 | void |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1793 | _PyObject_DebugMallocStats(FILE *out) |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1794 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1795 | uint i; |
| 1796 | const uint numclasses = SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT; |
| 1797 | /* # of pools, allocated blocks, and free blocks per class index */ |
| 1798 | size_t numpools[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT]; |
| 1799 | size_t numblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT]; |
| 1800 | size_t numfreeblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT]; |
| 1801 | /* total # of allocated bytes in used and full pools */ |
| 1802 | size_t allocated_bytes = 0; |
| 1803 | /* total # of available bytes in used pools */ |
| 1804 | size_t available_bytes = 0; |
| 1805 | /* # of free pools + pools not yet carved out of current arena */ |
| 1806 | uint numfreepools = 0; |
| 1807 | /* # of bytes for arena alignment padding */ |
| 1808 | size_t arena_alignment = 0; |
| 1809 | /* # of bytes in used and full pools used for pool_headers */ |
| 1810 | size_t pool_header_bytes = 0; |
| 1811 | /* # of bytes in used and full pools wasted due to quantization, |
| 1812 | * i.e. the necessarily leftover space at the ends of used and |
| 1813 | * full pools. |
| 1814 | */ |
| 1815 | size_t quantization = 0; |
| 1816 | /* # of arenas actually allocated. */ |
| 1817 | size_t narenas = 0; |
| 1818 | /* running total -- should equal narenas * ARENA_SIZE */ |
| 1819 | size_t total; |
| 1820 | char buf[128]; |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1821 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1822 | fprintf(out, "Small block threshold = %d, in %u size classes.\n", |
Stefan Krah | 735bb12 | 2010-11-26 10:54:09 +0000 | [diff] [blame] | 1823 | SMALL_REQUEST_THRESHOLD, numclasses); |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1825 | for (i = 0; i < numclasses; ++i) |
| 1826 | numpools[i] = numblocks[i] = numfreeblocks[i] = 0; |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1827 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1828 | /* Because full pools aren't linked to from anything, it's easiest |
| 1829 | * to march over all the arenas. If we're lucky, most of the memory |
| 1830 | * will be living in full pools -- would be a shame to miss them. |
| 1831 | */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1832 | for (i = 0; i < _PyRuntime.mem.maxarenas; ++i) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1833 | uint j; |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1834 | uintptr_t base = _PyRuntime.mem.arenas[i].address; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1835 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1836 | /* Skip arenas which are not allocated. */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1837 | if (_PyRuntime.mem.arenas[i].address == (uintptr_t)NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1838 | continue; |
| 1839 | narenas += 1; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1840 | |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1841 | numfreepools += _PyRuntime.mem.arenas[i].nfreepools; |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1843 | /* round up to pool alignment */ |
Benjamin Peterson | 5d4b09c | 2016-09-18 19:24:52 -0700 | [diff] [blame] | 1844 | if (base & (uintptr_t)POOL_SIZE_MASK) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1845 | arena_alignment += POOL_SIZE; |
Benjamin Peterson | 5d4b09c | 2016-09-18 19:24:52 -0700 | [diff] [blame] | 1846 | base &= ~(uintptr_t)POOL_SIZE_MASK; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | base += POOL_SIZE; |
| 1848 | } |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1849 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1850 | /* visit every pool in the arena */ |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1851 | assert(base <= (uintptr_t) _PyRuntime.mem.arenas[i].pool_address); |
| 1852 | for (j = 0; base < (uintptr_t) _PyRuntime.mem.arenas[i].pool_address; |
Benjamin Peterson | 19517e4 | 2016-09-18 19:22:22 -0700 | [diff] [blame] | 1853 | ++j, base += POOL_SIZE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | poolp p = (poolp)base; |
| 1855 | const uint sz = p->szidx; |
| 1856 | uint freeblocks; |
Tim Peters | 08d8215 | 2002-04-18 22:25:03 +0000 | [diff] [blame] | 1857 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1858 | if (p->ref.count == 0) { |
| 1859 | /* currently unused */ |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 1860 | #ifdef Py_DEBUG |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1861 | assert(pool_is_in_list(p, _PyRuntime.mem.arenas[i].freepools)); |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 1862 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1863 | continue; |
| 1864 | } |
| 1865 | ++numpools[sz]; |
| 1866 | numblocks[sz] += p->ref.count; |
| 1867 | freeblocks = NUMBLOCKS(sz) - p->ref.count; |
| 1868 | numfreeblocks[sz] += freeblocks; |
Tim Peters | 08d8215 | 2002-04-18 22:25:03 +0000 | [diff] [blame] | 1869 | #ifdef Py_DEBUG |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1870 | if (freeblocks > 0) |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1871 | assert(pool_is_in_list(p, _PyRuntime.mem.usedpools[sz + sz])); |
Tim Peters | 08d8215 | 2002-04-18 22:25:03 +0000 | [diff] [blame] | 1872 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1873 | } |
| 1874 | } |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1875 | assert(narenas == _PyRuntime.mem.narenas_currently_allocated); |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1876 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1877 | fputc('\n', out); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1878 | fputs("class size num pools blocks in use avail blocks\n" |
| 1879 | "----- ---- --------- ------------- ------------\n", |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1880 | out); |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1882 | for (i = 0; i < numclasses; ++i) { |
| 1883 | size_t p = numpools[i]; |
| 1884 | size_t b = numblocks[i]; |
| 1885 | size_t f = numfreeblocks[i]; |
| 1886 | uint size = INDEX2SIZE(i); |
| 1887 | if (p == 0) { |
| 1888 | assert(b == 0 && f == 0); |
| 1889 | continue; |
| 1890 | } |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1891 | fprintf(out, "%5u %6u " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 | "%11" PY_FORMAT_SIZE_T "u " |
| 1893 | "%15" PY_FORMAT_SIZE_T "u " |
| 1894 | "%13" PY_FORMAT_SIZE_T "u\n", |
Stefan Krah | 735bb12 | 2010-11-26 10:54:09 +0000 | [diff] [blame] | 1895 | i, size, p, b, f); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1896 | allocated_bytes += b * size; |
| 1897 | available_bytes += f * size; |
| 1898 | pool_header_bytes += p * POOL_OVERHEAD; |
| 1899 | quantization += p * ((POOL_SIZE - POOL_OVERHEAD) % size); |
| 1900 | } |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1901 | fputc('\n', out); |
Victor Stinner | 34be807c | 2016-03-14 12:04:26 +0100 | [diff] [blame] | 1902 | if (_PyMem_DebugEnabled()) |
Eric Snow | 76d5abc | 2017-09-05 18:26:16 -0700 | [diff] [blame] | 1903 | (void)printone(out, "# times object malloc called", _PyRuntime.mem.serialno); |
| 1904 | (void)printone(out, "# arenas allocated total", _PyRuntime.mem.ntimes_arena_allocated); |
| 1905 | (void)printone(out, "# arenas reclaimed", _PyRuntime.mem.ntimes_arena_allocated - narenas); |
| 1906 | (void)printone(out, "# arenas highwater mark", _PyRuntime.mem.narenas_highwater); |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1907 | (void)printone(out, "# arenas allocated current", narenas); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1908 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | PyOS_snprintf(buf, sizeof(buf), |
| 1910 | "%" PY_FORMAT_SIZE_T "u arenas * %d bytes/arena", |
| 1911 | narenas, ARENA_SIZE); |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1912 | (void)printone(out, buf, narenas * ARENA_SIZE); |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1913 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1914 | fputc('\n', out); |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1915 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1916 | total = printone(out, "# bytes in allocated blocks", allocated_bytes); |
| 1917 | total += printone(out, "# bytes in available blocks", available_bytes); |
Tim Peters | 49f2681 | 2002-04-06 01:45:35 +0000 | [diff] [blame] | 1918 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1919 | PyOS_snprintf(buf, sizeof(buf), |
| 1920 | "%u unused pools * %d bytes", numfreepools, POOL_SIZE); |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1921 | total += printone(out, buf, (size_t)numfreepools * POOL_SIZE); |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1922 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1923 | total += printone(out, "# bytes lost to pool headers", pool_header_bytes); |
| 1924 | total += printone(out, "# bytes lost to quantization", quantization); |
| 1925 | total += printone(out, "# bytes lost to arena alignment", arena_alignment); |
| 1926 | (void)printone(out, "Total", total); |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
David Malcolm | 49526f4 | 2012-06-22 14:55:41 -0400 | [diff] [blame] | 1929 | #endif /* #ifdef WITH_PYMALLOC */ |