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