1<<$randbits is not good for randbits=48.
[p5sagit/p5-mst-13.2.git] / malloc.c
1 /*    malloc.c
2  *
3  */
4
5 /*
6   Here are some notes on configuring Perl's malloc.
7  
8   There are two macros which serve as bulk disablers of advanced
9   features of this malloc: NO_FANCY_MALLOC, PLAIN_MALLOC (undef by
10   default).  Look in the list of default values below to understand
11   their exact effect.  Defining NO_FANCY_MALLOC returns malloc.c to the
12   state of the malloc in Perl 5.004.  Additionally defining PLAIN_MALLOC
13   returns it to the state as of Perl 5.000.
14
15   Note that some of the settings below may be ignored in the code based
16   on values of other macros.  The PERL_CORE symbol is only defined when
17   perl itself is being compiled (so malloc can make some assumptions
18   about perl's facilities being available to it).
19
20   Each config option has a short description, followed by its name,
21   default value, and a comment about the default (if applicable).  Some
22   options take a precise value, while the others are just boolean.
23   The boolean ones are listed first.
24
25     # Enable code for an emergency memory pool in $^M.  See perlvar.pod
26     # for a description of $^M.
27     PERL_EMERGENCY_SBRK         (!PLAIN_MALLOC && PERL_CORE)
28
29     # Enable code for printing memory statistics.
30     DEBUGGING_MSTATS            (!PLAIN_MALLOC && PERL_CORE)
31
32     # Move allocation info for small buckets into separate areas.
33     # Memory optimization (especially for small allocations, of the
34     # less than 64 bytes).  Since perl usually makes a large number
35     # of small allocations, this is usually a win.
36     PACK_MALLOC                 (!PLAIN_MALLOC && !RCHECK)
37
38     # Add one page to big powers of two when calculating bucket size.
39     # This is targeted at big allocations, as are common in image
40     # processing.
41     TWO_POT_OPTIMIZE            !PLAIN_MALLOC
42  
43     # Use intermediate bucket sizes between powers-of-two.  This is
44     # generally a memory optimization, and a (small) speed pessimization.
45     BUCKETS_ROOT2               !NO_FANCY_MALLOC
46
47     # Do not check small deallocations for bad free().  Memory
48     # and speed optimization, error reporting pessimization.
49     IGNORE_SMALL_BAD_FREE       (!NO_FANCY_MALLOC && !RCHECK)
50
51     # Use table lookup to decide in which bucket a given allocation will go.
52     SMALL_BUCKET_VIA_TABLE      !NO_FANCY_MALLOC
53
54     # Use a perl-defined sbrk() instead of the (presumably broken or
55     # missing) system-supplied sbrk().
56     USE_PERL_SBRK               undef
57
58     # Use system malloc() (or calloc() etc.) to emulate sbrk(). Normally
59     # only used with broken sbrk()s.
60     PERL_SBRK_VIA_MALLOC        undef
61
62     # Which allocator to use if PERL_SBRK_VIA_MALLOC
63     SYSTEM_ALLOC(a)             malloc(a)
64
65     # Disable memory overwrite checking with DEBUGGING.  Memory and speed
66     # optimization, error reporting pessimization.
67     NO_RCHECK                   undef
68
69     # Enable memory overwrite checking with DEBUGGING.  Memory and speed
70     # pessimization, error reporting optimization
71     RCHECK                      (DEBUGGING && !NO_RCHECK)
72
73     # Failed allocations bigger than this size croak (if
74     # PERL_EMERGENCY_SBRK is enabled) without touching $^M.  See
75     # perlvar.pod for a description of $^M.
76     BIG_SIZE                     (1<<16)        # 64K
77
78     # Starting from this power of two, add an extra page to the
79     # size of the bucket. This enables optimized allocations of sizes
80     # close to powers of 2.  Note that the value is indexed at 0.
81     FIRST_BIG_POW2              15              # 32K, 16K is used too often
82
83     # Estimate of minimal memory footprint.  malloc uses this value to
84     # request the most reasonable largest blocks of memory from the system.
85     FIRST_SBRK                  (48*1024)
86
87     # Round up sbrk()s to multiples of this.
88     MIN_SBRK                    2048
89
90     # Round up sbrk()s to multiples of this percent of footprint.
91     MIN_SBRK_FRAC               3
92
93     # Add this much memory to big powers of two to get the bucket size.
94     PERL_PAGESIZE               4096
95
96     # This many sbrk() discontinuities should be tolerated even
97     # from the start without deciding that sbrk() is usually
98     # discontinuous.
99     SBRK_ALLOW_FAILURES         3
100
101     # This many continuous sbrk()s compensate for one discontinuous one.
102     SBRK_FAILURE_PRICE          50
103
104     # Some configurations may ask for 12-byte-or-so allocations which
105     # require 8-byte alignment (?!).  In such situation one needs to
106     # define this to disable 12-byte bucket (will increase memory footprint)
107     STRICT_ALIGNMENT            undef
108
109   This implementation assumes that calling PerlIO_printf() does not
110   result in any memory allocation calls (used during a panic).
111
112  */
113
114 #ifndef NO_FANCY_MALLOC
115 #  ifndef SMALL_BUCKET_VIA_TABLE
116 #    define SMALL_BUCKET_VIA_TABLE
117 #  endif 
118 #  ifndef BUCKETS_ROOT2
119 #    define BUCKETS_ROOT2
120 #  endif 
121 #  ifndef IGNORE_SMALL_BAD_FREE
122 #    define IGNORE_SMALL_BAD_FREE
123 #  endif 
124 #endif 
125
126 #ifndef PLAIN_MALLOC                    /* Bulk enable features */
127 #  ifndef PACK_MALLOC
128 #      define PACK_MALLOC
129 #  endif 
130 #  ifndef TWO_POT_OPTIMIZE
131 #    define TWO_POT_OPTIMIZE
132 #  endif 
133 #  if defined(PERL_CORE) && !defined(PERL_EMERGENCY_SBRK)
134 #    define PERL_EMERGENCY_SBRK
135 #  endif 
136 #  if defined(PERL_CORE) && !defined(DEBUGGING_MSTATS)
137 #    define DEBUGGING_MSTATS
138 #  endif 
139 #endif
140
141 #define MIN_BUC_POW2 (sizeof(void*) > 4 ? 3 : 2) /* Allow for 4-byte arena. */
142 #define MIN_BUCKET (MIN_BUC_POW2 * BUCKETS_PER_POW2)
143
144 #if !(defined(I286) || defined(atarist))
145         /* take 2k unless the block is bigger than that */
146 #  define LOG_OF_MIN_ARENA 11
147 #else
148         /* take 16k unless the block is bigger than that 
149            (80286s like large segments!), probably good on the atari too */
150 #  define LOG_OF_MIN_ARENA 14
151 #endif
152
153 #ifndef lint
154 #  if defined(DEBUGGING) && !defined(NO_RCHECK)
155 #    define RCHECK
156 #  endif
157 #  if defined(RCHECK) && defined(IGNORE_SMALL_BAD_FREE)
158 #    undef IGNORE_SMALL_BAD_FREE
159 #  endif 
160 /*
161  * malloc.c (Caltech) 2/21/82
162  * Chris Kingsley, kingsley@cit-20.
163  *
164  * This is a very fast storage allocator.  It allocates blocks of a small 
165  * number of different sizes, and keeps free lists of each size.  Blocks that
166  * don't exactly fit are passed up to the next larger size.  In this 
167  * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
168  * If PACK_MALLOC is defined, small blocks are 2^n bytes long.
169  * This is designed for use in a program that uses vast quantities of memory,
170  * but bombs when it runs out. 
171  */
172
173 #ifdef PERL_CORE
174 #  include "EXTERN.h"
175 #  include "perl.h"
176 #else
177 #  ifdef PERL_FOR_X2P
178 #    include "../EXTERN.h"
179 #    include "../perl.h"
180 #  else
181 #    include <stdlib.h>
182 #    include <stdio.h>
183 #    include <memory.h>
184 #    define _(arg) arg
185 #    ifndef Malloc_t
186 #      define Malloc_t void *
187 #    endif
188 #    ifndef MEM_SIZE
189 #      define MEM_SIZE unsigned long
190 #    endif
191 #    ifndef LONG_MAX
192 #      define LONG_MAX 0x7FFFFFFF
193 #    endif
194 #    ifndef UV
195 #      define UV unsigned long
196 #    endif
197 #    ifndef caddr_t
198 #      define caddr_t char *
199 #    endif
200 #    ifndef Free_t
201 #      define Free_t void
202 #    endif
203 #    define Copy(s,d,n,t) (void)memcpy((char*)(d),(char*)(s), (n) * sizeof(t))
204 #    define PerlEnv_getenv getenv
205 #    define PerlIO_printf fprintf
206 #    define PerlIO_stderr() stderr
207 #  endif
208 #  ifndef croak                         /* make depend */
209 #    define croak(mess, arg) warn((mess), (arg)); exit(1);
210 #  endif 
211 #  ifndef warn
212 #    define warn(mess, arg) fprintf(stderr, (mess), (arg));
213 #  endif 
214 #  ifdef DEBUG_m
215 #    undef DEBUG_m
216 #  endif 
217 #  define DEBUG_m(a)
218 #  ifdef DEBUGGING
219 #     undef DEBUGGING
220 #  endif
221 #endif
222
223 #ifndef MUTEX_LOCK
224 #  define MUTEX_LOCK(l)
225 #endif 
226
227 #ifndef MUTEX_UNLOCK
228 #  define MUTEX_UNLOCK(l)
229 #endif 
230
231 #ifdef DEBUGGING
232 #  undef DEBUG_m
233 #  define DEBUG_m(a)  if (PL_debug & 128)   a
234 #endif
235
236 /* I don't much care whether these are defined in sys/types.h--LAW */
237
238 #define u_char unsigned char
239 #define u_int unsigned int
240
241 #ifdef HAS_QUAD
242 #  define u_bigint UV                   /* Needs to eat *void. */
243 #else  /* needed? */
244 #  define u_bigint unsigned long        /* Needs to eat *void. */
245 #endif
246
247 #define u_short unsigned short
248
249 /* 286 and atarist like big chunks, which gives too much overhead. */
250 #if (defined(RCHECK) || defined(I286) || defined(atarist)) && defined(PACK_MALLOC)
251 #  undef PACK_MALLOC
252 #endif 
253
254 /*
255  * The description below is applicable if PACK_MALLOC is not defined.
256  *
257  * The overhead on a block is at least 4 bytes.  When free, this space
258  * contains a pointer to the next free block, and the bottom two bits must
259  * be zero.  When in use, the first byte is set to MAGIC, and the second
260  * byte is the size index.  The remaining bytes are for alignment.
261  * If range checking is enabled and the size of the block fits
262  * in two bytes, then the top two bytes hold the size of the requested block
263  * plus the range checking words, and the header word MINUS ONE.
264  */
265 union   overhead {
266         union   overhead *ov_next;      /* when free */
267 #if MEM_ALIGNBYTES > 4
268         double  strut;                  /* alignment problems */
269 #endif
270         struct {
271                 u_char  ovu_magic;      /* magic number */
272                 u_char  ovu_index;      /* bucket # */
273 #ifdef RCHECK
274                 u_short ovu_size;       /* actual block size */
275                 u_int   ovu_rmagic;     /* range magic number */
276 #endif
277         } ovu;
278 #define ov_magic        ovu.ovu_magic
279 #define ov_index        ovu.ovu_index
280 #define ov_size         ovu.ovu_size
281 #define ov_rmagic       ovu.ovu_rmagic
282 };
283
284 #ifdef DEBUGGING
285 static void botch _((char *diag, char *s));
286 #endif
287 static void morecore _((int bucket));
288 static int findbucket _((union overhead *freep, int srchlen));
289 static void add_to_chain(void *p, MEM_SIZE size, MEM_SIZE chip);
290
291 #define MAGIC           0xff            /* magic # on accounting info */
292 #define RMAGIC          0x55555555      /* magic # on range info */
293 #define RMAGIC_C        0x55            /* magic # on range info */
294
295 #ifdef RCHECK
296 #  define       RSLOP           sizeof (u_int)
297 #  ifdef TWO_POT_OPTIMIZE
298 #    define MAX_SHORT_BUCKET (12 * BUCKETS_PER_POW2)
299 #  else
300 #    define MAX_SHORT_BUCKET (13 * BUCKETS_PER_POW2)
301 #  endif 
302 #else
303 #  define       RSLOP           0
304 #endif
305
306 #if !defined(PACK_MALLOC) && defined(BUCKETS_ROOT2)
307 #  undef BUCKETS_ROOT2
308 #endif 
309
310 #ifdef BUCKETS_ROOT2
311 #  define BUCKET_TABLE_SHIFT 2
312 #  define BUCKET_POW2_SHIFT 1
313 #  define BUCKETS_PER_POW2 2
314 #else
315 #  define BUCKET_TABLE_SHIFT MIN_BUC_POW2
316 #  define BUCKET_POW2_SHIFT 0
317 #  define BUCKETS_PER_POW2 1
318 #endif 
319
320 #if !defined(MEM_ALIGNBYTES) || ((MEM_ALIGNBYTES > 4) && !defined(STRICT_ALIGNMENT))
321 /* Figure out the alignment of void*. */
322 struct aligner {
323   char c;
324   void *p;
325 };
326 #  define ALIGN_SMALL ((int)((caddr_t)&(((struct aligner*)0)->p)))
327 #else
328 #  define ALIGN_SMALL MEM_ALIGNBYTES
329 #endif
330
331 #define IF_ALIGN_8(yes,no)      ((ALIGN_SMALL>4) ? (yes) : (no))
332
333 #ifdef BUCKETS_ROOT2
334 #  define MAX_BUCKET_BY_TABLE 13
335 static u_short buck_size[MAX_BUCKET_BY_TABLE + 1] = 
336   { 
337       0, 0, 0, 0, 4, 4, 8, 12, 16, 24, 32, 48, 64, 80,
338   };
339 #  define BUCKET_SIZE(i) ((i) % 2 ? buck_size[i] : (1 << ((i) >> BUCKET_POW2_SHIFT)))
340 #  define BUCKET_SIZE_REAL(i) ((i) <= MAX_BUCKET_BY_TABLE               \
341                                ? buck_size[i]                           \
342                                : ((1 << ((i) >> BUCKET_POW2_SHIFT))     \
343                                   - MEM_OVERHEAD(i)                     \
344                                   + POW2_OPTIMIZE_SURPLUS(i)))
345 #else
346 #  define BUCKET_SIZE(i) (1 << ((i) >> BUCKET_POW2_SHIFT))
347 #  define BUCKET_SIZE_REAL(i) (BUCKET_SIZE(i) - MEM_OVERHEAD(i) + POW2_OPTIMIZE_SURPLUS(i))
348 #endif 
349
350
351 #ifdef PACK_MALLOC
352 /* In this case it is assumed that if we do sbrk() in 2K units, we
353  * will get 2K aligned arenas (at least after some initial
354  * alignment). The bucket number of the given subblock is on the start
355  * of 2K arena which contains the subblock.  Several following bytes
356  * contain the magic numbers for the subblocks in the block.
357  *
358  * Sizes of chunks are powers of 2 for chunks in buckets <=
359  * MAX_PACKED, after this they are (2^n - sizeof(union overhead)) (to
360  * get alignment right).
361  *
362  * Consider an arena for 2^n with n>MAX_PACKED.  We suppose that
363  * starts of all the chunks in a 2K arena are in different
364  * 2^n-byte-long chunks.  If the top of the last chunk is aligned on a
365  * boundary of 2K block, this means that sizeof(union
366  * overhead)*"number of chunks" < 2^n, or sizeof(union overhead)*2K <
367  * 4^n, or n > 6 + log2(sizeof()/2)/2, since a chunk of size 2^n -
368  * overhead is used.  Since this rules out n = 7 for 8 byte alignment,
369  * we specialcase allocation of the first of 16 128-byte-long chunks.
370  *
371  * Note that with the above assumption we automatically have enough
372  * place for MAGIC at the start of 2K block.  Note also that we
373  * overlay union overhead over the chunk, thus the start of small chunks
374  * is immediately overwritten after freeing.  */
375 #  define MAX_PACKED_POW2 6
376 #  define MAX_PACKED (MAX_PACKED_POW2 * BUCKETS_PER_POW2 + BUCKET_POW2_SHIFT)
377 #  define MAX_POW2_ALGO ((1<<(MAX_PACKED_POW2 + 1)) - M_OVERHEAD)
378 #  define TWOK_MASK ((1<<LOG_OF_MIN_ARENA) - 1)
379 #  define TWOK_MASKED(x) ((u_bigint)(x) & ~TWOK_MASK)
380 #  define TWOK_SHIFT(x) ((u_bigint)(x) & TWOK_MASK)
381 #  define OV_INDEXp(block) ((u_char*)(TWOK_MASKED(block)))
382 #  define OV_INDEX(block) (*OV_INDEXp(block))
383 #  define OV_MAGIC(block,bucket) (*(OV_INDEXp(block) +                  \
384                                     (TWOK_SHIFT(block)>>                \
385                                      (bucket>>BUCKET_POW2_SHIFT)) +     \
386                                     (bucket >= MIN_NEEDS_SHIFT ? 1 : 0)))
387     /* A bucket can have a shift smaller than it size, we need to
388        shift its magic number so it will not overwrite index: */
389 #  ifdef BUCKETS_ROOT2
390 #    define MIN_NEEDS_SHIFT (7*BUCKETS_PER_POW2 - 1) /* Shift 80 greater than chunk 64. */
391 #  else
392 #    define MIN_NEEDS_SHIFT (7*BUCKETS_PER_POW2) /* Shift 128 greater than chunk 32. */
393 #  endif 
394 #  define CHUNK_SHIFT 0
395
396 /* Number of active buckets of given ordinal. */
397 #ifdef IGNORE_SMALL_BAD_FREE
398 #define FIRST_BUCKET_WITH_CHECK (6 * BUCKETS_PER_POW2) /* 64 */
399 #  define N_BLKS(bucket) ( (bucket) < FIRST_BUCKET_WITH_CHECK           \
400                          ? ((1<<LOG_OF_MIN_ARENA) - 1)/BUCKET_SIZE(bucket) \
401                          : n_blks[bucket] )
402 #else
403 #  define N_BLKS(bucket) n_blks[bucket]
404 #endif 
405
406 static u_short n_blks[LOG_OF_MIN_ARENA * BUCKETS_PER_POW2] = 
407   {
408 #  if BUCKETS_PER_POW2==1
409       0, 0,
410       (MIN_BUC_POW2==2 ? 384 : 0),
411       224, 120, 62, 31, 16, 8, 4, 2
412 #  else
413       0, 0, 0, 0,
414       (MIN_BUC_POW2==2 ? 384 : 0), (MIN_BUC_POW2==2 ? 384 : 0), /* 4, 4 */
415       224, 149, 120, 80, 62, 41, 31, 25, 16, 16, 8, 8, 4, 4, 2, 2
416 #  endif
417   };
418
419 /* Shift of the first bucket with the given ordinal inside 2K chunk. */
420 #ifdef IGNORE_SMALL_BAD_FREE
421 #  define BLK_SHIFT(bucket) ( (bucket) < FIRST_BUCKET_WITH_CHECK        \
422                               ? ((1<<LOG_OF_MIN_ARENA)                  \
423                                  - BUCKET_SIZE(bucket) * N_BLKS(bucket)) \
424                               : blk_shift[bucket])
425 #else
426 #  define BLK_SHIFT(bucket) blk_shift[bucket]
427 #endif 
428
429 static u_short blk_shift[LOG_OF_MIN_ARENA * BUCKETS_PER_POW2] = 
430   { 
431 #  if BUCKETS_PER_POW2==1
432       0, 0,
433       (MIN_BUC_POW2==2 ? 512 : 0),
434       256, 128, 64, 64,                 /* 8 to 64 */
435       16*sizeof(union overhead), 
436       8*sizeof(union overhead), 
437       4*sizeof(union overhead), 
438       2*sizeof(union overhead), 
439 #  else
440       0, 0, 0, 0,
441       (MIN_BUC_POW2==2 ? 512 : 0), (MIN_BUC_POW2==2 ? 512 : 0),
442       256, 260, 128, 128, 64, 80, 64, 48, /* 8 to 96 */
443       16*sizeof(union overhead), 16*sizeof(union overhead), 
444       8*sizeof(union overhead), 8*sizeof(union overhead), 
445       4*sizeof(union overhead), 4*sizeof(union overhead), 
446       2*sizeof(union overhead), 2*sizeof(union overhead), 
447 #  endif 
448   };
449
450 #else  /* !PACK_MALLOC */
451
452 #  define OV_MAGIC(block,bucket) (block)->ov_magic
453 #  define OV_INDEX(block) (block)->ov_index
454 #  define CHUNK_SHIFT 1
455 #  define MAX_PACKED -1
456 #endif /* !PACK_MALLOC */
457
458 #define M_OVERHEAD (sizeof(union overhead) + RSLOP)
459
460 #ifdef PACK_MALLOC
461 #  define MEM_OVERHEAD(bucket) \
462   (bucket <= MAX_PACKED ? 0 : M_OVERHEAD)
463 #  ifdef SMALL_BUCKET_VIA_TABLE
464 #    define START_SHIFTS_BUCKET ((MAX_PACKED_POW2 + 1) * BUCKETS_PER_POW2)
465 #    define START_SHIFT MAX_PACKED_POW2
466 #    ifdef BUCKETS_ROOT2                /* Chunks of size 3*2^n. */
467 #      define SIZE_TABLE_MAX 80
468 #    else
469 #      define SIZE_TABLE_MAX 64
470 #    endif 
471 static char bucket_of[] =
472   {
473 #    ifdef BUCKETS_ROOT2                /* Chunks of size 3*2^n. */
474       /* 0 to 15 in 4-byte increments. */
475       (sizeof(void*) > 4 ? 6 : 5),      /* 4/8, 5-th bucket for better reports */
476       6,                                /* 8 */
477       IF_ALIGN_8(8,7), 8,               /* 16/12, 16 */
478       9, 9, 10, 10,                     /* 24, 32 */
479       11, 11, 11, 11,                   /* 48 */
480       12, 12, 12, 12,                   /* 64 */
481       13, 13, 13, 13,                   /* 80 */
482       13, 13, 13, 13                    /* 80 */
483 #    else /* !BUCKETS_ROOT2 */
484       /* 0 to 15 in 4-byte increments. */
485       (sizeof(void*) > 4 ? 3 : 2),
486       3, 
487       4, 4, 
488       5, 5, 5, 5,
489       6, 6, 6, 6,
490       6, 6, 6, 6
491 #    endif /* !BUCKETS_ROOT2 */
492   };
493 #  else  /* !SMALL_BUCKET_VIA_TABLE */
494 #    define START_SHIFTS_BUCKET MIN_BUCKET
495 #    define START_SHIFT (MIN_BUC_POW2 - 1)
496 #  endif /* !SMALL_BUCKET_VIA_TABLE */
497 #else  /* !PACK_MALLOC */
498 #  define MEM_OVERHEAD(bucket) M_OVERHEAD
499 #  ifdef SMALL_BUCKET_VIA_TABLE
500 #    undef SMALL_BUCKET_VIA_TABLE
501 #  endif 
502 #  define START_SHIFTS_BUCKET MIN_BUCKET
503 #  define START_SHIFT (MIN_BUC_POW2 - 1)
504 #endif /* !PACK_MALLOC */
505
506 /*
507  * Big allocations are often of the size 2^n bytes. To make them a
508  * little bit better, make blocks of size 2^n+pagesize for big n.
509  */
510
511 #ifdef TWO_POT_OPTIMIZE
512
513 #  ifndef PERL_PAGESIZE
514 #    define PERL_PAGESIZE 4096
515 #  endif 
516 #  ifndef FIRST_BIG_POW2
517 #    define FIRST_BIG_POW2 15   /* 32K, 16K is used too often. */
518 #  endif
519 #  define FIRST_BIG_BLOCK (1<<FIRST_BIG_POW2)
520 /* If this value or more, check against bigger blocks. */
521 #  define FIRST_BIG_BOUND (FIRST_BIG_BLOCK - M_OVERHEAD)
522 /* If less than this value, goes into 2^n-overhead-block. */
523 #  define LAST_SMALL_BOUND ((FIRST_BIG_BLOCK>>1) - M_OVERHEAD)
524
525 #  define POW2_OPTIMIZE_ADJUST(nbytes)                          \
526    ((nbytes >= FIRST_BIG_BOUND) ? nbytes -= PERL_PAGESIZE : 0)
527 #  define POW2_OPTIMIZE_SURPLUS(bucket)                         \
528    ((bucket >= FIRST_BIG_POW2 * BUCKETS_PER_POW2) ? PERL_PAGESIZE : 0)
529
530 #else  /* !TWO_POT_OPTIMIZE */
531 #  define POW2_OPTIMIZE_ADJUST(nbytes)
532 #  define POW2_OPTIMIZE_SURPLUS(bucket) 0
533 #endif /* !TWO_POT_OPTIMIZE */
534
535 #if defined(HAS_64K_LIMIT) && defined(PERL_CORE)
536 #  define BARK_64K_LIMIT(what,nbytes,size)                              \
537         if (nbytes > 0xffff) {                                          \
538                 PerlIO_printf(PerlIO_stderr(),                          \
539                               "%s too large: %lx\n", what, size);       \
540                 my_exit(1);                                             \
541         }
542 #else /* !HAS_64K_LIMIT || !PERL_CORE */
543 #  define BARK_64K_LIMIT(what,nbytes,size)
544 #endif /* !HAS_64K_LIMIT || !PERL_CORE */
545
546 #ifndef MIN_SBRK
547 #  define MIN_SBRK 2048
548 #endif 
549
550 #ifndef FIRST_SBRK
551 #  define FIRST_SBRK (48*1024)
552 #endif 
553
554 /* Minimal sbrk in percents of what is already alloced. */
555 #ifndef MIN_SBRK_FRAC
556 #  define MIN_SBRK_FRAC 3
557 #endif 
558
559 #ifndef SBRK_ALLOW_FAILURES
560 #  define SBRK_ALLOW_FAILURES 3
561 #endif 
562
563 #ifndef SBRK_FAILURE_PRICE
564 #  define SBRK_FAILURE_PRICE 50
565 #endif 
566
567 #if defined(PERL_EMERGENCY_SBRK) && defined(PERL_CORE)
568
569 #  ifndef BIG_SIZE
570 #    define BIG_SIZE (1<<16)            /* 64K */
571 #  endif 
572
573 static char *emergency_buffer;
574 static MEM_SIZE emergency_buffer_size;
575
576 static Malloc_t
577 emergency_sbrk(size)
578     MEM_SIZE size;
579 {
580     MEM_SIZE rsize = (((size - 1)>>LOG_OF_MIN_ARENA) + 1)<<LOG_OF_MIN_ARENA;
581
582     if (size >= BIG_SIZE) {
583         /* Give the possibility to recover: */
584         MUTEX_UNLOCK(&PL_malloc_mutex);
585         croak("Out of memory during \"large\" request for %i bytes", size);
586     }
587
588     if (emergency_buffer_size >= rsize) {
589         char *old = emergency_buffer;
590         
591         emergency_buffer_size -= rsize;
592         emergency_buffer += rsize;
593         return old;
594     } else {            
595         dTHR;
596         /* First offense, give a possibility to recover by dieing. */
597         /* No malloc involved here: */
598         GV **gvp = (GV**)hv_fetch(PL_defstash, "^M", 2, 0);
599         SV *sv;
600         char *pv;
601         int have = 0;
602
603         if (emergency_buffer_size) {
604             add_to_chain(emergency_buffer, emergency_buffer_size, 0);
605             emergency_buffer_size = 0;
606             emergency_buffer = Nullch;
607             have = 1;
608         }
609         if (!gvp) gvp = (GV**)hv_fetch(PL_defstash, "\015", 1, 0);
610         if (!gvp || !(sv = GvSV(*gvp)) || !SvPOK(sv) 
611             || (SvLEN(sv) < (1<<LOG_OF_MIN_ARENA) - M_OVERHEAD)) {
612             if (have)
613                 goto do_croak;
614             return (char *)-1;          /* Now die die die... */
615         }
616         /* Got it, now detach SvPV: */
617         pv = SvPV(sv, PL_na);
618         /* Check alignment: */
619         if (((UV)(pv - sizeof(union overhead))) & ((1<<LOG_OF_MIN_ARENA) - 1)) {
620             PerlIO_puts(PerlIO_stderr(),"Bad alignment of $^M!\n");
621             return (char *)-1;          /* die die die */
622         }
623
624         emergency_buffer = pv - sizeof(union overhead);
625         emergency_buffer_size = malloced_size(pv) + M_OVERHEAD;
626         SvPOK_off(sv);
627         SvPVX(sv) = Nullch;
628         SvCUR(sv) = SvLEN(sv) = 0;
629     }
630   do_croak:
631     MUTEX_UNLOCK(&PL_malloc_mutex);
632     croak("Out of memory during request for %i bytes", size);
633 }
634
635 #else /* !(defined(PERL_EMERGENCY_SBRK) && defined(PERL_CORE)) */
636 #  define emergency_sbrk(size)  -1
637 #endif /* !(defined(PERL_EMERGENCY_SBRK) && defined(PERL_CORE)) */
638
639 /*
640  * nextf[i] is the pointer to the next free block of size 2^i.  The
641  * smallest allocatable block is 8 bytes.  The overhead information
642  * precedes the data area returned to the user.
643  */
644 #define NBUCKETS (32*BUCKETS_PER_POW2 + 1)
645 static  union overhead *nextf[NBUCKETS];
646
647 #ifdef USE_PERL_SBRK
648 #define sbrk(a) Perl_sbrk(a)
649 Malloc_t Perl_sbrk _((int size));
650 #else 
651 #ifdef DONT_DECLARE_STD
652 #ifdef I_UNISTD
653 #include <unistd.h>
654 #endif
655 #else
656 extern  Malloc_t sbrk(int);
657 #endif
658 #endif
659
660 #ifdef DEBUGGING_MSTATS
661 /*
662  * nmalloc[i] is the difference between the number of mallocs and frees
663  * for a given block size.
664  */
665 static  u_int nmalloc[NBUCKETS];
666 static  u_int sbrk_slack;
667 static  u_int start_slack;
668 #endif
669
670 static  u_int goodsbrk;
671
672 #ifdef DEBUGGING
673 #define ASSERT(p,diag)   if (!(p)) botch(diag,STRINGIFY(p));  else
674 static void
675 botch(char *diag, char *s)
676 {
677         PerlIO_printf(PerlIO_stderr(), "assertion botched (%s?): %s\n", diag, s);
678         PerlProc_abort();
679 }
680 #else
681 #define ASSERT(p, diag)
682 #endif
683
684 Malloc_t
685 malloc(register size_t nbytes)
686 {
687         register union overhead *p;
688         register int bucket;
689         register MEM_SIZE shiftr;
690
691 #if defined(DEBUGGING) || defined(RCHECK)
692         MEM_SIZE size = nbytes;
693 #endif
694
695         BARK_64K_LIMIT("Allocation",nbytes,nbytes);
696 #ifdef DEBUGGING
697         if ((long)nbytes < 0)
698                 croak("%s", "panic: malloc");
699 #endif
700
701         MUTEX_LOCK(&PL_malloc_mutex);
702         /*
703          * Convert amount of memory requested into
704          * closest block size stored in hash buckets
705          * which satisfies request.  Account for
706          * space used per block for accounting.
707          */
708 #ifdef PACK_MALLOC
709 #  ifdef SMALL_BUCKET_VIA_TABLE
710         if (nbytes == 0)
711             bucket = MIN_BUCKET;
712         else if (nbytes <= SIZE_TABLE_MAX) {
713             bucket = bucket_of[(nbytes - 1) >> BUCKET_TABLE_SHIFT];
714         } else
715 #  else
716         if (nbytes == 0)
717             nbytes = 1;
718         if (nbytes <= MAX_POW2_ALGO) goto do_shifts;
719         else
720 #  endif
721 #endif 
722         {
723             POW2_OPTIMIZE_ADJUST(nbytes);
724             nbytes += M_OVERHEAD;
725             nbytes = (nbytes + 3) &~ 3; 
726           do_shifts:
727             shiftr = (nbytes - 1) >> START_SHIFT;
728             bucket = START_SHIFTS_BUCKET;
729             /* apart from this loop, this is O(1) */
730             while (shiftr >>= 1)
731                 bucket += BUCKETS_PER_POW2;
732         }
733         /*
734          * If nothing in hash bucket right now,
735          * request more memory from the system.
736          */
737         if (nextf[bucket] == NULL)    
738                 morecore(bucket);
739         if ((p = nextf[bucket]) == NULL) {
740                 MUTEX_UNLOCK(&PL_malloc_mutex);
741 #ifdef PERL_CORE
742                 if (!PL_nomemok) {
743                     PerlIO_puts(PerlIO_stderr(),"Out of memory!\n");
744                     my_exit(1);
745                 }
746 #else
747                 return (NULL);
748 #endif
749         }
750
751         DEBUG_m(PerlIO_printf(Perl_debug_log,
752                               "0x%lx: (%05lu) malloc %ld bytes\n",
753                               (unsigned long)(p+1), (unsigned long)(PL_an++),
754                               (long)size));
755
756         /* remove from linked list */
757 #if defined(RCHECK)
758         if (((UV)p) & (MEM_ALIGNBYTES - 1))
759             PerlIO_printf(PerlIO_stderr(), "Corrupt malloc ptr 0x%lx at 0x%lx\n",
760                 (unsigned long)*((int*)p),(unsigned long)p);
761 #endif
762         nextf[bucket] = p->ov_next;
763 #ifdef IGNORE_SMALL_BAD_FREE
764         if (bucket >= FIRST_BUCKET_WITH_CHECK)
765 #endif 
766             OV_MAGIC(p, bucket) = MAGIC;
767 #ifndef PACK_MALLOC
768         OV_INDEX(p) = bucket;
769 #endif
770 #ifdef RCHECK
771         /*
772          * Record allocated size of block and
773          * bound space with magic numbers.
774          */
775         p->ov_rmagic = RMAGIC;
776         if (bucket <= MAX_SHORT_BUCKET) {
777             int i;
778             
779             nbytes = size + M_OVERHEAD; 
780             p->ov_size = nbytes - 1;
781             if ((i = nbytes & 3)) {
782                 i = 4 - i;
783                 while (i--)
784                     *((char *)((caddr_t)p + nbytes - RSLOP + i)) = RMAGIC_C;
785             }
786             nbytes = (nbytes + 3) &~ 3; 
787             *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC;
788         }
789 #endif
790         MUTEX_UNLOCK(&PL_malloc_mutex);
791         return ((Malloc_t)(p + CHUNK_SHIFT));
792 }
793
794 static char *last_sbrk_top;
795 static char *last_op;                   /* This arena can be easily extended. */
796 static int sbrked_remains;
797 static int sbrk_good = SBRK_ALLOW_FAILURES * SBRK_FAILURE_PRICE;
798
799 #ifdef DEBUGGING_MSTATS
800 static int sbrks;
801 #endif 
802
803 struct chunk_chain_s {
804     struct chunk_chain_s *next;
805     MEM_SIZE size;
806 };
807 static struct chunk_chain_s *chunk_chain;
808 static int n_chunks;
809 static char max_bucket;
810
811 /* Cutoff a piece of one of the chunks in the chain.  Prefer smaller chunk. */
812 static void *
813 get_from_chain(MEM_SIZE size)
814 {
815     struct chunk_chain_s *elt = chunk_chain, **oldp = &chunk_chain;
816     struct chunk_chain_s **oldgoodp = NULL;
817     long min_remain = LONG_MAX;
818
819     while (elt) {
820         if (elt->size >= size) {
821             long remains = elt->size - size;
822             if (remains >= 0 && remains < min_remain) {
823                 oldgoodp = oldp;
824                 min_remain = remains;
825             }
826             if (remains == 0) {
827                 break;
828             }
829         }
830         oldp = &( elt->next );
831         elt = elt->next;
832     }
833     if (!oldgoodp) return NULL;
834     if (min_remain) {
835         void *ret = *oldgoodp;
836         struct chunk_chain_s *next = (*oldgoodp)->next;
837         
838         *oldgoodp = (struct chunk_chain_s *)((char*)ret + size);
839         (*oldgoodp)->size = min_remain;
840         (*oldgoodp)->next = next;
841         return ret;
842     } else {
843         void *ret = *oldgoodp;
844         *oldgoodp = (*oldgoodp)->next;
845         n_chunks--;
846         return ret;
847     }
848 }
849
850 static void
851 add_to_chain(void *p, MEM_SIZE size, MEM_SIZE chip)
852 {
853     struct chunk_chain_s *next = chunk_chain;
854     char *cp = (char*)p;
855     
856     cp += chip;
857     chunk_chain = (struct chunk_chain_s *)cp;
858     chunk_chain->size = size - chip;
859     chunk_chain->next = next;
860     n_chunks++;
861 }
862
863 static void *
864 get_from_bigger_buckets(int bucket, MEM_SIZE size)
865 {
866     int price = 1;
867     static int bucketprice[NBUCKETS];
868     while (bucket <= max_bucket) {
869         /* We postpone stealing from bigger buckets until we want it
870            often enough. */
871         if (nextf[bucket] && bucketprice[bucket]++ >= price) {
872             /* Steal it! */
873             void *ret = (void*)(nextf[bucket] - 1 + CHUNK_SHIFT);
874             bucketprice[bucket] = 0;
875             if (((char*)nextf[bucket]) - M_OVERHEAD == last_op) {
876                 last_op = NULL;         /* Disable optimization */
877             }
878             nextf[bucket] = nextf[bucket]->ov_next;
879 #ifdef DEBUGGING_MSTATS
880             nmalloc[bucket]--;
881             start_slack -= M_OVERHEAD;
882 #endif 
883             add_to_chain(ret, (BUCKET_SIZE(bucket) +
884                                POW2_OPTIMIZE_SURPLUS(bucket)), 
885                          size);
886             return ret;
887         }
888         bucket++;
889     }
890     return NULL;
891 }
892
893 static union overhead *
894 getpages(int needed, int *nblksp, int bucket)
895 {
896     /* Need to do (possibly expensive) system call. Try to
897        optimize it for rare calling. */
898     MEM_SIZE require = needed - sbrked_remains;
899     char *cp;
900     union overhead *ovp;
901     int slack = 0;
902
903     if (sbrk_good > 0) {
904         if (!last_sbrk_top && require < FIRST_SBRK) 
905             require = FIRST_SBRK;
906         else if (require < MIN_SBRK) require = MIN_SBRK;
907
908         if (require < goodsbrk * MIN_SBRK_FRAC / 100)
909             require = goodsbrk * MIN_SBRK_FRAC / 100;
910         require = ((require - 1 + MIN_SBRK) / MIN_SBRK) * MIN_SBRK;
911     } else {
912         require = needed;
913         last_sbrk_top = 0;
914         sbrked_remains = 0;
915     }
916
917     DEBUG_m(PerlIO_printf(Perl_debug_log, 
918                           "sbrk(%ld) for %ld-byte-long arena\n",
919                           (long)require, (long) needed));
920     cp = (char *)sbrk(require);
921 #ifdef DEBUGGING_MSTATS
922     sbrks++;
923 #endif 
924     if (cp == last_sbrk_top) {
925         /* Common case, anything is fine. */
926         sbrk_good++;
927         ovp = (union overhead *) (cp - sbrked_remains);
928         sbrked_remains = require - (needed - sbrked_remains);
929     } else if (cp == (char *)-1) { /* no more room! */
930         ovp = (union overhead *)emergency_sbrk(needed);
931         if (ovp == (union overhead *)-1)
932             return 0;
933         return ovp;
934     } else {                    /* Non-continuous or first sbrk(). */
935         long add = sbrked_remains;
936         char *newcp;
937
938         if (sbrked_remains) {   /* Put rest into chain, we
939                                    cannot use it right now. */
940             add_to_chain((void*)(last_sbrk_top - sbrked_remains),
941                          sbrked_remains, 0);
942         }
943
944         /* Second, check alignment. */
945         slack = 0;
946
947 #ifndef atarist /* on the atari we dont have to worry about this */
948 #  ifndef I286  /* The sbrk(0) call on the I286 always returns the next segment */
949
950         /* CHUNK_SHIFT is 1 for PACK_MALLOC, 0 otherwise. */
951         if ((UV)cp & (0x7FF >> CHUNK_SHIFT)) { /* Not aligned. */
952             slack = (0x800 >> CHUNK_SHIFT)
953                 - ((UV)cp & (0x7FF >> CHUNK_SHIFT));
954             add += slack;
955         }
956 #  endif
957 #endif /* atarist */
958                 
959         if (add) {
960             DEBUG_m(PerlIO_printf(Perl_debug_log, 
961                                   "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",
962                                   (long)add, (long) slack,
963                                   (long) sbrked_remains));
964             newcp = (char *)sbrk(add);
965 #if defined(DEBUGGING_MSTATS)
966             sbrks++;
967             sbrk_slack += add;
968 #endif
969             if (newcp != cp + require) {
970                 /* Too bad: even rounding sbrk() is not continuous.*/
971                 DEBUG_m(PerlIO_printf(Perl_debug_log, 
972                                       "failed to fix bad sbrk()\n"));
973 #ifdef PACK_MALLOC
974                 if (slack) {
975                     MUTEX_UNLOCK(&PL_malloc_mutex);
976                     croak("%s", "panic: Off-page sbrk");
977                 }
978 #endif
979                 if (sbrked_remains) {
980                     /* Try again. */
981 #if defined(DEBUGGING_MSTATS)
982                     sbrk_slack += require;
983 #endif
984                     require = needed;
985                     DEBUG_m(PerlIO_printf(Perl_debug_log, 
986                                           "straight sbrk(%ld)\n",
987                                           (long)require));
988                     cp = (char *)sbrk(require);
989 #ifdef DEBUGGING_MSTATS
990                     sbrks++;
991 #endif 
992                     if (cp == (char *)-1)
993                         return 0;
994                 }
995                 sbrk_good = -1; /* Disable optimization!
996                                    Continue with not-aligned... */
997             } else {
998                 cp += slack;
999                 require += sbrked_remains;
1000             }
1001         }
1002
1003         if (last_sbrk_top) {
1004             sbrk_good -= SBRK_FAILURE_PRICE;
1005         }
1006
1007         ovp = (union overhead *) cp;
1008         /*
1009          * Round up to minimum allocation size boundary
1010          * and deduct from block count to reflect.
1011          */
1012
1013 #ifndef I286    /* Again, this should always be ok on an 80286 */
1014         if ((UV)ovp & 7) {
1015             ovp = (union overhead *)(((UV)ovp + 8) & ~7);
1016             DEBUG_m(PerlIO_printf(Perl_debug_log, 
1017                                   "fixing sbrk(): %d bytes off machine alignement\n",
1018                                   (int)((UV)ovp & 7)));
1019             (*nblksp)--;
1020 # if defined(DEBUGGING_MSTATS)
1021             /* This is only approx. if TWO_POT_OPTIMIZE: */
1022             sbrk_slack += (1 << bucket);
1023 # endif
1024         }
1025 #endif
1026         sbrked_remains = require - needed;
1027     }
1028     last_sbrk_top = cp + require;
1029     last_op = (char*) cp;
1030 #ifdef DEBUGGING_MSTATS
1031     goodsbrk += require;
1032 #endif  
1033     return ovp;
1034 }
1035
1036 static int
1037 getpages_adjacent(int require)
1038 {           
1039     if (require <= sbrked_remains) {
1040         sbrked_remains -= require;
1041     } else {
1042         char *cp;
1043
1044         require -= sbrked_remains;
1045         /* We do not try to optimize sbrks here, we go for place. */
1046         cp = (char*) sbrk(require);
1047 #ifdef DEBUGGING_MSTATS
1048         sbrks++;
1049         goodsbrk += require;
1050 #endif 
1051         if (cp == last_sbrk_top) {
1052             sbrked_remains = 0;
1053             last_sbrk_top = cp + require;
1054         } else {
1055             if (cp == (char*)-1) {      /* Out of memory */
1056 #ifdef DEBUGGING_MSTATS
1057                 goodsbrk -= require;
1058 #endif
1059                 return 0;
1060             }
1061             /* Report the failure: */
1062             if (sbrked_remains)
1063                 add_to_chain((void*)(last_sbrk_top - sbrked_remains),
1064                              sbrked_remains, 0);
1065             add_to_chain((void*)cp, require, 0);
1066             sbrk_good -= SBRK_FAILURE_PRICE;
1067             sbrked_remains = 0;
1068             last_sbrk_top = 0;
1069             last_op = 0;
1070             return 0;
1071         }
1072     }
1073             
1074     return 1;
1075 }
1076
1077 /*
1078  * Allocate more memory to the indicated bucket.
1079  */
1080 static void
1081 morecore(register int bucket)
1082 {
1083         register union overhead *ovp;
1084         register int rnu;       /* 2^rnu bytes will be requested */
1085         int nblks;              /* become nblks blocks of the desired size */
1086         register MEM_SIZE siz, needed;
1087
1088         if (nextf[bucket])
1089                 return;
1090         if (bucket == sizeof(MEM_SIZE)*8*BUCKETS_PER_POW2) {
1091             MUTEX_UNLOCK(&PL_malloc_mutex);
1092             croak("%s", "Out of memory during ridiculously large request");
1093         }
1094         if (bucket > max_bucket)
1095             max_bucket = bucket;
1096
1097         rnu = ( (bucket <= (LOG_OF_MIN_ARENA << BUCKET_POW2_SHIFT)) 
1098                 ? LOG_OF_MIN_ARENA 
1099                 : (bucket >> BUCKET_POW2_SHIFT) );
1100         /* This may be overwritten later: */
1101         nblks = 1 << (rnu - (bucket >> BUCKET_POW2_SHIFT)); /* how many blocks to get */
1102         needed = ((MEM_SIZE)1 << rnu) + POW2_OPTIMIZE_SURPLUS(bucket);
1103         if (nextf[rnu << BUCKET_POW2_SHIFT]) { /* 2048b bucket. */
1104             ovp = nextf[rnu << BUCKET_POW2_SHIFT] - 1 + CHUNK_SHIFT;
1105             nextf[rnu << BUCKET_POW2_SHIFT]
1106                 = nextf[rnu << BUCKET_POW2_SHIFT]->ov_next;
1107 #ifdef DEBUGGING_MSTATS
1108             nmalloc[rnu << BUCKET_POW2_SHIFT]--;
1109             start_slack -= M_OVERHEAD;
1110 #endif 
1111             DEBUG_m(PerlIO_printf(Perl_debug_log, 
1112                                   "stealing %ld bytes from %ld arena\n",
1113                                   (long) needed, (long) rnu << BUCKET_POW2_SHIFT));
1114         } else if (chunk_chain 
1115                    && (ovp = (union overhead*) get_from_chain(needed))) {
1116             DEBUG_m(PerlIO_printf(Perl_debug_log, 
1117                                   "stealing %ld bytes from chain\n",
1118                                   (long) needed));
1119         } else if ( (ovp = (union overhead*)
1120                      get_from_bigger_buckets((rnu << BUCKET_POW2_SHIFT) + 1,
1121                                              needed)) ) {
1122             DEBUG_m(PerlIO_printf(Perl_debug_log, 
1123                                   "stealing %ld bytes from bigger buckets\n",
1124                                   (long) needed));
1125         } else if (needed <= sbrked_remains) {
1126             ovp = (union overhead *)(last_sbrk_top - sbrked_remains);
1127             sbrked_remains -= needed;
1128             last_op = (char*)ovp;
1129         } else 
1130             ovp = getpages(needed, &nblks, bucket);
1131
1132         if (!ovp)
1133             return;
1134
1135         /*
1136          * Add new memory allocated to that on
1137          * free list for this hash bucket.
1138          */
1139         siz = BUCKET_SIZE(bucket);
1140 #ifdef PACK_MALLOC
1141         *(u_char*)ovp = bucket; /* Fill index. */
1142         if (bucket <= MAX_PACKED) {
1143             ovp = (union overhead *) ((char*)ovp + BLK_SHIFT(bucket));
1144             nblks = N_BLKS(bucket);
1145 #  ifdef DEBUGGING_MSTATS
1146             start_slack += BLK_SHIFT(bucket);
1147 #  endif
1148         } else if (bucket < LOG_OF_MIN_ARENA * BUCKETS_PER_POW2) {
1149             ovp = (union overhead *) ((char*)ovp + BLK_SHIFT(bucket));
1150             siz -= sizeof(union overhead);
1151         } else ovp++;           /* One chunk per block. */
1152 #endif /* PACK_MALLOC */
1153         nextf[bucket] = ovp;
1154 #ifdef DEBUGGING_MSTATS
1155         nmalloc[bucket] += nblks;
1156         if (bucket > MAX_PACKED) {
1157             start_slack += M_OVERHEAD * nblks;
1158         }
1159 #endif 
1160         while (--nblks > 0) {
1161                 ovp->ov_next = (union overhead *)((caddr_t)ovp + siz);
1162                 ovp = (union overhead *)((caddr_t)ovp + siz);
1163         }
1164         /* Not all sbrks return zeroed memory.*/
1165         ovp->ov_next = (union overhead *)NULL;
1166 #ifdef PACK_MALLOC
1167         if (bucket == 7*BUCKETS_PER_POW2) { /* Special case, explanation is above. */
1168             union overhead *n_op = nextf[7*BUCKETS_PER_POW2]->ov_next;
1169             nextf[7*BUCKETS_PER_POW2] = 
1170                 (union overhead *)((caddr_t)nextf[7*BUCKETS_PER_POW2] 
1171                                    - sizeof(union overhead));
1172             nextf[7*BUCKETS_PER_POW2]->ov_next = n_op;
1173         }
1174 #endif /* !PACK_MALLOC */
1175 }
1176
1177 Free_t
1178 free(void *mp)
1179 {   
1180         register MEM_SIZE size;
1181         register union overhead *ovp;
1182         char *cp = (char*)mp;
1183 #ifdef PACK_MALLOC
1184         u_char bucket;
1185 #endif 
1186
1187         DEBUG_m(PerlIO_printf(Perl_debug_log, 
1188                               "0x%lx: (%05lu) free\n",
1189                               (unsigned long)cp, (unsigned long)(PL_an++)));
1190
1191         if (cp == NULL)
1192                 return;
1193         ovp = (union overhead *)((caddr_t)cp 
1194                                 - sizeof (union overhead) * CHUNK_SHIFT);
1195 #ifdef PACK_MALLOC
1196         bucket = OV_INDEX(ovp);
1197 #endif 
1198 #ifdef IGNORE_SMALL_BAD_FREE
1199         if ((bucket >= FIRST_BUCKET_WITH_CHECK) 
1200             && (OV_MAGIC(ovp, bucket) != MAGIC))
1201 #else
1202         if (OV_MAGIC(ovp, bucket) != MAGIC)
1203 #endif 
1204             {
1205                 static int bad_free_warn = -1;
1206                 if (bad_free_warn == -1) {
1207                     char *pbf = PerlEnv_getenv("PERL_BADFREE");
1208                     bad_free_warn = (pbf) ? atoi(pbf) : 1;
1209                 }
1210                 if (!bad_free_warn)
1211                     return;
1212 #ifdef RCHECK
1213                 warn("%s free() ignored",
1214                     ovp->ov_rmagic == RMAGIC - 1 ? "Duplicate" : "Bad");
1215 #else
1216                 warn("%s", "Bad free() ignored");
1217 #endif
1218                 return;                         /* sanity */
1219             }
1220         MUTEX_LOCK(&PL_malloc_mutex);
1221 #ifdef RCHECK
1222         ASSERT(ovp->ov_rmagic == RMAGIC, "chunk's head overwrite");
1223         if (OV_INDEX(ovp) <= MAX_SHORT_BUCKET) {
1224             int i;
1225             MEM_SIZE nbytes = ovp->ov_size + 1;
1226
1227             if ((i = nbytes & 3)) {
1228                 i = 4 - i;
1229                 while (i--) {
1230                     ASSERT(*((char *)((caddr_t)ovp + nbytes - RSLOP + i))
1231                            == RMAGIC_C, "chunk's tail overwrite");
1232                 }
1233             }
1234             nbytes = (nbytes + 3) &~ 3; 
1235             ASSERT(*(u_int *)((caddr_t)ovp + nbytes - RSLOP) == RMAGIC, "chunk's tail overwrite");          
1236         }
1237         ovp->ov_rmagic = RMAGIC - 1;
1238 #endif
1239         ASSERT(OV_INDEX(ovp) < NBUCKETS, "chunk's head overwrite");
1240         size = OV_INDEX(ovp);
1241         ovp->ov_next = nextf[size];
1242         nextf[size] = ovp;
1243         MUTEX_UNLOCK(&PL_malloc_mutex);
1244 }
1245
1246 /*
1247  * When a program attempts "storage compaction" as mentioned in the
1248  * old malloc man page, it realloc's an already freed block.  Usually
1249  * this is the last block it freed; occasionally it might be farther
1250  * back.  We have to search all the free lists for the block in order
1251  * to determine its bucket: 1st we make one pass thru the lists
1252  * checking only the first block in each; if that fails we search
1253  * ``reall_srchlen'' blocks in each list for a match (the variable
1254  * is extern so the caller can modify it).  If that fails we just copy
1255  * however many bytes was given to realloc() and hope it's not huge.
1256  */
1257 int reall_srchlen = 4;  /* 4 should be plenty, -1 =>'s whole list */
1258
1259 Malloc_t
1260 realloc(void *mp, size_t nbytes)
1261 {   
1262         register MEM_SIZE onb;
1263         union overhead *ovp;
1264         char *res;
1265         int prev_bucket;
1266         register int bucket;
1267         int was_alloced = 0, incr;
1268         char *cp = (char*)mp;
1269
1270 #if defined(DEBUGGING) || !defined(PERL_CORE)
1271         MEM_SIZE size = nbytes;
1272
1273         if ((long)nbytes < 0)
1274                 croak("%s", "panic: realloc");
1275 #endif
1276
1277         BARK_64K_LIMIT("Reallocation",nbytes,size);
1278         if (!cp)
1279                 return malloc(nbytes);
1280
1281         MUTEX_LOCK(&PL_malloc_mutex);
1282         ovp = (union overhead *)((caddr_t)cp 
1283                                 - sizeof (union overhead) * CHUNK_SHIFT);
1284         bucket = OV_INDEX(ovp);
1285 #ifdef IGNORE_SMALL_BAD_FREE
1286         if ((bucket < FIRST_BUCKET_WITH_CHECK) 
1287             || (OV_MAGIC(ovp, bucket) == MAGIC))
1288 #else
1289         if (OV_MAGIC(ovp, bucket) == MAGIC) 
1290 #endif 
1291         {
1292                 was_alloced = 1;
1293         } else {
1294                 /*
1295                  * Already free, doing "compaction".
1296                  *
1297                  * Search for the old block of memory on the
1298                  * free list.  First, check the most common
1299                  * case (last element free'd), then (this failing)
1300                  * the last ``reall_srchlen'' items free'd.
1301                  * If all lookups fail, then assume the size of
1302                  * the memory block being realloc'd is the
1303                  * smallest possible.
1304                  */
1305                 if ((bucket = findbucket(ovp, 1)) < 0 &&
1306                     (bucket = findbucket(ovp, reall_srchlen)) < 0)
1307                         bucket = 0;
1308         }
1309         onb = BUCKET_SIZE_REAL(bucket);
1310         /* 
1311          *  avoid the copy if same size block.
1312          *  We are not agressive with boundary cases. Note that it might
1313          *  (for a small number of cases) give false negative if
1314          *  both new size and old one are in the bucket for
1315          *  FIRST_BIG_POW2, but the new one is near the lower end.
1316          *
1317          *  We do not try to go to 1.5 times smaller bucket so far.
1318          */
1319         if (nbytes > onb) incr = 1;
1320         else {
1321 #ifdef DO_NOT_TRY_HARDER_WHEN_SHRINKING
1322             if ( /* This is a little bit pessimal if PACK_MALLOC: */
1323                 nbytes > ( (onb >> 1) - M_OVERHEAD )
1324 #  ifdef TWO_POT_OPTIMIZE
1325                 || (bucket == FIRST_BIG_POW2 && nbytes >= LAST_SMALL_BOUND )
1326 #  endif        
1327                 )
1328 #else  /* !DO_NOT_TRY_HARDER_WHEN_SHRINKING */
1329                 prev_bucket = ( (bucket > MAX_PACKED + 1) 
1330                                 ? bucket - BUCKETS_PER_POW2
1331                                 : bucket - 1);
1332              if (nbytes > BUCKET_SIZE_REAL(prev_bucket))
1333 #endif /* !DO_NOT_TRY_HARDER_WHEN_SHRINKING */
1334                  incr = 0;
1335              else incr = -1;
1336         }
1337         if (!was_alloced
1338 #ifdef STRESS_REALLOC
1339             || 1 /* always do it the hard way */
1340 #endif
1341             ) goto hard_way;
1342         else if (incr == 0) {
1343           inplace_label:
1344 #ifdef RCHECK
1345                 /*
1346                  * Record new allocated size of block and
1347                  * bound space with magic numbers.
1348                  */
1349                 if (OV_INDEX(ovp) <= MAX_SHORT_BUCKET) {
1350                        int i, nb = ovp->ov_size + 1;
1351
1352                        if ((i = nb & 3)) {
1353                            i = 4 - i;
1354                            while (i--) {
1355                                ASSERT(*((char *)((caddr_t)ovp + nb - RSLOP + i)) == RMAGIC_C, "chunk's tail overwrite");
1356                            }
1357                        }
1358                        nb = (nb + 3) &~ 3; 
1359                        ASSERT(*(u_int *)((caddr_t)ovp + nb - RSLOP) == RMAGIC, "chunk's tail overwrite");
1360                         /*
1361                          * Convert amount of memory requested into
1362                          * closest block size stored in hash buckets
1363                          * which satisfies request.  Account for
1364                          * space used per block for accounting.
1365                          */
1366                         nbytes += M_OVERHEAD;
1367                         ovp->ov_size = nbytes - 1;
1368                         if ((i = nbytes & 3)) {
1369                             i = 4 - i;
1370                             while (i--)
1371                                 *((char *)((caddr_t)ovp + nbytes - RSLOP + i))
1372                                     = RMAGIC_C;
1373                         }
1374                         nbytes = (nbytes + 3) &~ 3; 
1375                         *((u_int *)((caddr_t)ovp + nbytes - RSLOP)) = RMAGIC;
1376                 }
1377 #endif
1378                 res = cp;
1379                 MUTEX_UNLOCK(&PL_malloc_mutex);
1380                 DEBUG_m(PerlIO_printf(Perl_debug_log, 
1381                               "0x%lx: (%05lu) realloc %ld bytes inplace\n",
1382                               (unsigned long)res,(unsigned long)(PL_an++),
1383                               (long)size));
1384         } else if (incr == 1 && (cp - M_OVERHEAD == last_op) 
1385                    && (onb > (1 << LOG_OF_MIN_ARENA))) {
1386             MEM_SIZE require, newarena = nbytes, pow;
1387             int shiftr;
1388
1389             POW2_OPTIMIZE_ADJUST(newarena);
1390             newarena = newarena + M_OVERHEAD;
1391             /* newarena = (newarena + 3) &~ 3; */
1392             shiftr = (newarena - 1) >> LOG_OF_MIN_ARENA;
1393             pow = LOG_OF_MIN_ARENA + 1;
1394             /* apart from this loop, this is O(1) */
1395             while (shiftr >>= 1)
1396                 pow++;
1397             newarena = (1 << pow) + POW2_OPTIMIZE_SURPLUS(pow * BUCKETS_PER_POW2);
1398             require = newarena - onb - M_OVERHEAD;
1399             
1400             if (getpages_adjacent(require)) {
1401 #ifdef DEBUGGING_MSTATS
1402                 nmalloc[bucket]--;
1403                 nmalloc[pow * BUCKETS_PER_POW2]++;
1404 #endif      
1405                 *(cp - M_OVERHEAD) = pow * BUCKETS_PER_POW2; /* Fill index. */
1406                 goto inplace_label;
1407             } else
1408                 goto hard_way;
1409         } else {
1410           hard_way:
1411             MUTEX_UNLOCK(&PL_malloc_mutex);
1412             DEBUG_m(PerlIO_printf(Perl_debug_log, 
1413                               "0x%lx: (%05lu) realloc %ld bytes the hard way\n",
1414                               (unsigned long)cp,(unsigned long)(PL_an++),
1415                               (long)size));
1416             if ((res = (char*)malloc(nbytes)) == NULL)
1417                 return (NULL);
1418             if (cp != res)                      /* common optimization */
1419                 Copy(cp, res, (MEM_SIZE)(nbytes<onb?nbytes:onb), char);
1420             if (was_alloced)
1421                 free(cp);
1422         }
1423         return ((Malloc_t)res);
1424 }
1425
1426 /*
1427  * Search ``srchlen'' elements of each free list for a block whose
1428  * header starts at ``freep''.  If srchlen is -1 search the whole list.
1429  * Return bucket number, or -1 if not found.
1430  */
1431 static int
1432 findbucket(union overhead *freep, int srchlen)
1433 {
1434         register union overhead *p;
1435         register int i, j;
1436
1437         for (i = 0; i < NBUCKETS; i++) {
1438                 j = 0;
1439                 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
1440                         if (p == freep)
1441                                 return (i);
1442                         j++;
1443                 }
1444         }
1445         return (-1);
1446 }
1447
1448 Malloc_t
1449 calloc(register size_t elements, register size_t size)
1450 {
1451     long sz = elements * size;
1452     Malloc_t p = malloc(sz);
1453
1454     if (p) {
1455         memset((void*)p, 0, sz);
1456     }
1457     return p;
1458 }
1459
1460 MEM_SIZE
1461 malloced_size(void *p)
1462 {
1463     union overhead *ovp = (union overhead *)
1464         ((caddr_t)p - sizeof (union overhead) * CHUNK_SHIFT);
1465     int bucket = OV_INDEX(ovp);
1466 #ifdef RCHECK
1467     /* The caller wants to have a complete control over the chunk,
1468        disable the memory checking inside the chunk.  */
1469     if (bucket <= MAX_SHORT_BUCKET) {
1470         MEM_SIZE size = BUCKET_SIZE_REAL(bucket);
1471         ovp->ov_size = size + M_OVERHEAD - 1;
1472         *((u_int *)((caddr_t)ovp + size + M_OVERHEAD - RSLOP)) = RMAGIC;
1473     }
1474 #endif
1475     return BUCKET_SIZE_REAL(bucket);
1476 }
1477
1478 #ifdef DEBUGGING_MSTATS
1479
1480 #  ifdef BUCKETS_ROOT2
1481 #    define MIN_EVEN_REPORT 6
1482 #  else
1483 #    define MIN_EVEN_REPORT MIN_BUCKET
1484 #  endif 
1485 /*
1486  * mstats - print out statistics about malloc
1487  * 
1488  * Prints two lines of numbers, one showing the length of the free list
1489  * for each size category, the second showing the number of mallocs -
1490  * frees for each size category.
1491  */
1492 void
1493 dump_mstats(char *s)
1494 {
1495         register int i, j;
1496         register union overhead *p;
1497         int topbucket=0, topbucket_ev=0, topbucket_odd=0, totfree=0, total=0;
1498         u_int nfree[NBUCKETS];
1499         int total_chain = 0;
1500         struct chunk_chain_s* nextchain = chunk_chain;
1501
1502         for (i = MIN_BUCKET ; i < NBUCKETS; i++) {
1503                 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
1504                         ;
1505                 nfree[i] = j;
1506                 totfree += nfree[i] * BUCKET_SIZE_REAL(i);
1507                 total += nmalloc[i] * BUCKET_SIZE_REAL(i);
1508                 if (nmalloc[i]) {
1509                     i % 2 ? (topbucket_odd = i) : (topbucket_ev = i);
1510                     topbucket = i;
1511                 }
1512         }
1513         if (s)
1514             PerlIO_printf(PerlIO_stderr(),
1515                           "Memory allocation statistics %s (buckets %ld(%ld)..%ld(%ld)\n",
1516                           s, 
1517                           (long)BUCKET_SIZE_REAL(MIN_BUCKET), 
1518                           (long)BUCKET_SIZE(MIN_BUCKET),
1519                           (long)BUCKET_SIZE_REAL(topbucket), (long)BUCKET_SIZE(topbucket));
1520         PerlIO_printf(PerlIO_stderr(), "%8d free:", totfree);
1521         for (i = MIN_EVEN_REPORT; i <= topbucket; i += BUCKETS_PER_POW2) {
1522                 PerlIO_printf(PerlIO_stderr(), 
1523                               ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
1524                                ? " %5d" 
1525                                : ((i < 12*BUCKETS_PER_POW2) ? " %3d" : " %d")),
1526                               nfree[i]);
1527         }
1528 #ifdef BUCKETS_ROOT2
1529         PerlIO_printf(PerlIO_stderr(), "\n\t   ");
1530         for (i = MIN_BUCKET + 1; i <= topbucket_odd; i += BUCKETS_PER_POW2) {
1531                 PerlIO_printf(PerlIO_stderr(), 
1532                               ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
1533                                ? " %5d" 
1534                                : ((i < 12*BUCKETS_PER_POW2) ? " %3d" : " %d")),
1535                               nfree[i]);
1536         }
1537 #endif 
1538         PerlIO_printf(PerlIO_stderr(), "\n%8d used:", total - totfree);
1539         for (i = MIN_EVEN_REPORT; i <= topbucket; i += BUCKETS_PER_POW2) {
1540                 PerlIO_printf(PerlIO_stderr(), 
1541                               ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
1542                                ? " %5d" 
1543                                : ((i < 12*BUCKETS_PER_POW2) ? " %3d" : " %d")), 
1544                               nmalloc[i] - nfree[i]);
1545         }
1546 #ifdef BUCKETS_ROOT2
1547         PerlIO_printf(PerlIO_stderr(), "\n\t   ");
1548         for (i = MIN_BUCKET + 1; i <= topbucket_odd; i += BUCKETS_PER_POW2) {
1549                 PerlIO_printf(PerlIO_stderr(), 
1550                               ((i < 8*BUCKETS_PER_POW2 || i == 10*BUCKETS_PER_POW2)
1551                                ? " %5d" 
1552                                : ((i < 12*BUCKETS_PER_POW2) ? " %3d" : " %d")),
1553                               nmalloc[i] - nfree[i]);
1554         }
1555 #endif 
1556         while (nextchain) {
1557             total_chain += nextchain->size;
1558             nextchain = nextchain->next;
1559         }
1560         PerlIO_printf(PerlIO_stderr(), "\nTotal sbrk(): %d/%d:%d. Odd ends: pad+heads+chain+tail: %d+%d+%d+%d.\n",
1561                       goodsbrk + sbrk_slack, sbrks, sbrk_good, sbrk_slack,
1562                       start_slack, total_chain, sbrked_remains);
1563 }
1564 #else
1565 void
1566 dump_mstats(char *s)
1567 {
1568 }
1569 #endif
1570 #endif /* lint */
1571
1572
1573 #ifdef USE_PERL_SBRK
1574
1575 #   if defined(__MACHTEN_PPC__) || defined(__NeXT__)
1576 #      define PERL_SBRK_VIA_MALLOC
1577 /*
1578  * MachTen's malloc() returns a buffer aligned on a two-byte boundary.
1579  * While this is adequate, it may slow down access to longer data
1580  * types by forcing multiple memory accesses.  It also causes
1581  * complaints when RCHECK is in force.  So we allocate six bytes
1582  * more than we need to, and return an address rounded up to an
1583  * eight-byte boundary.
1584  *
1585  * 980701 Dominic Dunlop <domo@computer.org>
1586  */
1587 #      define SYSTEM_ALLOC(a) ((void *)(((unsigned)malloc((a)+6)+6)&~7))
1588 #   endif
1589
1590 #   ifdef PERL_SBRK_VIA_MALLOC
1591 #      if defined(HIDEMYMALLOC) || defined(EMBEDMYMALLOC)
1592 #         undef malloc          /* Expose names that  */
1593 #         undef calloc          /* HIDEMYMALLOC hides */
1594 #         undef realloc
1595 #         undef free
1596 #      else
1597 #         include "Error: -DPERL_SBRK_VIA_MALLOC needs -D(HIDE|EMBED)MYMALLOC"
1598 #      endif
1599
1600 /* it may seem schizophrenic to use perl's malloc and let it call system */
1601 /* malloc, the reason for that is only the 3.2 version of the OS that had */
1602 /* frequent core dumps within nxzonefreenolock. This sbrk routine put an */
1603 /* end to the cores */
1604
1605 #      ifndef SYSTEM_ALLOC
1606 #         define SYSTEM_ALLOC(a) malloc(a)
1607 #      endif
1608
1609 #   endif  /* PERL_SBRK_VIA_MALLOC */
1610
1611 static IV Perl_sbrk_oldchunk;
1612 static long Perl_sbrk_oldsize;
1613
1614 #   define PERLSBRK_32_K (1<<15)
1615 #   define PERLSBRK_64_K (1<<16)
1616
1617 Malloc_t
1618 Perl_sbrk(size)
1619 int size;
1620 {
1621     IV got;
1622     int small, reqsize;
1623
1624     if (!size) return 0;
1625 #ifdef PERL_CORE
1626     reqsize = size; /* just for the DEBUG_m statement */
1627 #endif
1628 #ifdef PACK_MALLOC
1629     size = (size + 0x7ff) & ~0x7ff;
1630 #endif
1631     if (size <= Perl_sbrk_oldsize) {
1632         got = Perl_sbrk_oldchunk;
1633         Perl_sbrk_oldchunk += size;
1634         Perl_sbrk_oldsize -= size;
1635     } else {
1636       if (size >= PERLSBRK_32_K) {
1637         small = 0;
1638       } else {
1639         size = PERLSBRK_64_K;
1640         small = 1;
1641       }
1642       got = (IV)SYSTEM_ALLOC(size);
1643 #ifdef PACK_MALLOC
1644       got = (got + 0x7ff) & ~0x7ff;
1645 #endif
1646       if (small) {
1647         /* Chunk is small, register the rest for future allocs. */
1648         Perl_sbrk_oldchunk = got + reqsize;
1649         Perl_sbrk_oldsize = size - reqsize;
1650       }
1651     }
1652
1653     DEBUG_m(PerlIO_printf(Perl_debug_log, "sbrk malloc size %ld (reqsize %ld), left size %ld, give addr 0x%lx\n",
1654                     size, reqsize, Perl_sbrk_oldsize, got));
1655
1656     return (void *)got;
1657 }
1658
1659 #endif /* ! defined USE_PERL_SBRK */