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