The most direct way to allocate an object in an obstack is with
obstack_alloc, which is invoked almost like malloc.
struct obstack
object which represents the obstack. Each obstack function or macro
requires you to specify an obstack-ptr as the first argument.
This function calls the obstack's obstack_chunk_alloc function if
it needs to allocate a new chunk of memory; it returns a null pointer if
obstack_chunk_alloc returns one. In that case, it has not
changed the amount of memory allocated in the obstack. 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, then obstack_alloc will never return
a null pointer.
For example, here is a function that allocates a copy of a string str
in a specific obstack, which is in the variable string_obstack:
struct obstack string_obstack;
char *
copystring (char *string)
{
size_t len = strlen (string) + 1;
char *s = (char *) obstack_alloc (&string_obstack, len);
memcpy (s, string, len);
return s;
}
To allocate a block with specified contents, use the function
obstack_copy, declared like this:
obstack_alloc.
obstack_copy, but appends an extra byte containing a null
character. This extra byte is not counted in the argument size.
The obstack_copy0 function is convenient for copying a sequence
of characters into an obstack as a null-terminated string. Here is an
example of its use:
char *
obstack_savestring (char *addr, int size)
{
return obstack_copy0 (&myobstack, addr, size);
}
Contrast this with the previous example of savestring using
malloc (see section Basic Storage Allocation).
Go to the first, previous, next, last section, table of contents.