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