abort instead of just promising.
[p5sagit/p5-mst-13.2.git] / malloc.c
CommitLineData
a0d0e21e 1/* malloc.c
8d063cd8 2 *
8d063cd8 3 */
4
87c6202a 5/*
741df71a 6 Here are some notes on configuring Perl's malloc. (For non-perl
7 usage see below.)
87c6202a 8
9 There are two macros which serve as bulk disablers of advanced
10 features of this malloc: NO_FANCY_MALLOC, PLAIN_MALLOC (undef by
11 default). Look in the list of default values below to understand
12 their exact effect. Defining NO_FANCY_MALLOC returns malloc.c to the
13 state of the malloc in Perl 5.004. Additionally defining PLAIN_MALLOC
14 returns it to the state as of Perl 5.000.
15
16 Note that some of the settings below may be ignored in the code based
17 on values of other macros. The PERL_CORE symbol is only defined when
18 perl itself is being compiled (so malloc can make some assumptions
19 about perl's facilities being available to it).
20
21 Each config option has a short description, followed by its name,
22 default value, and a comment about the default (if applicable). Some
23 options take a precise value, while the others are just boolean.
24 The boolean ones are listed first.
25
26 # Enable code for an emergency memory pool in $^M. See perlvar.pod
27 # for a description of $^M.
28 PERL_EMERGENCY_SBRK (!PLAIN_MALLOC && PERL_CORE)
29
30 # Enable code for printing memory statistics.
31 DEBUGGING_MSTATS (!PLAIN_MALLOC && PERL_CORE)
32
33 # Move allocation info for small buckets into separate areas.
34 # Memory optimization (especially for small allocations, of the
35 # less than 64 bytes). Since perl usually makes a large number
36 # of small allocations, this is usually a win.
37 PACK_MALLOC (!PLAIN_MALLOC && !RCHECK)
38
39 # Add one page to big powers of two when calculating bucket size.
40 # This is targeted at big allocations, as are common in image
41 # processing.
42 TWO_POT_OPTIMIZE !PLAIN_MALLOC
43
44 # Use intermediate bucket sizes between powers-of-two. This is
45 # generally a memory optimization, and a (small) speed pessimization.
46 BUCKETS_ROOT2 !NO_FANCY_MALLOC
47
48 # Do not check small deallocations for bad free(). Memory
49 # and speed optimization, error reporting pessimization.
50 IGNORE_SMALL_BAD_FREE (!NO_FANCY_MALLOC && !RCHECK)
51
52 # Use table lookup to decide in which bucket a given allocation will go.
53 SMALL_BUCKET_VIA_TABLE !NO_FANCY_MALLOC
54
38ac2dc8 55 # Use a perl-defined sbrk() instead of the (presumably broken or
56 # missing) system-supplied sbrk().
57 USE_PERL_SBRK undef
58
59 # Use system malloc() (or calloc() etc.) to emulate sbrk(). Normally
60 # only used with broken sbrk()s.
87c6202a 61 PERL_SBRK_VIA_MALLOC undef
62
38ac2dc8 63 # Which allocator to use if PERL_SBRK_VIA_MALLOC
64 SYSTEM_ALLOC(a) malloc(a)
65
9ee81ef6 66 # Minimal alignment (in bytes, should be a power of 2) of SYSTEM_ALLOC
5bbd1ef5 67 SYSTEM_ALLOC_ALIGNMENT MEM_ALIGNBYTES
68
87c6202a 69 # Disable memory overwrite checking with DEBUGGING. Memory and speed
70 # optimization, error reporting pessimization.
71 NO_RCHECK undef
72
73 # Enable memory overwrite checking with DEBUGGING. Memory and speed
74 # pessimization, error reporting optimization
75 RCHECK (DEBUGGING && !NO_RCHECK)
76
77 # Failed allocations bigger than this size croak (if
78 # PERL_EMERGENCY_SBRK is enabled) without touching $^M. See
79 # perlvar.pod for a description of $^M.
80 BIG_SIZE (1<<16) # 64K
81
82 # Starting from this power of two, add an extra page to the
83 # size of the bucket. This enables optimized allocations of sizes
84 # close to powers of 2. Note that the value is indexed at 0.
85 FIRST_BIG_POW2 15 # 32K, 16K is used too often
86
87 # Estimate of minimal memory footprint. malloc uses this value to
88 # request the most reasonable largest blocks of memory from the system.
89 FIRST_SBRK (48*1024)
90
91 # Round up sbrk()s to multiples of this.
92 MIN_SBRK 2048
93
94 # Round up sbrk()s to multiples of this percent of footprint.
95 MIN_SBRK_FRAC 3
96
97 # Add this much memory to big powers of two to get the bucket size.
98 PERL_PAGESIZE 4096
99
100 # This many sbrk() discontinuities should be tolerated even
101 # from the start without deciding that sbrk() is usually
102 # discontinuous.
103 SBRK_ALLOW_FAILURES 3
104
105 # This many continuous sbrk()s compensate for one discontinuous one.
106 SBRK_FAILURE_PRICE 50
107
28ac10b1 108 # Some configurations may ask for 12-byte-or-so allocations which
109 # require 8-byte alignment (?!). In such situation one needs to
110 # define this to disable 12-byte bucket (will increase memory footprint)
111 STRICT_ALIGNMENT undef
112
87c6202a 113 This implementation assumes that calling PerlIO_printf() does not
114 result in any memory allocation calls (used during a panic).
115
116 */
117
741df71a 118/*
119 If used outside of Perl environment, it may be useful to redefine
120 the following macros (listed below with defaults):
121
122 # Type of address returned by allocation functions
123 Malloc_t void *
124
125 # Type of size argument for allocation functions
126 MEM_SIZE unsigned long
127
128 # Maximal value in LONG
129 LONG_MAX 0x7FFFFFFF
130
131 # Unsigned integer type big enough to keep a pointer
132 UV unsigned long
133
134 # Type of pointer with 1-byte granularity
135 caddr_t char *
136
137 # Type returned by free()
138 Free_t void
139
5bbd1ef5 140 # Very fatal condition reporting function (cannot call any )
141 fatalcroak(arg) write(2,arg,strlen(arg)) + exit(2)
142
741df71a 143 # Fatal error reporting function
144 croak(format, arg) warn(idem) + exit(1)
145
146 # Error reporting function
147 warn(format, arg) fprintf(stderr, idem)
148
149 # Locking/unlocking for MT operation
cea2e8a9 150 MALLOC_LOCK MUTEX_LOCK_NOCONTEXT(&PL_malloc_mutex)
151 MALLOC_UNLOCK MUTEX_UNLOCK_NOCONTEXT(&PL_malloc_mutex)
741df71a 152
153 # Locking/unlocking mutex for MT operation
154 MUTEX_LOCK(l) void
155 MUTEX_UNLOCK(l) void
156 */
157
e8bc2b5c 158#ifndef NO_FANCY_MALLOC
159# ifndef SMALL_BUCKET_VIA_TABLE
160# define SMALL_BUCKET_VIA_TABLE
161# endif
162# ifndef BUCKETS_ROOT2
163# define BUCKETS_ROOT2
164# endif
165# ifndef IGNORE_SMALL_BAD_FREE
166# define IGNORE_SMALL_BAD_FREE
167# endif
3562ef9b 168#endif
169
e8bc2b5c 170#ifndef PLAIN_MALLOC /* Bulk enable features */
171# ifndef PACK_MALLOC
172# define PACK_MALLOC
173# endif
174# ifndef TWO_POT_OPTIMIZE
175# define TWO_POT_OPTIMIZE
176# endif
d720c441 177# if defined(PERL_CORE) && !defined(PERL_EMERGENCY_SBRK)
178# define PERL_EMERGENCY_SBRK
e8bc2b5c 179# endif
180# if defined(PERL_CORE) && !defined(DEBUGGING_MSTATS)
181# define DEBUGGING_MSTATS
182# endif
183#endif
184
185#define MIN_BUC_POW2 (sizeof(void*) > 4 ? 3 : 2) /* Allow for 4-byte arena. */
186#define MIN_BUCKET (MIN_BUC_POW2 * BUCKETS_PER_POW2)
187
61ae2fbf 188#if !(defined(I286) || defined(atarist) || defined(__MINT__))
e8bc2b5c 189 /* take 2k unless the block is bigger than that */
190# define LOG_OF_MIN_ARENA 11
191#else
192 /* take 16k unless the block is bigger than that
193 (80286s like large segments!), probably good on the atari too */
194# define LOG_OF_MIN_ARENA 14
195#endif
196
8d063cd8 197#ifndef lint
1944739a 198# if defined(DEBUGGING) && !defined(NO_RCHECK)
199# define RCHECK
200# endif
e8bc2b5c 201# if defined(RCHECK) && defined(IGNORE_SMALL_BAD_FREE)
202# undef IGNORE_SMALL_BAD_FREE
203# endif
8d063cd8 204/*
205 * malloc.c (Caltech) 2/21/82
206 * Chris Kingsley, kingsley@cit-20.
207 *
208 * This is a very fast storage allocator. It allocates blocks of a small
209 * number of different sizes, and keeps free lists of each size. Blocks that
210 * don't exactly fit are passed up to the next larger size. In this
211 * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
cf5c4ad8 212 * If PACK_MALLOC is defined, small blocks are 2^n bytes long.
8d063cd8 213 * This is designed for use in a program that uses vast quantities of memory,
741df71a 214 * but bombs when it runs out.
215 *
4eb8286e 216 * Modifications Copyright Ilya Zakharevich 1996-99.
741df71a 217 *
218 * Still very quick, but much more thrifty. (Std config is 10% slower
219 * than it was, and takes 67% of old heap size for typical usage.)
220 *
221 * Allocations of small blocks are now table-driven to many different
222 * buckets. Sizes of really big buckets are increased to accomodata
223 * common size=power-of-2 blocks. Running-out-of-memory is made into
224 * an exception. Deeply configurable and thread-safe.
225 *
8d063cd8 226 */
227
d720c441 228#ifdef PERL_CORE
229# include "EXTERN.h"
4ad56ec9 230# define PERL_IN_MALLOC_C
d720c441 231# include "perl.h"
cea2e8a9 232# if defined(PERL_IMPLICIT_CONTEXT)
233# define croak Perl_croak_nocontext
234# define warn Perl_warn_nocontext
235# endif
d720c441 236#else
237# ifdef PERL_FOR_X2P
238# include "../EXTERN.h"
239# include "../perl.h"
240# else
241# include <stdlib.h>
242# include <stdio.h>
243# include <memory.h>
244# define _(arg) arg
245# ifndef Malloc_t
246# define Malloc_t void *
247# endif
248# ifndef MEM_SIZE
249# define MEM_SIZE unsigned long
250# endif
251# ifndef LONG_MAX
252# define LONG_MAX 0x7FFFFFFF
253# endif
254# ifndef UV
255# define UV unsigned long
256# endif
257# ifndef caddr_t
258# define caddr_t char *
259# endif
260# ifndef Free_t
261# define Free_t void
262# endif
263# define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
264# define PerlEnv_getenv getenv
265# define PerlIO_printf fprintf
266# define PerlIO_stderr() stderr
267# endif
e8bc2b5c 268# ifndef croak /* make depend */
741df71a 269# define croak(mess, arg) (warn((mess), (arg)), exit(1))
d720c441 270# endif
271# ifndef warn
741df71a 272# define warn(mess, arg) fprintf(stderr, (mess), (arg))
e8bc2b5c 273# endif
274# ifdef DEBUG_m
275# undef DEBUG_m
276# endif
277# define DEBUG_m(a)
278# ifdef DEBUGGING
279# undef DEBUGGING
280# endif
cea2e8a9 281# ifndef pTHX
282# define pTHX void
283# define pTHX_
284# define dTHX extern int Perl___notused
285# define WITH_THX(s) s
286# endif
c5be433b 287# ifndef PERL_GET_INTERP
288# define PERL_GET_INTERP PL_curinterp
289# endif
4ad56ec9 290# ifndef Perl_malloc
291# define Perl_malloc malloc
292# endif
293# ifndef Perl_mfree
294# define Perl_mfree free
295# endif
296# ifndef Perl_realloc
297# define Perl_realloc realloc
298# endif
299# ifndef Perl_calloc
300# define Perl_calloc calloc
301# endif
302# ifndef Perl_strdup
303# define Perl_strdup strdup
304# endif
e8bc2b5c 305#endif
306
307#ifndef MUTEX_LOCK
308# define MUTEX_LOCK(l)
309#endif
310
311#ifndef MUTEX_UNLOCK
312# define MUTEX_UNLOCK(l)
313#endif
314
741df71a 315#ifndef MALLOC_LOCK
cea2e8a9 316# define MALLOC_LOCK MUTEX_LOCK_NOCONTEXT(&PL_malloc_mutex)
741df71a 317#endif
318
319#ifndef MALLOC_UNLOCK
cea2e8a9 320# define MALLOC_UNLOCK MUTEX_UNLOCK_NOCONTEXT(&PL_malloc_mutex)
741df71a 321#endif
322
5bbd1ef5 323# ifndef fatalcroak /* make depend */
324# define fatalcroak(mess) (write(2, (mess), strlen(mess)), exit(2))
325# endif
326
760ac839 327#ifdef DEBUGGING
e8bc2b5c 328# undef DEBUG_m
0b250b9e 329# define DEBUG_m(a) \
330 STMT_START { \
331 if (PERL_GET_INTERP) { dTHX; if (PL_debug & 128) { a; } } \
332 } STMT_END
760ac839 333#endif
334
e9397286 335/*
336 * Layout of memory:
337 * ~~~~~~~~~~~~~~~~
338 * The memory is broken into "blocks" which occupy multiples of 2K (and
339 * generally speaking, have size "close" to a power of 2). The addresses
340 * of such *unused* blocks are kept in nextf[i] with big enough i. (nextf
341 * is an array of linked lists.) (Addresses of used blocks are not known.)
342 *
4ad56ec9 343 * Moreover, since the algorithm may try to "bite" smaller blocks out
e9397286 344 * of unused bigger ones, there are also regions of "irregular" size,
345 * managed separately, by a linked list chunk_chain.
346 *
347 * The third type of storage is the sbrk()ed-but-not-yet-used space, its
348 * end and size are kept in last_sbrk_top and sbrked_remains.
349 *
350 * Growing blocks "in place":
351 * ~~~~~~~~~~~~~~~~~~~~~~~~~
352 * The address of the block with the greatest address is kept in last_op
353 * (if not known, last_op is 0). If it is known that the memory above
354 * last_op is not continuous, or contains a chunk from chunk_chain,
355 * last_op is set to 0.
356 *
357 * The chunk with address last_op may be grown by expanding into
358 * sbrk()ed-but-not-yet-used space, or trying to sbrk() more continuous
359 * memory.
360 *
361 * Management of last_op:
362 * ~~~~~~~~~~~~~~~~~~~~~
363 *
364 * free() never changes the boundaries of blocks, so is not relevant.
365 *
366 * The only way realloc() may change the boundaries of blocks is if it
367 * grows a block "in place". However, in the case of success such a
368 * chunk is automatically last_op, and it remains last_op. In the case
369 * of failure getpages_adjacent() clears last_op.
370 *
371 * malloc() may change blocks by calling morecore() only.
372 *
373 * morecore() may create new blocks by:
374 * a) biting pieces from chunk_chain (cannot create one above last_op);
375 * b) biting a piece from an unused block (if block was last_op, this
376 * may create a chunk from chain above last_op, thus last_op is
377 * invalidated in such a case).
378 * c) biting of sbrk()ed-but-not-yet-used space. This creates
379 * a block which is last_op.
380 * d) Allocating new pages by calling getpages();
381 *
382 * getpages() creates a new block. It marks last_op at the bottom of
383 * the chunk of memory it returns.
384 *
385 * Active pages footprint:
386 * ~~~~~~~~~~~~~~~~~~~~~~
387 * Note that we do not need to traverse the lists in nextf[i], just take
388 * the first element of this list. However, we *need* to traverse the
389 * list in chunk_chain, but most the time it should be a very short one,
390 * so we do not step on a lot of pages we are not going to use.
391 *
392 * Flaws:
393 * ~~~~~
394 * get_from_bigger_buckets(): forget to increment price => Quite
395 * aggressive.
396 */
397
135863df 398/* I don't much care whether these are defined in sys/types.h--LAW */
399
400#define u_char unsigned char
401#define u_int unsigned int
56431972 402/*
403 * I removed the definition of u_bigint which appeared to be u_bigint = UV
404 * u_bigint was only used in TWOK_MASKED and TWOK_SHIFT
405 * where I have used PTR2UV. RMB
406 */
135863df 407#define u_short unsigned short
8d063cd8 408
cf5c4ad8 409/* 286 and atarist like big chunks, which gives too much overhead. */
61ae2fbf 410#if (defined(RCHECK) || defined(I286) || defined(atarist) || defined(__MINT__)) && defined(PACK_MALLOC)
e8bc2b5c 411# undef PACK_MALLOC
cf5c4ad8 412#endif
413
8d063cd8 414/*
cf5c4ad8 415 * The description below is applicable if PACK_MALLOC is not defined.
416 *
8d063cd8 417 * The overhead on a block is at least 4 bytes. When free, this space
418 * contains a pointer to the next free block, and the bottom two bits must
419 * be zero. When in use, the first byte is set to MAGIC, and the second
420 * byte is the size index. The remaining bytes are for alignment.
421 * If range checking is enabled and the size of the block fits
422 * in two bytes, then the top two bytes hold the size of the requested block
423 * plus the range checking words, and the header word MINUS ONE.
424 */
425union overhead {
426 union overhead *ov_next; /* when free */
85e6fe83 427#if MEM_ALIGNBYTES > 4
c623bd54 428 double strut; /* alignment problems */
a687059c 429#endif
8d063cd8 430 struct {
431 u_char ovu_magic; /* magic number */
432 u_char ovu_index; /* bucket # */
433#ifdef RCHECK
434 u_short ovu_size; /* actual block size */
435 u_int ovu_rmagic; /* range magic number */
436#endif
437 } ovu;
438#define ov_magic ovu.ovu_magic
439#define ov_index ovu.ovu_index
440#define ov_size ovu.ovu_size
441#define ov_rmagic ovu.ovu_rmagic
442};
443
444#define MAGIC 0xff /* magic # on accounting info */
445#define RMAGIC 0x55555555 /* magic # on range info */
e8bc2b5c 446#define RMAGIC_C 0x55 /* magic # on range info */
447
8d063cd8 448#ifdef RCHECK
c2a5c2d2 449# define RSLOP sizeof (u_int)
450# ifdef TWO_POT_OPTIMIZE
e8bc2b5c 451# define MAX_SHORT_BUCKET (12 * BUCKETS_PER_POW2)
c2a5c2d2 452# else
e8bc2b5c 453# define MAX_SHORT_BUCKET (13 * BUCKETS_PER_POW2)
c2a5c2d2 454# endif
8d063cd8 455#else
c2a5c2d2 456# define RSLOP 0
8d063cd8 457#endif
458
e8bc2b5c 459#if !defined(PACK_MALLOC) && defined(BUCKETS_ROOT2)
460# undef BUCKETS_ROOT2
461#endif
462
463#ifdef BUCKETS_ROOT2
464# define BUCKET_TABLE_SHIFT 2
465# define BUCKET_POW2_SHIFT 1
466# define BUCKETS_PER_POW2 2
467#else
468# define BUCKET_TABLE_SHIFT MIN_BUC_POW2
469# define BUCKET_POW2_SHIFT 0
470# define BUCKETS_PER_POW2 1
471#endif
472
274c7500 473#if !defined(MEM_ALIGNBYTES) || ((MEM_ALIGNBYTES > 4) && !defined(STRICT_ALIGNMENT))
474/* Figure out the alignment of void*. */
475struct aligner {
476 char c;
477 void *p;
478};
479# define ALIGN_SMALL ((int)((caddr_t)&(((struct aligner*)0)->p)))
480#else
481# define ALIGN_SMALL MEM_ALIGNBYTES
482#endif
483
484#define IF_ALIGN_8(yes,no) ((ALIGN_SMALL>4) ? (yes) : (no))
485
e8bc2b5c 486#ifdef BUCKETS_ROOT2
487# define MAX_BUCKET_BY_TABLE 13
488static u_short buck_size[MAX_BUCKET_BY_TABLE + 1] =
489 {
490 0, 0, 0, 0, 4, 4, 8, 12, 16, 24, 32, 48, 64, 80,
491 };
492# define BUCKET_SIZE(i) ((i) % 2 ? buck_size[i] : (1 << ((i) >> BUCKET_POW2_SHIFT)))
493# define BUCKET_SIZE_REAL(i) ((i) <= MAX_BUCKET_BY_TABLE \
494 ? buck_size[i] \
495 : ((1 << ((i) >> BUCKET_POW2_SHIFT)) \
496 - MEM_OVERHEAD(i) \
497 + POW2_OPTIMIZE_SURPLUS(i)))
498#else
499# define BUCKET_SIZE(i) (1 << ((i) >> BUCKET_POW2_SHIFT))
500# define BUCKET_SIZE_REAL(i) (BUCKET_SIZE(i) - MEM_OVERHEAD(i) + POW2_OPTIMIZE_SURPLUS(i))
501#endif
502
503
cf5c4ad8 504#ifdef PACK_MALLOC
4ad56ec9 505/* In this case there are several possible layout of arenas depending
506 * on the size. Arenas are of sizes multiple to 2K, 2K-aligned, and
507 * have a size close to a power of 2.
508 *
509 * Arenas of the size >= 4K keep one chunk only. Arenas of size 2K
510 * may keep one chunk or multiple chunks. Here are the possible
511 * layouts of arenas:
512 *
513 * # One chunk only, chunksize 2^k + SOMETHING - ALIGN, k >= 11
514 *
515 * INDEX MAGIC1 UNUSED CHUNK1
516 *
517 * # Multichunk with sanity checking and chunksize 2^k-ALIGN, k>7
518 *
519 * INDEX MAGIC1 MAGIC2 MAGIC3 UNUSED CHUNK1 CHUNK2 CHUNK3 ...
520 *
521 * # Multichunk with sanity checking and size 2^k-ALIGN, k=7
522 *
523 * INDEX MAGIC1 MAGIC2 MAGIC3 UNUSED CHUNK1 UNUSED CHUNK2 CHUNK3 ...
524 *
525 * # Multichunk with sanity checking and size up to 80
526 *
527 * INDEX UNUSED MAGIC1 UNUSED MAGIC2 UNUSED ... CHUNK1 CHUNK2 CHUNK3 ...
528 *
529 * # No sanity check (usually up to 48=byte-long buckets)
530 * INDEX UNUSED CHUNK1 CHUNK2 ...
531 *
532 * Above INDEX and MAGIC are one-byte-long. Sizes of UNUSED are
533 * appropriate to keep algorithms simple and memory aligned. INDEX
534 * encodes the size of the chunk, while MAGICn encodes state (used,
535 * free or non-managed-by-us-so-it-indicates-a-bug) of CHUNKn. MAGIC
536 * is used for sanity checking purposes only. SOMETHING is 0 or 4K
537 * (to make size of big CHUNK accomodate allocations for powers of two
538 * better).
539 *
540 * [There is no need to alignment between chunks, since C rules ensure
541 * that structs which need 2^k alignment have sizeof which is
542 * divisible by 2^k. Thus as far as the last chunk is aligned at the
543 * end of the arena, and 2K-alignment does not contradict things,
544 * everything is going to be OK for sizes of chunks 2^n and 2^n +
545 * 2^k. Say, 80-bit buckets will be 16-bit aligned, and as far as we
546 * put allocations for requests in 65..80 range, all is fine.
547 *
548 * Note, however, that standard malloc() puts more strict
549 * requirements than the above C rules. Moreover, our algorithms of
550 * realloc() may break this idyll, but we suppose that realloc() does
551 * need not change alignment.]
552 *
553 * Is very important to make calculation of the offset of MAGICm as
554 * quick as possible, since it is done on each malloc()/free(). In
555 * fact it is so quick that it has quite little effect on the speed of
556 * doing malloc()/free(). [By default] We forego such calculations
557 * for small chunks, but only to save extra 3% of memory, not because
558 * of speed considerations.
559 *
560 * Here is the algorithm [which is the same for all the allocations
561 * schemes above], see OV_MAGIC(block,bucket). Let OFFSETm be the
562 * offset of the CHUNKm from the start of ARENA. Then offset of
563 * MAGICm is (OFFSET1 >> SHIFT) + ADDOFFSET. Here SHIFT and ADDOFFSET
564 * are numbers which depend on the size of the chunks only.
565 *
566 * Let as check some sanity conditions. Numbers OFFSETm>>SHIFT are
567 * different for all the chunks in the arena if 2^SHIFT is not greater
568 * than size of the chunks in the arena. MAGIC1 will not overwrite
569 * INDEX provided ADDOFFSET is >0 if OFFSET1 < 2^SHIFT. MAGIClast
570 * will not overwrite CHUNK1 if OFFSET1 > (OFFSETlast >> SHIFT) +
571 * ADDOFFSET.
572 *
573 * Make SHIFT the maximal possible (there is no point in making it
574 * smaller). Since OFFSETlast is 2K - CHUNKSIZE, above restrictions
575 * give restrictions on OFFSET1 and on ADDOFFSET.
576 *
577 * In particular, for chunks of size 2^k with k>=6 we can put
578 * ADDOFFSET to be from 0 to 2^k - 2^(11-k), and have
579 * OFFSET1==chunksize. For chunks of size 80 OFFSET1 of 2K%80=48 is
580 * large enough to have ADDOFFSET between 1 and 16 (similarly for 96,
581 * when ADDOFFSET should be 1). In particular, keeping MAGICs for
582 * these sizes gives no additional size penalty.
583 *
584 * However, for chunks of size 2^k with k<=5 this gives OFFSET1 >=
585 * ADDOFSET + 2^(11-k). Keeping ADDOFFSET 0 allows for 2^(11-k)-2^(11-2k)
586 * chunks per arena. This is smaller than 2^(11-k) - 1 which are
587 * needed if no MAGIC is kept. [In fact, having a negative ADDOFFSET
588 * would allow for slightly more buckets per arena for k=2,3.]
589 *
590 * Similarly, for chunks of size 3/2*2^k with k<=5 MAGICs would span
591 * the area up to 2^(11-k)+ADDOFFSET. For k=4 this give optimal
592 * ADDOFFSET as -7..0. For k=3 ADDOFFSET can go up to 4 (with tiny
593 * savings for negative ADDOFFSET). For k=5 ADDOFFSET can go -1..16
594 * (with no savings for negative values).
cf5c4ad8 595 *
4ad56ec9 596 * In particular, keeping ADDOFFSET 0 for sizes of chunks up to 2^6
597 * leads to tiny pessimizations in case of sizes 4, 8, 12, 24, and
598 * leads to no contradictions except for size=80 (or 96.)
cf5c4ad8 599 *
4ad56ec9 600 * However, it also makes sense to keep no magic for sizes 48 or less.
601 * This is what we do. In this case one needs ADDOFFSET>=1 also for
602 * chunksizes 12, 24, and 48, unless one gets one less chunk per
603 * arena.
604 *
605 * The algo of OV_MAGIC(block,bucket) keeps ADDOFFSET 0 until
606 * chunksize of 64, then makes it 1.
cf5c4ad8 607 *
4ad56ec9 608 * This allows for an additional optimization: the above scheme leads
609 * to giant overheads for sizes 128 or more (one whole chunk needs to
610 * be sacrifised to keep INDEX). Instead we use chunks not of size
611 * 2^k, but of size 2^k-ALIGN. If we pack these chunks at the end of
612 * the arena, then the beginnings are still in different 2^k-long
613 * sections of the arena if k>=7 for ALIGN==4, and k>=8 if ALIGN=8.
614 * Thus for k>7 the above algo of calculating the offset of the magic
615 * will still give different answers for different chunks. And to
616 * avoid the overrun of MAGIC1 into INDEX, one needs ADDOFFSET of >=1.
617 * In the case k=7 we just move the first chunk an extra ALIGN
618 * backward inside the ARENA (this is done once per arena lifetime,
619 * thus is not a big overhead). */
e8bc2b5c 620# define MAX_PACKED_POW2 6
621# define MAX_PACKED (MAX_PACKED_POW2 * BUCKETS_PER_POW2 + BUCKET_POW2_SHIFT)
622# define MAX_POW2_ALGO ((1<<(MAX_PACKED_POW2 + 1)) - M_OVERHEAD)
623# define TWOK_MASK ((1<<LOG_OF_MIN_ARENA) - 1)
56431972 624# define TWOK_MASKED(x) (PTR2UV(x) & ~TWOK_MASK)
625# define TWOK_SHIFT(x) (PTR2UV(x) & TWOK_MASK)
626# define OV_INDEXp(block) (INT2PTR(u_char*,TWOK_MASKED(block)))
cf5c4ad8 627# define OV_INDEX(block) (*OV_INDEXp(block))
628# define OV_MAGIC(block,bucket) (*(OV_INDEXp(block) + \
e8bc2b5c 629 (TWOK_SHIFT(block)>> \
630 (bucket>>BUCKET_POW2_SHIFT)) + \
631 (bucket >= MIN_NEEDS_SHIFT ? 1 : 0)))
632 /* A bucket can have a shift smaller than it size, we need to
633 shift its magic number so it will not overwrite index: */
634# ifdef BUCKETS_ROOT2
635# define MIN_NEEDS_SHIFT (7*BUCKETS_PER_POW2 - 1) /* Shift 80 greater than chunk 64. */
636# else
637# define MIN_NEEDS_SHIFT (7*BUCKETS_PER_POW2) /* Shift 128 greater than chunk 32. */
638# endif
cf5c4ad8 639# define CHUNK_SHIFT 0
640
e8bc2b5c 641/* Number of active buckets of given ordinal. */
642#ifdef IGNORE_SMALL_BAD_FREE
643#define FIRST_BUCKET_WITH_CHECK (6 * BUCKETS_PER_POW2) /* 64 */
644# define N_BLKS(bucket) ( (bucket) < FIRST_BUCKET_WITH_CHECK \
645 ? ((1<<LOG_OF_MIN_ARENA) - 1)/BUCKET_SIZE(bucket) \
646 : n_blks[bucket] )
647#else
648# define N_BLKS(bucket) n_blks[bucket]
649#endif
650
651static u_short n_blks[LOG_OF_MIN_ARENA * BUCKETS_PER_POW2] =
652 {
653# if BUCKETS_PER_POW2==1
654 0, 0,
655 (MIN_BUC_POW2==2 ? 384 : 0),
656 224, 120, 62, 31, 16, 8, 4, 2
657# else
658 0, 0, 0, 0,
659 (MIN_BUC_POW2==2 ? 384 : 0), (MIN_BUC_POW2==2 ? 384 : 0), /* 4, 4 */
660 224, 149, 120, 80, 62, 41, 31, 25, 16, 16, 8, 8, 4, 4, 2, 2
661# endif
662 };
663
664/* Shift of the first bucket with the given ordinal inside 2K chunk. */
665#ifdef IGNORE_SMALL_BAD_FREE
666# define BLK_SHIFT(bucket) ( (bucket) < FIRST_BUCKET_WITH_CHECK \
667 ? ((1<<LOG_OF_MIN_ARENA) \
668 - BUCKET_SIZE(bucket) * N_BLKS(bucket)) \
669 : blk_shift[bucket])
670#else
671# define BLK_SHIFT(bucket) blk_shift[bucket]
672#endif
673
674static u_short blk_shift[LOG_OF_MIN_ARENA * BUCKETS_PER_POW2] =
675 {
676# if BUCKETS_PER_POW2==1
677 0, 0,
678 (MIN_BUC_POW2==2 ? 512 : 0),
679 256, 128, 64, 64, /* 8 to 64 */
680 16*sizeof(union overhead),
681 8*sizeof(union overhead),
682 4*sizeof(union overhead),
683 2*sizeof(union overhead),
684# else
685 0, 0, 0, 0,
686 (MIN_BUC_POW2==2 ? 512 : 0), (MIN_BUC_POW2==2 ? 512 : 0),
687 256, 260, 128, 128, 64, 80, 64, 48, /* 8 to 96 */
688 16*sizeof(union overhead), 16*sizeof(union overhead),
689 8*sizeof(union overhead), 8*sizeof(union overhead),
690 4*sizeof(union overhead), 4*sizeof(union overhead),
691 2*sizeof(union overhead), 2*sizeof(union overhead),
692# endif
693 };
cf5c4ad8 694
5bbd1ef5 695# define NEEDED_ALIGNMENT 0x800 /* 2k boundaries */
696# define WANTED_ALIGNMENT 0x800 /* 2k boundaries */
697
cf5c4ad8 698#else /* !PACK_MALLOC */
699
700# define OV_MAGIC(block,bucket) (block)->ov_magic
701# define OV_INDEX(block) (block)->ov_index
702# define CHUNK_SHIFT 1
e8bc2b5c 703# define MAX_PACKED -1
5bbd1ef5 704# define NEEDED_ALIGNMENT MEM_ALIGNBYTES
705# define WANTED_ALIGNMENT 0x400 /* 1k boundaries */
706
cf5c4ad8 707#endif /* !PACK_MALLOC */
708
e8bc2b5c 709#define M_OVERHEAD (sizeof(union overhead) + RSLOP)
710
711#ifdef PACK_MALLOC
712# define MEM_OVERHEAD(bucket) \
713 (bucket <= MAX_PACKED ? 0 : M_OVERHEAD)
714# ifdef SMALL_BUCKET_VIA_TABLE
715# define START_SHIFTS_BUCKET ((MAX_PACKED_POW2 + 1) * BUCKETS_PER_POW2)
716# define START_SHIFT MAX_PACKED_POW2
717# ifdef BUCKETS_ROOT2 /* Chunks of size 3*2^n. */
718# define SIZE_TABLE_MAX 80
719# else
720# define SIZE_TABLE_MAX 64
721# endif
722static char bucket_of[] =
723 {
724# ifdef BUCKETS_ROOT2 /* Chunks of size 3*2^n. */
725 /* 0 to 15 in 4-byte increments. */
726 (sizeof(void*) > 4 ? 6 : 5), /* 4/8, 5-th bucket for better reports */
727 6, /* 8 */
274c7500 728 IF_ALIGN_8(8,7), 8, /* 16/12, 16 */
e8bc2b5c 729 9, 9, 10, 10, /* 24, 32 */
730 11, 11, 11, 11, /* 48 */
731 12, 12, 12, 12, /* 64 */
732 13, 13, 13, 13, /* 80 */
733 13, 13, 13, 13 /* 80 */
734# else /* !BUCKETS_ROOT2 */
735 /* 0 to 15 in 4-byte increments. */
736 (sizeof(void*) > 4 ? 3 : 2),
737 3,
738 4, 4,
739 5, 5, 5, 5,
740 6, 6, 6, 6,
741 6, 6, 6, 6
742# endif /* !BUCKETS_ROOT2 */
743 };
744# else /* !SMALL_BUCKET_VIA_TABLE */
745# define START_SHIFTS_BUCKET MIN_BUCKET
746# define START_SHIFT (MIN_BUC_POW2 - 1)
747# endif /* !SMALL_BUCKET_VIA_TABLE */
748#else /* !PACK_MALLOC */
749# define MEM_OVERHEAD(bucket) M_OVERHEAD
750# ifdef SMALL_BUCKET_VIA_TABLE
751# undef SMALL_BUCKET_VIA_TABLE
752# endif
753# define START_SHIFTS_BUCKET MIN_BUCKET
754# define START_SHIFT (MIN_BUC_POW2 - 1)
755#endif /* !PACK_MALLOC */
cf5c4ad8 756
8d063cd8 757/*
55497cff 758 * Big allocations are often of the size 2^n bytes. To make them a
759 * little bit better, make blocks of size 2^n+pagesize for big n.
760 */
761
762#ifdef TWO_POT_OPTIMIZE
763
5f05dabc 764# ifndef PERL_PAGESIZE
765# define PERL_PAGESIZE 4096
766# endif
e8bc2b5c 767# ifndef FIRST_BIG_POW2
768# define FIRST_BIG_POW2 15 /* 32K, 16K is used too often. */
5f05dabc 769# endif
e8bc2b5c 770# define FIRST_BIG_BLOCK (1<<FIRST_BIG_POW2)
55497cff 771/* If this value or more, check against bigger blocks. */
772# define FIRST_BIG_BOUND (FIRST_BIG_BLOCK - M_OVERHEAD)
773/* If less than this value, goes into 2^n-overhead-block. */
774# define LAST_SMALL_BOUND ((FIRST_BIG_BLOCK>>1) - M_OVERHEAD)
775
e8bc2b5c 776# define POW2_OPTIMIZE_ADJUST(nbytes) \
777 ((nbytes >= FIRST_BIG_BOUND) ? nbytes -= PERL_PAGESIZE : 0)
778# define POW2_OPTIMIZE_SURPLUS(bucket) \
779 ((bucket >= FIRST_BIG_POW2 * BUCKETS_PER_POW2) ? PERL_PAGESIZE : 0)
780
781#else /* !TWO_POT_OPTIMIZE */
782# define POW2_OPTIMIZE_ADJUST(nbytes)
783# define POW2_OPTIMIZE_SURPLUS(bucket) 0
784#endif /* !TWO_POT_OPTIMIZE */
785
786#if defined(HAS_64K_LIMIT) && defined(PERL_CORE)
787# define BARK_64K_LIMIT(what,nbytes,size) \
788 if (nbytes > 0xffff) { \
789 PerlIO_printf(PerlIO_stderr(), \
790 "%s too large: %lx\n", what, size); \
791 my_exit(1); \
792 }
793#else /* !HAS_64K_LIMIT || !PERL_CORE */
794# define BARK_64K_LIMIT(what,nbytes,size)
795#endif /* !HAS_64K_LIMIT || !PERL_CORE */
55497cff 796
e8bc2b5c 797#ifndef MIN_SBRK
798# define MIN_SBRK 2048
799#endif
800
801#ifndef FIRST_SBRK
d720c441 802# define FIRST_SBRK (48*1024)
e8bc2b5c 803#endif
804
805/* Minimal sbrk in percents of what is already alloced. */
806#ifndef MIN_SBRK_FRAC
807# define MIN_SBRK_FRAC 3
808#endif
809
810#ifndef SBRK_ALLOW_FAILURES
811# define SBRK_ALLOW_FAILURES 3
812#endif
55497cff 813
e8bc2b5c 814#ifndef SBRK_FAILURE_PRICE
815# define SBRK_FAILURE_PRICE 50
55497cff 816#endif
817
e8bc2b5c 818#if defined(PERL_EMERGENCY_SBRK) && defined(PERL_CORE)
819
820# ifndef BIG_SIZE
821# define BIG_SIZE (1<<16) /* 64K */
822# endif
823
3541dd58 824#ifdef I_MACH_CTHREADS
772fe5b3 825# undef MUTEX_LOCK
826# define MUTEX_LOCK(m) STMT_START { if (*m) mutex_lock(*m); } STMT_END
827# undef MUTEX_UNLOCK
828# define MUTEX_UNLOCK(m) STMT_START { if (*m) mutex_unlock(*m); } STMT_END
3541dd58 829#endif
830
55497cff 831static char *emergency_buffer;
832static MEM_SIZE emergency_buffer_size;
833
cea2e8a9 834static int findbucket (union overhead *freep, int srchlen);
835static void morecore (register int bucket);
836# if defined(DEBUGGING)
837static void botch (char *diag, char *s);
838# endif
839static void add_to_chain (void *p, MEM_SIZE size, MEM_SIZE chip);
840static Malloc_t emergency_sbrk (MEM_SIZE size);
841static void* get_from_chain (MEM_SIZE size);
842static void* get_from_bigger_buckets(int bucket, MEM_SIZE size);
843static union overhead *getpages (int needed, int *nblksp, int bucket);
844static int getpages_adjacent(int require);
845
846static Malloc_t
847emergency_sbrk(MEM_SIZE size)
55497cff 848{
28ac10b1 849 MEM_SIZE rsize = (((size - 1)>>LOG_OF_MIN_ARENA) + 1)<<LOG_OF_MIN_ARENA;
850
55497cff 851 if (size >= BIG_SIZE) {
852 /* Give the possibility to recover: */
741df71a 853 MALLOC_UNLOCK;
1b979e0a 854 croak("Out of memory during \"large\" request for %i bytes", size);
55497cff 855 }
856
28ac10b1 857 if (emergency_buffer_size >= rsize) {
858 char *old = emergency_buffer;
859
860 emergency_buffer_size -= rsize;
861 emergency_buffer += rsize;
862 return old;
863 } else {
cea2e8a9 864 dTHX;
55497cff 865 /* First offense, give a possibility to recover by dieing. */
866 /* No malloc involved here: */
4a33f861 867 GV **gvp = (GV**)hv_fetch(PL_defstash, "^M", 2, 0);
55497cff 868 SV *sv;
869 char *pv;
28ac10b1 870 int have = 0;
2d8e6c8d 871 STRLEN n_a;
55497cff 872
28ac10b1 873 if (emergency_buffer_size) {
874 add_to_chain(emergency_buffer, emergency_buffer_size, 0);
875 emergency_buffer_size = 0;
876 emergency_buffer = Nullch;
877 have = 1;
878 }
4a33f861 879 if (!gvp) gvp = (GV**)hv_fetch(PL_defstash, "\015", 1, 0);
55497cff 880 if (!gvp || !(sv = GvSV(*gvp)) || !SvPOK(sv)
28ac10b1 881 || (SvLEN(sv) < (1<<LOG_OF_MIN_ARENA) - M_OVERHEAD)) {
882 if (have)
883 goto do_croak;
55497cff 884 return (char *)-1; /* Now die die die... */
28ac10b1 885 }
55497cff 886 /* Got it, now detach SvPV: */
2d8e6c8d 887 pv = SvPV(sv, n_a);
55497cff 888 /* Check alignment: */
56431972 889 if ((PTR2UV(pv) - sizeof(union overhead)) & (NEEDED_ALIGNMENT - 1)) {
55497cff 890 PerlIO_puts(PerlIO_stderr(),"Bad alignment of $^M!\n");
bbce6d69 891 return (char *)-1; /* die die die */
55497cff 892 }
893
28ac10b1 894 emergency_buffer = pv - sizeof(union overhead);
895 emergency_buffer_size = malloced_size(pv) + M_OVERHEAD;
55497cff 896 SvPOK_off(sv);
28ac10b1 897 SvPVX(sv) = Nullch;
898 SvCUR(sv) = SvLEN(sv) = 0;
55497cff 899 }
28ac10b1 900 do_croak:
741df71a 901 MALLOC_UNLOCK;
28ac10b1 902 croak("Out of memory during request for %i bytes", size);
ce70748c 903 /* NOTREACHED */
904 return Nullch;
55497cff 905}
906
e8bc2b5c 907#else /* !(defined(PERL_EMERGENCY_SBRK) && defined(PERL_CORE)) */
55497cff 908# define emergency_sbrk(size) -1
e8bc2b5c 909#endif /* !(defined(PERL_EMERGENCY_SBRK) && defined(PERL_CORE)) */
55497cff 910
911/*
e8bc2b5c 912 * nextf[i] is the pointer to the next free block of size 2^i. The
8d063cd8 913 * smallest allocatable block is 8 bytes. The overhead information
914 * precedes the data area returned to the user.
915 */
e8bc2b5c 916#define NBUCKETS (32*BUCKETS_PER_POW2 + 1)
8d063cd8 917static union overhead *nextf[NBUCKETS];
cf5c4ad8 918
e3663bad 919#if defined(PURIFY) && !defined(USE_PERL_SBRK)
920# define USE_PERL_SBRK
921#endif
922
cf5c4ad8 923#ifdef USE_PERL_SBRK
924#define sbrk(a) Perl_sbrk(a)
20ce7b12 925Malloc_t Perl_sbrk (int size);
8ac85365 926#else
927#ifdef DONT_DECLARE_STD
928#ifdef I_UNISTD
929#include <unistd.h>
930#endif
cf5c4ad8 931#else
52082926 932extern Malloc_t sbrk(int);
8ac85365 933#endif
cf5c4ad8 934#endif
8d063cd8 935
c07a80fd 936#ifdef DEBUGGING_MSTATS
8d063cd8 937/*
938 * nmalloc[i] is the difference between the number of mallocs and frees
939 * for a given block size.
940 */
941static u_int nmalloc[NBUCKETS];
5f05dabc 942static u_int sbrk_slack;
943static u_int start_slack;
8d063cd8 944#endif
945
e8bc2b5c 946static u_int goodsbrk;
947
760ac839 948#ifdef DEBUGGING
3541dd58 949#undef ASSERT
950#define ASSERT(p,diag) if (!(p)) botch(diag,STRINGIFY(p)); else
cea2e8a9 951static void
952botch(char *diag, char *s)
8d063cd8 953{
32e30700 954 dTHXo;
d720c441 955 PerlIO_printf(PerlIO_stderr(), "assertion botched (%s?): %s\n", diag, s);
3028581b 956 PerlProc_abort();
8d063cd8 957}
958#else
3541dd58 959#define ASSERT(p, diag)
8d063cd8 960#endif
961
2304df62 962Malloc_t
86058a2d 963Perl_malloc(register size_t nbytes)
8d063cd8 964{
965 register union overhead *p;
e8bc2b5c 966 register int bucket;
ee0007ab 967 register MEM_SIZE shiftr;
8d063cd8 968
c2a5c2d2 969#if defined(DEBUGGING) || defined(RCHECK)
ee0007ab 970 MEM_SIZE size = nbytes;
45d8adaa 971#endif
972
e8bc2b5c 973 BARK_64K_LIMIT("Allocation",nbytes,nbytes);
45d8adaa 974#ifdef DEBUGGING
975 if ((long)nbytes < 0)
cea2e8a9 976 croak("%s", "panic: malloc");
45d8adaa 977#endif
45d8adaa 978
8d063cd8 979 /*
980 * Convert amount of memory requested into
981 * closest block size stored in hash buckets
982 * which satisfies request. Account for
983 * space used per block for accounting.
984 */
cf5c4ad8 985#ifdef PACK_MALLOC
e8bc2b5c 986# ifdef SMALL_BUCKET_VIA_TABLE
987 if (nbytes == 0)
988 bucket = MIN_BUCKET;
989 else if (nbytes <= SIZE_TABLE_MAX) {
990 bucket = bucket_of[(nbytes - 1) >> BUCKET_TABLE_SHIFT];
991 } else
992# else
043bf814 993 if (nbytes == 0)
994 nbytes = 1;
e8bc2b5c 995 if (nbytes <= MAX_POW2_ALGO) goto do_shifts;
996 else
997# endif
55497cff 998#endif
e8bc2b5c 999 {
1000 POW2_OPTIMIZE_ADJUST(nbytes);
1001 nbytes += M_OVERHEAD;
1002 nbytes = (nbytes + 3) &~ 3;
1003 do_shifts:
1004 shiftr = (nbytes - 1) >> START_SHIFT;
1005 bucket = START_SHIFTS_BUCKET;
1006 /* apart from this loop, this is O(1) */
1007 while (shiftr >>= 1)
1008 bucket += BUCKETS_PER_POW2;
cf5c4ad8 1009 }
4ad56ec9 1010 MALLOC_LOCK;
8d063cd8 1011 /*
1012 * If nothing in hash bucket right now,
1013 * request more memory from the system.
1014 */
1015 if (nextf[bucket] == NULL)
1016 morecore(bucket);
e8bc2b5c 1017 if ((p = nextf[bucket]) == NULL) {
741df71a 1018 MALLOC_UNLOCK;
55497cff 1019#ifdef PERL_CORE
0b250b9e 1020 {
1021 dTHX;
1022 if (!PL_nomemok) {
1023 PerlIO_puts(PerlIO_stderr(),"Out of memory!\n");
1024 my_exit(1);
1025 }
ee0007ab 1026 }
45d8adaa 1027#endif
4ad56ec9 1028 return (NULL);
45d8adaa 1029 }
1030
e8bc2b5c 1031 DEBUG_m(PerlIO_printf(Perl_debug_log,
b900a521 1032 "0x%"UVxf": (%05lu) malloc %ld bytes\n",
1033 PTR2UV(p+1), (unsigned long)(PL_an++),
e8bc2b5c 1034 (long)size));
45d8adaa 1035
8d063cd8 1036 /* remove from linked list */
802004fa 1037#if defined(RCHECK)
32e30700 1038 if ((PTR2UV(p)) & (MEM_ALIGNBYTES - 1)) {
1039 dTHXo;
b900a521 1040 PerlIO_printf(PerlIO_stderr(),
7fa2f4f1 1041 "Unaligned pointer in the free chain 0x%"UVxf"\n",
1042 PTR2UV(p));
1043 }
1044 if ((PTR2UV(p->ov_next)) & (MEM_ALIGNBYTES - 1)) {
1045 dTHXo;
1046 PerlIO_printf(PerlIO_stderr(),
1047 "Unaligned `next' pointer in the free "
1048 "chain 0x"UVxf" at 0x%"UVxf"\n",
1049 PTR2UV(p->ov_next), PTR2UV(p));
32e30700 1050 }
bf38876a 1051#endif
1052 nextf[bucket] = p->ov_next;
4ad56ec9 1053
1054 MALLOC_UNLOCK;
1055
e8bc2b5c 1056#ifdef IGNORE_SMALL_BAD_FREE
1057 if (bucket >= FIRST_BUCKET_WITH_CHECK)
1058#endif
1059 OV_MAGIC(p, bucket) = MAGIC;
cf5c4ad8 1060#ifndef PACK_MALLOC
1061 OV_INDEX(p) = bucket;
1062#endif
8d063cd8 1063#ifdef RCHECK
1064 /*
1065 * Record allocated size of block and
1066 * bound space with magic numbers.
1067 */
8d063cd8 1068 p->ov_rmagic = RMAGIC;
e8bc2b5c 1069 if (bucket <= MAX_SHORT_BUCKET) {
1070 int i;
1071
1072 nbytes = size + M_OVERHEAD;
1073 p->ov_size = nbytes - 1;
1074 if ((i = nbytes & 3)) {
1075 i = 4 - i;
1076 while (i--)
1077 *((char *)((caddr_t)p + nbytes - RSLOP + i)) = RMAGIC_C;
1078 }
1079 nbytes = (nbytes + 3) &~ 3;
1080 *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC;
1081 }
8d063cd8 1082#endif
cf5c4ad8 1083 return ((Malloc_t)(p + CHUNK_SHIFT));
8d063cd8 1084}
1085
e8bc2b5c 1086static char *last_sbrk_top;
1087static char *last_op; /* This arena can be easily extended. */
1088static int sbrked_remains;
1089static int sbrk_good = SBRK_ALLOW_FAILURES * SBRK_FAILURE_PRICE;
1090
1091#ifdef DEBUGGING_MSTATS
1092static int sbrks;
1093#endif
1094
1095struct chunk_chain_s {
1096 struct chunk_chain_s *next;
1097 MEM_SIZE size;
1098};
1099static struct chunk_chain_s *chunk_chain;
1100static int n_chunks;
1101static char max_bucket;
1102
1103/* Cutoff a piece of one of the chunks in the chain. Prefer smaller chunk. */
cea2e8a9 1104static void *
1105get_from_chain(MEM_SIZE size)
e8bc2b5c 1106{
1107 struct chunk_chain_s *elt = chunk_chain, **oldp = &chunk_chain;
1108 struct chunk_chain_s **oldgoodp = NULL;
1109 long min_remain = LONG_MAX;
1110
1111 while (elt) {
1112 if (elt->size >= size) {
1113 long remains = elt->size - size;
1114 if (remains >= 0 && remains < min_remain) {
1115 oldgoodp = oldp;
1116 min_remain = remains;
1117 }
1118 if (remains == 0) {
1119 break;
1120 }
1121 }
1122 oldp = &( elt->next );
1123 elt = elt->next;
1124 }
1125 if (!oldgoodp) return NULL;
1126 if (min_remain) {
1127 void *ret = *oldgoodp;
1128 struct chunk_chain_s *next = (*oldgoodp)->next;
1129
1130 *oldgoodp = (struct chunk_chain_s *)((char*)ret + size);
1131 (*oldgoodp)->size = min_remain;
1132 (*oldgoodp)->next = next;
1133 return ret;
1134 } else {
1135 void *ret = *oldgoodp;
1136 *oldgoodp = (*oldgoodp)->next;
1137 n_chunks--;
1138 return ret;
1139 }
1140}
1141
cea2e8a9 1142static void
1143add_to_chain(void *p, MEM_SIZE size, MEM_SIZE chip)
e8bc2b5c 1144{
1145 struct chunk_chain_s *next = chunk_chain;
1146 char *cp = (char*)p;
1147
1148 cp += chip;
1149 chunk_chain = (struct chunk_chain_s *)cp;
1150 chunk_chain->size = size - chip;
1151 chunk_chain->next = next;
1152 n_chunks++;
1153}
1154
cea2e8a9 1155static void *
1156get_from_bigger_buckets(int bucket, MEM_SIZE size)
e8bc2b5c 1157{
1158 int price = 1;
1159 static int bucketprice[NBUCKETS];
1160 while (bucket <= max_bucket) {
1161 /* We postpone stealing from bigger buckets until we want it
1162 often enough. */
1163 if (nextf[bucket] && bucketprice[bucket]++ >= price) {
1164 /* Steal it! */
1165 void *ret = (void*)(nextf[bucket] - 1 + CHUNK_SHIFT);
1166 bucketprice[bucket] = 0;
1167 if (((char*)nextf[bucket]) - M_OVERHEAD == last_op) {
1168 last_op = NULL; /* Disable optimization */
1169 }
1170 nextf[bucket] = nextf[bucket]->ov_next;
1171#ifdef DEBUGGING_MSTATS
1172 nmalloc[bucket]--;
1173 start_slack -= M_OVERHEAD;
1174#endif
1175 add_to_chain(ret, (BUCKET_SIZE(bucket) +
1176 POW2_OPTIMIZE_SURPLUS(bucket)),
1177 size);
1178 return ret;
1179 }
1180 bucket++;
1181 }
1182 return NULL;
1183}
1184
cea2e8a9 1185static union overhead *
1186getpages(int needed, int *nblksp, int bucket)
fa423c5b 1187{
1188 /* Need to do (possibly expensive) system call. Try to
1189 optimize it for rare calling. */
1190 MEM_SIZE require = needed - sbrked_remains;
1191 char *cp;
1192 union overhead *ovp;
1193 int slack = 0;
1194
1195 if (sbrk_good > 0) {
1196 if (!last_sbrk_top && require < FIRST_SBRK)
1197 require = FIRST_SBRK;
1198 else if (require < MIN_SBRK) require = MIN_SBRK;
1199
1200 if (require < goodsbrk * MIN_SBRK_FRAC / 100)
1201 require = goodsbrk * MIN_SBRK_FRAC / 100;
1202 require = ((require - 1 + MIN_SBRK) / MIN_SBRK) * MIN_SBRK;
1203 } else {
1204 require = needed;
1205 last_sbrk_top = 0;
1206 sbrked_remains = 0;
1207 }
1208
1209 DEBUG_m(PerlIO_printf(Perl_debug_log,
1210 "sbrk(%ld) for %ld-byte-long arena\n",
1211 (long)require, (long) needed));
1212 cp = (char *)sbrk(require);
1213#ifdef DEBUGGING_MSTATS
1214 sbrks++;
1215#endif
1216 if (cp == last_sbrk_top) {
1217 /* Common case, anything is fine. */
1218 sbrk_good++;
1219 ovp = (union overhead *) (cp - sbrked_remains);
e9397286 1220 last_op = cp - sbrked_remains;
fa423c5b 1221 sbrked_remains = require - (needed - sbrked_remains);
1222 } else if (cp == (char *)-1) { /* no more room! */
1223 ovp = (union overhead *)emergency_sbrk(needed);
1224 if (ovp == (union overhead *)-1)
1225 return 0;
e9397286 1226 if (((char*)ovp) > last_op) { /* Cannot happen with current emergency_sbrk() */
1227 last_op = 0;
1228 }
fa423c5b 1229 return ovp;
1230 } else { /* Non-continuous or first sbrk(). */
1231 long add = sbrked_remains;
1232 char *newcp;
1233
1234 if (sbrked_remains) { /* Put rest into chain, we
1235 cannot use it right now. */
1236 add_to_chain((void*)(last_sbrk_top - sbrked_remains),
1237 sbrked_remains, 0);
1238 }
1239
1240 /* Second, check alignment. */
1241 slack = 0;
1242
61ae2fbf 1243#if !defined(atarist) && !defined(__MINT__) /* on the atari we dont have to worry about this */
fa423c5b 1244# ifndef I286 /* The sbrk(0) call on the I286 always returns the next segment */
5bbd1ef5 1245 /* WANTED_ALIGNMENT may be more than NEEDED_ALIGNMENT, but this may
1246 improve performance of memory access. */
56431972 1247 if (PTR2UV(cp) & (WANTED_ALIGNMENT - 1)) { /* Not aligned. */
1248 slack = WANTED_ALIGNMENT - (PTR2UV(cp) & (WANTED_ALIGNMENT - 1));
fa423c5b 1249 add += slack;
1250 }
1251# endif
61ae2fbf 1252#endif /* !atarist && !MINT */
fa423c5b 1253
1254 if (add) {
1255 DEBUG_m(PerlIO_printf(Perl_debug_log,
1256 "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",
1257 (long)add, (long) slack,
1258 (long) sbrked_remains));
1259 newcp = (char *)sbrk(add);
1260#if defined(DEBUGGING_MSTATS)
1261 sbrks++;
1262 sbrk_slack += add;
1263#endif
1264 if (newcp != cp + require) {
1265 /* Too bad: even rounding sbrk() is not continuous.*/
1266 DEBUG_m(PerlIO_printf(Perl_debug_log,
1267 "failed to fix bad sbrk()\n"));
1268#ifdef PACK_MALLOC
1269 if (slack) {
741df71a 1270 MALLOC_UNLOCK;
5bbd1ef5 1271 fatalcroak("panic: Off-page sbrk\n");
fa423c5b 1272 }
1273#endif
1274 if (sbrked_remains) {
1275 /* Try again. */
1276#if defined(DEBUGGING_MSTATS)
1277 sbrk_slack += require;
1278#endif
1279 require = needed;
1280 DEBUG_m(PerlIO_printf(Perl_debug_log,
1281 "straight sbrk(%ld)\n",
1282 (long)require));
1283 cp = (char *)sbrk(require);
1284#ifdef DEBUGGING_MSTATS
1285 sbrks++;
1286#endif
1287 if (cp == (char *)-1)
1288 return 0;
1289 }
1290 sbrk_good = -1; /* Disable optimization!
1291 Continue with not-aligned... */
1292 } else {
1293 cp += slack;
1294 require += sbrked_remains;
1295 }
1296 }
1297
1298 if (last_sbrk_top) {
1299 sbrk_good -= SBRK_FAILURE_PRICE;
1300 }
1301
1302 ovp = (union overhead *) cp;
1303 /*
1304 * Round up to minimum allocation size boundary
1305 * and deduct from block count to reflect.
1306 */
1307
5bbd1ef5 1308# if NEEDED_ALIGNMENT > MEM_ALIGNBYTES
56431972 1309 if (PTR2UV(ovp) & (NEEDED_ALIGNMENT - 1))
5bbd1ef5 1310 fatalcroak("Misalignment of sbrk()\n");
1311 else
1312# endif
fa423c5b 1313#ifndef I286 /* Again, this should always be ok on an 80286 */
56431972 1314 if (PTR2UV(ovp) & (MEM_ALIGNBYTES - 1)) {
fa423c5b 1315 DEBUG_m(PerlIO_printf(Perl_debug_log,
1316 "fixing sbrk(): %d bytes off machine alignement\n",
56431972 1317 (int)(PTR2UV(ovp) & (MEM_ALIGNBYTES - 1))));
1318 ovp = INT2PTR(union overhead *,(PTR2UV(ovp) + MEM_ALIGNBYTES) &
5bbd1ef5 1319 (MEM_ALIGNBYTES - 1));
fa423c5b 1320 (*nblksp)--;
1321# if defined(DEBUGGING_MSTATS)
1322 /* This is only approx. if TWO_POT_OPTIMIZE: */
5bbd1ef5 1323 sbrk_slack += (1 << (bucket >> BUCKET_POW2_SHIFT));
fa423c5b 1324# endif
1325 }
1326#endif
5bbd1ef5 1327 ; /* Finish `else' */
fa423c5b 1328 sbrked_remains = require - needed;
e9397286 1329 last_op = cp;
fa423c5b 1330 }
1331 last_sbrk_top = cp + require;
fa423c5b 1332#ifdef DEBUGGING_MSTATS
1333 goodsbrk += require;
1334#endif
1335 return ovp;
1336}
1337
cea2e8a9 1338static int
1339getpages_adjacent(int require)
fa423c5b 1340{
1341 if (require <= sbrked_remains) {
1342 sbrked_remains -= require;
1343 } else {
1344 char *cp;
1345
1346 require -= sbrked_remains;
1347 /* We do not try to optimize sbrks here, we go for place. */
1348 cp = (char*) sbrk(require);
1349#ifdef DEBUGGING_MSTATS
1350 sbrks++;
1351 goodsbrk += require;
1352#endif
1353 if (cp == last_sbrk_top) {
1354 sbrked_remains = 0;
1355 last_sbrk_top = cp + require;
1356 } else {
28ac10b1 1357 if (cp == (char*)-1) { /* Out of memory */
1358#ifdef DEBUGGING_MSTATS
1359 goodsbrk -= require;
1360#endif
1361 return 0;
1362 }
fa423c5b 1363 /* Report the failure: */
1364 if (sbrked_remains)
1365 add_to_chain((void*)(last_sbrk_top - sbrked_remains),
1366 sbrked_remains, 0);
1367 add_to_chain((void*)cp, require, 0);
1368 sbrk_good -= SBRK_FAILURE_PRICE;
1369 sbrked_remains = 0;
1370 last_sbrk_top = 0;
1371 last_op = 0;
1372 return 0;
1373 }
1374 }
1375
1376 return 1;
1377}
1378
8d063cd8 1379/*
1380 * Allocate more memory to the indicated bucket.
1381 */
cea2e8a9 1382static void
1383morecore(register int bucket)
8d063cd8 1384{
72aaf631 1385 register union overhead *ovp;
8d063cd8 1386 register int rnu; /* 2^rnu bytes will be requested */
fa423c5b 1387 int nblks; /* become nblks blocks of the desired size */
bbce6d69 1388 register MEM_SIZE siz, needed;
8d063cd8 1389
1390 if (nextf[bucket])
1391 return;
e8bc2b5c 1392 if (bucket == sizeof(MEM_SIZE)*8*BUCKETS_PER_POW2) {
741df71a 1393 MALLOC_UNLOCK;
d720c441 1394 croak("%s", "Out of memory during ridiculously large request");
55497cff 1395 }
d720c441 1396 if (bucket > max_bucket)
e8bc2b5c 1397 max_bucket = bucket;
d720c441 1398
e8bc2b5c 1399 rnu = ( (bucket <= (LOG_OF_MIN_ARENA << BUCKET_POW2_SHIFT))
1400 ? LOG_OF_MIN_ARENA
1401 : (bucket >> BUCKET_POW2_SHIFT) );
1402 /* This may be overwritten later: */
1403 nblks = 1 << (rnu - (bucket >> BUCKET_POW2_SHIFT)); /* how many blocks to get */
1404 needed = ((MEM_SIZE)1 << rnu) + POW2_OPTIMIZE_SURPLUS(bucket);
1405 if (nextf[rnu << BUCKET_POW2_SHIFT]) { /* 2048b bucket. */
1406 ovp = nextf[rnu << BUCKET_POW2_SHIFT] - 1 + CHUNK_SHIFT;
1407 nextf[rnu << BUCKET_POW2_SHIFT]
1408 = nextf[rnu << BUCKET_POW2_SHIFT]->ov_next;
1409#ifdef DEBUGGING_MSTATS
1410 nmalloc[rnu << BUCKET_POW2_SHIFT]--;
1411 start_slack -= M_OVERHEAD;
1412#endif
1413 DEBUG_m(PerlIO_printf(Perl_debug_log,
1414 "stealing %ld bytes from %ld arena\n",
1415 (long) needed, (long) rnu << BUCKET_POW2_SHIFT));
1416 } else if (chunk_chain
1417 && (ovp = (union overhead*) get_from_chain(needed))) {
1418 DEBUG_m(PerlIO_printf(Perl_debug_log,
1419 "stealing %ld bytes from chain\n",
1420 (long) needed));
d720c441 1421 } else if ( (ovp = (union overhead*)
1422 get_from_bigger_buckets((rnu << BUCKET_POW2_SHIFT) + 1,
1423 needed)) ) {
e8bc2b5c 1424 DEBUG_m(PerlIO_printf(Perl_debug_log,
1425 "stealing %ld bytes from bigger buckets\n",
1426 (long) needed));
1427 } else if (needed <= sbrked_remains) {
1428 ovp = (union overhead *)(last_sbrk_top - sbrked_remains);
1429 sbrked_remains -= needed;
1430 last_op = (char*)ovp;
fa423c5b 1431 } else
1432 ovp = getpages(needed, &nblks, bucket);
e8bc2b5c 1433
fa423c5b 1434 if (!ovp)
1435 return;
e8bc2b5c 1436
8d063cd8 1437 /*
1438 * Add new memory allocated to that on
1439 * free list for this hash bucket.
1440 */
e8bc2b5c 1441 siz = BUCKET_SIZE(bucket);
cf5c4ad8 1442#ifdef PACK_MALLOC
72aaf631 1443 *(u_char*)ovp = bucket; /* Fill index. */
e8bc2b5c 1444 if (bucket <= MAX_PACKED) {
1445 ovp = (union overhead *) ((char*)ovp + BLK_SHIFT(bucket));
1446 nblks = N_BLKS(bucket);
cf5c4ad8 1447# ifdef DEBUGGING_MSTATS
e8bc2b5c 1448 start_slack += BLK_SHIFT(bucket);
cf5c4ad8 1449# endif
e8bc2b5c 1450 } else if (bucket < LOG_OF_MIN_ARENA * BUCKETS_PER_POW2) {
1451 ovp = (union overhead *) ((char*)ovp + BLK_SHIFT(bucket));
cf5c4ad8 1452 siz -= sizeof(union overhead);
72aaf631 1453 } else ovp++; /* One chunk per block. */
e8bc2b5c 1454#endif /* PACK_MALLOC */
72aaf631 1455 nextf[bucket] = ovp;
5f05dabc 1456#ifdef DEBUGGING_MSTATS
1457 nmalloc[bucket] += nblks;
e8bc2b5c 1458 if (bucket > MAX_PACKED) {
1459 start_slack += M_OVERHEAD * nblks;
1460 }
5f05dabc 1461#endif
8d063cd8 1462 while (--nblks > 0) {
72aaf631 1463 ovp->ov_next = (union overhead *)((caddr_t)ovp + siz);
1464 ovp = (union overhead *)((caddr_t)ovp + siz);
8d063cd8 1465 }
8595d6f1 1466 /* Not all sbrks return zeroed memory.*/
72aaf631 1467 ovp->ov_next = (union overhead *)NULL;
cf5c4ad8 1468#ifdef PACK_MALLOC
e8bc2b5c 1469 if (bucket == 7*BUCKETS_PER_POW2) { /* Special case, explanation is above. */
1470 union overhead *n_op = nextf[7*BUCKETS_PER_POW2]->ov_next;
1471 nextf[7*BUCKETS_PER_POW2] =
1472 (union overhead *)((caddr_t)nextf[7*BUCKETS_PER_POW2]
1473 - sizeof(union overhead));
1474 nextf[7*BUCKETS_PER_POW2]->ov_next = n_op;
cf5c4ad8 1475 }
1476#endif /* !PACK_MALLOC */
8d063cd8 1477}
1478
94b6baf5 1479Free_t
86058a2d 1480Perl_mfree(void *mp)
cea2e8a9 1481{
ee0007ab 1482 register MEM_SIZE size;
72aaf631 1483 register union overhead *ovp;
352d5a3a 1484 char *cp = (char*)mp;
cf5c4ad8 1485#ifdef PACK_MALLOC
1486 u_char bucket;
1487#endif
8d063cd8 1488
e8bc2b5c 1489 DEBUG_m(PerlIO_printf(Perl_debug_log,
b900a521 1490 "0x%"UVxf": (%05lu) free\n",
1491 PTR2UV(cp), (unsigned long)(PL_an++)));
45d8adaa 1492
cf5c4ad8 1493 if (cp == NULL)
1494 return;
72aaf631 1495 ovp = (union overhead *)((caddr_t)cp
e8bc2b5c 1496 - sizeof (union overhead) * CHUNK_SHIFT);
cf5c4ad8 1497#ifdef PACK_MALLOC
72aaf631 1498 bucket = OV_INDEX(ovp);
cf5c4ad8 1499#endif
e8bc2b5c 1500#ifdef IGNORE_SMALL_BAD_FREE
1501 if ((bucket >= FIRST_BUCKET_WITH_CHECK)
1502 && (OV_MAGIC(ovp, bucket) != MAGIC))
1503#else
1504 if (OV_MAGIC(ovp, bucket) != MAGIC)
1505#endif
1506 {
68dc0745 1507 static int bad_free_warn = -1;
cf5c4ad8 1508 if (bad_free_warn == -1) {
32e30700 1509 dTHXo;
5fd9e9a4 1510 char *pbf = PerlEnv_getenv("PERL_BADFREE");
cf5c4ad8 1511 bad_free_warn = (pbf) ? atoi(pbf) : 1;
1512 }
1513 if (!bad_free_warn)
1514 return;
8990e307 1515#ifdef RCHECK
a687059c 1516 warn("%s free() ignored",
72aaf631 1517 ovp->ov_rmagic == RMAGIC - 1 ? "Duplicate" : "Bad");
8990e307 1518#else
d720c441 1519 warn("%s", "Bad free() ignored");
8990e307 1520#endif
8d063cd8 1521 return; /* sanity */
e8bc2b5c 1522 }
8d063cd8 1523#ifdef RCHECK
3541dd58 1524 ASSERT(ovp->ov_rmagic == RMAGIC, "chunk's head overwrite");
e8bc2b5c 1525 if (OV_INDEX(ovp) <= MAX_SHORT_BUCKET) {
1526 int i;
1527 MEM_SIZE nbytes = ovp->ov_size + 1;
1528
1529 if ((i = nbytes & 3)) {
1530 i = 4 - i;
1531 while (i--) {
3541dd58 1532 ASSERT(*((char *)((caddr_t)ovp + nbytes - RSLOP + i))
d720c441 1533 == RMAGIC_C, "chunk's tail overwrite");
e8bc2b5c 1534 }
1535 }
1536 nbytes = (nbytes + 3) &~ 3;
3541dd58 1537 ASSERT(*(u_int *)((caddr_t)ovp + nbytes - RSLOP) == RMAGIC, "chunk's tail overwrite");
e8bc2b5c 1538 }
72aaf631 1539 ovp->ov_rmagic = RMAGIC - 1;
8d063cd8 1540#endif
3541dd58 1541 ASSERT(OV_INDEX(ovp) < NBUCKETS, "chunk's head overwrite");
72aaf631 1542 size = OV_INDEX(ovp);
4ad56ec9 1543
1544 MALLOC_LOCK;
72aaf631 1545 ovp->ov_next = nextf[size];
1546 nextf[size] = ovp;
741df71a 1547 MALLOC_UNLOCK;
8d063cd8 1548}
1549
4ad56ec9 1550/* There is no need to do any locking in realloc (with an exception of
1551 trying to grow in place if we are at the end of the chain).
1552 If somebody calls us from a different thread with the same address,
1553 we are sole anyway. */
8d063cd8 1554
2304df62 1555Malloc_t
86058a2d 1556Perl_realloc(void *mp, size_t nbytes)
cea2e8a9 1557{
ee0007ab 1558 register MEM_SIZE onb;
72aaf631 1559 union overhead *ovp;
d720c441 1560 char *res;
1561 int prev_bucket;
e8bc2b5c 1562 register int bucket;
4ad56ec9 1563 int incr; /* 1 if does not fit, -1 if "easily" fits in a
1564 smaller bucket, otherwise 0. */
352d5a3a 1565 char *cp = (char*)mp;
8d063cd8 1566
e8bc2b5c 1567#if defined(DEBUGGING) || !defined(PERL_CORE)
ee0007ab 1568 MEM_SIZE size = nbytes;
45d8adaa 1569
45d8adaa 1570 if ((long)nbytes < 0)
cea2e8a9 1571 croak("%s", "panic: realloc");
45d8adaa 1572#endif
e8bc2b5c 1573
1574 BARK_64K_LIMIT("Reallocation",nbytes,size);
1575 if (!cp)
86058a2d 1576 return Perl_malloc(nbytes);
45d8adaa 1577
72aaf631 1578 ovp = (union overhead *)((caddr_t)cp
e8bc2b5c 1579 - sizeof (union overhead) * CHUNK_SHIFT);
1580 bucket = OV_INDEX(ovp);
4ad56ec9 1581
e8bc2b5c 1582#ifdef IGNORE_SMALL_BAD_FREE
4ad56ec9 1583 if ((bucket >= FIRST_BUCKET_WITH_CHECK)
1584 && (OV_MAGIC(ovp, bucket) != MAGIC))
e8bc2b5c 1585#else
4ad56ec9 1586 if (OV_MAGIC(ovp, bucket) != MAGIC)
e8bc2b5c 1587#endif
4ad56ec9 1588 {
1589 static int bad_free_warn = -1;
1590 if (bad_free_warn == -1) {
32e30700 1591 dTHXo;
4ad56ec9 1592 char *pbf = PerlEnv_getenv("PERL_BADFREE");
1593 bad_free_warn = (pbf) ? atoi(pbf) : 1;
1594 }
1595 if (!bad_free_warn)
ce70748c 1596 return Nullch;
4ad56ec9 1597#ifdef RCHECK
1598 warn("%srealloc() %signored",
1599 (ovp->ov_rmagic == RMAGIC - 1 ? "" : "Bad "),
1600 ovp->ov_rmagic == RMAGIC - 1 ? "of freed memory " : "");
1601#else
1602 warn("%s", "Bad realloc() ignored");
1603#endif
ce70748c 1604 return Nullch; /* sanity */
4ad56ec9 1605 }
1606
e8bc2b5c 1607 onb = BUCKET_SIZE_REAL(bucket);
55497cff 1608 /*
1609 * avoid the copy if same size block.
e8bc2b5c 1610 * We are not agressive with boundary cases. Note that it might
1611 * (for a small number of cases) give false negative if
55497cff 1612 * both new size and old one are in the bucket for
e8bc2b5c 1613 * FIRST_BIG_POW2, but the new one is near the lower end.
1614 *
1615 * We do not try to go to 1.5 times smaller bucket so far.
55497cff 1616 */
e8bc2b5c 1617 if (nbytes > onb) incr = 1;
1618 else {
1619#ifdef DO_NOT_TRY_HARDER_WHEN_SHRINKING
1620 if ( /* This is a little bit pessimal if PACK_MALLOC: */
1621 nbytes > ( (onb >> 1) - M_OVERHEAD )
1622# ifdef TWO_POT_OPTIMIZE
1623 || (bucket == FIRST_BIG_POW2 && nbytes >= LAST_SMALL_BOUND )
1624# endif
1625 )
1626#else /* !DO_NOT_TRY_HARDER_WHEN_SHRINKING */
1627 prev_bucket = ( (bucket > MAX_PACKED + 1)
1628 ? bucket - BUCKETS_PER_POW2
1629 : bucket - 1);
1630 if (nbytes > BUCKET_SIZE_REAL(prev_bucket))
1631#endif /* !DO_NOT_TRY_HARDER_WHEN_SHRINKING */
1632 incr = 0;
1633 else incr = -1;
1634 }
2ce36478 1635#ifdef STRESS_REALLOC
4ad56ec9 1636 goto hard_way;
2ce36478 1637#endif
4ad56ec9 1638 if (incr == 0) {
852c2e52 1639 inplace_label:
a687059c 1640#ifdef RCHECK
1641 /*
1642 * Record new allocated size of block and
1643 * bound space with magic numbers.
1644 */
72aaf631 1645 if (OV_INDEX(ovp) <= MAX_SHORT_BUCKET) {
e8bc2b5c 1646 int i, nb = ovp->ov_size + 1;
1647
1648 if ((i = nb & 3)) {
1649 i = 4 - i;
1650 while (i--) {
3541dd58 1651 ASSERT(*((char *)((caddr_t)ovp + nb - RSLOP + i)) == RMAGIC_C, "chunk's tail overwrite");
e8bc2b5c 1652 }
1653 }
1654 nb = (nb + 3) &~ 3;
3541dd58 1655 ASSERT(*(u_int *)((caddr_t)ovp + nb - RSLOP) == RMAGIC, "chunk's tail overwrite");
a687059c 1656 /*
1657 * Convert amount of memory requested into
1658 * closest block size stored in hash buckets
1659 * which satisfies request. Account for
1660 * space used per block for accounting.
1661 */
cf5c4ad8 1662 nbytes += M_OVERHEAD;
72aaf631 1663 ovp->ov_size = nbytes - 1;
e8bc2b5c 1664 if ((i = nbytes & 3)) {
1665 i = 4 - i;
1666 while (i--)
1667 *((char *)((caddr_t)ovp + nbytes - RSLOP + i))
1668 = RMAGIC_C;
1669 }
1670 nbytes = (nbytes + 3) &~ 3;
72aaf631 1671 *((u_int *)((caddr_t)ovp + nbytes - RSLOP)) = RMAGIC;
a687059c 1672 }
1673#endif
45d8adaa 1674 res = cp;
42ac124e 1675 DEBUG_m(PerlIO_printf(Perl_debug_log,
b900a521 1676 "0x%"UVxf": (%05lu) realloc %ld bytes inplace\n",
1677 PTR2UV(res),(unsigned long)(PL_an++),
42ac124e 1678 (long)size));
e8bc2b5c 1679 } else if (incr == 1 && (cp - M_OVERHEAD == last_op)
1680 && (onb > (1 << LOG_OF_MIN_ARENA))) {
1681 MEM_SIZE require, newarena = nbytes, pow;
1682 int shiftr;
1683
1684 POW2_OPTIMIZE_ADJUST(newarena);
1685 newarena = newarena + M_OVERHEAD;
1686 /* newarena = (newarena + 3) &~ 3; */
1687 shiftr = (newarena - 1) >> LOG_OF_MIN_ARENA;
1688 pow = LOG_OF_MIN_ARENA + 1;
1689 /* apart from this loop, this is O(1) */
1690 while (shiftr >>= 1)
1691 pow++;
1692 newarena = (1 << pow) + POW2_OPTIMIZE_SURPLUS(pow * BUCKETS_PER_POW2);
1693 require = newarena - onb - M_OVERHEAD;
1694
4ad56ec9 1695 MALLOC_LOCK;
1696 if (cp - M_OVERHEAD == last_op /* We *still* are the last chunk */
1697 && getpages_adjacent(require)) {
e8bc2b5c 1698#ifdef DEBUGGING_MSTATS
fa423c5b 1699 nmalloc[bucket]--;
1700 nmalloc[pow * BUCKETS_PER_POW2]++;
e8bc2b5c 1701#endif
fa423c5b 1702 *(cp - M_OVERHEAD) = pow * BUCKETS_PER_POW2; /* Fill index. */
4ad56ec9 1703 MALLOC_UNLOCK;
fa423c5b 1704 goto inplace_label;
4ad56ec9 1705 } else {
1706 MALLOC_UNLOCK;
fa423c5b 1707 goto hard_way;
4ad56ec9 1708 }
e8bc2b5c 1709 } else {
1710 hard_way:
42ac124e 1711 DEBUG_m(PerlIO_printf(Perl_debug_log,
b900a521 1712 "0x%"UVxf": (%05lu) realloc %ld bytes the hard way\n",
1713 PTR2UV(cp),(unsigned long)(PL_an++),
42ac124e 1714 (long)size));
86058a2d 1715 if ((res = (char*)Perl_malloc(nbytes)) == NULL)
e8bc2b5c 1716 return (NULL);
1717 if (cp != res) /* common optimization */
1718 Copy(cp, res, (MEM_SIZE)(nbytes<onb?nbytes:onb), char);
4ad56ec9 1719 Perl_mfree(cp);
45d8adaa 1720 }
2304df62 1721 return ((Malloc_t)res);
8d063cd8 1722}
1723
1724/*
1725 * Search ``srchlen'' elements of each free list for a block whose
1726 * header starts at ``freep''. If srchlen is -1 search the whole list.
1727 * Return bucket number, or -1 if not found.
1728 */
ee0007ab 1729static int
8ac85365 1730findbucket(union overhead *freep, int srchlen)
8d063cd8 1731{
1732 register union overhead *p;
1733 register int i, j;
1734
1735 for (i = 0; i < NBUCKETS; i++) {
1736 j = 0;
1737 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
1738 if (p == freep)
1739 return (i);
1740 j++;
1741 }
1742 }
1743 return (-1);
1744}
1745
cf5c4ad8 1746Malloc_t
86058a2d 1747Perl_calloc(register size_t elements, register size_t size)
cf5c4ad8 1748{
1749 long sz = elements * size;
86058a2d 1750 Malloc_t p = Perl_malloc(sz);
cf5c4ad8 1751
1752 if (p) {
1753 memset((void*)p, 0, sz);
1754 }
1755 return p;
1756}
1757
4ad56ec9 1758char *
1759Perl_strdup(const char *s)
1760{
1761 MEM_SIZE l = strlen(s);
b48f1ba5 1762 char *s1 = (char *)Perl_malloc(l+1);
4ad56ec9 1763
b48f1ba5 1764 Copy(s, s1, (MEM_SIZE)(l+1), char);
4ad56ec9 1765 return s1;
1766}
1767
1768#ifdef PERL_CORE
1769int
1770Perl_putenv(char *a)
1771{
1772 /* Sometimes system's putenv conflicts with my_setenv() - this is system
1773 malloc vs Perl's free(). */
1774 dTHX;
1775 char *var;
1776 char *val = a;
1777 MEM_SIZE l;
1778 char buf[80];
1779
1780 while (*val && *val != '=')
1781 val++;
1782 if (!*val)
1783 return -1;
1784 l = val - a;
1785 if (l < sizeof(buf))
1786 var = buf;
1787 else
1788 var = Perl_malloc(l + 1);
1789 Copy(a, var, l, char);
b48f1ba5 1790 var[l + 1] = 0;
1791 my_setenv(var, val+1);
4ad56ec9 1792 if (var != buf)
1793 Perl_mfree(var);
1794 return 0;
1795}
1796# endif
1797
e8bc2b5c 1798MEM_SIZE
cea2e8a9 1799Perl_malloced_size(void *p)
e8bc2b5c 1800{
8d6dde3e 1801 union overhead *ovp = (union overhead *)
1802 ((caddr_t)p - sizeof (union overhead) * CHUNK_SHIFT);
1803 int bucket = OV_INDEX(ovp);
1804#ifdef RCHECK
1805 /* The caller wants to have a complete control over the chunk,
1806 disable the memory checking inside the chunk. */
1807 if (bucket <= MAX_SHORT_BUCKET) {
1808 MEM_SIZE size = BUCKET_SIZE_REAL(bucket);
1809 ovp->ov_size = size + M_OVERHEAD - 1;
1810 *((u_int *)((caddr_t)ovp + size + M_OVERHEAD - RSLOP)) = RMAGIC;
1811 }
1812#endif
e8bc2b5c 1813 return BUCKET_SIZE_REAL(bucket);
1814}
1815
e8bc2b5c 1816# ifdef BUCKETS_ROOT2
1817# define MIN_EVEN_REPORT 6
1818# else
1819# define MIN_EVEN_REPORT MIN_BUCKET
1820# endif
8d063cd8 1821/*
1822 * mstats - print out statistics about malloc
1823 *
1824 * Prints two lines of numbers, one showing the length of the free list
1825 * for each size category, the second showing the number of mallocs -
1826 * frees for each size category.
1827 */
ee0007ab 1828void
864dbfa3 1829Perl_dump_mstats(pTHX_ char *s)
8d063cd8 1830{
df31f264 1831#ifdef DEBUGGING_MSTATS
8d063cd8 1832 register int i, j;
1833 register union overhead *p;
e8bc2b5c 1834 int topbucket=0, topbucket_ev=0, topbucket_odd=0, totfree=0, total=0;
c07a80fd 1835 u_int nfree[NBUCKETS];
e8bc2b5c 1836 int total_chain = 0;
4ad56ec9 1837 struct chunk_chain_s* nextchain;
8d063cd8 1838
4ad56ec9 1839 MALLOC_LOCK;
e8bc2b5c 1840 for (i = MIN_BUCKET ; i < NBUCKETS; i++) {
8d063cd8 1841 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
1842 ;
c07a80fd 1843 nfree[i] = j;
e8bc2b5c 1844 totfree += nfree[i] * BUCKET_SIZE_REAL(i);
1845 total += nmalloc[i] * BUCKET_SIZE_REAL(i);
1846 if (nmalloc[i]) {
1847 i % 2 ? (topbucket_odd = i) : (topbucket_ev = i);
1848 topbucket = i;
1849 }
c07a80fd 1850 }
4ad56ec9 1851 nextchain = chunk_chain;
1852 while (nextchain) {
1853 total_chain += nextchain->size;
1854 nextchain = nextchain->next;
1855 }
1856 MALLOC_UNLOCK;
c07a80fd 1857 if (s)
bf49b057 1858 PerlIO_printf(Perl_error_log,
d720c441 1859 "Memory allocation statistics %s (buckets %ld(%ld)..%ld(%ld)\n",
e8bc2b5c 1860 s,
d720c441 1861 (long)BUCKET_SIZE_REAL(MIN_BUCKET),
1862 (long)BUCKET_SIZE(MIN_BUCKET),
1863 (long)BUCKET_SIZE_REAL(topbucket), (long)BUCKET_SIZE(topbucket));
bf49b057 1864 PerlIO_printf(Perl_error_log, "%8d free:", totfree);
e8bc2b5c 1865 for (i = MIN_EVEN_REPORT; i <= topbucket; i += BUCKETS_PER_POW2) {
bf49b057 1866 PerlIO_printf(Perl_error_log,
e8bc2b5c 1867 ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
1868 ? " %5d"
1869 : ((i < 12*BUCKETS_PER_POW2) ? " %3d" : " %d")),
1870 nfree[i]);
1871 }
1872#ifdef BUCKETS_ROOT2
bf49b057 1873 PerlIO_printf(Perl_error_log, "\n\t ");
e8bc2b5c 1874 for (i = MIN_BUCKET + 1; i <= topbucket_odd; i += BUCKETS_PER_POW2) {
bf49b057 1875 PerlIO_printf(Perl_error_log,
e8bc2b5c 1876 ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
1877 ? " %5d"
1878 : ((i < 12*BUCKETS_PER_POW2) ? " %3d" : " %d")),
1879 nfree[i]);
8d063cd8 1880 }
e8bc2b5c 1881#endif
bf49b057 1882 PerlIO_printf(Perl_error_log, "\n%8d used:", total - totfree);
e8bc2b5c 1883 for (i = MIN_EVEN_REPORT; i <= topbucket; i += BUCKETS_PER_POW2) {
bf49b057 1884 PerlIO_printf(Perl_error_log,
e8bc2b5c 1885 ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
1886 ? " %5d"
1887 : ((i < 12*BUCKETS_PER_POW2) ? " %3d" : " %d")),
1888 nmalloc[i] - nfree[i]);
c07a80fd 1889 }
e8bc2b5c 1890#ifdef BUCKETS_ROOT2
bf49b057 1891 PerlIO_printf(Perl_error_log, "\n\t ");
e8bc2b5c 1892 for (i = MIN_BUCKET + 1; i <= topbucket_odd; i += BUCKETS_PER_POW2) {
bf49b057 1893 PerlIO_printf(Perl_error_log,
e8bc2b5c 1894 ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
1895 ? " %5d"
1896 : ((i < 12*BUCKETS_PER_POW2) ? " %3d" : " %d")),
1897 nmalloc[i] - nfree[i]);
1898 }
1899#endif
bf49b057 1900 PerlIO_printf(Perl_error_log, "\nTotal sbrk(): %d/%d:%d. Odd ends: pad+heads+chain+tail: %d+%d+%d+%d.\n",
e8bc2b5c 1901 goodsbrk + sbrk_slack, sbrks, sbrk_good, sbrk_slack,
1902 start_slack, total_chain, sbrked_remains);
df31f264 1903#endif /* DEBUGGING_MSTATS */
c07a80fd 1904}
a687059c 1905#endif /* lint */
cf5c4ad8 1906
cf5c4ad8 1907#ifdef USE_PERL_SBRK
1908
e3663bad 1909# if defined(__MACHTEN_PPC__) || defined(NeXT) || defined(__NeXT__) || defined(PURIFY)
38ac2dc8 1910# define PERL_SBRK_VIA_MALLOC
1911/*
1912 * MachTen's malloc() returns a buffer aligned on a two-byte boundary.
1913 * While this is adequate, it may slow down access to longer data
1914 * types by forcing multiple memory accesses. It also causes
1915 * complaints when RCHECK is in force. So we allocate six bytes
1916 * more than we need to, and return an address rounded up to an
1917 * eight-byte boundary.
1918 *
1919 * 980701 Dominic Dunlop <domo@computer.org>
1920 */
5bbd1ef5 1921# define SYSTEM_ALLOC_ALIGNMENT 2
38ac2dc8 1922# endif
1923
760ac839 1924# ifdef PERL_SBRK_VIA_MALLOC
cf5c4ad8 1925
1926/* it may seem schizophrenic to use perl's malloc and let it call system */
1927/* malloc, the reason for that is only the 3.2 version of the OS that had */
1928/* frequent core dumps within nxzonefreenolock. This sbrk routine put an */
1929/* end to the cores */
1930
38ac2dc8 1931# ifndef SYSTEM_ALLOC
1932# define SYSTEM_ALLOC(a) malloc(a)
1933# endif
5bbd1ef5 1934# ifndef SYSTEM_ALLOC_ALIGNMENT
1935# define SYSTEM_ALLOC_ALIGNMENT MEM_ALIGNBYTES
1936# endif
cf5c4ad8 1937
760ac839 1938# endif /* PERL_SBRK_VIA_MALLOC */
cf5c4ad8 1939
1940static IV Perl_sbrk_oldchunk;
1941static long Perl_sbrk_oldsize;
1942
760ac839 1943# define PERLSBRK_32_K (1<<15)
1944# define PERLSBRK_64_K (1<<16)
cf5c4ad8 1945
b63effbb 1946Malloc_t
df0003d4 1947Perl_sbrk(int size)
cf5c4ad8 1948{
1949 IV got;
1950 int small, reqsize;
1951
1952 if (!size) return 0;
55497cff 1953#ifdef PERL_CORE
cf5c4ad8 1954 reqsize = size; /* just for the DEBUG_m statement */
1955#endif
57569e04 1956#ifdef PACK_MALLOC
1957 size = (size + 0x7ff) & ~0x7ff;
1958#endif
cf5c4ad8 1959 if (size <= Perl_sbrk_oldsize) {
1960 got = Perl_sbrk_oldchunk;
1961 Perl_sbrk_oldchunk += size;
1962 Perl_sbrk_oldsize -= size;
1963 } else {
1964 if (size >= PERLSBRK_32_K) {
1965 small = 0;
1966 } else {
cf5c4ad8 1967 size = PERLSBRK_64_K;
1968 small = 1;
1969 }
5bbd1ef5 1970# if NEEDED_ALIGNMENT > SYSTEM_ALLOC_ALIGNMENT
1971 size += NEEDED_ALIGNMENT - SYSTEM_ALLOC_ALIGNMENT;
1972# endif
cf5c4ad8 1973 got = (IV)SYSTEM_ALLOC(size);
5bbd1ef5 1974# if NEEDED_ALIGNMENT > SYSTEM_ALLOC_ALIGNMENT
5a7d6335 1975 got = (got + NEEDED_ALIGNMENT - 1) & ~(NEEDED_ALIGNMENT - 1);
5bbd1ef5 1976# endif
cf5c4ad8 1977 if (small) {
1978 /* Chunk is small, register the rest for future allocs. */
1979 Perl_sbrk_oldchunk = got + reqsize;
1980 Perl_sbrk_oldsize = size - reqsize;
1981 }
1982 }
1983
b900a521 1984 DEBUG_m(PerlIO_printf(Perl_debug_log, "sbrk malloc size %ld (reqsize %ld), left size %ld, give addr 0x%"UVxf"\n",
1985 size, reqsize, Perl_sbrk_oldsize, PTR2UV(got)));
cf5c4ad8 1986
1987 return (void *)got;
1988}
1989
1990#endif /* ! defined USE_PERL_SBRK */