Tim Peters | 1221c0a | 2002-03-23 00:20:15 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | |
| 3 | #ifdef WITH_PYMALLOC |
| 4 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 5 | /* An object allocator for Python. |
| 6 | |
| 7 | Here is an introduction to the layers of the Python memory architecture, |
| 8 | showing where the object allocator is actually used (layer +2), It is |
| 9 | called for every object allocation and deallocation (PyObject_New/Del), |
| 10 | unless the object-specific allocators implement a proprietary allocation |
| 11 | scheme (ex.: ints use a simple free list). This is also the place where |
| 12 | the cyclic garbage collector operates selectively on container objects. |
| 13 | |
| 14 | |
| 15 | Object-specific allocators |
| 16 | _____ ______ ______ ________ |
| 17 | [ int ] [ dict ] [ list ] ... [ string ] Python core | |
| 18 | +3 | <----- Object-specific memory -----> | <-- Non-object memory --> | |
| 19 | _______________________________ | | |
| 20 | [ Python's object allocator ] | | |
| 21 | +2 | ####### Object memory ####### | <------ Internal buffers ------> | |
| 22 | ______________________________________________________________ | |
| 23 | [ Python's raw memory allocator (PyMem_ API) ] | |
| 24 | +1 | <----- Python memory (under PyMem manager's control) ------> | | |
| 25 | __________________________________________________________________ |
| 26 | [ Underlying general-purpose allocator (ex: C library malloc) ] |
| 27 | 0 | <------ Virtual memory allocated for the python process -------> | |
| 28 | |
| 29 | ========================================================================= |
| 30 | _______________________________________________________________________ |
| 31 | [ OS-specific Virtual Memory Manager (VMM) ] |
| 32 | -1 | <--- Kernel dynamic storage allocation & management (page-based) ---> | |
| 33 | __________________________________ __________________________________ |
| 34 | [ ] [ ] |
| 35 | -2 | <-- Physical memory: ROM/RAM --> | | <-- Secondary storage (swap) --> | |
| 36 | |
| 37 | */ |
| 38 | /*==========================================================================*/ |
| 39 | |
| 40 | /* A fast, special-purpose memory allocator for small blocks, to be used |
| 41 | on top of a general-purpose malloc -- heavily based on previous art. */ |
| 42 | |
| 43 | /* Vladimir Marangozov -- August 2000 */ |
| 44 | |
| 45 | /* |
| 46 | * "Memory management is where the rubber meets the road -- if we do the wrong |
| 47 | * thing at any level, the results will not be good. And if we don't make the |
| 48 | * levels work well together, we are in serious trouble." (1) |
| 49 | * |
| 50 | * (1) Paul R. Wilson, Mark S. Johnstone, Michael Neely, and David Boles, |
| 51 | * "Dynamic Storage Allocation: A Survey and Critical Review", |
| 52 | * in Proc. 1995 Int'l. Workshop on Memory Management, September 1995. |
| 53 | */ |
| 54 | |
| 55 | /* #undef WITH_MEMORY_LIMITS */ /* disable mem limit checks */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 56 | |
| 57 | /*==========================================================================*/ |
| 58 | |
| 59 | /* |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 60 | * Allocation strategy abstract: |
| 61 | * |
| 62 | * For small requests, the allocator sub-allocates <Big> blocks of memory. |
| 63 | * Requests greater than 256 bytes are routed to the system's allocator. |
Tim Peters | ce7fb9b | 2002-03-23 00:28:57 +0000 | [diff] [blame] | 64 | * |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 65 | * Small requests are grouped in size classes spaced 8 bytes apart, due |
| 66 | * to the required valid alignment of the returned address. Requests of |
| 67 | * a particular size are serviced from memory pools of 4K (one VMM page). |
| 68 | * Pools are fragmented on demand and contain free lists of blocks of one |
| 69 | * particular size class. In other words, there is a fixed-size allocator |
| 70 | * for each size class. Free pools are shared by the different allocators |
| 71 | * thus minimizing the space reserved for a particular size class. |
| 72 | * |
| 73 | * This allocation strategy is a variant of what is known as "simple |
| 74 | * segregated storage based on array of free lists". The main drawback of |
| 75 | * simple segregated storage is that we might end up with lot of reserved |
| 76 | * memory for the different free lists, which degenerate in time. To avoid |
| 77 | * this, we partition each free list in pools and we share dynamically the |
| 78 | * reserved space between all free lists. This technique is quite efficient |
| 79 | * for memory intensive programs which allocate mainly small-sized blocks. |
| 80 | * |
| 81 | * For small requests we have the following table: |
| 82 | * |
| 83 | * Request in bytes Size of allocated block Size class idx |
| 84 | * ---------------------------------------------------------------- |
| 85 | * 1-8 8 0 |
| 86 | * 9-16 16 1 |
| 87 | * 17-24 24 2 |
| 88 | * 25-32 32 3 |
| 89 | * 33-40 40 4 |
| 90 | * 41-48 48 5 |
| 91 | * 49-56 56 6 |
| 92 | * 57-64 64 7 |
| 93 | * 65-72 72 8 |
| 94 | * ... ... ... |
| 95 | * 241-248 248 30 |
| 96 | * 249-256 256 31 |
Tim Peters | ce7fb9b | 2002-03-23 00:28:57 +0000 | [diff] [blame] | 97 | * |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 98 | * 0, 257 and up: routed to the underlying allocator. |
| 99 | */ |
| 100 | |
| 101 | /*==========================================================================*/ |
| 102 | |
| 103 | /* |
| 104 | * -- Main tunable settings section -- |
| 105 | */ |
| 106 | |
| 107 | /* |
| 108 | * Alignment of addresses returned to the user. 8-bytes alignment works |
| 109 | * on most current architectures (with 32-bit or 64-bit address busses). |
| 110 | * The alignment value is also used for grouping small requests in size |
| 111 | * classes spaced ALIGNMENT bytes apart. |
| 112 | * |
| 113 | * You shouldn't change this unless you know what you are doing. |
| 114 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 115 | #define ALIGNMENT 8 /* must be 2^N */ |
| 116 | #define ALIGNMENT_SHIFT 3 |
| 117 | #define ALIGNMENT_MASK (ALIGNMENT - 1) |
| 118 | |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 119 | /* Return the number of bytes in size class I, as a uint. */ |
| 120 | #define INDEX2SIZE(I) (((uint)(I) + 1) << ALIGNMENT_SHIFT) |
| 121 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 122 | /* |
| 123 | * Max size threshold below which malloc requests are considered to be |
| 124 | * small enough in order to use preallocated memory pools. You can tune |
| 125 | * this value according to your application behaviour and memory needs. |
| 126 | * |
| 127 | * The following invariants must hold: |
| 128 | * 1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 256 |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 129 | * 2) SMALL_REQUEST_THRESHOLD is evenly divisible by ALIGNMENT |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 130 | * |
| 131 | * Although not required, for better performance and space efficiency, |
| 132 | * it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2. |
| 133 | */ |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 134 | #define SMALL_REQUEST_THRESHOLD 256 |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 135 | #define NB_SMALL_SIZE_CLASSES (SMALL_REQUEST_THRESHOLD / ALIGNMENT) |
| 136 | |
| 137 | /* |
| 138 | * The system's VMM page size can be obtained on most unices with a |
| 139 | * getpagesize() call or deduced from various header files. To make |
| 140 | * things simpler, we assume that it is 4K, which is OK for most systems. |
| 141 | * It is probably better if this is the native page size, but it doesn't |
Tim Peters | ecc6e6a | 2005-07-10 22:30:55 +0000 | [diff] [blame] | 142 | * have to be. In theory, if SYSTEM_PAGE_SIZE is larger than the native page |
| 143 | * size, then `POOL_ADDR(p)->arenaindex' could rarely cause a segmentation |
| 144 | * violation fault. 4K is apparently OK for all the platforms that python |
Martin v. Löwis | 8c14028 | 2002-10-26 15:01:53 +0000 | [diff] [blame] | 145 | * currently targets. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 146 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 147 | #define SYSTEM_PAGE_SIZE (4 * 1024) |
| 148 | #define SYSTEM_PAGE_SIZE_MASK (SYSTEM_PAGE_SIZE - 1) |
| 149 | |
| 150 | /* |
| 151 | * Maximum amount of memory managed by the allocator for small requests. |
| 152 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 153 | #ifdef WITH_MEMORY_LIMITS |
| 154 | #ifndef SMALL_MEMORY_LIMIT |
| 155 | #define SMALL_MEMORY_LIMIT (64 * 1024 * 1024) /* 64 MB -- more? */ |
| 156 | #endif |
| 157 | #endif |
| 158 | |
| 159 | /* |
| 160 | * The allocator sub-allocates <Big> blocks of memory (called arenas) aligned |
| 161 | * on a page boundary. This is a reserved virtual address space for the |
| 162 | * current process (obtained through a malloc call). In no way this means |
| 163 | * that the memory arenas will be used entirely. A malloc(<Big>) is usually |
| 164 | * an address range reservation for <Big> bytes, unless all pages within this |
| 165 | * space are referenced subsequently. So malloc'ing big blocks and not using |
| 166 | * them does not mean "wasting memory". It's an addressable range wastage... |
| 167 | * |
| 168 | * Therefore, allocating arenas with malloc is not optimal, because there is |
| 169 | * some address space wastage, but this is the most portable way to request |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 170 | * memory from the system across various platforms. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 171 | */ |
Tim Peters | 3c83df2 | 2002-03-30 07:04:41 +0000 | [diff] [blame] | 172 | #define ARENA_SIZE (256 << 10) /* 256KB */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 173 | |
| 174 | #ifdef WITH_MEMORY_LIMITS |
| 175 | #define MAX_ARENAS (SMALL_MEMORY_LIMIT / ARENA_SIZE) |
| 176 | #endif |
| 177 | |
| 178 | /* |
| 179 | * Size of the pools used for small blocks. Should be a power of 2, |
Tim Peters | c2ce91a | 2002-03-30 21:36:04 +0000 | [diff] [blame] | 180 | * between 1K and SYSTEM_PAGE_SIZE, that is: 1k, 2k, 4k. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 181 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 182 | #define POOL_SIZE SYSTEM_PAGE_SIZE /* must be 2^N */ |
| 183 | #define POOL_SIZE_MASK SYSTEM_PAGE_SIZE_MASK |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 184 | |
| 185 | /* |
| 186 | * -- End of tunable settings section -- |
| 187 | */ |
| 188 | |
| 189 | /*==========================================================================*/ |
| 190 | |
| 191 | /* |
| 192 | * Locking |
| 193 | * |
| 194 | * To reduce lock contention, it would probably be better to refine the |
| 195 | * crude function locking with per size class locking. I'm not positive |
| 196 | * however, whether it's worth switching to such locking policy because |
| 197 | * of the performance penalty it might introduce. |
| 198 | * |
| 199 | * The following macros describe the simplest (should also be the fastest) |
| 200 | * lock object on a particular platform and the init/fini/lock/unlock |
| 201 | * operations on it. The locks defined here are not expected to be recursive |
| 202 | * because it is assumed that they will always be called in the order: |
| 203 | * INIT, [LOCK, UNLOCK]*, FINI. |
| 204 | */ |
| 205 | |
| 206 | /* |
| 207 | * Python's threads are serialized, so object malloc locking is disabled. |
| 208 | */ |
| 209 | #define SIMPLELOCK_DECL(lock) /* simple lock declaration */ |
| 210 | #define SIMPLELOCK_INIT(lock) /* allocate (if needed) and initialize */ |
| 211 | #define SIMPLELOCK_FINI(lock) /* free/destroy an existing lock */ |
| 212 | #define SIMPLELOCK_LOCK(lock) /* acquire released lock */ |
| 213 | #define SIMPLELOCK_UNLOCK(lock) /* release acquired lock */ |
| 214 | |
| 215 | /* |
| 216 | * Basic types |
| 217 | * I don't care if these are defined in <sys/types.h> or elsewhere. Axiom. |
| 218 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 219 | #undef uchar |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 220 | #define uchar unsigned char /* assuming == 8 bits */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 221 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 222 | #undef uint |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 223 | #define uint unsigned int /* assuming >= 16 bits */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 224 | |
| 225 | #undef ulong |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 226 | #define ulong unsigned long /* assuming >= 32 bits */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 227 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 228 | #undef uptr |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 229 | #define uptr Py_uintptr_t |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 230 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 231 | /* When you say memory, my mind reasons in terms of (pointers to) blocks */ |
| 232 | typedef uchar block; |
| 233 | |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 234 | /* Pool for small blocks. */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 235 | struct pool_header { |
Tim Peters | b233652 | 2001-03-11 18:36:13 +0000 | [diff] [blame] | 236 | union { block *_padding; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 237 | uint count; } ref; /* number of allocated blocks */ |
| 238 | block *freeblock; /* pool's free list head */ |
| 239 | struct pool_header *nextpool; /* next pool of this size class */ |
| 240 | struct pool_header *prevpool; /* previous pool "" */ |
Tim Peters | 1d99af8 | 2002-03-30 10:35:09 +0000 | [diff] [blame] | 241 | uint arenaindex; /* index into arenas of base adr */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 242 | uint szidx; /* block size class index */ |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 243 | uint nextoffset; /* bytes to virgin block */ |
| 244 | uint maxnextoffset; /* largest valid nextoffset */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | typedef struct pool_header *poolp; |
| 248 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 249 | /* Record keeping for arenas. */ |
| 250 | struct arena_object { |
| 251 | /* The address of the arena, as returned by malloc. Note that 0 |
| 252 | * will never be returned by a successful malloc, and is used |
| 253 | * here to mark an arena_object that doesn't correspond to an |
| 254 | * allocated arena. |
| 255 | */ |
| 256 | uptr address; |
| 257 | |
| 258 | /* Pool-aligned pointer to the next pool to be carved off. */ |
| 259 | block* pool_address; |
| 260 | |
| 261 | /* The number of available pools in the arena: free pools + never- |
| 262 | * allocated pools. |
| 263 | */ |
| 264 | uint nfreepools; |
| 265 | |
| 266 | /* The total number of pools in the arena, whether or not available. */ |
| 267 | uint ntotalpools; |
| 268 | |
| 269 | /* Singly-linked list of available pools. */ |
| 270 | struct pool_header* freepools; |
| 271 | |
| 272 | /* Whenever this arena_object is not associated with an allocated |
| 273 | * arena, the nextarena member is used to link all unassociated |
| 274 | * arena_objects in the singly-linked `unused_arena_objects` list. |
| 275 | * The prevarena member is unused in this case. |
| 276 | * |
| 277 | * When this arena_object is associated with an allocated arena |
| 278 | * with at least one available pool, both members are used in the |
| 279 | * doubly-linked `usable_arenas` list, which is maintained in |
| 280 | * increasing order of `nfreepools` values. |
| 281 | * |
| 282 | * Else this arena_object is associated with an allocated arena |
| 283 | * all of whose pools are in use. `nextarena` and `prevarena` |
| 284 | * are both meaningless in this case. |
| 285 | */ |
| 286 | struct arena_object* nextarena; |
| 287 | struct arena_object* prevarena; |
| 288 | }; |
| 289 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 290 | #undef ROUNDUP |
| 291 | #define ROUNDUP(x) (((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK) |
| 292 | #define POOL_OVERHEAD ROUNDUP(sizeof(struct pool_header)) |
| 293 | |
| 294 | #define DUMMY_SIZE_IDX 0xffff /* size class of newly cached pools */ |
| 295 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 296 | /* Round pointer P down to the closest pool-aligned address <= P, as a poolp */ |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 297 | #define POOL_ADDR(P) ((poolp)((uptr)(P) & ~(uptr)POOL_SIZE_MASK)) |
| 298 | |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 299 | /* Return total number of blocks in pool of size index I, as a uint. */ |
| 300 | #define NUMBLOCKS(I) ((uint)(POOL_SIZE - POOL_OVERHEAD) / INDEX2SIZE(I)) |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 301 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 302 | /*==========================================================================*/ |
| 303 | |
| 304 | /* |
| 305 | * This malloc lock |
| 306 | */ |
Jeremy Hylton | d1fedb6 | 2002-07-18 18:49:52 +0000 | [diff] [blame] | 307 | SIMPLELOCK_DECL(_malloc_lock) |
Tim Peters | b233652 | 2001-03-11 18:36:13 +0000 | [diff] [blame] | 308 | #define LOCK() SIMPLELOCK_LOCK(_malloc_lock) |
| 309 | #define UNLOCK() SIMPLELOCK_UNLOCK(_malloc_lock) |
| 310 | #define LOCK_INIT() SIMPLELOCK_INIT(_malloc_lock) |
| 311 | #define LOCK_FINI() SIMPLELOCK_FINI(_malloc_lock) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 312 | |
| 313 | /* |
Tim Peters | 1e16db6 | 2002-03-31 01:05:22 +0000 | [diff] [blame] | 314 | * Pool table -- headed, circular, doubly-linked lists of partially used pools. |
| 315 | |
| 316 | This is involved. For an index i, usedpools[i+i] is the header for a list of |
| 317 | all partially used pools holding small blocks with "size class idx" i. So |
| 318 | usedpools[0] corresponds to blocks of size 8, usedpools[2] to blocks of size |
| 319 | 16, and so on: index 2*i <-> blocks of size (i+1)<<ALIGNMENT_SHIFT. |
| 320 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 321 | Pools are carved off an arena's highwater mark (an arena_object's pool_address |
| 322 | member) as needed. Once carved off, a pool is in one of three states forever |
| 323 | after: |
Tim Peters | 1e16db6 | 2002-03-31 01:05:22 +0000 | [diff] [blame] | 324 | |
Tim Peters | 338e010 | 2002-04-01 19:23:44 +0000 | [diff] [blame] | 325 | used == partially used, neither empty nor full |
| 326 | At least one block in the pool is currently allocated, and at least one |
| 327 | block in the pool is not currently allocated (note this implies a pool |
| 328 | has room for at least two blocks). |
| 329 | This is a pool's initial state, as a pool is created only when malloc |
| 330 | needs space. |
| 331 | The pool holds blocks of a fixed size, and is in the circular list headed |
| 332 | at usedpools[i] (see above). It's linked to the other used pools of the |
| 333 | same size class via the pool_header's nextpool and prevpool members. |
| 334 | If all but one block is currently allocated, a malloc can cause a |
| 335 | transition to the full state. If all but one block is not currently |
| 336 | allocated, a free can cause a transition to the empty state. |
Tim Peters | 1e16db6 | 2002-03-31 01:05:22 +0000 | [diff] [blame] | 337 | |
Tim Peters | 338e010 | 2002-04-01 19:23:44 +0000 | [diff] [blame] | 338 | full == all the pool's blocks are currently allocated |
| 339 | On transition to full, a pool is unlinked from its usedpools[] list. |
| 340 | It's not linked to from anything then anymore, and its nextpool and |
| 341 | prevpool members are meaningless until it transitions back to used. |
| 342 | A free of a block in a full pool puts the pool back in the used state. |
| 343 | Then it's linked in at the front of the appropriate usedpools[] list, so |
| 344 | that the next allocation for its size class will reuse the freed block. |
| 345 | |
| 346 | empty == all the pool's blocks are currently available for allocation |
| 347 | On transition to empty, a pool is unlinked from its usedpools[] list, |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 348 | and linked to the front of its arena_object's singly-linked freepools list, |
Tim Peters | 338e010 | 2002-04-01 19:23:44 +0000 | [diff] [blame] | 349 | via its nextpool member. The prevpool member has no meaning in this case. |
| 350 | Empty pools have no inherent size class: the next time a malloc finds |
| 351 | an empty list in usedpools[], it takes the first pool off of freepools. |
| 352 | If the size class needed happens to be the same as the size class the pool |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 353 | last had, some pool initialization can be skipped. |
Tim Peters | 338e010 | 2002-04-01 19:23:44 +0000 | [diff] [blame] | 354 | |
| 355 | |
| 356 | Block Management |
| 357 | |
| 358 | Blocks within pools are again carved out as needed. pool->freeblock points to |
| 359 | the start of a singly-linked list of free blocks within the pool. When a |
| 360 | block is freed, it's inserted at the front of its pool's freeblock list. Note |
| 361 | that the available blocks in a pool are *not* linked all together when a pool |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 362 | is initialized. Instead only "the first two" (lowest addresses) blocks are |
| 363 | set up, returning the first such block, and setting pool->freeblock to a |
| 364 | one-block list holding the second such block. This is consistent with that |
| 365 | pymalloc strives at all levels (arena, pool, and block) never to touch a piece |
| 366 | of memory until it's actually needed. |
| 367 | |
| 368 | So long as a pool is in the used state, we're certain there *is* a block |
Tim Peters | 52aefc8 | 2002-04-11 06:36:45 +0000 | [diff] [blame] | 369 | available for allocating, and pool->freeblock is not NULL. If pool->freeblock |
| 370 | points to the end of the free list before we've carved the entire pool into |
| 371 | blocks, that means we simply haven't yet gotten to one of the higher-address |
| 372 | blocks. The offset from the pool_header to the start of "the next" virgin |
| 373 | block is stored in the pool_header nextoffset member, and the largest value |
| 374 | of nextoffset that makes sense is stored in the maxnextoffset member when a |
| 375 | pool is initialized. All the blocks in a pool have been passed out at least |
| 376 | once when and only when nextoffset > maxnextoffset. |
Tim Peters | 338e010 | 2002-04-01 19:23:44 +0000 | [diff] [blame] | 377 | |
Tim Peters | 1e16db6 | 2002-03-31 01:05:22 +0000 | [diff] [blame] | 378 | |
| 379 | Major obscurity: While the usedpools vector is declared to have poolp |
| 380 | entries, it doesn't really. It really contains two pointers per (conceptual) |
| 381 | poolp entry, the nextpool and prevpool members of a pool_header. The |
| 382 | excruciating initialization code below fools C so that |
| 383 | |
| 384 | usedpool[i+i] |
| 385 | |
| 386 | "acts like" a genuine poolp, but only so long as you only reference its |
| 387 | nextpool and prevpool members. The "- 2*sizeof(block *)" gibberish is |
| 388 | compensating for that a pool_header's nextpool and prevpool members |
| 389 | immediately follow a pool_header's first two members: |
| 390 | |
| 391 | union { block *_padding; |
| 392 | uint count; } ref; |
| 393 | block *freeblock; |
| 394 | |
| 395 | each of which consume sizeof(block *) bytes. So what usedpools[i+i] really |
| 396 | contains is a fudged-up pointer p such that *if* C believes it's a poolp |
| 397 | pointer, then p->nextpool and p->prevpool are both p (meaning that the headed |
| 398 | circular list is empty). |
| 399 | |
| 400 | It's unclear why the usedpools setup is so convoluted. It could be to |
| 401 | minimize the amount of cache required to hold this heavily-referenced table |
| 402 | (which only *needs* the two interpool pointer members of a pool_header). OTOH, |
| 403 | referencing code has to remember to "double the index" and doing so isn't |
| 404 | free, usedpools[0] isn't a strictly legal pointer, and we're crucially relying |
| 405 | on that C doesn't insert any padding anywhere in a pool_header at or before |
| 406 | the prevpool member. |
| 407 | **************************************************************************** */ |
| 408 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 409 | #define PTA(x) ((poolp )((uchar *)&(usedpools[2*(x)]) - 2*sizeof(block *))) |
| 410 | #define PT(x) PTA(x), PTA(x) |
| 411 | |
| 412 | static poolp usedpools[2 * ((NB_SMALL_SIZE_CLASSES + 7) / 8) * 8] = { |
| 413 | PT(0), PT(1), PT(2), PT(3), PT(4), PT(5), PT(6), PT(7) |
| 414 | #if NB_SMALL_SIZE_CLASSES > 8 |
| 415 | , PT(8), PT(9), PT(10), PT(11), PT(12), PT(13), PT(14), PT(15) |
| 416 | #if NB_SMALL_SIZE_CLASSES > 16 |
| 417 | , PT(16), PT(17), PT(18), PT(19), PT(20), PT(21), PT(22), PT(23) |
| 418 | #if NB_SMALL_SIZE_CLASSES > 24 |
| 419 | , PT(24), PT(25), PT(26), PT(27), PT(28), PT(29), PT(30), PT(31) |
| 420 | #if NB_SMALL_SIZE_CLASSES > 32 |
| 421 | , PT(32), PT(33), PT(34), PT(35), PT(36), PT(37), PT(38), PT(39) |
| 422 | #if NB_SMALL_SIZE_CLASSES > 40 |
| 423 | , PT(40), PT(41), PT(42), PT(43), PT(44), PT(45), PT(46), PT(47) |
| 424 | #if NB_SMALL_SIZE_CLASSES > 48 |
| 425 | , PT(48), PT(49), PT(50), PT(51), PT(52), PT(53), PT(54), PT(55) |
| 426 | #if NB_SMALL_SIZE_CLASSES > 56 |
| 427 | , PT(56), PT(57), PT(58), PT(59), PT(60), PT(61), PT(62), PT(63) |
| 428 | #endif /* NB_SMALL_SIZE_CLASSES > 56 */ |
| 429 | #endif /* NB_SMALL_SIZE_CLASSES > 48 */ |
| 430 | #endif /* NB_SMALL_SIZE_CLASSES > 40 */ |
| 431 | #endif /* NB_SMALL_SIZE_CLASSES > 32 */ |
| 432 | #endif /* NB_SMALL_SIZE_CLASSES > 24 */ |
| 433 | #endif /* NB_SMALL_SIZE_CLASSES > 16 */ |
| 434 | #endif /* NB_SMALL_SIZE_CLASSES > 8 */ |
| 435 | }; |
| 436 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 437 | /*========================================================================== |
| 438 | Arena management. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 439 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 440 | `arenas` is a vector of arena_objects. It contains maxarenas entries, some of |
| 441 | which may not be currently used (== they're arena_objects that aren't |
| 442 | currently associated with an allocated arena). Note that arenas proper are |
| 443 | separately malloc'ed. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 444 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 445 | Prior to Python 2.5, arenas were never free()'ed. Starting with Python 2.5, |
| 446 | we do try to free() arenas, and use some mild heuristic strategies to increase |
| 447 | the likelihood that arenas eventually can be freed. |
| 448 | |
| 449 | unused_arena_objects |
| 450 | |
| 451 | This is a singly-linked list of the arena_objects that are currently not |
| 452 | being used (no arena is associated with them). Objects are taken off the |
| 453 | head of the list in new_arena(), and are pushed on the head of the list in |
| 454 | PyObject_Free() when the arena is empty. Key invariant: an arena_object |
| 455 | is on this list if and only if its .address member is 0. |
| 456 | |
| 457 | usable_arenas |
| 458 | |
| 459 | This is a doubly-linked list of the arena_objects associated with arenas |
| 460 | that have pools available. These pools are either waiting to be reused, |
| 461 | or have not been used before. The list is sorted to have the most- |
| 462 | allocated arenas first (ascending order based on the nfreepools member). |
| 463 | This means that the next allocation will come from a heavily used arena, |
| 464 | which gives the nearly empty arenas a chance to be returned to the system. |
| 465 | In my unscientific tests this dramatically improved the number of arenas |
| 466 | that could be freed. |
| 467 | |
| 468 | Note that an arena_object associated with an arena all of whose pools are |
| 469 | currently in use isn't on either list. |
| 470 | */ |
| 471 | |
| 472 | /* Array of objects used to track chunks of memory (arenas). */ |
| 473 | static struct arena_object* arenas = NULL; |
| 474 | /* Number of slots currently allocated in the `arenas` vector. */ |
Tim Peters | 1d99af8 | 2002-03-30 10:35:09 +0000 | [diff] [blame] | 475 | static uint maxarenas = 0; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 476 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 477 | /* The head of the singly-linked, NULL-terminated list of available |
| 478 | * arena_objects. |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 479 | */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 480 | static struct arena_object* unused_arena_objects = NULL; |
| 481 | |
| 482 | /* The head of the doubly-linked, NULL-terminated at each end, list of |
| 483 | * arena_objects associated with arenas that have pools available. |
| 484 | */ |
| 485 | static struct arena_object* usable_arenas = NULL; |
| 486 | |
| 487 | /* How many arena_objects do we initially allocate? |
| 488 | * 16 = can allocate 16 arenas = 16 * ARENA_SIZE = 4MB before growing the |
| 489 | * `arenas` vector. |
| 490 | */ |
| 491 | #define INITIAL_ARENA_OBJECTS 16 |
| 492 | |
| 493 | /* Number of arenas allocated that haven't been free()'d. */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 494 | static size_t narenas_currently_allocated = 0; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 495 | |
| 496 | #ifdef PYMALLOC_DEBUG |
| 497 | /* Total number of times malloc() called to allocate an arena. */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 498 | static size_t ntimes_arena_allocated = 0; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 499 | /* High water mark (max value ever seen) for narenas_currently_allocated. */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 500 | static size_t narenas_highwater = 0; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 501 | #endif |
| 502 | |
| 503 | /* Allocate a new arena. If we run out of memory, return NULL. Else |
| 504 | * allocate a new arena, and return the address of an arena_object |
| 505 | * describing the new arena. It's expected that the caller will set |
| 506 | * `usable_arenas` to the return value. |
| 507 | */ |
| 508 | static struct arena_object* |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 509 | new_arena(void) |
| 510 | { |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 511 | struct arena_object* arenaobj; |
Tim Peters | 3c83df2 | 2002-03-30 07:04:41 +0000 | [diff] [blame] | 512 | uint excess; /* number of bytes above pool alignment */ |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 513 | |
Tim Peters | 0e87118 | 2002-04-13 08:29:14 +0000 | [diff] [blame] | 514 | #ifdef PYMALLOC_DEBUG |
| 515 | if (Py_GETENV("PYTHONMALLOCSTATS")) |
| 516 | _PyObject_DebugMallocStats(); |
| 517 | #endif |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 518 | if (unused_arena_objects == NULL) { |
| 519 | uint i; |
Martin v. Löwis | 5aca882 | 2008-09-11 06:55:48 +0000 | [diff] [blame] | 520 | uint numarenas; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 521 | size_t nbytes; |
Tim Peters | 0e87118 | 2002-04-13 08:29:14 +0000 | [diff] [blame] | 522 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 523 | /* Double the number of arena objects on each allocation. |
| 524 | * Note that it's possible for `numarenas` to overflow. |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 525 | */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 526 | numarenas = maxarenas ? maxarenas << 1 : INITIAL_ARENA_OBJECTS; |
| 527 | if (numarenas <= maxarenas) |
| 528 | return NULL; /* overflow */ |
Martin v. Löwis | 5aca882 | 2008-09-11 06:55:48 +0000 | [diff] [blame] | 529 | #if SIZEOF_SIZE_T <= SIZEOF_INT |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 530 | if (numarenas > PY_SIZE_MAX / sizeof(*arenas)) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 531 | return NULL; /* overflow */ |
Martin v. Löwis | 5aca882 | 2008-09-11 06:55:48 +0000 | [diff] [blame] | 532 | #endif |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 533 | nbytes = numarenas * sizeof(*arenas); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 534 | arenaobj = (struct arena_object *)realloc(arenas, nbytes); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 535 | if (arenaobj == NULL) |
| 536 | return NULL; |
| 537 | arenas = arenaobj; |
| 538 | |
| 539 | /* We might need to fix pointers that were copied. However, |
| 540 | * new_arena only gets called when all the pages in the |
| 541 | * previous arenas are full. Thus, there are *no* pointers |
| 542 | * into the old array. Thus, we don't have to worry about |
| 543 | * invalid pointers. Just to be sure, some asserts: |
| 544 | */ |
| 545 | assert(usable_arenas == NULL); |
| 546 | assert(unused_arena_objects == NULL); |
| 547 | |
| 548 | /* Put the new arenas on the unused_arena_objects list. */ |
| 549 | for (i = maxarenas; i < numarenas; ++i) { |
| 550 | arenas[i].address = 0; /* mark as unassociated */ |
| 551 | arenas[i].nextarena = i < numarenas - 1 ? |
| 552 | &arenas[i+1] : NULL; |
| 553 | } |
| 554 | |
| 555 | /* Update globals. */ |
| 556 | unused_arena_objects = &arenas[maxarenas]; |
| 557 | maxarenas = numarenas; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 560 | /* Take the next available arena object off the head of the list. */ |
| 561 | assert(unused_arena_objects != NULL); |
| 562 | arenaobj = unused_arena_objects; |
| 563 | unused_arena_objects = arenaobj->nextarena; |
| 564 | assert(arenaobj->address == 0); |
| 565 | arenaobj->address = (uptr)malloc(ARENA_SIZE); |
| 566 | if (arenaobj->address == 0) { |
| 567 | /* The allocation failed: return NULL after putting the |
| 568 | * arenaobj back. |
| 569 | */ |
| 570 | arenaobj->nextarena = unused_arena_objects; |
| 571 | unused_arena_objects = arenaobj; |
| 572 | return NULL; |
| 573 | } |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 574 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 575 | ++narenas_currently_allocated; |
| 576 | #ifdef PYMALLOC_DEBUG |
| 577 | ++ntimes_arena_allocated; |
| 578 | if (narenas_currently_allocated > narenas_highwater) |
| 579 | narenas_highwater = narenas_currently_allocated; |
| 580 | #endif |
| 581 | arenaobj->freepools = NULL; |
| 582 | /* pool_address <- first pool-aligned address in the arena |
| 583 | nfreepools <- number of whole pools that fit after alignment */ |
| 584 | arenaobj->pool_address = (block*)arenaobj->address; |
| 585 | arenaobj->nfreepools = ARENA_SIZE / POOL_SIZE; |
| 586 | assert(POOL_SIZE * arenaobj->nfreepools == ARENA_SIZE); |
| 587 | excess = (uint)(arenaobj->address & POOL_SIZE_MASK); |
| 588 | if (excess != 0) { |
| 589 | --arenaobj->nfreepools; |
| 590 | arenaobj->pool_address += POOL_SIZE - excess; |
| 591 | } |
| 592 | arenaobj->ntotalpools = arenaobj->nfreepools; |
| 593 | |
| 594 | return arenaobj; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 597 | /* |
| 598 | Py_ADDRESS_IN_RANGE(P, POOL) |
| 599 | |
| 600 | Return true if and only if P is an address that was allocated by pymalloc. |
| 601 | POOL must be the pool address associated with P, i.e., POOL = POOL_ADDR(P) |
| 602 | (the caller is asked to compute this because the macro expands POOL more than |
| 603 | once, and for efficiency it's best for the caller to assign POOL_ADDR(P) to a |
| 604 | variable and pass the latter to the macro; because Py_ADDRESS_IN_RANGE is |
| 605 | called on every alloc/realloc/free, micro-efficiency is important here). |
| 606 | |
| 607 | Tricky: Let B be the arena base address associated with the pool, B = |
| 608 | arenas[(POOL)->arenaindex].address. Then P belongs to the arena if and only if |
| 609 | |
| 610 | B <= P < B + ARENA_SIZE |
| 611 | |
| 612 | Subtracting B throughout, this is true iff |
| 613 | |
| 614 | 0 <= P-B < ARENA_SIZE |
| 615 | |
| 616 | By using unsigned arithmetic, the "0 <=" half of the test can be skipped. |
| 617 | |
| 618 | Obscure: A PyMem "free memory" function can call the pymalloc free or realloc |
| 619 | before the first arena has been allocated. `arenas` is still NULL in that |
| 620 | case. We're relying on that maxarenas is also 0 in that case, so that |
| 621 | (POOL)->arenaindex < maxarenas must be false, saving us from trying to index |
| 622 | into a NULL arenas. |
| 623 | |
| 624 | Details: given P and POOL, the arena_object corresponding to P is AO = |
| 625 | arenas[(POOL)->arenaindex]. Suppose obmalloc controls P. Then (barring wild |
| 626 | stores, etc), POOL is the correct address of P's pool, AO.address is the |
| 627 | correct base address of the pool's arena, and P must be within ARENA_SIZE of |
| 628 | AO.address. In addition, AO.address is not 0 (no arena can start at address 0 |
| 629 | (NULL)). Therefore Py_ADDRESS_IN_RANGE correctly reports that obmalloc |
| 630 | controls P. |
| 631 | |
| 632 | Now suppose obmalloc does not control P (e.g., P was obtained via a direct |
| 633 | call to the system malloc() or realloc()). (POOL)->arenaindex may be anything |
| 634 | in this case -- it may even be uninitialized trash. If the trash arenaindex |
| 635 | is >= maxarenas, the macro correctly concludes at once that obmalloc doesn't |
| 636 | control P. |
| 637 | |
| 638 | Else arenaindex is < maxarena, and AO is read up. If AO corresponds to an |
| 639 | allocated arena, obmalloc controls all the memory in slice AO.address : |
| 640 | AO.address+ARENA_SIZE. By case assumption, P is not controlled by obmalloc, |
| 641 | so P doesn't lie in that slice, so the macro correctly reports that P is not |
| 642 | controlled by obmalloc. |
| 643 | |
| 644 | Finally, if P is not controlled by obmalloc and AO corresponds to an unused |
| 645 | arena_object (one not currently associated with an allocated arena), |
| 646 | AO.address is 0, and the second test in the macro reduces to: |
| 647 | |
| 648 | P < ARENA_SIZE |
| 649 | |
| 650 | If P >= ARENA_SIZE (extremely likely), the macro again correctly concludes |
| 651 | that P is not controlled by obmalloc. However, if P < ARENA_SIZE, this part |
| 652 | of the test still passes, and the third clause (AO.address != 0) is necessary |
| 653 | to get the correct result: AO.address is 0 in this case, so the macro |
| 654 | correctly reports that P is not controlled by obmalloc (despite that P lies in |
| 655 | slice AO.address : AO.address + ARENA_SIZE). |
| 656 | |
| 657 | Note: The third (AO.address != 0) clause was added in Python 2.5. Before |
| 658 | 2.5, arenas were never free()'ed, and an arenaindex < maxarena always |
| 659 | corresponded to a currently-allocated arena, so the "P is not controlled by |
| 660 | obmalloc, AO corresponds to an unused arena_object, and P < ARENA_SIZE" case |
| 661 | was impossible. |
| 662 | |
| 663 | Note that the logic is excruciating, and reading up possibly uninitialized |
| 664 | memory when P is not controlled by obmalloc (to get at (POOL)->arenaindex) |
| 665 | creates problems for some memory debuggers. The overwhelming advantage is |
| 666 | that this test determines whether an arbitrary address is controlled by |
| 667 | obmalloc in a small constant time, independent of the number of arenas |
| 668 | obmalloc controls. Since this test is needed at every entry point, it's |
| 669 | extremely desirable that it be this fast. |
| 670 | */ |
| 671 | #define Py_ADDRESS_IN_RANGE(P, POOL) \ |
| 672 | ((POOL)->arenaindex < maxarenas && \ |
| 673 | (uptr)(P) - arenas[(POOL)->arenaindex].address < (uptr)ARENA_SIZE && \ |
| 674 | arenas[(POOL)->arenaindex].address != 0) |
| 675 | |
Neal Norwitz | 7eb3c91 | 2004-06-06 19:20:22 +0000 | [diff] [blame] | 676 | |
| 677 | /* This is only useful when running memory debuggers such as |
| 678 | * Purify or Valgrind. Uncomment to use. |
| 679 | * |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 680 | #define Py_USING_MEMORY_DEBUGGER |
Martin v. Löwis | 6fea233 | 2008-09-25 04:15:27 +0000 | [diff] [blame] | 681 | */ |
Neal Norwitz | 7eb3c91 | 2004-06-06 19:20:22 +0000 | [diff] [blame] | 682 | |
| 683 | #ifdef Py_USING_MEMORY_DEBUGGER |
| 684 | |
| 685 | /* Py_ADDRESS_IN_RANGE may access uninitialized memory by design |
| 686 | * This leads to thousands of spurious warnings when using |
| 687 | * Purify or Valgrind. By making a function, we can easily |
| 688 | * suppress the uninitialized memory reads in this one function. |
| 689 | * So we won't ignore real errors elsewhere. |
| 690 | * |
| 691 | * Disable the macro and use a function. |
| 692 | */ |
| 693 | |
| 694 | #undef Py_ADDRESS_IN_RANGE |
| 695 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 696 | #if defined(__GNUC__) && ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1) || \ |
| 697 | (__GNUC__ >= 4)) |
Neal Norwitz | e5e5aa4 | 2005-11-13 18:55:39 +0000 | [diff] [blame] | 698 | #define Py_NO_INLINE __attribute__((__noinline__)) |
| 699 | #else |
| 700 | #define Py_NO_INLINE |
| 701 | #endif |
| 702 | |
| 703 | /* Don't make static, to try to ensure this isn't inlined. */ |
| 704 | int Py_ADDRESS_IN_RANGE(void *P, poolp pool) Py_NO_INLINE; |
| 705 | #undef Py_NO_INLINE |
Neal Norwitz | 7eb3c91 | 2004-06-06 19:20:22 +0000 | [diff] [blame] | 706 | #endif |
Tim Peters | 338e010 | 2002-04-01 19:23:44 +0000 | [diff] [blame] | 707 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 708 | /*==========================================================================*/ |
| 709 | |
Tim Peters | 84c1b97 | 2002-04-04 04:44:32 +0000 | [diff] [blame] | 710 | /* malloc. Note that nbytes==0 tries to return a non-NULL pointer, distinct |
| 711 | * from all other currently live pointers. This may not be possible. |
| 712 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 713 | |
| 714 | /* |
| 715 | * The basic blocks are ordered by decreasing execution frequency, |
| 716 | * which minimizes the number of jumps in the most common cases, |
| 717 | * improves branching prediction and instruction scheduling (small |
| 718 | * block allocations typically result in a couple of instructions). |
| 719 | * Unless the optimizer reorders everything, being too smart... |
| 720 | */ |
| 721 | |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 722 | #undef PyObject_Malloc |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 723 | void * |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 724 | PyObject_Malloc(size_t nbytes) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 725 | { |
| 726 | block *bp; |
| 727 | poolp pool; |
| 728 | poolp next; |
| 729 | uint size; |
| 730 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 731 | /* |
Georg Brandl | d492ad8 | 2008-07-23 16:13:07 +0000 | [diff] [blame] | 732 | * Limit ourselves to PY_SSIZE_T_MAX bytes to prevent security holes. |
| 733 | * Most python internals blindly use a signed Py_ssize_t to track |
| 734 | * things without checking for overflows or negatives. |
| 735 | * As size_t is unsigned, checking for nbytes < 0 is not required. |
| 736 | */ |
| 737 | if (nbytes > PY_SSIZE_T_MAX) |
| 738 | return NULL; |
| 739 | |
| 740 | /* |
Tim Peters | 84c1b97 | 2002-04-04 04:44:32 +0000 | [diff] [blame] | 741 | * This implicitly redirects malloc(0). |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 742 | */ |
| 743 | if ((nbytes - 1) < SMALL_REQUEST_THRESHOLD) { |
| 744 | LOCK(); |
| 745 | /* |
| 746 | * Most frequent paths first |
| 747 | */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 748 | size = (uint)(nbytes - 1) >> ALIGNMENT_SHIFT; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 749 | pool = usedpools[size + size]; |
| 750 | if (pool != pool->nextpool) { |
| 751 | /* |
| 752 | * There is a used pool for this size class. |
| 753 | * Pick up the head block of its free list. |
| 754 | */ |
| 755 | ++pool->ref.count; |
| 756 | bp = pool->freeblock; |
Tim Peters | 52aefc8 | 2002-04-11 06:36:45 +0000 | [diff] [blame] | 757 | assert(bp != NULL); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 758 | if ((pool->freeblock = *(block **)bp) != NULL) { |
| 759 | UNLOCK(); |
| 760 | return (void *)bp; |
| 761 | } |
| 762 | /* |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 763 | * Reached the end of the free list, try to extend it. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 764 | */ |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 765 | if (pool->nextoffset <= pool->maxnextoffset) { |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 766 | /* There is room for another block. */ |
| 767 | pool->freeblock = (block*)pool + |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 768 | pool->nextoffset; |
| 769 | pool->nextoffset += INDEX2SIZE(size); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 770 | *(block **)(pool->freeblock) = NULL; |
| 771 | UNLOCK(); |
| 772 | return (void *)bp; |
| 773 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 774 | /* Pool is full, unlink from used pools. */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 775 | next = pool->nextpool; |
| 776 | pool = pool->prevpool; |
| 777 | next->prevpool = pool; |
| 778 | pool->nextpool = next; |
| 779 | UNLOCK(); |
| 780 | return (void *)bp; |
| 781 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 782 | |
| 783 | /* There isn't a pool of the right size class immediately |
| 784 | * available: use a free pool. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 785 | */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 786 | if (usable_arenas == NULL) { |
| 787 | /* No arena has a free pool: allocate a new arena. */ |
| 788 | #ifdef WITH_MEMORY_LIMITS |
| 789 | if (narenas_currently_allocated >= MAX_ARENAS) { |
| 790 | UNLOCK(); |
| 791 | goto redirect; |
| 792 | } |
| 793 | #endif |
| 794 | usable_arenas = new_arena(); |
| 795 | if (usable_arenas == NULL) { |
| 796 | UNLOCK(); |
| 797 | goto redirect; |
| 798 | } |
| 799 | usable_arenas->nextarena = |
| 800 | usable_arenas->prevarena = NULL; |
| 801 | } |
| 802 | assert(usable_arenas->address != 0); |
| 803 | |
| 804 | /* Try to get a cached free pool. */ |
| 805 | pool = usable_arenas->freepools; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 806 | if (pool != NULL) { |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 807 | /* Unlink from cached pools. */ |
| 808 | usable_arenas->freepools = pool->nextpool; |
| 809 | |
| 810 | /* This arena already had the smallest nfreepools |
| 811 | * value, so decreasing nfreepools doesn't change |
| 812 | * that, and we don't need to rearrange the |
| 813 | * usable_arenas list. However, if the arena has |
| 814 | * become wholly allocated, we need to remove its |
| 815 | * arena_object from usable_arenas. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 816 | */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 817 | --usable_arenas->nfreepools; |
| 818 | if (usable_arenas->nfreepools == 0) { |
| 819 | /* Wholly allocated: remove. */ |
| 820 | assert(usable_arenas->freepools == NULL); |
| 821 | assert(usable_arenas->nextarena == NULL || |
| 822 | usable_arenas->nextarena->prevarena == |
| 823 | usable_arenas); |
| 824 | |
| 825 | usable_arenas = usable_arenas->nextarena; |
| 826 | if (usable_arenas != NULL) { |
| 827 | usable_arenas->prevarena = NULL; |
| 828 | assert(usable_arenas->address != 0); |
| 829 | } |
| 830 | } |
| 831 | else { |
| 832 | /* nfreepools > 0: it must be that freepools |
| 833 | * isn't NULL, or that we haven't yet carved |
| 834 | * off all the arena's pools for the first |
| 835 | * time. |
| 836 | */ |
| 837 | assert(usable_arenas->freepools != NULL || |
| 838 | usable_arenas->pool_address <= |
| 839 | (block*)usable_arenas->address + |
| 840 | ARENA_SIZE - POOL_SIZE); |
| 841 | } |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 842 | init_pool: |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 843 | /* Frontlink to used pools. */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 844 | next = usedpools[size + size]; /* == prev */ |
| 845 | pool->nextpool = next; |
| 846 | pool->prevpool = next; |
| 847 | next->nextpool = pool; |
| 848 | next->prevpool = pool; |
| 849 | pool->ref.count = 1; |
| 850 | if (pool->szidx == size) { |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 851 | /* Luckily, this pool last contained blocks |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 852 | * of the same size class, so its header |
| 853 | * and free list are already initialized. |
| 854 | */ |
| 855 | bp = pool->freeblock; |
| 856 | pool->freeblock = *(block **)bp; |
| 857 | UNLOCK(); |
| 858 | return (void *)bp; |
| 859 | } |
| 860 | /* |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 861 | * Initialize the pool header, set up the free list to |
| 862 | * contain just the second block, and return the first |
| 863 | * block. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 864 | */ |
| 865 | pool->szidx = size; |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 866 | size = INDEX2SIZE(size); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 867 | bp = (block *)pool + POOL_OVERHEAD; |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 868 | pool->nextoffset = POOL_OVERHEAD + (size << 1); |
| 869 | pool->maxnextoffset = POOL_SIZE - size; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 870 | pool->freeblock = bp + size; |
| 871 | *(block **)(pool->freeblock) = NULL; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 872 | UNLOCK(); |
| 873 | return (void *)bp; |
| 874 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 875 | |
| 876 | /* Carve off a new pool. */ |
| 877 | assert(usable_arenas->nfreepools > 0); |
| 878 | assert(usable_arenas->freepools == NULL); |
| 879 | pool = (poolp)usable_arenas->pool_address; |
| 880 | assert((block*)pool <= (block*)usable_arenas->address + |
| 881 | ARENA_SIZE - POOL_SIZE); |
| 882 | pool->arenaindex = usable_arenas - arenas; |
| 883 | assert(&arenas[pool->arenaindex] == usable_arenas); |
| 884 | pool->szidx = DUMMY_SIZE_IDX; |
| 885 | usable_arenas->pool_address += POOL_SIZE; |
| 886 | --usable_arenas->nfreepools; |
| 887 | |
| 888 | if (usable_arenas->nfreepools == 0) { |
| 889 | assert(usable_arenas->nextarena == NULL || |
| 890 | usable_arenas->nextarena->prevarena == |
| 891 | usable_arenas); |
| 892 | /* Unlink the arena: it is completely allocated. */ |
| 893 | usable_arenas = usable_arenas->nextarena; |
| 894 | if (usable_arenas != NULL) { |
| 895 | usable_arenas->prevarena = NULL; |
| 896 | assert(usable_arenas->address != 0); |
| 897 | } |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 898 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 899 | |
| 900 | goto init_pool; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | /* The small block allocator ends here. */ |
| 904 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 905 | redirect: |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 906 | /* Redirect the original request to the underlying (libc) allocator. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 907 | * We jump here on bigger requests, on error in the code above (as a |
| 908 | * last chance to serve the request) or when the max memory limit |
| 909 | * has been reached. |
| 910 | */ |
Tim Peters | 64d80c9 | 2002-04-18 21:58:56 +0000 | [diff] [blame] | 911 | if (nbytes == 0) |
| 912 | nbytes = 1; |
Tim Peters | 64d80c9 | 2002-04-18 21:58:56 +0000 | [diff] [blame] | 913 | return (void *)malloc(nbytes); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | /* free */ |
| 917 | |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 918 | #undef PyObject_Free |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 919 | void |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 920 | PyObject_Free(void *p) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 921 | { |
| 922 | poolp pool; |
Tim Peters | 2c95c99 | 2002-03-31 02:18:01 +0000 | [diff] [blame] | 923 | block *lastfree; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 924 | poolp next, prev; |
| 925 | uint size; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 926 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 927 | if (p == NULL) /* free(NULL) has no effect */ |
| 928 | return; |
| 929 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 930 | pool = POOL_ADDR(p); |
Neal Norwitz | 7eb3c91 | 2004-06-06 19:20:22 +0000 | [diff] [blame] | 931 | if (Py_ADDRESS_IN_RANGE(p, pool)) { |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 932 | /* We allocated this address. */ |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 933 | LOCK(); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 934 | /* Link p to the start of the pool's freeblock list. Since |
Tim Peters | 2c95c99 | 2002-03-31 02:18:01 +0000 | [diff] [blame] | 935 | * the pool had at least the p block outstanding, the pool |
| 936 | * wasn't empty (so it's already in a usedpools[] list, or |
| 937 | * was full and is in no list -- it's not in the freeblocks |
| 938 | * list in any case). |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 939 | */ |
Tim Peters | 57b17ad | 2002-03-31 02:59:48 +0000 | [diff] [blame] | 940 | assert(pool->ref.count > 0); /* else it was empty */ |
Tim Peters | 2c95c99 | 2002-03-31 02:18:01 +0000 | [diff] [blame] | 941 | *(block **)p = lastfree = pool->freeblock; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 942 | pool->freeblock = (block *)p; |
Tim Peters | 2c95c99 | 2002-03-31 02:18:01 +0000 | [diff] [blame] | 943 | if (lastfree) { |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 944 | struct arena_object* ao; |
| 945 | uint nf; /* ao->nfreepools */ |
| 946 | |
| 947 | /* freeblock wasn't NULL, so the pool wasn't full, |
Tim Peters | 2c95c99 | 2002-03-31 02:18:01 +0000 | [diff] [blame] | 948 | * and the pool is in a usedpools[] list. |
| 949 | */ |
Tim Peters | 2c95c99 | 2002-03-31 02:18:01 +0000 | [diff] [blame] | 950 | if (--pool->ref.count != 0) { |
| 951 | /* pool isn't empty: leave it in usedpools */ |
| 952 | UNLOCK(); |
| 953 | return; |
| 954 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 955 | /* Pool is now empty: unlink from usedpools, and |
Tim Peters | b1da050 | 2002-03-31 02:51:40 +0000 | [diff] [blame] | 956 | * link to the front of freepools. This ensures that |
Tim Peters | 2c95c99 | 2002-03-31 02:18:01 +0000 | [diff] [blame] | 957 | * previously freed pools will be allocated later |
| 958 | * (being not referenced, they are perhaps paged out). |
| 959 | */ |
| 960 | next = pool->nextpool; |
| 961 | prev = pool->prevpool; |
| 962 | next->prevpool = prev; |
| 963 | prev->nextpool = next; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 964 | |
| 965 | /* Link the pool to freepools. This is a singly-linked |
| 966 | * list, and pool->prevpool isn't used there. |
Tim Peters | 2c95c99 | 2002-03-31 02:18:01 +0000 | [diff] [blame] | 967 | */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 968 | ao = &arenas[pool->arenaindex]; |
| 969 | pool->nextpool = ao->freepools; |
| 970 | ao->freepools = pool; |
| 971 | nf = ++ao->nfreepools; |
| 972 | |
| 973 | /* All the rest is arena management. We just freed |
| 974 | * a pool, and there are 4 cases for arena mgmt: |
| 975 | * 1. If all the pools are free, return the arena to |
| 976 | * the system free(). |
| 977 | * 2. If this is the only free pool in the arena, |
| 978 | * add the arena back to the `usable_arenas` list. |
| 979 | * 3. If the "next" arena has a smaller count of free |
| 980 | * pools, we have to "slide this arena right" to |
| 981 | * restore that usable_arenas is sorted in order of |
| 982 | * nfreepools. |
| 983 | * 4. Else there's nothing more to do. |
| 984 | */ |
| 985 | if (nf == ao->ntotalpools) { |
| 986 | /* Case 1. First unlink ao from usable_arenas. |
| 987 | */ |
| 988 | assert(ao->prevarena == NULL || |
| 989 | ao->prevarena->address != 0); |
| 990 | assert(ao ->nextarena == NULL || |
| 991 | ao->nextarena->address != 0); |
| 992 | |
| 993 | /* Fix the pointer in the prevarena, or the |
| 994 | * usable_arenas pointer. |
| 995 | */ |
| 996 | if (ao->prevarena == NULL) { |
| 997 | usable_arenas = ao->nextarena; |
| 998 | assert(usable_arenas == NULL || |
| 999 | usable_arenas->address != 0); |
| 1000 | } |
| 1001 | else { |
| 1002 | assert(ao->prevarena->nextarena == ao); |
| 1003 | ao->prevarena->nextarena = |
| 1004 | ao->nextarena; |
| 1005 | } |
| 1006 | /* Fix the pointer in the nextarena. */ |
| 1007 | if (ao->nextarena != NULL) { |
| 1008 | assert(ao->nextarena->prevarena == ao); |
| 1009 | ao->nextarena->prevarena = |
| 1010 | ao->prevarena; |
| 1011 | } |
| 1012 | /* Record that this arena_object slot is |
| 1013 | * available to be reused. |
| 1014 | */ |
| 1015 | ao->nextarena = unused_arena_objects; |
| 1016 | unused_arena_objects = ao; |
| 1017 | |
| 1018 | /* Free the entire arena. */ |
| 1019 | free((void *)ao->address); |
| 1020 | ao->address = 0; /* mark unassociated */ |
| 1021 | --narenas_currently_allocated; |
| 1022 | |
| 1023 | UNLOCK(); |
| 1024 | return; |
| 1025 | } |
| 1026 | if (nf == 1) { |
| 1027 | /* Case 2. Put ao at the head of |
| 1028 | * usable_arenas. Note that because |
| 1029 | * ao->nfreepools was 0 before, ao isn't |
| 1030 | * currently on the usable_arenas list. |
| 1031 | */ |
| 1032 | ao->nextarena = usable_arenas; |
| 1033 | ao->prevarena = NULL; |
| 1034 | if (usable_arenas) |
| 1035 | usable_arenas->prevarena = ao; |
| 1036 | usable_arenas = ao; |
| 1037 | assert(usable_arenas->address != 0); |
| 1038 | |
| 1039 | UNLOCK(); |
| 1040 | return; |
| 1041 | } |
| 1042 | /* If this arena is now out of order, we need to keep |
| 1043 | * the list sorted. The list is kept sorted so that |
| 1044 | * the "most full" arenas are used first, which allows |
| 1045 | * the nearly empty arenas to be completely freed. In |
| 1046 | * a few un-scientific tests, it seems like this |
| 1047 | * approach allowed a lot more memory to be freed. |
| 1048 | */ |
| 1049 | if (ao->nextarena == NULL || |
| 1050 | nf <= ao->nextarena->nfreepools) { |
| 1051 | /* Case 4. Nothing to do. */ |
| 1052 | UNLOCK(); |
| 1053 | return; |
| 1054 | } |
| 1055 | /* Case 3: We have to move the arena towards the end |
| 1056 | * of the list, because it has more free pools than |
| 1057 | * the arena to its right. |
| 1058 | * First unlink ao from usable_arenas. |
| 1059 | */ |
| 1060 | if (ao->prevarena != NULL) { |
| 1061 | /* ao isn't at the head of the list */ |
| 1062 | assert(ao->prevarena->nextarena == ao); |
| 1063 | ao->prevarena->nextarena = ao->nextarena; |
| 1064 | } |
| 1065 | else { |
| 1066 | /* ao is at the head of the list */ |
| 1067 | assert(usable_arenas == ao); |
| 1068 | usable_arenas = ao->nextarena; |
| 1069 | } |
| 1070 | ao->nextarena->prevarena = ao->prevarena; |
| 1071 | |
| 1072 | /* Locate the new insertion point by iterating over |
| 1073 | * the list, using our nextarena pointer. |
| 1074 | */ |
| 1075 | while (ao->nextarena != NULL && |
| 1076 | nf > ao->nextarena->nfreepools) { |
| 1077 | ao->prevarena = ao->nextarena; |
| 1078 | ao->nextarena = ao->nextarena->nextarena; |
| 1079 | } |
| 1080 | |
| 1081 | /* Insert ao at this point. */ |
| 1082 | assert(ao->nextarena == NULL || |
| 1083 | ao->prevarena == ao->nextarena->prevarena); |
| 1084 | assert(ao->prevarena->nextarena == ao->nextarena); |
| 1085 | |
| 1086 | ao->prevarena->nextarena = ao; |
| 1087 | if (ao->nextarena != NULL) |
| 1088 | ao->nextarena->prevarena = ao; |
| 1089 | |
| 1090 | /* Verify that the swaps worked. */ |
| 1091 | assert(ao->nextarena == NULL || |
| 1092 | nf <= ao->nextarena->nfreepools); |
| 1093 | assert(ao->prevarena == NULL || |
| 1094 | nf > ao->prevarena->nfreepools); |
| 1095 | assert(ao->nextarena == NULL || |
| 1096 | ao->nextarena->prevarena == ao); |
| 1097 | assert((usable_arenas == ao && |
| 1098 | ao->prevarena == NULL) || |
| 1099 | ao->prevarena->nextarena == ao); |
| 1100 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 1101 | UNLOCK(); |
| 1102 | return; |
| 1103 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1104 | /* Pool was full, so doesn't currently live in any list: |
Tim Peters | 2c95c99 | 2002-03-31 02:18:01 +0000 | [diff] [blame] | 1105 | * link it to the front of the appropriate usedpools[] list. |
| 1106 | * This mimics LRU pool usage for new allocations and |
| 1107 | * targets optimal filling when several pools contain |
| 1108 | * blocks of the same size class. |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 1109 | */ |
Tim Peters | 2c95c99 | 2002-03-31 02:18:01 +0000 | [diff] [blame] | 1110 | --pool->ref.count; |
| 1111 | assert(pool->ref.count > 0); /* else the pool is empty */ |
| 1112 | size = pool->szidx; |
| 1113 | next = usedpools[size + size]; |
| 1114 | prev = next->prevpool; |
| 1115 | /* insert pool before next: prev <-> pool <-> next */ |
| 1116 | pool->nextpool = next; |
| 1117 | pool->prevpool = prev; |
| 1118 | next->prevpool = pool; |
| 1119 | prev->nextpool = pool; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 1120 | UNLOCK(); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1121 | return; |
| 1122 | } |
| 1123 | |
Tim Peters | 2c95c99 | 2002-03-31 02:18:01 +0000 | [diff] [blame] | 1124 | /* We didn't allocate this address. */ |
Tim Peters | 84c1b97 | 2002-04-04 04:44:32 +0000 | [diff] [blame] | 1125 | free(p); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
Tim Peters | 84c1b97 | 2002-04-04 04:44:32 +0000 | [diff] [blame] | 1128 | /* realloc. If p is NULL, this acts like malloc(nbytes). Else if nbytes==0, |
| 1129 | * then as the Python docs promise, we do not treat this like free(p), and |
| 1130 | * return a non-NULL result. |
| 1131 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1132 | |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1133 | #undef PyObject_Realloc |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1134 | void * |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1135 | PyObject_Realloc(void *p, size_t nbytes) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1136 | { |
Tim Peters | 84c1b97 | 2002-04-04 04:44:32 +0000 | [diff] [blame] | 1137 | void *bp; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1138 | poolp pool; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1139 | size_t size; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1140 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1141 | if (p == NULL) |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1142 | return PyObject_Malloc(nbytes); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1143 | |
Georg Brandl | d492ad8 | 2008-07-23 16:13:07 +0000 | [diff] [blame] | 1144 | /* |
| 1145 | * Limit ourselves to PY_SSIZE_T_MAX bytes to prevent security holes. |
| 1146 | * Most python internals blindly use a signed Py_ssize_t to track |
| 1147 | * things without checking for overflows or negatives. |
| 1148 | * As size_t is unsigned, checking for nbytes < 0 is not required. |
| 1149 | */ |
| 1150 | if (nbytes > PY_SSIZE_T_MAX) |
| 1151 | return NULL; |
| 1152 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 1153 | pool = POOL_ADDR(p); |
Neal Norwitz | 7eb3c91 | 2004-06-06 19:20:22 +0000 | [diff] [blame] | 1154 | if (Py_ADDRESS_IN_RANGE(p, pool)) { |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1155 | /* We're in charge of this block */ |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 1156 | size = INDEX2SIZE(pool->szidx); |
Tim Peters | 4ce71f7 | 2002-05-02 20:19:34 +0000 | [diff] [blame] | 1157 | if (nbytes <= size) { |
| 1158 | /* The block is staying the same or shrinking. If |
| 1159 | * it's shrinking, there's a tradeoff: it costs |
| 1160 | * cycles to copy the block to a smaller size class, |
| 1161 | * but it wastes memory not to copy it. The |
| 1162 | * compromise here is to copy on shrink only if at |
| 1163 | * least 25% of size can be shaved off. |
| 1164 | */ |
| 1165 | if (4 * nbytes > 3 * size) { |
| 1166 | /* It's the same, |
| 1167 | * or shrinking and new/old > 3/4. |
| 1168 | */ |
| 1169 | return p; |
| 1170 | } |
| 1171 | size = nbytes; |
| 1172 | } |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1173 | bp = PyObject_Malloc(nbytes); |
Tim Peters | 84c1b97 | 2002-04-04 04:44:32 +0000 | [diff] [blame] | 1174 | if (bp != NULL) { |
| 1175 | memcpy(bp, p, size); |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1176 | PyObject_Free(p); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1177 | } |
Tim Peters | 84c1b97 | 2002-04-04 04:44:32 +0000 | [diff] [blame] | 1178 | return bp; |
| 1179 | } |
Tim Peters | ecc6e6a | 2005-07-10 22:30:55 +0000 | [diff] [blame] | 1180 | /* We're not managing this block. If nbytes <= |
| 1181 | * SMALL_REQUEST_THRESHOLD, it's tempting to try to take over this |
| 1182 | * block. However, if we do, we need to copy the valid data from |
| 1183 | * the C-managed block to one of our blocks, and there's no portable |
| 1184 | * way to know how much of the memory space starting at p is valid. |
| 1185 | * As bug 1185883 pointed out the hard way, it's possible that the |
| 1186 | * C-managed block is "at the end" of allocated VM space, so that |
| 1187 | * a memory fault can occur if we try to copy nbytes bytes starting |
| 1188 | * at p. Instead we punt: let C continue to manage this block. |
| 1189 | */ |
| 1190 | if (nbytes) |
| 1191 | return realloc(p, nbytes); |
| 1192 | /* C doesn't define the result of realloc(p, 0) (it may or may not |
| 1193 | * return NULL then), but Python's docs promise that nbytes==0 never |
| 1194 | * returns NULL. We don't pass 0 to realloc(), to avoid that endcase |
| 1195 | * to begin with. Even then, we can't be sure that realloc() won't |
| 1196 | * return NULL. |
| 1197 | */ |
| 1198 | bp = realloc(p, 1); |
| 1199 | return bp ? bp : p; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Tim Peters | 1221c0a | 2002-03-23 00:20:15 +0000 | [diff] [blame] | 1202 | #else /* ! WITH_PYMALLOC */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1203 | |
| 1204 | /*==========================================================================*/ |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1205 | /* pymalloc not enabled: Redirect the entry points to malloc. These will |
| 1206 | * only be used by extensions that are compiled with pymalloc enabled. */ |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 1207 | |
Tim Peters | ce7fb9b | 2002-03-23 00:28:57 +0000 | [diff] [blame] | 1208 | void * |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1209 | PyObject_Malloc(size_t n) |
Tim Peters | 1221c0a | 2002-03-23 00:20:15 +0000 | [diff] [blame] | 1210 | { |
| 1211 | return PyMem_MALLOC(n); |
| 1212 | } |
| 1213 | |
Tim Peters | ce7fb9b | 2002-03-23 00:28:57 +0000 | [diff] [blame] | 1214 | void * |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1215 | PyObject_Realloc(void *p, size_t n) |
Tim Peters | 1221c0a | 2002-03-23 00:20:15 +0000 | [diff] [blame] | 1216 | { |
| 1217 | return PyMem_REALLOC(p, n); |
| 1218 | } |
| 1219 | |
| 1220 | void |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1221 | PyObject_Free(void *p) |
Tim Peters | 1221c0a | 2002-03-23 00:20:15 +0000 | [diff] [blame] | 1222 | { |
| 1223 | PyMem_FREE(p); |
| 1224 | } |
| 1225 | #endif /* WITH_PYMALLOC */ |
| 1226 | |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1227 | #ifdef PYMALLOC_DEBUG |
| 1228 | /*==========================================================================*/ |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 1229 | /* A x-platform debugging allocator. This doesn't manage memory directly, |
| 1230 | * it wraps a real allocator, adding extra debugging info to the memory blocks. |
| 1231 | */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1232 | |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1233 | /* Special bytes broadcast into debug memory blocks at appropriate times. |
| 1234 | * Strings of these are unlikely to be valid addresses, floats, ints or |
| 1235 | * 7-bit ASCII. |
| 1236 | */ |
| 1237 | #undef CLEANBYTE |
| 1238 | #undef DEADBYTE |
| 1239 | #undef FORBIDDENBYTE |
| 1240 | #define CLEANBYTE 0xCB /* clean (newly allocated) memory */ |
Tim Peters | 889f61d | 2002-07-10 19:29:49 +0000 | [diff] [blame] | 1241 | #define DEADBYTE 0xDB /* dead (newly freed) memory */ |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1242 | #define FORBIDDENBYTE 0xFB /* untouchable bytes at each end of a block */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1243 | |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1244 | /* We tag each block with an API ID in order to tag API violations */ |
| 1245 | #define _PYMALLOC_MEM_ID 'm' /* the PyMem_Malloc() API */ |
| 1246 | #define _PYMALLOC_OBJ_ID 'o' /* The PyObject_Malloc() API */ |
| 1247 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1248 | static size_t serialno = 0; /* incremented on each debug {m,re}alloc */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1249 | |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 1250 | /* serialno is always incremented via calling this routine. The point is |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1251 | * to supply a single place to set a breakpoint. |
| 1252 | */ |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 1253 | static void |
Neil Schemenauer | bd02b14 | 2002-03-28 21:05:38 +0000 | [diff] [blame] | 1254 | bumpserialno(void) |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 1255 | { |
| 1256 | ++serialno; |
| 1257 | } |
| 1258 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1259 | #define SST SIZEOF_SIZE_T |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 1260 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1261 | /* Read sizeof(size_t) bytes at p as a big-endian size_t. */ |
| 1262 | static size_t |
| 1263 | read_size_t(const void *p) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1264 | { |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 1265 | const uchar *q = (const uchar *)p; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1266 | size_t result = *q++; |
| 1267 | int i; |
| 1268 | |
| 1269 | for (i = SST; --i > 0; ++q) |
| 1270 | result = (result << 8) | *q; |
| 1271 | return result; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1274 | /* Write n as a big-endian size_t, MSB at address p, LSB at |
| 1275 | * p + sizeof(size_t) - 1. |
| 1276 | */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1277 | static void |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1278 | write_size_t(void *p, size_t n) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1279 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1280 | uchar *q = (uchar *)p + SST - 1; |
| 1281 | int i; |
| 1282 | |
| 1283 | for (i = SST; --i >= 0; --q) { |
| 1284 | *q = (uchar)(n & 0xff); |
| 1285 | n >>= 8; |
| 1286 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Tim Peters | 08d8215 | 2002-04-18 22:25:03 +0000 | [diff] [blame] | 1289 | #ifdef Py_DEBUG |
| 1290 | /* Is target in the list? The list is traversed via the nextpool pointers. |
| 1291 | * The list may be NULL-terminated, or circular. Return 1 if target is in |
| 1292 | * list, else 0. |
| 1293 | */ |
| 1294 | static int |
| 1295 | pool_is_in_list(const poolp target, poolp list) |
| 1296 | { |
| 1297 | poolp origlist = list; |
| 1298 | assert(target != NULL); |
| 1299 | if (list == NULL) |
| 1300 | return 0; |
| 1301 | do { |
| 1302 | if (target == list) |
| 1303 | return 1; |
| 1304 | list = list->nextpool; |
| 1305 | } while (list != NULL && list != origlist); |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
| 1309 | #else |
| 1310 | #define pool_is_in_list(X, Y) 1 |
| 1311 | |
| 1312 | #endif /* Py_DEBUG */ |
| 1313 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1314 | /* Let S = sizeof(size_t). The debug malloc asks for 4*S extra bytes and |
| 1315 | 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] | 1316 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1317 | p[0: S] |
| 1318 | Number of bytes originally asked for. This is a size_t, big-endian (easier |
| 1319 | to read in a memory dump). |
| 1320 | p[S: 2*S] |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1321 | Copies of FORBIDDENBYTE. Used to catch under- writes and reads. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1322 | p[2*S: 2*S+n] |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1323 | The requested memory, filled with copies of CLEANBYTE. |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1324 | Used to catch reference to uninitialized memory. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1325 | &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] | 1326 | handled the request itself. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1327 | p[2*S+n: 2*S+n+S] |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1328 | Copies of FORBIDDENBYTE. Used to catch over- writes and reads. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1329 | p[2*S+n+S: 2*S+n+2*S] |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1330 | A serial number, incremented by 1 on each call to _PyObject_DebugMalloc |
| 1331 | and _PyObject_DebugRealloc. |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1332 | This is a big-endian size_t. |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1333 | If "bad memory" is detected later, the serial number gives an |
| 1334 | excellent way to set a breakpoint on the next run, to capture the |
| 1335 | instant at which this block was passed out. |
| 1336 | */ |
| 1337 | |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1338 | /* debug replacements for the PyMem_* memory API */ |
| 1339 | void * |
| 1340 | _PyMem_DebugMalloc(size_t nbytes) |
| 1341 | { |
| 1342 | return _PyObject_DebugMallocApi(_PYMALLOC_MEM_ID, nbytes); |
| 1343 | } |
| 1344 | void * |
| 1345 | _PyMem_DebugRealloc(void *p, size_t nbytes) |
| 1346 | { |
| 1347 | return _PyObject_DebugReallocApi(_PYMALLOC_MEM_ID, p, nbytes); |
| 1348 | } |
| 1349 | void |
| 1350 | _PyMem_DebugFree(void *p) |
| 1351 | { |
| 1352 | _PyObject_DebugFreeApi(_PYMALLOC_MEM_ID, p); |
| 1353 | } |
| 1354 | |
| 1355 | /* debug replacements for the PyObject_* memory API */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1356 | void * |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1357 | _PyObject_DebugMalloc(size_t nbytes) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1358 | { |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1359 | return _PyObject_DebugMallocApi(_PYMALLOC_OBJ_ID, nbytes); |
| 1360 | } |
| 1361 | void * |
| 1362 | _PyObject_DebugRealloc(void *p, size_t nbytes) |
| 1363 | { |
| 1364 | return _PyObject_DebugReallocApi(_PYMALLOC_OBJ_ID, p, nbytes); |
| 1365 | } |
| 1366 | void |
| 1367 | _PyObject_DebugFree(void *p) |
| 1368 | { |
| 1369 | _PyObject_DebugFreeApi(_PYMALLOC_OBJ_ID, p); |
| 1370 | } |
| 1371 | void |
Kristján Valur Jónsson | 3436900 | 2009-09-28 15:57:53 +0000 | [diff] [blame] | 1372 | _PyObject_DebugCheckAddress(const void *p) |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1373 | { |
| 1374 | _PyObject_DebugCheckAddressApi(_PYMALLOC_OBJ_ID, p); |
| 1375 | } |
| 1376 | |
| 1377 | |
| 1378 | /* generic debug memory api, with an "id" to identify the API in use */ |
| 1379 | void * |
| 1380 | _PyObject_DebugMallocApi(char id, size_t nbytes) |
| 1381 | { |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1382 | uchar *p; /* base address of malloc'ed block */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1383 | uchar *tail; /* p + 2*SST + nbytes == pointer to tail pad bytes */ |
| 1384 | size_t total; /* nbytes + 4*SST */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1385 | |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 1386 | bumpserialno(); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1387 | total = nbytes + 4*SST; |
| 1388 | if (total < nbytes) |
| 1389 | /* overflow: can't represent total as a size_t */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1390 | return NULL; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1391 | |
Tim Peters | 8a8cdfd | 2002-04-12 20:49:36 +0000 | [diff] [blame] | 1392 | p = (uchar *)PyObject_Malloc(total); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1393 | if (p == NULL) |
| 1394 | return NULL; |
| 1395 | |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1396 | /* at p, write size (SST bytes), id (1 byte), pad (SST-1 bytes) */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1397 | write_size_t(p, nbytes); |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1398 | p[SST] = (uchar)id; |
| 1399 | memset(p + SST + 1 , FORBIDDENBYTE, SST-1); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1400 | |
| 1401 | if (nbytes > 0) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1402 | memset(p + 2*SST, CLEANBYTE, nbytes); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1403 | |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1404 | /* at tail, write pad (SST bytes) and serialno (SST bytes) */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1405 | tail = p + 2*SST + nbytes; |
| 1406 | memset(tail, FORBIDDENBYTE, SST); |
| 1407 | write_size_t(tail + SST, serialno); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1408 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1409 | return p + 2*SST; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1412 | /* 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] | 1413 | particular, that the FORBIDDENBYTEs with the api ID are still intact). |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1414 | Then fills the original bytes with DEADBYTE. |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1415 | Then calls the underlying free. |
| 1416 | */ |
| 1417 | void |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1418 | _PyObject_DebugFreeApi(char api, void *p) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1419 | { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1420 | uchar *q = (uchar *)p - 2*SST; /* address returned from malloc */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1421 | size_t nbytes; |
| 1422 | |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1423 | if (p == NULL) |
| 1424 | return; |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1425 | _PyObject_DebugCheckAddressApi(api, p); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1426 | nbytes = read_size_t(q); |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1427 | nbytes += 4*SST; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1428 | if (nbytes > 0) |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1429 | memset(q, DEADBYTE, nbytes); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1430 | PyObject_Free(q); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | void * |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1434 | _PyObject_DebugReallocApi(char api, void *p, size_t nbytes) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1435 | { |
| 1436 | uchar *q = (uchar *)p; |
Tim Peters | 85cc1c4 | 2002-04-12 08:52:50 +0000 | [diff] [blame] | 1437 | uchar *tail; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1438 | size_t total; /* nbytes + 4*SST */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1439 | size_t original_nbytes; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1440 | int i; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1441 | |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1442 | if (p == NULL) |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1443 | return _PyObject_DebugMallocApi(api, nbytes); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1444 | |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1445 | _PyObject_DebugCheckAddressApi(api, p); |
Tim Peters | 85cc1c4 | 2002-04-12 08:52:50 +0000 | [diff] [blame] | 1446 | bumpserialno(); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1447 | original_nbytes = read_size_t(q - 2*SST); |
| 1448 | total = nbytes + 4*SST; |
| 1449 | if (total < nbytes) |
| 1450 | /* overflow: can't represent total as a size_t */ |
Tim Peters | 85cc1c4 | 2002-04-12 08:52:50 +0000 | [diff] [blame] | 1451 | return NULL; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1452 | |
| 1453 | if (nbytes < original_nbytes) { |
Tim Peters | 85cc1c4 | 2002-04-12 08:52:50 +0000 | [diff] [blame] | 1454 | /* shrinking: mark old extra memory dead */ |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1455 | memset(q + nbytes, DEADBYTE, original_nbytes - nbytes + 2*SST); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1458 | /* Resize and add decorations. We may get a new pointer here, in which |
| 1459 | * case we didn't get the chance to mark the old memory with DEADBYTE, |
| 1460 | * but we live with that. |
| 1461 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1462 | q = (uchar *)PyObject_Realloc(q - 2*SST, total); |
Tim Peters | 85cc1c4 | 2002-04-12 08:52:50 +0000 | [diff] [blame] | 1463 | if (q == NULL) |
| 1464 | return NULL; |
| 1465 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1466 | write_size_t(q, nbytes); |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1467 | assert(q[SST] == (uchar)api); |
| 1468 | for (i = 1; i < SST; ++i) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1469 | assert(q[SST + i] == FORBIDDENBYTE); |
| 1470 | q += 2*SST; |
Tim Peters | 85cc1c4 | 2002-04-12 08:52:50 +0000 | [diff] [blame] | 1471 | tail = q + nbytes; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1472 | memset(tail, FORBIDDENBYTE, SST); |
| 1473 | write_size_t(tail + SST, serialno); |
Tim Peters | 85cc1c4 | 2002-04-12 08:52:50 +0000 | [diff] [blame] | 1474 | |
| 1475 | if (nbytes > original_nbytes) { |
| 1476 | /* growing: mark new extra memory clean */ |
| 1477 | memset(q + original_nbytes, CLEANBYTE, |
| 1478 | nbytes - original_nbytes); |
Tim Peters | 52aefc8 | 2002-04-11 06:36:45 +0000 | [diff] [blame] | 1479 | } |
Tim Peters | 85cc1c4 | 2002-04-12 08:52:50 +0000 | [diff] [blame] | 1480 | |
| 1481 | return q; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1484 | /* 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] | 1485 | * If anything is wrong, print info to stderr via _PyObject_DebugDumpAddress, |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1486 | * and call Py_FatalError to kill the program. |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1487 | * The API id, is also checked. |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1488 | */ |
| 1489 | void |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1490 | _PyObject_DebugCheckAddressApi(char api, const void *p) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1491 | { |
| 1492 | const uchar *q = (const uchar *)p; |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1493 | char msgbuf[64]; |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 1494 | char *msg; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1495 | size_t nbytes; |
Tim Peters | 449b5a8 | 2002-04-28 06:14:45 +0000 | [diff] [blame] | 1496 | const uchar *tail; |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 1497 | int i; |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1498 | char id; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1499 | |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 1500 | if (p == NULL) { |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1501 | msg = "didn't expect a NULL pointer"; |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 1502 | goto error; |
| 1503 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1504 | |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1505 | /* Check the API id */ |
| 1506 | id = (char)q[-SST]; |
| 1507 | if (id != api) { |
| 1508 | msg = msgbuf; |
| 1509 | snprintf(msg, sizeof(msgbuf), "bad ID: Allocated using API '%c', verified using API '%c'", id, api); |
| 1510 | msgbuf[sizeof(msgbuf)-1] = 0; |
| 1511 | goto error; |
| 1512 | } |
| 1513 | |
Tim Peters | 449b5a8 | 2002-04-28 06:14:45 +0000 | [diff] [blame] | 1514 | /* Check the stuff at the start of p first: if there's underwrite |
| 1515 | * corruption, the number-of-bytes field may be nuts, and checking |
| 1516 | * the tail could lead to a segfault then. |
| 1517 | */ |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1518 | for (i = SST-1; i >= 1; --i) { |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1519 | if (*(q-i) != FORBIDDENBYTE) { |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 1520 | msg = "bad leading pad byte"; |
| 1521 | goto error; |
| 1522 | } |
| 1523 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1524 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1525 | nbytes = read_size_t(q - 2*SST); |
Tim Peters | 449b5a8 | 2002-04-28 06:14:45 +0000 | [diff] [blame] | 1526 | tail = q + nbytes; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1527 | for (i = 0; i < SST; ++i) { |
Tim Peters | 449b5a8 | 2002-04-28 06:14:45 +0000 | [diff] [blame] | 1528 | if (tail[i] != FORBIDDENBYTE) { |
| 1529 | msg = "bad trailing pad byte"; |
| 1530 | goto error; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1531 | } |
| 1532 | } |
| 1533 | |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 1534 | return; |
| 1535 | |
| 1536 | error: |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1537 | _PyObject_DebugDumpAddress(p); |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 1538 | Py_FatalError(msg); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1541 | /* Display info to stderr about the memory block at p. */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1542 | void |
Neil Schemenauer | d2560cd | 2002-04-12 03:10:20 +0000 | [diff] [blame] | 1543 | _PyObject_DebugDumpAddress(const void *p) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1544 | { |
| 1545 | const uchar *q = (const uchar *)p; |
| 1546 | const uchar *tail; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1547 | size_t nbytes, serial; |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 1548 | int i; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1549 | int ok; |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1550 | char id; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1551 | |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1552 | fprintf(stderr, "Debug memory block at address p=%p:", p); |
| 1553 | if (p == NULL) { |
| 1554 | fprintf(stderr, "\n"); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1555 | return; |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1556 | } |
| 1557 | id = (char)q[-SST]; |
| 1558 | fprintf(stderr, " API '%c'\n", id); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1559 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1560 | nbytes = read_size_t(q - 2*SST); |
| 1561 | fprintf(stderr, " %" PY_FORMAT_SIZE_T "u bytes originally " |
| 1562 | "requested\n", nbytes); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1563 | |
Tim Peters | 449b5a8 | 2002-04-28 06:14:45 +0000 | [diff] [blame] | 1564 | /* In case this is nuts, check the leading pad bytes first. */ |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1565 | fprintf(stderr, " The %d pad bytes at p-%d are ", SST-1, SST-1); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1566 | ok = 1; |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1567 | for (i = 1; i <= SST-1; ++i) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1568 | if (*(q-i) != FORBIDDENBYTE) { |
| 1569 | ok = 0; |
| 1570 | break; |
| 1571 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1572 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1573 | if (ok) |
| 1574 | fputs("FORBIDDENBYTE, as expected.\n", stderr); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1575 | else { |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1576 | fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n", |
| 1577 | FORBIDDENBYTE); |
Kristján Valur Jónsson | ae4cfb1 | 2009-09-28 13:45:02 +0000 | [diff] [blame] | 1578 | for (i = SST-1; i >= 1; --i) { |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1579 | const uchar byte = *(q-i); |
| 1580 | fprintf(stderr, " at p-%d: 0x%02x", i, byte); |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1581 | if (byte != FORBIDDENBYTE) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1582 | fputs(" *** OUCH", stderr); |
| 1583 | fputc('\n', stderr); |
| 1584 | } |
Tim Peters | 449b5a8 | 2002-04-28 06:14:45 +0000 | [diff] [blame] | 1585 | |
| 1586 | fputs(" Because memory is corrupted at the start, the " |
| 1587 | "count of bytes requested\n" |
| 1588 | " may be bogus, and checking the trailing pad " |
| 1589 | "bytes may segfault.\n", stderr); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | tail = q + nbytes; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1593 | fprintf(stderr, " The %d pad bytes at tail=%p are ", SST, tail); |
| 1594 | ok = 1; |
| 1595 | for (i = 0; i < SST; ++i) { |
| 1596 | if (tail[i] != FORBIDDENBYTE) { |
| 1597 | ok = 0; |
| 1598 | break; |
| 1599 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1600 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1601 | if (ok) |
| 1602 | fputs("FORBIDDENBYTE, as expected.\n", stderr); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1603 | else { |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1604 | fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n", |
| 1605 | FORBIDDENBYTE); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1606 | for (i = 0; i < SST; ++i) { |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1607 | const uchar byte = tail[i]; |
| 1608 | fprintf(stderr, " at tail+%d: 0x%02x", |
| 1609 | i, byte); |
Tim Peters | f6fb501 | 2002-04-12 07:38:53 +0000 | [diff] [blame] | 1610 | if (byte != FORBIDDENBYTE) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1611 | fputs(" *** OUCH", stderr); |
| 1612 | fputc('\n', stderr); |
| 1613 | } |
| 1614 | } |
| 1615 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1616 | serial = read_size_t(tail + SST); |
| 1617 | fprintf(stderr, " The block was made by call #%" PY_FORMAT_SIZE_T |
| 1618 | "u to debug malloc/realloc.\n", serial); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1619 | |
| 1620 | if (nbytes > 0) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1621 | i = 0; |
Tim Peters | 449b5a8 | 2002-04-28 06:14:45 +0000 | [diff] [blame] | 1622 | fputs(" Data at p:", stderr); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1623 | /* print up to 8 bytes at the start */ |
| 1624 | while (q < tail && i < 8) { |
| 1625 | fprintf(stderr, " %02x", *q); |
| 1626 | ++i; |
| 1627 | ++q; |
| 1628 | } |
| 1629 | /* and up to 8 at the end */ |
| 1630 | if (q < tail) { |
| 1631 | if (tail - q > 8) { |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 1632 | fputs(" ...", stderr); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1633 | q = tail - 8; |
| 1634 | } |
| 1635 | while (q < tail) { |
| 1636 | fprintf(stderr, " %02x", *q); |
| 1637 | ++q; |
| 1638 | } |
| 1639 | } |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 1640 | fputc('\n', stderr); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1641 | } |
| 1642 | } |
| 1643 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1644 | static size_t |
| 1645 | printone(const char* msg, size_t value) |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1646 | { |
Tim Peters | 49f2681 | 2002-04-06 01:45:35 +0000 | [diff] [blame] | 1647 | int i, k; |
| 1648 | char buf[100]; |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1649 | size_t origvalue = value; |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1650 | |
| 1651 | fputs(msg, stderr); |
Tim Peters | 49f2681 | 2002-04-06 01:45:35 +0000 | [diff] [blame] | 1652 | for (i = (int)strlen(msg); i < 35; ++i) |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1653 | fputc(' ', stderr); |
Tim Peters | 49f2681 | 2002-04-06 01:45:35 +0000 | [diff] [blame] | 1654 | fputc('=', stderr); |
| 1655 | |
| 1656 | /* Write the value with commas. */ |
| 1657 | i = 22; |
| 1658 | buf[i--] = '\0'; |
| 1659 | buf[i--] = '\n'; |
| 1660 | k = 3; |
| 1661 | do { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1662 | size_t nextvalue = value / 10; |
| 1663 | uint digit = (uint)(value - nextvalue * 10); |
Tim Peters | 49f2681 | 2002-04-06 01:45:35 +0000 | [diff] [blame] | 1664 | value = nextvalue; |
| 1665 | buf[i--] = (char)(digit + '0'); |
| 1666 | --k; |
| 1667 | if (k == 0 && value && i >= 0) { |
| 1668 | k = 3; |
| 1669 | buf[i--] = ','; |
| 1670 | } |
| 1671 | } while (value && i >= 0); |
| 1672 | |
| 1673 | while (i >= 0) |
| 1674 | buf[i--] = ' '; |
| 1675 | fputs(buf, stderr); |
| 1676 | |
| 1677 | return origvalue; |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
Tim Peters | 08d8215 | 2002-04-18 22:25:03 +0000 | [diff] [blame] | 1680 | /* Print summary info to stderr about the state of pymalloc's structures. |
| 1681 | * In Py_DEBUG mode, also perform some expensive internal consistency |
| 1682 | * checks. |
| 1683 | */ |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1684 | void |
Tim Peters | 0e87118 | 2002-04-13 08:29:14 +0000 | [diff] [blame] | 1685 | _PyObject_DebugMallocStats(void) |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1686 | { |
| 1687 | uint i; |
| 1688 | const uint numclasses = SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT; |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1689 | /* # of pools, allocated blocks, and free blocks per class index */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1690 | size_t numpools[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT]; |
| 1691 | size_t numblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT]; |
| 1692 | size_t numfreeblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT]; |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1693 | /* total # of allocated bytes in used and full pools */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1694 | size_t allocated_bytes = 0; |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1695 | /* total # of available bytes in used pools */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1696 | size_t available_bytes = 0; |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1697 | /* # of free pools + pools not yet carved out of current arena */ |
| 1698 | uint numfreepools = 0; |
| 1699 | /* # of bytes for arena alignment padding */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1700 | size_t arena_alignment = 0; |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1701 | /* # of bytes in used and full pools used for pool_headers */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1702 | size_t pool_header_bytes = 0; |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1703 | /* # of bytes in used and full pools wasted due to quantization, |
| 1704 | * i.e. the necessarily leftover space at the ends of used and |
| 1705 | * full pools. |
| 1706 | */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1707 | size_t quantization = 0; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1708 | /* # of arenas actually allocated. */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1709 | size_t narenas = 0; |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1710 | /* running total -- should equal narenas * ARENA_SIZE */ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1711 | size_t total; |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1712 | char buf[128]; |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1713 | |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1714 | fprintf(stderr, "Small block threshold = %d, in %u size classes.\n", |
| 1715 | SMALL_REQUEST_THRESHOLD, numclasses); |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1716 | |
| 1717 | for (i = 0; i < numclasses; ++i) |
| 1718 | numpools[i] = numblocks[i] = numfreeblocks[i] = 0; |
| 1719 | |
Tim Peters | 6169f09 | 2002-04-01 20:12:59 +0000 | [diff] [blame] | 1720 | /* Because full pools aren't linked to from anything, it's easiest |
| 1721 | * to march over all the arenas. If we're lucky, most of the memory |
| 1722 | * will be living in full pools -- would be a shame to miss them. |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1723 | */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1724 | for (i = 0; i < maxarenas; ++i) { |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1725 | uint poolsinarena; |
| 1726 | uint j; |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1727 | uptr base = arenas[i].address; |
| 1728 | |
| 1729 | /* Skip arenas which are not allocated. */ |
| 1730 | if (arenas[i].address == (uptr)NULL) |
| 1731 | continue; |
| 1732 | narenas += 1; |
| 1733 | |
| 1734 | poolsinarena = arenas[i].ntotalpools; |
| 1735 | numfreepools += arenas[i].nfreepools; |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1736 | |
| 1737 | /* round up to pool alignment */ |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1738 | if (base & (uptr)POOL_SIZE_MASK) { |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1739 | arena_alignment += POOL_SIZE; |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1740 | base &= ~(uptr)POOL_SIZE_MASK; |
| 1741 | base += POOL_SIZE; |
| 1742 | } |
| 1743 | |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1744 | /* visit every pool in the arena */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1745 | assert(base <= (uptr) arenas[i].pool_address); |
| 1746 | for (j = 0; |
| 1747 | base < (uptr) arenas[i].pool_address; |
| 1748 | ++j, base += POOL_SIZE) { |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1749 | poolp p = (poolp)base; |
Tim Peters | 08d8215 | 2002-04-18 22:25:03 +0000 | [diff] [blame] | 1750 | const uint sz = p->szidx; |
| 1751 | uint freeblocks; |
| 1752 | |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1753 | if (p->ref.count == 0) { |
| 1754 | /* currently unused */ |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1755 | assert(pool_is_in_list(p, arenas[i].freepools)); |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1756 | continue; |
| 1757 | } |
Tim Peters | 08d8215 | 2002-04-18 22:25:03 +0000 | [diff] [blame] | 1758 | ++numpools[sz]; |
| 1759 | numblocks[sz] += p->ref.count; |
| 1760 | freeblocks = NUMBLOCKS(sz) - p->ref.count; |
| 1761 | numfreeblocks[sz] += freeblocks; |
| 1762 | #ifdef Py_DEBUG |
| 1763 | if (freeblocks > 0) |
| 1764 | assert(pool_is_in_list(p, usedpools[sz + sz])); |
| 1765 | #endif |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1766 | } |
| 1767 | } |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1768 | assert(narenas == narenas_currently_allocated); |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1769 | |
| 1770 | fputc('\n', stderr); |
Tim Peters | 49f2681 | 2002-04-06 01:45:35 +0000 | [diff] [blame] | 1771 | fputs("class size num pools blocks in use avail blocks\n" |
| 1772 | "----- ---- --------- ------------- ------------\n", |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1773 | stderr); |
| 1774 | |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1775 | for (i = 0; i < numclasses; ++i) { |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1776 | size_t p = numpools[i]; |
| 1777 | size_t b = numblocks[i]; |
| 1778 | size_t f = numfreeblocks[i]; |
Tim Peters | e70ddf3 | 2002-04-05 04:32:29 +0000 | [diff] [blame] | 1779 | uint size = INDEX2SIZE(i); |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1780 | if (p == 0) { |
| 1781 | assert(b == 0 && f == 0); |
| 1782 | continue; |
| 1783 | } |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1784 | fprintf(stderr, "%5u %6u " |
| 1785 | "%11" PY_FORMAT_SIZE_T "u " |
| 1786 | "%15" PY_FORMAT_SIZE_T "u " |
| 1787 | "%13" PY_FORMAT_SIZE_T "u\n", |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1788 | i, size, p, b, f); |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1789 | allocated_bytes += b * size; |
| 1790 | available_bytes += f * size; |
| 1791 | pool_header_bytes += p * POOL_OVERHEAD; |
| 1792 | quantization += p * ((POOL_SIZE - POOL_OVERHEAD) % size); |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1793 | } |
| 1794 | fputc('\n', stderr); |
Tim Peters | 0e87118 | 2002-04-13 08:29:14 +0000 | [diff] [blame] | 1795 | (void)printone("# times object malloc called", serialno); |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1796 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1797 | (void)printone("# arenas allocated total", ntimes_arena_allocated); |
| 1798 | (void)printone("# arenas reclaimed", ntimes_arena_allocated - narenas); |
| 1799 | (void)printone("# arenas highwater mark", narenas_highwater); |
| 1800 | (void)printone("# arenas allocated current", narenas); |
| 1801 | |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1802 | PyOS_snprintf(buf, sizeof(buf), |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1803 | "%" PY_FORMAT_SIZE_T "u arenas * %d bytes/arena", |
| 1804 | narenas, ARENA_SIZE); |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1805 | (void)printone(buf, narenas * ARENA_SIZE); |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1806 | |
| 1807 | fputc('\n', stderr); |
| 1808 | |
Tim Peters | 49f2681 | 2002-04-06 01:45:35 +0000 | [diff] [blame] | 1809 | total = printone("# bytes in allocated blocks", allocated_bytes); |
Tim Peters | 0e87118 | 2002-04-13 08:29:14 +0000 | [diff] [blame] | 1810 | total += printone("# bytes in available blocks", available_bytes); |
Tim Peters | 49f2681 | 2002-04-06 01:45:35 +0000 | [diff] [blame] | 1811 | |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1812 | PyOS_snprintf(buf, sizeof(buf), |
| 1813 | "%u unused pools * %d bytes", numfreepools, POOL_SIZE); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 1814 | total += printone(buf, (size_t)numfreepools * POOL_SIZE); |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1815 | |
Tim Peters | 16bcb6b | 2002-04-05 05:45:31 +0000 | [diff] [blame] | 1816 | total += printone("# bytes lost to pool headers", pool_header_bytes); |
| 1817 | total += printone("# bytes lost to quantization", quantization); |
| 1818 | total += printone("# bytes lost to arena alignment", arena_alignment); |
| 1819 | (void)printone("Total", total); |
Tim Peters | 7ccfadf | 2002-04-01 06:04:21 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1822 | #endif /* PYMALLOC_DEBUG */ |
Neal Norwitz | 7eb3c91 | 2004-06-06 19:20:22 +0000 | [diff] [blame] | 1823 | |
| 1824 | #ifdef Py_USING_MEMORY_DEBUGGER |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1825 | /* Make this function last so gcc won't inline it since the definition is |
| 1826 | * after the reference. |
| 1827 | */ |
Neal Norwitz | 7eb3c91 | 2004-06-06 19:20:22 +0000 | [diff] [blame] | 1828 | int |
| 1829 | Py_ADDRESS_IN_RANGE(void *P, poolp pool) |
| 1830 | { |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1831 | return pool->arenaindex < maxarenas && |
| 1832 | (uptr)P - arenas[pool->arenaindex].address < (uptr)ARENA_SIZE && |
| 1833 | arenas[pool->arenaindex].address != 0; |
Neal Norwitz | 7eb3c91 | 2004-06-06 19:20:22 +0000 | [diff] [blame] | 1834 | } |
| 1835 | #endif |