|
||||
File indexing completed on 2025-01-30 10:18:09
0001 #ifndef Py_INTERNAL_OBMALLOC_H 0002 #define Py_INTERNAL_OBMALLOC_H 0003 #ifdef __cplusplus 0004 extern "C" { 0005 #endif 0006 0007 #ifndef Py_BUILD_CORE 0008 # error "this header requires Py_BUILD_CORE define" 0009 #endif 0010 0011 0012 typedef unsigned int pymem_uint; /* assuming >= 16 bits */ 0013 0014 #undef uint 0015 #define uint pymem_uint 0016 0017 0018 /* An object allocator for Python. 0019 0020 Here is an introduction to the layers of the Python memory architecture, 0021 showing where the object allocator is actually used (layer +2), It is 0022 called for every object allocation and deallocation (PyObject_New/Del), 0023 unless the object-specific allocators implement a proprietary allocation 0024 scheme (ex.: ints use a simple free list). This is also the place where 0025 the cyclic garbage collector operates selectively on container objects. 0026 0027 0028 Object-specific allocators 0029 _____ ______ ______ ________ 0030 [ int ] [ dict ] [ list ] ... [ string ] Python core | 0031 +3 | <----- Object-specific memory -----> | <-- Non-object memory --> | 0032 _______________________________ | | 0033 [ Python's object allocator ] | | 0034 +2 | ####### Object memory ####### | <------ Internal buffers ------> | 0035 ______________________________________________________________ | 0036 [ Python's raw memory allocator (PyMem_ API) ] | 0037 +1 | <----- Python memory (under PyMem manager's control) ------> | | 0038 __________________________________________________________________ 0039 [ Underlying general-purpose allocator (ex: C library malloc) ] 0040 0 | <------ Virtual memory allocated for the python process -------> | 0041 0042 ========================================================================= 0043 _______________________________________________________________________ 0044 [ OS-specific Virtual Memory Manager (VMM) ] 0045 -1 | <--- Kernel dynamic storage allocation & management (page-based) ---> | 0046 __________________________________ __________________________________ 0047 [ ] [ ] 0048 -2 | <-- Physical memory: ROM/RAM --> | | <-- Secondary storage (swap) --> | 0049 0050 */ 0051 /*==========================================================================*/ 0052 0053 /* A fast, special-purpose memory allocator for small blocks, to be used 0054 on top of a general-purpose malloc -- heavily based on previous art. */ 0055 0056 /* Vladimir Marangozov -- August 2000 */ 0057 0058 /* 0059 * "Memory management is where the rubber meets the road -- if we do the wrong 0060 * thing at any level, the results will not be good. And if we don't make the 0061 * levels work well together, we are in serious trouble." (1) 0062 * 0063 * (1) Paul R. Wilson, Mark S. Johnstone, Michael Neely, and David Boles, 0064 * "Dynamic Storage Allocation: A Survey and Critical Review", 0065 * in Proc. 1995 Int'l. Workshop on Memory Management, September 1995. 0066 */ 0067 0068 /* #undef WITH_MEMORY_LIMITS */ /* disable mem limit checks */ 0069 0070 /*==========================================================================*/ 0071 0072 /* 0073 * Allocation strategy abstract: 0074 * 0075 * For small requests, the allocator sub-allocates <Big> blocks of memory. 0076 * Requests greater than SMALL_REQUEST_THRESHOLD bytes are routed to the 0077 * system's allocator. 0078 * 0079 * Small requests are grouped in size classes spaced 8 bytes apart, due 0080 * to the required valid alignment of the returned address. Requests of 0081 * a particular size are serviced from memory pools of 4K (one VMM page). 0082 * Pools are fragmented on demand and contain free lists of blocks of one 0083 * particular size class. In other words, there is a fixed-size allocator 0084 * for each size class. Free pools are shared by the different allocators 0085 * thus minimizing the space reserved for a particular size class. 0086 * 0087 * This allocation strategy is a variant of what is known as "simple 0088 * segregated storage based on array of free lists". The main drawback of 0089 * simple segregated storage is that we might end up with lot of reserved 0090 * memory for the different free lists, which degenerate in time. To avoid 0091 * this, we partition each free list in pools and we share dynamically the 0092 * reserved space between all free lists. This technique is quite efficient 0093 * for memory intensive programs which allocate mainly small-sized blocks. 0094 * 0095 * For small requests we have the following table: 0096 * 0097 * Request in bytes Size of allocated block Size class idx 0098 * ---------------------------------------------------------------- 0099 * 1-8 8 0 0100 * 9-16 16 1 0101 * 17-24 24 2 0102 * 25-32 32 3 0103 * 33-40 40 4 0104 * 41-48 48 5 0105 * 49-56 56 6 0106 * 57-64 64 7 0107 * 65-72 72 8 0108 * ... ... ... 0109 * 497-504 504 62 0110 * 505-512 512 63 0111 * 0112 * 0, SMALL_REQUEST_THRESHOLD + 1 and up: routed to the underlying 0113 * allocator. 0114 */ 0115 0116 /*==========================================================================*/ 0117 0118 /* 0119 * -- Main tunable settings section -- 0120 */ 0121 0122 /* 0123 * Alignment of addresses returned to the user. 8-bytes alignment works 0124 * on most current architectures (with 32-bit or 64-bit address buses). 0125 * The alignment value is also used for grouping small requests in size 0126 * classes spaced ALIGNMENT bytes apart. 0127 * 0128 * You shouldn't change this unless you know what you are doing. 0129 */ 0130 0131 #if SIZEOF_VOID_P > 4 0132 #define ALIGNMENT 16 /* must be 2^N */ 0133 #define ALIGNMENT_SHIFT 4 0134 #else 0135 #define ALIGNMENT 8 /* must be 2^N */ 0136 #define ALIGNMENT_SHIFT 3 0137 #endif 0138 0139 /* Return the number of bytes in size class I, as a uint. */ 0140 #define INDEX2SIZE(I) (((pymem_uint)(I) + 1) << ALIGNMENT_SHIFT) 0141 0142 /* 0143 * Max size threshold below which malloc requests are considered to be 0144 * small enough in order to use preallocated memory pools. You can tune 0145 * this value according to your application behaviour and memory needs. 0146 * 0147 * Note: a size threshold of 512 guarantees that newly created dictionaries 0148 * will be allocated from preallocated memory pools on 64-bit. 0149 * 0150 * The following invariants must hold: 0151 * 1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 512 0152 * 2) SMALL_REQUEST_THRESHOLD is evenly divisible by ALIGNMENT 0153 * 0154 * Although not required, for better performance and space efficiency, 0155 * it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2. 0156 */ 0157 #define SMALL_REQUEST_THRESHOLD 512 0158 #define NB_SMALL_SIZE_CLASSES (SMALL_REQUEST_THRESHOLD / ALIGNMENT) 0159 0160 /* 0161 * The system's VMM page size can be obtained on most unices with a 0162 * getpagesize() call or deduced from various header files. To make 0163 * things simpler, we assume that it is 4K, which is OK for most systems. 0164 * It is probably better if this is the native page size, but it doesn't 0165 * have to be. In theory, if SYSTEM_PAGE_SIZE is larger than the native page 0166 * size, then `POOL_ADDR(p)->arenaindex' could rarely cause a segmentation 0167 * violation fault. 4K is apparently OK for all the platforms that python 0168 * currently targets. 0169 */ 0170 #define SYSTEM_PAGE_SIZE (4 * 1024) 0171 0172 /* 0173 * Maximum amount of memory managed by the allocator for small requests. 0174 */ 0175 #ifdef WITH_MEMORY_LIMITS 0176 #ifndef SMALL_MEMORY_LIMIT 0177 #define SMALL_MEMORY_LIMIT (64 * 1024 * 1024) /* 64 MB -- more? */ 0178 #endif 0179 #endif 0180 0181 #if !defined(WITH_PYMALLOC_RADIX_TREE) 0182 /* Use radix-tree to track arena memory regions, for address_in_range(). 0183 * Enable by default since it allows larger pool sizes. Can be disabled 0184 * using -DWITH_PYMALLOC_RADIX_TREE=0 */ 0185 #define WITH_PYMALLOC_RADIX_TREE 1 0186 #endif 0187 0188 #if SIZEOF_VOID_P > 4 0189 /* on 64-bit platforms use larger pools and arenas if we can */ 0190 #define USE_LARGE_ARENAS 0191 #if WITH_PYMALLOC_RADIX_TREE 0192 /* large pools only supported if radix-tree is enabled */ 0193 #define USE_LARGE_POOLS 0194 #endif 0195 #endif 0196 0197 /* 0198 * The allocator sub-allocates <Big> blocks of memory (called arenas) aligned 0199 * on a page boundary. This is a reserved virtual address space for the 0200 * current process (obtained through a malloc()/mmap() call). In no way this 0201 * means that the memory arenas will be used entirely. A malloc(<Big>) is 0202 * usually an address range reservation for <Big> bytes, unless all pages within 0203 * this space are referenced subsequently. So malloc'ing big blocks and not 0204 * using them does not mean "wasting memory". It's an addressable range 0205 * wastage... 0206 * 0207 * Arenas are allocated with mmap() on systems supporting anonymous memory 0208 * mappings to reduce heap fragmentation. 0209 */ 0210 #ifdef USE_LARGE_ARENAS 0211 #define ARENA_BITS 20 /* 1 MiB */ 0212 #else 0213 #define ARENA_BITS 18 /* 256 KiB */ 0214 #endif 0215 #define ARENA_SIZE (1 << ARENA_BITS) 0216 #define ARENA_SIZE_MASK (ARENA_SIZE - 1) 0217 0218 #ifdef WITH_MEMORY_LIMITS 0219 #define MAX_ARENAS (SMALL_MEMORY_LIMIT / ARENA_SIZE) 0220 #endif 0221 0222 /* 0223 * Size of the pools used for small blocks. Must be a power of 2. 0224 */ 0225 #ifdef USE_LARGE_POOLS 0226 #define POOL_BITS 14 /* 16 KiB */ 0227 #else 0228 #define POOL_BITS 12 /* 4 KiB */ 0229 #endif 0230 #define POOL_SIZE (1 << POOL_BITS) 0231 #define POOL_SIZE_MASK (POOL_SIZE - 1) 0232 0233 #if !WITH_PYMALLOC_RADIX_TREE 0234 #if POOL_SIZE != SYSTEM_PAGE_SIZE 0235 # error "pool size must be equal to system page size" 0236 #endif 0237 #endif 0238 0239 #define MAX_POOLS_IN_ARENA (ARENA_SIZE / POOL_SIZE) 0240 #if MAX_POOLS_IN_ARENA * POOL_SIZE != ARENA_SIZE 0241 # error "arena size not an exact multiple of pool size" 0242 #endif 0243 0244 /* 0245 * -- End of tunable settings section -- 0246 */ 0247 0248 /*==========================================================================*/ 0249 0250 /* When you say memory, my mind reasons in terms of (pointers to) blocks */ 0251 typedef uint8_t pymem_block; 0252 0253 /* Pool for small blocks. */ 0254 struct pool_header { 0255 union { pymem_block *_padding; 0256 uint count; } ref; /* number of allocated blocks */ 0257 pymem_block *freeblock; /* pool's free list head */ 0258 struct pool_header *nextpool; /* next pool of this size class */ 0259 struct pool_header *prevpool; /* previous pool "" */ 0260 uint arenaindex; /* index into arenas of base adr */ 0261 uint szidx; /* block size class index */ 0262 uint nextoffset; /* bytes to virgin block */ 0263 uint maxnextoffset; /* largest valid nextoffset */ 0264 }; 0265 0266 typedef struct pool_header *poolp; 0267 0268 /* Record keeping for arenas. */ 0269 struct arena_object { 0270 /* The address of the arena, as returned by malloc. Note that 0 0271 * will never be returned by a successful malloc, and is used 0272 * here to mark an arena_object that doesn't correspond to an 0273 * allocated arena. 0274 */ 0275 uintptr_t address; 0276 0277 /* Pool-aligned pointer to the next pool to be carved off. */ 0278 pymem_block* pool_address; 0279 0280 /* The number of available pools in the arena: free pools + never- 0281 * allocated pools. 0282 */ 0283 uint nfreepools; 0284 0285 /* The total number of pools in the arena, whether or not available. */ 0286 uint ntotalpools; 0287 0288 /* Singly-linked list of available pools. */ 0289 struct pool_header* freepools; 0290 0291 /* Whenever this arena_object is not associated with an allocated 0292 * arena, the nextarena member is used to link all unassociated 0293 * arena_objects in the singly-linked `unused_arena_objects` list. 0294 * The prevarena member is unused in this case. 0295 * 0296 * When this arena_object is associated with an allocated arena 0297 * with at least one available pool, both members are used in the 0298 * doubly-linked `usable_arenas` list, which is maintained in 0299 * increasing order of `nfreepools` values. 0300 * 0301 * Else this arena_object is associated with an allocated arena 0302 * all of whose pools are in use. `nextarena` and `prevarena` 0303 * are both meaningless in this case. 0304 */ 0305 struct arena_object* nextarena; 0306 struct arena_object* prevarena; 0307 }; 0308 0309 #define POOL_OVERHEAD _Py_SIZE_ROUND_UP(sizeof(struct pool_header), ALIGNMENT) 0310 0311 #define DUMMY_SIZE_IDX 0xffff /* size class of newly cached pools */ 0312 0313 /* Round pointer P down to the closest pool-aligned address <= P, as a poolp */ 0314 #define POOL_ADDR(P) ((poolp)_Py_ALIGN_DOWN((P), POOL_SIZE)) 0315 0316 /* Return total number of blocks in pool of size index I, as a uint. */ 0317 #define NUMBLOCKS(I) ((pymem_uint)(POOL_SIZE - POOL_OVERHEAD) / INDEX2SIZE(I)) 0318 0319 /*==========================================================================*/ 0320 0321 /* 0322 * Pool table -- headed, circular, doubly-linked lists of partially used pools. 0323 0324 This is involved. For an index i, usedpools[i+i] is the header for a list of 0325 all partially used pools holding small blocks with "size class idx" i. So 0326 usedpools[0] corresponds to blocks of size 8, usedpools[2] to blocks of size 0327 16, and so on: index 2*i <-> blocks of size (i+1)<<ALIGNMENT_SHIFT. 0328 0329 Pools are carved off an arena's highwater mark (an arena_object's pool_address 0330 member) as needed. Once carved off, a pool is in one of three states forever 0331 after: 0332 0333 used == partially used, neither empty nor full 0334 At least one block in the pool is currently allocated, and at least one 0335 block in the pool is not currently allocated (note this implies a pool 0336 has room for at least two blocks). 0337 This is a pool's initial state, as a pool is created only when malloc 0338 needs space. 0339 The pool holds blocks of a fixed size, and is in the circular list headed 0340 at usedpools[i] (see above). It's linked to the other used pools of the 0341 same size class via the pool_header's nextpool and prevpool members. 0342 If all but one block is currently allocated, a malloc can cause a 0343 transition to the full state. If all but one block is not currently 0344 allocated, a free can cause a transition to the empty state. 0345 0346 full == all the pool's blocks are currently allocated 0347 On transition to full, a pool is unlinked from its usedpools[] list. 0348 It's not linked to from anything then anymore, and its nextpool and 0349 prevpool members are meaningless until it transitions back to used. 0350 A free of a block in a full pool puts the pool back in the used state. 0351 Then it's linked in at the front of the appropriate usedpools[] list, so 0352 that the next allocation for its size class will reuse the freed block. 0353 0354 empty == all the pool's blocks are currently available for allocation 0355 On transition to empty, a pool is unlinked from its usedpools[] list, 0356 and linked to the front of its arena_object's singly-linked freepools list, 0357 via its nextpool member. The prevpool member has no meaning in this case. 0358 Empty pools have no inherent size class: the next time a malloc finds 0359 an empty list in usedpools[], it takes the first pool off of freepools. 0360 If the size class needed happens to be the same as the size class the pool 0361 last had, some pool initialization can be skipped. 0362 0363 0364 Block Management 0365 0366 Blocks within pools are again carved out as needed. pool->freeblock points to 0367 the start of a singly-linked list of free blocks within the pool. When a 0368 block is freed, it's inserted at the front of its pool's freeblock list. Note 0369 that the available blocks in a pool are *not* linked all together when a pool 0370 is initialized. Instead only "the first two" (lowest addresses) blocks are 0371 set up, returning the first such block, and setting pool->freeblock to a 0372 one-block list holding the second such block. This is consistent with that 0373 pymalloc strives at all levels (arena, pool, and block) never to touch a piece 0374 of memory until it's actually needed. 0375 0376 So long as a pool is in the used state, we're certain there *is* a block 0377 available for allocating, and pool->freeblock is not NULL. If pool->freeblock 0378 points to the end of the free list before we've carved the entire pool into 0379 blocks, that means we simply haven't yet gotten to one of the higher-address 0380 blocks. The offset from the pool_header to the start of "the next" virgin 0381 block is stored in the pool_header nextoffset member, and the largest value 0382 of nextoffset that makes sense is stored in the maxnextoffset member when a 0383 pool is initialized. All the blocks in a pool have been passed out at least 0384 once when and only when nextoffset > maxnextoffset. 0385 0386 0387 Major obscurity: While the usedpools vector is declared to have poolp 0388 entries, it doesn't really. It really contains two pointers per (conceptual) 0389 poolp entry, the nextpool and prevpool members of a pool_header. The 0390 excruciating initialization code below fools C so that 0391 0392 usedpool[i+i] 0393 0394 "acts like" a genuine poolp, but only so long as you only reference its 0395 nextpool and prevpool members. The "- 2*sizeof(pymem_block *)" gibberish is 0396 compensating for that a pool_header's nextpool and prevpool members 0397 immediately follow a pool_header's first two members: 0398 0399 union { pymem_block *_padding; 0400 uint count; } ref; 0401 pymem_block *freeblock; 0402 0403 each of which consume sizeof(pymem_block *) bytes. So what usedpools[i+i] really 0404 contains is a fudged-up pointer p such that *if* C believes it's a poolp 0405 pointer, then p->nextpool and p->prevpool are both p (meaning that the headed 0406 circular list is empty). 0407 0408 It's unclear why the usedpools setup is so convoluted. It could be to 0409 minimize the amount of cache required to hold this heavily-referenced table 0410 (which only *needs* the two interpool pointer members of a pool_header). OTOH, 0411 referencing code has to remember to "double the index" and doing so isn't 0412 free, usedpools[0] isn't a strictly legal pointer, and we're crucially relying 0413 on that C doesn't insert any padding anywhere in a pool_header at or before 0414 the prevpool member. 0415 **************************************************************************** */ 0416 0417 #define OBMALLOC_USED_POOLS_SIZE (2 * ((NB_SMALL_SIZE_CLASSES + 7) / 8) * 8) 0418 0419 struct _obmalloc_pools { 0420 poolp used[OBMALLOC_USED_POOLS_SIZE]; 0421 }; 0422 0423 0424 /*========================================================================== 0425 Arena management. 0426 0427 `arenas` is a vector of arena_objects. It contains maxarenas entries, some of 0428 which may not be currently used (== they're arena_objects that aren't 0429 currently associated with an allocated arena). Note that arenas proper are 0430 separately malloc'ed. 0431 0432 Prior to Python 2.5, arenas were never free()'ed. Starting with Python 2.5, 0433 we do try to free() arenas, and use some mild heuristic strategies to increase 0434 the likelihood that arenas eventually can be freed. 0435 0436 unused_arena_objects 0437 0438 This is a singly-linked list of the arena_objects that are currently not 0439 being used (no arena is associated with them). Objects are taken off the 0440 head of the list in new_arena(), and are pushed on the head of the list in 0441 PyObject_Free() when the arena is empty. Key invariant: an arena_object 0442 is on this list if and only if its .address member is 0. 0443 0444 usable_arenas 0445 0446 This is a doubly-linked list of the arena_objects associated with arenas 0447 that have pools available. These pools are either waiting to be reused, 0448 or have not been used before. The list is sorted to have the most- 0449 allocated arenas first (ascending order based on the nfreepools member). 0450 This means that the next allocation will come from a heavily used arena, 0451 which gives the nearly empty arenas a chance to be returned to the system. 0452 In my unscientific tests this dramatically improved the number of arenas 0453 that could be freed. 0454 0455 Note that an arena_object associated with an arena all of whose pools are 0456 currently in use isn't on either list. 0457 0458 Changed in Python 3.8: keeping usable_arenas sorted by number of free pools 0459 used to be done by one-at-a-time linear search when an arena's number of 0460 free pools changed. That could, overall, consume time quadratic in the 0461 number of arenas. That didn't really matter when there were only a few 0462 hundred arenas (typical!), but could be a timing disaster when there were 0463 hundreds of thousands. See bpo-37029. 0464 0465 Now we have a vector of "search fingers" to eliminate the need to search: 0466 nfp2lasta[nfp] returns the last ("rightmost") arena in usable_arenas 0467 with nfp free pools. This is NULL if and only if there is no arena with 0468 nfp free pools in usable_arenas. 0469 */ 0470 0471 /* How many arena_objects do we initially allocate? 0472 * 16 = can allocate 16 arenas = 16 * ARENA_SIZE = 4MB before growing the 0473 * `arenas` vector. 0474 */ 0475 #define INITIAL_ARENA_OBJECTS 16 0476 0477 struct _obmalloc_mgmt { 0478 /* Array of objects used to track chunks of memory (arenas). */ 0479 struct arena_object* arenas; 0480 /* Number of slots currently allocated in the `arenas` vector. */ 0481 uint maxarenas; 0482 0483 /* The head of the singly-linked, NULL-terminated list of available 0484 * arena_objects. 0485 */ 0486 struct arena_object* unused_arena_objects; 0487 0488 /* The head of the doubly-linked, NULL-terminated at each end, list of 0489 * arena_objects associated with arenas that have pools available. 0490 */ 0491 struct arena_object* usable_arenas; 0492 0493 /* nfp2lasta[nfp] is the last arena in usable_arenas with nfp free pools */ 0494 struct arena_object* nfp2lasta[MAX_POOLS_IN_ARENA + 1]; 0495 0496 /* Number of arenas allocated that haven't been free()'d. */ 0497 size_t narenas_currently_allocated; 0498 0499 /* Total number of times malloc() called to allocate an arena. */ 0500 size_t ntimes_arena_allocated; 0501 /* High water mark (max value ever seen) for narenas_currently_allocated. */ 0502 size_t narenas_highwater; 0503 0504 Py_ssize_t raw_allocated_blocks; 0505 }; 0506 0507 0508 #if WITH_PYMALLOC_RADIX_TREE 0509 /*==========================================================================*/ 0510 /* radix tree for tracking arena usage. If enabled, used to implement 0511 address_in_range(). 0512 0513 memory address bit allocation for keys 0514 0515 64-bit pointers, IGNORE_BITS=0 and 2^20 arena size: 0516 15 -> MAP_TOP_BITS 0517 15 -> MAP_MID_BITS 0518 14 -> MAP_BOT_BITS 0519 20 -> ideal aligned arena 0520 ---- 0521 64 0522 0523 64-bit pointers, IGNORE_BITS=16, and 2^20 arena size: 0524 16 -> IGNORE_BITS 0525 10 -> MAP_TOP_BITS 0526 10 -> MAP_MID_BITS 0527 8 -> MAP_BOT_BITS 0528 20 -> ideal aligned arena 0529 ---- 0530 64 0531 0532 32-bit pointers and 2^18 arena size: 0533 14 -> MAP_BOT_BITS 0534 18 -> ideal aligned arena 0535 ---- 0536 32 0537 0538 */ 0539 0540 #if SIZEOF_VOID_P == 8 0541 0542 /* number of bits in a pointer */ 0543 #define POINTER_BITS 64 0544 0545 /* High bits of memory addresses that will be ignored when indexing into the 0546 * radix tree. Setting this to zero is the safe default. For most 64-bit 0547 * machines, setting this to 16 would be safe. The kernel would not give 0548 * user-space virtual memory addresses that have significant information in 0549 * those high bits. The main advantage to setting IGNORE_BITS > 0 is that less 0550 * virtual memory will be used for the top and middle radix tree arrays. Those 0551 * arrays are allocated in the BSS segment and so will typically consume real 0552 * memory only if actually accessed. 0553 */ 0554 #define IGNORE_BITS 0 0555 0556 /* use the top and mid layers of the radix tree */ 0557 #define USE_INTERIOR_NODES 0558 0559 #elif SIZEOF_VOID_P == 4 0560 0561 #define POINTER_BITS 32 0562 #define IGNORE_BITS 0 0563 0564 #else 0565 0566 /* Currently this code works for 64-bit or 32-bit pointers only. */ 0567 #error "obmalloc radix tree requires 64-bit or 32-bit pointers." 0568 0569 #endif /* SIZEOF_VOID_P */ 0570 0571 /* arena_coverage_t members require this to be true */ 0572 #if ARENA_BITS >= 32 0573 # error "arena size must be < 2^32" 0574 #endif 0575 0576 /* the lower bits of the address that are not ignored */ 0577 #define ADDRESS_BITS (POINTER_BITS - IGNORE_BITS) 0578 0579 #ifdef USE_INTERIOR_NODES 0580 /* number of bits used for MAP_TOP and MAP_MID nodes */ 0581 #define INTERIOR_BITS ((ADDRESS_BITS - ARENA_BITS + 2) / 3) 0582 #else 0583 #define INTERIOR_BITS 0 0584 #endif 0585 0586 #define MAP_TOP_BITS INTERIOR_BITS 0587 #define MAP_TOP_LENGTH (1 << MAP_TOP_BITS) 0588 #define MAP_TOP_MASK (MAP_TOP_LENGTH - 1) 0589 0590 #define MAP_MID_BITS INTERIOR_BITS 0591 #define MAP_MID_LENGTH (1 << MAP_MID_BITS) 0592 #define MAP_MID_MASK (MAP_MID_LENGTH - 1) 0593 0594 #define MAP_BOT_BITS (ADDRESS_BITS - ARENA_BITS - 2*INTERIOR_BITS) 0595 #define MAP_BOT_LENGTH (1 << MAP_BOT_BITS) 0596 #define MAP_BOT_MASK (MAP_BOT_LENGTH - 1) 0597 0598 #define MAP_BOT_SHIFT ARENA_BITS 0599 #define MAP_MID_SHIFT (MAP_BOT_BITS + MAP_BOT_SHIFT) 0600 #define MAP_TOP_SHIFT (MAP_MID_BITS + MAP_MID_SHIFT) 0601 0602 #define AS_UINT(p) ((uintptr_t)(p)) 0603 #define MAP_BOT_INDEX(p) ((AS_UINT(p) >> MAP_BOT_SHIFT) & MAP_BOT_MASK) 0604 #define MAP_MID_INDEX(p) ((AS_UINT(p) >> MAP_MID_SHIFT) & MAP_MID_MASK) 0605 #define MAP_TOP_INDEX(p) ((AS_UINT(p) >> MAP_TOP_SHIFT) & MAP_TOP_MASK) 0606 0607 #if IGNORE_BITS > 0 0608 /* Return the ignored part of the pointer address. Those bits should be same 0609 * for all valid pointers if IGNORE_BITS is set correctly. 0610 */ 0611 #define HIGH_BITS(p) (AS_UINT(p) >> ADDRESS_BITS) 0612 #else 0613 #define HIGH_BITS(p) 0 0614 #endif 0615 0616 0617 /* This is the leaf of the radix tree. See arena_map_mark_used() for the 0618 * meaning of these members. */ 0619 typedef struct { 0620 int32_t tail_hi; 0621 int32_t tail_lo; 0622 } arena_coverage_t; 0623 0624 typedef struct arena_map_bot { 0625 /* The members tail_hi and tail_lo are accessed together. So, it 0626 * better to have them as an array of structs, rather than two 0627 * arrays. 0628 */ 0629 arena_coverage_t arenas[MAP_BOT_LENGTH]; 0630 } arena_map_bot_t; 0631 0632 #ifdef USE_INTERIOR_NODES 0633 typedef struct arena_map_mid { 0634 struct arena_map_bot *ptrs[MAP_MID_LENGTH]; 0635 } arena_map_mid_t; 0636 0637 typedef struct arena_map_top { 0638 struct arena_map_mid *ptrs[MAP_TOP_LENGTH]; 0639 } arena_map_top_t; 0640 #endif 0641 0642 struct _obmalloc_usage { 0643 /* The root of radix tree. Note that by initializing like this, the memory 0644 * should be in the BSS. The OS will only memory map pages as the MAP_MID 0645 * nodes get used (OS pages are demand loaded as needed). 0646 */ 0647 #ifdef USE_INTERIOR_NODES 0648 arena_map_top_t arena_map_root; 0649 /* accounting for number of used interior nodes */ 0650 int arena_map_mid_count; 0651 int arena_map_bot_count; 0652 #else 0653 arena_map_bot_t arena_map_root; 0654 #endif 0655 }; 0656 0657 #endif /* WITH_PYMALLOC_RADIX_TREE */ 0658 0659 0660 struct _obmalloc_global_state { 0661 int dump_debug_stats; 0662 Py_ssize_t interpreter_leaks; 0663 }; 0664 0665 struct _obmalloc_state { 0666 struct _obmalloc_pools pools; 0667 struct _obmalloc_mgmt mgmt; 0668 #if WITH_PYMALLOC_RADIX_TREE 0669 struct _obmalloc_usage usage; 0670 #endif 0671 }; 0672 0673 0674 #undef uint 0675 0676 0677 /* Allocate memory directly from the O/S virtual memory system, 0678 * where supported. Otherwise fallback on malloc */ 0679 void *_PyObject_VirtualAlloc(size_t size); 0680 void _PyObject_VirtualFree(void *, size_t size); 0681 0682 0683 /* This function returns the number of allocated memory blocks, regardless of size */ 0684 extern Py_ssize_t _Py_GetGlobalAllocatedBlocks(void); 0685 #define _Py_GetAllocatedBlocks() \ 0686 _Py_GetGlobalAllocatedBlocks() 0687 extern Py_ssize_t _PyInterpreterState_GetAllocatedBlocks(PyInterpreterState *); 0688 extern void _PyInterpreterState_FinalizeAllocatedBlocks(PyInterpreterState *); 0689 0690 0691 #ifdef WITH_PYMALLOC 0692 // Export the symbol for the 3rd party guppy3 project 0693 PyAPI_FUNC(int) _PyObject_DebugMallocStats(FILE *out); 0694 #endif 0695 0696 0697 #ifdef __cplusplus 0698 } 0699 #endif 0700 #endif // !Py_INTERNAL_OBMALLOC_H
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |