1991-11-23 10:41:43 +00:00
|
|
|
/* Copyright (c) 1991 by the Vrije Universiteit, Amsterdam, the Netherlands.
|
1995-07-31 09:17:14 +00:00
|
|
|
* For full copyright and restrictions on use see the file COPYING in the top
|
1995-07-31 09:10:42 +00:00
|
|
|
* level of the LLgen tree.
|
1984-10-08 14:14:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* L L G E N
|
|
|
|
*
|
|
|
|
* An Extended LL(1) Parser Generator
|
|
|
|
*
|
|
|
|
* Author : Ceriel J.H. Jacobs
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* alloc.c
|
|
|
|
* Interface to malloc() and realloc()
|
|
|
|
*/
|
|
|
|
|
2006-02-03 22:23:37 +00:00
|
|
|
#include <stdlib.h>
|
2019-02-18 16:39:05 +00:00
|
|
|
# include "alloc.h"
|
1984-10-08 14:14:53 +00:00
|
|
|
# include "types.h"
|
|
|
|
# include "extern.h"
|
|
|
|
|
1984-10-08 17:11:03 +00:00
|
|
|
# ifndef NORCSID
|
1994-06-24 11:31:16 +00:00
|
|
|
static string rcsid = "$Id$";
|
1984-10-08 17:11:03 +00:00
|
|
|
# endif
|
1984-10-08 14:14:53 +00:00
|
|
|
|
|
|
|
static string e_nomem = "Out of memory";
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
p_mem alloc(unsigned int size)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
/*
|
|
|
|
Allocate "size" bytes. Panic if it fails
|
|
|
|
*/
|
|
|
|
p_mem p;
|
1984-10-08 14:14:53 +00:00
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
if ((p = malloc(size)) == 0) fatal(linecount,e_nomem, NULL);
|
1984-10-08 14:14:53 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
p_mem ralloc(p_mem p,unsigned int size)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
/*
|
|
|
|
Re-allocate the chunk of memory indicated by "p", to
|
|
|
|
occupy "size" bytes
|
|
|
|
*/
|
2019-02-18 16:39:05 +00:00
|
|
|
if ((p = realloc(p,size)) == 0) fatal(linecount,e_nomem, NULL);
|
1985-11-25 15:47:51 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2019-02-18 16:39:05 +00:00
|
|
|
p_mem new_mem(register p_info p)
|
|
|
|
{
|
1985-11-25 15:47:51 +00:00
|
|
|
/*
|
|
|
|
This routine implements arrays that can grow.
|
|
|
|
It must be called every time a new element is added to it.
|
|
|
|
Also, the array has associated with it a "info_alloc" structure,
|
|
|
|
which contains info on the element size, total allocated size,
|
|
|
|
a pointer to the array, a pointer to the first free element,
|
|
|
|
and a pointer to the top.
|
|
|
|
If the base of the array is remembered elsewhere, it should
|
|
|
|
be updated each time this routine is called
|
|
|
|
*/
|
|
|
|
p_mem rp;
|
|
|
|
unsigned sz;
|
1986-11-07 16:01:45 +00:00
|
|
|
|
1985-11-25 15:47:51 +00:00
|
|
|
if (p->i_max >= p->i_top) { /* No more free elements */
|
|
|
|
sz = p->i_size;
|
1991-11-23 10:41:43 +00:00
|
|
|
if (sizeof(char *) > 2) {
|
|
|
|
/*
|
|
|
|
Do not worry about size. Just double it.
|
|
|
|
*/
|
|
|
|
p->i_size += p->i_size;
|
|
|
|
if (! p->i_size)
|
|
|
|
p->i_size += p->i_incr * p->i_esize;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
Worry about size, so only increment in chunks of i_incr.
|
|
|
|
*/
|
1987-05-12 18:23:09 +00:00
|
|
|
p->i_size += p->i_incr * p->i_esize;
|
1991-11-23 10:41:43 +00:00
|
|
|
}
|
1997-02-21 11:27:57 +00:00
|
|
|
p->i_ptr = !p->i_ptr ?
|
1985-11-25 15:47:51 +00:00
|
|
|
alloc(p->i_size) :
|
|
|
|
ralloc(p->i_ptr, p->i_size);
|
|
|
|
p->i_max = p->i_ptr + sz;
|
|
|
|
p->i_top = p->i_ptr + p->i_size;
|
|
|
|
}
|
|
|
|
rp = p->i_max;
|
|
|
|
p->i_max += p->i_esize;
|
|
|
|
return rp;
|
1984-10-08 14:14:53 +00:00
|
|
|
}
|