xref: /original-bsd/bin/csh/alloc.c (revision 42f60e33)
1 /*-
2  * Copyright (c) 1983, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)alloc.c	8.1 (Berkeley) 05/31/93";
10 #endif /* not lint */
11 
12 #include <sys/types.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #if __STDC__
16 # include <stdarg.h>
17 #else
18 # include <varargs.h>
19 #endif
20 
21 #include "csh.h"
22 #include "extern.h"
23 
24 char   *memtop = NULL;		/* PWP: top of current memory */
25 char   *membot = NULL;		/* PWP: bottom of allocatable memory */
26 
27 ptr_t
28 Malloc(n)
29     size_t  n;
30 {
31     ptr_t   ptr;
32 
33     if (membot == NULL)
34 	memtop = membot = sbrk(0);
35     if ((ptr = malloc(n)) == (ptr_t) 0) {
36 	child++;
37 	stderror(ERR_NOMEM);
38     }
39     return (ptr);
40 }
41 
42 ptr_t
43 Realloc(p, n)
44     ptr_t   p;
45     size_t  n;
46 {
47     ptr_t   ptr;
48 
49     if (membot == NULL)
50 	memtop = membot = sbrk(0);
51     if ((ptr = realloc(p, n)) == (ptr_t) 0) {
52 	child++;
53 	stderror(ERR_NOMEM);
54     }
55     return (ptr);
56 }
57 
58 ptr_t
59 Calloc(s, n)
60     size_t  s, n;
61 {
62     ptr_t   ptr;
63 
64     if (membot == NULL)
65 	memtop = membot = sbrk(0);
66     if ((ptr = calloc(s, n)) == (ptr_t) 0) {
67 	child++;
68 	stderror(ERR_NOMEM);
69     }
70 
71     return (ptr);
72 }
73 
74 void
75 Free(p)
76     ptr_t   p;
77 {
78     if (p)
79 	free(p);
80 }
81 
82 /*
83  * mstats - print out statistics about malloc
84  *
85  * Prints two lines of numbers, one showing the length of the free list
86  * for each size category, the second showing the number of mallocs -
87  * frees for each size category.
88  */
89 void
90 /*ARGSUSED*/
91 showall(v, t)
92     Char **v;
93     struct command *t;
94 {
95     memtop = (char *) sbrk(0);
96     (void) fprintf(cshout, "Allocated memory from 0x%lx to 0x%lx (%ld).\n",
97 	    (unsigned long) membot, (unsigned long) memtop, memtop - membot);
98 }
99