ack/util/ego/share/alloc.c

242 lines
4.6 KiB
C
Raw Normal View History

1994-06-24 11:31:16 +00:00
/* $Id$ */
1987-03-09 19:15:41 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
1984-11-26 15:04:22 +00:00
/* S H A R E D F I L E
*
* A L L O C . C
*/
#include <stdlib.h>
1984-11-26 15:04:22 +00:00
#include <stdio.h>
#include "types.h"
#include "debug.h"
#include "alloc.h"
void *myalloc(size_t);
1984-11-26 15:04:22 +00:00
#ifdef DEBUG
STATIC size_t maxuse, curruse;
1984-11-26 15:04:22 +00:00
void *newcore(size_t size)
1984-11-26 15:04:22 +00:00
{
if ((curruse += (size+2)) > maxuse) maxuse = curruse;
1984-11-26 15:04:22 +00:00
return myalloc(size);
}
void oldcore(void *p, size_t size)
1984-11-26 15:04:22 +00:00
{
curruse -= (size+2);
free(p);
}
void coreusage(void)
1984-11-26 15:04:22 +00:00
{
fprintf(stderr,"Maximal core usage (excl. buffers):%u\n",maxuse);
}
#endif
/*
* The following two sizetables contain the sizes of the various kinds
* of line and argument structures.
* The assumption when making the tables was that every non-byte object
* had to be aligned on an even boundary. On machines where alignment
* is worse ( for example a long has to be aligned on a longword bound )
* these tables should be revised.
* A wasteful but safe approach is to replace every line of them by
* sizeof(line_t)
* and
* sizeof(arg_t)
* respectively.
*/
#ifndef NOTCOMPACT
int lsizetab[] = {
2*sizeof(line_p)+2*sizeof(byte),
2*sizeof(line_p)+2*sizeof(byte)+sizeof(short),
2*sizeof(line_p)+2*sizeof(byte)+sizeof(offset),
2*sizeof(line_p)+2*sizeof(byte)+sizeof(lab_id),
2*sizeof(line_p)+2*sizeof(byte)+sizeof(obj_p),
2*sizeof(line_p)+2*sizeof(byte)+sizeof(proc_p),
2*sizeof(line_p)+2*sizeof(byte)+sizeof(arg_p),
};
int asizetab[] = {
sizeof(arg_p)+sizeof(short)+sizeof(offset),
sizeof(arg_p)+sizeof(short)+sizeof(lab_id),
sizeof(arg_p)+sizeof(short)+sizeof(obj_p),
sizeof(arg_p)+sizeof(short)+sizeof(proc_p),
sizeof(arg_p)+sizeof(short)+sizeof(argb_t),
sizeof(arg_p)+sizeof(short)+sizeof(short)+sizeof(argb_t),
sizeof(arg_p)+sizeof(short)+sizeof(short)+sizeof(argb_t),
sizeof(arg_p)+sizeof(short)+sizeof(short)+sizeof(argb_t)
};
#else
int lsizetab[] = {
sizeof(struct line),
sizeof(struct line),
sizeof(struct line),
sizeof(struct line),
sizeof(struct line),
sizeof(struct line),
sizeof(struct line)
};
int asizetab[] = {
sizeof (struct arg),
sizeof (struct arg),
sizeof (struct arg),
sizeof (struct arg),
sizeof (struct arg),
sizeof (struct arg),
sizeof (struct arg),
sizeof (struct arg)
};
#endif
/*
* alloc routines:
* Two parts:
* 1) typed alloc and free routines
* 2) untyped raw core allocation
*/
/*
* PART 1
*/
line_p newline(byte optyp) {
line_p lnp;
int kind=optyp;
1984-11-26 15:04:22 +00:00
lnp = (line_p) newcore(lsizetab[kind]);
TYPE(lnp) = optyp;
return(lnp);
}
void oldline(line_p lnp) {
int kind=TYPE(lnp)&BMASK;
1984-11-26 15:04:22 +00:00
if (kind == OPLIST)
oldargs(ARG(lnp));
oldcore(lnp, lsizetab[kind]);
1984-11-26 15:04:22 +00:00
}
arg_p newarg(byte kind) {
arg_p ap;
1984-11-26 15:04:22 +00:00
ap = (arg_p) newcore(asizetab[kind]);
ap->a_type = kind;
return(ap);
}
void oldargs(arg_p ap) {
arg_p next;
1984-11-26 15:04:22 +00:00
while (ap != (arg_p) 0) {
next = ap->a_next;
switch(ap->a_type) {
case ARGSTRING:
oldargb(ap->a_a.a_string.ab_next);
break;
case ARGICN:
case ARGUCN:
case ARGFCN:
oldargb(ap->a_a.a_con.ac_con.ab_next);
break;
}
oldcore(ap, asizetab[ap->a_type]);
1984-11-26 15:04:22 +00:00
ap = next;
}
}
void oldargb(argb_p abp) {
argb_p next;
1984-11-26 15:04:22 +00:00
while (abp != (argb_p) 0) {
next = abp->ab_next;
oldcore(abp, sizeof (argb_t));
1984-11-26 15:04:22 +00:00
abp = next;
}
}
void oldobjects(obj_p op) {
obj_p next;
1984-11-26 15:04:22 +00:00
while (op != (obj_p) 0) {
next = op->o_next;
oldcore(op, sizeof(struct obj));
1984-11-26 15:04:22 +00:00
op = next;
}
}
void olddblock(dblock_p dbl) {
1984-11-26 15:04:22 +00:00
oldobjects(dbl->d_objlist);
oldargs(dbl->d_values);
oldcore(dbl, sizeof(struct dblock));
1984-11-26 15:04:22 +00:00
}
void **newmap(short length) {
return(newcore((length+1) * sizeof(short *)));
1984-11-26 15:04:22 +00:00
}
/*ARGSUSED1*/
void oldmap(void **mp, short length) {
oldcore(mp, (length+1) * sizeof(short *));
1984-11-26 15:04:22 +00:00
}
cset newbitvect(short n) {
1984-11-26 15:04:22 +00:00
return((cset) newcore((n-1)*sizeof(int) + sizeof(struct bitvector)));
/* sizeof(struct bitvector) equals to the size of a struct with
* one short, followed by one ALLIGNED int. So the above statement
* also works e.g. on a VAX.
*/
}
/*ARGSUSED1*/
void oldbitvect(cset s, short n) {
oldcore(s, (n-1)*sizeof(int) + sizeof(struct bitvector));
1984-11-26 15:04:22 +00:00
}
short *newtable(short length) {
1984-11-26 15:04:22 +00:00
return((short *) newcore((length+1) * sizeof(short)));
}
/*ARGSUSED1*/
void oldtable(short *mp, short length) {
oldcore(mp, (length+1) * sizeof(short));
1984-11-26 15:04:22 +00:00
}
cond_p newcondtab(int l)
1984-11-26 15:04:22 +00:00
{
return (cond_p) newcore(l * (sizeof (struct cond_tab)));
}
void oldcondtab(cond_p tab)
1984-11-26 15:04:22 +00:00
{
int i;
for (i = 0; tab[i].mc_cond != DEFAULT; i++)
continue;
oldcore(tab, ((i+1) * sizeof (struct cond_tab)));
1984-11-26 15:04:22 +00:00
}
void *myalloc(size_t size) {
void *p;
1984-11-26 15:04:22 +00:00
p = calloc((unsigned) size, 1);
if (p == NULL)
1984-11-26 15:04:22 +00:00
error("out of memory");
return(p);
}