Each source file in which you plan to use the obstack functions must include the header file `obstack.h', like this:
#include <obstack.h>
Also, if the source file uses the macro obstack_init
, it must
declare or define two functions or macros that will be called by the
obstack library. One, obstack_chunk_alloc
, is used to allocate
the chunks of memory into which objects are packed. The other,
obstack_chunk_free
, is used to return chunks when the objects in
them are freed. These macros should appear before any use of obstacks
in the source file.
Usually these are defined to use malloc
via the intermediary
xmalloc
(see section Unconstrained Allocation). This is done with
the following pair of macro definitions:
#define obstack_chunk_alloc xmalloc #define obstack_chunk_free free
Though the storage you get using obstacks really comes from malloc
,
using obstacks is faster because malloc
is called less often, for
larger blocks of memory. See section Obstack Chunks, for full details.
At run time, before the program can use a struct obstack
object
as an obstack, it must initialize the obstack by calling
obstack_init
.
obstack_chunk_alloc
function. It
returns 0 if obstack_chunk_alloc
returns a null pointer, meaning
that it is out of memory. Otherwise, it returns 1. If you supply an
obstack_chunk_alloc
function that calls exit
(see section Program Termination) or longjmp
(see section Non-Local Exits) when out of memory, you can safely ignore the value that
obstack_init
returns.
Here are two examples of how to allocate the space for an obstack and initialize it. First, an obstack that is a static variable:
static struct obstack myobstack; ... obstack_init (&myobstack);
Second, an obstack that is itself dynamically allocated:
struct obstack *myobstack_ptr = (struct obstack *) xmalloc (sizeof (struct obstack)); obstack_init (myobstack_ptr);
Go to the first, previous, next, last section, table of contents.