1 /* Copyright (c) 1991 by the Vrije Universiteit, Amsterdam, the Netherlands.
2 * For full copyright and restrictions on use see the file COPYING in the top
3 * level of the LLgen tree.
9 * An Extended LL(1) Parser Generator
11 * Author : Ceriel J.H. Jacobs
16 * Interface to malloc() and realloc()
23 static string rcsid = "$Id: alloc.c,v 2.10 1997/02/21 11:27:40 ceriel Exp $";
26 static string e_nomem = "Out of memory";
29 alloc(size) unsigned size; {
31 Allocate "size" bytes. Panic if it fails
36 if ((p = malloc(size)) == 0) fatal(linecount,e_nomem);
41 ralloc(p,size) p_mem p; unsigned size; {
43 Re-allocate the chunk of memory indicated by "p", to
48 if ((p = realloc(p,size)) == 0) fatal(linecount,e_nomem);
53 new_mem(p) register p_info p; {
55 This routine implements arrays that can grow.
56 It must be called every time a new element is added to it.
57 Also, the array has associated with it a "info_alloc" structure,
58 which contains info on the element size, total allocated size,
59 a pointer to the array, a pointer to the first free element,
60 and a pointer to the top.
61 If the base of the array is remembered elsewhere, it should
62 be updated each time this routine is called
67 if (p->i_max >= p->i_top) { /* No more free elements */
69 if (sizeof(char *) > 2) {
71 Do not worry about size. Just double it.
73 p->i_size += p->i_size;
75 p->i_size += p->i_incr * p->i_esize;
79 Worry about size, so only increment in chunks of i_incr.
81 p->i_size += p->i_incr * p->i_esize;
83 p->i_ptr = !p->i_ptr ?
85 ralloc(p->i_ptr, p->i_size);
86 p->i_max = p->i_ptr + sz;
87 p->i_top = p->i_ptr + p->i_size;
90 p->i_max += p->i_esize;