blob: 9353ba8c8d9295f4e2e94a4f34b358e8bf89702b [file] [log] [blame]
Tim Peters1221c0a2002-03-23 00:20:15 +00001#include "Python.h"
2
3#ifdef WITH_PYMALLOC
4
Neil Schemenauera35c6882001-02-27 04:45:05 +00005/* An object allocator for Python.
6
7 Here is an introduction to the layers of the Python memory architecture,
8 showing where the object allocator is actually used (layer +2), It is
9 called for every object allocation and deallocation (PyObject_New/Del),
10 unless the object-specific allocators implement a proprietary allocation
11 scheme (ex.: ints use a simple free list). This is also the place where
12 the cyclic garbage collector operates selectively on container objects.
13
14
15 Object-specific allocators
16 _____ ______ ______ ________
17 [ int ] [ dict ] [ list ] ... [ string ] Python core |
18+3 | <----- Object-specific memory -----> | <-- Non-object memory --> |
19 _______________________________ | |
20 [ Python's object allocator ] | |
21+2 | ####### Object memory ####### | <------ Internal buffers ------> |
22 ______________________________________________________________ |
23 [ Python's raw memory allocator (PyMem_ API) ] |
24+1 | <----- Python memory (under PyMem manager's control) ------> | |
25 __________________________________________________________________
26 [ Underlying general-purpose allocator (ex: C library malloc) ]
27 0 | <------ Virtual memory allocated for the python process -------> |
28
29 =========================================================================
30 _______________________________________________________________________
31 [ OS-specific Virtual Memory Manager (VMM) ]
32-1 | <--- Kernel dynamic storage allocation & management (page-based) ---> |
33 __________________________________ __________________________________
34 [ ] [ ]
35-2 | <-- Physical memory: ROM/RAM --> | | <-- Secondary storage (swap) --> |
36
37*/
38/*==========================================================================*/
39
40/* A fast, special-purpose memory allocator for small blocks, to be used
41 on top of a general-purpose malloc -- heavily based on previous art. */
42
43/* Vladimir Marangozov -- August 2000 */
44
45/*
46 * "Memory management is where the rubber meets the road -- if we do the wrong
47 * thing at any level, the results will not be good. And if we don't make the
48 * levels work well together, we are in serious trouble." (1)
49 *
50 * (1) Paul R. Wilson, Mark S. Johnstone, Michael Neely, and David Boles,
51 * "Dynamic Storage Allocation: A Survey and Critical Review",
52 * in Proc. 1995 Int'l. Workshop on Memory Management, September 1995.
53 */
54
55/* #undef WITH_MEMORY_LIMITS */ /* disable mem limit checks */
Neil Schemenauera35c6882001-02-27 04:45:05 +000056
57/*==========================================================================*/
58
59/*
Neil Schemenauera35c6882001-02-27 04:45:05 +000060 * Allocation strategy abstract:
61 *
62 * For small requests, the allocator sub-allocates <Big> blocks of memory.
63 * Requests greater than 256 bytes are routed to the system's allocator.
Tim Petersce7fb9b2002-03-23 00:28:57 +000064 *
Neil Schemenauera35c6882001-02-27 04:45:05 +000065 * Small requests are grouped in size classes spaced 8 bytes apart, due
66 * to the required valid alignment of the returned address. Requests of
67 * a particular size are serviced from memory pools of 4K (one VMM page).
68 * Pools are fragmented on demand and contain free lists of blocks of one
69 * particular size class. In other words, there is a fixed-size allocator
70 * for each size class. Free pools are shared by the different allocators
71 * thus minimizing the space reserved for a particular size class.
72 *
73 * This allocation strategy is a variant of what is known as "simple
74 * segregated storage based on array of free lists". The main drawback of
75 * simple segregated storage is that we might end up with lot of reserved
76 * memory for the different free lists, which degenerate in time. To avoid
77 * this, we partition each free list in pools and we share dynamically the
78 * reserved space between all free lists. This technique is quite efficient
79 * for memory intensive programs which allocate mainly small-sized blocks.
80 *
81 * For small requests we have the following table:
82 *
83 * Request in bytes Size of allocated block Size class idx
84 * ----------------------------------------------------------------
85 * 1-8 8 0
86 * 9-16 16 1
87 * 17-24 24 2
88 * 25-32 32 3
89 * 33-40 40 4
90 * 41-48 48 5
91 * 49-56 56 6
92 * 57-64 64 7
93 * 65-72 72 8
94 * ... ... ...
95 * 241-248 248 30
96 * 249-256 256 31
Tim Petersce7fb9b2002-03-23 00:28:57 +000097 *
Neil Schemenauera35c6882001-02-27 04:45:05 +000098 * 0, 257 and up: routed to the underlying allocator.
99 */
100
101/*==========================================================================*/
102
103/*
104 * -- Main tunable settings section --
105 */
106
107/*
108 * Alignment of addresses returned to the user. 8-bytes alignment works
109 * on most current architectures (with 32-bit or 64-bit address busses).
110 * The alignment value is also used for grouping small requests in size
111 * classes spaced ALIGNMENT bytes apart.
112 *
113 * You shouldn't change this unless you know what you are doing.
114 */
Neil Schemenauera35c6882001-02-27 04:45:05 +0000115#define ALIGNMENT 8 /* must be 2^N */
116#define ALIGNMENT_SHIFT 3
117#define ALIGNMENT_MASK (ALIGNMENT - 1)
118
119/*
120 * Max size threshold below which malloc requests are considered to be
121 * small enough in order to use preallocated memory pools. You can tune
122 * this value according to your application behaviour and memory needs.
123 *
124 * The following invariants must hold:
125 * 1) ALIGNMENT <= SMALL_REQUEST_THRESHOLD <= 256
Tim Petersd97a1c02002-03-30 06:09:22 +0000126 * 2) SMALL_REQUEST_THRESHOLD is evenly divisible by ALIGNMENT
Neil Schemenauera35c6882001-02-27 04:45:05 +0000127 *
128 * Although not required, for better performance and space efficiency,
129 * it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2.
130 */
Tim Petersd97a1c02002-03-30 06:09:22 +0000131#define SMALL_REQUEST_THRESHOLD 256
Neil Schemenauera35c6882001-02-27 04:45:05 +0000132#define NB_SMALL_SIZE_CLASSES (SMALL_REQUEST_THRESHOLD / ALIGNMENT)
133
134/*
135 * The system's VMM page size can be obtained on most unices with a
136 * getpagesize() call or deduced from various header files. To make
137 * things simpler, we assume that it is 4K, which is OK for most systems.
138 * It is probably better if this is the native page size, but it doesn't
139 * have to be.
140 */
Neil Schemenauera35c6882001-02-27 04:45:05 +0000141#define SYSTEM_PAGE_SIZE (4 * 1024)
142#define SYSTEM_PAGE_SIZE_MASK (SYSTEM_PAGE_SIZE - 1)
143
144/*
145 * Maximum amount of memory managed by the allocator for small requests.
146 */
Neil Schemenauera35c6882001-02-27 04:45:05 +0000147#ifdef WITH_MEMORY_LIMITS
148#ifndef SMALL_MEMORY_LIMIT
149#define SMALL_MEMORY_LIMIT (64 * 1024 * 1024) /* 64 MB -- more? */
150#endif
151#endif
152
153/*
154 * The allocator sub-allocates <Big> blocks of memory (called arenas) aligned
155 * on a page boundary. This is a reserved virtual address space for the
156 * current process (obtained through a malloc call). In no way this means
157 * that the memory arenas will be used entirely. A malloc(<Big>) is usually
158 * an address range reservation for <Big> bytes, unless all pages within this
159 * space are referenced subsequently. So malloc'ing big blocks and not using
160 * them does not mean "wasting memory". It's an addressable range wastage...
161 *
162 * Therefore, allocating arenas with malloc is not optimal, because there is
163 * some address space wastage, but this is the most portable way to request
Tim Petersd97a1c02002-03-30 06:09:22 +0000164 * memory from the system across various platforms.
Neil Schemenauera35c6882001-02-27 04:45:05 +0000165 */
Tim Peters3c83df22002-03-30 07:04:41 +0000166#define ARENA_SIZE (256 << 10) /* 256KB */
Neil Schemenauera35c6882001-02-27 04:45:05 +0000167
168#ifdef WITH_MEMORY_LIMITS
169#define MAX_ARENAS (SMALL_MEMORY_LIMIT / ARENA_SIZE)
170#endif
171
172/*
173 * Size of the pools used for small blocks. Should be a power of 2,
Tim Petersc2ce91a2002-03-30 21:36:04 +0000174 * between 1K and SYSTEM_PAGE_SIZE, that is: 1k, 2k, 4k.
Neil Schemenauera35c6882001-02-27 04:45:05 +0000175 */
Neil Schemenauera35c6882001-02-27 04:45:05 +0000176#define POOL_SIZE SYSTEM_PAGE_SIZE /* must be 2^N */
177#define POOL_SIZE_MASK SYSTEM_PAGE_SIZE_MASK
Neil Schemenauera35c6882001-02-27 04:45:05 +0000178
179/*
180 * -- End of tunable settings section --
181 */
182
183/*==========================================================================*/
184
185/*
186 * Locking
187 *
188 * To reduce lock contention, it would probably be better to refine the
189 * crude function locking with per size class locking. I'm not positive
190 * however, whether it's worth switching to such locking policy because
191 * of the performance penalty it might introduce.
192 *
193 * The following macros describe the simplest (should also be the fastest)
194 * lock object on a particular platform and the init/fini/lock/unlock
195 * operations on it. The locks defined here are not expected to be recursive
196 * because it is assumed that they will always be called in the order:
197 * INIT, [LOCK, UNLOCK]*, FINI.
198 */
199
200/*
201 * Python's threads are serialized, so object malloc locking is disabled.
202 */
203#define SIMPLELOCK_DECL(lock) /* simple lock declaration */
204#define SIMPLELOCK_INIT(lock) /* allocate (if needed) and initialize */
205#define SIMPLELOCK_FINI(lock) /* free/destroy an existing lock */
206#define SIMPLELOCK_LOCK(lock) /* acquire released lock */
207#define SIMPLELOCK_UNLOCK(lock) /* release acquired lock */
208
209/*
210 * Basic types
211 * I don't care if these are defined in <sys/types.h> or elsewhere. Axiom.
212 */
Neil Schemenauera35c6882001-02-27 04:45:05 +0000213#undef uchar
214#define uchar unsigned char /* assuming == 8 bits */
215
Neil Schemenauera35c6882001-02-27 04:45:05 +0000216#undef uint
217#define uint unsigned int /* assuming >= 16 bits */
218
219#undef ulong
220#define ulong unsigned long /* assuming >= 32 bits */
221
Tim Petersd97a1c02002-03-30 06:09:22 +0000222#undef uptr
223#define uptr Py_uintptr_t
224
Neil Schemenauera35c6882001-02-27 04:45:05 +0000225/* When you say memory, my mind reasons in terms of (pointers to) blocks */
226typedef uchar block;
227
228/* Pool for small blocks */
229struct pool_header {
Tim Petersb2336522001-03-11 18:36:13 +0000230 union { block *_padding;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000231 uint count; } ref; /* number of allocated blocks */
232 block *freeblock; /* pool's free list head */
233 struct pool_header *nextpool; /* next pool of this size class */
234 struct pool_header *prevpool; /* previous pool "" */
Tim Peters1d99af82002-03-30 10:35:09 +0000235 uint arenaindex; /* index into arenas of base adr */
Neil Schemenauera35c6882001-02-27 04:45:05 +0000236 uint szidx; /* block size class index */
237 uint capacity; /* pool capacity in # of blocks */
238};
239
240typedef struct pool_header *poolp;
241
242#undef ROUNDUP
243#define ROUNDUP(x) (((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK)
244#define POOL_OVERHEAD ROUNDUP(sizeof(struct pool_header))
245
246#define DUMMY_SIZE_IDX 0xffff /* size class of newly cached pools */
247
Tim Petersd97a1c02002-03-30 06:09:22 +0000248/* Round pointer P down to the closest pool-aligned address <= P, as a poolp */
249#define POOL_ADDR(P) \
250 ((poolp)((uptr)(P) & ~(uptr)POOL_SIZE_MASK))
251
Neil Schemenauera35c6882001-02-27 04:45:05 +0000252/*==========================================================================*/
253
254/*
255 * This malloc lock
256 */
Tim Petersb2336522001-03-11 18:36:13 +0000257SIMPLELOCK_DECL(_malloc_lock);
258#define LOCK() SIMPLELOCK_LOCK(_malloc_lock)
259#define UNLOCK() SIMPLELOCK_UNLOCK(_malloc_lock)
260#define LOCK_INIT() SIMPLELOCK_INIT(_malloc_lock)
261#define LOCK_FINI() SIMPLELOCK_FINI(_malloc_lock)
Neil Schemenauera35c6882001-02-27 04:45:05 +0000262
263/*
Tim Peters1e16db62002-03-31 01:05:22 +0000264 * Pool table -- headed, circular, doubly-linked lists of partially used pools.
265
266This is involved. For an index i, usedpools[i+i] is the header for a list of
267all partially used pools holding small blocks with "size class idx" i. So
268usedpools[0] corresponds to blocks of size 8, usedpools[2] to blocks of size
26916, and so on: index 2*i <-> blocks of size (i+1)<<ALIGNMENT_SHIFT.
270
Tim Peters338e0102002-04-01 19:23:44 +0000271Pools are carved off the current arena highwater mark (file static arenabase)
272as needed. Once carved off, a pool is in one of three states forever after:
Tim Peters1e16db62002-03-31 01:05:22 +0000273
Tim Peters338e0102002-04-01 19:23:44 +0000274used == partially used, neither empty nor full
275 At least one block in the pool is currently allocated, and at least one
276 block in the pool is not currently allocated (note this implies a pool
277 has room for at least two blocks).
278 This is a pool's initial state, as a pool is created only when malloc
279 needs space.
280 The pool holds blocks of a fixed size, and is in the circular list headed
281 at usedpools[i] (see above). It's linked to the other used pools of the
282 same size class via the pool_header's nextpool and prevpool members.
283 If all but one block is currently allocated, a malloc can cause a
284 transition to the full state. If all but one block is not currently
285 allocated, a free can cause a transition to the empty state.
Tim Peters1e16db62002-03-31 01:05:22 +0000286
Tim Peters338e0102002-04-01 19:23:44 +0000287full == all the pool's blocks are currently allocated
288 On transition to full, a pool is unlinked from its usedpools[] list.
289 It's not linked to from anything then anymore, and its nextpool and
290 prevpool members are meaningless until it transitions back to used.
291 A free of a block in a full pool puts the pool back in the used state.
292 Then it's linked in at the front of the appropriate usedpools[] list, so
293 that the next allocation for its size class will reuse the freed block.
294
295empty == all the pool's blocks are currently available for allocation
296 On transition to empty, a pool is unlinked from its usedpools[] list,
297 and linked to the front of the (file static) singly-linked freepools list,
298 via its nextpool member. The prevpool member has no meaning in this case.
299 Empty pools have no inherent size class: the next time a malloc finds
300 an empty list in usedpools[], it takes the first pool off of freepools.
301 If the size class needed happens to be the same as the size class the pool
302 last had, some expensive initialization can be skipped (including an
303 integer division -- XXX since the value
304
305 pool->capacity = (POOL_SIZE - POOL_OVERHEAD) / size;
306
307 is invariant across all pools of a given size class, it may make more
308 sense to compute those at compile-time into a const vector indexed by
309 size class, and lose the pool->capacity member and the runtime divisions).
310
311
312Block Management
313
314Blocks within pools are again carved out as needed. pool->freeblock points to
315the start of a singly-linked list of free blocks within the pool. When a
316block is freed, it's inserted at the front of its pool's freeblock list. Note
317that the available blocks in a pool are *not* linked all together when a pool
318is initialized. Instead only "the first" (lowest address) block is set up,
319setting pool->freeblock to NULL. This is consistent with that pymalloc
320strives at all levels (arena, pool, and block) never to touch a piece of
321memory until it's actually needed. So long as a pool is in the used state,
322we're certain there *is* a block available for allocating. If pool->freeblock
323is NULL then, that means we simply haven't yet gotten to one of the higher-
324address blocks. The address of "the next" available block can be computed
325then from pool->ref.count (the number of currently allocated blocks). This
326computation can be expensive, because it requires an integer multiply.
327However, so long as the pool's size class doesn't change, it's a one-time cost
328for that block; the computation could be made cheaper via adding a highwater
329pointer to the pool_header, but the tradeoff is murky.
330
Tim Peters1e16db62002-03-31 01:05:22 +0000331
332Major obscurity: While the usedpools vector is declared to have poolp
333entries, it doesn't really. It really contains two pointers per (conceptual)
334poolp entry, the nextpool and prevpool members of a pool_header. The
335excruciating initialization code below fools C so that
336
337 usedpool[i+i]
338
339"acts like" a genuine poolp, but only so long as you only reference its
340nextpool and prevpool members. The "- 2*sizeof(block *)" gibberish is
341compensating for that a pool_header's nextpool and prevpool members
342immediately follow a pool_header's first two members:
343
344 union { block *_padding;
345 uint count; } ref;
346 block *freeblock;
347
348each of which consume sizeof(block *) bytes. So what usedpools[i+i] really
349contains is a fudged-up pointer p such that *if* C believes it's a poolp
350pointer, then p->nextpool and p->prevpool are both p (meaning that the headed
351circular list is empty).
352
353It's unclear why the usedpools setup is so convoluted. It could be to
354minimize the amount of cache required to hold this heavily-referenced table
355(which only *needs* the two interpool pointer members of a pool_header). OTOH,
356referencing code has to remember to "double the index" and doing so isn't
357free, usedpools[0] isn't a strictly legal pointer, and we're crucially relying
358on that C doesn't insert any padding anywhere in a pool_header at or before
359the prevpool member.
360**************************************************************************** */
361
Neil Schemenauera35c6882001-02-27 04:45:05 +0000362#define PTA(x) ((poolp )((uchar *)&(usedpools[2*(x)]) - 2*sizeof(block *)))
363#define PT(x) PTA(x), PTA(x)
364
365static poolp usedpools[2 * ((NB_SMALL_SIZE_CLASSES + 7) / 8) * 8] = {
366 PT(0), PT(1), PT(2), PT(3), PT(4), PT(5), PT(6), PT(7)
367#if NB_SMALL_SIZE_CLASSES > 8
368 , PT(8), PT(9), PT(10), PT(11), PT(12), PT(13), PT(14), PT(15)
369#if NB_SMALL_SIZE_CLASSES > 16
370 , PT(16), PT(17), PT(18), PT(19), PT(20), PT(21), PT(22), PT(23)
371#if NB_SMALL_SIZE_CLASSES > 24
372 , PT(24), PT(25), PT(26), PT(27), PT(28), PT(29), PT(30), PT(31)
373#if NB_SMALL_SIZE_CLASSES > 32
374 , PT(32), PT(33), PT(34), PT(35), PT(36), PT(37), PT(38), PT(39)
375#if NB_SMALL_SIZE_CLASSES > 40
376 , PT(40), PT(41), PT(42), PT(43), PT(44), PT(45), PT(46), PT(47)
377#if NB_SMALL_SIZE_CLASSES > 48
378 , PT(48), PT(49), PT(50), PT(51), PT(52), PT(53), PT(54), PT(55)
379#if NB_SMALL_SIZE_CLASSES > 56
380 , PT(56), PT(57), PT(58), PT(59), PT(60), PT(61), PT(62), PT(63)
381#endif /* NB_SMALL_SIZE_CLASSES > 56 */
382#endif /* NB_SMALL_SIZE_CLASSES > 48 */
383#endif /* NB_SMALL_SIZE_CLASSES > 40 */
384#endif /* NB_SMALL_SIZE_CLASSES > 32 */
385#endif /* NB_SMALL_SIZE_CLASSES > 24 */
386#endif /* NB_SMALL_SIZE_CLASSES > 16 */
387#endif /* NB_SMALL_SIZE_CLASSES > 8 */
388};
389
390/*
391 * Free (cached) pools
392 */
393static poolp freepools = NULL; /* free list for cached pools */
394
Tim Petersd97a1c02002-03-30 06:09:22 +0000395/*==========================================================================*/
396/* Arena management. */
Neil Schemenauera35c6882001-02-27 04:45:05 +0000397
Tim Petersd97a1c02002-03-30 06:09:22 +0000398/* arenas is a vector of arena base addresses, in order of allocation time.
399 * arenas currently contains narenas entries, and has space allocated
400 * for at most maxarenas entries.
401 *
402 * CAUTION: See the long comment block about thread safety in new_arena():
403 * the code currently relies in deep ways on that this vector only grows,
404 * and only grows by appending at the end. For now we never return an arena
405 * to the OS.
406 */
Tim Petersc2ce91a2002-03-30 21:36:04 +0000407static uptr *volatile arenas = NULL; /* the pointer itself is volatile */
408static volatile uint narenas = 0;
Tim Peters1d99af82002-03-30 10:35:09 +0000409static uint maxarenas = 0;
Tim Petersd97a1c02002-03-30 06:09:22 +0000410
Tim Peters3c83df22002-03-30 07:04:41 +0000411/* Number of pools still available to be allocated in the current arena. */
412static uint nfreepools = 0;
Tim Petersd97a1c02002-03-30 06:09:22 +0000413
Tim Peters3c83df22002-03-30 07:04:41 +0000414/* Free space start address in current arena. This is pool-aligned. */
Tim Petersd97a1c02002-03-30 06:09:22 +0000415static block *arenabase = NULL;
416
417#if 0
418static ulong wasmine = 0;
419static ulong wasntmine = 0;
420
421static void
422dumpem(void *ptr)
423{
424 if (ptr)
425 printf("inserted new arena at %08x\n", ptr);
Tim Peters1d99af82002-03-30 10:35:09 +0000426 printf("# arenas %u\n", narenas);
Tim Petersd97a1c02002-03-30 06:09:22 +0000427 printf("was mine %lu wasn't mine %lu\n", wasmine, wasntmine);
428}
429#define INCMINE ++wasmine
430#define INCTHEIRS ++wasntmine
431
432#else
433#define dumpem(ptr)
434#define INCMINE
435#define INCTHEIRS
436#endif
437
438/* Allocate a new arena and return its base address. If we run out of
439 * memory, return NULL.
440 */
441static block *
442new_arena(void)
443{
Tim Peters3c83df22002-03-30 07:04:41 +0000444 uint excess; /* number of bytes above pool alignment */
445 block *bp = (block *)PyMem_MALLOC(ARENA_SIZE);
Tim Petersd97a1c02002-03-30 06:09:22 +0000446 if (bp == NULL)
447 return NULL;
448
Tim Peters3c83df22002-03-30 07:04:41 +0000449 /* arenabase <- first pool-aligned address in the arena
450 nfreepools <- number of whole pools that fit after alignment */
451 arenabase = bp;
452 nfreepools = ARENA_SIZE / POOL_SIZE;
Tim Petersc2ce91a2002-03-30 21:36:04 +0000453 assert(POOL_SIZE * nfreepools == ARENA_SIZE);
Tim Peters3c83df22002-03-30 07:04:41 +0000454 excess = (uint)bp & POOL_SIZE_MASK;
455 if (excess != 0) {
456 --nfreepools;
457 arenabase += POOL_SIZE - excess;
458 }
Tim Petersd97a1c02002-03-30 06:09:22 +0000459
460 /* Make room for a new entry in the arenas vector. */
461 if (arenas == NULL) {
Tim Petersc2ce91a2002-03-30 21:36:04 +0000462 assert(narenas == 0 && maxarenas == 0);
Tim Petersd97a1c02002-03-30 06:09:22 +0000463 arenas = (uptr *)PyMem_MALLOC(16 * sizeof(*arenas));
464 if (arenas == NULL)
465 goto error;
466 maxarenas = 16;
Tim Petersd97a1c02002-03-30 06:09:22 +0000467 }
468 else if (narenas == maxarenas) {
469 /* Grow arenas. Don't use realloc: if this fails, we
470 * don't want to lose the base addresses we already have.
Tim Petersc2ce91a2002-03-30 21:36:04 +0000471 *
Tim Petersd97a1c02002-03-30 06:09:22 +0000472 * Exceedingly subtle: Someone may be calling the pymalloc
473 * free via PyMem_{DEL, Del, FREE, Free} without holding the
474 *.GIL. Someone else may simultaneously be calling the
475 * pymalloc malloc while holding the GIL via, e.g.,
476 * PyObject_New. Now the pymalloc free may index into arenas
477 * for an address check, while the pymalloc malloc calls
478 * new_arena and we end up here to grow a new arena *and*
479 * grow the arenas vector. If the value for arenas pymalloc
480 * free picks up "vanishes" during this resize, anything may
481 * happen, and it would be an incredibly rare bug. Therefore
482 * the code here takes great pains to make sure that, at every
483 * moment, arenas always points to an intact vector of
484 * addresses. It doesn't matter whether arenas points to a
485 * wholly up-to-date vector when pymalloc free checks it in
486 * this case, because the only legal (and that even this is
487 * legal is debatable) way to call PyMem_{Del, etc} while not
488 * holding the GIL is if the memory being released is not
489 * object memory, i.e. if the address check in pymalloc free
490 * is supposed to fail. Having an incomplete vector can't
491 * make a supposed-to-fail case succeed by mistake (it could
492 * only make a supposed-to-succeed case fail by mistake).
Tim Petersc2ce91a2002-03-30 21:36:04 +0000493 *
494 * In addition, without a lock we can't know for sure when
495 * an old vector is no longer referenced, so we simply let
496 * old vectors leak.
497 *
498 * And on top of that, since narenas and arenas can't be
499 * changed as-a-pair atomically without a lock, we're also
500 * careful to declare them volatile and ensure that we change
501 * arenas first. This prevents another thread from picking
502 * up an narenas value too large for the arenas value it
503 * reads up (arenas never shrinks).
504 *
Tim Petersd97a1c02002-03-30 06:09:22 +0000505 * Read the above 50 times before changing anything in this
506 * block.
507 */
Tim Peters1d99af82002-03-30 10:35:09 +0000508 uptr *p;
Tim Petersc2ce91a2002-03-30 21:36:04 +0000509 uint newmax = maxarenas << 1;
Tim Peters1d99af82002-03-30 10:35:09 +0000510 if (newmax <= maxarenas) /* overflow */
511 goto error;
512 p = (uptr *)PyMem_MALLOC(newmax * sizeof(*arenas));
Tim Petersd97a1c02002-03-30 06:09:22 +0000513 if (p == NULL)
514 goto error;
515 memcpy(p, arenas, narenas * sizeof(*arenas));
Tim Petersc2ce91a2002-03-30 21:36:04 +0000516 arenas = p; /* old arenas deliberately leaked */
Tim Petersd97a1c02002-03-30 06:09:22 +0000517 maxarenas = newmax;
518 }
519
520 /* Append the new arena address to arenas. */
521 assert(narenas < maxarenas);
522 arenas[narenas] = (uptr)bp;
Tim Peters1d99af82002-03-30 10:35:09 +0000523 ++narenas; /* can't overflow, since narenas < maxarenas before */
Tim Petersd97a1c02002-03-30 06:09:22 +0000524 dumpem(bp);
525 return bp;
526
527error:
528 PyMem_FREE(bp);
Tim Peters7b85b4a2002-03-30 10:42:09 +0000529 nfreepools = 0;
Tim Petersd97a1c02002-03-30 06:09:22 +0000530 return NULL;
531}
532
533/* Return true if and only if P is an address that was allocated by
534 * pymalloc. I must be the index into arenas that the address claims
535 * to come from.
Tim Petersc2ce91a2002-03-30 21:36:04 +0000536 *
Tim Petersd97a1c02002-03-30 06:09:22 +0000537 * Tricky: Letting B be the arena base address in arenas[I], P belongs to the
538 * arena if and only if
Tim Peters3c83df22002-03-30 07:04:41 +0000539 * B <= P < B + ARENA_SIZE
Tim Petersd97a1c02002-03-30 06:09:22 +0000540 * Subtracting B throughout, this is true iff
Tim Peters3c83df22002-03-30 07:04:41 +0000541 * 0 <= P-B < ARENA_SIZE
Tim Petersd97a1c02002-03-30 06:09:22 +0000542 * By using unsigned arithmetic, the "0 <=" half of the test can be skipped.
Tim Petersc2ce91a2002-03-30 21:36:04 +0000543 *
544 * Obscure: A PyMem "free memory" function can call the pymalloc free or
545 * realloc before the first arena has been allocated. arenas is still
546 * NULL in that case. We're relying on that narenas is also 0 in that case,
547 * so the (I) < narenas must be false, saving us from trying to index into
548 * a NULL arenas.
Tim Petersd97a1c02002-03-30 06:09:22 +0000549 */
550#define ADDRESS_IN_RANGE(P, I) \
Tim Peters3c83df22002-03-30 07:04:41 +0000551 ((I) < narenas && (uptr)(P) - arenas[I] < (uptr)ARENA_SIZE)
Tim Peters338e0102002-04-01 19:23:44 +0000552
Neil Schemenauera35c6882001-02-27 04:45:05 +0000553/*==========================================================================*/
554
555/* malloc */
556
557/*
558 * The basic blocks are ordered by decreasing execution frequency,
559 * which minimizes the number of jumps in the most common cases,
560 * improves branching prediction and instruction scheduling (small
561 * block allocations typically result in a couple of instructions).
562 * Unless the optimizer reorders everything, being too smart...
563 */
564
565void *
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000566_PyMalloc_Malloc(size_t nbytes)
Neil Schemenauera35c6882001-02-27 04:45:05 +0000567{
568 block *bp;
569 poolp pool;
570 poolp next;
571 uint size;
572
Neil Schemenauera35c6882001-02-27 04:45:05 +0000573 /*
574 * This implicitly redirects malloc(0)
575 */
576 if ((nbytes - 1) < SMALL_REQUEST_THRESHOLD) {
577 LOCK();
578 /*
579 * Most frequent paths first
580 */
581 size = (uint )(nbytes - 1) >> ALIGNMENT_SHIFT;
582 pool = usedpools[size + size];
583 if (pool != pool->nextpool) {
584 /*
585 * There is a used pool for this size class.
586 * Pick up the head block of its free list.
587 */
588 ++pool->ref.count;
589 bp = pool->freeblock;
590 if ((pool->freeblock = *(block **)bp) != NULL) {
591 UNLOCK();
592 return (void *)bp;
593 }
594 /*
595 * Reached the end of the free list, try to extend it
596 */
597 if (pool->ref.count < pool->capacity) {
598 /*
599 * There is room for another block
600 */
601 size++;
602 size <<= ALIGNMENT_SHIFT; /* block size */
603 pool->freeblock = (block *)pool + \
604 POOL_OVERHEAD + \
605 pool->ref.count * size;
606 *(block **)(pool->freeblock) = NULL;
607 UNLOCK();
608 return (void *)bp;
609 }
610 /*
611 * Pool is full, unlink from used pools
612 */
613 next = pool->nextpool;
614 pool = pool->prevpool;
615 next->prevpool = pool;
616 pool->nextpool = next;
617 UNLOCK();
618 return (void *)bp;
619 }
620 /*
621 * Try to get a cached free pool
622 */
623 pool = freepools;
624 if (pool != NULL) {
625 /*
626 * Unlink from cached pools
627 */
628 freepools = pool->nextpool;
629 init_pool:
630 /*
631 * Frontlink to used pools
632 */
633 next = usedpools[size + size]; /* == prev */
634 pool->nextpool = next;
635 pool->prevpool = next;
636 next->nextpool = pool;
637 next->prevpool = pool;
638 pool->ref.count = 1;
639 if (pool->szidx == size) {
640 /*
641 * Luckily, this pool last contained blocks
642 * of the same size class, so its header
643 * and free list are already initialized.
644 */
645 bp = pool->freeblock;
646 pool->freeblock = *(block **)bp;
647 UNLOCK();
648 return (void *)bp;
649 }
650 /*
651 * Initialize the pool header and free list
652 * then return the first block.
653 */
654 pool->szidx = size;
655 size++;
656 size <<= ALIGNMENT_SHIFT; /* block size */
657 bp = (block *)pool + POOL_OVERHEAD;
658 pool->freeblock = bp + size;
659 *(block **)(pool->freeblock) = NULL;
660 pool->capacity = (POOL_SIZE - POOL_OVERHEAD) / size;
661 UNLOCK();
662 return (void *)bp;
663 }
664 /*
665 * Allocate new pool
666 */
Tim Peters3c83df22002-03-30 07:04:41 +0000667 if (nfreepools) {
Neil Schemenauera35c6882001-02-27 04:45:05 +0000668 commit_pool:
Tim Peters3c83df22002-03-30 07:04:41 +0000669 --nfreepools;
670 pool = (poolp)arenabase;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000671 arenabase += POOL_SIZE;
Tim Petersd97a1c02002-03-30 06:09:22 +0000672 pool->arenaindex = narenas - 1;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000673 pool->szidx = DUMMY_SIZE_IDX;
674 goto init_pool;
675 }
676 /*
677 * Allocate new arena
678 */
679#ifdef WITH_MEMORY_LIMITS
Tim Petersd97a1c02002-03-30 06:09:22 +0000680 if (!(narenas < MAX_ARENAS)) {
Neil Schemenauera35c6882001-02-27 04:45:05 +0000681 UNLOCK();
682 goto redirect;
683 }
684#endif
Tim Petersd97a1c02002-03-30 06:09:22 +0000685 bp = new_arena();
686 if (bp != NULL)
687 goto commit_pool;
688 UNLOCK();
689 goto redirect;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000690 }
691
692 /* The small block allocator ends here. */
693
Tim Petersd97a1c02002-03-30 06:09:22 +0000694redirect:
Neil Schemenauera35c6882001-02-27 04:45:05 +0000695 /*
696 * Redirect the original request to the underlying (libc) allocator.
697 * We jump here on bigger requests, on error in the code above (as a
698 * last chance to serve the request) or when the max memory limit
699 * has been reached.
700 */
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000701 return (void *)PyMem_MALLOC(nbytes);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000702}
703
704/* free */
705
706void
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000707_PyMalloc_Free(void *p)
Neil Schemenauera35c6882001-02-27 04:45:05 +0000708{
709 poolp pool;
Tim Peters2c95c992002-03-31 02:18:01 +0000710 block *lastfree;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000711 poolp next, prev;
712 uint size;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000713
Neil Schemenauera35c6882001-02-27 04:45:05 +0000714 if (p == NULL) /* free(NULL) has no effect */
715 return;
716
Tim Petersd97a1c02002-03-30 06:09:22 +0000717 pool = POOL_ADDR(p);
718 if (ADDRESS_IN_RANGE(p, pool->arenaindex)) {
719 /* We allocated this address. */
Tim Petersd97a1c02002-03-30 06:09:22 +0000720 LOCK();
Tim Peters1e16db62002-03-31 01:05:22 +0000721 INCMINE;
Tim Petersd97a1c02002-03-30 06:09:22 +0000722 /*
Tim Peters2c95c992002-03-31 02:18:01 +0000723 * Link p to the start of the pool's freeblock list. Since
724 * the pool had at least the p block outstanding, the pool
725 * wasn't empty (so it's already in a usedpools[] list, or
726 * was full and is in no list -- it's not in the freeblocks
727 * list in any case).
Tim Petersd97a1c02002-03-30 06:09:22 +0000728 */
Tim Peters57b17ad2002-03-31 02:59:48 +0000729 assert(pool->ref.count > 0); /* else it was empty */
Tim Peters2c95c992002-03-31 02:18:01 +0000730 *(block **)p = lastfree = pool->freeblock;
Tim Petersd97a1c02002-03-30 06:09:22 +0000731 pool->freeblock = (block *)p;
Tim Peters2c95c992002-03-31 02:18:01 +0000732 if (lastfree) {
733 /*
734 * freeblock wasn't NULL, so the pool wasn't full,
735 * and the pool is in a usedpools[] list.
736 */
Tim Peters4c5be0c2002-03-31 02:52:29 +0000737 assert(pool->ref.count < pool->capacity);
Tim Peters2c95c992002-03-31 02:18:01 +0000738 if (--pool->ref.count != 0) {
739 /* pool isn't empty: leave it in usedpools */
740 UNLOCK();
741 return;
742 }
743 /*
744 * Pool is now empty: unlink from usedpools, and
Tim Petersb1da0502002-03-31 02:51:40 +0000745 * link to the front of freepools. This ensures that
Tim Peters2c95c992002-03-31 02:18:01 +0000746 * previously freed pools will be allocated later
747 * (being not referenced, they are perhaps paged out).
748 */
749 next = pool->nextpool;
750 prev = pool->prevpool;
751 next->prevpool = prev;
752 prev->nextpool = next;
753 /* Link to freepools. This is a singly-linked list,
754 * and pool->prevpool isn't used there.
755 */
756 pool->nextpool = freepools;
757 freepools = pool;
Tim Petersd97a1c02002-03-30 06:09:22 +0000758 UNLOCK();
759 return;
760 }
761 /*
Tim Peters2c95c992002-03-31 02:18:01 +0000762 * Pool was full, so doesn't currently live in any list:
763 * link it to the front of the appropriate usedpools[] list.
764 * This mimics LRU pool usage for new allocations and
765 * targets optimal filling when several pools contain
766 * blocks of the same size class.
Tim Petersd97a1c02002-03-30 06:09:22 +0000767 */
Tim Peters2c95c992002-03-31 02:18:01 +0000768 assert(pool->ref.count == pool->capacity); /* else not full */
769 --pool->ref.count;
770 assert(pool->ref.count > 0); /* else the pool is empty */
771 size = pool->szidx;
772 next = usedpools[size + size];
773 prev = next->prevpool;
774 /* insert pool before next: prev <-> pool <-> next */
775 pool->nextpool = next;
776 pool->prevpool = prev;
777 next->prevpool = pool;
778 prev->nextpool = pool;
Tim Petersd97a1c02002-03-30 06:09:22 +0000779 UNLOCK();
Neil Schemenauera35c6882001-02-27 04:45:05 +0000780 return;
781 }
782
Tim Peters2c95c992002-03-31 02:18:01 +0000783 /* We didn't allocate this address. */
Tim Petersd97a1c02002-03-30 06:09:22 +0000784 INCTHEIRS;
785 PyMem_FREE(p);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000786}
787
788/* realloc */
789
790void *
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000791_PyMalloc_Realloc(void *p, size_t nbytes)
Neil Schemenauera35c6882001-02-27 04:45:05 +0000792{
793 block *bp;
794 poolp pool;
795 uint size;
796
Neil Schemenauera35c6882001-02-27 04:45:05 +0000797 if (p == NULL)
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000798 return _PyMalloc_Malloc(nbytes);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000799
800 /* realloc(p, 0) on big blocks is redirected. */
Tim Petersd97a1c02002-03-30 06:09:22 +0000801 pool = POOL_ADDR(p);
802 if (ADDRESS_IN_RANGE(p, pool->arenaindex)) {
Neil Schemenauera35c6882001-02-27 04:45:05 +0000803 /* We're in charge of this block */
Tim Petersd97a1c02002-03-30 06:09:22 +0000804 INCMINE;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000805 size = (pool->szidx + 1) << ALIGNMENT_SHIFT; /* block size */
806 if (size >= nbytes) {
807 /* Don't bother if a smaller size was requested
808 except for realloc(p, 0) == free(p), ret NULL */
Tim Petersd97a1c02002-03-30 06:09:22 +0000809 /* XXX but Python guarantees that *its* flavor of
810 resize(p, 0) will not do a free or return NULL */
Neil Schemenauera35c6882001-02-27 04:45:05 +0000811 if (nbytes == 0) {
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000812 _PyMalloc_Free(p);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000813 bp = NULL;
814 }
815 else
816 bp = (block *)p;
817 }
818 else {
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000819 bp = (block *)_PyMalloc_Malloc(nbytes);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000820 if (bp != NULL) {
821 memcpy(bp, p, size);
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000822 _PyMalloc_Free(p);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000823 }
824 }
825 }
Tim Petersd97a1c02002-03-30 06:09:22 +0000826 else {
827 /* We haven't allocated this block */
828 INCTHEIRS;
829 if (nbytes <= SMALL_REQUEST_THRESHOLD && nbytes) {
830 /* small request */
831 size = nbytes;
832 bp = (block *)_PyMalloc_Malloc(nbytes);
833 if (bp != NULL) {
834 memcpy(bp, p, size);
835 _PyMalloc_Free(p);
836 }
837 }
838 else
839 bp = (block *)PyMem_REALLOC(p, nbytes);
840 }
Neil Schemenauera35c6882001-02-27 04:45:05 +0000841 return (void *)bp;
842}
843
Tim Peters1221c0a2002-03-23 00:20:15 +0000844#else /* ! WITH_PYMALLOC */
Tim Petersddea2082002-03-23 10:03:50 +0000845
846/*==========================================================================*/
847/* pymalloc not enabled: Redirect the entry points to the PyMem family. */
Tim Peters62c06ba2002-03-23 22:28:18 +0000848
Tim Petersce7fb9b2002-03-23 00:28:57 +0000849void *
850_PyMalloc_Malloc(size_t n)
Tim Peters1221c0a2002-03-23 00:20:15 +0000851{
852 return PyMem_MALLOC(n);
853}
854
Tim Petersce7fb9b2002-03-23 00:28:57 +0000855void *
856_PyMalloc_Realloc(void *p, size_t n)
Tim Peters1221c0a2002-03-23 00:20:15 +0000857{
858 return PyMem_REALLOC(p, n);
859}
860
861void
862_PyMalloc_Free(void *p)
863{
864 PyMem_FREE(p);
865}
866#endif /* WITH_PYMALLOC */
867
Tim Peters62c06ba2002-03-23 22:28:18 +0000868/*==========================================================================*/
869/* Regardless of whether pymalloc is enabled, export entry points for
870 * the object-oriented pymalloc functions.
871 */
872
Tim Petersce7fb9b2002-03-23 00:28:57 +0000873PyObject *
874_PyMalloc_New(PyTypeObject *tp)
Tim Peters1221c0a2002-03-23 00:20:15 +0000875{
876 PyObject *op;
877 op = (PyObject *) _PyMalloc_MALLOC(_PyObject_SIZE(tp));
878 if (op == NULL)
879 return PyErr_NoMemory();
880 return PyObject_INIT(op, tp);
881}
882
883PyVarObject *
884_PyMalloc_NewVar(PyTypeObject *tp, int nitems)
885{
886 PyVarObject *op;
887 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
888 op = (PyVarObject *) _PyMalloc_MALLOC(size);
889 if (op == NULL)
890 return (PyVarObject *)PyErr_NoMemory();
891 return PyObject_INIT_VAR(op, tp, nitems);
892}
893
894void
895_PyMalloc_Del(PyObject *op)
896{
897 _PyMalloc_FREE(op);
898}
Tim Petersddea2082002-03-23 10:03:50 +0000899
900#ifdef PYMALLOC_DEBUG
901/*==========================================================================*/
Tim Peters62c06ba2002-03-23 22:28:18 +0000902/* A x-platform debugging allocator. This doesn't manage memory directly,
903 * it wraps a real allocator, adding extra debugging info to the memory blocks.
904 */
Tim Petersddea2082002-03-23 10:03:50 +0000905
906#define PYMALLOC_CLEANBYTE 0xCB /* uninitialized memory */
907#define PYMALLOC_DEADBYTE 0xDB /* free()ed memory */
908#define PYMALLOC_FORBIDDENBYTE 0xFB /* unusable memory */
909
910static ulong serialno = 0; /* incremented on each debug {m,re}alloc */
911
Tim Peterse0850172002-03-24 00:34:21 +0000912/* serialno is always incremented via calling this routine. The point is
913 to supply a single place to set a breakpoint.
914*/
915static void
Neil Schemenauerbd02b142002-03-28 21:05:38 +0000916bumpserialno(void)
Tim Peterse0850172002-03-24 00:34:21 +0000917{
918 ++serialno;
919}
920
921
Tim Petersddea2082002-03-23 10:03:50 +0000922/* Read 4 bytes at p as a big-endian ulong. */
923static ulong
924read4(const void *p)
925{
Tim Peters62c06ba2002-03-23 22:28:18 +0000926 const uchar *q = (const uchar *)p;
Tim Petersddea2082002-03-23 10:03:50 +0000927 return ((ulong)q[0] << 24) |
928 ((ulong)q[1] << 16) |
929 ((ulong)q[2] << 8) |
930 (ulong)q[3];
931}
932
933/* Write the 4 least-significant bytes of n as a big-endian unsigned int,
934 MSB at address p, LSB at p+3. */
935static void
936write4(void *p, ulong n)
937{
Tim Peters62c06ba2002-03-23 22:28:18 +0000938 uchar *q = (uchar *)p;
939 q[0] = (uchar)((n >> 24) & 0xff);
940 q[1] = (uchar)((n >> 16) & 0xff);
941 q[2] = (uchar)((n >> 8) & 0xff);
942 q[3] = (uchar)( n & 0xff);
Tim Petersddea2082002-03-23 10:03:50 +0000943}
944
Tim Petersddea2082002-03-23 10:03:50 +0000945/* The debug malloc asks for 16 extra bytes and fills them with useful stuff,
946 here calling the underlying malloc's result p:
947
948p[0:4]
949 Number of bytes originally asked for. 4-byte unsigned integer,
950 big-endian (easier to read in a memory dump).
Tim Petersd1139e02002-03-28 07:32:11 +0000951p[4:8]
Tim Petersddea2082002-03-23 10:03:50 +0000952 Copies of PYMALLOC_FORBIDDENBYTE. Used to catch under- writes
953 and reads.
954p[8:8+n]
955 The requested memory, filled with copies of PYMALLOC_CLEANBYTE.
956 Used to catch reference to uninitialized memory.
957 &p[8] is returned. Note that this is 8-byte aligned if PyMalloc
958 handled the request itself.
959p[8+n:8+n+4]
960 Copies of PYMALLOC_FORBIDDENBYTE. Used to catch over- writes
961 and reads.
962p[8+n+4:8+n+8]
963 A serial number, incremented by 1 on each call to _PyMalloc_DebugMalloc
964 and _PyMalloc_DebugRealloc.
965 4-byte unsigned integer, big-endian.
966 If "bad memory" is detected later, the serial number gives an
967 excellent way to set a breakpoint on the next run, to capture the
968 instant at which this block was passed out.
969*/
970
971void *
Tim Petersd1139e02002-03-28 07:32:11 +0000972_PyMalloc_DebugMalloc(size_t nbytes)
Tim Petersddea2082002-03-23 10:03:50 +0000973{
974 uchar *p; /* base address of malloc'ed block */
Tim Peters62c06ba2002-03-23 22:28:18 +0000975 uchar *tail; /* p + 8 + nbytes == pointer to tail pad bytes */
Tim Petersddea2082002-03-23 10:03:50 +0000976 size_t total; /* nbytes + 16 */
977
Tim Peterse0850172002-03-24 00:34:21 +0000978 bumpserialno();
Tim Petersddea2082002-03-23 10:03:50 +0000979 total = nbytes + 16;
980 if (total < nbytes || (total >> 31) > 1) {
981 /* overflow, or we can't represent it in 4 bytes */
982 /* Obscure: can't do (total >> 32) != 0 instead, because
983 C doesn't define what happens for a right-shift of 32
984 when size_t is a 32-bit type. At least C guarantees
985 size_t is an unsigned type. */
986 return NULL;
987 }
988
Tim Petersd1139e02002-03-28 07:32:11 +0000989 p = _PyMalloc_Malloc(total);
Tim Petersddea2082002-03-23 10:03:50 +0000990 if (p == NULL)
991 return NULL;
992
993 write4(p, nbytes);
Tim Petersd1139e02002-03-28 07:32:11 +0000994 p[4] = p[5] = p[6] = p[7] = PYMALLOC_FORBIDDENBYTE;
Tim Petersddea2082002-03-23 10:03:50 +0000995
996 if (nbytes > 0)
997 memset(p+8, PYMALLOC_CLEANBYTE, nbytes);
998
Tim Peters62c06ba2002-03-23 22:28:18 +0000999 tail = p + 8 + nbytes;
1000 tail[0] = tail[1] = tail[2] = tail[3] = PYMALLOC_FORBIDDENBYTE;
1001 write4(tail + 4, serialno);
Tim Petersddea2082002-03-23 10:03:50 +00001002
1003 return p+8;
1004}
1005
Tim Peters62c06ba2002-03-23 22:28:18 +00001006/* The debug free first checks the 8 bytes on each end for sanity (in
1007 particular, that the PYMALLOC_FORBIDDENBYTEs are still intact).
Tim Petersddea2082002-03-23 10:03:50 +00001008 Then fills the original bytes with PYMALLOC_DEADBYTE.
1009 Then calls the underlying free.
1010*/
1011void
Tim Petersd1139e02002-03-28 07:32:11 +00001012_PyMalloc_DebugFree(void *p)
Tim Petersddea2082002-03-23 10:03:50 +00001013{
Tim Peters62c06ba2002-03-23 22:28:18 +00001014 uchar *q = (uchar *)p;
Tim Petersddea2082002-03-23 10:03:50 +00001015 size_t nbytes;
1016
Tim Petersddea2082002-03-23 10:03:50 +00001017 if (p == NULL)
1018 return;
Tim Petersddea2082002-03-23 10:03:50 +00001019 _PyMalloc_DebugCheckAddress(p);
1020 nbytes = read4(q-8);
1021 if (nbytes > 0)
1022 memset(q, PYMALLOC_DEADBYTE, nbytes);
Tim Petersd1139e02002-03-28 07:32:11 +00001023 _PyMalloc_Free(q-8);
Tim Petersddea2082002-03-23 10:03:50 +00001024}
1025
1026void *
Tim Petersd1139e02002-03-28 07:32:11 +00001027_PyMalloc_DebugRealloc(void *p, size_t nbytes)
Tim Petersddea2082002-03-23 10:03:50 +00001028{
1029 uchar *q = (uchar *)p;
1030 size_t original_nbytes;
Tim Peterse0850172002-03-24 00:34:21 +00001031 void *fresh; /* new memory block, if needed */
Tim Petersddea2082002-03-23 10:03:50 +00001032
Tim Petersddea2082002-03-23 10:03:50 +00001033 if (p == NULL)
Tim Petersd1139e02002-03-28 07:32:11 +00001034 return _PyMalloc_DebugMalloc(nbytes);
Tim Petersddea2082002-03-23 10:03:50 +00001035
Tim Petersddea2082002-03-23 10:03:50 +00001036 _PyMalloc_DebugCheckAddress(p);
Tim Petersddea2082002-03-23 10:03:50 +00001037 original_nbytes = read4(q-8);
1038 if (nbytes == original_nbytes) {
1039 /* note that this case is likely to be common due to the
1040 way Python appends to lists */
Tim Peterse0850172002-03-24 00:34:21 +00001041 bumpserialno();
Tim Petersddea2082002-03-23 10:03:50 +00001042 write4(q + nbytes + 4, serialno);
1043 return p;
1044 }
1045
1046 if (nbytes < original_nbytes) {
1047 /* shrinking -- leave the guts alone, except to
1048 fill the excess with DEADBYTE */
1049 const size_t excess = original_nbytes - nbytes;
Tim Peterse0850172002-03-24 00:34:21 +00001050 bumpserialno();
Tim Petersddea2082002-03-23 10:03:50 +00001051 write4(q-8, nbytes);
1052 /* kill the excess bytes plus the trailing 8 pad bytes */
Tim Petersddea2082002-03-23 10:03:50 +00001053 q += nbytes;
1054 q[0] = q[1] = q[2] = q[3] = PYMALLOC_FORBIDDENBYTE;
1055 write4(q+4, serialno);
Tim Petersd1139e02002-03-28 07:32:11 +00001056 memset(q+8, PYMALLOC_DEADBYTE, excess);
Tim Petersddea2082002-03-23 10:03:50 +00001057 return p;
1058 }
1059
1060 /* More memory is needed: get it, copy over the first original_nbytes
1061 of the original data, and free the original memory. */
Tim Petersd1139e02002-03-28 07:32:11 +00001062 fresh = _PyMalloc_DebugMalloc(nbytes);
Tim Petersddea2082002-03-23 10:03:50 +00001063 if (fresh != NULL && original_nbytes > 0)
1064 memcpy(fresh, p, original_nbytes);
Tim Petersd1139e02002-03-28 07:32:11 +00001065 _PyMalloc_DebugFree(p);
Tim Petersddea2082002-03-23 10:03:50 +00001066 return fresh;
1067}
1068
Tim Peters7ccfadf2002-04-01 06:04:21 +00001069/* Check the forbidden bytes on both ends of the memory allocated for p.
1070 * If anything is wrong, print info to stderr via _PyMalloc_DebugDumpAddress,
1071 * and call Py_FatalError to kill the program.
1072 */
1073 void
Tim Petersddea2082002-03-23 10:03:50 +00001074_PyMalloc_DebugCheckAddress(const void *p)
1075{
1076 const uchar *q = (const uchar *)p;
Tim Petersd1139e02002-03-28 07:32:11 +00001077 char *msg;
1078 int i;
Tim Petersddea2082002-03-23 10:03:50 +00001079
Tim Petersd1139e02002-03-28 07:32:11 +00001080 if (p == NULL) {
Tim Petersddea2082002-03-23 10:03:50 +00001081 msg = "didn't expect a NULL pointer";
Tim Petersd1139e02002-03-28 07:32:11 +00001082 goto error;
1083 }
Tim Petersddea2082002-03-23 10:03:50 +00001084
Tim Petersd1139e02002-03-28 07:32:11 +00001085 for (i = 4; i >= 1; --i) {
1086 if (*(q-i) != PYMALLOC_FORBIDDENBYTE) {
1087 msg = "bad leading pad byte";
1088 goto error;
1089 }
1090 }
Tim Petersddea2082002-03-23 10:03:50 +00001091
Tim Petersd1139e02002-03-28 07:32:11 +00001092 {
Tim Petersddea2082002-03-23 10:03:50 +00001093 const ulong nbytes = read4(q-8);
1094 const uchar *tail = q + nbytes;
Tim Petersddea2082002-03-23 10:03:50 +00001095 for (i = 0; i < 4; ++i) {
1096 if (tail[i] != PYMALLOC_FORBIDDENBYTE) {
1097 msg = "bad trailing pad byte";
Tim Petersd1139e02002-03-28 07:32:11 +00001098 goto error;
Tim Petersddea2082002-03-23 10:03:50 +00001099 }
1100 }
1101 }
1102
Tim Petersd1139e02002-03-28 07:32:11 +00001103 return;
1104
1105error:
1106 _PyMalloc_DebugDumpAddress(p);
1107 Py_FatalError(msg);
Tim Petersddea2082002-03-23 10:03:50 +00001108}
1109
Tim Peters7ccfadf2002-04-01 06:04:21 +00001110/* Display info to stderr about the memory block at p. */
Tim Petersddea2082002-03-23 10:03:50 +00001111void
1112_PyMalloc_DebugDumpAddress(const void *p)
1113{
1114 const uchar *q = (const uchar *)p;
1115 const uchar *tail;
1116 ulong nbytes, serial;
Tim Petersd1139e02002-03-28 07:32:11 +00001117 int i;
Tim Petersddea2082002-03-23 10:03:50 +00001118
1119 fprintf(stderr, "Debug memory block at address p=%p:\n", p);
1120 if (p == NULL)
1121 return;
1122
1123 nbytes = read4(q-8);
1124 fprintf(stderr, " %lu bytes originally allocated\n", nbytes);
Tim Petersddea2082002-03-23 10:03:50 +00001125
1126 /* In case this is nuts, check the pad bytes before trying to read up
1127 the serial number (the address deref could blow up). */
1128
Tim Petersd1139e02002-03-28 07:32:11 +00001129 fputs(" the 4 pad bytes at p-4 are ", stderr);
1130 if (*(q-4) == PYMALLOC_FORBIDDENBYTE &&
1131 *(q-3) == PYMALLOC_FORBIDDENBYTE &&
Tim Petersddea2082002-03-23 10:03:50 +00001132 *(q-2) == PYMALLOC_FORBIDDENBYTE &&
1133 *(q-1) == PYMALLOC_FORBIDDENBYTE) {
Tim Peters62c06ba2002-03-23 22:28:18 +00001134 fputs("PYMALLOC_FORBIDDENBYTE, as expected\n", stderr);
Tim Petersddea2082002-03-23 10:03:50 +00001135 }
1136 else {
Tim Petersddea2082002-03-23 10:03:50 +00001137 fprintf(stderr, "not all PYMALLOC_FORBIDDENBYTE (0x%02x):\n",
1138 PYMALLOC_FORBIDDENBYTE);
Tim Petersd1139e02002-03-28 07:32:11 +00001139 for (i = 4; i >= 1; --i) {
Tim Petersddea2082002-03-23 10:03:50 +00001140 const uchar byte = *(q-i);
1141 fprintf(stderr, " at p-%d: 0x%02x", i, byte);
1142 if (byte != PYMALLOC_FORBIDDENBYTE)
1143 fputs(" *** OUCH", stderr);
1144 fputc('\n', stderr);
1145 }
1146 }
1147
1148 tail = q + nbytes;
1149 fprintf(stderr, " the 4 pad bytes at tail=%p are ", tail);
1150 if (tail[0] == PYMALLOC_FORBIDDENBYTE &&
1151 tail[1] == PYMALLOC_FORBIDDENBYTE &&
1152 tail[2] == PYMALLOC_FORBIDDENBYTE &&
1153 tail[3] == PYMALLOC_FORBIDDENBYTE) {
Tim Peters62c06ba2002-03-23 22:28:18 +00001154 fputs("PYMALLOC_FORBIDDENBYTE, as expected\n", stderr);
Tim Petersddea2082002-03-23 10:03:50 +00001155 }
1156 else {
Tim Petersddea2082002-03-23 10:03:50 +00001157 fprintf(stderr, "not all PYMALLOC_FORBIDDENBYTE (0x%02x):\n",
1158 PYMALLOC_FORBIDDENBYTE);
1159 for (i = 0; i < 4; ++i) {
1160 const uchar byte = tail[i];
1161 fprintf(stderr, " at tail+%d: 0x%02x",
1162 i, byte);
1163 if (byte != PYMALLOC_FORBIDDENBYTE)
1164 fputs(" *** OUCH", stderr);
1165 fputc('\n', stderr);
1166 }
1167 }
1168
1169 serial = read4(tail+4);
1170 fprintf(stderr, " the block was made by call #%lu to "
1171 "debug malloc/realloc\n", serial);
1172
1173 if (nbytes > 0) {
1174 int i = 0;
Tim Peters62c06ba2002-03-23 22:28:18 +00001175 fputs(" data at p:", stderr);
Tim Petersddea2082002-03-23 10:03:50 +00001176 /* print up to 8 bytes at the start */
1177 while (q < tail && i < 8) {
1178 fprintf(stderr, " %02x", *q);
1179 ++i;
1180 ++q;
1181 }
1182 /* and up to 8 at the end */
1183 if (q < tail) {
1184 if (tail - q > 8) {
Tim Peters62c06ba2002-03-23 22:28:18 +00001185 fputs(" ...", stderr);
Tim Petersddea2082002-03-23 10:03:50 +00001186 q = tail - 8;
1187 }
1188 while (q < tail) {
1189 fprintf(stderr, " %02x", *q);
1190 ++q;
1191 }
1192 }
Tim Peters62c06ba2002-03-23 22:28:18 +00001193 fputc('\n', stderr);
Tim Petersddea2082002-03-23 10:03:50 +00001194 }
1195}
1196
Tim Peters7ccfadf2002-04-01 06:04:21 +00001197/* Print summary info to stderr about the state of pymalloc's structures. */
1198void
1199_PyMalloc_DebugDumpStats(void)
1200{
1201 uint i;
1202 const uint numclasses = SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT;
1203 uint numfreepools = 0;
1204 /* # of pools per class index */
1205 ulong numpools[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT];
1206 /* # of allocated blocks per class index */
1207 ulong numblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT];
1208 /* # of free blocks per class index */
1209 ulong numfreeblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT];
1210 ulong grandtotal; /* total # of allocated bytes */
Tim Peters6169f092002-04-01 20:12:59 +00001211 ulong freegrandtotal; /* total # of available bytes in used pools */
Tim Peters7ccfadf2002-04-01 06:04:21 +00001212
1213 fprintf(stderr, "%u arenas * %d bytes/arena = %lu total bytes.\n",
1214 narenas, ARENA_SIZE, narenas * (ulong)ARENA_SIZE);
1215 fprintf(stderr, "Small block threshold = %d, in %u size classes.\n",
1216 SMALL_REQUEST_THRESHOLD, numclasses);
1217 fprintf(stderr, "pymalloc malloc+realloc called %lu times.\n",
1218 serialno);
1219
1220 for (i = 0; i < numclasses; ++i)
1221 numpools[i] = numblocks[i] = numfreeblocks[i] = 0;
1222
Tim Peters6169f092002-04-01 20:12:59 +00001223 /* Because full pools aren't linked to from anything, it's easiest
1224 * to march over all the arenas. If we're lucky, most of the memory
1225 * will be living in full pools -- would be a shame to miss them.
Tim Peters7ccfadf2002-04-01 06:04:21 +00001226 */
1227 for (i = 0; i < narenas; ++i) {
1228 uint poolsinarena;
1229 uint j;
1230 uptr base = arenas[i];
1231
1232 /* round up to pool alignment */
1233 poolsinarena = ARENA_SIZE / POOL_SIZE;
1234 if (base & (uptr)POOL_SIZE_MASK) {
1235 --poolsinarena;
1236 base &= ~(uptr)POOL_SIZE_MASK;
1237 base += POOL_SIZE;
1238 }
1239
1240 if (i == narenas - 1) {
1241 /* current arena may have raw memory at the end */
1242 numfreepools += nfreepools;
1243 poolsinarena -= nfreepools;
1244 }
1245
1246 /* visit every pool in the arena */
1247 for (j = 0; j < poolsinarena; ++j, base += POOL_SIZE) {
1248 poolp p = (poolp)base;
1249 if (p->ref.count == 0) {
1250 /* currently unused */
1251 ++numfreepools;
1252 continue;
1253 }
1254 ++numpools[p->szidx];
1255 numblocks[p->szidx] += p->ref.count;
1256 numfreeblocks[p->szidx] += p->capacity - p->ref.count;
1257 }
1258 }
1259
1260 fputc('\n', stderr);
1261 fprintf(stderr, "Number of unused pools: %u\n", numfreepools);
1262 fputc('\n', stderr);
1263 fputs("class num bytes num pools blocks in use avail blocks\n"
1264 "----- --------- --------- ------------- ------------\n",
1265 stderr);
1266
1267 grandtotal = freegrandtotal = 0;
1268 for (i = 0; i < numclasses; ++i) {
1269 ulong p = numpools[i];
1270 ulong b = numblocks[i];
1271 ulong f = numfreeblocks[i];
1272 uint size = (i+1) << ALIGNMENT_SHIFT;
1273 if (p == 0) {
1274 assert(b == 0 && f == 0);
1275 continue;
1276 }
1277 fprintf(stderr, "%5u %11u %11lu %15lu %13lu\n",
1278 i, size, p, b, f);
1279 grandtotal += b * size;
1280 freegrandtotal += f * size;
1281 }
1282 fputc('\n', stderr);
1283 fprintf(stderr, "Total bytes in allocated blocks: %lu\n",
1284 grandtotal);
1285 fprintf(stderr, "Total free bytes in used pools: %lu\n",
1286 freegrandtotal);
1287}
1288
Tim Petersddea2082002-03-23 10:03:50 +00001289#endif /* PYMALLOC_DEBUG */