This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove xcv_condp CV field which is no longer used.
[perl5.git] / malloc.c
1 /*    malloc.c
2  *
3  */
4
5 #if defined(PERL_CORE) && !defined(DEBUGGING_MSTATS)
6 #  define DEBUGGING_MSTATS
7 #endif 
8
9 #ifndef lint
10 #  if defined(DEBUGGING) && !defined(NO_RCHECK)
11 #    define RCHECK
12 #  endif
13 /*
14  * malloc.c (Caltech) 2/21/82
15  * Chris Kingsley, kingsley@cit-20.
16  *
17  * This is a very fast storage allocator.  It allocates blocks of a small 
18  * number of different sizes, and keeps free lists of each size.  Blocks that
19  * don't exactly fit are passed up to the next larger size.  In this 
20  * implementation, the available sizes are 2^n-4 (or 2^n-12) bytes long.
21  * If PACK_MALLOC is defined, small blocks are 2^n bytes long.
22  * This is designed for use in a program that uses vast quantities of memory,
23  * but bombs when it runs out. 
24  */
25
26 #include "EXTERN.h"
27 #include "perl.h"
28
29 #ifdef DEBUGGING
30 #undef DEBUG_m
31 #define DEBUG_m(a)  if (debug & 128)   a
32 #endif
33
34 /* I don't much care whether these are defined in sys/types.h--LAW */
35
36 #define u_char unsigned char
37 #define u_int unsigned int
38 #define u_short unsigned short
39
40 /* 286 and atarist like big chunks, which gives too much overhead. */
41 #if (defined(RCHECK) || defined(I286) || defined(atarist)) && defined(PACK_MALLOC)
42 #undef PACK_MALLOC
43 #endif 
44
45
46 /*
47  * The description below is applicable if PACK_MALLOC is not defined.
48  *
49  * The overhead on a block is at least 4 bytes.  When free, this space
50  * contains a pointer to the next free block, and the bottom two bits must
51  * be zero.  When in use, the first byte is set to MAGIC, and the second
52  * byte is the size index.  The remaining bytes are for alignment.
53  * If range checking is enabled and the size of the block fits
54  * in two bytes, then the top two bytes hold the size of the requested block
55  * plus the range checking words, and the header word MINUS ONE.
56  */
57 union   overhead {
58         union   overhead *ov_next;      /* when free */
59 #if MEM_ALIGNBYTES > 4
60         double  strut;                  /* alignment problems */
61 #endif
62         struct {
63                 u_char  ovu_magic;      /* magic number */
64                 u_char  ovu_index;      /* bucket # */
65 #ifdef RCHECK
66                 u_short ovu_size;       /* actual block size */
67                 u_int   ovu_rmagic;     /* range magic number */
68 #endif
69         } ovu;
70 #define ov_magic        ovu.ovu_magic
71 #define ov_index        ovu.ovu_index
72 #define ov_size         ovu.ovu_size
73 #define ov_rmagic       ovu.ovu_rmagic
74 };
75
76 #ifdef DEBUGGING
77 static void botch _((char *s));
78 #endif
79 static void morecore _((int bucket));
80 static int findbucket _((union overhead *freep, int srchlen));
81
82 #define MAGIC           0xff            /* magic # on accounting info */
83 #define RMAGIC          0x55555555      /* magic # on range info */
84 #ifdef RCHECK
85 #  define       RSLOP           sizeof (u_int)
86 #  ifdef TWO_POT_OPTIMIZE
87 #    define MAX_SHORT_BUCKET 12
88 #  else
89 #    define MAX_SHORT_BUCKET 13
90 #  endif 
91 #else
92 #  define       RSLOP           0
93 #endif
94
95 #ifdef PACK_MALLOC
96 /*
97  * In this case it is assumed that if we do sbrk() in 2K units, we
98  * will get 2K aligned blocks. The bucket number of the given subblock is
99  * on the boundary of 2K block which contains the subblock.
100  * Several following bytes contain the magic numbers for the subblocks
101  * in the block.
102  *
103  * Sizes of chunks are powers of 2 for chunks in buckets <=
104  * MAX_PACKED, after this they are (2^n - sizeof(union overhead)) (to
105  * get alignment right).
106  *
107  * We suppose that starts of all the chunks in a 2K block are in
108  * different 2^n-byte-long chunks.  If the top of the last chunk is
109  * aligned on a boundary of 2K block, this means that
110  * sizeof(union overhead)*"number of chunks" < 2^n, or
111  * sizeof(union overhead)*2K < 4^n, or n > 6 + log2(sizeof()/2)/2, if a
112  * chunk of size 2^n - overhead is used. Since this rules out n = 7
113  * for 8 byte alignment, we specialcase allocation of the first of 16
114  * 128-byte-long chunks.
115  *
116  * Note that with the above assumption we automatically have enough
117  * place for MAGIC at the start of 2K block.  Note also that we
118  * overlay union overhead over the chunk, thus the start of the chunk
119  * is immediately overwritten after freeing.
120  */
121 #  define MAX_PACKED 6
122 #  define MAX_2_POT_ALGO ((1<<(MAX_PACKED + 1)) - M_OVERHEAD)
123 #  define TWOK_MASK ((1<<11) - 1)
124 #  define TWOK_MASKED(x) ((u_int)(x) & ~TWOK_MASK)
125 #  define TWOK_SHIFT(x) ((u_int)(x) & TWOK_MASK)
126 #  define OV_INDEXp(block) ((u_char*)(TWOK_MASKED(block)))
127 #  define OV_INDEX(block) (*OV_INDEXp(block))
128 #  define OV_MAGIC(block,bucket) (*(OV_INDEXp(block) +                  \
129                                     (TWOK_SHIFT(block)>>(bucket + 3)) + \
130                                     (bucket > MAX_NONSHIFT ? 1 : 0)))
131 #  define CHUNK_SHIFT 0
132
133 static u_char n_blks[11 - 3]     = {224, 120, 62, 31, 16, 8, 4, 2};
134 static u_short blk_shift[11 - 3] = {256, 128, 64, 32, 
135                                     16*sizeof(union overhead), 
136                                     8*sizeof(union overhead), 
137                                     4*sizeof(union overhead), 
138                                     2*sizeof(union overhead), 
139 #  define MAX_NONSHIFT 2        /* Shift 64 greater than chunk 32. */
140 };
141
142 #else  /* !PACK_MALLOC */
143
144 #  define OV_MAGIC(block,bucket) (block)->ov_magic
145 #  define OV_INDEX(block) (block)->ov_index
146 #  define CHUNK_SHIFT 1
147 #endif /* !PACK_MALLOC */
148
149 #  define M_OVERHEAD (sizeof(union overhead) + RSLOP)
150
151 /*
152  * Big allocations are often of the size 2^n bytes. To make them a
153  * little bit better, make blocks of size 2^n+pagesize for big n.
154  */
155
156 #ifdef TWO_POT_OPTIMIZE
157
158 #  ifndef PERL_PAGESIZE
159 #    define PERL_PAGESIZE 4096
160 #  endif 
161 #  ifndef FIRST_BIG_TWO_POT
162 #    define FIRST_BIG_TWO_POT 14        /* 16K */
163 #  endif
164 #  define FIRST_BIG_BLOCK (1<<FIRST_BIG_TWO_POT) /* 16K */
165 /* If this value or more, check against bigger blocks. */
166 #  define FIRST_BIG_BOUND (FIRST_BIG_BLOCK - M_OVERHEAD)
167 /* If less than this value, goes into 2^n-overhead-block. */
168 #  define LAST_SMALL_BOUND ((FIRST_BIG_BLOCK>>1) - M_OVERHEAD)
169
170 #endif /* TWO_POT_OPTIMIZE */
171
172 #if defined(PERL_EMERGENCY_SBRK) && defined(PERL_CORE)
173
174 #ifndef BIG_SIZE
175 #  define BIG_SIZE (1<<16)              /* 64K */
176 #endif 
177
178 static char *emergency_buffer;
179 static MEM_SIZE emergency_buffer_size;
180
181 static char *
182 emergency_sbrk(size)
183     MEM_SIZE size;
184 {
185     if (size >= BIG_SIZE) {
186         /* Give the possibility to recover: */
187         die("Out of memory during request for %i bytes", size);
188         /* croak may eat too much memory. */
189     }
190
191     if (!emergency_buffer) {            
192         /* First offense, give a possibility to recover by dieing. */
193         /* No malloc involved here: */
194         GV **gvp = (GV**)hv_fetch(defstash, "^M", 2, 0);
195         SV *sv;
196         char *pv;
197
198         if (!gvp) gvp = (GV**)hv_fetch(defstash, "\015", 1, 0);
199         if (!gvp || !(sv = GvSV(*gvp)) || !SvPOK(sv) 
200             || (SvLEN(sv) < (1<<11) - M_OVERHEAD)) 
201             return (char *)-1;          /* Now die die die... */
202
203         /* Got it, now detach SvPV: */
204         pv = SvPV(sv, na);
205         /* Check alignment: */
206         if (((u_int)(pv - M_OVERHEAD)) & ((1<<11) - 1)) {
207             PerlIO_puts(PerlIO_stderr(),"Bad alignment of $^M!\n");
208             return (char *)-1;          /* die die die */
209         }
210
211         emergency_buffer = pv - M_OVERHEAD;
212         emergency_buffer_size = SvLEN(sv) + M_OVERHEAD;
213         SvPOK_off(sv);
214         SvREADONLY_on(sv);
215         die("Out of memory!");          /* croak may eat too much memory. */
216     }
217     else if (emergency_buffer_size >= size) {
218         emergency_buffer_size -= size;
219         return emergency_buffer + emergency_buffer_size;
220     }
221     
222     return (char *)-1;                  /* poor guy... */
223 }
224
225 #else /* !(defined(TWO_POT_OPTIMIZE) && defined(PERL_CORE)) */
226 #  define emergency_sbrk(size)  -1
227 #endif /* !(defined(TWO_POT_OPTIMIZE) && defined(PERL_CORE)) */
228
229 /*
230  * nextf[i] is the pointer to the next free block of size 2^(i+3).  The
231  * smallest allocatable block is 8 bytes.  The overhead information
232  * precedes the data area returned to the user.
233  */
234 #define NBUCKETS 30
235 static  union overhead *nextf[NBUCKETS];
236
237 #ifdef USE_PERL_SBRK
238 #define sbrk(a) Perl_sbrk(a)
239 char *  Perl_sbrk _((int size));
240 #else
241 extern  char *sbrk();
242 #endif
243
244 #ifdef DEBUGGING_MSTATS
245 /*
246  * nmalloc[i] is the difference between the number of mallocs and frees
247  * for a given block size.
248  */
249 static  u_int nmalloc[NBUCKETS];
250 static  u_int goodsbrk;
251 static  u_int sbrk_slack;
252 static  u_int start_slack;
253 #endif
254
255 #ifdef DEBUGGING
256 #define ASSERT(p)   if (!(p)) botch(STRINGIFY(p));  else
257 static void
258 botch(s)
259         char *s;
260 {
261         PerlIO_printf(PerlIO_stderr(), "assertion botched: %s\n", s);
262         abort();
263 }
264 #else
265 #define ASSERT(p)
266 #endif
267
268 Malloc_t
269 malloc(nbytes)
270         register MEM_SIZE nbytes;
271 {
272         register union overhead *p;
273         register int bucket = 0;
274         register MEM_SIZE shiftr;
275
276 #if defined(DEBUGGING) || defined(RCHECK)
277         MEM_SIZE size = nbytes;
278 #endif
279
280 #ifdef PERL_CORE
281 #ifdef HAS_64K_LIMIT
282         if (nbytes > 0xffff) {
283                 PerlIO_printf(PerlIO_stderr(),
284                               "Allocation too large: %lx\n", (long)nbytes);
285                 my_exit(1);
286         }
287 #endif /* HAS_64K_LIMIT */
288 #ifdef DEBUGGING
289         if ((long)nbytes < 0)
290                 croak("panic: malloc");
291 #endif
292 #endif /* PERL_CORE */
293
294         MUTEX_LOCK(&malloc_mutex);
295         /*
296          * Convert amount of memory requested into
297          * closest block size stored in hash buckets
298          * which satisfies request.  Account for
299          * space used per block for accounting.
300          */
301 #ifdef PACK_MALLOC
302         if (nbytes == 0)
303             nbytes = 1;
304         else if (nbytes > MAX_2_POT_ALGO)
305 #endif
306         {
307 #ifdef TWO_POT_OPTIMIZE
308                 if (nbytes >= FIRST_BIG_BOUND)
309                         nbytes -= PERL_PAGESIZE;
310 #endif 
311                 nbytes += M_OVERHEAD;
312                 nbytes = (nbytes + 3) &~ 3; 
313         }
314         shiftr = (nbytes - 1) >> 2;
315         /* apart from this loop, this is O(1) */
316         while (shiftr >>= 1)
317                 bucket++;
318         /*
319          * If nothing in hash bucket right now,
320          * request more memory from the system.
321          */
322         if (nextf[bucket] == NULL)    
323                 morecore(bucket);
324         if ((p = (union overhead *)nextf[bucket]) == NULL) {
325                 MUTEX_UNLOCK(&malloc_mutex);
326 #ifdef PERL_CORE
327                 if (!nomemok) {
328                     PerlIO_puts(PerlIO_stderr(),"Out of memory!\n");
329                     my_exit(1);
330                 }
331 #else
332                 return (NULL);
333 #endif
334         }
335
336 #ifdef PERL_CORE
337     DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05lu) malloc %ld bytes\n",
338         (unsigned long)(p+1),(unsigned long)(an++),(long)size));
339 #endif /* PERL_CORE */
340
341         /* remove from linked list */
342 #ifdef RCHECK
343         if (*((int*)p) & (sizeof(union overhead) - 1))
344             PerlIO_printf(PerlIO_stderr(), "Corrupt malloc ptr 0x%lx at 0x%lx\n",
345                 (unsigned long)*((int*)p),(unsigned long)p);
346 #endif
347         nextf[bucket] = p->ov_next;
348         OV_MAGIC(p, bucket) = MAGIC;
349 #ifndef PACK_MALLOC
350         OV_INDEX(p) = bucket;
351 #endif
352 #ifdef RCHECK
353         /*
354          * Record allocated size of block and
355          * bound space with magic numbers.
356          */
357         nbytes = (size + M_OVERHEAD + 3) &~ 3; 
358         if (nbytes <= 0x10000)
359                 p->ov_size = nbytes - 1;
360         p->ov_rmagic = RMAGIC;
361         *((u_int *)((caddr_t)p + nbytes - RSLOP)) = RMAGIC;
362 #endif
363         MUTEX_UNLOCK(&malloc_mutex);
364         return ((Malloc_t)(p + CHUNK_SHIFT));
365 }
366
367 /*
368  * Allocate more memory to the indicated bucket.
369  */
370 static void
371 morecore(bucket)
372         register int bucket;
373 {
374         register union overhead *ovp;
375         register int rnu;       /* 2^rnu bytes will be requested */
376         register int nblks;     /* become nblks blocks of the desired size */
377         register MEM_SIZE siz, needed;
378         int slack = 0;
379
380         if (nextf[bucket])
381                 return;
382         if (bucket == (sizeof(MEM_SIZE)*8 - 3)) {
383             croak("Allocation too large");
384         }
385         /*
386          * Insure memory is allocated
387          * on a page boundary.  Should
388          * make getpageize call?
389          */
390 #ifndef atarist /* on the atari we dont have to worry about this */
391         ovp = (union overhead *)sbrk(0);
392 #  ifndef I286
393         if ((UV)ovp & (0x7FF >> CHUNK_SHIFT)) {
394             slack = (0x800 >> CHUNK_SHIFT) - ((UV)ovp & (0x7FF >> CHUNK_SHIFT));
395             (void)sbrk(slack);
396 #    if defined(DEBUGGING_MSTATS)
397             sbrk_slack += slack;
398 #    endif
399         }
400 #  else
401         /* The sbrk(0) call on the I286 always returns the next segment */
402 #  endif
403 #endif /* atarist */
404
405 #if !(defined(I286) || defined(atarist))
406         /* take 2k unless the block is bigger than that */
407         rnu = (bucket <= 8) ? 11 : bucket + 3;
408 #else
409         /* take 16k unless the block is bigger than that 
410            (80286s like large segments!), probably good on the atari too */
411         rnu = (bucket <= 11) ? 14 : bucket + 3;
412 #endif
413         nblks = 1 << (rnu - (bucket + 3));  /* how many blocks to get */
414         needed = (MEM_SIZE)1 << rnu;
415 #ifdef TWO_POT_OPTIMIZE
416         needed += (bucket >= (FIRST_BIG_TWO_POT - 3) ? PERL_PAGESIZE : 0);
417 #endif 
418         ovp = (union overhead *)sbrk(needed);
419         /* no more room! */
420         if (ovp == (union overhead *)-1) {
421             ovp = (union overhead *)emergency_sbrk(needed);
422             if (ovp == (union overhead *)-1)
423                 return;
424         }
425 #ifdef DEBUGGING_MSTATS
426         goodsbrk += needed;
427 #endif  
428         /*
429          * Round up to minimum allocation size boundary
430          * and deduct from block count to reflect.
431          */
432 #ifndef I286
433 #  ifdef PACK_MALLOC
434         if ((UV)ovp & 0x7FF)
435                 croak("panic: Off-page sbrk");
436 #  endif
437         if ((UV)ovp & 7) {
438                 ovp = (union overhead *)(((UV)ovp + 8) & ~7);
439                 nblks--;
440         }
441 #else
442         /* Again, this should always be ok on an 80286 */
443 #endif
444         /*
445          * Add new memory allocated to that on
446          * free list for this hash bucket.
447          */
448         siz = 1 << (bucket + 3);
449 #ifdef PACK_MALLOC
450         *(u_char*)ovp = bucket; /* Fill index. */
451         if (bucket <= MAX_PACKED - 3) {
452             ovp = (union overhead *) ((char*)ovp + blk_shift[bucket]);
453             nblks = n_blks[bucket];
454 #  ifdef DEBUGGING_MSTATS
455             start_slack += blk_shift[bucket];
456 #  endif
457         } else if (bucket <= 11 - 1 - 3) {
458             ovp = (union overhead *) ((char*)ovp + blk_shift[bucket]);
459             /* nblks = n_blks[bucket]; */
460             siz -= sizeof(union overhead);
461         } else ovp++;           /* One chunk per block. */
462 #endif /* !PACK_MALLOC */
463         nextf[bucket] = ovp;
464 #ifdef DEBUGGING_MSTATS
465         nmalloc[bucket] += nblks;
466 #endif 
467         while (--nblks > 0) {
468                 ovp->ov_next = (union overhead *)((caddr_t)ovp + siz);
469                 ovp = (union overhead *)((caddr_t)ovp + siz);
470         }
471         /* Not all sbrks return zeroed memory.*/
472         ovp->ov_next = (union overhead *)NULL;
473 #ifdef PACK_MALLOC
474         if (bucket == 7 - 3) {  /* Special case, explanation is above. */
475             union overhead *n_op = nextf[7 - 3]->ov_next;
476             nextf[7 - 3] = (union overhead *)((caddr_t)nextf[7 - 3] 
477                                               - sizeof(union overhead));
478             nextf[7 - 3]->ov_next = n_op;
479         }
480 #endif /* !PACK_MALLOC */
481 }
482
483 Free_t
484 free(mp)
485         Malloc_t mp;
486 {   
487         register MEM_SIZE size;
488         register union overhead *ovp;
489         char *cp = (char*)mp;
490 #ifdef PACK_MALLOC
491         u_char bucket;
492 #endif 
493
494 #ifdef PERL_CORE
495     DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%lx: (%05lu) free\n",(unsigned long)cp,(unsigned long)(an++)));
496 #endif /* PERL_CORE */
497
498         if (cp == NULL)
499                 return;
500         ovp = (union overhead *)((caddr_t)cp 
501                                  - sizeof (union overhead) * CHUNK_SHIFT);
502 #ifdef PACK_MALLOC
503         bucket = OV_INDEX(ovp);
504 #endif 
505         if (OV_MAGIC(ovp, bucket) != MAGIC) {
506                 static int bad_free_warn = -1;
507                 if (bad_free_warn == -1) {
508                     char *pbf = getenv("PERL_BADFREE");
509                     bad_free_warn = (pbf) ? atoi(pbf) : 1;
510                 }
511                 if (!bad_free_warn)
512                     return;
513 #ifdef RCHECK
514                 warn("%s free() ignored",
515                     ovp->ov_rmagic == RMAGIC - 1 ? "Duplicate" : "Bad");
516 #else
517                 warn("Bad free() ignored");
518 #endif
519                 return;                         /* sanity */
520         }
521         MUTEX_LOCK(&malloc_mutex);
522 #ifdef RCHECK
523         ASSERT(ovp->ov_rmagic == RMAGIC);
524         if (OV_INDEX(ovp) <= MAX_SHORT_BUCKET)
525                 ASSERT(*(u_int *)((caddr_t)ovp + ovp->ov_size + 1 - RSLOP) == RMAGIC);
526         ovp->ov_rmagic = RMAGIC - 1;
527 #endif
528         ASSERT(OV_INDEX(ovp) < NBUCKETS);
529         size = OV_INDEX(ovp);
530         ovp->ov_next = nextf[size];
531         nextf[size] = ovp;
532         MUTEX_UNLOCK(&malloc_mutex);
533 }
534
535 /*
536  * When a program attempts "storage compaction" as mentioned in the
537  * old malloc man page, it realloc's an already freed block.  Usually
538  * this is the last block it freed; occasionally it might be farther
539  * back.  We have to search all the free lists for the block in order
540  * to determine its bucket: 1st we make one pass thru the lists
541  * checking only the first block in each; if that fails we search
542  * ``reall_srchlen'' blocks in each list for a match (the variable
543  * is extern so the caller can modify it).  If that fails we just copy
544  * however many bytes was given to realloc() and hope it's not huge.
545  */
546 int reall_srchlen = 4;  /* 4 should be plenty, -1 =>'s whole list */
547
548 Malloc_t
549 realloc(mp, nbytes)
550         Malloc_t mp; 
551         MEM_SIZE nbytes;
552 {   
553         register MEM_SIZE onb;
554         union overhead *ovp;
555         char *res;
556         register int i;
557         int was_alloced = 0;
558         char *cp = (char*)mp;
559
560 #ifdef DEBUGGING
561         MEM_SIZE size = nbytes;
562 #endif
563
564 #ifdef PERL_CORE
565 #ifdef HAS_64K_LIMIT
566         if (nbytes > 0xffff) {
567                 PerlIO_printf(PerlIO_stderr(),
568                               "Reallocation too large: %lx\n", size);
569                 my_exit(1);
570         }
571 #endif /* HAS_64K_LIMIT */
572         if (!cp)
573                 return malloc(nbytes);
574 #ifdef DEBUGGING
575         if ((long)nbytes < 0)
576                 croak("panic: realloc");
577 #endif
578 #endif /* PERL_CORE */
579
580         MUTEX_LOCK(&malloc_mutex);
581         ovp = (union overhead *)((caddr_t)cp 
582                                  - sizeof (union overhead) * CHUNK_SHIFT);
583         i = OV_INDEX(ovp);
584         if (OV_MAGIC(ovp, i) == MAGIC) {
585                 was_alloced = 1;
586         } else {
587                 /*
588                  * Already free, doing "compaction".
589                  *
590                  * Search for the old block of memory on the
591                  * free list.  First, check the most common
592                  * case (last element free'd), then (this failing)
593                  * the last ``reall_srchlen'' items free'd.
594                  * If all lookups fail, then assume the size of
595                  * the memory block being realloc'd is the
596                  * smallest possible.
597                  */
598                 if ((i = findbucket(ovp, 1)) < 0 &&
599                     (i = findbucket(ovp, reall_srchlen)) < 0)
600                         i = 0;
601         }
602         onb = (1L << (i + 3)) - 
603 #ifdef PACK_MALLOC
604             (i <= (MAX_PACKED - 3) ? 0 : M_OVERHEAD)
605 #else
606             M_OVERHEAD
607 #endif
608 #ifdef TWO_POT_OPTIMIZE
609             + (i >= (FIRST_BIG_TWO_POT - 3) ? PERL_PAGESIZE : 0)
610 #endif
611             ;
612         /* 
613          *  avoid the copy if same size block.
614          *  We are not agressive with boundary cases. Note that it is
615          *  possible for small number of cases give false negative if
616          *  both new size and old one are in the bucket for
617          *  FIRST_BIG_TWO_POT, but the new one is near the lower end.
618          */
619         if (was_alloced &&
620             nbytes <= onb && (nbytes > ( (onb >> 1) - M_OVERHEAD )
621 #ifdef TWO_POT_OPTIMIZE
622                               || (i == (FIRST_BIG_TWO_POT - 3) 
623                                   && nbytes >= LAST_SMALL_BOUND )
624 #endif  
625                 )) {
626 #ifdef RCHECK
627                 /*
628                  * Record new allocated size of block and
629                  * bound space with magic numbers.
630                  */
631                 if (OV_INDEX(ovp) <= MAX_SHORT_BUCKET) {
632                         /*
633                          * Convert amount of memory requested into
634                          * closest block size stored in hash buckets
635                          * which satisfies request.  Account for
636                          * space used per block for accounting.
637                          */
638                         nbytes += M_OVERHEAD;
639                         nbytes = (nbytes + 3) &~ 3; 
640                         ovp->ov_size = nbytes - 1;
641                         *((u_int *)((caddr_t)ovp + nbytes - RSLOP)) = RMAGIC;
642                 }
643 #endif
644                 res = cp;
645                 MUTEX_UNLOCK(&malloc_mutex);
646         }
647         else {
648                 MUTEX_UNLOCK(&malloc_mutex);
649                 if ((res = (char*)malloc(nbytes)) == NULL)
650                         return (NULL);
651                 if (cp != res)                  /* common optimization */
652                         Copy(cp, res, (MEM_SIZE)(nbytes<onb?nbytes:onb), char);
653                 if (was_alloced)
654                         free(cp);
655         }
656
657 #ifdef PERL_CORE
658 #ifdef DEBUGGING
659     if (debug & 128) {
660         PerlIO_printf(Perl_debug_log, "0x%lx: (%05lu) rfree\n",(unsigned long)res,(unsigned long)(an++));
661         PerlIO_printf(Perl_debug_log, "0x%lx: (%05lu) realloc %ld bytes\n",
662             (unsigned long)res,(unsigned long)(an++),(long)size);
663     }
664 #endif
665 #endif /* PERL_CORE */
666         return ((Malloc_t)res);
667 }
668
669 /*
670  * Search ``srchlen'' elements of each free list for a block whose
671  * header starts at ``freep''.  If srchlen is -1 search the whole list.
672  * Return bucket number, or -1 if not found.
673  */
674 static int
675 findbucket(freep, srchlen)
676         union overhead *freep;
677         int srchlen;
678 {
679         register union overhead *p;
680         register int i, j;
681
682         for (i = 0; i < NBUCKETS; i++) {
683                 j = 0;
684                 for (p = nextf[i]; p && j != srchlen; p = p->ov_next) {
685                         if (p == freep)
686                                 return (i);
687                         j++;
688                 }
689         }
690         return (-1);
691 }
692
693 Malloc_t
694 calloc(elements, size)
695         register MEM_SIZE elements;
696         register MEM_SIZE size;
697 {
698     long sz = elements * size;
699     Malloc_t p = malloc(sz);
700
701     if (p) {
702         memset((void*)p, 0, sz);
703     }
704     return p;
705 }
706
707 #ifdef DEBUGGING_MSTATS
708 /*
709  * mstats - print out statistics about malloc
710  * 
711  * Prints two lines of numbers, one showing the length of the free list
712  * for each size category, the second showing the number of mallocs -
713  * frees for each size category.
714  */
715 void
716 dump_mstats(s)
717         char *s;
718 {
719         register int i, j;
720         register union overhead *p;
721         int topbucket=0, totfree=0, total=0;
722         u_int nfree[NBUCKETS];
723
724         for (i=0; i < NBUCKETS; i++) {
725                 for (j = 0, p = nextf[i]; p; p = p->ov_next, j++)
726                         ;
727                 nfree[i] = j;
728                 totfree += nfree[i]   * (1 << (i + 3));
729                 total += nmalloc[i] * (1 << (i + 3));
730                 if (nmalloc[i])
731                         topbucket = i;
732         }
733         if (s)
734                 PerlIO_printf(PerlIO_stderr(), "Memory allocation statistics %s (buckets 8..%d)\n",
735                         s, (1 << (topbucket + 3)) );
736         PerlIO_printf(PerlIO_stderr(), "%8d free:", totfree);
737         for (i=0; i <= topbucket; i++) {
738                 PerlIO_printf(PerlIO_stderr(), (i<5 || i==7)?" %5d": (i<9)?" %3d":" %d", nfree[i]);
739         }
740         PerlIO_printf(PerlIO_stderr(), "\n%8d used:", total - totfree);
741         for (i=0; i <= topbucket; i++) {
742                 PerlIO_printf(PerlIO_stderr(), (i<5 || i==7)?" %5d": (i<9)?" %3d":" %d", nmalloc[i] - nfree[i]);
743         }
744         PerlIO_printf(PerlIO_stderr(), "\nTotal sbrk(): %8d. Odd ends: sbrk(): %7d, malloc(): %7d bytes.\n",
745                       goodsbrk + sbrk_slack, sbrk_slack, start_slack);
746 }
747 #else
748 void
749 dump_mstats(s)
750     char *s;
751 {
752 }
753 #endif
754 #endif /* lint */
755
756
757 #ifdef USE_PERL_SBRK
758
759 #   ifdef NeXT
760 #      define PERL_SBRK_VIA_MALLOC
761 #   endif
762
763 #   ifdef PERL_SBRK_VIA_MALLOC
764 #      if defined(HIDEMYMALLOC) || defined(EMBEDMYMALLOC)
765 #         undef malloc
766 #      else
767 #         include "Error: -DPERL_SBRK_VIA_MALLOC needs -D(HIDE|EMBED)MYMALLOC"
768 #      endif
769
770 /* it may seem schizophrenic to use perl's malloc and let it call system */
771 /* malloc, the reason for that is only the 3.2 version of the OS that had */
772 /* frequent core dumps within nxzonefreenolock. This sbrk routine put an */
773 /* end to the cores */
774
775 #      define SYSTEM_ALLOC(a) malloc(a)
776
777 #   endif  /* PERL_SBRK_VIA_MALLOC */
778
779 static IV Perl_sbrk_oldchunk;
780 static long Perl_sbrk_oldsize;
781
782 #   define PERLSBRK_32_K (1<<15)
783 #   define PERLSBRK_64_K (1<<16)
784
785 char *
786 Perl_sbrk(size)
787 int size;
788 {
789     IV got;
790     int small, reqsize;
791
792     if (!size) return 0;
793 #ifdef PERL_CORE
794     reqsize = size; /* just for the DEBUG_m statement */
795 #endif
796 #ifdef PACK_MALLOC
797     size = (size + 0x7ff) & ~0x7ff;
798 #endif
799     if (size <= Perl_sbrk_oldsize) {
800         got = Perl_sbrk_oldchunk;
801         Perl_sbrk_oldchunk += size;
802         Perl_sbrk_oldsize -= size;
803     } else {
804       if (size >= PERLSBRK_32_K) {
805         small = 0;
806       } else {
807 #ifndef PERL_CORE
808         reqsize = size;
809 #endif
810         size = PERLSBRK_64_K;
811         small = 1;
812       }
813       got = (IV)SYSTEM_ALLOC(size);
814 #ifdef PACK_MALLOC
815       got = (got + 0x7ff) & ~0x7ff;
816 #endif
817       if (small) {
818         /* Chunk is small, register the rest for future allocs. */
819         Perl_sbrk_oldchunk = got + reqsize;
820         Perl_sbrk_oldsize = size - reqsize;
821       }
822     }
823
824 #ifdef PERL_CORE
825     DEBUG_m(PerlIO_printf(Perl_debug_log, "sbrk malloc size %ld (reqsize %ld), left size %ld, give addr 0x%lx\n",
826                     size, reqsize, Perl_sbrk_oldsize, got));
827 #endif
828
829     return (void *)got;
830 }
831
832 #endif /* ! defined USE_PERL_SBRK */