6 * "'The Chamber of Records,' said Gimli. 'I guess that is where we now stand.'"
10 Here are some notes on configuring Perl's malloc. (For non-perl
13 There are two macros which serve as bulk disablers of advanced
14 features of this malloc: NO_FANCY_MALLOC, PLAIN_MALLOC (undef by
15 default). Look in the list of default values below to understand
16 their exact effect. Defining NO_FANCY_MALLOC returns malloc.c to the
17 state of the malloc in Perl 5.004. Additionally defining PLAIN_MALLOC
18 returns it to the state as of Perl 5.000.
20 Note that some of the settings below may be ignored in the code based
21 on values of other macros. The PERL_CORE symbol is only defined when
22 perl itself is being compiled (so malloc can make some assumptions
23 about perl's facilities being available to it).
25 Each config option has a short description, followed by its name,
26 default value, and a comment about the default (if applicable). Some
27 options take a precise value, while the others are just boolean.
28 The boolean ones are listed first.
30 # Enable code for an emergency memory pool in $^M. See perlvar.pod
31 # for a description of $^M.
32 PERL_EMERGENCY_SBRK (!PLAIN_MALLOC && PERL_CORE)
34 # Enable code for printing memory statistics.
35 DEBUGGING_MSTATS (!PLAIN_MALLOC && PERL_CORE)
37 # Move allocation info for small buckets into separate areas.
38 # Memory optimization (especially for small allocations, of the
39 # less than 64 bytes). Since perl usually makes a large number
40 # of small allocations, this is usually a win.
41 PACK_MALLOC (!PLAIN_MALLOC && !RCHECK)
43 # Add one page to big powers of two when calculating bucket size.
44 # This is targeted at big allocations, as are common in image
46 TWO_POT_OPTIMIZE !PLAIN_MALLOC
48 # Use intermediate bucket sizes between powers-of-two. This is
49 # generally a memory optimization, and a (small) speed pessimization.
50 BUCKETS_ROOT2 !NO_FANCY_MALLOC
52 # Do not check small deallocations for bad free(). Memory
53 # and speed optimization, error reporting pessimization.
54 IGNORE_SMALL_BAD_FREE (!NO_FANCY_MALLOC && !RCHECK)
56 # Use table lookup to decide in which bucket a given allocation will go.
57 SMALL_BUCKET_VIA_TABLE !NO_FANCY_MALLOC
59 # Use a perl-defined sbrk() instead of the (presumably broken or
60 # missing) system-supplied sbrk().
63 # Use system malloc() (or calloc() etc.) to emulate sbrk(). Normally
64 # only used with broken sbrk()s.
65 PERL_SBRK_VIA_MALLOC undef
67 # Which allocator to use if PERL_SBRK_VIA_MALLOC
68 SYSTEM_ALLOC(a) malloc(a)
70 # Minimal alignment (in bytes, should be a power of 2) of SYSTEM_ALLOC
71 SYSTEM_ALLOC_ALIGNMENT MEM_ALIGNBYTES
73 # Disable memory overwrite checking with DEBUGGING. Memory and speed
74 # optimization, error reporting pessimization.
77 # Enable memory overwrite checking with DEBUGGING. Memory and speed
78 # pessimization, error reporting optimization
79 RCHECK (DEBUGGING && !NO_RCHECK)
81 # Failed allocations bigger than this size croak (if
82 # PERL_EMERGENCY_SBRK is enabled) without touching $^M. See
83 # perlvar.pod for a description of $^M.
84 BIG_SIZE (1<<16) # 64K
86 # Starting from this power of two, add an extra page to the
87 # size of the bucket. This enables optimized allocations of sizes
88 # close to powers of 2. Note that the value is indexed at 0.
89 FIRST_BIG_POW2 15 # 32K, 16K is used too often
91 # Estimate of minimal memory footprint. malloc uses this value to
92 # request the most reasonable largest blocks of memory from the system.
95 # Round up sbrk()s to multiples of this.
98 # Round up sbrk()s to multiples of this percent of footprint.
101 # Add this much memory to big powers of two to get the bucket size.
104 # This many sbrk() discontinuities should be tolerated even
105 # from the start without deciding that sbrk() is usually
107 SBRK_ALLOW_FAILURES 3
109 # This many continuous sbrk()s compensate for one discontinuous one.
110 SBRK_FAILURE_PRICE 50
112 # Some configurations may ask for 12-byte-or-so allocations which
113 # require 8-byte alignment (?!). In such situation one needs to
114 # define this to disable 12-byte bucket (will increase memory footprint)
115 STRICT_ALIGNMENT undef
117 This implementation assumes that calling PerlIO_printf() does not
118 result in any memory allocation calls (used during a panic).
123 If used outside of Perl environment, it may be useful to redefine
124 the following macros (listed below with defaults):
126 # Type of address returned by allocation functions
129 # Type of size argument for allocation functions
130 MEM_SIZE unsigned long
135 # Maximal value in LONG
138 # Unsigned integer type big enough to keep a pointer
141 # Type of pointer with 1-byte granularity
144 # Type returned by free()
147 # Very fatal condition reporting function (cannot call any )
148 fatalcroak(arg) write(2,arg,strlen(arg)) + exit(2)
150 # Fatal error reporting function
151 croak(format, arg) warn(idem) + exit(1)
153 # Fatal error reporting function
154 croak2(format, arg1, arg2) warn2(idem) + exit(1)
156 # Error reporting function
157 warn(format, arg) fprintf(stderr, idem)
159 # Error reporting function
160 warn2(format, arg1, arg2) fprintf(stderr, idem)
162 # Locking/unlocking for MT operation
163 MALLOC_LOCK MUTEX_LOCK(&PL_malloc_mutex)
164 MALLOC_UNLOCK MUTEX_UNLOCK(&PL_malloc_mutex)
166 # Locking/unlocking mutex for MT operation
171 #ifndef NO_FANCY_MALLOC
172 # ifndef SMALL_BUCKET_VIA_TABLE
173 # define SMALL_BUCKET_VIA_TABLE
175 # ifndef BUCKETS_ROOT2
176 # define BUCKETS_ROOT2
178 # ifndef IGNORE_SMALL_BAD_FREE
179 # define IGNORE_SMALL_BAD_FREE
183 #ifndef PLAIN_MALLOC /* Bulk enable features */
187 # ifndef TWO_POT_OPTIMIZE
188 # define TWO_POT_OPTIMIZE
190 # if defined(PERL_CORE) && !defined(PERL_EMERGENCY_SBRK)
191 # define PERL_EMERGENCY_SBRK
193 # if defined(PERL_CORE) && !defined(DEBUGGING_MSTATS)
194 # define DEBUGGING_MSTATS
198 #define MIN_BUC_POW2 (sizeof(void*) > 4 ? 3 : 2) /* Allow for 4-byte arena. */
199 #define MIN_BUCKET (MIN_BUC_POW2 * BUCKETS_PER_POW2)
201 #if !(defined(I286) || defined(atarist) || defined(__MINT__))
202 /* take 2k unless the block is bigger than that */
203 # define LOG_OF_MIN_ARENA 11
205 /* take 16k unless the block is bigger than that
206 (80286s like large segments!), probably good on the atari too */
207 # define LOG_OF_MIN_ARENA 14
211 # if defined(DEBUGGING) && !defined(NO_RCHECK)
214 # if defined(RCHECK) && defined(IGNORE_SMALL_BAD_FREE)
215 # undef IGNORE_SMALL_BAD_FREE
218 * malloc.c (Caltech) 2/21/82
219 * Chris Kingsley, kingsley@cit-20.
221 * This is a very fast storage allocator. It allocates blocks of a small
222 * number of different sizes, and keeps free lists of each size. Blocks that
223 * don't exactly fit are passed up to the next larger size. In this
224 * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
225 * If PACK_MALLOC is defined, small blocks are 2^n bytes long.
226 * This is designed for use in a program that uses vast quantities of memory,
227 * but bombs when it runs out.
229 * Modifications Copyright Ilya Zakharevich 1996-99.
231 * Still very quick, but much more thrifty. (Std config is 10% slower
232 * than it was, and takes 67% of old heap size for typical usage.)
234 * Allocations of small blocks are now table-driven to many different
235 * buckets. Sizes of really big buckets are increased to accomodata
236 * common size=power-of-2 blocks. Running-out-of-memory is made into
237 * an exception. Deeply configurable and thread-safe.
243 # define PERL_IN_MALLOC_C
245 # if defined(PERL_IMPLICIT_CONTEXT)
246 # define croak Perl_croak_nocontext
247 # define croak2 Perl_croak_nocontext
248 # define warn Perl_warn_nocontext
249 # define warn2 Perl_warn_nocontext
251 # define croak2 croak
256 # include "../EXTERN.h"
257 # include "../perl.h"
263 # define Malloc_t void *
269 # define MEM_SIZE unsigned long
272 # define LONG_MAX 0x7FFFFFFF
275 # define UV unsigned long
278 # define caddr_t char *
283 # define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
284 # define PerlEnv_getenv getenv
285 # define PerlIO_printf fprintf
286 # define PerlIO_stderr() stderr
288 # ifndef croak /* make depend */
289 # define croak(mess, arg) (warn((mess), (arg)), exit(1))
291 # ifndef croak2 /* make depend */
292 # define croak2(mess, arg1, arg2) (warn2((mess), (arg1), (arg2)), exit(1))
295 # define warn(mess, arg) fprintf(stderr, (mess), (arg))
298 # define warn2(mess, arg1) fprintf(stderr, (mess), (arg1), (arg2))
311 # define dTHX extern int Perl___notused PERL_UNUSED_DECL
313 # define dTHX extern int Perl___notused
315 # define WITH_THX(s) s
317 # ifndef PERL_GET_INTERP
318 # define PERL_GET_INTERP PL_curinterp
321 # define Perl_malloc malloc
324 # define Perl_mfree free
326 # ifndef Perl_realloc
327 # define Perl_realloc realloc
330 # define Perl_calloc calloc
333 # define Perl_strdup strdup
338 # define MUTEX_LOCK(l)
342 # define MUTEX_UNLOCK(l)
346 # define MALLOC_LOCK MUTEX_LOCK(&PL_malloc_mutex)
349 #ifndef MALLOC_UNLOCK
350 # define MALLOC_UNLOCK MUTEX_UNLOCK(&PL_malloc_mutex)
353 # ifndef fatalcroak /* make depend */
354 # define fatalcroak(mess) (write(2, (mess), strlen(mess)), exit(2))
359 # define DEBUG_m(a) \
361 if (PERL_GET_INTERP) { \
363 if (DEBUG_m_TEST) { \
364 PL_debug &= ~DEBUG_m_FLAG; \
366 PL_debug |= DEBUG_m_FLAG; \
372 #ifdef PERL_IMPLICIT_CONTEXT
373 # define PERL_IS_ALIVE aTHX
375 # define PERL_IS_ALIVE TRUE
382 * The memory is broken into "blocks" which occupy multiples of 2K (and
383 * generally speaking, have size "close" to a power of 2). The addresses
384 * of such *unused* blocks are kept in nextf[i] with big enough i. (nextf
385 * is an array of linked lists.) (Addresses of used blocks are not known.)
387 * Moreover, since the algorithm may try to "bite" smaller blocks out
388 * of unused bigger ones, there are also regions of "irregular" size,
389 * managed separately, by a linked list chunk_chain.
391 * The third type of storage is the sbrk()ed-but-not-yet-used space, its
392 * end and size are kept in last_sbrk_top and sbrked_remains.
394 * Growing blocks "in place":
395 * ~~~~~~~~~~~~~~~~~~~~~~~~~
396 * The address of the block with the greatest address is kept in last_op
397 * (if not known, last_op is 0). If it is known that the memory above
398 * last_op is not continuous, or contains a chunk from chunk_chain,
399 * last_op is set to 0.
401 * The chunk with address last_op may be grown by expanding into
402 * sbrk()ed-but-not-yet-used space, or trying to sbrk() more continuous
405 * Management of last_op:
406 * ~~~~~~~~~~~~~~~~~~~~~
408 * free() never changes the boundaries of blocks, so is not relevant.
410 * The only way realloc() may change the boundaries of blocks is if it
411 * grows a block "in place". However, in the case of success such a
412 * chunk is automatically last_op, and it remains last_op. In the case
413 * of failure getpages_adjacent() clears last_op.
415 * malloc() may change blocks by calling morecore() only.
417 * morecore() may create new blocks by:
418 * a) biting pieces from chunk_chain (cannot create one above last_op);
419 * b) biting a piece from an unused block (if block was last_op, this
420 * may create a chunk from chain above last_op, thus last_op is
421 * invalidated in such a case).
422 * c) biting of sbrk()ed-but-not-yet-used space. This creates
423 * a block which is last_op.
424 * d) Allocating new pages by calling getpages();
426 * getpages() creates a new block. It marks last_op at the bottom of
427 * the chunk of memory it returns.
429 * Active pages footprint:
430 * ~~~~~~~~~~~~~~~~~~~~~~
431 * Note that we do not need to traverse the lists in nextf[i], just take
432 * the first element of this list. However, we *need* to traverse the
433 * list in chunk_chain, but most the time it should be a very short one,
434 * so we do not step on a lot of pages we are not going to use.
438 * get_from_bigger_buckets(): forget to increment price => Quite
442 /* I don't much care whether these are defined in sys/types.h--LAW */
444 #define u_char unsigned char
445 #define u_int unsigned int
447 * I removed the definition of u_bigint which appeared to be u_bigint = UV
448 * u_bigint was only used in TWOK_MASKED and TWOK_SHIFT
449 * where I have used PTR2UV. RMB
451 #define u_short unsigned short
453 /* 286 and atarist like big chunks, which gives too much overhead. */
454 #if (defined(RCHECK) || defined(I286) || defined(atarist) || defined(__MINT__)) && defined(PACK_MALLOC)
459 * The description below is applicable if PACK_MALLOC is not defined.
461 * The overhead on a block is at least 4 bytes. When free, this space
462 * contains a pointer to the next free block, and the bottom two bits must
463 * be zero. When in use, the first byte is set to MAGIC, and the second
464 * byte is the size index. The remaining bytes are for alignment.
465 * If range checking is enabled and the size of the block fits
466 * in two bytes, then the top two bytes hold the size of the requested block
467 * plus the range checking words, and the header word MINUS ONE.
470 union overhead *ov_next; /* when free */
471 #if MEM_ALIGNBYTES > 4
472 double strut; /* alignment problems */
476 * Keep the ovu_index and ovu_magic in this order, having a char
477 * field first gives alignment indigestion in some systems, such as
480 u_char ovu_index; /* bucket # */
481 u_char ovu_magic; /* magic number */
483 u_short ovu_size; /* actual block size */
484 u_int ovu_rmagic; /* range magic number */
487 #define ov_magic ovu.ovu_magic
488 #define ov_index ovu.ovu_index
489 #define ov_size ovu.ovu_size
490 #define ov_rmagic ovu.ovu_rmagic
493 #define MAGIC 0xff /* magic # on accounting info */
494 #define RMAGIC 0x55555555 /* magic # on range info */
495 #define RMAGIC_C 0x55 /* magic # on range info */
498 # define RSLOP sizeof (u_int)
499 # ifdef TWO_POT_OPTIMIZE
500 # define MAX_SHORT_BUCKET (12 * BUCKETS_PER_POW2)
502 # define MAX_SHORT_BUCKET (13 * BUCKETS_PER_POW2)
508 #if !defined(PACK_MALLOC) && defined(BUCKETS_ROOT2)
509 # undef BUCKETS_ROOT2
513 # define BUCKET_TABLE_SHIFT 2
514 # define BUCKET_POW2_SHIFT 1
515 # define BUCKETS_PER_POW2 2
517 # define BUCKET_TABLE_SHIFT MIN_BUC_POW2
518 # define BUCKET_POW2_SHIFT 0
519 # define BUCKETS_PER_POW2 1
522 #if !defined(MEM_ALIGNBYTES) || ((MEM_ALIGNBYTES > 4) && !defined(STRICT_ALIGNMENT))
523 /* Figure out the alignment of void*. */
528 # define ALIGN_SMALL ((int)((caddr_t)&(((struct aligner*)0)->p)))
530 # define ALIGN_SMALL MEM_ALIGNBYTES
533 #define IF_ALIGN_8(yes,no) ((ALIGN_SMALL>4) ? (yes) : (no))
536 # define MAX_BUCKET_BY_TABLE 13
537 static u_short buck_size[MAX_BUCKET_BY_TABLE + 1] =
539 0, 0, 0, 0, 4, 4, 8, 12, 16, 24, 32, 48, 64, 80,
541 # define BUCKET_SIZE(i) ((i) % 2 ? buck_size[i] : (1 << ((i) >> BUCKET_POW2_SHIFT)))
542 # define BUCKET_SIZE_REAL(i) ((i) <= MAX_BUCKET_BY_TABLE \
544 : ((1 << ((i) >> BUCKET_POW2_SHIFT)) \
546 + POW2_OPTIMIZE_SURPLUS(i)))
548 # define BUCKET_SIZE(i) (1 << ((i) >> BUCKET_POW2_SHIFT))
549 # define BUCKET_SIZE_REAL(i) (BUCKET_SIZE(i) - MEM_OVERHEAD(i) + POW2_OPTIMIZE_SURPLUS(i))
554 /* In this case there are several possible layout of arenas depending
555 * on the size. Arenas are of sizes multiple to 2K, 2K-aligned, and
556 * have a size close to a power of 2.
558 * Arenas of the size >= 4K keep one chunk only. Arenas of size 2K
559 * may keep one chunk or multiple chunks. Here are the possible
562 * # One chunk only, chunksize 2^k + SOMETHING - ALIGN, k >= 11
564 * INDEX MAGIC1 UNUSED CHUNK1
566 * # Multichunk with sanity checking and chunksize 2^k-ALIGN, k>7
568 * INDEX MAGIC1 MAGIC2 MAGIC3 UNUSED CHUNK1 CHUNK2 CHUNK3 ...
570 * # Multichunk with sanity checking and size 2^k-ALIGN, k=7
572 * INDEX MAGIC1 MAGIC2 MAGIC3 UNUSED CHUNK1 UNUSED CHUNK2 CHUNK3 ...
574 * # Multichunk with sanity checking and size up to 80
576 * INDEX UNUSED MAGIC1 UNUSED MAGIC2 UNUSED ... CHUNK1 CHUNK2 CHUNK3 ...
578 * # No sanity check (usually up to 48=byte-long buckets)
579 * INDEX UNUSED CHUNK1 CHUNK2 ...
581 * Above INDEX and MAGIC are one-byte-long. Sizes of UNUSED are
582 * appropriate to keep algorithms simple and memory aligned. INDEX
583 * encodes the size of the chunk, while MAGICn encodes state (used,
584 * free or non-managed-by-us-so-it-indicates-a-bug) of CHUNKn. MAGIC
585 * is used for sanity checking purposes only. SOMETHING is 0 or 4K
586 * (to make size of big CHUNK accomodate allocations for powers of two
589 * [There is no need to alignment between chunks, since C rules ensure
590 * that structs which need 2^k alignment have sizeof which is
591 * divisible by 2^k. Thus as far as the last chunk is aligned at the
592 * end of the arena, and 2K-alignment does not contradict things,
593 * everything is going to be OK for sizes of chunks 2^n and 2^n +
594 * 2^k. Say, 80-bit buckets will be 16-bit aligned, and as far as we
595 * put allocations for requests in 65..80 range, all is fine.
597 * Note, however, that standard malloc() puts more strict
598 * requirements than the above C rules. Moreover, our algorithms of
599 * realloc() may break this idyll, but we suppose that realloc() does
600 * need not change alignment.]
602 * Is very important to make calculation of the offset of MAGICm as
603 * quick as possible, since it is done on each malloc()/free(). In
604 * fact it is so quick that it has quite little effect on the speed of
605 * doing malloc()/free(). [By default] We forego such calculations
606 * for small chunks, but only to save extra 3% of memory, not because
607 * of speed considerations.
609 * Here is the algorithm [which is the same for all the allocations
610 * schemes above], see OV_MAGIC(block,bucket). Let OFFSETm be the
611 * offset of the CHUNKm from the start of ARENA. Then offset of
612 * MAGICm is (OFFSET1 >> SHIFT) + ADDOFFSET. Here SHIFT and ADDOFFSET
613 * are numbers which depend on the size of the chunks only.
615 * Let as check some sanity conditions. Numbers OFFSETm>>SHIFT are
616 * different for all the chunks in the arena if 2^SHIFT is not greater
617 * than size of the chunks in the arena. MAGIC1 will not overwrite
618 * INDEX provided ADDOFFSET is >0 if OFFSET1 < 2^SHIFT. MAGIClast
619 * will not overwrite CHUNK1 if OFFSET1 > (OFFSETlast >> SHIFT) +
622 * Make SHIFT the maximal possible (there is no point in making it
623 * smaller). Since OFFSETlast is 2K - CHUNKSIZE, above restrictions
624 * give restrictions on OFFSET1 and on ADDOFFSET.
626 * In particular, for chunks of size 2^k with k>=6 we can put
627 * ADDOFFSET to be from 0 to 2^k - 2^(11-k), and have
628 * OFFSET1==chunksize. For chunks of size 80 OFFSET1 of 2K%80=48 is
629 * large enough to have ADDOFFSET between 1 and 16 (similarly for 96,
630 * when ADDOFFSET should be 1). In particular, keeping MAGICs for
631 * these sizes gives no additional size penalty.
633 * However, for chunks of size 2^k with k<=5 this gives OFFSET1 >=
634 * ADDOFSET + 2^(11-k). Keeping ADDOFFSET 0 allows for 2^(11-k)-2^(11-2k)
635 * chunks per arena. This is smaller than 2^(11-k) - 1 which are
636 * needed if no MAGIC is kept. [In fact, having a negative ADDOFFSET
637 * would allow for slightly more buckets per arena for k=2,3.]
639 * Similarly, for chunks of size 3/2*2^k with k<=5 MAGICs would span
640 * the area up to 2^(11-k)+ADDOFFSET. For k=4 this give optimal
641 * ADDOFFSET as -7..0. For k=3 ADDOFFSET can go up to 4 (with tiny
642 * savings for negative ADDOFFSET). For k=5 ADDOFFSET can go -1..16
643 * (with no savings for negative values).
645 * In particular, keeping ADDOFFSET 0 for sizes of chunks up to 2^6
646 * leads to tiny pessimizations in case of sizes 4, 8, 12, 24, and
647 * leads to no contradictions except for size=80 (or 96.)
649 * However, it also makes sense to keep no magic for sizes 48 or less.
650 * This is what we do. In this case one needs ADDOFFSET>=1 also for
651 * chunksizes 12, 24, and 48, unless one gets one less chunk per
654 * The algo of OV_MAGIC(block,bucket) keeps ADDOFFSET 0 until
655 * chunksize of 64, then makes it 1.
657 * This allows for an additional optimization: the above scheme leads
658 * to giant overheads for sizes 128 or more (one whole chunk needs to
659 * be sacrifised to keep INDEX). Instead we use chunks not of size
660 * 2^k, but of size 2^k-ALIGN. If we pack these chunks at the end of
661 * the arena, then the beginnings are still in different 2^k-long
662 * sections of the arena if k>=7 for ALIGN==4, and k>=8 if ALIGN=8.
663 * Thus for k>7 the above algo of calculating the offset of the magic
664 * will still give different answers for different chunks. And to
665 * avoid the overrun of MAGIC1 into INDEX, one needs ADDOFFSET of >=1.
666 * In the case k=7 we just move the first chunk an extra ALIGN
667 * backward inside the ARENA (this is done once per arena lifetime,
668 * thus is not a big overhead). */
669 # define MAX_PACKED_POW2 6
670 # define MAX_PACKED (MAX_PACKED_POW2 * BUCKETS_PER_POW2 + BUCKET_POW2_SHIFT)
671 # define MAX_POW2_ALGO ((1<<(MAX_PACKED_POW2 + 1)) - M_OVERHEAD)
672 # define TWOK_MASK ((1<<LOG_OF_MIN_ARENA) - 1)
673 # define TWOK_MASKED(x) (PTR2UV(x) & ~TWOK_MASK)
674 # define TWOK_SHIFT(x) (PTR2UV(x) & TWOK_MASK)
675 # define OV_INDEXp(block) (INT2PTR(u_char*,TWOK_MASKED(block)))
676 # define OV_INDEX(block) (*OV_INDEXp(block))
677 # define OV_MAGIC(block,bucket) (*(OV_INDEXp(block) + \
678 (TWOK_SHIFT(block)>> \
679 (bucket>>BUCKET_POW2_SHIFT)) + \
680 (bucket >= MIN_NEEDS_SHIFT ? 1 : 0)))
681 /* A bucket can have a shift smaller than it size, we need to
682 shift its magic number so it will not overwrite index: */
683 # ifdef BUCKETS_ROOT2
684 # define MIN_NEEDS_SHIFT (7*BUCKETS_PER_POW2 - 1) /* Shift 80 greater than chunk 64. */
686 # define MIN_NEEDS_SHIFT (7*BUCKETS_PER_POW2) /* Shift 128 greater than chunk 32. */
688 # define CHUNK_SHIFT 0
690 /* Number of active buckets of given ordinal. */
691 #ifdef IGNORE_SMALL_BAD_FREE
692 #define FIRST_BUCKET_WITH_CHECK (6 * BUCKETS_PER_POW2) /* 64 */
693 # define N_BLKS(bucket) ( (bucket) < FIRST_BUCKET_WITH_CHECK \
694 ? ((1<<LOG_OF_MIN_ARENA) - 1)/BUCKET_SIZE(bucket) \
697 # define N_BLKS(bucket) n_blks[bucket]
700 static u_short n_blks[LOG_OF_MIN_ARENA * BUCKETS_PER_POW2] =
702 # if BUCKETS_PER_POW2==1
704 (MIN_BUC_POW2==2 ? 384 : 0),
705 224, 120, 62, 31, 16, 8, 4, 2
708 (MIN_BUC_POW2==2 ? 384 : 0), (MIN_BUC_POW2==2 ? 384 : 0), /* 4, 4 */
709 224, 149, 120, 80, 62, 41, 31, 25, 16, 16, 8, 8, 4, 4, 2, 2
713 /* Shift of the first bucket with the given ordinal inside 2K chunk. */
714 #ifdef IGNORE_SMALL_BAD_FREE
715 # define BLK_SHIFT(bucket) ( (bucket) < FIRST_BUCKET_WITH_CHECK \
716 ? ((1<<LOG_OF_MIN_ARENA) \
717 - BUCKET_SIZE(bucket) * N_BLKS(bucket)) \
720 # define BLK_SHIFT(bucket) blk_shift[bucket]
723 static u_short blk_shift[LOG_OF_MIN_ARENA * BUCKETS_PER_POW2] =
725 # if BUCKETS_PER_POW2==1
727 (MIN_BUC_POW2==2 ? 512 : 0),
728 256, 128, 64, 64, /* 8 to 64 */
729 16*sizeof(union overhead),
730 8*sizeof(union overhead),
731 4*sizeof(union overhead),
732 2*sizeof(union overhead),
735 (MIN_BUC_POW2==2 ? 512 : 0), (MIN_BUC_POW2==2 ? 512 : 0),
736 256, 260, 128, 128, 64, 80, 64, 48, /* 8 to 96 */
737 16*sizeof(union overhead), 16*sizeof(union overhead),
738 8*sizeof(union overhead), 8*sizeof(union overhead),
739 4*sizeof(union overhead), 4*sizeof(union overhead),
740 2*sizeof(union overhead), 2*sizeof(union overhead),
744 # define NEEDED_ALIGNMENT 0x800 /* 2k boundaries */
745 # define WANTED_ALIGNMENT 0x800 /* 2k boundaries */
747 #else /* !PACK_MALLOC */
749 # define OV_MAGIC(block,bucket) (block)->ov_magic
750 # define OV_INDEX(block) (block)->ov_index
751 # define CHUNK_SHIFT 1
752 # define MAX_PACKED -1
753 # define NEEDED_ALIGNMENT MEM_ALIGNBYTES
754 # define WANTED_ALIGNMENT 0x400 /* 1k boundaries */
756 #endif /* !PACK_MALLOC */
758 #define M_OVERHEAD (sizeof(union overhead) + RSLOP)
761 # define MEM_OVERHEAD(bucket) \
762 (bucket <= MAX_PACKED ? 0 : M_OVERHEAD)
763 # ifdef SMALL_BUCKET_VIA_TABLE
764 # define START_SHIFTS_BUCKET ((MAX_PACKED_POW2 + 1) * BUCKETS_PER_POW2)
765 # define START_SHIFT MAX_PACKED_POW2
766 # ifdef BUCKETS_ROOT2 /* Chunks of size 3*2^n. */
767 # define SIZE_TABLE_MAX 80
769 # define SIZE_TABLE_MAX 64
771 static char bucket_of[] =
773 # ifdef BUCKETS_ROOT2 /* Chunks of size 3*2^n. */
774 /* 0 to 15 in 4-byte increments. */
775 (sizeof(void*) > 4 ? 6 : 5), /* 4/8, 5-th bucket for better reports */
777 IF_ALIGN_8(8,7), 8, /* 16/12, 16 */
778 9, 9, 10, 10, /* 24, 32 */
779 11, 11, 11, 11, /* 48 */
780 12, 12, 12, 12, /* 64 */
781 13, 13, 13, 13, /* 80 */
782 13, 13, 13, 13 /* 80 */
783 # else /* !BUCKETS_ROOT2 */
784 /* 0 to 15 in 4-byte increments. */
785 (sizeof(void*) > 4 ? 3 : 2),
791 # endif /* !BUCKETS_ROOT2 */
793 # else /* !SMALL_BUCKET_VIA_TABLE */
794 # define START_SHIFTS_BUCKET MIN_BUCKET
795 # define START_SHIFT (MIN_BUC_POW2 - 1)
796 # endif /* !SMALL_BUCKET_VIA_TABLE */
797 #else /* !PACK_MALLOC */
798 # define MEM_OVERHEAD(bucket) M_OVERHEAD
799 # ifdef SMALL_BUCKET_VIA_TABLE
800 # undef SMALL_BUCKET_VIA_TABLE
802 # define START_SHIFTS_BUCKET MIN_BUCKET
803 # define START_SHIFT (MIN_BUC_POW2 - 1)
804 #endif /* !PACK_MALLOC */
807 * Big allocations are often of the size 2^n bytes. To make them a
808 * little bit better, make blocks of size 2^n+pagesize for big n.
811 #ifdef TWO_POT_OPTIMIZE
813 # ifndef PERL_PAGESIZE
814 # define PERL_PAGESIZE 4096
816 # ifndef FIRST_BIG_POW2
817 # define FIRST_BIG_POW2 15 /* 32K, 16K is used too often. */
819 # define FIRST_BIG_BLOCK (1<<FIRST_BIG_POW2)
820 /* If this value or more, check against bigger blocks. */
821 # define FIRST_BIG_BOUND (FIRST_BIG_BLOCK - M_OVERHEAD)
822 /* If less than this value, goes into 2^n-overhead-block. */
823 # define LAST_SMALL_BOUND ((FIRST_BIG_BLOCK>>1) - M_OVERHEAD)
825 # define POW2_OPTIMIZE_ADJUST(nbytes) \
826 ((nbytes >= FIRST_BIG_BOUND) ? nbytes -= PERL_PAGESIZE : 0)
827 # define POW2_OPTIMIZE_SURPLUS(bucket) \
828 ((bucket >= FIRST_BIG_POW2 * BUCKETS_PER_POW2) ? PERL_PAGESIZE : 0)
830 #else /* !TWO_POT_OPTIMIZE */
831 # define POW2_OPTIMIZE_ADJUST(nbytes)
832 # define POW2_OPTIMIZE_SURPLUS(bucket) 0
833 #endif /* !TWO_POT_OPTIMIZE */
835 #if defined(HAS_64K_LIMIT) && defined(PERL_CORE)
836 # define BARK_64K_LIMIT(what,nbytes,size) \
837 if (nbytes > 0xffff) { \
838 PerlIO_printf(PerlIO_stderr(), \
839 "%s too large: %lx\n", what, size); \
842 #else /* !HAS_64K_LIMIT || !PERL_CORE */
843 # define BARK_64K_LIMIT(what,nbytes,size)
844 #endif /* !HAS_64K_LIMIT || !PERL_CORE */
847 # define MIN_SBRK 2048
851 # define FIRST_SBRK (48*1024)
854 /* Minimal sbrk in percents of what is already alloced. */
855 #ifndef MIN_SBRK_FRAC
856 # define MIN_SBRK_FRAC 3
859 #ifndef SBRK_ALLOW_FAILURES
860 # define SBRK_ALLOW_FAILURES 3
863 #ifndef SBRK_FAILURE_PRICE
864 # define SBRK_FAILURE_PRICE 50
867 static void morecore (register int bucket);
868 # if defined(DEBUGGING)
869 static void botch (char *diag, char *s);
871 static void add_to_chain (void *p, MEM_SIZE size, MEM_SIZE chip);
872 static void* get_from_chain (MEM_SIZE size);
873 static void* get_from_bigger_buckets(int bucket, MEM_SIZE size);
874 static union overhead *getpages (MEM_SIZE needed, int *nblksp, int bucket);
875 static int getpages_adjacent(MEM_SIZE require);
879 #ifdef I_MACH_CTHREADS
881 # define MUTEX_LOCK(m) STMT_START { if (*m) mutex_lock(*m); } STMT_END
883 # define MUTEX_UNLOCK(m) STMT_START { if (*m) mutex_unlock(*m); } STMT_END
887 # define BITS_IN_PTR (8*PTRSIZE)
891 * nextf[i] is the pointer to the next free block of size 2^i. The
892 * smallest allocatable block is 8 bytes. The overhead information
893 * precedes the data area returned to the user.
895 #define NBUCKETS (BITS_IN_PTR*BUCKETS_PER_POW2 + 1)
896 static union overhead *nextf[NBUCKETS];
898 #if defined(PURIFY) && !defined(USE_PERL_SBRK)
899 # define USE_PERL_SBRK
903 # define sbrk(a) Perl_sbrk(a)
904 Malloc_t Perl_sbrk (int size);
906 # ifndef HAS_SBRK_PROTO /* <unistd.h> usually takes care of this */
907 extern Malloc_t sbrk(int);
911 #ifdef DEBUGGING_MSTATS
913 * nmalloc[i] is the difference between the number of mallocs and frees
914 * for a given block size.
916 static u_int nmalloc[NBUCKETS];
917 static u_int sbrk_slack;
918 static u_int start_slack;
919 #else /* !( defined DEBUGGING_MSTATS ) */
920 # define sbrk_slack 0
923 static u_int goodsbrk;
925 # ifdef PERL_EMERGENCY_SBRK
928 # define BIG_SIZE (1<<16) /* 64K */
931 static char *emergency_buffer;
932 static MEM_SIZE emergency_buffer_size;
933 static MEM_SIZE no_mem; /* 0 if the last request for more memory succeeded.
934 Otherwise the size of the failing request. */
937 emergency_sbrk(MEM_SIZE size)
939 MEM_SIZE rsize = (((size - 1)>>LOG_OF_MIN_ARENA) + 1)<<LOG_OF_MIN_ARENA;
941 if (size >= BIG_SIZE && (!no_mem || (size < no_mem))) {
942 /* Give the possibility to recover, but avoid an infinite cycle. */
945 croak2("Out of memory during \"large\" request for %"UVuf" bytes, total sbrk() is %"UVuf" bytes", (UV)size, (UV)(goodsbrk + sbrk_slack));
948 if (emergency_buffer_size >= rsize) {
949 char *old = emergency_buffer;
951 emergency_buffer_size -= rsize;
952 emergency_buffer += rsize;
956 /* First offense, give a possibility to recover by dieing. */
957 /* No malloc involved here: */
958 GV **gvp = (GV**)hv_fetch(PL_defstash, "^M", 2, 0);
964 if (emergency_buffer_size) {
965 add_to_chain(emergency_buffer, emergency_buffer_size, 0);
966 emergency_buffer_size = 0;
967 emergency_buffer = Nullch;
970 if (!gvp) gvp = (GV**)hv_fetch(PL_defstash, "\015", 1, 0);
971 if (!gvp || !(sv = GvSV(*gvp)) || !SvPOK(sv)
972 || (SvLEN(sv) < (1<<LOG_OF_MIN_ARENA) - M_OVERHEAD)) {
975 return (char *)-1; /* Now die die die... */
977 /* Got it, now detach SvPV: */
979 /* Check alignment: */
980 if ((PTR2UV(pv) - sizeof(union overhead)) & (NEEDED_ALIGNMENT - 1)) {
981 PerlIO_puts(PerlIO_stderr(),"Bad alignment of $^M!\n");
982 return (char *)-1; /* die die die */
985 emergency_buffer = pv - sizeof(union overhead);
986 emergency_buffer_size = malloced_size(pv) + M_OVERHEAD;
989 SvCUR(sv) = SvLEN(sv) = 0;
993 croak("Out of memory during request for %"UVuf" bytes, total sbrk() is %"UVuf" bytes", (UV)size, (UV)(goodsbrk + sbrk_slack));
998 # else /* !defined(PERL_EMERGENCY_SBRK) */
999 # define emergency_sbrk(size) -1
1001 #endif /* ifdef PERL_CORE */
1005 #define ASSERT(p,diag) if (!(p)) botch(diag,STRINGIFY(p)); else
1007 botch(char *diag, char *s)
1010 PerlIO_printf(PerlIO_stderr(), "assertion botched (%s?): %s\n", diag, s);
1014 #define ASSERT(p, diag)
1018 Perl_malloc(register size_t nbytes)
1020 register union overhead *p;
1021 register int bucket;
1022 register MEM_SIZE shiftr;
1024 #if defined(DEBUGGING) || defined(RCHECK)
1025 MEM_SIZE size = nbytes;
1028 BARK_64K_LIMIT("Allocation",nbytes,nbytes);
1030 if ((long)nbytes < 0)
1031 croak("%s", "panic: malloc");
1035 * Convert amount of memory requested into
1036 * closest block size stored in hash buckets
1037 * which satisfies request. Account for
1038 * space used per block for accounting.
1041 # ifdef SMALL_BUCKET_VIA_TABLE
1043 bucket = MIN_BUCKET;
1044 else if (nbytes <= SIZE_TABLE_MAX) {
1045 bucket = bucket_of[(nbytes - 1) >> BUCKET_TABLE_SHIFT];
1050 if (nbytes <= MAX_POW2_ALGO) goto do_shifts;
1055 POW2_OPTIMIZE_ADJUST(nbytes);
1056 nbytes += M_OVERHEAD;
1057 nbytes = (nbytes + 3) &~ 3;
1058 #if defined(PACK_MALLOC) && !defined(SMALL_BUCKET_VIA_TABLE)
1061 shiftr = (nbytes - 1) >> START_SHIFT;
1062 bucket = START_SHIFTS_BUCKET;
1063 /* apart from this loop, this is O(1) */
1064 while (shiftr >>= 1)
1065 bucket += BUCKETS_PER_POW2;
1069 * If nothing in hash bucket right now,
1070 * request more memory from the system.
1072 if (nextf[bucket] == NULL)
1074 if ((p = nextf[bucket]) == NULL) {
1080 #if defined(PLAIN_MALLOC) && defined(NO_FANCY_MALLOC)
1081 PerlIO_puts(PerlIO_stderr(),"Out of memory!\n");
1084 char *eb = buff + sizeof(buff) - 1;
1088 PerlIO_puts(PerlIO_stderr(),"Out of memory during request for ");
1089 #if defined(DEBUGGING) || defined(RCHECK)
1094 *--s = '0' + (n % 10);
1096 PerlIO_puts(PerlIO_stderr(),s);
1097 PerlIO_puts(PerlIO_stderr()," bytes, total sbrk() is ");
1099 n = goodsbrk + sbrk_slack;
1101 *--s = '0' + (n % 10);
1103 PerlIO_puts(PerlIO_stderr(),s);
1104 PerlIO_puts(PerlIO_stderr()," bytes!\n");
1105 #endif /* defined(PLAIN_MALLOC) && defined(NO_FANCY_MALLOC) */
1113 /* remove from linked list */
1115 if ((PTR2UV(p)) & (MEM_ALIGNBYTES - 1)) {
1117 PerlIO_printf(PerlIO_stderr(),
1118 "Unaligned pointer in the free chain 0x%"UVxf"\n",
1121 if ((PTR2UV(p->ov_next)) & (MEM_ALIGNBYTES - 1)) {
1123 PerlIO_printf(PerlIO_stderr(),
1124 "Unaligned `next' pointer in the free "
1125 "chain 0x%"UVxf" at 0x%"UVxf"\n",
1126 PTR2UV(p->ov_next), PTR2UV(p));
1129 nextf[bucket] = p->ov_next;
1133 DEBUG_m(PerlIO_printf(Perl_debug_log,
1134 "0x%"UVxf": (%05lu) malloc %ld bytes\n",
1135 PTR2UV((Malloc_t)(p + CHUNK_SHIFT)), (unsigned long)(PL_an++),
1138 #ifdef IGNORE_SMALL_BAD_FREE
1139 if (bucket >= FIRST_BUCKET_WITH_CHECK)
1141 OV_MAGIC(p, bucket) = MAGIC;
1143 OV_INDEX(p) = bucket;
1147 * Record allocated size of block and
1148 * bound space with magic numbers.
1150 p->ov_rmagic = RMAGIC;
1151 if (bucket <= MAX_SHORT_BUCKET) {
1154 nbytes = size + M_OVERHEAD;
1155 p->ov_size = nbytes - 1;
1156 if ((i = nbytes & 3)) {
1159 *((char *)((caddr_t)p + nbytes - RSLOP + i)) = RMAGIC_C;
1161 nbytes = (nbytes + 3) &~ 3;
1162 *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC;
1165 return ((Malloc_t)(p + CHUNK_SHIFT));
1168 static char *last_sbrk_top;
1169 static char *last_op; /* This arena can be easily extended. */
1170 static MEM_SIZE sbrked_remains;
1171 static int sbrk_good = SBRK_ALLOW_FAILURES * SBRK_FAILURE_PRICE;
1173 #ifdef DEBUGGING_MSTATS
1177 struct chunk_chain_s {
1178 struct chunk_chain_s *next;
1181 static struct chunk_chain_s *chunk_chain;
1182 static int n_chunks;
1183 static char max_bucket;
1185 /* Cutoff a piece of one of the chunks in the chain. Prefer smaller chunk. */
1187 get_from_chain(MEM_SIZE size)
1189 struct chunk_chain_s *elt = chunk_chain, **oldp = &chunk_chain;
1190 struct chunk_chain_s **oldgoodp = NULL;
1191 long min_remain = LONG_MAX;
1194 if (elt->size >= size) {
1195 long remains = elt->size - size;
1196 if (remains >= 0 && remains < min_remain) {
1198 min_remain = remains;
1204 oldp = &( elt->next );
1207 if (!oldgoodp) return NULL;
1209 void *ret = *oldgoodp;
1210 struct chunk_chain_s *next = (*oldgoodp)->next;
1212 *oldgoodp = (struct chunk_chain_s *)((char*)ret + size);
1213 (*oldgoodp)->size = min_remain;
1214 (*oldgoodp)->next = next;
1217 void *ret = *oldgoodp;
1218 *oldgoodp = (*oldgoodp)->next;
1225 add_to_chain(void *p, MEM_SIZE size, MEM_SIZE chip)
1227 struct chunk_chain_s *next = chunk_chain;
1228 char *cp = (char*)p;
1231 chunk_chain = (struct chunk_chain_s *)cp;
1232 chunk_chain->size = size - chip;
1233 chunk_chain->next = next;
1238 get_from_bigger_buckets(int bucket, MEM_SIZE size)
1241 static int bucketprice[NBUCKETS];
1242 while (bucket <= max_bucket) {
1243 /* We postpone stealing from bigger buckets until we want it
1245 if (nextf[bucket] && bucketprice[bucket]++ >= price) {
1247 void *ret = (void*)(nextf[bucket] - 1 + CHUNK_SHIFT);
1248 bucketprice[bucket] = 0;
1249 if (((char*)nextf[bucket]) - M_OVERHEAD == last_op) {
1250 last_op = NULL; /* Disable optimization */
1252 nextf[bucket] = nextf[bucket]->ov_next;
1253 #ifdef DEBUGGING_MSTATS
1255 start_slack -= M_OVERHEAD;
1257 add_to_chain(ret, (BUCKET_SIZE(bucket) +
1258 POW2_OPTIMIZE_SURPLUS(bucket)),
1267 static union overhead *
1268 getpages(MEM_SIZE needed, int *nblksp, int bucket)
1270 /* Need to do (possibly expensive) system call. Try to
1271 optimize it for rare calling. */
1272 MEM_SIZE require = needed - sbrked_remains;
1274 union overhead *ovp;
1277 if (sbrk_good > 0) {
1278 if (!last_sbrk_top && require < FIRST_SBRK)
1279 require = FIRST_SBRK;
1280 else if (require < MIN_SBRK) require = MIN_SBRK;
1282 if (require < goodsbrk * MIN_SBRK_FRAC / 100)
1283 require = goodsbrk * MIN_SBRK_FRAC / 100;
1284 require = ((require - 1 + MIN_SBRK) / MIN_SBRK) * MIN_SBRK;
1291 DEBUG_m(PerlIO_printf(Perl_debug_log,
1292 "sbrk(%ld) for %ld-byte-long arena\n",
1293 (long)require, (long) needed));
1294 cp = (char *)sbrk(require);
1295 #ifdef DEBUGGING_MSTATS
1298 if (cp == last_sbrk_top) {
1299 /* Common case, anything is fine. */
1301 ovp = (union overhead *) (cp - sbrked_remains);
1302 last_op = cp - sbrked_remains;
1303 sbrked_remains = require - (needed - sbrked_remains);
1304 } else if (cp == (char *)-1) { /* no more room! */
1305 ovp = (union overhead *)emergency_sbrk(needed);
1306 if (ovp == (union overhead *)-1)
1308 if (((char*)ovp) > last_op) { /* Cannot happen with current emergency_sbrk() */
1312 } else { /* Non-continuous or first sbrk(). */
1313 long add = sbrked_remains;
1316 if (sbrked_remains) { /* Put rest into chain, we
1317 cannot use it right now. */
1318 add_to_chain((void*)(last_sbrk_top - sbrked_remains),
1322 /* Second, check alignment. */
1325 #if !defined(atarist) && !defined(__MINT__) /* on the atari we dont have to worry about this */
1326 # ifndef I286 /* The sbrk(0) call on the I286 always returns the next segment */
1327 /* WANTED_ALIGNMENT may be more than NEEDED_ALIGNMENT, but this may
1328 improve performance of memory access. */
1329 if (PTR2UV(cp) & (WANTED_ALIGNMENT - 1)) { /* Not aligned. */
1330 slack = WANTED_ALIGNMENT - (PTR2UV(cp) & (WANTED_ALIGNMENT - 1));
1334 #endif /* !atarist && !MINT */
1337 DEBUG_m(PerlIO_printf(Perl_debug_log,
1338 "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",
1339 (long)add, (long) slack,
1340 (long) sbrked_remains));
1341 newcp = (char *)sbrk(add);
1342 #if defined(DEBUGGING_MSTATS)
1346 if (newcp != cp + require) {
1347 /* Too bad: even rounding sbrk() is not continuous.*/
1348 DEBUG_m(PerlIO_printf(Perl_debug_log,
1349 "failed to fix bad sbrk()\n"));
1353 fatalcroak("panic: Off-page sbrk\n");
1356 if (sbrked_remains) {
1358 #if defined(DEBUGGING_MSTATS)
1359 sbrk_slack += require;
1362 DEBUG_m(PerlIO_printf(Perl_debug_log,
1363 "straight sbrk(%ld)\n",
1365 cp = (char *)sbrk(require);
1366 #ifdef DEBUGGING_MSTATS
1369 if (cp == (char *)-1)
1372 sbrk_good = -1; /* Disable optimization!
1373 Continue with not-aligned... */
1376 require += sbrked_remains;
1380 if (last_sbrk_top) {
1381 sbrk_good -= SBRK_FAILURE_PRICE;
1384 ovp = (union overhead *) cp;
1386 * Round up to minimum allocation size boundary
1387 * and deduct from block count to reflect.
1390 # if NEEDED_ALIGNMENT > MEM_ALIGNBYTES
1391 if (PTR2UV(ovp) & (NEEDED_ALIGNMENT - 1))
1392 fatalcroak("Misalignment of sbrk()\n");
1395 #ifndef I286 /* Again, this should always be ok on an 80286 */
1396 if (PTR2UV(ovp) & (MEM_ALIGNBYTES - 1)) {
1397 DEBUG_m(PerlIO_printf(Perl_debug_log,
1398 "fixing sbrk(): %d bytes off machine alignement\n",
1399 (int)(PTR2UV(ovp) & (MEM_ALIGNBYTES - 1))));
1400 ovp = INT2PTR(union overhead *,(PTR2UV(ovp) + MEM_ALIGNBYTES) &
1401 (MEM_ALIGNBYTES - 1));
1403 # if defined(DEBUGGING_MSTATS)
1404 /* This is only approx. if TWO_POT_OPTIMIZE: */
1405 sbrk_slack += (1 << (bucket >> BUCKET_POW2_SHIFT));
1409 ; /* Finish `else' */
1410 sbrked_remains = require - needed;
1413 #if !defined(PLAIN_MALLOC) && !defined(NO_FANCY_MALLOC)
1416 last_sbrk_top = cp + require;
1417 #ifdef DEBUGGING_MSTATS
1418 goodsbrk += require;
1424 getpages_adjacent(MEM_SIZE require)
1426 if (require <= sbrked_remains) {
1427 sbrked_remains -= require;
1431 require -= sbrked_remains;
1432 /* We do not try to optimize sbrks here, we go for place. */
1433 cp = (char*) sbrk(require);
1434 #ifdef DEBUGGING_MSTATS
1436 goodsbrk += require;
1438 if (cp == last_sbrk_top) {
1440 last_sbrk_top = cp + require;
1442 if (cp == (char*)-1) { /* Out of memory */
1443 #ifdef DEBUGGING_MSTATS
1444 goodsbrk -= require;
1448 /* Report the failure: */
1450 add_to_chain((void*)(last_sbrk_top - sbrked_remains),
1452 add_to_chain((void*)cp, require, 0);
1453 sbrk_good -= SBRK_FAILURE_PRICE;
1465 * Allocate more memory to the indicated bucket.
1468 morecore(register int bucket)
1470 register union overhead *ovp;
1471 register int rnu; /* 2^rnu bytes will be requested */
1472 int nblks; /* become nblks blocks of the desired size */
1473 register MEM_SIZE siz, needed;
1477 if (bucket == sizeof(MEM_SIZE)*8*BUCKETS_PER_POW2) {
1479 croak("%s", "Out of memory during ridiculously large request");
1481 if (bucket > max_bucket)
1482 max_bucket = bucket;
1484 rnu = ( (bucket <= (LOG_OF_MIN_ARENA << BUCKET_POW2_SHIFT))
1486 : (bucket >> BUCKET_POW2_SHIFT) );
1487 /* This may be overwritten later: */
1488 nblks = 1 << (rnu - (bucket >> BUCKET_POW2_SHIFT)); /* how many blocks to get */
1489 needed = ((MEM_SIZE)1 << rnu) + POW2_OPTIMIZE_SURPLUS(bucket);
1490 if (nextf[rnu << BUCKET_POW2_SHIFT]) { /* 2048b bucket. */
1491 ovp = nextf[rnu << BUCKET_POW2_SHIFT] - 1 + CHUNK_SHIFT;
1492 nextf[rnu << BUCKET_POW2_SHIFT]
1493 = nextf[rnu << BUCKET_POW2_SHIFT]->ov_next;
1494 #ifdef DEBUGGING_MSTATS
1495 nmalloc[rnu << BUCKET_POW2_SHIFT]--;
1496 start_slack -= M_OVERHEAD;
1498 DEBUG_m(PerlIO_printf(Perl_debug_log,
1499 "stealing %ld bytes from %ld arena\n",
1500 (long) needed, (long) rnu << BUCKET_POW2_SHIFT));
1501 } else if (chunk_chain
1502 && (ovp = (union overhead*) get_from_chain(needed))) {
1503 DEBUG_m(PerlIO_printf(Perl_debug_log,
1504 "stealing %ld bytes from chain\n",
1506 } else if ( (ovp = (union overhead*)
1507 get_from_bigger_buckets((rnu << BUCKET_POW2_SHIFT) + 1,
1509 DEBUG_m(PerlIO_printf(Perl_debug_log,
1510 "stealing %ld bytes from bigger buckets\n",
1512 } else if (needed <= sbrked_remains) {
1513 ovp = (union overhead *)(last_sbrk_top - sbrked_remains);
1514 sbrked_remains -= needed;
1515 last_op = (char*)ovp;
1517 ovp = getpages(needed, &nblks, bucket);
1523 * Add new memory allocated to that on
1524 * free list for this hash bucket.
1526 siz = BUCKET_SIZE(bucket);
1528 *(u_char*)ovp = bucket; /* Fill index. */
1529 if (bucket <= MAX_PACKED) {
1530 ovp = (union overhead *) ((char*)ovp + BLK_SHIFT(bucket));
1531 nblks = N_BLKS(bucket);
1532 # ifdef DEBUGGING_MSTATS
1533 start_slack += BLK_SHIFT(bucket);
1535 } else if (bucket < LOG_OF_MIN_ARENA * BUCKETS_PER_POW2) {
1536 ovp = (union overhead *) ((char*)ovp + BLK_SHIFT(bucket));
1537 siz -= sizeof(union overhead);
1538 } else ovp++; /* One chunk per block. */
1539 #endif /* PACK_MALLOC */
1540 nextf[bucket] = ovp;
1541 #ifdef DEBUGGING_MSTATS
1542 nmalloc[bucket] += nblks;
1543 if (bucket > MAX_PACKED) {
1544 start_slack += M_OVERHEAD * nblks;
1547 while (--nblks > 0) {
1548 ovp->ov_next = (union overhead *)((caddr_t)ovp + siz);
1549 ovp = (union overhead *)((caddr_t)ovp + siz);
1551 /* Not all sbrks return zeroed memory.*/
1552 ovp->ov_next = (union overhead *)NULL;
1554 if (bucket == 7*BUCKETS_PER_POW2) { /* Special case, explanation is above. */
1555 union overhead *n_op = nextf[7*BUCKETS_PER_POW2]->ov_next;
1556 nextf[7*BUCKETS_PER_POW2] =
1557 (union overhead *)((caddr_t)nextf[7*BUCKETS_PER_POW2]
1558 - sizeof(union overhead));
1559 nextf[7*BUCKETS_PER_POW2]->ov_next = n_op;
1561 #endif /* !PACK_MALLOC */
1565 Perl_mfree(void *mp)
1567 register MEM_SIZE size;
1568 register union overhead *ovp;
1569 char *cp = (char*)mp;
1574 DEBUG_m(PerlIO_printf(Perl_debug_log,
1575 "0x%"UVxf": (%05lu) free\n",
1576 PTR2UV(cp), (unsigned long)(PL_an++)));
1580 ovp = (union overhead *)((caddr_t)cp
1581 - sizeof (union overhead) * CHUNK_SHIFT);
1583 bucket = OV_INDEX(ovp);
1585 #ifdef IGNORE_SMALL_BAD_FREE
1586 if ((bucket >= FIRST_BUCKET_WITH_CHECK)
1587 && (OV_MAGIC(ovp, bucket) != MAGIC))
1589 if (OV_MAGIC(ovp, bucket) != MAGIC)
1592 static int bad_free_warn = -1;
1593 if (bad_free_warn == -1) {
1595 char *pbf = PerlEnv_getenv("PERL_BADFREE");
1596 bad_free_warn = (pbf) ? atoi(pbf) : 1;
1604 if (!PERL_IS_ALIVE || !PL_curcop || ckWARN_d(WARN_MALLOC))
1605 Perl_warner(aTHX_ packWARN(WARN_MALLOC), "%s free() ignored (RMAGIC, PERL_CORE)",
1606 ovp->ov_rmagic == RMAGIC - 1 ?
1607 "Duplicate" : "Bad");
1610 warn("%s free() ignored (RMAGIC)",
1611 ovp->ov_rmagic == RMAGIC - 1 ? "Duplicate" : "Bad");
1617 if (!PERL_IS_ALIVE || !PL_curcop || ckWARN_d(WARN_MALLOC))
1618 Perl_warner(aTHX_ packWARN(WARN_MALLOC), "%s", "Bad free() ignored (PERL_CORE)");
1621 warn("%s", "Bad free() ignored");
1624 return; /* sanity */
1627 ASSERT(ovp->ov_rmagic == RMAGIC, "chunk's head overwrite");
1628 if (OV_INDEX(ovp) <= MAX_SHORT_BUCKET) {
1630 MEM_SIZE nbytes = ovp->ov_size + 1;
1632 if ((i = nbytes & 3)) {
1635 ASSERT(*((char *)((caddr_t)ovp + nbytes - RSLOP + i))
1636 == RMAGIC_C, "chunk's tail overwrite");
1639 nbytes = (nbytes + 3) &~ 3;
1640 ASSERT(*(u_int *)((caddr_t)ovp + nbytes - RSLOP) == RMAGIC, "chunk's tail overwrite");
1642 ovp->ov_rmagic = RMAGIC - 1;
1644 ASSERT(OV_INDEX(ovp) < NBUCKETS, "chunk's head overwrite");
1645 size = OV_INDEX(ovp);
1648 ovp->ov_next = nextf[size];
1653 /* There is no need to do any locking in realloc (with an exception of
1654 trying to grow in place if we are at the end of the chain).
1655 If somebody calls us from a different thread with the same address,
1656 we are sole anyway. */
1659 Perl_realloc(void *mp, size_t nbytes)
1661 register MEM_SIZE onb;
1662 union overhead *ovp;
1665 register int bucket;
1666 int incr; /* 1 if does not fit, -1 if "easily" fits in a
1667 smaller bucket, otherwise 0. */
1668 char *cp = (char*)mp;
1670 #if defined(DEBUGGING) || !defined(PERL_CORE)
1671 MEM_SIZE size = nbytes;
1673 if ((long)nbytes < 0)
1674 croak("%s", "panic: realloc");
1677 BARK_64K_LIMIT("Reallocation",nbytes,size);
1679 return Perl_malloc(nbytes);
1681 ovp = (union overhead *)((caddr_t)cp
1682 - sizeof (union overhead) * CHUNK_SHIFT);
1683 bucket = OV_INDEX(ovp);
1685 #ifdef IGNORE_SMALL_BAD_FREE
1686 if ((bucket >= FIRST_BUCKET_WITH_CHECK)
1687 && (OV_MAGIC(ovp, bucket) != MAGIC))
1689 if (OV_MAGIC(ovp, bucket) != MAGIC)
1692 static int bad_free_warn = -1;
1693 if (bad_free_warn == -1) {
1695 char *pbf = PerlEnv_getenv("PERL_BADFREE");
1696 bad_free_warn = (pbf) ? atoi(pbf) : 1;
1704 if (!PERL_IS_ALIVE || !PL_curcop || ckWARN_d(WARN_MALLOC))
1705 Perl_warner(aTHX_ packWARN(WARN_MALLOC), "%srealloc() %signored",
1706 (ovp->ov_rmagic == RMAGIC - 1 ? "" : "Bad "),
1707 ovp->ov_rmagic == RMAGIC - 1
1708 ? "of freed memory " : "");
1711 warn("%srealloc() %signored",
1712 (ovp->ov_rmagic == RMAGIC - 1 ? "" : "Bad "),
1713 ovp->ov_rmagic == RMAGIC - 1 ? "of freed memory " : "");
1719 if (!PERL_IS_ALIVE || !PL_curcop || ckWARN_d(WARN_MALLOC))
1720 Perl_warner(aTHX_ packWARN(WARN_MALLOC), "%s",
1721 "Bad realloc() ignored");
1724 warn("%s", "Bad realloc() ignored");
1727 return Nullch; /* sanity */
1730 onb = BUCKET_SIZE_REAL(bucket);
1732 * avoid the copy if same size block.
1733 * We are not agressive with boundary cases. Note that it might
1734 * (for a small number of cases) give false negative if
1735 * both new size and old one are in the bucket for
1736 * FIRST_BIG_POW2, but the new one is near the lower end.
1738 * We do not try to go to 1.5 times smaller bucket so far.
1740 if (nbytes > onb) incr = 1;
1742 #ifdef DO_NOT_TRY_HARDER_WHEN_SHRINKING
1743 if ( /* This is a little bit pessimal if PACK_MALLOC: */
1744 nbytes > ( (onb >> 1) - M_OVERHEAD )
1745 # ifdef TWO_POT_OPTIMIZE
1746 || (bucket == FIRST_BIG_POW2 && nbytes >= LAST_SMALL_BOUND )
1749 #else /* !DO_NOT_TRY_HARDER_WHEN_SHRINKING */
1750 prev_bucket = ( (bucket > MAX_PACKED + 1)
1751 ? bucket - BUCKETS_PER_POW2
1753 if (nbytes > BUCKET_SIZE_REAL(prev_bucket))
1754 #endif /* !DO_NOT_TRY_HARDER_WHEN_SHRINKING */
1758 #ifdef STRESS_REALLOC
1765 * Record new allocated size of block and
1766 * bound space with magic numbers.
1768 if (OV_INDEX(ovp) <= MAX_SHORT_BUCKET) {
1769 int i, nb = ovp->ov_size + 1;
1774 ASSERT(*((char *)((caddr_t)ovp + nb - RSLOP + i)) == RMAGIC_C, "chunk's tail overwrite");
1778 ASSERT(*(u_int *)((caddr_t)ovp + nb - RSLOP) == RMAGIC, "chunk's tail overwrite");
1780 * Convert amount of memory requested into
1781 * closest block size stored in hash buckets
1782 * which satisfies request. Account for
1783 * space used per block for accounting.
1785 nbytes += M_OVERHEAD;
1786 ovp->ov_size = nbytes - 1;
1787 if ((i = nbytes & 3)) {
1790 *((char *)((caddr_t)ovp + nbytes - RSLOP + i))
1793 nbytes = (nbytes + 3) &~ 3;
1794 *((u_int *)((caddr_t)ovp + nbytes - RSLOP)) = RMAGIC;
1798 DEBUG_m(PerlIO_printf(Perl_debug_log,
1799 "0x%"UVxf": (%05lu) realloc %ld bytes inplace\n",
1800 PTR2UV(res),(unsigned long)(PL_an++),
1802 } else if (incr == 1 && (cp - M_OVERHEAD == last_op)
1803 && (onb > (1 << LOG_OF_MIN_ARENA))) {
1804 MEM_SIZE require, newarena = nbytes, pow;
1807 POW2_OPTIMIZE_ADJUST(newarena);
1808 newarena = newarena + M_OVERHEAD;
1809 /* newarena = (newarena + 3) &~ 3; */
1810 shiftr = (newarena - 1) >> LOG_OF_MIN_ARENA;
1811 pow = LOG_OF_MIN_ARENA + 1;
1812 /* apart from this loop, this is O(1) */
1813 while (shiftr >>= 1)
1815 newarena = (1 << pow) + POW2_OPTIMIZE_SURPLUS(pow * BUCKETS_PER_POW2);
1816 require = newarena - onb - M_OVERHEAD;
1819 if (cp - M_OVERHEAD == last_op /* We *still* are the last chunk */
1820 && getpages_adjacent(require)) {
1821 #ifdef DEBUGGING_MSTATS
1823 nmalloc[pow * BUCKETS_PER_POW2]++;
1825 *(cp - M_OVERHEAD) = pow * BUCKETS_PER_POW2; /* Fill index. */
1834 DEBUG_m(PerlIO_printf(Perl_debug_log,
1835 "0x%"UVxf": (%05lu) realloc %ld bytes the hard way\n",
1836 PTR2UV(cp),(unsigned long)(PL_an++),
1838 if ((res = (char*)Perl_malloc(nbytes)) == NULL)
1840 if (cp != res) /* common optimization */
1841 Copy(cp, res, (MEM_SIZE)(nbytes<onb?nbytes:onb), char);
1844 return ((Malloc_t)res);
1848 Perl_calloc(register size_t elements, register size_t size)
1850 long sz = elements * size;
1851 Malloc_t p = Perl_malloc(sz);
1854 memset((void*)p, 0, sz);
1860 Perl_strdup(const char *s)
1862 MEM_SIZE l = strlen(s);
1863 char *s1 = (char *)Perl_malloc(l+1);
1865 Copy(s, s1, (MEM_SIZE)(l+1), char);
1871 Perl_putenv(char *a)
1873 /* Sometimes system's putenv conflicts with my_setenv() - this is system
1874 malloc vs Perl's free(). */
1881 while (*val && *val != '=')
1886 if (l < sizeof(buf))
1889 var = Perl_malloc(l + 1);
1890 Copy(a, var, l, char);
1892 my_setenv(var, val+1);
1900 Perl_malloced_size(void *p)
1902 union overhead *ovp = (union overhead *)
1903 ((caddr_t)p - sizeof (union overhead) * CHUNK_SHIFT);
1904 int bucket = OV_INDEX(ovp);
1906 /* The caller wants to have a complete control over the chunk,
1907 disable the memory checking inside the chunk. */
1908 if (bucket <= MAX_SHORT_BUCKET) {
1909 MEM_SIZE size = BUCKET_SIZE_REAL(bucket);
1910 ovp->ov_size = size + M_OVERHEAD - 1;
1911 *((u_int *)((caddr_t)ovp + size + M_OVERHEAD - RSLOP)) = RMAGIC;
1914 return BUCKET_SIZE_REAL(bucket);
1917 # ifdef BUCKETS_ROOT2
1918 # define MIN_EVEN_REPORT 6
1920 # define MIN_EVEN_REPORT MIN_BUCKET
1924 Perl_get_mstats(pTHX_ perl_mstats_t *buf, int buflen, int level)
1926 #ifdef DEBUGGING_MSTATS
1928 register union overhead *p;
1929 struct chunk_chain_s* nextchain;
1931 buf->topbucket = buf->topbucket_ev = buf->topbucket_odd
1932 = buf->totfree = buf->total = buf->total_chain = 0;
1934 buf->minbucket = MIN_BUCKET;
1936 for (i = MIN_BUCKET ; i < NBUCKETS; i++) {
1937 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
1941 buf->ntotal[i] = nmalloc[i];
1943 buf->totfree += j * BUCKET_SIZE_REAL(i);
1944 buf->total += nmalloc[i] * BUCKET_SIZE_REAL(i);
1946 i % 2 ? (buf->topbucket_odd = i) : (buf->topbucket_ev = i);
1950 nextchain = chunk_chain;
1952 buf->total_chain += nextchain->size;
1953 nextchain = nextchain->next;
1955 buf->total_sbrk = goodsbrk + sbrk_slack;
1957 buf->sbrk_good = sbrk_good;
1958 buf->sbrk_slack = sbrk_slack;
1959 buf->start_slack = start_slack;
1960 buf->sbrked_remains = sbrked_remains;
1962 buf->nbuckets = NBUCKETS;
1964 for (i = MIN_BUCKET ; i < NBUCKETS; i++) {
1967 buf->bucket_mem_size[i] = BUCKET_SIZE(i);
1968 buf->bucket_available_size[i] = BUCKET_SIZE_REAL(i);
1971 #endif /* defined DEBUGGING_MSTATS */
1972 return 0; /* XXX unused */
1975 * mstats - print out statistics about malloc
1977 * Prints two lines of numbers, one showing the length of the free list
1978 * for each size category, the second showing the number of mallocs -
1979 * frees for each size category.
1982 Perl_dump_mstats(pTHX_ char *s)
1984 #ifdef DEBUGGING_MSTATS
1986 perl_mstats_t buffer;
1992 get_mstats(&buffer, NBUCKETS, 0);
1995 PerlIO_printf(Perl_error_log,
1996 "Memory allocation statistics %s (buckets %"IVdf"(%"IVdf")..%"IVdf"(%"IVdf")\n",
1998 (IV)BUCKET_SIZE_REAL(MIN_BUCKET),
1999 (IV)BUCKET_SIZE(MIN_BUCKET),
2000 (IV)BUCKET_SIZE_REAL(buffer.topbucket),
2001 (IV)BUCKET_SIZE(buffer.topbucket));
2002 PerlIO_printf(Perl_error_log, "%8"IVdf" free:", buffer.totfree);
2003 for (i = MIN_EVEN_REPORT; i <= buffer.topbucket; i += BUCKETS_PER_POW2) {
2004 PerlIO_printf(Perl_error_log,
2005 ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
2007 : ((i < 12*BUCKETS_PER_POW2) ? " %3"UVuf : " %"UVuf)),
2010 #ifdef BUCKETS_ROOT2
2011 PerlIO_printf(Perl_error_log, "\n\t ");
2012 for (i = MIN_BUCKET + 1; i <= buffer.topbucket_odd; i += BUCKETS_PER_POW2) {
2013 PerlIO_printf(Perl_error_log,
2014 ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
2016 : ((i < 12*BUCKETS_PER_POW2) ? " %3"UVuf : " %"UVuf)),
2020 PerlIO_printf(Perl_error_log, "\n%8"IVdf" used:", buffer.total - buffer.totfree);
2021 for (i = MIN_EVEN_REPORT; i <= buffer.topbucket; i += BUCKETS_PER_POW2) {
2022 PerlIO_printf(Perl_error_log,
2023 ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
2025 : ((i < 12*BUCKETS_PER_POW2) ? " %3"IVdf : " %"IVdf)),
2026 buffer.ntotal[i] - buffer.nfree[i]);
2028 #ifdef BUCKETS_ROOT2
2029 PerlIO_printf(Perl_error_log, "\n\t ");
2030 for (i = MIN_BUCKET + 1; i <= buffer.topbucket_odd; i += BUCKETS_PER_POW2) {
2031 PerlIO_printf(Perl_error_log,
2032 ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
2034 : ((i < 12*BUCKETS_PER_POW2) ? " %3"IVdf : " %"IVdf)),
2035 buffer.ntotal[i] - buffer.nfree[i]);
2038 PerlIO_printf(Perl_error_log, "\nTotal sbrk(): %"IVdf"/%"IVdf":%"IVdf". Odd ends: pad+heads+chain+tail: %"IVdf"+%"IVdf"+%"IVdf"+%"IVdf".\n",
2039 buffer.total_sbrk, buffer.sbrks, buffer.sbrk_good,
2040 buffer.sbrk_slack, buffer.start_slack,
2041 buffer.total_chain, buffer.sbrked_remains);
2042 #endif /* DEBUGGING_MSTATS */
2046 #ifdef USE_PERL_SBRK
2048 # if defined(__MACHTEN_PPC__) || defined(NeXT) || defined(__NeXT__) || defined(PURIFY)
2049 # define PERL_SBRK_VIA_MALLOC
2052 # ifdef PERL_SBRK_VIA_MALLOC
2054 /* it may seem schizophrenic to use perl's malloc and let it call system */
2055 /* malloc, the reason for that is only the 3.2 version of the OS that had */
2056 /* frequent core dumps within nxzonefreenolock. This sbrk routine put an */
2057 /* end to the cores */
2059 # ifndef SYSTEM_ALLOC
2060 # define SYSTEM_ALLOC(a) malloc(a)
2062 # ifndef SYSTEM_ALLOC_ALIGNMENT
2063 # define SYSTEM_ALLOC_ALIGNMENT MEM_ALIGNBYTES
2066 # endif /* PERL_SBRK_VIA_MALLOC */
2068 static IV Perl_sbrk_oldchunk;
2069 static long Perl_sbrk_oldsize;
2071 # define PERLSBRK_32_K (1<<15)
2072 # define PERLSBRK_64_K (1<<16)
2080 if (!size) return 0;
2082 reqsize = size; /* just for the DEBUG_m statement */
2085 size = (size + 0x7ff) & ~0x7ff;
2087 if (size <= Perl_sbrk_oldsize) {
2088 got = Perl_sbrk_oldchunk;
2089 Perl_sbrk_oldchunk += size;
2090 Perl_sbrk_oldsize -= size;
2092 if (size >= PERLSBRK_32_K) {
2095 size = PERLSBRK_64_K;
2098 # if NEEDED_ALIGNMENT > SYSTEM_ALLOC_ALIGNMENT
2099 size += NEEDED_ALIGNMENT - SYSTEM_ALLOC_ALIGNMENT;
2101 got = (IV)SYSTEM_ALLOC(size);
2102 # if NEEDED_ALIGNMENT > SYSTEM_ALLOC_ALIGNMENT
2103 got = (got + NEEDED_ALIGNMENT - 1) & ~(NEEDED_ALIGNMENT - 1);
2106 /* Chunk is small, register the rest for future allocs. */
2107 Perl_sbrk_oldchunk = got + reqsize;
2108 Perl_sbrk_oldsize = size - reqsize;
2112 DEBUG_m(PerlIO_printf(Perl_debug_log, "sbrk malloc size %ld (reqsize %ld), left size %ld, give addr 0x%"UVxf"\n",
2113 size, reqsize, Perl_sbrk_oldsize, PTR2UV(got)));
2118 #endif /* ! defined USE_PERL_SBRK */