xref: /csrg-svn/bin/csh/alloc.c (revision 60765)
147823Sbostic /*-
2*60765Sbostic  * Copyright (c) 1983, 1991, 1993
3*60765Sbostic  *	The Regents of the University of California.  All rights reserved.
447823Sbostic  *
547823Sbostic  * %sccs.include.redist.c%
621948Sdist  */
721948Sdist 
815537Slayer #ifndef lint
9*60765Sbostic static char sccsid[] = "@(#)alloc.c	8.1 (Berkeley) 05/31/93";
1047823Sbostic #endif /* not lint */
1147823Sbostic 
1250033Schristos #include <sys/types.h>
1350033Schristos #include <unistd.h>
1452477Schristos #include <stdlib.h>
1550033Schristos #if __STDC__
1650033Schristos # include <stdarg.h>
1750033Schristos #else
1850033Schristos # include <varargs.h>
1950033Schristos #endif
2050033Schristos 
2150023Sbostic #include "csh.h"
2250033Schristos #include "extern.h"
2315537Slayer 
2449992Sbostic char   *memtop = NULL;		/* PWP: top of current memory */
2549992Sbostic char   *membot = NULL;		/* PWP: bottom of allocatable memory */
2649992Sbostic 
2749992Sbostic ptr_t
Malloc(n)2849992Sbostic Malloc(n)
2949992Sbostic     size_t  n;
3049992Sbostic {
3149992Sbostic     ptr_t   ptr;
3249992Sbostic 
3352477Schristos     if (membot == NULL)
3452477Schristos 	memtop = membot = sbrk(0);
3549992Sbostic     if ((ptr = malloc(n)) == (ptr_t) 0) {
3649992Sbostic 	child++;
3749992Sbostic 	stderror(ERR_NOMEM);
3849992Sbostic     }
3952477Schristos     return (ptr);
4049992Sbostic }
4149992Sbostic 
4252477Schristos ptr_t
Realloc(p,n)4349992Sbostic Realloc(p, n)
4449992Sbostic     ptr_t   p;
4549992Sbostic     size_t  n;
4649992Sbostic {
4749992Sbostic     ptr_t   ptr;
4849992Sbostic 
4952477Schristos     if (membot == NULL)
5052477Schristos 	memtop = membot = sbrk(0);
5152477Schristos     if ((ptr = realloc(p, n)) == (ptr_t) 0) {
5249992Sbostic 	child++;
5349992Sbostic 	stderror(ERR_NOMEM);
5449992Sbostic     }
5552477Schristos     return (ptr);
5649992Sbostic }
5749992Sbostic 
5852477Schristos ptr_t
Calloc(s,n)5949992Sbostic Calloc(s, n)
6049992Sbostic     size_t  s, n;
6149992Sbostic {
6249992Sbostic     ptr_t   ptr;
6349992Sbostic 
6452477Schristos     if (membot == NULL)
6552477Schristos 	memtop = membot = sbrk(0);
6652477Schristos     if ((ptr = calloc(s, n)) == (ptr_t) 0) {
6749992Sbostic 	child++;
6849992Sbostic 	stderror(ERR_NOMEM);
6949992Sbostic     }
7049992Sbostic 
7152477Schristos     return (ptr);
7249992Sbostic }
7349992Sbostic 
7449992Sbostic void
Free(p)7549992Sbostic Free(p)
7649992Sbostic     ptr_t   p;
7749992Sbostic {
7849992Sbostic     if (p)
7949992Sbostic 	free(p);
8049992Sbostic }
8149992Sbostic 
8215537Slayer /*
8315537Slayer  * mstats - print out statistics about malloc
8449992Sbostic  *
8515537Slayer  * Prints two lines of numbers, one showing the length of the free list
8615537Slayer  * for each size category, the second showing the number of mallocs -
8715537Slayer  * frees for each size category.
8815537Slayer  */
8949992Sbostic void
9050439Schristos /*ARGSUSED*/
showall(v,t)9150439Schristos showall(v, t)
9250439Schristos     Char **v;
9350439Schristos     struct command *t;
941285Sbill {
9552477Schristos     memtop = (char *) sbrk(0);
9650439Schristos     (void) fprintf(cshout, "Allocated memory from 0x%lx to 0x%lx (%ld).\n",
9760237Schristos 	    (unsigned long) membot, (unsigned long) memtop, memtop - membot);
981285Sbill }
99