Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1 | /* An object allocator for Python. |
| 2 | |
| 3 | Here is an introduction to the layers of the Python memory architecture, |
| 4 | showing where the object allocator is actually used (layer +2), It is |
| 5 | called for every object allocation and deallocation (PyObject_New/Del), |
| 6 | unless the object-specific allocators implement a proprietary allocation |
| 7 | scheme (ex.: ints use a simple free list). This is also the place where |
| 8 | the cyclic garbage collector operates selectively on container objects. |
| 9 | |
| 10 | |
| 11 | Object-specific allocators |
| 12 | _____ ______ ______ ________ |
| 13 | [ int ] [ dict ] [ list ] ... [ string ] Python core | |
| 14 | +3 | <----- Object-specific memory -----> | <-- Non-object memory --> | |
| 15 | _______________________________ | | |
| 16 | [ Python's object allocator ] | | |
| 17 | +2 | ####### Object memory ####### | <------ Internal buffers ------> | |
| 18 | ______________________________________________________________ | |
| 19 | [ Python's raw memory allocator (PyMem_ API) ] | |
| 20 | +1 | <----- Python memory (under PyMem manager's control) ------> | | |
| 21 | __________________________________________________________________ |
| 22 | [ Underlying general-purpose allocator (ex: C library malloc) ] |
| 23 | 0 | <------ Virtual memory allocated for the python process -------> | |
| 24 | |
| 25 | ========================================================================= |
| 26 | _______________________________________________________________________ |
| 27 | [ OS-specific Virtual Memory Manager (VMM) ] |
| 28 | -1 | <--- Kernel dynamic storage allocation & management (page-based) ---> | |
| 29 | __________________________________ __________________________________ |
| 30 | [ ] [ ] |
| 31 | -2 | <-- Physical memory: ROM/RAM --> | | <-- Secondary storage (swap) --> | |
| 32 | |
| 33 | */ |
| 34 | /*==========================================================================*/ |
| 35 | |
| 36 | /* A fast, special-purpose memory allocator for small blocks, to be used |
| 37 | on top of a general-purpose malloc -- heavily based on previous art. */ |
| 38 | |
| 39 | /* Vladimir Marangozov -- August 2000 */ |
| 40 | |
| 41 | /* |
| 42 | * "Memory management is where the rubber meets the road -- if we do the wrong |
| 43 | * thing at any level, the results will not be good. And if we don't make the |
| 44 | * levels work well together, we are in serious trouble." (1) |
| 45 | * |
| 46 | * (1) Paul R. Wilson, Mark S. Johnstone, Michael Neely, and David Boles, |
| 47 | * "Dynamic Storage Allocation: A Survey and Critical Review", |
| 48 | * in Proc. 1995 Int'l. Workshop on Memory Management, September 1995. |
| 49 | */ |
| 50 | |
| 51 | /* #undef WITH_MEMORY_LIMITS */ /* disable mem limit checks */ |
| 52 | #define WITH_MALLOC_HOOKS /* for profiling & debugging */ |
| 53 | |
| 54 | /*==========================================================================*/ |
| 55 | |
| 56 | /* |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 57 | * Allocation strategy abstract: |
| 58 | * |
| 59 | * For small requests, the allocator sub-allocates <Big> blocks of memory. |
| 60 | * Requests greater than 256 bytes are routed to the system's allocator. |
| 61 | * |
| 62 | * Small requests are grouped in size classes spaced 8 bytes apart, due |
| 63 | * to the required valid alignment of the returned address. Requests of |
| 64 | * a particular size are serviced from memory pools of 4K (one VMM page). |
| 65 | * Pools are fragmented on demand and contain free lists of blocks of one |
| 66 | * particular size class. In other words, there is a fixed-size allocator |
| 67 | * for each size class. Free pools are shared by the different allocators |
| 68 | * thus minimizing the space reserved for a particular size class. |
| 69 | * |
| 70 | * This allocation strategy is a variant of what is known as "simple |
| 71 | * segregated storage based on array of free lists". The main drawback of |
| 72 | * simple segregated storage is that we might end up with lot of reserved |
| 73 | * memory for the different free lists, which degenerate in time. To avoid |
| 74 | * this, we partition each free list in pools and we share dynamically the |
| 75 | * reserved space between all free lists. This technique is quite efficient |
| 76 | * for memory intensive programs which allocate mainly small-sized blocks. |
| 77 | * |
| 78 | * For small requests we have the following table: |
| 79 | * |
| 80 | * Request in bytes Size of allocated block Size class idx |
| 81 | * ---------------------------------------------------------------- |
| 82 | * 1-8 8 0 |
| 83 | * 9-16 16 1 |
| 84 | * 17-24 24 2 |
| 85 | * 25-32 32 3 |
| 86 | * 33-40 40 4 |
| 87 | * 41-48 48 5 |
| 88 | * 49-56 56 6 |
| 89 | * 57-64 64 7 |
| 90 | * 65-72 72 8 |
| 91 | * ... ... ... |
| 92 | * 241-248 248 30 |
| 93 | * 249-256 256 31 |
| 94 | * |
| 95 | * 0, 257 and up: routed to the underlying allocator. |
| 96 | */ |
| 97 | |
| 98 | /*==========================================================================*/ |
| 99 | |
| 100 | /* |
| 101 | * -- Main tunable settings section -- |
| 102 | */ |
| 103 | |
| 104 | /* |
| 105 | * Alignment of addresses returned to the user. 8-bytes alignment works |
| 106 | * on most current architectures (with 32-bit or 64-bit address busses). |
| 107 | * The alignment value is also used for grouping small requests in size |
| 108 | * classes spaced ALIGNMENT bytes apart. |
| 109 | * |
| 110 | * You shouldn't change this unless you know what you are doing. |
| 111 | */ |
| 112 | |
| 113 | #define ALIGNMENT 8 /* must be 2^N */ |
| 114 | #define ALIGNMENT_SHIFT 3 |
| 115 | #define ALIGNMENT_MASK (ALIGNMENT - 1) |
| 116 | |
| 117 | /* |
| 118 | * Max size threshold below which malloc requests are considered to be |
| 119 | * small enough in order to use preallocated memory pools. You can tune |
| 120 | * this value according to your application behaviour and memory needs. |
| 121 | * |
| 122 | * The following invariants must hold: |
| 123 | * 1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 256 |
| 124 | * 2) SMALL_REQUEST_THRESHOLD == N * ALIGNMENT |
| 125 | * |
| 126 | * Although not required, for better performance and space efficiency, |
| 127 | * it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2. |
| 128 | */ |
| 129 | |
| 130 | /* |
| 131 | * For Python compiled on systems with 32 bit pointers and integers, |
| 132 | * a value of 64 (= 8 * 8) is a reasonable speed/space tradeoff for |
| 133 | * the object allocator. To adjust automatically this threshold for |
| 134 | * systems with 64 bit pointers, we make this setting depend on a |
| 135 | * Python-specific slot size unit = sizeof(long) + sizeof(void *), |
| 136 | * which is expected to be 8, 12 or 16 bytes. |
| 137 | */ |
| 138 | |
| 139 | #define _PYOBJECT_THRESHOLD ((SIZEOF_LONG + SIZEOF_VOID_P) * ALIGNMENT) |
| 140 | |
| 141 | #define SMALL_REQUEST_THRESHOLD _PYOBJECT_THRESHOLD /* must be N * ALIGNMENT */ |
| 142 | |
| 143 | #define NB_SMALL_SIZE_CLASSES (SMALL_REQUEST_THRESHOLD / ALIGNMENT) |
| 144 | |
| 145 | /* |
| 146 | * The system's VMM page size can be obtained on most unices with a |
| 147 | * getpagesize() call or deduced from various header files. To make |
| 148 | * things simpler, we assume that it is 4K, which is OK for most systems. |
| 149 | * It is probably better if this is the native page size, but it doesn't |
| 150 | * have to be. |
| 151 | */ |
| 152 | |
| 153 | #define SYSTEM_PAGE_SIZE (4 * 1024) |
| 154 | #define SYSTEM_PAGE_SIZE_MASK (SYSTEM_PAGE_SIZE - 1) |
| 155 | |
| 156 | /* |
| 157 | * Maximum amount of memory managed by the allocator for small requests. |
| 158 | */ |
| 159 | |
| 160 | #ifdef WITH_MEMORY_LIMITS |
| 161 | #ifndef SMALL_MEMORY_LIMIT |
| 162 | #define SMALL_MEMORY_LIMIT (64 * 1024 * 1024) /* 64 MB -- more? */ |
| 163 | #endif |
| 164 | #endif |
| 165 | |
| 166 | /* |
| 167 | * The allocator sub-allocates <Big> blocks of memory (called arenas) aligned |
| 168 | * on a page boundary. This is a reserved virtual address space for the |
| 169 | * current process (obtained through a malloc call). In no way this means |
| 170 | * that the memory arenas will be used entirely. A malloc(<Big>) is usually |
| 171 | * an address range reservation for <Big> bytes, unless all pages within this |
| 172 | * space are referenced subsequently. So malloc'ing big blocks and not using |
| 173 | * them does not mean "wasting memory". It's an addressable range wastage... |
| 174 | * |
| 175 | * Therefore, allocating arenas with malloc is not optimal, because there is |
| 176 | * some address space wastage, but this is the most portable way to request |
| 177 | * memory from the system accross various platforms. |
| 178 | */ |
| 179 | |
| 180 | #define ARENA_SIZE (256 * 1024 - SYSTEM_PAGE_SIZE) /* 256k - 1p */ |
| 181 | |
| 182 | #ifdef WITH_MEMORY_LIMITS |
| 183 | #define MAX_ARENAS (SMALL_MEMORY_LIMIT / ARENA_SIZE) |
| 184 | #endif |
| 185 | |
| 186 | /* |
| 187 | * Size of the pools used for small blocks. Should be a power of 2, |
| 188 | * between 1K and SYSTEM_PAGE_SIZE, that is: 1k, 2k, 4k, eventually 8k. |
| 189 | */ |
| 190 | |
| 191 | #define POOL_SIZE SYSTEM_PAGE_SIZE /* must be 2^N */ |
| 192 | #define POOL_SIZE_MASK SYSTEM_PAGE_SIZE_MASK |
| 193 | #define POOL_MAGIC 0x74D3A651 /* authentication id */ |
| 194 | |
| 195 | #define ARENA_NB_POOLS (ARENA_SIZE / POOL_SIZE) |
| 196 | #define ARENA_NB_PAGES (ARENA_SIZE / SYSTEM_PAGE_SIZE) |
| 197 | |
| 198 | /* |
| 199 | * -- End of tunable settings section -- |
| 200 | */ |
| 201 | |
| 202 | /*==========================================================================*/ |
| 203 | |
| 204 | /* |
| 205 | * Locking |
| 206 | * |
| 207 | * To reduce lock contention, it would probably be better to refine the |
| 208 | * crude function locking with per size class locking. I'm not positive |
| 209 | * however, whether it's worth switching to such locking policy because |
| 210 | * of the performance penalty it might introduce. |
| 211 | * |
| 212 | * The following macros describe the simplest (should also be the fastest) |
| 213 | * lock object on a particular platform and the init/fini/lock/unlock |
| 214 | * operations on it. The locks defined here are not expected to be recursive |
| 215 | * because it is assumed that they will always be called in the order: |
| 216 | * INIT, [LOCK, UNLOCK]*, FINI. |
| 217 | */ |
| 218 | |
| 219 | /* |
| 220 | * Python's threads are serialized, so object malloc locking is disabled. |
| 221 | */ |
| 222 | #define SIMPLELOCK_DECL(lock) /* simple lock declaration */ |
| 223 | #define SIMPLELOCK_INIT(lock) /* allocate (if needed) and initialize */ |
| 224 | #define SIMPLELOCK_FINI(lock) /* free/destroy an existing lock */ |
| 225 | #define SIMPLELOCK_LOCK(lock) /* acquire released lock */ |
| 226 | #define SIMPLELOCK_UNLOCK(lock) /* release acquired lock */ |
| 227 | |
| 228 | /* |
| 229 | * Basic types |
| 230 | * I don't care if these are defined in <sys/types.h> or elsewhere. Axiom. |
| 231 | */ |
| 232 | |
| 233 | #undef uchar |
| 234 | #define uchar unsigned char /* assuming == 8 bits */ |
| 235 | |
| 236 | #undef ushort |
| 237 | #define ushort unsigned short /* assuming >= 16 bits */ |
| 238 | |
| 239 | #undef uint |
| 240 | #define uint unsigned int /* assuming >= 16 bits */ |
| 241 | |
| 242 | #undef ulong |
| 243 | #define ulong unsigned long /* assuming >= 32 bits */ |
| 244 | |
| 245 | #undef off_t |
| 246 | #define off_t uint /* 16 bits <= off_t <= 64 bits */ |
| 247 | |
| 248 | /* When you say memory, my mind reasons in terms of (pointers to) blocks */ |
| 249 | typedef uchar block; |
| 250 | |
| 251 | /* Pool for small blocks */ |
| 252 | struct pool_header { |
Tim Peters | b233652 | 2001-03-11 18:36:13 +0000 | [diff] [blame] | 253 | union { block *_padding; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 254 | uint count; } ref; /* number of allocated blocks */ |
| 255 | block *freeblock; /* pool's free list head */ |
| 256 | struct pool_header *nextpool; /* next pool of this size class */ |
| 257 | struct pool_header *prevpool; /* previous pool "" */ |
| 258 | struct pool_header *pooladdr; /* pool address (always aligned) */ |
| 259 | uint magic; /* pool magic number */ |
| 260 | uint szidx; /* block size class index */ |
| 261 | uint capacity; /* pool capacity in # of blocks */ |
| 262 | }; |
| 263 | |
| 264 | typedef struct pool_header *poolp; |
| 265 | |
| 266 | #undef ROUNDUP |
| 267 | #define ROUNDUP(x) (((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK) |
| 268 | #define POOL_OVERHEAD ROUNDUP(sizeof(struct pool_header)) |
| 269 | |
| 270 | #define DUMMY_SIZE_IDX 0xffff /* size class of newly cached pools */ |
| 271 | |
| 272 | /*==========================================================================*/ |
| 273 | |
| 274 | /* |
| 275 | * This malloc lock |
| 276 | */ |
Tim Peters | b233652 | 2001-03-11 18:36:13 +0000 | [diff] [blame] | 277 | SIMPLELOCK_DECL(_malloc_lock); |
| 278 | #define LOCK() SIMPLELOCK_LOCK(_malloc_lock) |
| 279 | #define UNLOCK() SIMPLELOCK_UNLOCK(_malloc_lock) |
| 280 | #define LOCK_INIT() SIMPLELOCK_INIT(_malloc_lock) |
| 281 | #define LOCK_FINI() SIMPLELOCK_FINI(_malloc_lock) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 282 | |
| 283 | /* |
| 284 | * Pool table -- doubly linked lists of partially used pools |
| 285 | */ |
| 286 | #define PTA(x) ((poolp )((uchar *)&(usedpools[2*(x)]) - 2*sizeof(block *))) |
| 287 | #define PT(x) PTA(x), PTA(x) |
| 288 | |
| 289 | static poolp usedpools[2 * ((NB_SMALL_SIZE_CLASSES + 7) / 8) * 8] = { |
| 290 | PT(0), PT(1), PT(2), PT(3), PT(4), PT(5), PT(6), PT(7) |
| 291 | #if NB_SMALL_SIZE_CLASSES > 8 |
| 292 | , PT(8), PT(9), PT(10), PT(11), PT(12), PT(13), PT(14), PT(15) |
| 293 | #if NB_SMALL_SIZE_CLASSES > 16 |
| 294 | , PT(16), PT(17), PT(18), PT(19), PT(20), PT(21), PT(22), PT(23) |
| 295 | #if NB_SMALL_SIZE_CLASSES > 24 |
| 296 | , PT(24), PT(25), PT(26), PT(27), PT(28), PT(29), PT(30), PT(31) |
| 297 | #if NB_SMALL_SIZE_CLASSES > 32 |
| 298 | , PT(32), PT(33), PT(34), PT(35), PT(36), PT(37), PT(38), PT(39) |
| 299 | #if NB_SMALL_SIZE_CLASSES > 40 |
| 300 | , PT(40), PT(41), PT(42), PT(43), PT(44), PT(45), PT(46), PT(47) |
| 301 | #if NB_SMALL_SIZE_CLASSES > 48 |
| 302 | , PT(48), PT(49), PT(50), PT(51), PT(52), PT(53), PT(54), PT(55) |
| 303 | #if NB_SMALL_SIZE_CLASSES > 56 |
| 304 | , PT(56), PT(57), PT(58), PT(59), PT(60), PT(61), PT(62), PT(63) |
| 305 | #endif /* NB_SMALL_SIZE_CLASSES > 56 */ |
| 306 | #endif /* NB_SMALL_SIZE_CLASSES > 48 */ |
| 307 | #endif /* NB_SMALL_SIZE_CLASSES > 40 */ |
| 308 | #endif /* NB_SMALL_SIZE_CLASSES > 32 */ |
| 309 | #endif /* NB_SMALL_SIZE_CLASSES > 24 */ |
| 310 | #endif /* NB_SMALL_SIZE_CLASSES > 16 */ |
| 311 | #endif /* NB_SMALL_SIZE_CLASSES > 8 */ |
| 312 | }; |
| 313 | |
| 314 | /* |
| 315 | * Free (cached) pools |
| 316 | */ |
| 317 | static poolp freepools = NULL; /* free list for cached pools */ |
| 318 | |
| 319 | /* |
| 320 | * Arenas |
| 321 | */ |
| 322 | static uint arenacnt = 0; /* number of allocated arenas */ |
| 323 | static uint watermark = ARENA_NB_POOLS; /* number of pools allocated from |
| 324 | the current arena */ |
| 325 | static block *arenalist = NULL; /* list of allocated arenas */ |
| 326 | static block *arenabase = NULL; /* free space start address in |
| 327 | current arena */ |
| 328 | |
| 329 | /* |
| 330 | * Hooks |
| 331 | */ |
| 332 | #ifdef WITH_MALLOC_HOOKS |
| 333 | static void *(*malloc_hook)(size_t) = NULL; |
| 334 | static void *(*calloc_hook)(size_t, size_t) = NULL; |
| 335 | static void *(*realloc_hook)(void *, size_t) = NULL; |
| 336 | static void (*free_hook)(void *) = NULL; |
| 337 | #endif /* !WITH_MALLOC_HOOKS */ |
| 338 | |
| 339 | /*==========================================================================*/ |
| 340 | |
| 341 | /* malloc */ |
| 342 | |
| 343 | /* |
| 344 | * The basic blocks are ordered by decreasing execution frequency, |
| 345 | * which minimizes the number of jumps in the most common cases, |
| 346 | * improves branching prediction and instruction scheduling (small |
| 347 | * block allocations typically result in a couple of instructions). |
| 348 | * Unless the optimizer reorders everything, being too smart... |
| 349 | */ |
| 350 | |
| 351 | void * |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 352 | _PyMalloc_Malloc(size_t nbytes) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 353 | { |
| 354 | block *bp; |
| 355 | poolp pool; |
| 356 | poolp next; |
| 357 | uint size; |
| 358 | |
| 359 | #ifdef WITH_MALLOC_HOOKS |
| 360 | if (malloc_hook != NULL) |
| 361 | return (*malloc_hook)(nbytes); |
| 362 | #endif |
| 363 | |
| 364 | /* |
| 365 | * This implicitly redirects malloc(0) |
| 366 | */ |
| 367 | if ((nbytes - 1) < SMALL_REQUEST_THRESHOLD) { |
| 368 | LOCK(); |
| 369 | /* |
| 370 | * Most frequent paths first |
| 371 | */ |
| 372 | size = (uint )(nbytes - 1) >> ALIGNMENT_SHIFT; |
| 373 | pool = usedpools[size + size]; |
| 374 | if (pool != pool->nextpool) { |
| 375 | /* |
| 376 | * There is a used pool for this size class. |
| 377 | * Pick up the head block of its free list. |
| 378 | */ |
| 379 | ++pool->ref.count; |
| 380 | bp = pool->freeblock; |
| 381 | if ((pool->freeblock = *(block **)bp) != NULL) { |
| 382 | UNLOCK(); |
| 383 | return (void *)bp; |
| 384 | } |
| 385 | /* |
| 386 | * Reached the end of the free list, try to extend it |
| 387 | */ |
| 388 | if (pool->ref.count < pool->capacity) { |
| 389 | /* |
| 390 | * There is room for another block |
| 391 | */ |
| 392 | size++; |
| 393 | size <<= ALIGNMENT_SHIFT; /* block size */ |
| 394 | pool->freeblock = (block *)pool + \ |
| 395 | POOL_OVERHEAD + \ |
| 396 | pool->ref.count * size; |
| 397 | *(block **)(pool->freeblock) = NULL; |
| 398 | UNLOCK(); |
| 399 | return (void *)bp; |
| 400 | } |
| 401 | /* |
| 402 | * Pool is full, unlink from used pools |
| 403 | */ |
| 404 | next = pool->nextpool; |
| 405 | pool = pool->prevpool; |
| 406 | next->prevpool = pool; |
| 407 | pool->nextpool = next; |
| 408 | UNLOCK(); |
| 409 | return (void *)bp; |
| 410 | } |
| 411 | /* |
| 412 | * Try to get a cached free pool |
| 413 | */ |
| 414 | pool = freepools; |
| 415 | if (pool != NULL) { |
| 416 | /* |
| 417 | * Unlink from cached pools |
| 418 | */ |
| 419 | freepools = pool->nextpool; |
| 420 | init_pool: |
| 421 | /* |
| 422 | * Frontlink to used pools |
| 423 | */ |
| 424 | next = usedpools[size + size]; /* == prev */ |
| 425 | pool->nextpool = next; |
| 426 | pool->prevpool = next; |
| 427 | next->nextpool = pool; |
| 428 | next->prevpool = pool; |
| 429 | pool->ref.count = 1; |
| 430 | if (pool->szidx == size) { |
| 431 | /* |
| 432 | * Luckily, this pool last contained blocks |
| 433 | * of the same size class, so its header |
| 434 | * and free list are already initialized. |
| 435 | */ |
| 436 | bp = pool->freeblock; |
| 437 | pool->freeblock = *(block **)bp; |
| 438 | UNLOCK(); |
| 439 | return (void *)bp; |
| 440 | } |
| 441 | /* |
| 442 | * Initialize the pool header and free list |
| 443 | * then return the first block. |
| 444 | */ |
| 445 | pool->szidx = size; |
| 446 | size++; |
| 447 | size <<= ALIGNMENT_SHIFT; /* block size */ |
| 448 | bp = (block *)pool + POOL_OVERHEAD; |
| 449 | pool->freeblock = bp + size; |
| 450 | *(block **)(pool->freeblock) = NULL; |
| 451 | pool->capacity = (POOL_SIZE - POOL_OVERHEAD) / size; |
| 452 | UNLOCK(); |
| 453 | return (void *)bp; |
| 454 | } |
| 455 | /* |
| 456 | * Allocate new pool |
| 457 | */ |
| 458 | if (watermark < ARENA_NB_POOLS) { |
| 459 | /* commit malloc(POOL_SIZE) from the current arena */ |
| 460 | commit_pool: |
| 461 | watermark++; |
| 462 | pool = (poolp )arenabase; |
| 463 | arenabase += POOL_SIZE; |
| 464 | pool->pooladdr = pool; |
| 465 | pool->magic = (uint )POOL_MAGIC; |
| 466 | pool->szidx = DUMMY_SIZE_IDX; |
| 467 | goto init_pool; |
| 468 | } |
| 469 | /* |
| 470 | * Allocate new arena |
| 471 | */ |
| 472 | #ifdef WITH_MEMORY_LIMITS |
| 473 | if (!(arenacnt < MAX_ARENAS)) { |
| 474 | UNLOCK(); |
| 475 | goto redirect; |
| 476 | } |
| 477 | #endif |
| 478 | /* |
| 479 | * With malloc, we can't avoid loosing one page address space |
| 480 | * per arena due to the required alignment on page boundaries. |
| 481 | */ |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 482 | bp = (block *)PyMem_MALLOC(ARENA_SIZE + SYSTEM_PAGE_SIZE); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 483 | if (bp == NULL) { |
| 484 | UNLOCK(); |
| 485 | goto redirect; |
| 486 | } |
| 487 | /* |
| 488 | * Keep a reference in the list of allocated arenas. We might |
| 489 | * want to release (some of) them in the future. The first |
| 490 | * word is never used, no matter whether the returned address |
| 491 | * is page-aligned or not, so we safely store a pointer in it. |
| 492 | */ |
| 493 | *(block **)bp = arenalist; |
| 494 | arenalist = bp; |
| 495 | arenacnt++; |
| 496 | watermark = 0; |
| 497 | /* Page-round up */ |
| 498 | arenabase = bp + (SYSTEM_PAGE_SIZE - |
| 499 | ((off_t )bp & SYSTEM_PAGE_SIZE_MASK)); |
| 500 | goto commit_pool; |
| 501 | } |
| 502 | |
| 503 | /* The small block allocator ends here. */ |
| 504 | |
| 505 | redirect: |
| 506 | |
| 507 | /* |
| 508 | * Redirect the original request to the underlying (libc) allocator. |
| 509 | * We jump here on bigger requests, on error in the code above (as a |
| 510 | * last chance to serve the request) or when the max memory limit |
| 511 | * has been reached. |
| 512 | */ |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 513 | return (void *)PyMem_MALLOC(nbytes); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /* free */ |
| 517 | |
| 518 | void |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 519 | _PyMalloc_Free(void *p) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 520 | { |
| 521 | poolp pool; |
| 522 | poolp next, prev; |
| 523 | uint size; |
| 524 | off_t offset; |
| 525 | |
| 526 | #ifdef WITH_MALLOC_HOOKS |
| 527 | if (free_hook != NULL) { |
| 528 | (*free_hook)(p); |
| 529 | return; |
| 530 | } |
| 531 | #endif |
| 532 | |
| 533 | if (p == NULL) /* free(NULL) has no effect */ |
| 534 | return; |
| 535 | |
| 536 | offset = (off_t )p & POOL_SIZE_MASK; |
| 537 | pool = (poolp )((block *)p - offset); |
| 538 | if (pool->pooladdr != pool || pool->magic != (uint )POOL_MAGIC) { |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 539 | PyMem_FREE(p); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 540 | return; |
| 541 | } |
| 542 | |
| 543 | LOCK(); |
| 544 | /* |
| 545 | * At this point, the pool is not empty |
| 546 | */ |
| 547 | if ((*(block **)p = pool->freeblock) == NULL) { |
| 548 | /* |
| 549 | * Pool was full |
| 550 | */ |
| 551 | pool->freeblock = (block *)p; |
| 552 | --pool->ref.count; |
| 553 | /* |
| 554 | * Frontlink to used pools |
| 555 | * This mimics LRU pool usage for new allocations and |
| 556 | * targets optimal filling when several pools contain |
| 557 | * blocks of the same size class. |
| 558 | */ |
| 559 | size = pool->szidx; |
| 560 | next = usedpools[size + size]; |
| 561 | prev = next->prevpool; |
| 562 | pool->nextpool = next; |
| 563 | pool->prevpool = prev; |
| 564 | next->prevpool = pool; |
| 565 | prev->nextpool = pool; |
| 566 | UNLOCK(); |
| 567 | return; |
| 568 | } |
| 569 | /* |
| 570 | * Pool was not full |
| 571 | */ |
| 572 | pool->freeblock = (block *)p; |
| 573 | if (--pool->ref.count != 0) { |
| 574 | UNLOCK(); |
| 575 | return; |
| 576 | } |
| 577 | /* |
| 578 | * Pool is now empty, unlink from used pools |
| 579 | */ |
| 580 | next = pool->nextpool; |
| 581 | prev = pool->prevpool; |
| 582 | next->prevpool = prev; |
| 583 | prev->nextpool = next; |
| 584 | /* |
| 585 | * Frontlink to free pools |
| 586 | * This ensures that previously freed pools will be allocated |
| 587 | * later (being not referenced, they are perhaps paged out). |
| 588 | */ |
| 589 | pool->nextpool = freepools; |
| 590 | freepools = pool; |
| 591 | UNLOCK(); |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | /* realloc */ |
| 596 | |
| 597 | void * |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 598 | _PyMalloc_Realloc(void *p, size_t nbytes) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 599 | { |
| 600 | block *bp; |
| 601 | poolp pool; |
| 602 | uint size; |
| 603 | |
| 604 | #ifdef WITH_MALLOC_HOOKS |
| 605 | if (realloc_hook != NULL) |
| 606 | return (*realloc_hook)(p, nbytes); |
| 607 | #endif |
| 608 | |
| 609 | if (p == NULL) |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 610 | return _PyMalloc_Malloc(nbytes); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 611 | |
| 612 | /* realloc(p, 0) on big blocks is redirected. */ |
| 613 | pool = (poolp )((block *)p - ((off_t )p & POOL_SIZE_MASK)); |
| 614 | if (pool->pooladdr != pool || pool->magic != (uint )POOL_MAGIC) { |
| 615 | /* We haven't allocated this block */ |
| 616 | if (!(nbytes > SMALL_REQUEST_THRESHOLD) && nbytes) { |
| 617 | /* small request */ |
| 618 | size = nbytes; |
| 619 | goto malloc_copy_free; |
| 620 | } |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 621 | bp = (block *)PyMem_REALLOC(p, nbytes); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 622 | } |
| 623 | else { |
| 624 | /* We're in charge of this block */ |
| 625 | size = (pool->szidx + 1) << ALIGNMENT_SHIFT; /* block size */ |
| 626 | if (size >= nbytes) { |
| 627 | /* Don't bother if a smaller size was requested |
| 628 | except for realloc(p, 0) == free(p), ret NULL */ |
| 629 | if (nbytes == 0) { |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 630 | _PyMalloc_Free(p); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 631 | bp = NULL; |
| 632 | } |
| 633 | else |
| 634 | bp = (block *)p; |
| 635 | } |
| 636 | else { |
| 637 | |
| 638 | malloc_copy_free: |
| 639 | |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 640 | bp = (block *)_PyMalloc_Malloc(nbytes); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 641 | if (bp != NULL) { |
| 642 | memcpy(bp, p, size); |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 643 | _PyMalloc_Free(p); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | } |
| 647 | return (void *)bp; |
| 648 | } |
| 649 | |
| 650 | /* calloc */ |
| 651 | |
| 652 | /* -- unused -- |
| 653 | void * |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 654 | _PyMalloc_Calloc(size_t nbel, size_t elsz) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 655 | { |
| 656 | void *p; |
| 657 | size_t nbytes; |
| 658 | |
| 659 | #ifdef WITH_MALLOC_HOOKS |
| 660 | if (calloc_hook != NULL) |
| 661 | return (*calloc_hook)(nbel, elsz); |
| 662 | #endif |
| 663 | |
| 664 | nbytes = nbel * elsz; |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 665 | p = _PyMalloc_Malloc(nbytes); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 666 | if (p != NULL) |
| 667 | memset(p, 0, nbytes); |
| 668 | return p; |
| 669 | } |
| 670 | */ |
| 671 | |
| 672 | /*==========================================================================*/ |
| 673 | |
| 674 | /* |
| 675 | * Hooks |
| 676 | */ |
| 677 | |
| 678 | #ifdef WITH_MALLOC_HOOKS |
| 679 | |
| 680 | void |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 681 | _PyMalloc_SetHooks( void *(*malloc_func)(size_t), |
| 682 | void *(*calloc_func)(size_t, size_t), |
| 683 | void *(*realloc_func)(void *, size_t), |
| 684 | void (*free_func)(void *) ) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 685 | { |
| 686 | LOCK(); |
| 687 | malloc_hook = malloc_func; |
| 688 | calloc_hook = calloc_func; |
| 689 | realloc_hook = realloc_func; |
| 690 | free_hook = free_func; |
| 691 | UNLOCK(); |
| 692 | } |
| 693 | |
| 694 | void |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 695 | _PyMalloc_FetchHooks( void *(**malloc_funcp)(size_t), |
| 696 | void *(**calloc_funcp)(size_t, size_t), |
| 697 | void *(**realloc_funcp)(void *, size_t), |
| 698 | void (**free_funcp)(void *) ) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 699 | { |
| 700 | LOCK(); |
| 701 | *malloc_funcp = malloc_hook; |
| 702 | *calloc_funcp = calloc_hook; |
| 703 | *realloc_funcp = realloc_hook; |
| 704 | *free_funcp = free_hook; |
| 705 | UNLOCK(); |
| 706 | } |
| 707 | #endif /* !WITH_MALLOC_HOOKS */ |