blob: 3c6ae574100f7e1d6f6e2577bea6666011833a78 [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,
174 * between 1K and SYSTEM_PAGE_SIZE, that is: 1k, 2k, 4k, eventually 8k.
175 */
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
216#undef ushort
217#define ushort unsigned short /* assuming >= 16 bits */
218
219#undef uint
220#define uint unsigned int /* assuming >= 16 bits */
221
222#undef ulong
223#define ulong unsigned long /* assuming >= 32 bits */
224
Tim Petersd97a1c02002-03-30 06:09:22 +0000225#undef uptr
226#define uptr Py_uintptr_t
227
Neil Schemenauera35c6882001-02-27 04:45:05 +0000228/* When you say memory, my mind reasons in terms of (pointers to) blocks */
229typedef uchar block;
230
231/* Pool for small blocks */
232struct pool_header {
Tim Petersb2336522001-03-11 18:36:13 +0000233 union { block *_padding;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000234 uint count; } ref; /* number of allocated blocks */
235 block *freeblock; /* pool's free list head */
236 struct pool_header *nextpool; /* next pool of this size class */
237 struct pool_header *prevpool; /* previous pool "" */
Tim Petersd97a1c02002-03-30 06:09:22 +0000238 ulong arenaindex; /* index into arenas of base adr */
Neil Schemenauera35c6882001-02-27 04:45:05 +0000239 uint szidx; /* block size class index */
240 uint capacity; /* pool capacity in # of blocks */
241};
242
243typedef struct pool_header *poolp;
244
245#undef ROUNDUP
246#define ROUNDUP(x) (((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK)
247#define POOL_OVERHEAD ROUNDUP(sizeof(struct pool_header))
248
249#define DUMMY_SIZE_IDX 0xffff /* size class of newly cached pools */
250
Tim Petersd97a1c02002-03-30 06:09:22 +0000251/* Round pointer P down to the closest pool-aligned address <= P, as a poolp */
252#define POOL_ADDR(P) \
253 ((poolp)((uptr)(P) & ~(uptr)POOL_SIZE_MASK))
254
Neil Schemenauera35c6882001-02-27 04:45:05 +0000255/*==========================================================================*/
256
257/*
258 * This malloc lock
259 */
Tim Petersb2336522001-03-11 18:36:13 +0000260SIMPLELOCK_DECL(_malloc_lock);
261#define LOCK() SIMPLELOCK_LOCK(_malloc_lock)
262#define UNLOCK() SIMPLELOCK_UNLOCK(_malloc_lock)
263#define LOCK_INIT() SIMPLELOCK_INIT(_malloc_lock)
264#define LOCK_FINI() SIMPLELOCK_FINI(_malloc_lock)
Neil Schemenauera35c6882001-02-27 04:45:05 +0000265
266/*
267 * Pool table -- doubly linked lists of partially used pools
268 */
269#define PTA(x) ((poolp )((uchar *)&(usedpools[2*(x)]) - 2*sizeof(block *)))
270#define PT(x) PTA(x), PTA(x)
271
272static poolp usedpools[2 * ((NB_SMALL_SIZE_CLASSES + 7) / 8) * 8] = {
273 PT(0), PT(1), PT(2), PT(3), PT(4), PT(5), PT(6), PT(7)
274#if NB_SMALL_SIZE_CLASSES > 8
275 , PT(8), PT(9), PT(10), PT(11), PT(12), PT(13), PT(14), PT(15)
276#if NB_SMALL_SIZE_CLASSES > 16
277 , PT(16), PT(17), PT(18), PT(19), PT(20), PT(21), PT(22), PT(23)
278#if NB_SMALL_SIZE_CLASSES > 24
279 , PT(24), PT(25), PT(26), PT(27), PT(28), PT(29), PT(30), PT(31)
280#if NB_SMALL_SIZE_CLASSES > 32
281 , PT(32), PT(33), PT(34), PT(35), PT(36), PT(37), PT(38), PT(39)
282#if NB_SMALL_SIZE_CLASSES > 40
283 , PT(40), PT(41), PT(42), PT(43), PT(44), PT(45), PT(46), PT(47)
284#if NB_SMALL_SIZE_CLASSES > 48
285 , PT(48), PT(49), PT(50), PT(51), PT(52), PT(53), PT(54), PT(55)
286#if NB_SMALL_SIZE_CLASSES > 56
287 , PT(56), PT(57), PT(58), PT(59), PT(60), PT(61), PT(62), PT(63)
288#endif /* NB_SMALL_SIZE_CLASSES > 56 */
289#endif /* NB_SMALL_SIZE_CLASSES > 48 */
290#endif /* NB_SMALL_SIZE_CLASSES > 40 */
291#endif /* NB_SMALL_SIZE_CLASSES > 32 */
292#endif /* NB_SMALL_SIZE_CLASSES > 24 */
293#endif /* NB_SMALL_SIZE_CLASSES > 16 */
294#endif /* NB_SMALL_SIZE_CLASSES > 8 */
295};
296
297/*
298 * Free (cached) pools
299 */
300static poolp freepools = NULL; /* free list for cached pools */
301
Tim Petersd97a1c02002-03-30 06:09:22 +0000302/*==========================================================================*/
303/* Arena management. */
Neil Schemenauera35c6882001-02-27 04:45:05 +0000304
Tim Petersd97a1c02002-03-30 06:09:22 +0000305/* arenas is a vector of arena base addresses, in order of allocation time.
306 * arenas currently contains narenas entries, and has space allocated
307 * for at most maxarenas entries.
308 *
309 * CAUTION: See the long comment block about thread safety in new_arena():
310 * the code currently relies in deep ways on that this vector only grows,
311 * and only grows by appending at the end. For now we never return an arena
312 * to the OS.
313 */
314static uptr *arenas = NULL;
315static ulong narenas = 0;
316static ulong maxarenas = 0;
317
Tim Peters3c83df22002-03-30 07:04:41 +0000318/* Number of pools still available to be allocated in the current arena. */
319static uint nfreepools = 0;
Tim Petersd97a1c02002-03-30 06:09:22 +0000320
Tim Peters3c83df22002-03-30 07:04:41 +0000321/* Free space start address in current arena. This is pool-aligned. */
Tim Petersd97a1c02002-03-30 06:09:22 +0000322static block *arenabase = NULL;
323
324#if 0
325static ulong wasmine = 0;
326static ulong wasntmine = 0;
327
328static void
329dumpem(void *ptr)
330{
331 if (ptr)
332 printf("inserted new arena at %08x\n", ptr);
333 printf("# arenas %d\n", narenas);
334 printf("was mine %lu wasn't mine %lu\n", wasmine, wasntmine);
335}
336#define INCMINE ++wasmine
337#define INCTHEIRS ++wasntmine
338
339#else
340#define dumpem(ptr)
341#define INCMINE
342#define INCTHEIRS
343#endif
344
345/* Allocate a new arena and return its base address. If we run out of
346 * memory, return NULL.
347 */
348static block *
349new_arena(void)
350{
Tim Peters3c83df22002-03-30 07:04:41 +0000351 uint excess; /* number of bytes above pool alignment */
352 block *bp = (block *)PyMem_MALLOC(ARENA_SIZE);
Tim Petersd97a1c02002-03-30 06:09:22 +0000353 if (bp == NULL)
354 return NULL;
355
Tim Peters3c83df22002-03-30 07:04:41 +0000356 /* arenabase <- first pool-aligned address in the arena
357 nfreepools <- number of whole pools that fit after alignment */
358 arenabase = bp;
359 nfreepools = ARENA_SIZE / POOL_SIZE;
360 excess = (uint)bp & POOL_SIZE_MASK;
361 if (excess != 0) {
362 --nfreepools;
363 arenabase += POOL_SIZE - excess;
364 }
Tim Petersd97a1c02002-03-30 06:09:22 +0000365
366 /* Make room for a new entry in the arenas vector. */
367 if (arenas == NULL) {
368 arenas = (uptr *)PyMem_MALLOC(16 * sizeof(*arenas));
369 if (arenas == NULL)
370 goto error;
371 maxarenas = 16;
372 narenas = 0;
373 }
374 else if (narenas == maxarenas) {
375 /* Grow arenas. Don't use realloc: if this fails, we
376 * don't want to lose the base addresses we already have.
377 * Exceedingly subtle: Someone may be calling the pymalloc
378 * free via PyMem_{DEL, Del, FREE, Free} without holding the
379 *.GIL. Someone else may simultaneously be calling the
380 * pymalloc malloc while holding the GIL via, e.g.,
381 * PyObject_New. Now the pymalloc free may index into arenas
382 * for an address check, while the pymalloc malloc calls
383 * new_arena and we end up here to grow a new arena *and*
384 * grow the arenas vector. If the value for arenas pymalloc
385 * free picks up "vanishes" during this resize, anything may
386 * happen, and it would be an incredibly rare bug. Therefore
387 * the code here takes great pains to make sure that, at every
388 * moment, arenas always points to an intact vector of
389 * addresses. It doesn't matter whether arenas points to a
390 * wholly up-to-date vector when pymalloc free checks it in
391 * this case, because the only legal (and that even this is
392 * legal is debatable) way to call PyMem_{Del, etc} while not
393 * holding the GIL is if the memory being released is not
394 * object memory, i.e. if the address check in pymalloc free
395 * is supposed to fail. Having an incomplete vector can't
396 * make a supposed-to-fail case succeed by mistake (it could
397 * only make a supposed-to-succeed case fail by mistake).
398 * Read the above 50 times before changing anything in this
399 * block.
Tim Peters12300682002-03-30 06:20:23 +0000400 * XXX Fudge. This is still vulnerable: there's nothing
401 * XXX to stop the bad-guy thread from picking up the
402 * XXX current value of arenas, but not indexing off of it
403 * XXX until after the PyMem_FREE(oldarenas) below completes.
Tim Petersd97a1c02002-03-30 06:09:22 +0000404 */
405 uptr *oldarenas;
406 int newmax = maxarenas + (maxarenas >> 1);
407 uptr *p = (uptr *)PyMem_MALLOC(newmax * sizeof(*arenas));
408 if (p == NULL)
409 goto error;
410 memcpy(p, arenas, narenas * sizeof(*arenas));
411 oldarenas = arenas;
412 arenas = p;
413 PyMem_FREE(oldarenas);
414 maxarenas = newmax;
415 }
416
417 /* Append the new arena address to arenas. */
418 assert(narenas < maxarenas);
419 arenas[narenas] = (uptr)bp;
420 ++narenas;
421 dumpem(bp);
422 return bp;
423
424error:
425 PyMem_FREE(bp);
426 return NULL;
427}
428
429/* Return true if and only if P is an address that was allocated by
430 * pymalloc. I must be the index into arenas that the address claims
431 * to come from.
432 * Tricky: Letting B be the arena base address in arenas[I], P belongs to the
433 * arena if and only if
Tim Peters3c83df22002-03-30 07:04:41 +0000434 * B <= P < B + ARENA_SIZE
Tim Petersd97a1c02002-03-30 06:09:22 +0000435 * Subtracting B throughout, this is true iff
Tim Peters3c83df22002-03-30 07:04:41 +0000436 * 0 <= P-B < ARENA_SIZE
Tim Petersd97a1c02002-03-30 06:09:22 +0000437 * By using unsigned arithmetic, the "0 <=" half of the test can be skipped.
438 */
439#define ADDRESS_IN_RANGE(P, I) \
Tim Peters3c83df22002-03-30 07:04:41 +0000440 ((I) < narenas && (uptr)(P) - arenas[I] < (uptr)ARENA_SIZE)
Neil Schemenauera35c6882001-02-27 04:45:05 +0000441/*==========================================================================*/
442
443/* malloc */
444
445/*
446 * The basic blocks are ordered by decreasing execution frequency,
447 * which minimizes the number of jumps in the most common cases,
448 * improves branching prediction and instruction scheduling (small
449 * block allocations typically result in a couple of instructions).
450 * Unless the optimizer reorders everything, being too smart...
451 */
452
453void *
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000454_PyMalloc_Malloc(size_t nbytes)
Neil Schemenauera35c6882001-02-27 04:45:05 +0000455{
456 block *bp;
457 poolp pool;
458 poolp next;
459 uint size;
460
Neil Schemenauera35c6882001-02-27 04:45:05 +0000461 /*
462 * This implicitly redirects malloc(0)
463 */
464 if ((nbytes - 1) < SMALL_REQUEST_THRESHOLD) {
465 LOCK();
466 /*
467 * Most frequent paths first
468 */
469 size = (uint )(nbytes - 1) >> ALIGNMENT_SHIFT;
470 pool = usedpools[size + size];
471 if (pool != pool->nextpool) {
472 /*
473 * There is a used pool for this size class.
474 * Pick up the head block of its free list.
475 */
476 ++pool->ref.count;
477 bp = pool->freeblock;
478 if ((pool->freeblock = *(block **)bp) != NULL) {
479 UNLOCK();
480 return (void *)bp;
481 }
482 /*
483 * Reached the end of the free list, try to extend it
484 */
485 if (pool->ref.count < pool->capacity) {
486 /*
487 * There is room for another block
488 */
489 size++;
490 size <<= ALIGNMENT_SHIFT; /* block size */
491 pool->freeblock = (block *)pool + \
492 POOL_OVERHEAD + \
493 pool->ref.count * size;
494 *(block **)(pool->freeblock) = NULL;
495 UNLOCK();
496 return (void *)bp;
497 }
498 /*
499 * Pool is full, unlink from used pools
500 */
501 next = pool->nextpool;
502 pool = pool->prevpool;
503 next->prevpool = pool;
504 pool->nextpool = next;
505 UNLOCK();
506 return (void *)bp;
507 }
508 /*
509 * Try to get a cached free pool
510 */
511 pool = freepools;
512 if (pool != NULL) {
513 /*
514 * Unlink from cached pools
515 */
516 freepools = pool->nextpool;
517 init_pool:
518 /*
519 * Frontlink to used pools
520 */
521 next = usedpools[size + size]; /* == prev */
522 pool->nextpool = next;
523 pool->prevpool = next;
524 next->nextpool = pool;
525 next->prevpool = pool;
526 pool->ref.count = 1;
527 if (pool->szidx == size) {
528 /*
529 * Luckily, this pool last contained blocks
530 * of the same size class, so its header
531 * and free list are already initialized.
532 */
533 bp = pool->freeblock;
534 pool->freeblock = *(block **)bp;
535 UNLOCK();
536 return (void *)bp;
537 }
538 /*
539 * Initialize the pool header and free list
540 * then return the first block.
541 */
542 pool->szidx = size;
543 size++;
544 size <<= ALIGNMENT_SHIFT; /* block size */
545 bp = (block *)pool + POOL_OVERHEAD;
546 pool->freeblock = bp + size;
547 *(block **)(pool->freeblock) = NULL;
548 pool->capacity = (POOL_SIZE - POOL_OVERHEAD) / size;
549 UNLOCK();
550 return (void *)bp;
551 }
552 /*
553 * Allocate new pool
554 */
Tim Peters3c83df22002-03-30 07:04:41 +0000555 if (nfreepools) {
Neil Schemenauera35c6882001-02-27 04:45:05 +0000556 commit_pool:
Tim Peters3c83df22002-03-30 07:04:41 +0000557 --nfreepools;
558 pool = (poolp)arenabase;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000559 arenabase += POOL_SIZE;
Tim Petersd97a1c02002-03-30 06:09:22 +0000560 pool->arenaindex = narenas - 1;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000561 pool->szidx = DUMMY_SIZE_IDX;
562 goto init_pool;
563 }
564 /*
565 * Allocate new arena
566 */
567#ifdef WITH_MEMORY_LIMITS
Tim Petersd97a1c02002-03-30 06:09:22 +0000568 if (!(narenas < MAX_ARENAS)) {
Neil Schemenauera35c6882001-02-27 04:45:05 +0000569 UNLOCK();
570 goto redirect;
571 }
572#endif
Tim Petersd97a1c02002-03-30 06:09:22 +0000573 bp = new_arena();
574 if (bp != NULL)
575 goto commit_pool;
576 UNLOCK();
577 goto redirect;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000578 }
579
580 /* The small block allocator ends here. */
581
Tim Petersd97a1c02002-03-30 06:09:22 +0000582redirect:
Neil Schemenauera35c6882001-02-27 04:45:05 +0000583 /*
584 * Redirect the original request to the underlying (libc) allocator.
585 * We jump here on bigger requests, on error in the code above (as a
586 * last chance to serve the request) or when the max memory limit
587 * has been reached.
588 */
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000589 return (void *)PyMem_MALLOC(nbytes);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000590}
591
592/* free */
593
594void
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000595_PyMalloc_Free(void *p)
Neil Schemenauera35c6882001-02-27 04:45:05 +0000596{
597 poolp pool;
598 poolp next, prev;
599 uint size;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000600
Neil Schemenauera35c6882001-02-27 04:45:05 +0000601 if (p == NULL) /* free(NULL) has no effect */
602 return;
603
Tim Petersd97a1c02002-03-30 06:09:22 +0000604 pool = POOL_ADDR(p);
605 if (ADDRESS_IN_RANGE(p, pool->arenaindex)) {
606 /* We allocated this address. */
607 INCMINE;
608 LOCK();
609 /*
610 * At this point, the pool is not empty
611 */
612 if ((*(block **)p = pool->freeblock) == NULL) {
613 /*
614 * Pool was full
615 */
616 pool->freeblock = (block *)p;
617 --pool->ref.count;
618 /*
619 * Frontlink to used pools
620 * This mimics LRU pool usage for new allocations and
621 * targets optimal filling when several pools contain
622 * blocks of the same size class.
623 */
624 size = pool->szidx;
625 next = usedpools[size + size];
626 prev = next->prevpool;
627 pool->nextpool = next;
628 pool->prevpool = prev;
629 next->prevpool = pool;
630 prev->nextpool = pool;
631 UNLOCK();
632 return;
633 }
634 /*
635 * Pool was not full
636 */
637 pool->freeblock = (block *)p;
638 if (--pool->ref.count != 0) {
639 UNLOCK();
640 return;
641 }
642 /*
643 * Pool is now empty, unlink from used pools
644 */
645 next = pool->nextpool;
646 prev = pool->prevpool;
647 next->prevpool = prev;
648 prev->nextpool = next;
649 /*
650 * Frontlink to free pools
651 * This ensures that previously freed pools will be allocated
652 * later (being not referenced, they are perhaps paged out).
653 */
654 pool->nextpool = freepools;
655 freepools = pool;
656 UNLOCK();
Neil Schemenauera35c6882001-02-27 04:45:05 +0000657 return;
658 }
659
Tim Petersd97a1c02002-03-30 06:09:22 +0000660 /* We did not allocate this address. */
661 INCTHEIRS;
662 PyMem_FREE(p);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000663}
664
665/* realloc */
666
667void *
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000668_PyMalloc_Realloc(void *p, size_t nbytes)
Neil Schemenauera35c6882001-02-27 04:45:05 +0000669{
670 block *bp;
671 poolp pool;
672 uint size;
673
Neil Schemenauera35c6882001-02-27 04:45:05 +0000674 if (p == NULL)
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000675 return _PyMalloc_Malloc(nbytes);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000676
677 /* realloc(p, 0) on big blocks is redirected. */
Tim Petersd97a1c02002-03-30 06:09:22 +0000678 pool = POOL_ADDR(p);
679 if (ADDRESS_IN_RANGE(p, pool->arenaindex)) {
Neil Schemenauera35c6882001-02-27 04:45:05 +0000680 /* We're in charge of this block */
Tim Petersd97a1c02002-03-30 06:09:22 +0000681 INCMINE;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000682 size = (pool->szidx + 1) << ALIGNMENT_SHIFT; /* block size */
683 if (size >= nbytes) {
684 /* Don't bother if a smaller size was requested
685 except for realloc(p, 0) == free(p), ret NULL */
Tim Petersd97a1c02002-03-30 06:09:22 +0000686 /* XXX but Python guarantees that *its* flavor of
687 resize(p, 0) will not do a free or return NULL */
Neil Schemenauera35c6882001-02-27 04:45:05 +0000688 if (nbytes == 0) {
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000689 _PyMalloc_Free(p);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000690 bp = NULL;
691 }
692 else
693 bp = (block *)p;
694 }
695 else {
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000696 bp = (block *)_PyMalloc_Malloc(nbytes);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000697 if (bp != NULL) {
698 memcpy(bp, p, size);
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000699 _PyMalloc_Free(p);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000700 }
701 }
702 }
Tim Petersd97a1c02002-03-30 06:09:22 +0000703 else {
704 /* We haven't allocated this block */
705 INCTHEIRS;
706 if (nbytes <= SMALL_REQUEST_THRESHOLD && nbytes) {
707 /* small request */
708 size = nbytes;
709 bp = (block *)_PyMalloc_Malloc(nbytes);
710 if (bp != NULL) {
711 memcpy(bp, p, size);
712 _PyMalloc_Free(p);
713 }
714 }
715 else
716 bp = (block *)PyMem_REALLOC(p, nbytes);
717 }
Neil Schemenauera35c6882001-02-27 04:45:05 +0000718 return (void *)bp;
719}
720
Tim Peters1221c0a2002-03-23 00:20:15 +0000721#else /* ! WITH_PYMALLOC */
Tim Petersddea2082002-03-23 10:03:50 +0000722
723/*==========================================================================*/
724/* pymalloc not enabled: Redirect the entry points to the PyMem family. */
Tim Peters62c06ba2002-03-23 22:28:18 +0000725
Tim Petersce7fb9b2002-03-23 00:28:57 +0000726void *
727_PyMalloc_Malloc(size_t n)
Tim Peters1221c0a2002-03-23 00:20:15 +0000728{
729 return PyMem_MALLOC(n);
730}
731
Tim Petersce7fb9b2002-03-23 00:28:57 +0000732void *
733_PyMalloc_Realloc(void *p, size_t n)
Tim Peters1221c0a2002-03-23 00:20:15 +0000734{
735 return PyMem_REALLOC(p, n);
736}
737
738void
739_PyMalloc_Free(void *p)
740{
741 PyMem_FREE(p);
742}
743#endif /* WITH_PYMALLOC */
744
Tim Peters62c06ba2002-03-23 22:28:18 +0000745/*==========================================================================*/
746/* Regardless of whether pymalloc is enabled, export entry points for
747 * the object-oriented pymalloc functions.
748 */
749
Tim Petersce7fb9b2002-03-23 00:28:57 +0000750PyObject *
751_PyMalloc_New(PyTypeObject *tp)
Tim Peters1221c0a2002-03-23 00:20:15 +0000752{
753 PyObject *op;
754 op = (PyObject *) _PyMalloc_MALLOC(_PyObject_SIZE(tp));
755 if (op == NULL)
756 return PyErr_NoMemory();
757 return PyObject_INIT(op, tp);
758}
759
760PyVarObject *
761_PyMalloc_NewVar(PyTypeObject *tp, int nitems)
762{
763 PyVarObject *op;
764 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
765 op = (PyVarObject *) _PyMalloc_MALLOC(size);
766 if (op == NULL)
767 return (PyVarObject *)PyErr_NoMemory();
768 return PyObject_INIT_VAR(op, tp, nitems);
769}
770
771void
772_PyMalloc_Del(PyObject *op)
773{
774 _PyMalloc_FREE(op);
775}
Tim Petersddea2082002-03-23 10:03:50 +0000776
777#ifdef PYMALLOC_DEBUG
778/*==========================================================================*/
Tim Peters62c06ba2002-03-23 22:28:18 +0000779/* A x-platform debugging allocator. This doesn't manage memory directly,
780 * it wraps a real allocator, adding extra debugging info to the memory blocks.
781 */
Tim Petersddea2082002-03-23 10:03:50 +0000782
783#define PYMALLOC_CLEANBYTE 0xCB /* uninitialized memory */
784#define PYMALLOC_DEADBYTE 0xDB /* free()ed memory */
785#define PYMALLOC_FORBIDDENBYTE 0xFB /* unusable memory */
786
787static ulong serialno = 0; /* incremented on each debug {m,re}alloc */
788
Tim Peterse0850172002-03-24 00:34:21 +0000789/* serialno is always incremented via calling this routine. The point is
790 to supply a single place to set a breakpoint.
791*/
792static void
Neil Schemenauerbd02b142002-03-28 21:05:38 +0000793bumpserialno(void)
Tim Peterse0850172002-03-24 00:34:21 +0000794{
795 ++serialno;
796}
797
798
Tim Petersddea2082002-03-23 10:03:50 +0000799/* Read 4 bytes at p as a big-endian ulong. */
800static ulong
801read4(const void *p)
802{
Tim Peters62c06ba2002-03-23 22:28:18 +0000803 const uchar *q = (const uchar *)p;
Tim Petersddea2082002-03-23 10:03:50 +0000804 return ((ulong)q[0] << 24) |
805 ((ulong)q[1] << 16) |
806 ((ulong)q[2] << 8) |
807 (ulong)q[3];
808}
809
810/* Write the 4 least-significant bytes of n as a big-endian unsigned int,
811 MSB at address p, LSB at p+3. */
812static void
813write4(void *p, ulong n)
814{
Tim Peters62c06ba2002-03-23 22:28:18 +0000815 uchar *q = (uchar *)p;
816 q[0] = (uchar)((n >> 24) & 0xff);
817 q[1] = (uchar)((n >> 16) & 0xff);
818 q[2] = (uchar)((n >> 8) & 0xff);
819 q[3] = (uchar)( n & 0xff);
Tim Petersddea2082002-03-23 10:03:50 +0000820}
821
Tim Petersddea2082002-03-23 10:03:50 +0000822/* The debug malloc asks for 16 extra bytes and fills them with useful stuff,
823 here calling the underlying malloc's result p:
824
825p[0:4]
826 Number of bytes originally asked for. 4-byte unsigned integer,
827 big-endian (easier to read in a memory dump).
Tim Petersd1139e02002-03-28 07:32:11 +0000828p[4:8]
Tim Petersddea2082002-03-23 10:03:50 +0000829 Copies of PYMALLOC_FORBIDDENBYTE. Used to catch under- writes
830 and reads.
831p[8:8+n]
832 The requested memory, filled with copies of PYMALLOC_CLEANBYTE.
833 Used to catch reference to uninitialized memory.
834 &p[8] is returned. Note that this is 8-byte aligned if PyMalloc
835 handled the request itself.
836p[8+n:8+n+4]
837 Copies of PYMALLOC_FORBIDDENBYTE. Used to catch over- writes
838 and reads.
839p[8+n+4:8+n+8]
840 A serial number, incremented by 1 on each call to _PyMalloc_DebugMalloc
841 and _PyMalloc_DebugRealloc.
842 4-byte unsigned integer, big-endian.
843 If "bad memory" is detected later, the serial number gives an
844 excellent way to set a breakpoint on the next run, to capture the
845 instant at which this block was passed out.
846*/
847
848void *
Tim Petersd1139e02002-03-28 07:32:11 +0000849_PyMalloc_DebugMalloc(size_t nbytes)
Tim Petersddea2082002-03-23 10:03:50 +0000850{
851 uchar *p; /* base address of malloc'ed block */
Tim Peters62c06ba2002-03-23 22:28:18 +0000852 uchar *tail; /* p + 8 + nbytes == pointer to tail pad bytes */
Tim Petersddea2082002-03-23 10:03:50 +0000853 size_t total; /* nbytes + 16 */
854
Tim Peterse0850172002-03-24 00:34:21 +0000855 bumpserialno();
Tim Petersddea2082002-03-23 10:03:50 +0000856 total = nbytes + 16;
857 if (total < nbytes || (total >> 31) > 1) {
858 /* overflow, or we can't represent it in 4 bytes */
859 /* Obscure: can't do (total >> 32) != 0 instead, because
860 C doesn't define what happens for a right-shift of 32
861 when size_t is a 32-bit type. At least C guarantees
862 size_t is an unsigned type. */
863 return NULL;
864 }
865
Tim Petersd1139e02002-03-28 07:32:11 +0000866 p = _PyMalloc_Malloc(total);
Tim Petersddea2082002-03-23 10:03:50 +0000867 if (p == NULL)
868 return NULL;
869
870 write4(p, nbytes);
Tim Petersd1139e02002-03-28 07:32:11 +0000871 p[4] = p[5] = p[6] = p[7] = PYMALLOC_FORBIDDENBYTE;
Tim Petersddea2082002-03-23 10:03:50 +0000872
873 if (nbytes > 0)
874 memset(p+8, PYMALLOC_CLEANBYTE, nbytes);
875
Tim Peters62c06ba2002-03-23 22:28:18 +0000876 tail = p + 8 + nbytes;
877 tail[0] = tail[1] = tail[2] = tail[3] = PYMALLOC_FORBIDDENBYTE;
878 write4(tail + 4, serialno);
Tim Petersddea2082002-03-23 10:03:50 +0000879
880 return p+8;
881}
882
Tim Peters62c06ba2002-03-23 22:28:18 +0000883/* The debug free first checks the 8 bytes on each end for sanity (in
884 particular, that the PYMALLOC_FORBIDDENBYTEs are still intact).
Tim Petersddea2082002-03-23 10:03:50 +0000885 Then fills the original bytes with PYMALLOC_DEADBYTE.
886 Then calls the underlying free.
887*/
888void
Tim Petersd1139e02002-03-28 07:32:11 +0000889_PyMalloc_DebugFree(void *p)
Tim Petersddea2082002-03-23 10:03:50 +0000890{
Tim Peters62c06ba2002-03-23 22:28:18 +0000891 uchar *q = (uchar *)p;
Tim Petersddea2082002-03-23 10:03:50 +0000892 size_t nbytes;
893
Tim Petersddea2082002-03-23 10:03:50 +0000894 if (p == NULL)
895 return;
Tim Petersddea2082002-03-23 10:03:50 +0000896 _PyMalloc_DebugCheckAddress(p);
897 nbytes = read4(q-8);
898 if (nbytes > 0)
899 memset(q, PYMALLOC_DEADBYTE, nbytes);
Tim Petersd1139e02002-03-28 07:32:11 +0000900 _PyMalloc_Free(q-8);
Tim Petersddea2082002-03-23 10:03:50 +0000901}
902
903void *
Tim Petersd1139e02002-03-28 07:32:11 +0000904_PyMalloc_DebugRealloc(void *p, size_t nbytes)
Tim Petersddea2082002-03-23 10:03:50 +0000905{
906 uchar *q = (uchar *)p;
907 size_t original_nbytes;
Tim Peterse0850172002-03-24 00:34:21 +0000908 void *fresh; /* new memory block, if needed */
Tim Petersddea2082002-03-23 10:03:50 +0000909
Tim Petersddea2082002-03-23 10:03:50 +0000910 if (p == NULL)
Tim Petersd1139e02002-03-28 07:32:11 +0000911 return _PyMalloc_DebugMalloc(nbytes);
Tim Petersddea2082002-03-23 10:03:50 +0000912
Tim Petersddea2082002-03-23 10:03:50 +0000913 _PyMalloc_DebugCheckAddress(p);
Tim Petersddea2082002-03-23 10:03:50 +0000914 original_nbytes = read4(q-8);
915 if (nbytes == original_nbytes) {
916 /* note that this case is likely to be common due to the
917 way Python appends to lists */
Tim Peterse0850172002-03-24 00:34:21 +0000918 bumpserialno();
Tim Petersddea2082002-03-23 10:03:50 +0000919 write4(q + nbytes + 4, serialno);
920 return p;
921 }
922
923 if (nbytes < original_nbytes) {
924 /* shrinking -- leave the guts alone, except to
925 fill the excess with DEADBYTE */
926 const size_t excess = original_nbytes - nbytes;
Tim Peterse0850172002-03-24 00:34:21 +0000927 bumpserialno();
Tim Petersddea2082002-03-23 10:03:50 +0000928 write4(q-8, nbytes);
929 /* kill the excess bytes plus the trailing 8 pad bytes */
Tim Petersddea2082002-03-23 10:03:50 +0000930 q += nbytes;
931 q[0] = q[1] = q[2] = q[3] = PYMALLOC_FORBIDDENBYTE;
932 write4(q+4, serialno);
Tim Petersd1139e02002-03-28 07:32:11 +0000933 memset(q+8, PYMALLOC_DEADBYTE, excess);
Tim Petersddea2082002-03-23 10:03:50 +0000934 return p;
935 }
936
937 /* More memory is needed: get it, copy over the first original_nbytes
938 of the original data, and free the original memory. */
Tim Petersd1139e02002-03-28 07:32:11 +0000939 fresh = _PyMalloc_DebugMalloc(nbytes);
Tim Petersddea2082002-03-23 10:03:50 +0000940 if (fresh != NULL && original_nbytes > 0)
941 memcpy(fresh, p, original_nbytes);
Tim Petersd1139e02002-03-28 07:32:11 +0000942 _PyMalloc_DebugFree(p);
Tim Petersddea2082002-03-23 10:03:50 +0000943 return fresh;
944}
945
946void
947_PyMalloc_DebugCheckAddress(const void *p)
948{
949 const uchar *q = (const uchar *)p;
Tim Petersd1139e02002-03-28 07:32:11 +0000950 char *msg;
951 int i;
Tim Petersddea2082002-03-23 10:03:50 +0000952
Tim Petersd1139e02002-03-28 07:32:11 +0000953 if (p == NULL) {
Tim Petersddea2082002-03-23 10:03:50 +0000954 msg = "didn't expect a NULL pointer";
Tim Petersd1139e02002-03-28 07:32:11 +0000955 goto error;
956 }
Tim Petersddea2082002-03-23 10:03:50 +0000957
Tim Petersd1139e02002-03-28 07:32:11 +0000958 for (i = 4; i >= 1; --i) {
959 if (*(q-i) != PYMALLOC_FORBIDDENBYTE) {
960 msg = "bad leading pad byte";
961 goto error;
962 }
963 }
Tim Petersddea2082002-03-23 10:03:50 +0000964
Tim Petersd1139e02002-03-28 07:32:11 +0000965 {
Tim Petersddea2082002-03-23 10:03:50 +0000966 const ulong nbytes = read4(q-8);
967 const uchar *tail = q + nbytes;
Tim Petersddea2082002-03-23 10:03:50 +0000968 for (i = 0; i < 4; ++i) {
969 if (tail[i] != PYMALLOC_FORBIDDENBYTE) {
970 msg = "bad trailing pad byte";
Tim Petersd1139e02002-03-28 07:32:11 +0000971 goto error;
Tim Petersddea2082002-03-23 10:03:50 +0000972 }
973 }
974 }
975
Tim Petersd1139e02002-03-28 07:32:11 +0000976 return;
977
978error:
979 _PyMalloc_DebugDumpAddress(p);
980 Py_FatalError(msg);
Tim Petersddea2082002-03-23 10:03:50 +0000981}
982
983void
984_PyMalloc_DebugDumpAddress(const void *p)
985{
986 const uchar *q = (const uchar *)p;
987 const uchar *tail;
988 ulong nbytes, serial;
Tim Petersd1139e02002-03-28 07:32:11 +0000989 int i;
Tim Petersddea2082002-03-23 10:03:50 +0000990
991 fprintf(stderr, "Debug memory block at address p=%p:\n", p);
992 if (p == NULL)
993 return;
994
995 nbytes = read4(q-8);
996 fprintf(stderr, " %lu bytes originally allocated\n", nbytes);
Tim Petersddea2082002-03-23 10:03:50 +0000997
998 /* In case this is nuts, check the pad bytes before trying to read up
999 the serial number (the address deref could blow up). */
1000
Tim Petersd1139e02002-03-28 07:32:11 +00001001 fputs(" the 4 pad bytes at p-4 are ", stderr);
1002 if (*(q-4) == PYMALLOC_FORBIDDENBYTE &&
1003 *(q-3) == PYMALLOC_FORBIDDENBYTE &&
Tim Petersddea2082002-03-23 10:03:50 +00001004 *(q-2) == PYMALLOC_FORBIDDENBYTE &&
1005 *(q-1) == PYMALLOC_FORBIDDENBYTE) {
Tim Peters62c06ba2002-03-23 22:28:18 +00001006 fputs("PYMALLOC_FORBIDDENBYTE, as expected\n", stderr);
Tim Petersddea2082002-03-23 10:03:50 +00001007 }
1008 else {
Tim Petersddea2082002-03-23 10:03:50 +00001009 fprintf(stderr, "not all PYMALLOC_FORBIDDENBYTE (0x%02x):\n",
1010 PYMALLOC_FORBIDDENBYTE);
Tim Petersd1139e02002-03-28 07:32:11 +00001011 for (i = 4; i >= 1; --i) {
Tim Petersddea2082002-03-23 10:03:50 +00001012 const uchar byte = *(q-i);
1013 fprintf(stderr, " at p-%d: 0x%02x", i, byte);
1014 if (byte != PYMALLOC_FORBIDDENBYTE)
1015 fputs(" *** OUCH", stderr);
1016 fputc('\n', stderr);
1017 }
1018 }
1019
1020 tail = q + nbytes;
1021 fprintf(stderr, " the 4 pad bytes at tail=%p are ", tail);
1022 if (tail[0] == PYMALLOC_FORBIDDENBYTE &&
1023 tail[1] == PYMALLOC_FORBIDDENBYTE &&
1024 tail[2] == PYMALLOC_FORBIDDENBYTE &&
1025 tail[3] == PYMALLOC_FORBIDDENBYTE) {
Tim Peters62c06ba2002-03-23 22:28:18 +00001026 fputs("PYMALLOC_FORBIDDENBYTE, as expected\n", stderr);
Tim Petersddea2082002-03-23 10:03:50 +00001027 }
1028 else {
Tim Petersddea2082002-03-23 10:03:50 +00001029 fprintf(stderr, "not all PYMALLOC_FORBIDDENBYTE (0x%02x):\n",
1030 PYMALLOC_FORBIDDENBYTE);
1031 for (i = 0; i < 4; ++i) {
1032 const uchar byte = tail[i];
1033 fprintf(stderr, " at tail+%d: 0x%02x",
1034 i, byte);
1035 if (byte != PYMALLOC_FORBIDDENBYTE)
1036 fputs(" *** OUCH", stderr);
1037 fputc('\n', stderr);
1038 }
1039 }
1040
1041 serial = read4(tail+4);
1042 fprintf(stderr, " the block was made by call #%lu to "
1043 "debug malloc/realloc\n", serial);
1044
1045 if (nbytes > 0) {
1046 int i = 0;
Tim Peters62c06ba2002-03-23 22:28:18 +00001047 fputs(" data at p:", stderr);
Tim Petersddea2082002-03-23 10:03:50 +00001048 /* print up to 8 bytes at the start */
1049 while (q < tail && i < 8) {
1050 fprintf(stderr, " %02x", *q);
1051 ++i;
1052 ++q;
1053 }
1054 /* and up to 8 at the end */
1055 if (q < tail) {
1056 if (tail - q > 8) {
Tim Peters62c06ba2002-03-23 22:28:18 +00001057 fputs(" ...", stderr);
Tim Petersddea2082002-03-23 10:03:50 +00001058 q = tail - 8;
1059 }
1060 while (q < tail) {
1061 fprintf(stderr, " %02x", *q);
1062 ++q;
1063 }
1064 }
Tim Peters62c06ba2002-03-23 22:28:18 +00001065 fputc('\n', stderr);
Tim Petersddea2082002-03-23 10:03:50 +00001066 }
1067}
1068
1069#endif /* PYMALLOC_DEBUG */