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 | |
| 119 | /* |
| 120 | * Max size threshold below which malloc requests are considered to be |
| 121 | * small enough in order to use preallocated memory pools. You can tune |
| 122 | * this value according to your application behaviour and memory needs. |
| 123 | * |
| 124 | * The following invariants must hold: |
| 125 | * 1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 256 |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 126 | * 2) SMALL_REQUEST_THRESHOLD is evenly divisible by ALIGNMENT |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 127 | * |
| 128 | * Although not required, for better performance and space efficiency, |
| 129 | * it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2. |
| 130 | */ |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 131 | #define SMALL_REQUEST_THRESHOLD 256 |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 132 | #define NB_SMALL_SIZE_CLASSES (SMALL_REQUEST_THRESHOLD / ALIGNMENT) |
| 133 | |
| 134 | /* |
| 135 | * The system's VMM page size can be obtained on most unices with a |
| 136 | * getpagesize() call or deduced from various header files. To make |
| 137 | * things simpler, we assume that it is 4K, which is OK for most systems. |
| 138 | * It is probably better if this is the native page size, but it doesn't |
| 139 | * have to be. |
| 140 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 141 | #define SYSTEM_PAGE_SIZE (4 * 1024) |
| 142 | #define SYSTEM_PAGE_SIZE_MASK (SYSTEM_PAGE_SIZE - 1) |
| 143 | |
| 144 | /* |
| 145 | * Maximum amount of memory managed by the allocator for small requests. |
| 146 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 147 | #ifdef WITH_MEMORY_LIMITS |
| 148 | #ifndef SMALL_MEMORY_LIMIT |
| 149 | #define SMALL_MEMORY_LIMIT (64 * 1024 * 1024) /* 64 MB -- more? */ |
| 150 | #endif |
| 151 | #endif |
| 152 | |
| 153 | /* |
| 154 | * The allocator sub-allocates <Big> blocks of memory (called arenas) aligned |
| 155 | * on a page boundary. This is a reserved virtual address space for the |
| 156 | * current process (obtained through a malloc call). In no way this means |
| 157 | * that the memory arenas will be used entirely. A malloc(<Big>) is usually |
| 158 | * an address range reservation for <Big> bytes, unless all pages within this |
| 159 | * space are referenced subsequently. So malloc'ing big blocks and not using |
| 160 | * them does not mean "wasting memory". It's an addressable range wastage... |
| 161 | * |
| 162 | * Therefore, allocating arenas with malloc is not optimal, because there is |
| 163 | * 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] | 164 | * memory from the system across various platforms. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 165 | */ |
Tim Peters | 3c83df2 | 2002-03-30 07:04:41 +0000 | [diff] [blame] | 166 | #define ARENA_SIZE (256 << 10) /* 256KB */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 167 | |
| 168 | #ifdef WITH_MEMORY_LIMITS |
| 169 | #define MAX_ARENAS (SMALL_MEMORY_LIMIT / ARENA_SIZE) |
| 170 | #endif |
| 171 | |
| 172 | /* |
| 173 | * 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] | 174 | * between 1K and SYSTEM_PAGE_SIZE, that is: 1k, 2k, 4k. |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 175 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 176 | #define POOL_SIZE SYSTEM_PAGE_SIZE /* must be 2^N */ |
| 177 | #define POOL_SIZE_MASK SYSTEM_PAGE_SIZE_MASK |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * -- End of tunable settings section -- |
| 181 | */ |
| 182 | |
| 183 | /*==========================================================================*/ |
| 184 | |
| 185 | /* |
| 186 | * Locking |
| 187 | * |
| 188 | * To reduce lock contention, it would probably be better to refine the |
| 189 | * crude function locking with per size class locking. I'm not positive |
| 190 | * however, whether it's worth switching to such locking policy because |
| 191 | * of the performance penalty it might introduce. |
| 192 | * |
| 193 | * The following macros describe the simplest (should also be the fastest) |
| 194 | * lock object on a particular platform and the init/fini/lock/unlock |
| 195 | * operations on it. The locks defined here are not expected to be recursive |
| 196 | * because it is assumed that they will always be called in the order: |
| 197 | * INIT, [LOCK, UNLOCK]*, FINI. |
| 198 | */ |
| 199 | |
| 200 | /* |
| 201 | * Python's threads are serialized, so object malloc locking is disabled. |
| 202 | */ |
| 203 | #define SIMPLELOCK_DECL(lock) /* simple lock declaration */ |
| 204 | #define SIMPLELOCK_INIT(lock) /* allocate (if needed) and initialize */ |
| 205 | #define SIMPLELOCK_FINI(lock) /* free/destroy an existing lock */ |
| 206 | #define SIMPLELOCK_LOCK(lock) /* acquire released lock */ |
| 207 | #define SIMPLELOCK_UNLOCK(lock) /* release acquired lock */ |
| 208 | |
| 209 | /* |
| 210 | * Basic types |
| 211 | * I don't care if these are defined in <sys/types.h> or elsewhere. Axiom. |
| 212 | */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 213 | #undef uchar |
| 214 | #define uchar unsigned char /* assuming == 8 bits */ |
| 215 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 216 | #undef uint |
| 217 | #define uint unsigned int /* assuming >= 16 bits */ |
| 218 | |
| 219 | #undef ulong |
| 220 | #define ulong unsigned long /* assuming >= 32 bits */ |
| 221 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 222 | #undef uptr |
| 223 | #define uptr Py_uintptr_t |
| 224 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 225 | /* When you say memory, my mind reasons in terms of (pointers to) blocks */ |
| 226 | typedef uchar block; |
| 227 | |
| 228 | /* Pool for small blocks */ |
| 229 | struct pool_header { |
Tim Peters | b233652 | 2001-03-11 18:36:13 +0000 | [diff] [blame] | 230 | union { block *_padding; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 231 | uint count; } ref; /* number of allocated blocks */ |
| 232 | block *freeblock; /* pool's free list head */ |
| 233 | struct pool_header *nextpool; /* next pool of this size class */ |
| 234 | struct pool_header *prevpool; /* previous pool "" */ |
Tim Peters | 1d99af8 | 2002-03-30 10:35:09 +0000 | [diff] [blame] | 235 | uint arenaindex; /* index into arenas of base adr */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 236 | uint szidx; /* block size class index */ |
| 237 | uint capacity; /* pool capacity in # of blocks */ |
| 238 | }; |
| 239 | |
| 240 | typedef struct pool_header *poolp; |
| 241 | |
| 242 | #undef ROUNDUP |
| 243 | #define ROUNDUP(x) (((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK) |
| 244 | #define POOL_OVERHEAD ROUNDUP(sizeof(struct pool_header)) |
| 245 | |
| 246 | #define DUMMY_SIZE_IDX 0xffff /* size class of newly cached pools */ |
| 247 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 248 | /* Round pointer P down to the closest pool-aligned address <= P, as a poolp */ |
| 249 | #define POOL_ADDR(P) \ |
| 250 | ((poolp)((uptr)(P) & ~(uptr)POOL_SIZE_MASK)) |
| 251 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 252 | /*==========================================================================*/ |
| 253 | |
| 254 | /* |
| 255 | * This malloc lock |
| 256 | */ |
Tim Peters | b233652 | 2001-03-11 18:36:13 +0000 | [diff] [blame] | 257 | SIMPLELOCK_DECL(_malloc_lock); |
| 258 | #define LOCK() SIMPLELOCK_LOCK(_malloc_lock) |
| 259 | #define UNLOCK() SIMPLELOCK_UNLOCK(_malloc_lock) |
| 260 | #define LOCK_INIT() SIMPLELOCK_INIT(_malloc_lock) |
| 261 | #define LOCK_FINI() SIMPLELOCK_FINI(_malloc_lock) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 262 | |
| 263 | /* |
| 264 | * Pool table -- doubly linked lists of partially used pools |
| 265 | */ |
| 266 | #define PTA(x) ((poolp )((uchar *)&(usedpools[2*(x)]) - 2*sizeof(block *))) |
| 267 | #define PT(x) PTA(x), PTA(x) |
| 268 | |
| 269 | static poolp usedpools[2 * ((NB_SMALL_SIZE_CLASSES + 7) / 8) * 8] = { |
| 270 | PT(0), PT(1), PT(2), PT(3), PT(4), PT(5), PT(6), PT(7) |
| 271 | #if NB_SMALL_SIZE_CLASSES > 8 |
| 272 | , PT(8), PT(9), PT(10), PT(11), PT(12), PT(13), PT(14), PT(15) |
| 273 | #if NB_SMALL_SIZE_CLASSES > 16 |
| 274 | , PT(16), PT(17), PT(18), PT(19), PT(20), PT(21), PT(22), PT(23) |
| 275 | #if NB_SMALL_SIZE_CLASSES > 24 |
| 276 | , PT(24), PT(25), PT(26), PT(27), PT(28), PT(29), PT(30), PT(31) |
| 277 | #if NB_SMALL_SIZE_CLASSES > 32 |
| 278 | , PT(32), PT(33), PT(34), PT(35), PT(36), PT(37), PT(38), PT(39) |
| 279 | #if NB_SMALL_SIZE_CLASSES > 40 |
| 280 | , PT(40), PT(41), PT(42), PT(43), PT(44), PT(45), PT(46), PT(47) |
| 281 | #if NB_SMALL_SIZE_CLASSES > 48 |
| 282 | , PT(48), PT(49), PT(50), PT(51), PT(52), PT(53), PT(54), PT(55) |
| 283 | #if NB_SMALL_SIZE_CLASSES > 56 |
| 284 | , PT(56), PT(57), PT(58), PT(59), PT(60), PT(61), PT(62), PT(63) |
| 285 | #endif /* NB_SMALL_SIZE_CLASSES > 56 */ |
| 286 | #endif /* NB_SMALL_SIZE_CLASSES > 48 */ |
| 287 | #endif /* NB_SMALL_SIZE_CLASSES > 40 */ |
| 288 | #endif /* NB_SMALL_SIZE_CLASSES > 32 */ |
| 289 | #endif /* NB_SMALL_SIZE_CLASSES > 24 */ |
| 290 | #endif /* NB_SMALL_SIZE_CLASSES > 16 */ |
| 291 | #endif /* NB_SMALL_SIZE_CLASSES > 8 */ |
| 292 | }; |
| 293 | |
| 294 | /* |
| 295 | * Free (cached) pools |
| 296 | */ |
| 297 | static poolp freepools = NULL; /* free list for cached pools */ |
| 298 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 299 | /*==========================================================================*/ |
| 300 | /* Arena management. */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 301 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 302 | /* arenas is a vector of arena base addresses, in order of allocation time. |
| 303 | * arenas currently contains narenas entries, and has space allocated |
| 304 | * for at most maxarenas entries. |
| 305 | * |
| 306 | * CAUTION: See the long comment block about thread safety in new_arena(): |
| 307 | * the code currently relies in deep ways on that this vector only grows, |
| 308 | * and only grows by appending at the end. For now we never return an arena |
| 309 | * to the OS. |
| 310 | */ |
Tim Peters | c2ce91a | 2002-03-30 21:36:04 +0000 | [diff] [blame] | 311 | static uptr *volatile arenas = NULL; /* the pointer itself is volatile */ |
| 312 | static volatile uint narenas = 0; |
Tim Peters | 1d99af8 | 2002-03-30 10:35:09 +0000 | [diff] [blame] | 313 | static uint maxarenas = 0; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 314 | |
Tim Peters | 3c83df2 | 2002-03-30 07:04:41 +0000 | [diff] [blame] | 315 | /* Number of pools still available to be allocated in the current arena. */ |
| 316 | static uint nfreepools = 0; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 317 | |
Tim Peters | 3c83df2 | 2002-03-30 07:04:41 +0000 | [diff] [blame] | 318 | /* Free space start address in current arena. This is pool-aligned. */ |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 319 | static block *arenabase = NULL; |
| 320 | |
| 321 | #if 0 |
| 322 | static ulong wasmine = 0; |
| 323 | static ulong wasntmine = 0; |
| 324 | |
| 325 | static void |
| 326 | dumpem(void *ptr) |
| 327 | { |
| 328 | if (ptr) |
| 329 | printf("inserted new arena at %08x\n", ptr); |
Tim Peters | 1d99af8 | 2002-03-30 10:35:09 +0000 | [diff] [blame] | 330 | printf("# arenas %u\n", narenas); |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 331 | printf("was mine %lu wasn't mine %lu\n", wasmine, wasntmine); |
| 332 | } |
| 333 | #define INCMINE ++wasmine |
| 334 | #define INCTHEIRS ++wasntmine |
| 335 | |
| 336 | #else |
| 337 | #define dumpem(ptr) |
| 338 | #define INCMINE |
| 339 | #define INCTHEIRS |
| 340 | #endif |
| 341 | |
| 342 | /* Allocate a new arena and return its base address. If we run out of |
| 343 | * memory, return NULL. |
| 344 | */ |
| 345 | static block * |
| 346 | new_arena(void) |
| 347 | { |
Tim Peters | 3c83df2 | 2002-03-30 07:04:41 +0000 | [diff] [blame] | 348 | uint excess; /* number of bytes above pool alignment */ |
| 349 | block *bp = (block *)PyMem_MALLOC(ARENA_SIZE); |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 350 | if (bp == NULL) |
| 351 | return NULL; |
| 352 | |
Tim Peters | 3c83df2 | 2002-03-30 07:04:41 +0000 | [diff] [blame] | 353 | /* arenabase <- first pool-aligned address in the arena |
| 354 | nfreepools <- number of whole pools that fit after alignment */ |
| 355 | arenabase = bp; |
| 356 | nfreepools = ARENA_SIZE / POOL_SIZE; |
Tim Peters | c2ce91a | 2002-03-30 21:36:04 +0000 | [diff] [blame] | 357 | assert(POOL_SIZE * nfreepools == ARENA_SIZE); |
Tim Peters | 3c83df2 | 2002-03-30 07:04:41 +0000 | [diff] [blame] | 358 | excess = (uint)bp & POOL_SIZE_MASK; |
| 359 | if (excess != 0) { |
| 360 | --nfreepools; |
| 361 | arenabase += POOL_SIZE - excess; |
| 362 | } |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 363 | |
| 364 | /* Make room for a new entry in the arenas vector. */ |
| 365 | if (arenas == NULL) { |
Tim Peters | c2ce91a | 2002-03-30 21:36:04 +0000 | [diff] [blame] | 366 | assert(narenas == 0 && maxarenas == 0); |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 367 | arenas = (uptr *)PyMem_MALLOC(16 * sizeof(*arenas)); |
| 368 | if (arenas == NULL) |
| 369 | goto error; |
| 370 | maxarenas = 16; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 371 | } |
| 372 | else if (narenas == maxarenas) { |
| 373 | /* Grow arenas. Don't use realloc: if this fails, we |
| 374 | * don't want to lose the base addresses we already have. |
Tim Peters | c2ce91a | 2002-03-30 21:36:04 +0000 | [diff] [blame] | 375 | * |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 376 | * Exceedingly subtle: Someone may be calling the pymalloc |
| 377 | * free via PyMem_{DEL, Del, FREE, Free} without holding the |
| 378 | *.GIL. Someone else may simultaneously be calling the |
| 379 | * pymalloc malloc while holding the GIL via, e.g., |
| 380 | * PyObject_New. Now the pymalloc free may index into arenas |
| 381 | * for an address check, while the pymalloc malloc calls |
| 382 | * new_arena and we end up here to grow a new arena *and* |
| 383 | * grow the arenas vector. If the value for arenas pymalloc |
| 384 | * free picks up "vanishes" during this resize, anything may |
| 385 | * happen, and it would be an incredibly rare bug. Therefore |
| 386 | * the code here takes great pains to make sure that, at every |
| 387 | * moment, arenas always points to an intact vector of |
| 388 | * addresses. It doesn't matter whether arenas points to a |
| 389 | * wholly up-to-date vector when pymalloc free checks it in |
| 390 | * this case, because the only legal (and that even this is |
| 391 | * legal is debatable) way to call PyMem_{Del, etc} while not |
| 392 | * holding the GIL is if the memory being released is not |
| 393 | * object memory, i.e. if the address check in pymalloc free |
| 394 | * is supposed to fail. Having an incomplete vector can't |
| 395 | * make a supposed-to-fail case succeed by mistake (it could |
| 396 | * only make a supposed-to-succeed case fail by mistake). |
Tim Peters | c2ce91a | 2002-03-30 21:36:04 +0000 | [diff] [blame] | 397 | * |
| 398 | * In addition, without a lock we can't know for sure when |
| 399 | * an old vector is no longer referenced, so we simply let |
| 400 | * old vectors leak. |
| 401 | * |
| 402 | * And on top of that, since narenas and arenas can't be |
| 403 | * changed as-a-pair atomically without a lock, we're also |
| 404 | * careful to declare them volatile and ensure that we change |
| 405 | * arenas first. This prevents another thread from picking |
| 406 | * up an narenas value too large for the arenas value it |
| 407 | * reads up (arenas never shrinks). |
| 408 | * |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 409 | * Read the above 50 times before changing anything in this |
| 410 | * block. |
| 411 | */ |
Tim Peters | 1d99af8 | 2002-03-30 10:35:09 +0000 | [diff] [blame] | 412 | uptr *p; |
Tim Peters | c2ce91a | 2002-03-30 21:36:04 +0000 | [diff] [blame] | 413 | uint newmax = maxarenas << 1; |
Tim Peters | 1d99af8 | 2002-03-30 10:35:09 +0000 | [diff] [blame] | 414 | if (newmax <= maxarenas) /* overflow */ |
| 415 | goto error; |
| 416 | p = (uptr *)PyMem_MALLOC(newmax * sizeof(*arenas)); |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 417 | if (p == NULL) |
| 418 | goto error; |
| 419 | memcpy(p, arenas, narenas * sizeof(*arenas)); |
Tim Peters | c2ce91a | 2002-03-30 21:36:04 +0000 | [diff] [blame] | 420 | arenas = p; /* old arenas deliberately leaked */ |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 421 | maxarenas = newmax; |
| 422 | } |
| 423 | |
| 424 | /* Append the new arena address to arenas. */ |
| 425 | assert(narenas < maxarenas); |
| 426 | arenas[narenas] = (uptr)bp; |
Tim Peters | 1d99af8 | 2002-03-30 10:35:09 +0000 | [diff] [blame] | 427 | ++narenas; /* can't overflow, since narenas < maxarenas before */ |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 428 | dumpem(bp); |
| 429 | return bp; |
| 430 | |
| 431 | error: |
| 432 | PyMem_FREE(bp); |
Tim Peters | 7b85b4a | 2002-03-30 10:42:09 +0000 | [diff] [blame] | 433 | nfreepools = 0; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 434 | return NULL; |
| 435 | } |
| 436 | |
| 437 | /* Return true if and only if P is an address that was allocated by |
| 438 | * pymalloc. I must be the index into arenas that the address claims |
| 439 | * to come from. |
Tim Peters | c2ce91a | 2002-03-30 21:36:04 +0000 | [diff] [blame] | 440 | * |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 441 | * Tricky: Letting B be the arena base address in arenas[I], P belongs to the |
| 442 | * arena if and only if |
Tim Peters | 3c83df2 | 2002-03-30 07:04:41 +0000 | [diff] [blame] | 443 | * B <= P < B + ARENA_SIZE |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 444 | * Subtracting B throughout, this is true iff |
Tim Peters | 3c83df2 | 2002-03-30 07:04:41 +0000 | [diff] [blame] | 445 | * 0 <= P-B < ARENA_SIZE |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 446 | * By using unsigned arithmetic, the "0 <=" half of the test can be skipped. |
Tim Peters | c2ce91a | 2002-03-30 21:36:04 +0000 | [diff] [blame] | 447 | * |
| 448 | * Obscure: A PyMem "free memory" function can call the pymalloc free or |
| 449 | * realloc before the first arena has been allocated. arenas is still |
| 450 | * NULL in that case. We're relying on that narenas is also 0 in that case, |
| 451 | * so the (I) < narenas must be false, saving us from trying to index into |
| 452 | * a NULL arenas. |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 453 | */ |
| 454 | #define ADDRESS_IN_RANGE(P, I) \ |
Tim Peters | 3c83df2 | 2002-03-30 07:04:41 +0000 | [diff] [blame] | 455 | ((I) < narenas && (uptr)(P) - arenas[I] < (uptr)ARENA_SIZE) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 456 | /*==========================================================================*/ |
| 457 | |
| 458 | /* malloc */ |
| 459 | |
| 460 | /* |
| 461 | * The basic blocks are ordered by decreasing execution frequency, |
| 462 | * which minimizes the number of jumps in the most common cases, |
| 463 | * improves branching prediction and instruction scheduling (small |
| 464 | * block allocations typically result in a couple of instructions). |
| 465 | * Unless the optimizer reorders everything, being too smart... |
| 466 | */ |
| 467 | |
| 468 | void * |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 469 | _PyMalloc_Malloc(size_t nbytes) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 470 | { |
| 471 | block *bp; |
| 472 | poolp pool; |
| 473 | poolp next; |
| 474 | uint size; |
| 475 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 476 | /* |
| 477 | * This implicitly redirects malloc(0) |
| 478 | */ |
| 479 | if ((nbytes - 1) < SMALL_REQUEST_THRESHOLD) { |
| 480 | LOCK(); |
| 481 | /* |
| 482 | * Most frequent paths first |
| 483 | */ |
| 484 | size = (uint )(nbytes - 1) >> ALIGNMENT_SHIFT; |
| 485 | pool = usedpools[size + size]; |
| 486 | if (pool != pool->nextpool) { |
| 487 | /* |
| 488 | * There is a used pool for this size class. |
| 489 | * Pick up the head block of its free list. |
| 490 | */ |
| 491 | ++pool->ref.count; |
| 492 | bp = pool->freeblock; |
| 493 | if ((pool->freeblock = *(block **)bp) != NULL) { |
| 494 | UNLOCK(); |
| 495 | return (void *)bp; |
| 496 | } |
| 497 | /* |
| 498 | * Reached the end of the free list, try to extend it |
| 499 | */ |
| 500 | if (pool->ref.count < pool->capacity) { |
| 501 | /* |
| 502 | * There is room for another block |
| 503 | */ |
| 504 | size++; |
| 505 | size <<= ALIGNMENT_SHIFT; /* block size */ |
| 506 | pool->freeblock = (block *)pool + \ |
| 507 | POOL_OVERHEAD + \ |
| 508 | pool->ref.count * size; |
| 509 | *(block **)(pool->freeblock) = NULL; |
| 510 | UNLOCK(); |
| 511 | return (void *)bp; |
| 512 | } |
| 513 | /* |
| 514 | * Pool is full, unlink from used pools |
| 515 | */ |
| 516 | next = pool->nextpool; |
| 517 | pool = pool->prevpool; |
| 518 | next->prevpool = pool; |
| 519 | pool->nextpool = next; |
| 520 | UNLOCK(); |
| 521 | return (void *)bp; |
| 522 | } |
| 523 | /* |
| 524 | * Try to get a cached free pool |
| 525 | */ |
| 526 | pool = freepools; |
| 527 | if (pool != NULL) { |
| 528 | /* |
| 529 | * Unlink from cached pools |
| 530 | */ |
| 531 | freepools = pool->nextpool; |
| 532 | init_pool: |
| 533 | /* |
| 534 | * Frontlink to used pools |
| 535 | */ |
| 536 | next = usedpools[size + size]; /* == prev */ |
| 537 | pool->nextpool = next; |
| 538 | pool->prevpool = next; |
| 539 | next->nextpool = pool; |
| 540 | next->prevpool = pool; |
| 541 | pool->ref.count = 1; |
| 542 | if (pool->szidx == size) { |
| 543 | /* |
| 544 | * Luckily, this pool last contained blocks |
| 545 | * of the same size class, so its header |
| 546 | * and free list are already initialized. |
| 547 | */ |
| 548 | bp = pool->freeblock; |
| 549 | pool->freeblock = *(block **)bp; |
| 550 | UNLOCK(); |
| 551 | return (void *)bp; |
| 552 | } |
| 553 | /* |
| 554 | * Initialize the pool header and free list |
| 555 | * then return the first block. |
| 556 | */ |
| 557 | pool->szidx = size; |
| 558 | size++; |
| 559 | size <<= ALIGNMENT_SHIFT; /* block size */ |
| 560 | bp = (block *)pool + POOL_OVERHEAD; |
| 561 | pool->freeblock = bp + size; |
| 562 | *(block **)(pool->freeblock) = NULL; |
| 563 | pool->capacity = (POOL_SIZE - POOL_OVERHEAD) / size; |
| 564 | UNLOCK(); |
| 565 | return (void *)bp; |
| 566 | } |
| 567 | /* |
| 568 | * Allocate new pool |
| 569 | */ |
Tim Peters | 3c83df2 | 2002-03-30 07:04:41 +0000 | [diff] [blame] | 570 | if (nfreepools) { |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 571 | commit_pool: |
Tim Peters | 3c83df2 | 2002-03-30 07:04:41 +0000 | [diff] [blame] | 572 | --nfreepools; |
| 573 | pool = (poolp)arenabase; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 574 | arenabase += POOL_SIZE; |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 575 | pool->arenaindex = narenas - 1; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 576 | pool->szidx = DUMMY_SIZE_IDX; |
| 577 | goto init_pool; |
| 578 | } |
| 579 | /* |
| 580 | * Allocate new arena |
| 581 | */ |
| 582 | #ifdef WITH_MEMORY_LIMITS |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 583 | if (!(narenas < MAX_ARENAS)) { |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 584 | UNLOCK(); |
| 585 | goto redirect; |
| 586 | } |
| 587 | #endif |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 588 | bp = new_arena(); |
| 589 | if (bp != NULL) |
| 590 | goto commit_pool; |
| 591 | UNLOCK(); |
| 592 | goto redirect; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /* The small block allocator ends here. */ |
| 596 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 597 | redirect: |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 598 | /* |
| 599 | * Redirect the original request to the underlying (libc) allocator. |
| 600 | * We jump here on bigger requests, on error in the code above (as a |
| 601 | * last chance to serve the request) or when the max memory limit |
| 602 | * has been reached. |
| 603 | */ |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 604 | return (void *)PyMem_MALLOC(nbytes); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /* free */ |
| 608 | |
| 609 | void |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 610 | _PyMalloc_Free(void *p) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 611 | { |
| 612 | poolp pool; |
| 613 | poolp next, prev; |
| 614 | uint size; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 615 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 616 | if (p == NULL) /* free(NULL) has no effect */ |
| 617 | return; |
| 618 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 619 | pool = POOL_ADDR(p); |
| 620 | if (ADDRESS_IN_RANGE(p, pool->arenaindex)) { |
| 621 | /* We allocated this address. */ |
| 622 | INCMINE; |
| 623 | LOCK(); |
| 624 | /* |
| 625 | * At this point, the pool is not empty |
| 626 | */ |
| 627 | if ((*(block **)p = pool->freeblock) == NULL) { |
| 628 | /* |
| 629 | * Pool was full |
| 630 | */ |
| 631 | pool->freeblock = (block *)p; |
| 632 | --pool->ref.count; |
| 633 | /* |
| 634 | * Frontlink to used pools |
| 635 | * This mimics LRU pool usage for new allocations and |
| 636 | * targets optimal filling when several pools contain |
| 637 | * blocks of the same size class. |
| 638 | */ |
| 639 | size = pool->szidx; |
| 640 | next = usedpools[size + size]; |
| 641 | prev = next->prevpool; |
| 642 | pool->nextpool = next; |
| 643 | pool->prevpool = prev; |
| 644 | next->prevpool = pool; |
| 645 | prev->nextpool = pool; |
| 646 | UNLOCK(); |
| 647 | return; |
| 648 | } |
| 649 | /* |
| 650 | * Pool was not full |
| 651 | */ |
| 652 | pool->freeblock = (block *)p; |
| 653 | if (--pool->ref.count != 0) { |
| 654 | UNLOCK(); |
| 655 | return; |
| 656 | } |
| 657 | /* |
| 658 | * Pool is now empty, unlink from used pools |
| 659 | */ |
| 660 | next = pool->nextpool; |
| 661 | prev = pool->prevpool; |
| 662 | next->prevpool = prev; |
| 663 | prev->nextpool = next; |
| 664 | /* |
| 665 | * Frontlink to free pools |
| 666 | * This ensures that previously freed pools will be allocated |
| 667 | * later (being not referenced, they are perhaps paged out). |
| 668 | */ |
| 669 | pool->nextpool = freepools; |
| 670 | freepools = pool; |
| 671 | UNLOCK(); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 672 | return; |
| 673 | } |
| 674 | |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 675 | /* We did not allocate this address. */ |
| 676 | INCTHEIRS; |
| 677 | PyMem_FREE(p); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /* realloc */ |
| 681 | |
| 682 | void * |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 683 | _PyMalloc_Realloc(void *p, size_t nbytes) |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 684 | { |
| 685 | block *bp; |
| 686 | poolp pool; |
| 687 | uint size; |
| 688 | |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 689 | if (p == NULL) |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 690 | return _PyMalloc_Malloc(nbytes); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 691 | |
| 692 | /* realloc(p, 0) on big blocks is redirected. */ |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 693 | pool = POOL_ADDR(p); |
| 694 | if (ADDRESS_IN_RANGE(p, pool->arenaindex)) { |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 695 | /* We're in charge of this block */ |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 696 | INCMINE; |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 697 | size = (pool->szidx + 1) << ALIGNMENT_SHIFT; /* block size */ |
| 698 | if (size >= nbytes) { |
| 699 | /* Don't bother if a smaller size was requested |
| 700 | except for realloc(p, 0) == free(p), ret NULL */ |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 701 | /* XXX but Python guarantees that *its* flavor of |
| 702 | resize(p, 0) will not do a free or return NULL */ |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 703 | if (nbytes == 0) { |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 704 | _PyMalloc_Free(p); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 705 | bp = NULL; |
| 706 | } |
| 707 | else |
| 708 | bp = (block *)p; |
| 709 | } |
| 710 | else { |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 711 | bp = (block *)_PyMalloc_Malloc(nbytes); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 712 | if (bp != NULL) { |
| 713 | memcpy(bp, p, size); |
Neil Schemenauer | 25f3dc2 | 2002-03-18 21:06:21 +0000 | [diff] [blame] | 714 | _PyMalloc_Free(p); |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | } |
Tim Peters | d97a1c0 | 2002-03-30 06:09:22 +0000 | [diff] [blame] | 718 | else { |
| 719 | /* We haven't allocated this block */ |
| 720 | INCTHEIRS; |
| 721 | if (nbytes <= SMALL_REQUEST_THRESHOLD && nbytes) { |
| 722 | /* small request */ |
| 723 | size = nbytes; |
| 724 | bp = (block *)_PyMalloc_Malloc(nbytes); |
| 725 | if (bp != NULL) { |
| 726 | memcpy(bp, p, size); |
| 727 | _PyMalloc_Free(p); |
| 728 | } |
| 729 | } |
| 730 | else |
| 731 | bp = (block *)PyMem_REALLOC(p, nbytes); |
| 732 | } |
Neil Schemenauer | a35c688 | 2001-02-27 04:45:05 +0000 | [diff] [blame] | 733 | return (void *)bp; |
| 734 | } |
| 735 | |
Tim Peters | 1221c0a | 2002-03-23 00:20:15 +0000 | [diff] [blame] | 736 | #else /* ! WITH_PYMALLOC */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 737 | |
| 738 | /*==========================================================================*/ |
| 739 | /* pymalloc not enabled: Redirect the entry points to the PyMem family. */ |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 740 | |
Tim Peters | ce7fb9b | 2002-03-23 00:28:57 +0000 | [diff] [blame] | 741 | void * |
| 742 | _PyMalloc_Malloc(size_t n) |
Tim Peters | 1221c0a | 2002-03-23 00:20:15 +0000 | [diff] [blame] | 743 | { |
| 744 | return PyMem_MALLOC(n); |
| 745 | } |
| 746 | |
Tim Peters | ce7fb9b | 2002-03-23 00:28:57 +0000 | [diff] [blame] | 747 | void * |
| 748 | _PyMalloc_Realloc(void *p, size_t n) |
Tim Peters | 1221c0a | 2002-03-23 00:20:15 +0000 | [diff] [blame] | 749 | { |
| 750 | return PyMem_REALLOC(p, n); |
| 751 | } |
| 752 | |
| 753 | void |
| 754 | _PyMalloc_Free(void *p) |
| 755 | { |
| 756 | PyMem_FREE(p); |
| 757 | } |
| 758 | #endif /* WITH_PYMALLOC */ |
| 759 | |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 760 | /*==========================================================================*/ |
| 761 | /* Regardless of whether pymalloc is enabled, export entry points for |
| 762 | * the object-oriented pymalloc functions. |
| 763 | */ |
| 764 | |
Tim Peters | ce7fb9b | 2002-03-23 00:28:57 +0000 | [diff] [blame] | 765 | PyObject * |
| 766 | _PyMalloc_New(PyTypeObject *tp) |
Tim Peters | 1221c0a | 2002-03-23 00:20:15 +0000 | [diff] [blame] | 767 | { |
| 768 | PyObject *op; |
| 769 | op = (PyObject *) _PyMalloc_MALLOC(_PyObject_SIZE(tp)); |
| 770 | if (op == NULL) |
| 771 | return PyErr_NoMemory(); |
| 772 | return PyObject_INIT(op, tp); |
| 773 | } |
| 774 | |
| 775 | PyVarObject * |
| 776 | _PyMalloc_NewVar(PyTypeObject *tp, int nitems) |
| 777 | { |
| 778 | PyVarObject *op; |
| 779 | const size_t size = _PyObject_VAR_SIZE(tp, nitems); |
| 780 | op = (PyVarObject *) _PyMalloc_MALLOC(size); |
| 781 | if (op == NULL) |
| 782 | return (PyVarObject *)PyErr_NoMemory(); |
| 783 | return PyObject_INIT_VAR(op, tp, nitems); |
| 784 | } |
| 785 | |
| 786 | void |
| 787 | _PyMalloc_Del(PyObject *op) |
| 788 | { |
| 789 | _PyMalloc_FREE(op); |
| 790 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 791 | |
| 792 | #ifdef PYMALLOC_DEBUG |
| 793 | /*==========================================================================*/ |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 794 | /* A x-platform debugging allocator. This doesn't manage memory directly, |
| 795 | * it wraps a real allocator, adding extra debugging info to the memory blocks. |
| 796 | */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 797 | |
| 798 | #define PYMALLOC_CLEANBYTE 0xCB /* uninitialized memory */ |
| 799 | #define PYMALLOC_DEADBYTE 0xDB /* free()ed memory */ |
| 800 | #define PYMALLOC_FORBIDDENBYTE 0xFB /* unusable memory */ |
| 801 | |
| 802 | static ulong serialno = 0; /* incremented on each debug {m,re}alloc */ |
| 803 | |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 804 | /* serialno is always incremented via calling this routine. The point is |
| 805 | to supply a single place to set a breakpoint. |
| 806 | */ |
| 807 | static void |
Neil Schemenauer | bd02b14 | 2002-03-28 21:05:38 +0000 | [diff] [blame] | 808 | bumpserialno(void) |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 809 | { |
| 810 | ++serialno; |
| 811 | } |
| 812 | |
| 813 | |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 814 | /* Read 4 bytes at p as a big-endian ulong. */ |
| 815 | static ulong |
| 816 | read4(const void *p) |
| 817 | { |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 818 | const uchar *q = (const uchar *)p; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 819 | return ((ulong)q[0] << 24) | |
| 820 | ((ulong)q[1] << 16) | |
| 821 | ((ulong)q[2] << 8) | |
| 822 | (ulong)q[3]; |
| 823 | } |
| 824 | |
| 825 | /* Write the 4 least-significant bytes of n as a big-endian unsigned int, |
| 826 | MSB at address p, LSB at p+3. */ |
| 827 | static void |
| 828 | write4(void *p, ulong n) |
| 829 | { |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 830 | uchar *q = (uchar *)p; |
| 831 | q[0] = (uchar)((n >> 24) & 0xff); |
| 832 | q[1] = (uchar)((n >> 16) & 0xff); |
| 833 | q[2] = (uchar)((n >> 8) & 0xff); |
| 834 | q[3] = (uchar)( n & 0xff); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 837 | /* The debug malloc asks for 16 extra bytes and fills them with useful stuff, |
| 838 | here calling the underlying malloc's result p: |
| 839 | |
| 840 | p[0:4] |
| 841 | Number of bytes originally asked for. 4-byte unsigned integer, |
| 842 | big-endian (easier to read in a memory dump). |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 843 | p[4:8] |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 844 | Copies of PYMALLOC_FORBIDDENBYTE. Used to catch under- writes |
| 845 | and reads. |
| 846 | p[8:8+n] |
| 847 | The requested memory, filled with copies of PYMALLOC_CLEANBYTE. |
| 848 | Used to catch reference to uninitialized memory. |
| 849 | &p[8] is returned. Note that this is 8-byte aligned if PyMalloc |
| 850 | handled the request itself. |
| 851 | p[8+n:8+n+4] |
| 852 | Copies of PYMALLOC_FORBIDDENBYTE. Used to catch over- writes |
| 853 | and reads. |
| 854 | p[8+n+4:8+n+8] |
| 855 | A serial number, incremented by 1 on each call to _PyMalloc_DebugMalloc |
| 856 | and _PyMalloc_DebugRealloc. |
| 857 | 4-byte unsigned integer, big-endian. |
| 858 | If "bad memory" is detected later, the serial number gives an |
| 859 | excellent way to set a breakpoint on the next run, to capture the |
| 860 | instant at which this block was passed out. |
| 861 | */ |
| 862 | |
| 863 | void * |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 864 | _PyMalloc_DebugMalloc(size_t nbytes) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 865 | { |
| 866 | uchar *p; /* base address of malloc'ed block */ |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 867 | uchar *tail; /* p + 8 + nbytes == pointer to tail pad bytes */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 868 | size_t total; /* nbytes + 16 */ |
| 869 | |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 870 | bumpserialno(); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 871 | total = nbytes + 16; |
| 872 | if (total < nbytes || (total >> 31) > 1) { |
| 873 | /* overflow, or we can't represent it in 4 bytes */ |
| 874 | /* Obscure: can't do (total >> 32) != 0 instead, because |
| 875 | C doesn't define what happens for a right-shift of 32 |
| 876 | when size_t is a 32-bit type. At least C guarantees |
| 877 | size_t is an unsigned type. */ |
| 878 | return NULL; |
| 879 | } |
| 880 | |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 881 | p = _PyMalloc_Malloc(total); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 882 | if (p == NULL) |
| 883 | return NULL; |
| 884 | |
| 885 | write4(p, nbytes); |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 886 | p[4] = p[5] = p[6] = p[7] = PYMALLOC_FORBIDDENBYTE; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 887 | |
| 888 | if (nbytes > 0) |
| 889 | memset(p+8, PYMALLOC_CLEANBYTE, nbytes); |
| 890 | |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 891 | tail = p + 8 + nbytes; |
| 892 | tail[0] = tail[1] = tail[2] = tail[3] = PYMALLOC_FORBIDDENBYTE; |
| 893 | write4(tail + 4, serialno); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 894 | |
| 895 | return p+8; |
| 896 | } |
| 897 | |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 898 | /* The debug free first checks the 8 bytes on each end for sanity (in |
| 899 | particular, that the PYMALLOC_FORBIDDENBYTEs are still intact). |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 900 | Then fills the original bytes with PYMALLOC_DEADBYTE. |
| 901 | Then calls the underlying free. |
| 902 | */ |
| 903 | void |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 904 | _PyMalloc_DebugFree(void *p) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 905 | { |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 906 | uchar *q = (uchar *)p; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 907 | size_t nbytes; |
| 908 | |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 909 | if (p == NULL) |
| 910 | return; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 911 | _PyMalloc_DebugCheckAddress(p); |
| 912 | nbytes = read4(q-8); |
| 913 | if (nbytes > 0) |
| 914 | memset(q, PYMALLOC_DEADBYTE, nbytes); |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 915 | _PyMalloc_Free(q-8); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | void * |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 919 | _PyMalloc_DebugRealloc(void *p, size_t nbytes) |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 920 | { |
| 921 | uchar *q = (uchar *)p; |
| 922 | size_t original_nbytes; |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 923 | void *fresh; /* new memory block, if needed */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 924 | |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 925 | if (p == NULL) |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 926 | return _PyMalloc_DebugMalloc(nbytes); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 927 | |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 928 | _PyMalloc_DebugCheckAddress(p); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 929 | original_nbytes = read4(q-8); |
| 930 | if (nbytes == original_nbytes) { |
| 931 | /* note that this case is likely to be common due to the |
| 932 | way Python appends to lists */ |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 933 | bumpserialno(); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 934 | write4(q + nbytes + 4, serialno); |
| 935 | return p; |
| 936 | } |
| 937 | |
| 938 | if (nbytes < original_nbytes) { |
| 939 | /* shrinking -- leave the guts alone, except to |
| 940 | fill the excess with DEADBYTE */ |
| 941 | const size_t excess = original_nbytes - nbytes; |
Tim Peters | e085017 | 2002-03-24 00:34:21 +0000 | [diff] [blame] | 942 | bumpserialno(); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 943 | write4(q-8, nbytes); |
| 944 | /* kill the excess bytes plus the trailing 8 pad bytes */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 945 | q += nbytes; |
| 946 | q[0] = q[1] = q[2] = q[3] = PYMALLOC_FORBIDDENBYTE; |
| 947 | write4(q+4, serialno); |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 948 | memset(q+8, PYMALLOC_DEADBYTE, excess); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 949 | return p; |
| 950 | } |
| 951 | |
| 952 | /* More memory is needed: get it, copy over the first original_nbytes |
| 953 | of the original data, and free the original memory. */ |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 954 | fresh = _PyMalloc_DebugMalloc(nbytes); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 955 | if (fresh != NULL && original_nbytes > 0) |
| 956 | memcpy(fresh, p, original_nbytes); |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 957 | _PyMalloc_DebugFree(p); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 958 | return fresh; |
| 959 | } |
| 960 | |
| 961 | void |
| 962 | _PyMalloc_DebugCheckAddress(const void *p) |
| 963 | { |
| 964 | const uchar *q = (const uchar *)p; |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 965 | char *msg; |
| 966 | int i; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 967 | |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 968 | if (p == NULL) { |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 969 | msg = "didn't expect a NULL pointer"; |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 970 | goto error; |
| 971 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 972 | |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 973 | for (i = 4; i >= 1; --i) { |
| 974 | if (*(q-i) != PYMALLOC_FORBIDDENBYTE) { |
| 975 | msg = "bad leading pad byte"; |
| 976 | goto error; |
| 977 | } |
| 978 | } |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 979 | |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 980 | { |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 981 | const ulong nbytes = read4(q-8); |
| 982 | const uchar *tail = q + nbytes; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 983 | for (i = 0; i < 4; ++i) { |
| 984 | if (tail[i] != PYMALLOC_FORBIDDENBYTE) { |
| 985 | msg = "bad trailing pad byte"; |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 986 | goto error; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | } |
| 990 | |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 991 | return; |
| 992 | |
| 993 | error: |
| 994 | _PyMalloc_DebugDumpAddress(p); |
| 995 | Py_FatalError(msg); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | void |
| 999 | _PyMalloc_DebugDumpAddress(const void *p) |
| 1000 | { |
| 1001 | const uchar *q = (const uchar *)p; |
| 1002 | const uchar *tail; |
| 1003 | ulong nbytes, serial; |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 1004 | int i; |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1005 | |
| 1006 | fprintf(stderr, "Debug memory block at address p=%p:\n", p); |
| 1007 | if (p == NULL) |
| 1008 | return; |
| 1009 | |
| 1010 | nbytes = read4(q-8); |
| 1011 | fprintf(stderr, " %lu bytes originally allocated\n", nbytes); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1012 | |
| 1013 | /* In case this is nuts, check the pad bytes before trying to read up |
| 1014 | the serial number (the address deref could blow up). */ |
| 1015 | |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 1016 | fputs(" the 4 pad bytes at p-4 are ", stderr); |
| 1017 | if (*(q-4) == PYMALLOC_FORBIDDENBYTE && |
| 1018 | *(q-3) == PYMALLOC_FORBIDDENBYTE && |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1019 | *(q-2) == PYMALLOC_FORBIDDENBYTE && |
| 1020 | *(q-1) == PYMALLOC_FORBIDDENBYTE) { |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 1021 | fputs("PYMALLOC_FORBIDDENBYTE, as expected\n", stderr); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1022 | } |
| 1023 | else { |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1024 | fprintf(stderr, "not all PYMALLOC_FORBIDDENBYTE (0x%02x):\n", |
| 1025 | PYMALLOC_FORBIDDENBYTE); |
Tim Peters | d1139e0 | 2002-03-28 07:32:11 +0000 | [diff] [blame] | 1026 | for (i = 4; i >= 1; --i) { |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1027 | const uchar byte = *(q-i); |
| 1028 | fprintf(stderr, " at p-%d: 0x%02x", i, byte); |
| 1029 | if (byte != PYMALLOC_FORBIDDENBYTE) |
| 1030 | fputs(" *** OUCH", stderr); |
| 1031 | fputc('\n', stderr); |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | tail = q + nbytes; |
| 1036 | fprintf(stderr, " the 4 pad bytes at tail=%p are ", tail); |
| 1037 | if (tail[0] == PYMALLOC_FORBIDDENBYTE && |
| 1038 | tail[1] == PYMALLOC_FORBIDDENBYTE && |
| 1039 | tail[2] == PYMALLOC_FORBIDDENBYTE && |
| 1040 | tail[3] == PYMALLOC_FORBIDDENBYTE) { |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 1041 | fputs("PYMALLOC_FORBIDDENBYTE, as expected\n", stderr); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1042 | } |
| 1043 | else { |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1044 | fprintf(stderr, "not all PYMALLOC_FORBIDDENBYTE (0x%02x):\n", |
| 1045 | PYMALLOC_FORBIDDENBYTE); |
| 1046 | for (i = 0; i < 4; ++i) { |
| 1047 | const uchar byte = tail[i]; |
| 1048 | fprintf(stderr, " at tail+%d: 0x%02x", |
| 1049 | i, byte); |
| 1050 | if (byte != PYMALLOC_FORBIDDENBYTE) |
| 1051 | fputs(" *** OUCH", stderr); |
| 1052 | fputc('\n', stderr); |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | serial = read4(tail+4); |
| 1057 | fprintf(stderr, " the block was made by call #%lu to " |
| 1058 | "debug malloc/realloc\n", serial); |
| 1059 | |
| 1060 | if (nbytes > 0) { |
| 1061 | int i = 0; |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 1062 | fputs(" data at p:", stderr); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1063 | /* print up to 8 bytes at the start */ |
| 1064 | while (q < tail && i < 8) { |
| 1065 | fprintf(stderr, " %02x", *q); |
| 1066 | ++i; |
| 1067 | ++q; |
| 1068 | } |
| 1069 | /* and up to 8 at the end */ |
| 1070 | if (q < tail) { |
| 1071 | if (tail - q > 8) { |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 1072 | fputs(" ...", stderr); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1073 | q = tail - 8; |
| 1074 | } |
| 1075 | while (q < tail) { |
| 1076 | fprintf(stderr, " %02x", *q); |
| 1077 | ++q; |
| 1078 | } |
| 1079 | } |
Tim Peters | 62c06ba | 2002-03-23 22:28:18 +0000 | [diff] [blame] | 1080 | fputc('\n', stderr); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | #endif /* PYMALLOC_DEBUG */ |