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