blob: 49eaacd69fdb5f920fb8a7ed213cf364131ed61c [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
225#undef off_t
226#define off_t uint /* 16 bits <= off_t <= 64 bits */
227
Tim Petersd97a1c02002-03-30 06:09:22 +0000228#undef uptr
229#define uptr Py_uintptr_t
230
Neil Schemenauera35c6882001-02-27 04:45:05 +0000231/* When you say memory, my mind reasons in terms of (pointers to) blocks */
232typedef uchar block;
233
234/* Pool for small blocks */
235struct pool_header {
Tim Petersb2336522001-03-11 18:36:13 +0000236 union { block *_padding;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000237 uint count; } ref; /* number of allocated blocks */
238 block *freeblock; /* pool's free list head */
239 struct pool_header *nextpool; /* next pool of this size class */
240 struct pool_header *prevpool; /* previous pool "" */
Tim Petersd97a1c02002-03-30 06:09:22 +0000241 ulong arenaindex; /* index into arenas of base adr */
Neil Schemenauera35c6882001-02-27 04:45:05 +0000242 uint szidx; /* block size class index */
243 uint capacity; /* pool capacity in # of blocks */
244};
245
246typedef struct pool_header *poolp;
247
248#undef ROUNDUP
249#define ROUNDUP(x) (((x) + ALIGNMENT_MASK) & ~ALIGNMENT_MASK)
250#define POOL_OVERHEAD ROUNDUP(sizeof(struct pool_header))
251
252#define DUMMY_SIZE_IDX 0xffff /* size class of newly cached pools */
253
Tim Petersd97a1c02002-03-30 06:09:22 +0000254/* Round pointer P down to the closest pool-aligned address <= P, as a poolp */
255#define POOL_ADDR(P) \
256 ((poolp)((uptr)(P) & ~(uptr)POOL_SIZE_MASK))
257
Neil Schemenauera35c6882001-02-27 04:45:05 +0000258/*==========================================================================*/
259
260/*
261 * This malloc lock
262 */
Tim Petersb2336522001-03-11 18:36:13 +0000263SIMPLELOCK_DECL(_malloc_lock);
264#define LOCK() SIMPLELOCK_LOCK(_malloc_lock)
265#define UNLOCK() SIMPLELOCK_UNLOCK(_malloc_lock)
266#define LOCK_INIT() SIMPLELOCK_INIT(_malloc_lock)
267#define LOCK_FINI() SIMPLELOCK_FINI(_malloc_lock)
Neil Schemenauera35c6882001-02-27 04:45:05 +0000268
269/*
270 * Pool table -- doubly linked lists of partially used pools
271 */
272#define PTA(x) ((poolp )((uchar *)&(usedpools[2*(x)]) - 2*sizeof(block *)))
273#define PT(x) PTA(x), PTA(x)
274
275static poolp usedpools[2 * ((NB_SMALL_SIZE_CLASSES + 7) / 8) * 8] = {
276 PT(0), PT(1), PT(2), PT(3), PT(4), PT(5), PT(6), PT(7)
277#if NB_SMALL_SIZE_CLASSES > 8
278 , PT(8), PT(9), PT(10), PT(11), PT(12), PT(13), PT(14), PT(15)
279#if NB_SMALL_SIZE_CLASSES > 16
280 , PT(16), PT(17), PT(18), PT(19), PT(20), PT(21), PT(22), PT(23)
281#if NB_SMALL_SIZE_CLASSES > 24
282 , PT(24), PT(25), PT(26), PT(27), PT(28), PT(29), PT(30), PT(31)
283#if NB_SMALL_SIZE_CLASSES > 32
284 , PT(32), PT(33), PT(34), PT(35), PT(36), PT(37), PT(38), PT(39)
285#if NB_SMALL_SIZE_CLASSES > 40
286 , PT(40), PT(41), PT(42), PT(43), PT(44), PT(45), PT(46), PT(47)
287#if NB_SMALL_SIZE_CLASSES > 48
288 , PT(48), PT(49), PT(50), PT(51), PT(52), PT(53), PT(54), PT(55)
289#if NB_SMALL_SIZE_CLASSES > 56
290 , PT(56), PT(57), PT(58), PT(59), PT(60), PT(61), PT(62), PT(63)
291#endif /* NB_SMALL_SIZE_CLASSES > 56 */
292#endif /* NB_SMALL_SIZE_CLASSES > 48 */
293#endif /* NB_SMALL_SIZE_CLASSES > 40 */
294#endif /* NB_SMALL_SIZE_CLASSES > 32 */
295#endif /* NB_SMALL_SIZE_CLASSES > 24 */
296#endif /* NB_SMALL_SIZE_CLASSES > 16 */
297#endif /* NB_SMALL_SIZE_CLASSES > 8 */
298};
299
300/*
301 * Free (cached) pools
302 */
303static poolp freepools = NULL; /* free list for cached pools */
304
Tim Petersd97a1c02002-03-30 06:09:22 +0000305/*==========================================================================*/
306/* Arena management. */
Neil Schemenauera35c6882001-02-27 04:45:05 +0000307
Tim Petersd97a1c02002-03-30 06:09:22 +0000308/* arenas is a vector of arena base addresses, in order of allocation time.
309 * arenas currently contains narenas entries, and has space allocated
310 * for at most maxarenas entries.
311 *
312 * CAUTION: See the long comment block about thread safety in new_arena():
313 * the code currently relies in deep ways on that this vector only grows,
314 * and only grows by appending at the end. For now we never return an arena
315 * to the OS.
316 */
317static uptr *arenas = NULL;
318static ulong narenas = 0;
319static ulong maxarenas = 0;
320
Tim Peters3c83df22002-03-30 07:04:41 +0000321/* Number of pools still available to be allocated in the current arena. */
322static uint nfreepools = 0;
Tim Petersd97a1c02002-03-30 06:09:22 +0000323
Tim Peters3c83df22002-03-30 07:04:41 +0000324/* Free space start address in current arena. This is pool-aligned. */
Tim Petersd97a1c02002-03-30 06:09:22 +0000325static block *arenabase = NULL;
326
327#if 0
328static ulong wasmine = 0;
329static ulong wasntmine = 0;
330
331static void
332dumpem(void *ptr)
333{
334 if (ptr)
335 printf("inserted new arena at %08x\n", ptr);
336 printf("# arenas %d\n", narenas);
337 printf("was mine %lu wasn't mine %lu\n", wasmine, wasntmine);
338}
339#define INCMINE ++wasmine
340#define INCTHEIRS ++wasntmine
341
342#else
343#define dumpem(ptr)
344#define INCMINE
345#define INCTHEIRS
346#endif
347
348/* Allocate a new arena and return its base address. If we run out of
349 * memory, return NULL.
350 */
351static block *
352new_arena(void)
353{
Tim Peters3c83df22002-03-30 07:04:41 +0000354 uint excess; /* number of bytes above pool alignment */
355 block *bp = (block *)PyMem_MALLOC(ARENA_SIZE);
Tim Petersd97a1c02002-03-30 06:09:22 +0000356 if (bp == NULL)
357 return NULL;
358
Tim Peters3c83df22002-03-30 07:04:41 +0000359 /* arenabase <- first pool-aligned address in the arena
360 nfreepools <- number of whole pools that fit after alignment */
361 arenabase = bp;
362 nfreepools = ARENA_SIZE / POOL_SIZE;
363 excess = (uint)bp & POOL_SIZE_MASK;
364 if (excess != 0) {
365 --nfreepools;
366 arenabase += POOL_SIZE - excess;
367 }
Tim Petersd97a1c02002-03-30 06:09:22 +0000368
369 /* Make room for a new entry in the arenas vector. */
370 if (arenas == NULL) {
371 arenas = (uptr *)PyMem_MALLOC(16 * sizeof(*arenas));
372 if (arenas == NULL)
373 goto error;
374 maxarenas = 16;
375 narenas = 0;
376 }
377 else if (narenas == maxarenas) {
378 /* Grow arenas. Don't use realloc: if this fails, we
379 * don't want to lose the base addresses we already have.
380 * Exceedingly subtle: Someone may be calling the pymalloc
381 * free via PyMem_{DEL, Del, FREE, Free} without holding the
382 *.GIL. Someone else may simultaneously be calling the
383 * pymalloc malloc while holding the GIL via, e.g.,
384 * PyObject_New. Now the pymalloc free may index into arenas
385 * for an address check, while the pymalloc malloc calls
386 * new_arena and we end up here to grow a new arena *and*
387 * grow the arenas vector. If the value for arenas pymalloc
388 * free picks up "vanishes" during this resize, anything may
389 * happen, and it would be an incredibly rare bug. Therefore
390 * the code here takes great pains to make sure that, at every
391 * moment, arenas always points to an intact vector of
392 * addresses. It doesn't matter whether arenas points to a
393 * wholly up-to-date vector when pymalloc free checks it in
394 * this case, because the only legal (and that even this is
395 * legal is debatable) way to call PyMem_{Del, etc} while not
396 * holding the GIL is if the memory being released is not
397 * object memory, i.e. if the address check in pymalloc free
398 * is supposed to fail. Having an incomplete vector can't
399 * make a supposed-to-fail case succeed by mistake (it could
400 * only make a supposed-to-succeed case fail by mistake).
401 * Read the above 50 times before changing anything in this
402 * block.
Tim Peters12300682002-03-30 06:20:23 +0000403 * XXX Fudge. This is still vulnerable: there's nothing
404 * XXX to stop the bad-guy thread from picking up the
405 * XXX current value of arenas, but not indexing off of it
406 * XXX until after the PyMem_FREE(oldarenas) below completes.
Tim Petersd97a1c02002-03-30 06:09:22 +0000407 */
408 uptr *oldarenas;
409 int newmax = maxarenas + (maxarenas >> 1);
410 uptr *p = (uptr *)PyMem_MALLOC(newmax * sizeof(*arenas));
411 if (p == NULL)
412 goto error;
413 memcpy(p, arenas, narenas * sizeof(*arenas));
414 oldarenas = arenas;
415 arenas = p;
416 PyMem_FREE(oldarenas);
417 maxarenas = newmax;
418 }
419
420 /* Append the new arena address to arenas. */
421 assert(narenas < maxarenas);
422 arenas[narenas] = (uptr)bp;
423 ++narenas;
424 dumpem(bp);
425 return bp;
426
427error:
428 PyMem_FREE(bp);
429 return NULL;
430}
431
432/* Return true if and only if P is an address that was allocated by
433 * pymalloc. I must be the index into arenas that the address claims
434 * to come from.
435 * Tricky: Letting B be the arena base address in arenas[I], P belongs to the
436 * arena if and only if
Tim Peters3c83df22002-03-30 07:04:41 +0000437 * B <= P < B + ARENA_SIZE
Tim Petersd97a1c02002-03-30 06:09:22 +0000438 * Subtracting B throughout, this is true iff
Tim Peters3c83df22002-03-30 07:04:41 +0000439 * 0 <= P-B < ARENA_SIZE
Tim Petersd97a1c02002-03-30 06:09:22 +0000440 * By using unsigned arithmetic, the "0 <=" half of the test can be skipped.
441 */
442#define ADDRESS_IN_RANGE(P, I) \
Tim Peters3c83df22002-03-30 07:04:41 +0000443 ((I) < narenas && (uptr)(P) - arenas[I] < (uptr)ARENA_SIZE)
Neil Schemenauera35c6882001-02-27 04:45:05 +0000444/*==========================================================================*/
445
446/* malloc */
447
448/*
449 * The basic blocks are ordered by decreasing execution frequency,
450 * which minimizes the number of jumps in the most common cases,
451 * improves branching prediction and instruction scheduling (small
452 * block allocations typically result in a couple of instructions).
453 * Unless the optimizer reorders everything, being too smart...
454 */
455
456void *
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000457_PyMalloc_Malloc(size_t nbytes)
Neil Schemenauera35c6882001-02-27 04:45:05 +0000458{
459 block *bp;
460 poolp pool;
461 poolp next;
462 uint size;
463
Neil Schemenauera35c6882001-02-27 04:45:05 +0000464 /*
465 * This implicitly redirects malloc(0)
466 */
467 if ((nbytes - 1) < SMALL_REQUEST_THRESHOLD) {
468 LOCK();
469 /*
470 * Most frequent paths first
471 */
472 size = (uint )(nbytes - 1) >> ALIGNMENT_SHIFT;
473 pool = usedpools[size + size];
474 if (pool != pool->nextpool) {
475 /*
476 * There is a used pool for this size class.
477 * Pick up the head block of its free list.
478 */
479 ++pool->ref.count;
480 bp = pool->freeblock;
481 if ((pool->freeblock = *(block **)bp) != NULL) {
482 UNLOCK();
483 return (void *)bp;
484 }
485 /*
486 * Reached the end of the free list, try to extend it
487 */
488 if (pool->ref.count < pool->capacity) {
489 /*
490 * There is room for another block
491 */
492 size++;
493 size <<= ALIGNMENT_SHIFT; /* block size */
494 pool->freeblock = (block *)pool + \
495 POOL_OVERHEAD + \
496 pool->ref.count * size;
497 *(block **)(pool->freeblock) = NULL;
498 UNLOCK();
499 return (void *)bp;
500 }
501 /*
502 * Pool is full, unlink from used pools
503 */
504 next = pool->nextpool;
505 pool = pool->prevpool;
506 next->prevpool = pool;
507 pool->nextpool = next;
508 UNLOCK();
509 return (void *)bp;
510 }
511 /*
512 * Try to get a cached free pool
513 */
514 pool = freepools;
515 if (pool != NULL) {
516 /*
517 * Unlink from cached pools
518 */
519 freepools = pool->nextpool;
520 init_pool:
521 /*
522 * Frontlink to used pools
523 */
524 next = usedpools[size + size]; /* == prev */
525 pool->nextpool = next;
526 pool->prevpool = next;
527 next->nextpool = pool;
528 next->prevpool = pool;
529 pool->ref.count = 1;
530 if (pool->szidx == size) {
531 /*
532 * Luckily, this pool last contained blocks
533 * of the same size class, so its header
534 * and free list are already initialized.
535 */
536 bp = pool->freeblock;
537 pool->freeblock = *(block **)bp;
538 UNLOCK();
539 return (void *)bp;
540 }
541 /*
542 * Initialize the pool header and free list
543 * then return the first block.
544 */
545 pool->szidx = size;
546 size++;
547 size <<= ALIGNMENT_SHIFT; /* block size */
548 bp = (block *)pool + POOL_OVERHEAD;
549 pool->freeblock = bp + size;
550 *(block **)(pool->freeblock) = NULL;
551 pool->capacity = (POOL_SIZE - POOL_OVERHEAD) / size;
552 UNLOCK();
553 return (void *)bp;
554 }
555 /*
556 * Allocate new pool
557 */
Tim Peters3c83df22002-03-30 07:04:41 +0000558 if (nfreepools) {
Neil Schemenauera35c6882001-02-27 04:45:05 +0000559 commit_pool:
Tim Peters3c83df22002-03-30 07:04:41 +0000560 --nfreepools;
561 pool = (poolp)arenabase;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000562 arenabase += POOL_SIZE;
Tim Petersd97a1c02002-03-30 06:09:22 +0000563 pool->arenaindex = narenas - 1;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000564 pool->szidx = DUMMY_SIZE_IDX;
565 goto init_pool;
566 }
567 /*
568 * Allocate new arena
569 */
570#ifdef WITH_MEMORY_LIMITS
Tim Petersd97a1c02002-03-30 06:09:22 +0000571 if (!(narenas < MAX_ARENAS)) {
Neil Schemenauera35c6882001-02-27 04:45:05 +0000572 UNLOCK();
573 goto redirect;
574 }
575#endif
Tim Petersd97a1c02002-03-30 06:09:22 +0000576 bp = new_arena();
577 if (bp != NULL)
578 goto commit_pool;
579 UNLOCK();
580 goto redirect;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000581 }
582
583 /* The small block allocator ends here. */
584
Tim Petersd97a1c02002-03-30 06:09:22 +0000585redirect:
Neil Schemenauera35c6882001-02-27 04:45:05 +0000586 /*
587 * Redirect the original request to the underlying (libc) allocator.
588 * We jump here on bigger requests, on error in the code above (as a
589 * last chance to serve the request) or when the max memory limit
590 * has been reached.
591 */
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000592 return (void *)PyMem_MALLOC(nbytes);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000593}
594
595/* free */
596
597void
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000598_PyMalloc_Free(void *p)
Neil Schemenauera35c6882001-02-27 04:45:05 +0000599{
600 poolp pool;
601 poolp next, prev;
602 uint size;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000603
Neil Schemenauera35c6882001-02-27 04:45:05 +0000604 if (p == NULL) /* free(NULL) has no effect */
605 return;
606
Tim Petersd97a1c02002-03-30 06:09:22 +0000607 pool = POOL_ADDR(p);
608 if (ADDRESS_IN_RANGE(p, pool->arenaindex)) {
609 /* We allocated this address. */
610 INCMINE;
611 LOCK();
612 /*
613 * At this point, the pool is not empty
614 */
615 if ((*(block **)p = pool->freeblock) == NULL) {
616 /*
617 * Pool was full
618 */
619 pool->freeblock = (block *)p;
620 --pool->ref.count;
621 /*
622 * Frontlink to used pools
623 * This mimics LRU pool usage for new allocations and
624 * targets optimal filling when several pools contain
625 * blocks of the same size class.
626 */
627 size = pool->szidx;
628 next = usedpools[size + size];
629 prev = next->prevpool;
630 pool->nextpool = next;
631 pool->prevpool = prev;
632 next->prevpool = pool;
633 prev->nextpool = pool;
634 UNLOCK();
635 return;
636 }
637 /*
638 * Pool was not full
639 */
640 pool->freeblock = (block *)p;
641 if (--pool->ref.count != 0) {
642 UNLOCK();
643 return;
644 }
645 /*
646 * Pool is now empty, unlink from used pools
647 */
648 next = pool->nextpool;
649 prev = pool->prevpool;
650 next->prevpool = prev;
651 prev->nextpool = next;
652 /*
653 * Frontlink to free pools
654 * This ensures that previously freed pools will be allocated
655 * later (being not referenced, they are perhaps paged out).
656 */
657 pool->nextpool = freepools;
658 freepools = pool;
659 UNLOCK();
Neil Schemenauera35c6882001-02-27 04:45:05 +0000660 return;
661 }
662
Tim Petersd97a1c02002-03-30 06:09:22 +0000663 /* We did not allocate this address. */
664 INCTHEIRS;
665 PyMem_FREE(p);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000666}
667
668/* realloc */
669
670void *
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000671_PyMalloc_Realloc(void *p, size_t nbytes)
Neil Schemenauera35c6882001-02-27 04:45:05 +0000672{
673 block *bp;
674 poolp pool;
675 uint size;
676
Neil Schemenauera35c6882001-02-27 04:45:05 +0000677 if (p == NULL)
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000678 return _PyMalloc_Malloc(nbytes);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000679
680 /* realloc(p, 0) on big blocks is redirected. */
Tim Petersd97a1c02002-03-30 06:09:22 +0000681 pool = POOL_ADDR(p);
682 if (ADDRESS_IN_RANGE(p, pool->arenaindex)) {
Neil Schemenauera35c6882001-02-27 04:45:05 +0000683 /* We're in charge of this block */
Tim Petersd97a1c02002-03-30 06:09:22 +0000684 INCMINE;
Neil Schemenauera35c6882001-02-27 04:45:05 +0000685 size = (pool->szidx + 1) << ALIGNMENT_SHIFT; /* block size */
686 if (size >= nbytes) {
687 /* Don't bother if a smaller size was requested
688 except for realloc(p, 0) == free(p), ret NULL */
Tim Petersd97a1c02002-03-30 06:09:22 +0000689 /* XXX but Python guarantees that *its* flavor of
690 resize(p, 0) will not do a free or return NULL */
Neil Schemenauera35c6882001-02-27 04:45:05 +0000691 if (nbytes == 0) {
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000692 _PyMalloc_Free(p);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000693 bp = NULL;
694 }
695 else
696 bp = (block *)p;
697 }
698 else {
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000699 bp = (block *)_PyMalloc_Malloc(nbytes);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000700 if (bp != NULL) {
701 memcpy(bp, p, size);
Neil Schemenauer25f3dc22002-03-18 21:06:21 +0000702 _PyMalloc_Free(p);
Neil Schemenauera35c6882001-02-27 04:45:05 +0000703 }
704 }
705 }
Tim Petersd97a1c02002-03-30 06:09:22 +0000706 else {
707 /* We haven't allocated this block */
708 INCTHEIRS;
709 if (nbytes <= SMALL_REQUEST_THRESHOLD && nbytes) {
710 /* small request */
711 size = nbytes;
712 bp = (block *)_PyMalloc_Malloc(nbytes);
713 if (bp != NULL) {
714 memcpy(bp, p, size);
715 _PyMalloc_Free(p);
716 }
717 }
718 else
719 bp = (block *)PyMem_REALLOC(p, nbytes);
720 }
Neil Schemenauera35c6882001-02-27 04:45:05 +0000721 return (void *)bp;
722}
723
Tim Peters1221c0a2002-03-23 00:20:15 +0000724#else /* ! WITH_PYMALLOC */
Tim Petersddea2082002-03-23 10:03:50 +0000725
726/*==========================================================================*/
727/* pymalloc not enabled: Redirect the entry points to the PyMem family. */
Tim Peters62c06ba2002-03-23 22:28:18 +0000728
Tim Petersce7fb9b2002-03-23 00:28:57 +0000729void *
730_PyMalloc_Malloc(size_t n)
Tim Peters1221c0a2002-03-23 00:20:15 +0000731{
732 return PyMem_MALLOC(n);
733}
734
Tim Petersce7fb9b2002-03-23 00:28:57 +0000735void *
736_PyMalloc_Realloc(void *p, size_t n)
Tim Peters1221c0a2002-03-23 00:20:15 +0000737{
738 return PyMem_REALLOC(p, n);
739}
740
741void
742_PyMalloc_Free(void *p)
743{
744 PyMem_FREE(p);
745}
746#endif /* WITH_PYMALLOC */
747
Tim Peters62c06ba2002-03-23 22:28:18 +0000748/*==========================================================================*/
749/* Regardless of whether pymalloc is enabled, export entry points for
750 * the object-oriented pymalloc functions.
751 */
752
Tim Petersce7fb9b2002-03-23 00:28:57 +0000753PyObject *
754_PyMalloc_New(PyTypeObject *tp)
Tim Peters1221c0a2002-03-23 00:20:15 +0000755{
756 PyObject *op;
757 op = (PyObject *) _PyMalloc_MALLOC(_PyObject_SIZE(tp));
758 if (op == NULL)
759 return PyErr_NoMemory();
760 return PyObject_INIT(op, tp);
761}
762
763PyVarObject *
764_PyMalloc_NewVar(PyTypeObject *tp, int nitems)
765{
766 PyVarObject *op;
767 const size_t size = _PyObject_VAR_SIZE(tp, nitems);
768 op = (PyVarObject *) _PyMalloc_MALLOC(size);
769 if (op == NULL)
770 return (PyVarObject *)PyErr_NoMemory();
771 return PyObject_INIT_VAR(op, tp, nitems);
772}
773
774void
775_PyMalloc_Del(PyObject *op)
776{
777 _PyMalloc_FREE(op);
778}
Tim Petersddea2082002-03-23 10:03:50 +0000779
780#ifdef PYMALLOC_DEBUG
781/*==========================================================================*/
Tim Peters62c06ba2002-03-23 22:28:18 +0000782/* A x-platform debugging allocator. This doesn't manage memory directly,
783 * it wraps a real allocator, adding extra debugging info to the memory blocks.
784 */
Tim Petersddea2082002-03-23 10:03:50 +0000785
786#define PYMALLOC_CLEANBYTE 0xCB /* uninitialized memory */
787#define PYMALLOC_DEADBYTE 0xDB /* free()ed memory */
788#define PYMALLOC_FORBIDDENBYTE 0xFB /* unusable memory */
789
790static ulong serialno = 0; /* incremented on each debug {m,re}alloc */
791
Tim Peterse0850172002-03-24 00:34:21 +0000792/* serialno is always incremented via calling this routine. The point is
793 to supply a single place to set a breakpoint.
794*/
795static void
Neil Schemenauerbd02b142002-03-28 21:05:38 +0000796bumpserialno(void)
Tim Peterse0850172002-03-24 00:34:21 +0000797{
798 ++serialno;
799}
800
801
Tim Petersddea2082002-03-23 10:03:50 +0000802/* Read 4 bytes at p as a big-endian ulong. */
803static ulong
804read4(const void *p)
805{
Tim Peters62c06ba2002-03-23 22:28:18 +0000806 const uchar *q = (const uchar *)p;
Tim Petersddea2082002-03-23 10:03:50 +0000807 return ((ulong)q[0] << 24) |
808 ((ulong)q[1] << 16) |
809 ((ulong)q[2] << 8) |
810 (ulong)q[3];
811}
812
813/* Write the 4 least-significant bytes of n as a big-endian unsigned int,
814 MSB at address p, LSB at p+3. */
815static void
816write4(void *p, ulong n)
817{
Tim Peters62c06ba2002-03-23 22:28:18 +0000818 uchar *q = (uchar *)p;
819 q[0] = (uchar)((n >> 24) & 0xff);
820 q[1] = (uchar)((n >> 16) & 0xff);
821 q[2] = (uchar)((n >> 8) & 0xff);
822 q[3] = (uchar)( n & 0xff);
Tim Petersddea2082002-03-23 10:03:50 +0000823}
824
Tim Petersddea2082002-03-23 10:03:50 +0000825/* The debug malloc asks for 16 extra bytes and fills them with useful stuff,
826 here calling the underlying malloc's result p:
827
828p[0:4]
829 Number of bytes originally asked for. 4-byte unsigned integer,
830 big-endian (easier to read in a memory dump).
Tim Petersd1139e02002-03-28 07:32:11 +0000831p[4:8]
Tim Petersddea2082002-03-23 10:03:50 +0000832 Copies of PYMALLOC_FORBIDDENBYTE. Used to catch under- writes
833 and reads.
834p[8:8+n]
835 The requested memory, filled with copies of PYMALLOC_CLEANBYTE.
836 Used to catch reference to uninitialized memory.
837 &p[8] is returned. Note that this is 8-byte aligned if PyMalloc
838 handled the request itself.
839p[8+n:8+n+4]
840 Copies of PYMALLOC_FORBIDDENBYTE. Used to catch over- writes
841 and reads.
842p[8+n+4:8+n+8]
843 A serial number, incremented by 1 on each call to _PyMalloc_DebugMalloc
844 and _PyMalloc_DebugRealloc.
845 4-byte unsigned integer, big-endian.
846 If "bad memory" is detected later, the serial number gives an
847 excellent way to set a breakpoint on the next run, to capture the
848 instant at which this block was passed out.
849*/
850
851void *
Tim Petersd1139e02002-03-28 07:32:11 +0000852_PyMalloc_DebugMalloc(size_t nbytes)
Tim Petersddea2082002-03-23 10:03:50 +0000853{
854 uchar *p; /* base address of malloc'ed block */
Tim Peters62c06ba2002-03-23 22:28:18 +0000855 uchar *tail; /* p + 8 + nbytes == pointer to tail pad bytes */
Tim Petersddea2082002-03-23 10:03:50 +0000856 size_t total; /* nbytes + 16 */
857
Tim Peterse0850172002-03-24 00:34:21 +0000858 bumpserialno();
Tim Petersddea2082002-03-23 10:03:50 +0000859 total = nbytes + 16;
860 if (total < nbytes || (total >> 31) > 1) {
861 /* overflow, or we can't represent it in 4 bytes */
862 /* Obscure: can't do (total >> 32) != 0 instead, because
863 C doesn't define what happens for a right-shift of 32
864 when size_t is a 32-bit type. At least C guarantees
865 size_t is an unsigned type. */
866 return NULL;
867 }
868
Tim Petersd1139e02002-03-28 07:32:11 +0000869 p = _PyMalloc_Malloc(total);
Tim Petersddea2082002-03-23 10:03:50 +0000870 if (p == NULL)
871 return NULL;
872
873 write4(p, nbytes);
Tim Petersd1139e02002-03-28 07:32:11 +0000874 p[4] = p[5] = p[6] = p[7] = PYMALLOC_FORBIDDENBYTE;
Tim Petersddea2082002-03-23 10:03:50 +0000875
876 if (nbytes > 0)
877 memset(p+8, PYMALLOC_CLEANBYTE, nbytes);
878
Tim Peters62c06ba2002-03-23 22:28:18 +0000879 tail = p + 8 + nbytes;
880 tail[0] = tail[1] = tail[2] = tail[3] = PYMALLOC_FORBIDDENBYTE;
881 write4(tail + 4, serialno);
Tim Petersddea2082002-03-23 10:03:50 +0000882
883 return p+8;
884}
885
Tim Peters62c06ba2002-03-23 22:28:18 +0000886/* The debug free first checks the 8 bytes on each end for sanity (in
887 particular, that the PYMALLOC_FORBIDDENBYTEs are still intact).
Tim Petersddea2082002-03-23 10:03:50 +0000888 Then fills the original bytes with PYMALLOC_DEADBYTE.
889 Then calls the underlying free.
890*/
891void
Tim Petersd1139e02002-03-28 07:32:11 +0000892_PyMalloc_DebugFree(void *p)
Tim Petersddea2082002-03-23 10:03:50 +0000893{
Tim Peters62c06ba2002-03-23 22:28:18 +0000894 uchar *q = (uchar *)p;
Tim Petersddea2082002-03-23 10:03:50 +0000895 size_t nbytes;
896
Tim Petersddea2082002-03-23 10:03:50 +0000897 if (p == NULL)
898 return;
Tim Petersddea2082002-03-23 10:03:50 +0000899 _PyMalloc_DebugCheckAddress(p);
900 nbytes = read4(q-8);
901 if (nbytes > 0)
902 memset(q, PYMALLOC_DEADBYTE, nbytes);
Tim Petersd1139e02002-03-28 07:32:11 +0000903 _PyMalloc_Free(q-8);
Tim Petersddea2082002-03-23 10:03:50 +0000904}
905
906void *
Tim Petersd1139e02002-03-28 07:32:11 +0000907_PyMalloc_DebugRealloc(void *p, size_t nbytes)
Tim Petersddea2082002-03-23 10:03:50 +0000908{
909 uchar *q = (uchar *)p;
910 size_t original_nbytes;
Tim Peterse0850172002-03-24 00:34:21 +0000911 void *fresh; /* new memory block, if needed */
Tim Petersddea2082002-03-23 10:03:50 +0000912
Tim Petersddea2082002-03-23 10:03:50 +0000913 if (p == NULL)
Tim Petersd1139e02002-03-28 07:32:11 +0000914 return _PyMalloc_DebugMalloc(nbytes);
Tim Petersddea2082002-03-23 10:03:50 +0000915
Tim Petersddea2082002-03-23 10:03:50 +0000916 _PyMalloc_DebugCheckAddress(p);
Tim Petersddea2082002-03-23 10:03:50 +0000917 original_nbytes = read4(q-8);
918 if (nbytes == original_nbytes) {
919 /* note that this case is likely to be common due to the
920 way Python appends to lists */
Tim Peterse0850172002-03-24 00:34:21 +0000921 bumpserialno();
Tim Petersddea2082002-03-23 10:03:50 +0000922 write4(q + nbytes + 4, serialno);
923 return p;
924 }
925
926 if (nbytes < original_nbytes) {
927 /* shrinking -- leave the guts alone, except to
928 fill the excess with DEADBYTE */
929 const size_t excess = original_nbytes - nbytes;
Tim Peterse0850172002-03-24 00:34:21 +0000930 bumpserialno();
Tim Petersddea2082002-03-23 10:03:50 +0000931 write4(q-8, nbytes);
932 /* kill the excess bytes plus the trailing 8 pad bytes */
Tim Petersddea2082002-03-23 10:03:50 +0000933 q += nbytes;
934 q[0] = q[1] = q[2] = q[3] = PYMALLOC_FORBIDDENBYTE;
935 write4(q+4, serialno);
Tim Petersd1139e02002-03-28 07:32:11 +0000936 memset(q+8, PYMALLOC_DEADBYTE, excess);
Tim Petersddea2082002-03-23 10:03:50 +0000937 return p;
938 }
939
940 /* More memory is needed: get it, copy over the first original_nbytes
941 of the original data, and free the original memory. */
Tim Petersd1139e02002-03-28 07:32:11 +0000942 fresh = _PyMalloc_DebugMalloc(nbytes);
Tim Petersddea2082002-03-23 10:03:50 +0000943 if (fresh != NULL && original_nbytes > 0)
944 memcpy(fresh, p, original_nbytes);
Tim Petersd1139e02002-03-28 07:32:11 +0000945 _PyMalloc_DebugFree(p);
Tim Petersddea2082002-03-23 10:03:50 +0000946 return fresh;
947}
948
949void
950_PyMalloc_DebugCheckAddress(const void *p)
951{
952 const uchar *q = (const uchar *)p;
Tim Petersd1139e02002-03-28 07:32:11 +0000953 char *msg;
954 int i;
Tim Petersddea2082002-03-23 10:03:50 +0000955
Tim Petersd1139e02002-03-28 07:32:11 +0000956 if (p == NULL) {
Tim Petersddea2082002-03-23 10:03:50 +0000957 msg = "didn't expect a NULL pointer";
Tim Petersd1139e02002-03-28 07:32:11 +0000958 goto error;
959 }
Tim Petersddea2082002-03-23 10:03:50 +0000960
Tim Petersd1139e02002-03-28 07:32:11 +0000961 for (i = 4; i >= 1; --i) {
962 if (*(q-i) != PYMALLOC_FORBIDDENBYTE) {
963 msg = "bad leading pad byte";
964 goto error;
965 }
966 }
Tim Petersddea2082002-03-23 10:03:50 +0000967
Tim Petersd1139e02002-03-28 07:32:11 +0000968 {
Tim Petersddea2082002-03-23 10:03:50 +0000969 const ulong nbytes = read4(q-8);
970 const uchar *tail = q + nbytes;
Tim Petersddea2082002-03-23 10:03:50 +0000971 for (i = 0; i < 4; ++i) {
972 if (tail[i] != PYMALLOC_FORBIDDENBYTE) {
973 msg = "bad trailing pad byte";
Tim Petersd1139e02002-03-28 07:32:11 +0000974 goto error;
Tim Petersddea2082002-03-23 10:03:50 +0000975 }
976 }
977 }
978
Tim Petersd1139e02002-03-28 07:32:11 +0000979 return;
980
981error:
982 _PyMalloc_DebugDumpAddress(p);
983 Py_FatalError(msg);
Tim Petersddea2082002-03-23 10:03:50 +0000984}
985
986void
987_PyMalloc_DebugDumpAddress(const void *p)
988{
989 const uchar *q = (const uchar *)p;
990 const uchar *tail;
991 ulong nbytes, serial;
Tim Petersd1139e02002-03-28 07:32:11 +0000992 int i;
Tim Petersddea2082002-03-23 10:03:50 +0000993
994 fprintf(stderr, "Debug memory block at address p=%p:\n", p);
995 if (p == NULL)
996 return;
997
998 nbytes = read4(q-8);
999 fprintf(stderr, " %lu bytes originally allocated\n", nbytes);
Tim Petersddea2082002-03-23 10:03:50 +00001000
1001 /* In case this is nuts, check the pad bytes before trying to read up
1002 the serial number (the address deref could blow up). */
1003
Tim Petersd1139e02002-03-28 07:32:11 +00001004 fputs(" the 4 pad bytes at p-4 are ", stderr);
1005 if (*(q-4) == PYMALLOC_FORBIDDENBYTE &&
1006 *(q-3) == PYMALLOC_FORBIDDENBYTE &&
Tim Petersddea2082002-03-23 10:03:50 +00001007 *(q-2) == PYMALLOC_FORBIDDENBYTE &&
1008 *(q-1) == PYMALLOC_FORBIDDENBYTE) {
Tim Peters62c06ba2002-03-23 22:28:18 +00001009 fputs("PYMALLOC_FORBIDDENBYTE, as expected\n", stderr);
Tim Petersddea2082002-03-23 10:03:50 +00001010 }
1011 else {
Tim Petersddea2082002-03-23 10:03:50 +00001012 fprintf(stderr, "not all PYMALLOC_FORBIDDENBYTE (0x%02x):\n",
1013 PYMALLOC_FORBIDDENBYTE);
Tim Petersd1139e02002-03-28 07:32:11 +00001014 for (i = 4; i >= 1; --i) {
Tim Petersddea2082002-03-23 10:03:50 +00001015 const uchar byte = *(q-i);
1016 fprintf(stderr, " at p-%d: 0x%02x", i, byte);
1017 if (byte != PYMALLOC_FORBIDDENBYTE)
1018 fputs(" *** OUCH", stderr);
1019 fputc('\n', stderr);
1020 }
1021 }
1022
1023 tail = q + nbytes;
1024 fprintf(stderr, " the 4 pad bytes at tail=%p are ", tail);
1025 if (tail[0] == PYMALLOC_FORBIDDENBYTE &&
1026 tail[1] == PYMALLOC_FORBIDDENBYTE &&
1027 tail[2] == PYMALLOC_FORBIDDENBYTE &&
1028 tail[3] == PYMALLOC_FORBIDDENBYTE) {
Tim Peters62c06ba2002-03-23 22:28:18 +00001029 fputs("PYMALLOC_FORBIDDENBYTE, as expected\n", stderr);
Tim Petersddea2082002-03-23 10:03:50 +00001030 }
1031 else {
Tim Petersddea2082002-03-23 10:03:50 +00001032 fprintf(stderr, "not all PYMALLOC_FORBIDDENBYTE (0x%02x):\n",
1033 PYMALLOC_FORBIDDENBYTE);
1034 for (i = 0; i < 4; ++i) {
1035 const uchar byte = tail[i];
1036 fprintf(stderr, " at tail+%d: 0x%02x",
1037 i, byte);
1038 if (byte != PYMALLOC_FORBIDDENBYTE)
1039 fputs(" *** OUCH", stderr);
1040 fputc('\n', stderr);
1041 }
1042 }
1043
1044 serial = read4(tail+4);
1045 fprintf(stderr, " the block was made by call #%lu to "
1046 "debug malloc/realloc\n", serial);
1047
1048 if (nbytes > 0) {
1049 int i = 0;
Tim Peters62c06ba2002-03-23 22:28:18 +00001050 fputs(" data at p:", stderr);
Tim Petersddea2082002-03-23 10:03:50 +00001051 /* print up to 8 bytes at the start */
1052 while (q < tail && i < 8) {
1053 fprintf(stderr, " %02x", *q);
1054 ++i;
1055 ++q;
1056 }
1057 /* and up to 8 at the end */
1058 if (q < tail) {
1059 if (tail - q > 8) {
Tim Peters62c06ba2002-03-23 22:28:18 +00001060 fputs(" ...", stderr);
Tim Petersddea2082002-03-23 10:03:50 +00001061 q = tail - 8;
1062 }
1063 while (q < tail) {
1064 fprintf(stderr, " %02x", *q);
1065 ++q;
1066 }
1067 }
Tim Peters62c06ba2002-03-23 22:28:18 +00001068 fputc('\n', stderr);
Tim Petersddea2082002-03-23 10:03:50 +00001069 }
1070}
1071
1072#endif /* PYMALLOC_DEBUG */