6 Here are some notes on configuring Perl's malloc.
8 There are two macros which serve as bulk disablers of advanced
9 features of this malloc: NO_FANCY_MALLOC, PLAIN_MALLOC (undef by
10 default). Look in the list of default values below to understand
11 their exact effect. Defining NO_FANCY_MALLOC returns malloc.c to the
12 state of the malloc in Perl 5.004. Additionally defining PLAIN_MALLOC
13 returns it to the state as of Perl 5.000.
15 Note that some of the settings below may be ignored in the code based
16 on values of other macros. The PERL_CORE symbol is only defined when
17 perl itself is being compiled (so malloc can make some assumptions
18 about perl's facilities being available to it).
20 Each config option has a short description, followed by its name,
21 default value, and a comment about the default (if applicable). Some
22 options take a precise value, while the others are just boolean.
23 The boolean ones are listed first.
25 # Enable code for an emergency memory pool in $^M. See perlvar.pod
26 # for a description of $^M.
27 PERL_EMERGENCY_SBRK (!PLAIN_MALLOC && PERL_CORE)
29 # Enable code for printing memory statistics.
30 DEBUGGING_MSTATS (!PLAIN_MALLOC && PERL_CORE)
32 # Move allocation info for small buckets into separate areas.
33 # Memory optimization (especially for small allocations, of the
34 # less than 64 bytes). Since perl usually makes a large number
35 # of small allocations, this is usually a win.
36 PACK_MALLOC (!PLAIN_MALLOC && !RCHECK)
38 # Add one page to big powers of two when calculating bucket size.
39 # This is targeted at big allocations, as are common in image
41 TWO_POT_OPTIMIZE !PLAIN_MALLOC
43 # Use intermediate bucket sizes between powers-of-two. This is
44 # generally a memory optimization, and a (small) speed pessimization.
45 BUCKETS_ROOT2 !NO_FANCY_MALLOC
47 # Do not check small deallocations for bad free(). Memory
48 # and speed optimization, error reporting pessimization.
49 IGNORE_SMALL_BAD_FREE (!NO_FANCY_MALLOC && !RCHECK)
51 # Use table lookup to decide in which bucket a given allocation will go.
52 SMALL_BUCKET_VIA_TABLE !NO_FANCY_MALLOC
54 # Use system-malloc() to emulate sbrk(). Normally only used with broken
56 PERL_SBRK_VIA_MALLOC undef
58 # Disable memory overwrite checking with DEBUGGING. Memory and speed
59 # optimization, error reporting pessimization.
62 # Enable memory overwrite checking with DEBUGGING. Memory and speed
63 # pessimization, error reporting optimization
64 RCHECK (DEBUGGING && !NO_RCHECK)
66 # Failed allocations bigger than this size croak (if
67 # PERL_EMERGENCY_SBRK is enabled) without touching $^M. See
68 # perlvar.pod for a description of $^M.
69 BIG_SIZE (1<<16) # 64K
71 # Starting from this power of two, add an extra page to the
72 # size of the bucket. This enables optimized allocations of sizes
73 # close to powers of 2. Note that the value is indexed at 0.
74 FIRST_BIG_POW2 15 # 32K, 16K is used too often
76 # Estimate of minimal memory footprint. malloc uses this value to
77 # request the most reasonable largest blocks of memory from the system.
80 # Round up sbrk()s to multiples of this.
83 # Round up sbrk()s to multiples of this percent of footprint.
86 # Add this much memory to big powers of two to get the bucket size.
89 # This many sbrk() discontinuities should be tolerated even
90 # from the start without deciding that sbrk() is usually
94 # This many continuous sbrk()s compensate for one discontinuous one.
97 # Which allocator to use if PERL_SBRK_VIA_MALLOC
98 SYSTEM_ALLOC(a) malloc(a)
100 This implementation assumes that calling PerlIO_printf() does not
101 result in any memory allocation calls (used during a panic).
105 #ifndef NO_FANCY_MALLOC
106 # ifndef SMALL_BUCKET_VIA_TABLE
107 # define SMALL_BUCKET_VIA_TABLE
109 # ifndef BUCKETS_ROOT2
110 # define BUCKETS_ROOT2
112 # ifndef IGNORE_SMALL_BAD_FREE
113 # define IGNORE_SMALL_BAD_FREE
117 #ifndef PLAIN_MALLOC /* Bulk enable features */
121 # ifndef TWO_POT_OPTIMIZE
122 # define TWO_POT_OPTIMIZE
124 # if defined(PERL_CORE) && !defined(PERL_EMERGENCY_SBRK)
125 # define PERL_EMERGENCY_SBRK
127 # if defined(PERL_CORE) && !defined(DEBUGGING_MSTATS)
128 # define DEBUGGING_MSTATS
132 #define MIN_BUC_POW2 (sizeof(void*) > 4 ? 3 : 2) /* Allow for 4-byte arena. */
133 #define MIN_BUCKET (MIN_BUC_POW2 * BUCKETS_PER_POW2)
135 #if !(defined(I286) || defined(atarist))
136 /* take 2k unless the block is bigger than that */
137 # define LOG_OF_MIN_ARENA 11
139 /* take 16k unless the block is bigger than that
140 (80286s like large segments!), probably good on the atari too */
141 # define LOG_OF_MIN_ARENA 14
145 # if defined(DEBUGGING) && !defined(NO_RCHECK)
148 # if defined(RCHECK) && defined(IGNORE_SMALL_BAD_FREE)
149 # undef IGNORE_SMALL_BAD_FREE
152 * malloc.c (Caltech) 2/21/82
153 * Chris Kingsley, kingsley@cit-20.
155 * This is a very fast storage allocator. It allocates blocks of a small
156 * number of different sizes, and keeps free lists of each size. Blocks that
157 * don't exactly fit are passed up to the next larger size. In this
158 * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
159 * If PACK_MALLOC is defined, small blocks are 2^n bytes long.
160 * This is designed for use in a program that uses vast quantities of memory,
161 * but bombs when it runs out.
169 # include "../EXTERN.h"
170 # include "../perl.h"
177 # define Malloc_t void *
180 # define MEM_SIZE unsigned long
183 # define LONG_MAX 0x7FFFFFFF
186 # define UV unsigned long
189 # define caddr_t char *
194 # define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
195 # define PerlEnv_getenv getenv
196 # define PerlIO_printf fprintf
197 # define PerlIO_stderr() stderr
199 # ifndef croak /* make depend */
200 # define croak(mess, arg) warn((mess), (arg)); exit(1);
203 # define warn(mess, arg) fprintf(stderr, (mess), (arg));
215 # define MUTEX_LOCK(l)
219 # define MUTEX_UNLOCK(l)
224 # define DEBUG_m(a) if (debug & 128) a
227 /* I don't much care whether these are defined in sys/types.h--LAW */
229 #define u_char unsigned char
230 #define u_int unsigned int
233 # define u_bigint UV /* Needs to eat *void. */
235 # define u_bigint unsigned long /* Needs to eat *void. */
238 #define u_short unsigned short
240 /* 286 and atarist like big chunks, which gives too much overhead. */
241 #if (defined(RCHECK) || defined(I286) || defined(atarist)) && defined(PACK_MALLOC)
246 * The description below is applicable if PACK_MALLOC is not defined.
248 * The overhead on a block is at least 4 bytes. When free, this space
249 * contains a pointer to the next free block, and the bottom two bits must
250 * be zero. When in use, the first byte is set to MAGIC, and the second
251 * byte is the size index. The remaining bytes are for alignment.
252 * If range checking is enabled and the size of the block fits
253 * in two bytes, then the top two bytes hold the size of the requested block
254 * plus the range checking words, and the header word MINUS ONE.
257 union overhead *ov_next; /* when free */
258 #if MEM_ALIGNBYTES > 4
259 double strut; /* alignment problems */
262 u_char ovu_magic; /* magic number */
263 u_char ovu_index; /* bucket # */
265 u_short ovu_size; /* actual block size */
266 u_int ovu_rmagic; /* range magic number */
269 #define ov_magic ovu.ovu_magic
270 #define ov_index ovu.ovu_index
271 #define ov_size ovu.ovu_size
272 #define ov_rmagic ovu.ovu_rmagic
276 static void botch _((char *diag, char *s));
278 static void morecore _((int bucket));
279 static int findbucket _((union overhead *freep, int srchlen));
281 #define MAGIC 0xff /* magic # on accounting info */
282 #define RMAGIC 0x55555555 /* magic # on range info */
283 #define RMAGIC_C 0x55 /* magic # on range info */
286 # define RSLOP sizeof (u_int)
287 # ifdef TWO_POT_OPTIMIZE
288 # define MAX_SHORT_BUCKET (12 * BUCKETS_PER_POW2)
290 # define MAX_SHORT_BUCKET (13 * BUCKETS_PER_POW2)
296 #if !defined(PACK_MALLOC) && defined(BUCKETS_ROOT2)
297 # undef BUCKETS_ROOT2
301 # define BUCKET_TABLE_SHIFT 2
302 # define BUCKET_POW2_SHIFT 1
303 # define BUCKETS_PER_POW2 2
305 # define BUCKET_TABLE_SHIFT MIN_BUC_POW2
306 # define BUCKET_POW2_SHIFT 0
307 # define BUCKETS_PER_POW2 1
311 # define MAX_BUCKET_BY_TABLE 13
312 static u_short buck_size[MAX_BUCKET_BY_TABLE + 1] =
314 0, 0, 0, 0, 4, 4, 8, 12, 16, 24, 32, 48, 64, 80,
316 # define BUCKET_SIZE(i) ((i) % 2 ? buck_size[i] : (1 << ((i) >> BUCKET_POW2_SHIFT)))
317 # define BUCKET_SIZE_REAL(i) ((i) <= MAX_BUCKET_BY_TABLE \
319 : ((1 << ((i) >> BUCKET_POW2_SHIFT)) \
321 + POW2_OPTIMIZE_SURPLUS(i)))
323 # define BUCKET_SIZE(i) (1 << ((i) >> BUCKET_POW2_SHIFT))
324 # define BUCKET_SIZE_REAL(i) (BUCKET_SIZE(i) - MEM_OVERHEAD(i) + POW2_OPTIMIZE_SURPLUS(i))
329 /* In this case it is assumed that if we do sbrk() in 2K units, we
330 * will get 2K aligned arenas (at least after some initial
331 * alignment). The bucket number of the given subblock is on the start
332 * of 2K arena which contains the subblock. Several following bytes
333 * contain the magic numbers for the subblocks in the block.
335 * Sizes of chunks are powers of 2 for chunks in buckets <=
336 * MAX_PACKED, after this they are (2^n - sizeof(union overhead)) (to
337 * get alignment right).
339 * Consider an arena for 2^n with n>MAX_PACKED. We suppose that
340 * starts of all the chunks in a 2K arena are in different
341 * 2^n-byte-long chunks. If the top of the last chunk is aligned on a
342 * boundary of 2K block, this means that sizeof(union
343 * overhead)*"number of chunks" < 2^n, or sizeof(union overhead)*2K <
344 * 4^n, or n > 6 + log2(sizeof()/2)/2, since a chunk of size 2^n -
345 * overhead is used. Since this rules out n = 7 for 8 byte alignment,
346 * we specialcase allocation of the first of 16 128-byte-long chunks.
348 * Note that with the above assumption we automatically have enough
349 * place for MAGIC at the start of 2K block. Note also that we
350 * overlay union overhead over the chunk, thus the start of small chunks
351 * is immediately overwritten after freeing. */
352 # define MAX_PACKED_POW2 6
353 # define MAX_PACKED (MAX_PACKED_POW2 * BUCKETS_PER_POW2 + BUCKET_POW2_SHIFT)
354 # define MAX_POW2_ALGO ((1<<(MAX_PACKED_POW2 + 1)) - M_OVERHEAD)
355 # define TWOK_MASK ((1<<LOG_OF_MIN_ARENA) - 1)
356 # define TWOK_MASKED(x) ((u_bigint)(x) & ~TWOK_MASK)
357 # define TWOK_SHIFT(x) ((u_bigint)(x) & TWOK_MASK)
358 # define OV_INDEXp(block) ((u_char*)(TWOK_MASKED(block)))
359 # define OV_INDEX(block) (*OV_INDEXp(block))
360 # define OV_MAGIC(block,bucket) (*(OV_INDEXp(block) + \
361 (TWOK_SHIFT(block)>> \
362 (bucket>>BUCKET_POW2_SHIFT)) + \
363 (bucket >= MIN_NEEDS_SHIFT ? 1 : 0)))
364 /* A bucket can have a shift smaller than it size, we need to
365 shift its magic number so it will not overwrite index: */
366 # ifdef BUCKETS_ROOT2
367 # define MIN_NEEDS_SHIFT (7*BUCKETS_PER_POW2 - 1) /* Shift 80 greater than chunk 64. */
369 # define MIN_NEEDS_SHIFT (7*BUCKETS_PER_POW2) /* Shift 128 greater than chunk 32. */
371 # define CHUNK_SHIFT 0
373 /* Number of active buckets of given ordinal. */
374 #ifdef IGNORE_SMALL_BAD_FREE
375 #define FIRST_BUCKET_WITH_CHECK (6 * BUCKETS_PER_POW2) /* 64 */
376 # define N_BLKS(bucket) ( (bucket) < FIRST_BUCKET_WITH_CHECK \
377 ? ((1<<LOG_OF_MIN_ARENA) - 1)/BUCKET_SIZE(bucket) \
380 # define N_BLKS(bucket) n_blks[bucket]
383 static u_short n_blks[LOG_OF_MIN_ARENA * BUCKETS_PER_POW2] =
385 # if BUCKETS_PER_POW2==1
387 (MIN_BUC_POW2==2 ? 384 : 0),
388 224, 120, 62, 31, 16, 8, 4, 2
391 (MIN_BUC_POW2==2 ? 384 : 0), (MIN_BUC_POW2==2 ? 384 : 0), /* 4, 4 */
392 224, 149, 120, 80, 62, 41, 31, 25, 16, 16, 8, 8, 4, 4, 2, 2
396 /* Shift of the first bucket with the given ordinal inside 2K chunk. */
397 #ifdef IGNORE_SMALL_BAD_FREE
398 # define BLK_SHIFT(bucket) ( (bucket) < FIRST_BUCKET_WITH_CHECK \
399 ? ((1<<LOG_OF_MIN_ARENA) \
400 - BUCKET_SIZE(bucket) * N_BLKS(bucket)) \
403 # define BLK_SHIFT(bucket) blk_shift[bucket]
406 static u_short blk_shift[LOG_OF_MIN_ARENA * BUCKETS_PER_POW2] =
408 # if BUCKETS_PER_POW2==1
410 (MIN_BUC_POW2==2 ? 512 : 0),
411 256, 128, 64, 64, /* 8 to 64 */
412 16*sizeof(union overhead),
413 8*sizeof(union overhead),
414 4*sizeof(union overhead),
415 2*sizeof(union overhead),
418 (MIN_BUC_POW2==2 ? 512 : 0), (MIN_BUC_POW2==2 ? 512 : 0),
419 256, 260, 128, 128, 64, 80, 64, 48, /* 8 to 96 */
420 16*sizeof(union overhead), 16*sizeof(union overhead),
421 8*sizeof(union overhead), 8*sizeof(union overhead),
422 4*sizeof(union overhead), 4*sizeof(union overhead),
423 2*sizeof(union overhead), 2*sizeof(union overhead),
427 #else /* !PACK_MALLOC */
429 # define OV_MAGIC(block,bucket) (block)->ov_magic
430 # define OV_INDEX(block) (block)->ov_index
431 # define CHUNK_SHIFT 1
432 # define MAX_PACKED -1
433 #endif /* !PACK_MALLOC */
435 #define M_OVERHEAD (sizeof(union overhead) + RSLOP)
438 # define MEM_OVERHEAD(bucket) \
439 (bucket <= MAX_PACKED ? 0 : M_OVERHEAD)
440 # ifdef SMALL_BUCKET_VIA_TABLE
441 # define START_SHIFTS_BUCKET ((MAX_PACKED_POW2 + 1) * BUCKETS_PER_POW2)
442 # define START_SHIFT MAX_PACKED_POW2
443 # ifdef BUCKETS_ROOT2 /* Chunks of size 3*2^n. */
444 # define SIZE_TABLE_MAX 80
446 # define SIZE_TABLE_MAX 64
448 static char bucket_of[] =
450 # ifdef BUCKETS_ROOT2 /* Chunks of size 3*2^n. */
451 /* 0 to 15 in 4-byte increments. */
452 (sizeof(void*) > 4 ? 6 : 5), /* 4/8, 5-th bucket for better reports */
455 9, 9, 10, 10, /* 24, 32 */
456 11, 11, 11, 11, /* 48 */
457 12, 12, 12, 12, /* 64 */
458 13, 13, 13, 13, /* 80 */
459 13, 13, 13, 13 /* 80 */
460 # else /* !BUCKETS_ROOT2 */
461 /* 0 to 15 in 4-byte increments. */
462 (sizeof(void*) > 4 ? 3 : 2),
468 # endif /* !BUCKETS_ROOT2 */
470 # else /* !SMALL_BUCKET_VIA_TABLE */
471 # define START_SHIFTS_BUCKET MIN_BUCKET
472 # define START_SHIFT (MIN_BUC_POW2 - 1)
473 # endif /* !SMALL_BUCKET_VIA_TABLE */
474 #else /* !PACK_MALLOC */
475 # define MEM_OVERHEAD(bucket) M_OVERHEAD
476 # ifdef SMALL_BUCKET_VIA_TABLE
477 # undef SMALL_BUCKET_VIA_TABLE
479 # define START_SHIFTS_BUCKET MIN_BUCKET
480 # define START_SHIFT (MIN_BUC_POW2 - 1)
481 #endif /* !PACK_MALLOC */
484 * Big allocations are often of the size 2^n bytes. To make them a
485 * little bit better, make blocks of size 2^n+pagesize for big n.
488 #ifdef TWO_POT_OPTIMIZE
490 # ifndef PERL_PAGESIZE
491 # define PERL_PAGESIZE 4096
493 # ifndef FIRST_BIG_POW2
494 # define FIRST_BIG_POW2 15 /* 32K, 16K is used too often. */
496 # define FIRST_BIG_BLOCK (1<<FIRST_BIG_POW2)
497 /* If this value or more, check against bigger blocks. */
498 # define FIRST_BIG_BOUND (FIRST_BIG_BLOCK - M_OVERHEAD)
499 /* If less than this value, goes into 2^n-overhead-block. */
500 # define LAST_SMALL_BOUND ((FIRST_BIG_BLOCK>>1) - M_OVERHEAD)
502 # define POW2_OPTIMIZE_ADJUST(nbytes) \
503 ((nbytes >= FIRST_BIG_BOUND) ? nbytes -= PERL_PAGESIZE : 0)
504 # define POW2_OPTIMIZE_SURPLUS(bucket) \
505 ((bucket >= FIRST_BIG_POW2 * BUCKETS_PER_POW2) ? PERL_PAGESIZE : 0)
507 #else /* !TWO_POT_OPTIMIZE */
508 # define POW2_OPTIMIZE_ADJUST(nbytes)
509 # define POW2_OPTIMIZE_SURPLUS(bucket) 0
510 #endif /* !TWO_POT_OPTIMIZE */
512 #if defined(HAS_64K_LIMIT) && defined(PERL_CORE)
513 # define BARK_64K_LIMIT(what,nbytes,size) \
514 if (nbytes > 0xffff) { \
515 PerlIO_printf(PerlIO_stderr(), \
516 "%s too large: %lx\n", what, size); \
519 #else /* !HAS_64K_LIMIT || !PERL_CORE */
520 # define BARK_64K_LIMIT(what,nbytes,size)
521 #endif /* !HAS_64K_LIMIT || !PERL_CORE */
524 # define MIN_SBRK 2048
528 # define FIRST_SBRK (48*1024)
531 /* Minimal sbrk in percents of what is already alloced. */
532 #ifndef MIN_SBRK_FRAC
533 # define MIN_SBRK_FRAC 3
536 #ifndef SBRK_ALLOW_FAILURES
537 # define SBRK_ALLOW_FAILURES 3
540 #ifndef SBRK_FAILURE_PRICE
541 # define SBRK_FAILURE_PRICE 50
544 #if defined(PERL_EMERGENCY_SBRK) && defined(PERL_CORE)
547 # define BIG_SIZE (1<<16) /* 64K */
550 static char *emergency_buffer;
551 static MEM_SIZE emergency_buffer_size;
557 if (size >= BIG_SIZE) {
558 /* Give the possibility to recover: */
559 MUTEX_UNLOCK(&malloc_mutex);
560 croak("Out of memory during \"large\" request for %i bytes", size);
563 if (!emergency_buffer) {
565 /* First offense, give a possibility to recover by dieing. */
566 /* No malloc involved here: */
567 GV **gvp = (GV**)hv_fetch(defstash, "^M", 2, 0);
571 if (!gvp) gvp = (GV**)hv_fetch(defstash, "\015", 1, 0);
572 if (!gvp || !(sv = GvSV(*gvp)) || !SvPOK(sv)
573 || (SvLEN(sv) < (1<<LOG_OF_MIN_ARENA) - M_OVERHEAD))
574 return (char *)-1; /* Now die die die... */
576 /* Got it, now detach SvPV: */
578 /* Check alignment: */
579 if (((u_bigint)(pv - M_OVERHEAD)) & ((1<<LOG_OF_MIN_ARENA) - 1)) {
580 PerlIO_puts(PerlIO_stderr(),"Bad alignment of $^M!\n");
581 return (char *)-1; /* die die die */
584 emergency_buffer = pv - M_OVERHEAD;
585 emergency_buffer_size = SvLEN(sv) + M_OVERHEAD;
588 MUTEX_UNLOCK(&malloc_mutex);
589 croak("Out of memory during request for %i bytes", size);
591 else if (emergency_buffer_size >= size) {
592 emergency_buffer_size -= size;
593 return emergency_buffer + emergency_buffer_size;
596 return (char *)-1; /* poor guy... */
599 #else /* !(defined(PERL_EMERGENCY_SBRK) && defined(PERL_CORE)) */
600 # define emergency_sbrk(size) -1
601 #endif /* !(defined(PERL_EMERGENCY_SBRK) && defined(PERL_CORE)) */
604 * nextf[i] is the pointer to the next free block of size 2^i. The
605 * smallest allocatable block is 8 bytes. The overhead information
606 * precedes the data area returned to the user.
608 #define NBUCKETS (32*BUCKETS_PER_POW2 + 1)
609 static union overhead *nextf[NBUCKETS];
612 #define sbrk(a) Perl_sbrk(a)
613 Malloc_t Perl_sbrk _((int size));
615 #ifdef DONT_DECLARE_STD
620 extern Malloc_t sbrk(int);
624 #ifdef DEBUGGING_MSTATS
626 * nmalloc[i] is the difference between the number of mallocs and frees
627 * for a given block size.
629 static u_int nmalloc[NBUCKETS];
630 static u_int sbrk_slack;
631 static u_int start_slack;
634 static u_int goodsbrk;
637 #define ASSERT(p,diag) if (!(p)) botch(diag,STRINGIFY(p)); else
639 botch(char *diag, char *s)
641 PerlIO_printf(PerlIO_stderr(), "assertion botched (%s?): %s\n", diag, s);
645 #define ASSERT(p, diag)
649 malloc(register size_t nbytes)
651 register union overhead *p;
653 register MEM_SIZE shiftr;
655 #if defined(DEBUGGING) || defined(RCHECK)
656 MEM_SIZE size = nbytes;
659 BARK_64K_LIMIT("Allocation",nbytes,nbytes);
661 if ((long)nbytes < 0)
662 croak("%s", "panic: malloc");
665 MUTEX_LOCK(&malloc_mutex);
667 * Convert amount of memory requested into
668 * closest block size stored in hash buckets
669 * which satisfies request. Account for
670 * space used per block for accounting.
673 # ifdef SMALL_BUCKET_VIA_TABLE
676 else if (nbytes <= SIZE_TABLE_MAX) {
677 bucket = bucket_of[(nbytes - 1) >> BUCKET_TABLE_SHIFT];
682 if (nbytes <= MAX_POW2_ALGO) goto do_shifts;
687 POW2_OPTIMIZE_ADJUST(nbytes);
688 nbytes += M_OVERHEAD;
689 nbytes = (nbytes + 3) &~ 3;
691 shiftr = (nbytes - 1) >> START_SHIFT;
692 bucket = START_SHIFTS_BUCKET;
693 /* apart from this loop, this is O(1) */
695 bucket += BUCKETS_PER_POW2;
698 * If nothing in hash bucket right now,
699 * request more memory from the system.
701 if (nextf[bucket] == NULL)
703 if ((p = nextf[bucket]) == NULL) {
704 MUTEX_UNLOCK(&malloc_mutex);
707 PerlIO_puts(PerlIO_stderr(),"Out of memory!\n");
715 DEBUG_m(PerlIO_printf(Perl_debug_log,
716 "0x%lx: (%05lu) malloc %ld bytes\n",
717 (unsigned long)(p+1), (unsigned long)(an++),
720 /* remove from linked list */
722 if (*((int*)p) & (sizeof(union overhead) - 1))
723 PerlIO_printf(PerlIO_stderr(), "Corrupt malloc ptr 0x%lx at 0x%lx\n",
724 (unsigned long)*((int*)p),(unsigned long)p);
726 nextf[bucket] = p->ov_next;
727 #ifdef IGNORE_SMALL_BAD_FREE
728 if (bucket >= FIRST_BUCKET_WITH_CHECK)
730 OV_MAGIC(p, bucket) = MAGIC;
732 OV_INDEX(p) = bucket;
736 * Record allocated size of block and
737 * bound space with magic numbers.
739 p->ov_rmagic = RMAGIC;
740 if (bucket <= MAX_SHORT_BUCKET) {
743 nbytes = size + M_OVERHEAD;
744 p->ov_size = nbytes - 1;
745 if ((i = nbytes & 3)) {
748 *((char *)((caddr_t)p + nbytes - RSLOP + i)) = RMAGIC_C;
750 nbytes = (nbytes + 3) &~ 3;
751 *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC;
754 MUTEX_UNLOCK(&malloc_mutex);
755 return ((Malloc_t)(p + CHUNK_SHIFT));
758 static char *last_sbrk_top;
759 static char *last_op; /* This arena can be easily extended. */
760 static int sbrked_remains;
761 static int sbrk_good = SBRK_ALLOW_FAILURES * SBRK_FAILURE_PRICE;
763 #ifdef DEBUGGING_MSTATS
767 struct chunk_chain_s {
768 struct chunk_chain_s *next;
771 static struct chunk_chain_s *chunk_chain;
773 static char max_bucket;
775 /* Cutoff a piece of one of the chunks in the chain. Prefer smaller chunk. */
777 get_from_chain(MEM_SIZE size)
779 struct chunk_chain_s *elt = chunk_chain, **oldp = &chunk_chain;
780 struct chunk_chain_s **oldgoodp = NULL;
781 long min_remain = LONG_MAX;
784 if (elt->size >= size) {
785 long remains = elt->size - size;
786 if (remains >= 0 && remains < min_remain) {
788 min_remain = remains;
794 oldp = &( elt->next );
797 if (!oldgoodp) return NULL;
799 void *ret = *oldgoodp;
800 struct chunk_chain_s *next = (*oldgoodp)->next;
802 *oldgoodp = (struct chunk_chain_s *)((char*)ret + size);
803 (*oldgoodp)->size = min_remain;
804 (*oldgoodp)->next = next;
807 void *ret = *oldgoodp;
808 *oldgoodp = (*oldgoodp)->next;
815 add_to_chain(void *p, MEM_SIZE size, MEM_SIZE chip)
817 struct chunk_chain_s *next = chunk_chain;
821 chunk_chain = (struct chunk_chain_s *)cp;
822 chunk_chain->size = size - chip;
823 chunk_chain->next = next;
828 get_from_bigger_buckets(int bucket, MEM_SIZE size)
831 static int bucketprice[NBUCKETS];
832 while (bucket <= max_bucket) {
833 /* We postpone stealing from bigger buckets until we want it
835 if (nextf[bucket] && bucketprice[bucket]++ >= price) {
837 void *ret = (void*)(nextf[bucket] - 1 + CHUNK_SHIFT);
838 bucketprice[bucket] = 0;
839 if (((char*)nextf[bucket]) - M_OVERHEAD == last_op) {
840 last_op = NULL; /* Disable optimization */
842 nextf[bucket] = nextf[bucket]->ov_next;
843 #ifdef DEBUGGING_MSTATS
845 start_slack -= M_OVERHEAD;
847 add_to_chain(ret, (BUCKET_SIZE(bucket) +
848 POW2_OPTIMIZE_SURPLUS(bucket)),
857 static union overhead *
858 getpages(int needed, int *nblksp, int bucket)
860 /* Need to do (possibly expensive) system call. Try to
861 optimize it for rare calling. */
862 MEM_SIZE require = needed - sbrked_remains;
868 if (!last_sbrk_top && require < FIRST_SBRK)
869 require = FIRST_SBRK;
870 else if (require < MIN_SBRK) require = MIN_SBRK;
872 if (require < goodsbrk * MIN_SBRK_FRAC / 100)
873 require = goodsbrk * MIN_SBRK_FRAC / 100;
874 require = ((require - 1 + MIN_SBRK) / MIN_SBRK) * MIN_SBRK;
881 DEBUG_m(PerlIO_printf(Perl_debug_log,
882 "sbrk(%ld) for %ld-byte-long arena\n",
883 (long)require, (long) needed));
884 cp = (char *)sbrk(require);
885 #ifdef DEBUGGING_MSTATS
888 if (cp == last_sbrk_top) {
889 /* Common case, anything is fine. */
891 ovp = (union overhead *) (cp - sbrked_remains);
892 sbrked_remains = require - (needed - sbrked_remains);
893 } else if (cp == (char *)-1) { /* no more room! */
894 ovp = (union overhead *)emergency_sbrk(needed);
895 if (ovp == (union overhead *)-1)
898 } else { /* Non-continuous or first sbrk(). */
899 long add = sbrked_remains;
902 if (sbrked_remains) { /* Put rest into chain, we
903 cannot use it right now. */
904 add_to_chain((void*)(last_sbrk_top - sbrked_remains),
908 /* Second, check alignment. */
911 #ifndef atarist /* on the atari we dont have to worry about this */
912 # ifndef I286 /* The sbrk(0) call on the I286 always returns the next segment */
914 /* CHUNK_SHIFT is 1 for PACK_MALLOC, 0 otherwise. */
915 if ((UV)cp & (0x7FF >> CHUNK_SHIFT)) { /* Not aligned. */
916 slack = (0x800 >> CHUNK_SHIFT)
917 - ((UV)cp & (0x7FF >> CHUNK_SHIFT));
924 DEBUG_m(PerlIO_printf(Perl_debug_log,
925 "sbrk(%ld) to fix non-continuous/off-page sbrk:\n\t%ld for alignement,\t%ld were assumed to come from the tail of the previous sbrk\n",
926 (long)add, (long) slack,
927 (long) sbrked_remains));
928 newcp = (char *)sbrk(add);
929 #if defined(DEBUGGING_MSTATS)
933 if (newcp != cp + require) {
934 /* Too bad: even rounding sbrk() is not continuous.*/
935 DEBUG_m(PerlIO_printf(Perl_debug_log,
936 "failed to fix bad sbrk()\n"));
939 MUTEX_UNLOCK(&malloc_mutex);
940 croak("%s", "panic: Off-page sbrk");
943 if (sbrked_remains) {
945 #if defined(DEBUGGING_MSTATS)
946 sbrk_slack += require;
949 DEBUG_m(PerlIO_printf(Perl_debug_log,
950 "straight sbrk(%ld)\n",
952 cp = (char *)sbrk(require);
953 #ifdef DEBUGGING_MSTATS
956 if (cp == (char *)-1)
959 sbrk_good = -1; /* Disable optimization!
960 Continue with not-aligned... */
963 require += sbrked_remains;
968 sbrk_good -= SBRK_FAILURE_PRICE;
971 ovp = (union overhead *) cp;
973 * Round up to minimum allocation size boundary
974 * and deduct from block count to reflect.
977 #ifndef I286 /* Again, this should always be ok on an 80286 */
979 ovp = (union overhead *)(((UV)ovp + 8) & ~7);
980 DEBUG_m(PerlIO_printf(Perl_debug_log,
981 "fixing sbrk(): %d bytes off machine alignement\n",
982 (int)((UV)ovp & 7)));
984 # if defined(DEBUGGING_MSTATS)
985 /* This is only approx. if TWO_POT_OPTIMIZE: */
986 sbrk_slack += (1 << bucket);
990 sbrked_remains = require - needed;
992 last_sbrk_top = cp + require;
993 last_op = (char*) cp;
994 #ifdef DEBUGGING_MSTATS
1001 getpages_adjacent(int require)
1003 if (require <= sbrked_remains) {
1004 sbrked_remains -= require;
1008 require -= sbrked_remains;
1009 /* We do not try to optimize sbrks here, we go for place. */
1010 cp = (char*) sbrk(require);
1011 #ifdef DEBUGGING_MSTATS
1013 goodsbrk += require;
1015 if (cp == last_sbrk_top) {
1017 last_sbrk_top = cp + require;
1019 /* Report the failure: */
1021 add_to_chain((void*)(last_sbrk_top - sbrked_remains),
1023 add_to_chain((void*)cp, require, 0);
1024 sbrk_good -= SBRK_FAILURE_PRICE;
1036 * Allocate more memory to the indicated bucket.
1039 morecore(register int bucket)
1041 register union overhead *ovp;
1042 register int rnu; /* 2^rnu bytes will be requested */
1043 int nblks; /* become nblks blocks of the desired size */
1044 register MEM_SIZE siz, needed;
1048 if (bucket == sizeof(MEM_SIZE)*8*BUCKETS_PER_POW2) {
1049 MUTEX_UNLOCK(&malloc_mutex);
1050 croak("%s", "Out of memory during ridiculously large request");
1052 if (bucket > max_bucket)
1053 max_bucket = bucket;
1055 rnu = ( (bucket <= (LOG_OF_MIN_ARENA << BUCKET_POW2_SHIFT))
1057 : (bucket >> BUCKET_POW2_SHIFT) );
1058 /* This may be overwritten later: */
1059 nblks = 1 << (rnu - (bucket >> BUCKET_POW2_SHIFT)); /* how many blocks to get */
1060 needed = ((MEM_SIZE)1 << rnu) + POW2_OPTIMIZE_SURPLUS(bucket);
1061 if (nextf[rnu << BUCKET_POW2_SHIFT]) { /* 2048b bucket. */
1062 ovp = nextf[rnu << BUCKET_POW2_SHIFT] - 1 + CHUNK_SHIFT;
1063 nextf[rnu << BUCKET_POW2_SHIFT]
1064 = nextf[rnu << BUCKET_POW2_SHIFT]->ov_next;
1065 #ifdef DEBUGGING_MSTATS
1066 nmalloc[rnu << BUCKET_POW2_SHIFT]--;
1067 start_slack -= M_OVERHEAD;
1069 DEBUG_m(PerlIO_printf(Perl_debug_log,
1070 "stealing %ld bytes from %ld arena\n",
1071 (long) needed, (long) rnu << BUCKET_POW2_SHIFT));
1072 } else if (chunk_chain
1073 && (ovp = (union overhead*) get_from_chain(needed))) {
1074 DEBUG_m(PerlIO_printf(Perl_debug_log,
1075 "stealing %ld bytes from chain\n",
1077 } else if ( (ovp = (union overhead*)
1078 get_from_bigger_buckets((rnu << BUCKET_POW2_SHIFT) + 1,
1080 DEBUG_m(PerlIO_printf(Perl_debug_log,
1081 "stealing %ld bytes from bigger buckets\n",
1083 } else if (needed <= sbrked_remains) {
1084 ovp = (union overhead *)(last_sbrk_top - sbrked_remains);
1085 sbrked_remains -= needed;
1086 last_op = (char*)ovp;
1088 ovp = getpages(needed, &nblks, bucket);
1094 * Add new memory allocated to that on
1095 * free list for this hash bucket.
1097 siz = BUCKET_SIZE(bucket);
1099 *(u_char*)ovp = bucket; /* Fill index. */
1100 if (bucket <= MAX_PACKED) {
1101 ovp = (union overhead *) ((char*)ovp + BLK_SHIFT(bucket));
1102 nblks = N_BLKS(bucket);
1103 # ifdef DEBUGGING_MSTATS
1104 start_slack += BLK_SHIFT(bucket);
1106 } else if (bucket < LOG_OF_MIN_ARENA * BUCKETS_PER_POW2) {
1107 ovp = (union overhead *) ((char*)ovp + BLK_SHIFT(bucket));
1108 siz -= sizeof(union overhead);
1109 } else ovp++; /* One chunk per block. */
1110 #endif /* PACK_MALLOC */
1111 nextf[bucket] = ovp;
1112 #ifdef DEBUGGING_MSTATS
1113 nmalloc[bucket] += nblks;
1114 if (bucket > MAX_PACKED) {
1115 start_slack += M_OVERHEAD * nblks;
1118 while (--nblks > 0) {
1119 ovp->ov_next = (union overhead *)((caddr_t)ovp + siz);
1120 ovp = (union overhead *)((caddr_t)ovp + siz);
1122 /* Not all sbrks return zeroed memory.*/
1123 ovp->ov_next = (union overhead *)NULL;
1125 if (bucket == 7*BUCKETS_PER_POW2) { /* Special case, explanation is above. */
1126 union overhead *n_op = nextf[7*BUCKETS_PER_POW2]->ov_next;
1127 nextf[7*BUCKETS_PER_POW2] =
1128 (union overhead *)((caddr_t)nextf[7*BUCKETS_PER_POW2]
1129 - sizeof(union overhead));
1130 nextf[7*BUCKETS_PER_POW2]->ov_next = n_op;
1132 #endif /* !PACK_MALLOC */
1138 register MEM_SIZE size;
1139 register union overhead *ovp;
1140 char *cp = (char*)mp;
1145 DEBUG_m(PerlIO_printf(Perl_debug_log,
1146 "0x%lx: (%05lu) free\n",
1147 (unsigned long)cp, (unsigned long)(an++)));
1151 ovp = (union overhead *)((caddr_t)cp
1152 - sizeof (union overhead) * CHUNK_SHIFT);
1154 bucket = OV_INDEX(ovp);
1156 #ifdef IGNORE_SMALL_BAD_FREE
1157 if ((bucket >= FIRST_BUCKET_WITH_CHECK)
1158 && (OV_MAGIC(ovp, bucket) != MAGIC))
1160 if (OV_MAGIC(ovp, bucket) != MAGIC)
1163 static int bad_free_warn = -1;
1164 if (bad_free_warn == -1) {
1165 char *pbf = PerlEnv_getenv("PERL_BADFREE");
1166 bad_free_warn = (pbf) ? atoi(pbf) : 1;
1171 warn("%s free() ignored",
1172 ovp->ov_rmagic == RMAGIC - 1 ? "Duplicate" : "Bad");
1174 warn("%s", "Bad free() ignored");
1176 return; /* sanity */
1178 MUTEX_LOCK(&malloc_mutex);
1180 ASSERT(ovp->ov_rmagic == RMAGIC, "chunk's head overwrite");
1181 if (OV_INDEX(ovp) <= MAX_SHORT_BUCKET) {
1183 MEM_SIZE nbytes = ovp->ov_size + 1;
1185 if ((i = nbytes & 3)) {
1188 ASSERT(*((char *)((caddr_t)ovp + nbytes - RSLOP + i))
1189 == RMAGIC_C, "chunk's tail overwrite");
1192 nbytes = (nbytes + 3) &~ 3;
1193 ASSERT(*(u_int *)((caddr_t)ovp + nbytes - RSLOP) == RMAGIC, "chunk's tail overwrite");
1195 ovp->ov_rmagic = RMAGIC - 1;
1197 ASSERT(OV_INDEX(ovp) < NBUCKETS, "chunk's head overwrite");
1198 size = OV_INDEX(ovp);
1199 ovp->ov_next = nextf[size];
1201 MUTEX_UNLOCK(&malloc_mutex);
1205 * When a program attempts "storage compaction" as mentioned in the
1206 * old malloc man page, it realloc's an already freed block. Usually
1207 * this is the last block it freed; occasionally it might be farther
1208 * back. We have to search all the free lists for the block in order
1209 * to determine its bucket: 1st we make one pass thru the lists
1210 * checking only the first block in each; if that fails we search
1211 * ``reall_srchlen'' blocks in each list for a match (the variable
1212 * is extern so the caller can modify it). If that fails we just copy
1213 * however many bytes was given to realloc() and hope it's not huge.
1215 int reall_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */
1218 realloc(void *mp, size_t nbytes)
1220 register MEM_SIZE onb;
1221 union overhead *ovp;
1224 register int bucket;
1225 int was_alloced = 0, incr;
1226 char *cp = (char*)mp;
1228 #if defined(DEBUGGING) || !defined(PERL_CORE)
1229 MEM_SIZE size = nbytes;
1231 if ((long)nbytes < 0)
1232 croak("%s", "panic: realloc");
1235 BARK_64K_LIMIT("Reallocation",nbytes,size);
1237 return malloc(nbytes);
1239 MUTEX_LOCK(&malloc_mutex);
1240 ovp = (union overhead *)((caddr_t)cp
1241 - sizeof (union overhead) * CHUNK_SHIFT);
1242 bucket = OV_INDEX(ovp);
1243 #ifdef IGNORE_SMALL_BAD_FREE
1244 if ((bucket < FIRST_BUCKET_WITH_CHECK)
1245 || (OV_MAGIC(ovp, bucket) == MAGIC))
1247 if (OV_MAGIC(ovp, bucket) == MAGIC)
1253 * Already free, doing "compaction".
1255 * Search for the old block of memory on the
1256 * free list. First, check the most common
1257 * case (last element free'd), then (this failing)
1258 * the last ``reall_srchlen'' items free'd.
1259 * If all lookups fail, then assume the size of
1260 * the memory block being realloc'd is the
1261 * smallest possible.
1263 if ((bucket = findbucket(ovp, 1)) < 0 &&
1264 (bucket = findbucket(ovp, reall_srchlen)) < 0)
1267 onb = BUCKET_SIZE_REAL(bucket);
1269 * avoid the copy if same size block.
1270 * We are not agressive with boundary cases. Note that it might
1271 * (for a small number of cases) give false negative if
1272 * both new size and old one are in the bucket for
1273 * FIRST_BIG_POW2, but the new one is near the lower end.
1275 * We do not try to go to 1.5 times smaller bucket so far.
1277 if (nbytes > onb) incr = 1;
1279 #ifdef DO_NOT_TRY_HARDER_WHEN_SHRINKING
1280 if ( /* This is a little bit pessimal if PACK_MALLOC: */
1281 nbytes > ( (onb >> 1) - M_OVERHEAD )
1282 # ifdef TWO_POT_OPTIMIZE
1283 || (bucket == FIRST_BIG_POW2 && nbytes >= LAST_SMALL_BOUND )
1286 #else /* !DO_NOT_TRY_HARDER_WHEN_SHRINKING */
1287 prev_bucket = ( (bucket > MAX_PACKED + 1)
1288 ? bucket - BUCKETS_PER_POW2
1290 if (nbytes > BUCKET_SIZE_REAL(prev_bucket))
1291 #endif /* !DO_NOT_TRY_HARDER_WHEN_SHRINKING */
1296 #ifdef STRESS_REALLOC
1297 || 1 /* always do it the hard way */
1300 else if (incr == 0) {
1304 * Record new allocated size of block and
1305 * bound space with magic numbers.
1307 if (OV_INDEX(ovp) <= MAX_SHORT_BUCKET) {
1308 int i, nb = ovp->ov_size + 1;
1313 ASSERT(*((char *)((caddr_t)ovp + nb - RSLOP + i)) == RMAGIC_C, "chunk's tail overwrite");
1317 ASSERT(*(u_int *)((caddr_t)ovp + nb - RSLOP) == RMAGIC, "chunk's tail overwrite");
1319 * Convert amount of memory requested into
1320 * closest block size stored in hash buckets
1321 * which satisfies request. Account for
1322 * space used per block for accounting.
1324 nbytes += M_OVERHEAD;
1325 ovp->ov_size = nbytes - 1;
1326 if ((i = nbytes & 3)) {
1329 *((char *)((caddr_t)ovp + nbytes - RSLOP + i))
1332 nbytes = (nbytes + 3) &~ 3;
1333 *((u_int *)((caddr_t)ovp + nbytes - RSLOP)) = RMAGIC;
1337 MUTEX_UNLOCK(&malloc_mutex);
1338 } else if (incr == 1 && (cp - M_OVERHEAD == last_op)
1339 && (onb > (1 << LOG_OF_MIN_ARENA))) {
1340 MEM_SIZE require, newarena = nbytes, pow;
1343 POW2_OPTIMIZE_ADJUST(newarena);
1344 newarena = newarena + M_OVERHEAD;
1345 /* newarena = (newarena + 3) &~ 3; */
1346 shiftr = (newarena - 1) >> LOG_OF_MIN_ARENA;
1347 pow = LOG_OF_MIN_ARENA + 1;
1348 /* apart from this loop, this is O(1) */
1349 while (shiftr >>= 1)
1351 newarena = (1 << pow) + POW2_OPTIMIZE_SURPLUS(pow * BUCKETS_PER_POW2);
1352 require = newarena - onb - M_OVERHEAD;
1354 if (getpages_adjacent(require)) {
1355 #ifdef DEBUGGING_MSTATS
1357 nmalloc[pow * BUCKETS_PER_POW2]++;
1359 *(cp - M_OVERHEAD) = pow * BUCKETS_PER_POW2; /* Fill index. */
1365 MUTEX_UNLOCK(&malloc_mutex);
1366 if ((res = (char*)malloc(nbytes)) == NULL)
1368 if (cp != res) /* common optimization */
1369 Copy(cp, res, (MEM_SIZE)(nbytes<onb?nbytes:onb), char);
1374 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lu: (%05lu) rfree\n",
1375 (unsigned long)res,(unsigned long)(an++)));
1376 DEBUG_m(PerlIO_printf(Perl_debug_log,
1377 "0x%lx: (%05lu) realloc %ld bytes\n",
1378 (unsigned long)res,(unsigned long)(an++),
1380 return ((Malloc_t)res);
1384 * Search ``srchlen'' elements of each free list for a block whose
1385 * header starts at ``freep''. If srchlen is -1 search the whole list.
1386 * Return bucket number, or -1 if not found.
1389 findbucket(union overhead *freep, int srchlen)
1391 register union overhead *p;
1394 for (i = 0; i < NBUCKETS; i++) {
1396 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
1406 calloc(register size_t elements, register size_t size)
1408 long sz = elements * size;
1409 Malloc_t p = malloc(sz);
1412 memset((void*)p, 0, sz);
1418 malloced_size(void *p)
1420 int bucket = OV_INDEX((union overhead *)p);
1422 return BUCKET_SIZE_REAL(bucket);
1425 #ifdef DEBUGGING_MSTATS
1427 # ifdef BUCKETS_ROOT2
1428 # define MIN_EVEN_REPORT 6
1430 # define MIN_EVEN_REPORT MIN_BUCKET
1433 * mstats - print out statistics about malloc
1435 * Prints two lines of numbers, one showing the length of the free list
1436 * for each size category, the second showing the number of mallocs -
1437 * frees for each size category.
1440 dump_mstats(char *s)
1443 register union overhead *p;
1444 int topbucket=0, topbucket_ev=0, topbucket_odd=0, totfree=0, total=0;
1445 u_int nfree[NBUCKETS];
1446 int total_chain = 0;
1447 struct chunk_chain_s* nextchain = chunk_chain;
1449 for (i = MIN_BUCKET ; i < NBUCKETS; i++) {
1450 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
1453 totfree += nfree[i] * BUCKET_SIZE_REAL(i);
1454 total += nmalloc[i] * BUCKET_SIZE_REAL(i);
1456 i % 2 ? (topbucket_odd = i) : (topbucket_ev = i);
1461 PerlIO_printf(PerlIO_stderr(),
1462 "Memory allocation statistics %s (buckets %ld(%ld)..%ld(%ld)\n",
1464 (long)BUCKET_SIZE_REAL(MIN_BUCKET),
1465 (long)BUCKET_SIZE(MIN_BUCKET),
1466 (long)BUCKET_SIZE_REAL(topbucket), (long)BUCKET_SIZE(topbucket));
1467 PerlIO_printf(PerlIO_stderr(), "%8d free:", totfree);
1468 for (i = MIN_EVEN_REPORT; i <= topbucket; i += BUCKETS_PER_POW2) {
1469 PerlIO_printf(PerlIO_stderr(),
1470 ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
1472 : ((i < 12*BUCKETS_PER_POW2) ? " %3d" : " %d")),
1475 #ifdef BUCKETS_ROOT2
1476 PerlIO_printf(PerlIO_stderr(), "\n\t ");
1477 for (i = MIN_BUCKET + 1; i <= topbucket_odd; i += BUCKETS_PER_POW2) {
1478 PerlIO_printf(PerlIO_stderr(),
1479 ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
1481 : ((i < 12*BUCKETS_PER_POW2) ? " %3d" : " %d")),
1485 PerlIO_printf(PerlIO_stderr(), "\n%8d used:", total - totfree);
1486 for (i = MIN_EVEN_REPORT; i <= topbucket; i += BUCKETS_PER_POW2) {
1487 PerlIO_printf(PerlIO_stderr(),
1488 ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
1490 : ((i < 12*BUCKETS_PER_POW2) ? " %3d" : " %d")),
1491 nmalloc[i] - nfree[i]);
1493 #ifdef BUCKETS_ROOT2
1494 PerlIO_printf(PerlIO_stderr(), "\n\t ");
1495 for (i = MIN_BUCKET + 1; i <= topbucket_odd; i += BUCKETS_PER_POW2) {
1496 PerlIO_printf(PerlIO_stderr(),
1497 ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
1499 : ((i < 12*BUCKETS_PER_POW2) ? " %3d" : " %d")),
1500 nmalloc[i] - nfree[i]);
1504 total_chain += nextchain->size;
1505 nextchain = nextchain->next;
1507 PerlIO_printf(PerlIO_stderr(), "\nTotal sbrk(): %d/%d:%d. Odd ends: pad+heads+chain+tail: %d+%d+%d+%d.\n",
1508 goodsbrk + sbrk_slack, sbrks, sbrk_good, sbrk_slack,
1509 start_slack, total_chain, sbrked_remains);
1513 dump_mstats(char *s)
1520 #ifdef USE_PERL_SBRK
1523 # define PERL_SBRK_VIA_MALLOC
1526 # ifdef PERL_SBRK_VIA_MALLOC
1527 # if defined(HIDEMYMALLOC) || defined(EMBEDMYMALLOC)
1530 # include "Error: -DPERL_SBRK_VIA_MALLOC needs -D(HIDE|EMBED)MYMALLOC"
1533 /* it may seem schizophrenic to use perl's malloc and let it call system */
1534 /* malloc, the reason for that is only the 3.2 version of the OS that had */
1535 /* frequent core dumps within nxzonefreenolock. This sbrk routine put an */
1536 /* end to the cores */
1538 # define SYSTEM_ALLOC(a) malloc(a)
1540 # endif /* PERL_SBRK_VIA_MALLOC */
1542 static IV Perl_sbrk_oldchunk;
1543 static long Perl_sbrk_oldsize;
1545 # define PERLSBRK_32_K (1<<15)
1546 # define PERLSBRK_64_K (1<<16)
1555 if (!size) return 0;
1557 reqsize = size; /* just for the DEBUG_m statement */
1560 size = (size + 0x7ff) & ~0x7ff;
1562 if (size <= Perl_sbrk_oldsize) {
1563 got = Perl_sbrk_oldchunk;
1564 Perl_sbrk_oldchunk += size;
1565 Perl_sbrk_oldsize -= size;
1567 if (size >= PERLSBRK_32_K) {
1570 size = PERLSBRK_64_K;
1573 got = (IV)SYSTEM_ALLOC(size);
1575 got = (got + 0x7ff) & ~0x7ff;
1578 /* Chunk is small, register the rest for future allocs. */
1579 Perl_sbrk_oldchunk = got + reqsize;
1580 Perl_sbrk_oldsize = size - reqsize;
1584 DEBUG_m(PerlIO_printf(Perl_debug_log, "sbrk malloc size %ld (reqsize %ld), left size %ld, give addr 0x%lx\n",
1585 size, reqsize, Perl_sbrk_oldsize, got));
1590 #endif /* ! defined USE_PERL_SBRK */