]> andersk Git - moira.git/blob - server/mr_smalloc.c
5f8b770b025b3936e762482f30195c8f9b7ac243
[moira.git] / server / mr_smalloc.c
1 #include <stdio.h>
2 botch(message)
3         char *message;
4 {
5         fprintf(stderr, "Malloc botch: %s\n", message);
6         abort();
7 }       
8
9 #define rcheck
10 /****************************************************************
11  *                                                              *
12  *              Storage Allocator for Foundation.               *
13  *              Built from gnuemacs storage allocator           *
14  *                                                              *
15  ****************************************************************/
16
17 /*   Copyright (C) 1985 Richard M. Stallman,
18     based mostly on the public domain work of others.
19
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.
25
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.
33
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.
42
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.
56
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.
61
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!  */
65 \f
66 /****************************************************************
67  *                                                              *
68  *              Helpful historical comments                     *
69  *                                                              *
70  ****************************************************************/
71
72 /*
73  * @(#)nmalloc.c 1 (Caltech) 2/21/82
74  *
75  *      U of M Modified: 20 Jun 1983 ACT: strange hacks for Emacs
76  *
77  *      Nov 1983, Mike@BRL, Added support for 4.1C/4.2 BSD.
78  *
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.
86  *
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.
89  *
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.
96  *
97  * Jan 86, WDC: Removed Emacs specific stuff, and neatened a few comments.
98  *
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.
105  *
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.
113  *
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.
119  *
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.
125  */
126 \f
127 /****************************************************************
128  *                                                              *
129  *       Includes, declarations, and definitions                *
130  *                                                              *
131  ****************************************************************/
132
133 /* Determine which kind of system this is.  */
134 #include <signal.h>
135 #ifndef SIGTSTP
136 #define USG
137 #else /* SIGTSTP */
138 #ifdef SIGIO
139 #define BSD42
140 #endif /* SIGIO */
141 #endif /* SIGTSTP */
142
143 #ifndef BSD42
144 #ifndef USG
145 #include <sys/vlimit.h>         /* warn the user when near the end */
146 #endif
147 #else /* if BSD42 */
148 #include <sys/time.h>
149 #include <sys/resource.h>
150 #endif /* BSD42 */
151 #include <moira_site.h>
152
153 #ifdef scribblecheck
154 #define rcheck
155 #endif /* we need to have range data to use block boundary checking */
156
157 #ifdef rcheck
158 /*
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.
162  */
163
164 /* Written in each of the 4 bytes following the block's real space */
165 #define MAGIC1 0x55
166 #define MAGICFREE 0x69          /* 0110 1001 Magic value for Free blocks */
167
168 /* Written in the 4 bytes before the block's real space */
169 #define MAGIC4 0x55555555
170 #define MAGICFREE4 0x69696969
171
172 #define ASSERT(p) if (!(p)) botch("p"); else
173 #define EXTRA  4                /* 4 bytes extra for MAGIC1s */
174 #else
175 #define ASSERT(p)
176 #define EXTRA  0
177 #endif /* rcheck */
178
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 */
182
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.
188  */
189 struct mhead {
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 */
195 #ifdef rcheck
196         int      mh_magic4;     /* should be == MAGIC4 */
197 #endif /* rcheck */
198 };
199
200 /*
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
206  * to count in that.
207  */
208 #define CHAIN(a) \
209   (*(struct mhead **) (sizeof (char *) + (char *) (a)))
210
211 \f
212 /****************************************************************
213  *                                                              *
214  *              Variable Creations                              *
215  *                                                              *
216  ****************************************************************/
217
218 extern char etext;
219 extern char *start_of_data ();  /* This seems necessary for USG */
220
221 #ifdef notdef
222
223 /* These two are for user programs to look at, when they are interested.  */
224
225 int malloc_sbrk_used;       /* amount of data space used now */
226 int malloc_sbrk_unused;     /* amount more we can have */
227 #endif notdef
228 /* start of data space; can be changed by calling init_malloc */
229 static char *data_space_start;
230
231 #ifdef MSTATS
232 /*
233  * nmalloc[i] is the difference between the number of mallocs and frees
234  * for a given block size.
235  */
236 static int nmalloc[30];
237 static int nmal, nfre;
238 #endif /* MSTATS */
239
240 /*
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
244  * to the second.
245  */
246 static struct mhead *nextf[30];
247
248 /* Number of bytes of writable memory we can expect to be able to get */
249 static int lim_data;
250 /* Level number of warnings already issued.
251  * 0 -- no warnings issued.
252  * 1 -- 75% warning already issued.
253  * 2 -- 85% warning already issued.
254  */
255 static int warnlevel;
256
257 #ifdef notdef
258 /* nonzero once initial bunch of free blocks made */
259 static int gotpool;
260 #endif notdef
261 \f
262 /****************************************************************
263  *                                                              *
264  *              Start of procedures                             *
265  *                                                              *
266  *      malloc_init, m_blocksize                                *
267  *                                                              *
268  ****************************************************************/
269
270 /*
271  * Cause reinitialization based on job parameters;
272  * also declare where the end of pure storage is.
273  */
274 malloc_init (start)
275      char *start;
276 {
277   data_space_start = start;
278   lim_data = 0;
279   warnlevel = 0;
280 }
281
282 int m_blocksize(a_block)
283      char *a_block;
284 {
285   return(((struct mhead *)a_block-1)->mh_nbytes);
286 }
287
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.
291  */
292 extern int MEinitLists();
293
294 static int (*foo)() = MEinitLists;
295 #endif
296         
297 \f
298 /****************************************************************
299  *                                                              *
300  *    morecore - Ask the system for more memory                 *
301  *                                                              *
302  ****************************************************************/
303
304 static
305 morecore (nu)                   /* ask system for more memory */
306      register int nu;           /* size index to get more of  */
307 {
308   char *sbrk ();
309   register char *cp;
310   register int nblks;
311   register int siz;
312
313 #ifdef notdef
314   if (!data_space_start)
315     {
316 #if defined(USG)
317       data_space_start = start_of_data ();
318 #else /* not USG */
319       data_space_start = &etext;
320 #endif /* not USG */
321     }
322
323   if (lim_data == 0)
324     get_lim_data ();
325
326   /* On initial startup, get two blocks of each size up to 1k bytes */
327   if (!gotpool)
328     getpool (), getpool (), gotpool = 1;
329
330   /* Find current end of memory and issue warning if getting near max */
331
332   cp = sbrk (0);
333   siz = cp - data_space_start;
334   malloc_sbrk_used = siz;
335   malloc_sbrk_unused = lim_data - siz;
336
337   switch (warnlevel)
338     {
339     case 0: 
340       if (siz > (lim_data / 4) * 3)
341         {
342           warnlevel++;
343           malloc_warning ("Warning: past 75% of memory limit");
344         }
345       break;
346     case 1: 
347       if (siz > (lim_data / 20) * 17)
348         {
349           warnlevel++;
350           malloc_warning ("Warning: past 85% of memory limit");
351         }
352       break;
353     case 2: 
354       if (siz > (lim_data / 20) * 19)
355         {
356           warnlevel++;
357           malloc_warning ("Warning: past 95% of memory limit");
358         }
359       break;
360     }
361
362   if ((int) cp & 0x3ff) /* land on 1K boundaries */
363     sbrk (1024 - ((int) cp & 0x3ff));
364 #endif notdef
365   
366   /* Take at least 2k, and figure out how many blocks of the desired size
367     we're about to get */
368   nblks = 1;
369   if ((siz = nu) < 8)
370     nblks = 1 << ((siz = 8) - nu);
371 #if INGRESVER == 5 && defined(vax)
372   {
373       char *tcp;          
374       if (MEalloc(1, 1 << (siz+3), &tcp))
375         return;                 /* No more room! */
376       cp = tcp;
377   }
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) */
382   if ((int) cp & 7)
383     {           /* shouldn't happen, but just in case */
384       cp = (char *) (((int) cp + 8) & ~7);
385       nblks--;
386     }
387
388   /* save new header and link the nblks blocks together */
389   nextf[nu] = (struct mhead *) cp;
390   siz = 1 << (nu + 3);
391   while (1)
392     {
393       ((struct mhead *) cp) -> mh_alloc = ISFREE;
394       ((struct mhead *) cp) -> mh_index = nu;
395 #ifdef rcheck
396       ((struct mhead *) cp) -> mh_magic4 = MAGICFREE4;
397 #endif /* rcheck */
398 #ifdef scribblecheck
399     {
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;
405     }
406 #endif /* scribblecheck */
407
408       /* Clear newly allocated blocks, to match free ones */
409       if (--nblks <= 0) break;
410       CHAIN ((struct mhead *) cp) = (struct mhead *) (cp + siz);
411       cp += siz;
412     }
413   CHAIN ((struct mhead *) cp) = 0;
414 }
415 \f
416 /****************************************************************
417  *                                                              *
418  *      getpool - Get initial pools of small blocks             *
419  *                                                              *
420  ****************************************************************/
421 #ifdef notdef
422 static
423 getpool ()
424 {
425   register int nu;
426   register char *cp = sbrk (0);
427
428   if ((int) cp & 0x3ff) /* land on 1K boundaries */
429     sbrk (1024 - ((int) cp & 0x3ff));
430
431   /* Get 2k of storage */
432
433   cp = sbrk (04000);
434   if (cp == (char *) -1)
435     return;
436
437   /* Divide it into an initial 8-word block
438      plus one block of size 2**nu for nu = 3 ... 10.  */
439
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;
444 #ifdef rcheck
445       ((struct mhead *) cp) -> mh_magic4 = MAGICFREE4;
446 #endif /* rcheck */
447   cp += 8;
448
449   for (nu = 0; nu < 7; nu++)
450     {
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;
455 #ifdef rcheck
456       ((struct mhead *) cp) -> mh_magic4 = MAGICFREE4;
457 #endif /* rcheck */
458 #ifdef scribblecheck
459     {
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;
464     }
465 #endif /* scribblecheck */
466       cp += 8 << nu;
467     }
468 }
469 #endif notdef
470 \f
471 /****************************************************************
472  *                                                              *
473  *      malloc - get a block of space from a pool               *
474  *                                                              *
475  ****************************************************************/
476
477 char *
478 malloc (n)              /* get a block */
479      unsigned n;
480 {
481   register struct mhead *p;
482   register unsigned int nbytes;
483   register int nunits = 0;
484
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;
488   {
489     register unsigned int   shiftr = (nbytes - 1) >> 2;
490
491     while (shiftr >>= 1)
492       nunits++;
493   }
494
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)
498     morecore (nunits);
499
500   /* Get one block off the list, and set the new list head */
501   if ((p = nextf[nunits]) == 0)
502     return 0;
503   nextf[nunits] = CHAIN (p);
504
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)
509 #ifdef rcheck
510     botch ("block on free list clobbered");
511 #else /* not rcheck */
512     abort ();
513 #endif /* not rcheck */
514 #ifdef rcheck
515   if (p -> mh_magic4 != MAGICFREE4)
516     botch ("Magic in block on free list clobbered");
517 #endif /* rcheck */
518 #ifdef scribblecheck
519   /* Check for block filled with magic numbers, then change to zeros */
520   {
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)))
525       *m++=(char)0;
526     /* so, status comes out as 1 if ok, 0 if terminated */
527     if (!block_valid) botch ("data on free list damaged");
528   }
529 #endif /* scribblecheck */
530   /* Fill in the info, and if range checking, set up the magic numbers */
531   p -> mh_alloc = ISALLOC;
532   p -> mh_nbytes = n;
533 #ifdef rcheck
534   p -> mh_magic4 = MAGIC4;
535   {
536     register char  *m = (char *) (p + 1) + n;
537 #ifdef scribblecheck
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 */
544   }
545 #endif /* not rcheck */
546 #ifdef MSTATS
547   nmalloc[nunits]++;
548   nmal++;
549 #endif /* MSTATS */
550   return (char *) (p + 1);
551 }
552 \f
553 /****************************************************************
554  *                                                              *
555  *      free - Free a block of space                            *
556  *                                                              *
557  ****************************************************************/
558
559 free (mem)
560      char *mem;
561 {
562   register struct mhead *p;
563   {
564     register char *ap = mem;
565
566     ASSERT (ap != 0);
567     p = (struct mhead *) ap - 1;
568     ASSERT (p -> mh_alloc == ISALLOC);
569 #ifdef rcheck
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);
575 #endif /* rcheck */
576   }
577   {
578     register int nunits = p -> mh_index;
579
580     ASSERT (nunits <= 29);
581 #ifdef scribblecheck
582     {
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;
592     }
593 #endif /* scribblecheck */
594     p -> mh_alloc = ISFREE;
595     CHAIN (p) = nextf[nunits];
596     nextf[nunits] = p;
597 #ifdef MSTATS
598     nmalloc[nunits]--;
599     nfre++;
600 #endif /* MSTATS */
601   }
602 }
603 \f
604 /****************************************************************
605  *                                                              *
606  *      realloc - resize a block, copy if necessary             *
607  *                                                              *
608  ****************************************************************/
609
610 char *
611 realloc (mem, n)
612      char *mem;
613      register unsigned n;
614 {
615   register struct mhead *p;
616   register unsigned int tocopy;
617   register int nbytes;
618   register int nunits;
619
620   if ((p = (struct mhead *) mem) == 0)
621     return malloc (n);
622   p--;
623   nunits = p -> mh_index;
624   ASSERT (p -> mh_alloc == ISALLOC);
625   tocopy = p -> mh_nbytes;
626 #ifdef rcheck
627   ASSERT (p -> mh_magic4 == MAGIC4);
628   {
629     register char *m = mem + tocopy;
630 #ifdef scribblecheck
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 */
639   }
640 #endif /* not rcheck */
641
642   /* See if desired size rounds to same power of 2 as actual size. */
643   nbytes = (n + sizeof *p + EXTRA + 7) & ~7;
644
645   /* If ok, use the same block, just marking its size as changed.  */
646   if (nbytes > (4 << nunits) && nbytes <= (8 << nunits))
647     {
648       /* Here we check on realloc if we are grabbing unused space */
649 #ifdef rcheck
650       register char *m = mem + tocopy;
651 #ifdef scribblecheck
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 */
657       m = mem + n;
658 #ifdef 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 */
664       p-> mh_nbytes = n;
665       return mem;
666     }
667
668   if (n < tocopy)
669     tocopy = n;
670   {
671     register char *new;
672
673     if ((new = malloc (n)) == 0)
674       return 0;
675     bcopy (mem, new, tocopy);
676     free (mem);
677     return new;
678   }
679 }
680 \f
681 /****************************************************************
682  *                                                              *
683  *      Memory Statistics stuff                                 *
684  *                                                              *
685  ****************************************************************/
686
687 #ifdef MSTATS
688 /* Return statistics describing allocation of blocks of size 2**n. */
689
690 struct mstats_value
691   {
692     int blocksize;
693     int nfree;
694     int nused;
695   };
696
697 struct mstats_value
698 malloc_stats (size)
699      int size;
700 {
701   struct mstats_value v;
702   register int i;
703   register struct mhead *p;
704
705   v.nfree = 0;
706
707   if (size < 0 || size >= 30)
708     {
709       v.blocksize = 0;
710       v.nused = 0;
711       return v;
712     }
713
714   v.blocksize = 1 << (size + 3);
715   v.nused = nmalloc[size];
716
717   for (p = nextf[size]; p; p = CHAIN (p))
718     v.nfree++;
719
720   return v;
721 }
722 #endif /* MSTATS */
723 \f
724 #ifdef notdef
725 /****************************************************************
726  *                                                              *
727  *      Stuff having to do with determining memory limits       *
728  *                                                              *
729  ****************************************************************/
730
731 /*
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.
736  */
737
738 #ifdef USG
739
740 get_lim_data ()
741 {
742   extern long ulimit ();
743     
744   lim_data = ulimit (3, 0);
745   lim_data -= (long) data_space_start;
746 }
747
748 #else /* not USG */
749 #ifndef BSD42
750
751 get_lim_data ()
752 {
753   lim_data = vlimit (LIM_DATA, -1);
754 }
755
756 #else /* BSD42 */
757
758 get_lim_data ()
759 {
760   struct rlimit XXrlimit;
761
762   getrlimit (RLIMIT_DATA, &XXrlimit);
763   lim_data = XXrlimit.rlim_cur;         /* soft limit */
764 }
765
766 #endif /* BSD42 */
767 #endif /* not USG */
768 #endif notdef
769
770 /*
771  * Calloc - allocate and clear memory block
772  */
773 char *
774 calloc(num, size)
775         register unsigned num, size;
776 {
777         extern char *malloc();
778         register char *p;
779
780         size *= num;
781         if (p = malloc(size))
782                 bzero(p, size);
783         return (p);
784 }
785
786 cfree(p, num, size)
787         char *p;
788         unsigned num;
789         unsigned size;
790 {
791         free(p);
792 }
This page took 0.110488 seconds and 3 git commands to generate.