groff before CVS: release 1.05
[s-roff.git] / lib / malloc.c
blobb27c27779419c82389502a3cadc6f762f2f6d402
1 /* dynamic memory allocation for GNU.
2 Copyright (C) 1985, 1987 Free Software Foundation, Inc.
4 NO WARRANTY
6 BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
7 NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
8 WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
9 RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
10 WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
11 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
12 FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
13 AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
14 DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
15 CORRECTION.
17 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
18 STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
19 WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
20 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
21 OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
22 USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
23 DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
24 A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
25 PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
28 GENERAL PUBLIC LICENSE TO COPY
30 1. You may copy and distribute verbatim copies of this source file
31 as you receive it, in any medium, provided that you conspicuously and
32 appropriately publish on each copy a valid copyright notice "Copyright
33 (C) 1985 Free Software Foundation, Inc."; and include following the
34 copyright notice a verbatim copy of the above disclaimer of warranty
35 and of this License. You may charge a distribution fee for the
36 physical act of transferring a copy.
38 2. You may modify your copy or copies of this source file or
39 any portion of it, and copy and distribute such modifications under
40 the terms of Paragraph 1 above, provided that you also do the following:
42 a) cause the modified files to carry prominent notices stating
43 that you changed the files and the date of any change; and
45 b) cause the whole of any work that you distribute or publish,
46 that in whole or in part contains or is a derivative of this
47 program or any part thereof, to be licensed at no charge to all
48 third parties on terms identical to those contained in this
49 License Agreement (except that you may choose to grant more extensive
50 warranty protection to some or all third parties, at your option).
52 c) You may charge a distribution fee for the physical act of
53 transferring a copy, and you may at your option offer warranty
54 protection in exchange for a fee.
56 Mere aggregation of another unrelated program with this program (or its
57 derivative) on a volume of a storage or distribution medium does not bring
58 the other program under the scope of these terms.
60 3. You may copy and distribute this program (or a portion or derivative
61 of it, under Paragraph 2) in object code or executable form under the terms
62 of Paragraphs 1 and 2 above provided that you also do one of the following:
64 a) accompany it with the complete corresponding machine-readable
65 source code, which must be distributed under the terms of
66 Paragraphs 1 and 2 above; or,
68 b) accompany it with a written offer, valid for at least three
69 years, to give any third party free (except for a nominal
70 shipping charge) a complete machine-readable copy of the
71 corresponding source code, to be distributed under the terms of
72 Paragraphs 1 and 2 above; or,
74 c) accompany it with the information you received as to where the
75 corresponding source code may be obtained. (This alternative is
76 allowed only for noncommercial distribution and only if you
77 received the program in object code or executable form alone.)
79 For an executable file, complete source code means all the source code for
80 all modules it contains; but, as a special exception, it need not include
81 source code for modules which are standard libraries that accompany the
82 operating system on which the executable file runs.
84 4. You may not copy, sublicense, distribute or transfer this program
85 except as expressly provided under this License Agreement. Any attempt
86 otherwise to copy, sublicense, distribute or transfer this program is void and
87 your rights to use the program under this License agreement shall be
88 automatically terminated. However, parties who have received computer
89 software programs from you with this License Agreement will not have
90 their licenses terminated so long as such parties remain in full compliance.
92 5. If you wish to incorporate parts of this program into other free
93 programs whose distribution conditions are different, write to the Free
94 Software Foundation at 675 Mass Ave, Cambridge, MA 02139. We have not yet
95 worked out a simple rule that can be stated here, but we will often permit
96 this. We will be guided by the two goals of preserving the free status of
97 all derivatives of our free software and of promoting the sharing and reuse of
98 software.
101 In other words, you are welcome to use, share and improve this program.
102 You are forbidden to forbid anyone else to use, share and improve
103 what you give them. Help stamp out software-hoarding! */
107 * @(#)nmalloc.c 1 (Caltech) 2/21/82
109 * U of M Modified: 20 Jun 1983 ACT: strange hacks for Emacs
111 * Nov 1983, Mike@BRL, Added support for 4.1C/4.2 BSD.
113 * This is a very fast storage allocator. It allocates blocks of a small
114 * number of different sizes, and keeps free lists of each size. Blocks
115 * that don't exactly fit are passed up to the next larger size. In this
116 * implementation, the available sizes are (2^n)-4 (or -16) bytes long.
117 * This is designed for use in a program that uses vast quantities of
118 * memory, but bombs when it runs out. To make it a little better, it
119 * warns the user when he starts to get near the end.
121 * June 84, ACT: modified rcheck code to check the range given to malloc,
122 * rather than the range determined by the 2-power used.
124 * Jan 85, RMS: calls malloc_warning to issue warning on nearly full.
125 * No longer Emacs-specific; can serve as all-purpose malloc for GNU.
126 * You should call malloc_init to reinitialize after loading dumped Emacs.
127 * Call malloc_stats to get info on memory stats if MSTATS turned on.
128 * realloc knows how to return same block given, just changing its size,
129 * if the power of 2 is correct.
133 * nextf[i] is the pointer to the next free block of size 2^(i+3). The
134 * smallest allocatable block is 8 bytes. The overhead information will
135 * go in the first int of the block, and the returned pointer will point
136 * to the second.
138 #ifdef MSTATS
139 * nmalloc[i] is the difference between the number of mallocs and frees
140 * for a given block size.
141 #endif MSTATS
144 #ifdef emacs
145 /* config.h specifies which kind of system this is. */
146 #include "config.h"
147 #else
149 /* Determine which kind of system this is. */
150 #include <signal.h>
151 #ifndef SIGTSTP
152 #ifndef VMS
153 #ifndef USG
154 #define USG
155 #endif
156 #endif /* not VMS */
157 #else /* SIGTSTP */
158 #ifdef SIGIO
159 #define BSD4_2
160 #endif /* SIGIO */
161 #endif /* SIGTSTP */
163 #endif /* not emacs */
165 /* Define getpagesize () if the system does not. */
166 #include "getpagesize.h"
168 #ifndef BSD4_2
169 #ifndef USG
170 #include <sys/vlimit.h> /* warn the user when near the end */
171 #endif /* not USG */
172 #else /* if BSD4_2 */
173 #include <sys/time.h>
174 #include <sys/resource.h>
175 #endif /* BSD4_2 */
177 #ifdef USG
178 #define bcopy(from, to, n) memcpy(to, from, n)
179 #endif
181 extern char *start_of_data ();
183 #ifdef BSD
184 #ifndef DATA_SEG_BITS
185 #define start_of_data() &etext
186 #endif
187 #endif
189 #ifndef emacs
190 #define start_of_data() &etext
191 #endif
193 #define ISALLOC ((char) 0xf7) /* magic byte that implies allocation */
194 #define ISFREE ((char) 0x54) /* magic byte that implies free block */
195 /* this is for error checking only */
196 #define ISMEMALIGN ((char) 0xd6) /* Stored before the value returned by
197 memalign, with the rest of the word
198 being the distance to the true
199 beginning of the block. */
201 extern char etext;
203 /* These two are for user programs to look at, when they are interested. */
205 unsigned int malloc_sbrk_used; /* amount of data space used now */
206 unsigned int malloc_sbrk_unused; /* amount more we can have */
208 /* start of data space; can be changed by calling init_malloc */
209 static char *data_space_start;
211 #ifdef MSTATS
212 static int nmalloc[30];
213 static int nmal, nfre;
214 #endif /* MSTATS */
216 /* If range checking is not turned on, all we have is a flag indicating
217 whether memory is allocated, an index in nextf[], and a size field; to
218 realloc() memory we copy either size bytes or 1<<(index+3) bytes depending
219 on whether the former can hold the exact size (given the value of
220 'index'). If range checking is on, we always need to know how much space
221 is allocated, so the 'size' field is never used. */
223 struct mhead {
224 char mh_alloc; /* ISALLOC or ISFREE */
225 char mh_index; /* index in nextf[] */
226 /* Remainder are valid only when block is allocated */
227 unsigned short mh_size; /* size, if < 0x10000 */
228 #ifdef rcheck
229 unsigned mh_nbytes; /* number of bytes allocated */
230 int mh_magic4; /* should be == MAGIC4 */
231 #endif /* rcheck */
234 /* Access free-list pointer of a block.
235 It is stored at block + 4.
236 This is not a field in the mhead structure
237 because we want sizeof (struct mhead)
238 to describe the overhead for when the block is in use,
239 and we do not want the free-list pointer to count in that. */
241 #define CHAIN(a) \
242 (*(struct mhead **) (sizeof (char *) + (char *) (a)))
244 #ifdef rcheck
246 /* To implement range checking, we write magic values in at the beginning and
247 end of each allocated block, and make sure they are undisturbed whenever a
248 free or a realloc occurs. */
249 /* Written in each of the 4 bytes following the block's real space */
250 #define MAGIC1 0x55
251 /* Written in the 4 bytes before the block's real space */
252 #define MAGIC4 0x55555555
253 #define ASSERT(p) if (!(p)) botch("p"); else
254 #define EXTRA 4 /* 4 bytes extra for MAGIC1s */
255 #else
256 #define ASSERT(p)
257 #define EXTRA 0
258 #endif /* rcheck */
261 /* nextf[i] is free list of blocks of size 2**(i + 3) */
263 static struct mhead *nextf[30];
265 /* busy[i] is nonzero while allocation of block size i is in progress. */
267 static char busy[30];
269 /* Number of bytes of writable memory we can expect to be able to get */
270 static unsigned int lim_data;
272 /* Level number of warnings already issued.
273 0 -- no warnings issued.
274 1 -- 75% warning already issued.
275 2 -- 85% warning already issued.
277 static int warnlevel;
279 /* Function to call to issue a warning;
280 0 means don't issue them. */
281 static void (*warnfunction) ();
283 /* nonzero once initial bunch of free blocks made */
284 static int gotpool;
286 char *_malloc_base;
288 static void getpool ();
290 /* Cause reinitialization based on job parameters;
291 also declare where the end of pure storage is. */
292 void
293 malloc_init (start, warnfun)
294 char *start;
295 void (*warnfun) ();
297 if (start)
298 data_space_start = start;
299 lim_data = 0;
300 warnlevel = 0;
301 warnfunction = warnfun;
304 /* Return the maximum size to which MEM can be realloc'd
305 without actually requiring copying. */
308 malloc_usable_size (mem)
309 char *mem;
311 int blocksize = 8 << (((struct mhead *) mem) - 1) -> mh_index;
313 return blocksize - sizeof (struct mhead) - EXTRA;
316 static void
317 morecore (nu) /* ask system for more memory */
318 register int nu; /* size index to get more of */
320 char *sbrk ();
321 register char *cp;
322 register int nblks;
323 register unsigned int siz;
324 int oldmask;
326 #ifdef BSD
327 #ifndef BSD4_1
328 /* ?? There was a suggestion not to block SIGILL, somehow for GDB's sake. */
329 oldmask = sigsetmask (-1);
330 #endif
331 #endif
333 if (!data_space_start)
335 data_space_start = start_of_data ();
338 if (lim_data == 0)
339 get_lim_data ();
341 /* On initial startup, get two blocks of each size up to 1k bytes */
342 if (!gotpool)
343 { getpool (); getpool (); gotpool = 1; }
345 /* Find current end of memory and issue warning if getting near max */
347 #ifndef VMS
348 /* Maximum virtual memory on VMS is difficult to calculate since it
349 * depends on several dynmacially changing things. Also, alignment
350 * isn't that important. That is why much of the code here is ifdef'ed
351 * out for VMS systems.
353 cp = sbrk (0);
354 siz = cp - data_space_start;
356 if (warnfunction)
357 switch (warnlevel)
359 case 0:
360 if (siz > (lim_data / 4) * 3)
362 warnlevel++;
363 (*warnfunction) ("Warning: past 75% of memory limit");
365 break;
366 case 1:
367 if (siz > (lim_data / 20) * 17)
369 warnlevel++;
370 (*warnfunction) ("Warning: past 85% of memory limit");
372 break;
373 case 2:
374 if (siz > (lim_data / 20) * 19)
376 warnlevel++;
377 (*warnfunction) ("Warning: past 95% of memory limit");
379 break;
382 if ((int) cp & 0x3ff) /* land on 1K boundaries */
383 sbrk (1024 - ((int) cp & 0x3ff));
384 #endif /* not VMS */
386 /* Take at least 2k, and figure out how many blocks of the desired size
387 we're about to get */
388 nblks = 1;
389 if ((siz = nu) < 8)
390 nblks = 1 << ((siz = 8) - nu);
392 if ((cp = sbrk (1 << (siz + 3))) == (char *) -1)
394 #ifdef BSD
395 #ifndef BSD4_1
396 sigsetmask (oldmask);
397 #endif
398 #endif
399 return; /* no more room! */
401 malloc_sbrk_used = siz;
402 malloc_sbrk_unused = lim_data - siz;
404 #ifndef VMS
405 if ((int) cp & 7)
406 { /* shouldn't happen, but just in case */
407 cp = (char *) (((int) cp + 8) & ~7);
408 nblks--;
410 #endif /* not VMS */
412 /* save new header and link the nblks blocks together */
413 nextf[nu] = (struct mhead *) cp;
414 siz = 1 << (nu + 3);
415 while (1)
417 ((struct mhead *) cp) -> mh_alloc = ISFREE;
418 ((struct mhead *) cp) -> mh_index = nu;
419 if (--nblks <= 0) break;
420 CHAIN ((struct mhead *) cp) = (struct mhead *) (cp + siz);
421 cp += siz;
423 CHAIN ((struct mhead *) cp) = 0;
425 #ifdef BSD
426 #ifndef BSD4_1
427 sigsetmask (oldmask);
428 #endif
429 #endif
432 static void
433 getpool ()
435 register int nu;
436 char * sbrk ();
437 register char *cp = sbrk (0);
439 if ((int) cp & 0x3ff) /* land on 1K boundaries */
440 sbrk (1024 - ((int) cp & 0x3ff));
442 /* Record address of start of space allocated by malloc. */
443 if (_malloc_base == 0)
444 _malloc_base = cp;
446 /* Get 2k of storage */
448 cp = sbrk (04000);
449 if (cp == (char *) -1)
450 return;
452 /* Divide it into an initial 8-word block
453 plus one block of size 2**nu for nu = 3 ... 10. */
455 CHAIN (cp) = nextf[0];
456 nextf[0] = (struct mhead *) cp;
457 ((struct mhead *) cp) -> mh_alloc = ISFREE;
458 ((struct mhead *) cp) -> mh_index = 0;
459 cp += 8;
461 for (nu = 0; nu < 7; nu++)
463 CHAIN (cp) = nextf[nu];
464 nextf[nu] = (struct mhead *) cp;
465 ((struct mhead *) cp) -> mh_alloc = ISFREE;
466 ((struct mhead *) cp) -> mh_index = nu;
467 cp += 8 << nu;
471 char *
472 malloc (n) /* get a block */
473 unsigned n;
475 register struct mhead *p;
476 register unsigned int nbytes;
477 register int nunits = 0;
479 /* Figure out how many bytes are required, rounding up to the nearest
480 multiple of 8, then figure out which nestf[] area to use.
481 Both the beginning of the header and the beginning of the
482 block should be on an eight byte boundary. */
483 #ifdef SUNOS_LOCALTIME_BUG
484 /* SunOS 4.1 localtime scribbles on the ninth byte. */
485 nbytes = (n + ((sizeof *p + 15) & ~15) + EXTRA + 15) & ~15;
486 #else
487 nbytes = (n + ((sizeof *p + 7) & ~7) + EXTRA + 7) & ~7;
488 #endif
490 register unsigned int shiftr = (nbytes - 1) >> 2;
492 while (shiftr >>= 1)
493 nunits++;
496 /* In case this is reentrant use of malloc from signal handler,
497 pick a block size that no other malloc level is currently
498 trying to allocate. That's the easiest harmless way not to
499 interfere with the other level of execution. */
500 while (busy[nunits]) nunits++;
501 busy[nunits] = 1;
503 /* If there are no blocks of the appropriate size, go get some */
504 /* COULD SPLIT UP A LARGER BLOCK HERE ... ACT */
505 if (nextf[nunits] == 0)
506 morecore (nunits);
508 /* Get one block off the list, and set the new list head */
509 if ((p = nextf[nunits]) == 0)
511 busy[nunits] = 0;
512 return 0;
514 nextf[nunits] = CHAIN (p);
515 busy[nunits] = 0;
517 /* Check for free block clobbered */
518 /* If not for this check, we would gobble a clobbered free chain ptr */
519 /* and bomb out on the NEXT allocate of this size block */
520 if (p -> mh_alloc != ISFREE || p -> mh_index != nunits)
521 #ifdef rcheck
522 botch ("block on free list clobbered");
523 #else /* not rcheck */
524 abort ();
525 #endif /* not rcheck */
527 /* Fill in the info, and if range checking, set up the magic numbers */
528 p -> mh_alloc = ISALLOC;
529 #ifdef rcheck
530 p -> mh_nbytes = n;
531 p -> mh_magic4 = MAGIC4;
533 /* Get the location n after the beginning of the user's space. */
534 register char *m = (char *) p + ((sizeof *p + 7) & ~7) + n;
536 *m++ = MAGIC1, *m++ = MAGIC1, *m++ = MAGIC1, *m = MAGIC1;
538 #else /* not rcheck */
539 p -> mh_size = n;
540 #endif /* not rcheck */
541 #ifdef MSTATS
542 nmalloc[nunits]++;
543 nmal++;
544 #endif /* MSTATS */
545 return (char *) p + ((sizeof *p + 7) & ~7);
548 free (mem)
549 char *mem;
551 register struct mhead *p;
553 register char *ap = mem;
555 if (ap == 0)
556 return;
558 p = (struct mhead *) (ap - ((sizeof *p + 7) & ~7));
559 if (p -> mh_alloc == ISMEMALIGN)
561 ap -= p->mh_size;
562 p = (struct mhead *) (ap - ((sizeof *p + 7) & ~7));
565 #ifndef rcheck
566 if (p -> mh_alloc != ISALLOC)
567 abort ();
569 #else rcheck
570 if (p -> mh_alloc != ISALLOC)
572 if (p -> mh_alloc == ISFREE)
573 botch ("free: Called with already freed block argument\n");
574 else
575 botch ("free: Called with bad argument\n");
578 ASSERT (p -> mh_magic4 == MAGIC4);
579 ap += p -> mh_nbytes;
580 ASSERT (*ap++ == MAGIC1); ASSERT (*ap++ == MAGIC1);
581 ASSERT (*ap++ == MAGIC1); ASSERT (*ap == MAGIC1);
582 #endif /* rcheck */
585 register int nunits = p -> mh_index;
587 ASSERT (nunits <= 29);
588 p -> mh_alloc = ISFREE;
590 /* Protect against signal handlers calling malloc. */
591 busy[nunits] = 1;
592 /* Put this block on the free list. */
593 CHAIN (p) = nextf[nunits];
594 nextf[nunits] = p;
595 busy[nunits] = 0;
597 #ifdef MSTATS
598 nmalloc[nunits]--;
599 nfre++;
600 #endif /* MSTATS */
604 char *
605 realloc (mem, n)
606 char *mem;
607 register unsigned n;
609 register struct mhead *p;
610 register unsigned int tocopy;
611 register unsigned int nbytes;
612 register int nunits;
614 if (mem == 0)
615 return malloc (n);
616 p = (struct mhead *) (mem - ((sizeof *p + 7) & ~7));
617 nunits = p -> mh_index;
618 ASSERT (p -> mh_alloc == ISALLOC);
619 #ifdef rcheck
620 ASSERT (p -> mh_magic4 == MAGIC4);
622 register char *m = mem + (tocopy = p -> mh_nbytes);
623 ASSERT (*m++ == MAGIC1); ASSERT (*m++ == MAGIC1);
624 ASSERT (*m++ == MAGIC1); ASSERT (*m == MAGIC1);
626 #else /* not rcheck */
627 if (p -> mh_index >= 13)
628 tocopy = (1 << (p -> mh_index + 3)) - ((sizeof *p + 7) & ~7);
629 else
630 tocopy = p -> mh_size;
631 #endif /* not rcheck */
633 /* See if desired size rounds to same power of 2 as actual size. */
634 nbytes = (n + ((sizeof *p + 7) & ~7) + EXTRA + 7) & ~7;
636 /* If ok, use the same block, just marking its size as changed. */
637 if (nbytes > (4 << nunits) && nbytes <= (8 << nunits))
639 #ifdef rcheck
640 register char *m = mem + tocopy;
641 *m++ = 0; *m++ = 0; *m++ = 0; *m++ = 0;
642 p-> mh_nbytes = n;
643 m = mem + n;
644 *m++ = MAGIC1; *m++ = MAGIC1; *m++ = MAGIC1; *m++ = MAGIC1;
645 #else /* not rcheck */
646 p -> mh_size = n;
647 #endif /* not rcheck */
648 return mem;
651 if (n < tocopy)
652 tocopy = n;
654 register char *new;
656 if ((new = malloc (n)) == 0)
657 return 0;
658 bcopy (mem, new, tocopy);
659 free (mem);
660 return new;
664 #ifndef VMS
666 char *
667 memalign (alignment, size)
668 unsigned alignment, size;
670 register char *ptr = malloc (size + alignment);
671 register char *aligned;
672 register struct mhead *p;
674 if (ptr == 0)
675 return 0;
676 /* If entire block has the desired alignment, just accept it. */
677 if (((int) ptr & (alignment - 1)) == 0)
678 return ptr;
679 /* Otherwise, get address of byte in the block that has that alignment. */
680 aligned = (char *) (((int) ptr + alignment - 1) & -alignment);
682 /* Store a suitable indication of how to free the block,
683 so that free can find the true beginning of it. */
684 p = (struct mhead *) aligned - 1;
685 p -> mh_size = aligned - ptr;
686 p -> mh_alloc = ISMEMALIGN;
687 return aligned;
690 #ifndef HPUX
691 /* This runs into trouble with getpagesize on HPUX.
692 Patching out seems cleaner than the ugly fix needed. */
693 char *
694 valloc (size)
696 return memalign (getpagesize (), size);
698 #endif /* not HPUX */
699 #endif /* not VMS */
701 #ifdef MSTATS
702 /* Return statistics describing allocation of blocks of size 2**n. */
704 struct mstats_value
706 int blocksize;
707 int nfree;
708 int nused;
711 struct mstats_value
712 malloc_stats (size)
713 int size;
715 struct mstats_value v;
716 register int i;
717 register struct mhead *p;
719 v.nfree = 0;
721 if (size < 0 || size >= 30)
723 v.blocksize = 0;
724 v.nused = 0;
725 return v;
728 v.blocksize = 1 << (size + 3);
729 v.nused = nmalloc[size];
731 for (p = nextf[size]; p; p = CHAIN (p))
732 v.nfree++;
734 return v;
737 malloc_mem_used ()
739 int i;
740 int size_used;
742 size_used = 0;
744 for (i = 0; i < 30; i++)
746 int allocation_size = 1 << (i + 3);
747 struct mhead *p;
749 size_used += nmalloc[i] * allocation_size;
752 return size_used;
755 int
756 malloc_mem_free ()
758 int i;
759 int size_unused;
761 size_unused = 0;
763 for (i = 0; i < 30; i++)
765 int allocation_size = 1 << (i + 3);
766 struct mhead *p;
768 for (p = nextf[i]; p ; p = CHAIN (p))
769 size_unused += allocation_size;
772 return size_unused;
774 #endif /* MSTATS */
777 * This function returns the total number of bytes that the process
778 * will be allowed to allocate via the sbrk(2) system call. On
779 * BSD systems this is the total space allocatable to stack and
780 * data. On USG systems this is the data space only.
783 #ifdef USG
785 get_lim_data ()
787 extern long ulimit ();
789 #ifdef ULIMIT_BREAK_VALUE
790 lim_data = ULIMIT_BREAK_VALUE;
791 #else
792 lim_data = ulimit (3, 0);
793 #endif
795 lim_data -= (long) data_space_start;
798 #else /* not USG */
799 #ifndef BSD4_2
801 get_lim_data ()
803 lim_data = vlimit (LIM_DATA, -1);
806 #else /* BSD4_2 */
808 get_lim_data ()
810 struct rlimit XXrlimit;
812 getrlimit (RLIMIT_DATA, &XXrlimit);
813 #ifdef RLIM_INFINITY
814 lim_data = XXrlimit.rlim_cur & RLIM_INFINITY; /* soft limit */
815 #else
816 lim_data = XXrlimit.rlim_cur; /* soft limit */
817 #endif
820 #endif /* BSD4_2 */
821 #endif /* not USG */
823 #ifdef VMS
824 /* There is a problem when dumping and restoring things on VMS. Calls
825 * to SBRK don't necessarily result in contiguous allocation. Dumping
826 * doesn't work when it isn't. Therefore, we make the initial
827 * allocation contiguous by allocating a big chunk, and do SBRKs from
828 * there. Once Emacs has dumped there is no reason to continue
829 * contiguous allocation, malloc doesn't depend on it.
831 * There is a further problem of using brk and sbrk while using VMS C
832 * run time library routines malloc, calloc, etc. The documentation
833 * says that this is a no-no, although I'm not sure why this would be
834 * a problem. In any case, we remove the necessity to call brk and
835 * sbrk, by calling calloc (to assure zero filled data) rather than
836 * sbrk.
838 * VMS_ALLOCATION_SIZE is the size of the allocation array. This
839 * should be larger than the malloc size before dumping. Making this
840 * too large will result in the startup procedure slowing down since
841 * it will require more space and time to map it in.
843 * The value for VMS_ALLOCATION_SIZE in the following define was determined
844 * by running emacs linked (and a large allocation) with the debugger and
845 * looking to see how much storage was used. The allocation was 201 pages,
846 * so I rounded it up to a power of two.
848 #ifndef VMS_ALLOCATION_SIZE
849 #define VMS_ALLOCATION_SIZE (512*256)
850 #endif
852 /* Use VMS RTL definitions */
853 #undef sbrk
854 #undef brk
855 #undef malloc
856 int vms_out_initial = 0;
857 char vms_initial_buffer[VMS_ALLOCATION_SIZE];
858 static char *vms_current_brk = &vms_initial_buffer;
859 static char *vms_end_brk = &vms_initial_buffer[VMS_ALLOCATION_SIZE-1];
861 #include <stdio.h>
863 char *
864 sys_sbrk (incr)
865 int incr;
867 char *sbrk(), *temp, *ptr;
869 if (vms_out_initial)
871 /* out of initial allocation... */
872 if (!(temp = malloc (incr)))
873 temp = (char *) -1;
875 else
877 /* otherwise, go out of our area */
878 ptr = vms_current_brk + incr; /* new current_brk */
879 if (ptr <= vms_end_brk)
881 temp = vms_current_brk;
882 vms_current_brk = ptr;
884 else
886 vms_out_initial = 1; /* mark as out of initial allocation */
887 if (!(temp = malloc (incr)))
888 temp = (char *) -1;
891 return temp;
893 #endif /* VMS */