Use C89 size_t for sizes from sizeof() or to malloc() or realloc().
Remove obsolete (unsigned) casts. Sizes were unsigned int in
traditional C but are size_t in C89.
Silence some clang warnings. Add the second pair of round brackets in
`while ((ff = ff->ff_next))` to silence -Wparentheses. Change
`if (nc_first(...))/*nothing*/;` to `(void)nc_first(...);` to silence
-Wempty-body. The code in compute.c nc_first() had the form
`if (x) if (y) s; else t;`. The old indentation (before 10717cc
)
suggests that the "else" belongs to the 2nd "if", so add braces like
`if (x) { if (y) s; else t; }` to silence -Wdangling-else.
Shuffle extern function declarations. Add missing declaration for
LLparse(). Stop declaring RENAME(); it doesn't exist. Move some
declarations from main.c to extern.h, so the C compiler may check that
the declarations are compatible with the function definitions.
Assume that standard C89 remove() is available and doesn't need the
UNLINK() wrapper.
In lib/incl, don't need to include <stdio.h> nor <stdlib.h> to use
assert().
Remove alloc.h. If you don't clean your build, then an outdated
BUILDDIR/obj/util/LLgen/headers/alloc.h will survive but should not
cause harm, because nothing includes it. Don't need to remove alloc.h
from util/LLgen/distr.sh, because it isn't there.
Run the bootstrap to rebuild LLgen.c, Lpars.c, tokens.c.
90 lines
1.9 KiB
C
90 lines
1.9 KiB
C
/* Copyright (c) 1991 by the Vrije Universiteit, Amsterdam, the Netherlands.
|
|
* For full copyright and restrictions on use see the file COPYING in the top
|
|
* level of the LLgen tree.
|
|
*/
|
|
|
|
/*
|
|
* L L G E N
|
|
*
|
|
* An Extended LL(1) Parser Generator
|
|
*
|
|
* Author : Ceriel J.H. Jacobs
|
|
*/
|
|
|
|
/*
|
|
* alloc.c
|
|
* Interface to malloc() and realloc()
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
# include "types.h"
|
|
# include "extern.h"
|
|
|
|
# ifndef NORCSID
|
|
static string rcsid = "$Id$";
|
|
# endif
|
|
|
|
static string e_nomem = "Out of memory";
|
|
|
|
p_mem alloc(size_t size)
|
|
{
|
|
/*
|
|
Allocate "size" bytes. Panic if it fails
|
|
*/
|
|
p_mem p;
|
|
|
|
if ((p = malloc(size)) == 0) fatal(linecount,e_nomem, NULL);
|
|
return p;
|
|
}
|
|
|
|
p_mem ralloc(p_mem p,size_t size)
|
|
{
|
|
/*
|
|
Re-allocate the chunk of memory indicated by "p", to
|
|
occupy "size" bytes
|
|
*/
|
|
if ((p = realloc(p,size)) == 0) fatal(linecount,e_nomem, NULL);
|
|
return p;
|
|
}
|
|
|
|
p_mem new_mem(register p_info p)
|
|
{
|
|
/*
|
|
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;
|
|
size_t sz;
|
|
|
|
if (p->i_max >= p->i_top) { /* No more free elements */
|
|
sz = p->i_size;
|
|
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.
|
|
*/
|
|
p->i_size += p->i_incr * p->i_esize;
|
|
}
|
|
p->i_ptr = !p->i_ptr ?
|
|
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;
|
|
}
|