5 fprintf(stderr, "Malloc botch: %s\n", message);
10 /****************************************************************
12 * Storage Allocator for Foundation. *
13 * Built from gnuemacs storage allocator *
15 ****************************************************************/
17 /* Copyright (C) 1985 Richard M. Stallman,
18 based mostly on the public domain work of others.
20 This program is distributed in the hope that it will be useful,
21 but without any warranty. No author or distributor
22 accepts responsibility to anyone for the consequences of using it
23 or for whether it serves any particular purpose or works at all,
24 unless he says so in writing.
26 Permission is granted to anyone to distribute verbatim copies
27 of this program's source code as received, in any medium, provided that
28 the copyright notice, the nonwarraty notice above
29 and this permission notice are preserved,
30 and that the distributor grants the recipient all rights
31 for further redistribution as permitted by this notice,
32 and informs him of these rights.
34 Permission is granted to distribute modified versions of this
35 program's source code, or of portions of it, under the above
36 conditions, plus the conditions that all changed files carry
37 prominent notices stating who last changed them and that the
38 derived material, including anything packaged together with it and
39 conceptually functioning as a modification of it rather than an
40 application of it, is in its entirety subject to a permission
41 notice identical to this one.
43 Permission is granted to distribute this program (verbatim or
44 as modified) in compiled or executable form, provided verbatim
45 redistribution is permitted as stated above for source code, and
46 A. it is accompanied by the corresponding machine-readable
47 source code, under the above conditions, or
48 B. it is accompanied by a written offer, with no time limit,
49 to distribute the corresponding machine-readable source code,
50 under the above conditions, to any one, in return for reimbursement
51 of the cost of distribution. Verbatim redistribution of the
52 written offer must be permitted. Or,
53 C. it is distributed by someone who received only the
54 compiled or executable form, and is accompanied by a copy of the
55 written offer of source code which he received along with it.
57 Permission is granted to distribute this program (verbatim or as modified)
58 in executable form as part of a larger system provided that the source
59 code for this program, including any modifications used,
60 is also distributed or offered as stated in the preceding paragraph.
62 In other words, you are welcome to use, share and improve this program.
63 You are forbidden to forbid anyone else to use, share and improve
64 what you give them. Help stamp out software-hoarding! */
66 /****************************************************************
68 * Helpful historical comments *
70 ****************************************************************/
73 * @(#)nmalloc.c 1 (Caltech) 2/21/82
75 * U of M Modified: 20 Jun 1983 ACT: strange hacks for Emacs
77 * Nov 1983, Mike@BRL, Added support for 4.1C/4.2 BSD.
79 * This is a very fast storage allocator. It allocates blocks of a small
80 * number of different sizes, and keeps free lists of each size. Blocks
81 * that don't exactly fit are passed up to the next larger size. In this
82 * implementation, the available sizes are (2^n)-4 (or -16) bytes long.
83 * This is designed for use in a program that uses vast quantities of
84 * memory, but bombs when it runs out. To make it a little better, it
85 * warns the user when he starts to get near the end.
87 * June 84, ACT: modified rcheck code to check the range given to malloc,
88 * rather than the range determined by the 2-power used.
90 * Jan 85, RMS: calls malloc_warning to issue warning on nearly full.
91 * No longer Emacs-specific; can serve as all-purpose malloc for GNU.
92 * You should call malloc_init to reinitialize after loading dumped Emacs.
93 * Call malloc_stats to get info on memory stats if MSTATS turned on.
94 * realloc knows how to return same block given, just changing its size,
95 * if the power of 2 is correct.
97 * Jan 86, WDC: Removed Emacs specific stuff, and neatened a few comments.
99 * March 86 WDC: Added in code by Eichin for Scribble checking of blocks
100 * Scribble check writes a known pattern into the free blocks, checks it
101 * to see if it is still undamaged before allocating it. It writes a
102 * different pattern into the space beyond the end of an allocated block,
103 * and tests it for damage when expanding the block's bounds in realloc.
104 * Note, this check takes *TIME* and should not be compiled in by default.
106 * Berkeley UNIX 4.3 has a storage allocator that shares a common
107 * ancestor with this one. It handles realloc compatibly with the
108 * archaic use of realloc on an already freed block to "compact"
109 * storage. It uses a pagesize system call rather than assuming the
110 * page size is 1024 bytes. Finally it guarantees that a freed block
111 * is not munged by the allocator itself, incase someone wants to fiddle
112 * with freed space after freeing it but before allocating more.
114 * This particular storage allocator would benefit from having a
115 * non-hardwired pagesize. But because of the scribble check it would
116 * not be useful to keep the free pointer in the header. SO: When you
117 * free something allocated with this allocator, DONT TRY TO USE IT.
118 * It is GUARANTEED to be damaged by the freeing process.
120 * For interfacing to systems that want to be able to ask the size of
121 * the allocated block, rather than remembering it, the m_blocksize
122 * function, rips open the block and tells you how big it is. The size
123 * returned is nbytes, the number of bytes asked for, NOT the actual
124 * amount of space in the block.
127 /****************************************************************
129 * Includes, declarations, and definitions *
131 ****************************************************************/
133 /* Determine which kind of system this is. */
145 #include <sys/vlimit.h> /* warn the user when near the end */
148 #include <sys/time.h>
149 #include <sys/resource.h>
151 #include <moira_site.h>
155 #endif /* we need to have range data to use block boundary checking */
159 * To implement range checking, we write magic values in at the
160 * beginning and end of each allocated block, and make sure they
161 * are undisturbed whenever a free or a realloc occurs.
164 /* Written in each of the 4 bytes following the block's real space */
166 #define MAGICFREE 0x69 /* 0110 1001 Magic value for Free blocks */
168 /* Written in the 4 bytes before the block's real space */
169 #define MAGIC4 0x55555555
170 #define MAGICFREE4 0x69696969
172 #define ASSERT(p) if (!(p)) botch("p"); else
173 #define EXTRA 4 /* 4 bytes extra for MAGIC1s */
179 #define ISALLOC ((char) 0xf7) /* magic byte that implies allocation */
180 #define ISFREE ((char) 0x54) /* magic byte that implies free block */
181 /* this is for error checking only */
183 /* If range checking is not turned on, all we have is a flag
184 * indicating whether memory is allocated, an index in nextf[],
185 * and a field that tells how many bytes.
186 * To realloc() memory we copy nbytes.
187 * 16 bits of header space is unused.
190 char mh_alloc; /* ISALLOC or ISFREE */
191 char mh_index; /* index in nextf[] */
192 unsigned short mh_extra;/* Currently wasted 16 bits */
193 /* Remainder are valid only when block is allocated */
194 unsigned mh_nbytes; /* number of bytes allocated */
196 int mh_magic4; /* should be == MAGIC4 */
201 * Access free-list pointer of a block.
202 * It is stored at block + 4.
203 * This is not a field in the mhead structure because we want
204 * sizeof (struct mhead) to describe the overhead for when the
205 * block is in use, and we do not want the free-list pointer
209 (*(struct mhead **) (sizeof (char *) + (char *) (a)))
212 /****************************************************************
214 * Variable Creations *
216 ****************************************************************/
219 extern char *start_of_data (); /* This seems necessary for USG */
223 /* These two are for user programs to look at, when they are interested. */
225 int malloc_sbrk_used; /* amount of data space used now */
226 int malloc_sbrk_unused; /* amount more we can have */
228 /* start of data space; can be changed by calling init_malloc */
229 static char *data_space_start;
233 * nmalloc[i] is the difference between the number of mallocs and frees
234 * for a given block size.
236 static int nmalloc[30];
237 static int nmal, nfre;
241 * nextf[i] is the pointer to the next free block of size 2^(i+3). The
242 * smallest allocatable block is 8 bytes. The overhead information will
243 * go in the first int of the block, and the returned pointer will point
246 static struct mhead *nextf[30];
248 /* Number of bytes of writable memory we can expect to be able to get */
250 /* Level number of warnings already issued.
251 * 0 -- no warnings issued.
252 * 1 -- 75% warning already issued.
253 * 2 -- 85% warning already issued.
255 static int warnlevel;
258 /* nonzero once initial bunch of free blocks made */
262 /****************************************************************
264 * Start of procedures *
266 * malloc_init, m_blocksize *
268 ****************************************************************/
271 * Cause reinitialization based on job parameters;
272 * also declare where the end of pure storage is.
277 data_space_start = start;
282 int m_blocksize(a_block)
285 return(((struct mhead *)a_block-1)->mh_nbytes);
288 #if INGRESVER == 5 && defined(vax)
289 /* This is here to pull in our private version of meinitlst.o
290 * so that we don't try to get the buggy Ingres 5.0 one.
292 extern int MEinitLists();
294 static int (*foo)() = MEinitLists;
298 /****************************************************************
300 * morecore - Ask the system for more memory *
302 ****************************************************************/
305 morecore (nu) /* ask system for more memory */
306 register int nu; /* size index to get more of */
314 if (!data_space_start)
317 data_space_start = start_of_data ();
319 data_space_start = &etext;
326 /* On initial startup, get two blocks of each size up to 1k bytes */
328 getpool (), getpool (), gotpool = 1;
330 /* Find current end of memory and issue warning if getting near max */
333 siz = cp - data_space_start;
334 malloc_sbrk_used = siz;
335 malloc_sbrk_unused = lim_data - siz;
340 if (siz > (lim_data / 4) * 3)
343 malloc_warning ("Warning: past 75% of memory limit");
347 if (siz > (lim_data / 20) * 17)
350 malloc_warning ("Warning: past 85% of memory limit");
354 if (siz > (lim_data / 20) * 19)
357 malloc_warning ("Warning: past 95% of memory limit");
362 if ((int) cp & 0x3ff) /* land on 1K boundaries */
363 sbrk (1024 - ((int) cp & 0x3ff));
366 /* Take at least 2k, and figure out how many blocks of the desired size
367 we're about to get */
370 nblks = 1 << ((siz = 8) - nu);
371 #if INGRESVER == 5 && defined(vax)
374 if (MEalloc(1, 1 << (siz+3), &tcp))
375 return; /* No more room! */
378 #else /* INGRESVER == 5 && defined(vax) */
379 if ((cp = sbrk (1 << (siz + 3))) == (char *) -1)
380 return; /* no more room! */
381 #endif /* INGRESVER == 5 && defined(vax) */
383 { /* shouldn't happen, but just in case */
384 cp = (char *) (((int) cp + 8) & ~7);
388 /* save new header and link the nblks blocks together */
389 nextf[nu] = (struct mhead *) cp;
393 ((struct mhead *) cp) -> mh_alloc = ISFREE;
394 ((struct mhead *) cp) -> mh_index = nu;
396 ((struct mhead *) cp) -> mh_magic4 = MAGICFREE4;
400 /* Check that upper stuff was still MAGIC1 */
401 register char *m = (char *)((struct mhead *)cp+1);
402 register char *en = (8<<nu) + cp;
403 /* Fill whole block with MAGICFREE */
404 while (m<en) *m++ = MAGICFREE;
406 #endif /* scribblecheck */
408 /* Clear newly allocated blocks, to match free ones */
409 if (--nblks <= 0) break;
410 CHAIN ((struct mhead *) cp) = (struct mhead *) (cp + siz);
413 CHAIN ((struct mhead *) cp) = 0;
416 /****************************************************************
418 * getpool - Get initial pools of small blocks *
420 ****************************************************************/
426 register char *cp = sbrk (0);
428 if ((int) cp & 0x3ff) /* land on 1K boundaries */
429 sbrk (1024 - ((int) cp & 0x3ff));
431 /* Get 2k of storage */
434 if (cp == (char *) -1)
437 /* Divide it into an initial 8-word block
438 plus one block of size 2**nu for nu = 3 ... 10. */
440 CHAIN (cp) = nextf[0];
441 nextf[0] = (struct mhead *) cp;
442 ((struct mhead *) cp) -> mh_alloc = ISFREE;
443 ((struct mhead *) cp) -> mh_index = 0;
445 ((struct mhead *) cp) -> mh_magic4 = MAGICFREE4;
449 for (nu = 0; nu < 7; nu++)
451 CHAIN (cp) = nextf[nu];
452 nextf[nu] = (struct mhead *) cp;
453 ((struct mhead *) cp) -> mh_alloc = ISFREE;
454 ((struct mhead *) cp) -> mh_index = nu;
456 ((struct mhead *) cp) -> mh_magic4 = MAGICFREE4;
460 register char *m = (char *)((struct mhead *)cp+1);
461 register char *en = (8<<nu) + cp;
462 /* Fill whole block with MAGICFREE */
463 while (m<en) *m++ = MAGICFREE;
465 #endif /* scribblecheck */
471 /****************************************************************
473 * malloc - get a block of space from a pool *
475 ****************************************************************/
478 malloc (n) /* get a block */
481 register struct mhead *p;
482 register unsigned int nbytes;
483 register int nunits = 0;
485 /* Figure out how many bytes are required, rounding up to the nearest
486 multiple of 4, then figure out which nextf[] area to use */
487 nbytes = (n + sizeof *p + EXTRA + 3) & ~3;
489 register unsigned int shiftr = (nbytes - 1) >> 2;
495 /* If there are no blocks of the appropriate size, go get some */
496 /* COULD SPLIT UP A LARGER BLOCK HERE ... ACT */
497 if (nextf[nunits] == 0)
500 /* Get one block off the list, and set the new list head */
501 if ((p = nextf[nunits]) == 0)
503 nextf[nunits] = CHAIN (p);
505 /* Check for free block clobbered */
506 /* If not for this check, we would gobble a clobbered free chain ptr */
507 /* and bomb out on the NEXT allocate of this size block */
508 if (p -> mh_alloc != ISFREE || p -> mh_index != nunits)
510 botch ("block on free list clobbered");
511 #else /* not rcheck */
513 #endif /* not rcheck */
515 if (p -> mh_magic4 != MAGICFREE4)
516 botch ("Magic in block on free list clobbered");
519 /* Check for block filled with magic numbers, then change to zeros */
521 register char *m = (char *) (p + 1);
522 register char *en = (8<<p->mh_index) + (char *) p;
523 register int block_valid = 0;
524 while(m<en && (block_valid=(*m==MAGICFREE)))
526 /* so, status comes out as 1 if ok, 0 if terminated */
527 if (!block_valid) botch ("data on free list damaged");
529 #endif /* scribblecheck */
530 /* Fill in the info, and if range checking, set up the magic numbers */
531 p -> mh_alloc = ISALLOC;
534 p -> mh_magic4 = MAGIC4;
536 register char *m = (char *) (p + 1) + n;
538 register char *en = (8<<p->mh_index)+(char *)p;
539 /* point to end of block */
540 while (m<en) *m++ = MAGIC1;
541 #else /* scribblecheck */
542 *m++ = MAGIC1, *m++ = MAGIC1, *m++ = MAGIC1, *m = MAGIC1;
543 #endif /* scribblecheck */
545 #endif /* not rcheck */
550 return (char *) (p + 1);
553 /****************************************************************
555 * free - Free a block of space *
557 ****************************************************************/
562 register struct mhead *p;
564 register char *ap = mem;
567 p = (struct mhead *) ap - 1;
568 ASSERT (p -> mh_alloc == ISALLOC);
570 ASSERT (p -> mh_magic4 == MAGIC4);
571 ap += p -> mh_nbytes;
572 p->mh_magic4 = MAGICFREE4;
573 ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1);
574 ASSERT (*ap++ == MAGIC1); ASSERT (*ap == MAGIC1);
578 register int nunits = p -> mh_index;
580 ASSERT (nunits <= 29);
583 /* Check that upper stuff was still MAGIC1 */
584 register char *m = (char *) (p + 1) + p->mh_nbytes;
585 register char *en = (8<<p->mh_index) + (char *) p;
586 register int block_valid = 0;
587 while(m<en && (block_valid=(*m++==MAGIC1)));
588 if (!block_valid) botch ("block freed with data out of bounds");
589 /* Fill whole block with MAGICFREE */
590 m = (char *) (p + 1);
591 while (m<en) *m++ = MAGICFREE;
593 #endif /* scribblecheck */
594 p -> mh_alloc = ISFREE;
595 CHAIN (p) = nextf[nunits];
604 /****************************************************************
606 * realloc - resize a block, copy if necessary *
608 ****************************************************************/
615 register struct mhead *p;
616 register unsigned int tocopy;
620 if ((p = (struct mhead *) mem) == 0)
623 nunits = p -> mh_index;
624 ASSERT (p -> mh_alloc == ISALLOC);
625 tocopy = p -> mh_nbytes;
627 ASSERT (p -> mh_magic4 == MAGIC4);
629 register char *m = mem + tocopy;
631 register char *en = (8<<p->mh_index) + (char *)p;
632 register int block_valid = 0;
633 while(m<en && (block_valid=(*m++==MAGIC1)));
634 if (!block_valid) botch ("out of bounds data on realloc");
635 #else /* scribblecheck */
636 ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1);
637 ASSERT (*m++ == MAGIC1); ASSERT (*m == MAGIC1);
638 #endif /* scribblecheck */
640 #endif /* not rcheck */
642 /* See if desired size rounds to same power of 2 as actual size. */
643 nbytes = (n + sizeof *p + EXTRA + 7) & ~7;
645 /* If ok, use the same block, just marking its size as changed. */
646 if (nbytes > (4 << nunits) && nbytes <= (8 << nunits))
648 /* Here we check on realloc if we are grabbing unused space */
650 register char *m = mem + tocopy;
652 register char *en = (8<<p->mh_index) + (char *) p;
653 while (m<en) *m++=(char)0;
654 #else /* scribblecheck */
655 *m++ = 0; *m++ = 0; *m++ = 0; *m++ = 0;
656 #endif /* scribblecheck */
659 while(m<en) *m++ = MAGIC1;
660 #else /* scribblecheck */
661 *m++ = MAGIC1; *m++ = MAGIC1; *m++ = MAGIC1; *m++ = MAGIC1;
662 #endif /* scribblecheck */
663 #endif /* not rcheck */
673 if ((new = malloc (n)) == 0)
675 bcopy (mem, new, tocopy);
681 /****************************************************************
683 * Memory Statistics stuff *
685 ****************************************************************/
688 /* Return statistics describing allocation of blocks of size 2**n. */
701 struct mstats_value v;
703 register struct mhead *p;
707 if (size < 0 || size >= 30)
714 v.blocksize = 1 << (size + 3);
715 v.nused = nmalloc[size];
717 for (p = nextf[size]; p; p = CHAIN (p))
725 /****************************************************************
727 * Stuff having to do with determining memory limits *
729 ****************************************************************/
732 * This function returns the total number of bytes that the process
733 * will be allowed to allocate via the sbrk(2) system call. On
734 * BSD systems this is the total space allocatable to stack and
735 * data. On USG systems this is the data space only.
742 extern long ulimit ();
744 lim_data = ulimit (3, 0);
745 lim_data -= (long) data_space_start;
753 lim_data = vlimit (LIM_DATA, -1);
760 struct rlimit XXrlimit;
762 getrlimit (RLIMIT_DATA, &XXrlimit);
763 lim_data = XXrlimit.rlim_cur; /* soft limit */
771 * Calloc - allocate and clear memory block
775 register unsigned num, size;
777 extern char *malloc();
781 if (p = malloc(size))