| STAK(3) | C LIBRARY FUNCTIONS | STAK(3) |
|---|
#include <stak.h>Stak_t *stakcreate(int flags); Stak_t *stakinstall(Stak_t *stack, char *(overflow)(int)); int stakdelete(Stak_t *stack); void staklink(Stak_t *stack)
char *stakalloc(unsigned size); char *stakcopy(const char *string); char *stakset(char *address, unsigned offset);
char *stakseek(unsigned offset); int stakputc(int c); int stakputs(const char *string); int stakwrite(const char *address, unsigned size); int staktell(void); char *stakptr(unsigned offset); char *stakfreeze(unsigned extra);
stak is a package of routines designed to provide efficient stack oriented dynamic storage. A stack abstraction consists of an ordered list of contiguous memory regions, called stack frames, that can hold objects of arbitrary size. A stack is represented by the type Stak_t defined in header <stak.h>. At any instant there is one active stack. Variable size objects can be added to the active stack and programs can reference these objects directly with pointers. In addition, the last object on the stack (referred to here as the current object) can be built incrementally. The current object has an associated offset that determines its current size. While the current object is being built incrementally, its location might change so that it is necessary to reference the object with relative offsets ranging from zero to the current offset of the object.
There is a preset initial active stack.
To use an additional stack, it is necessary to create it and to
install it as the active stack.
A stack is created with the stakcreate() function.
A flags argument of STAK_SMALL indicates that unused
space on the stack should be freed whenever this stack ceases
to be the active stack.
If successful,
stakcreate() returns a pointer to a stack whose reference
count is 1.
Otherwise, stakcreate() returns a null pointer.
The staklink() function increases the reference count for the
given stack.
The stakinstall() function
makes the specified stack the active stack and returns a pointer
to the previous active stack.
When the overflow argument is not null,
it specifies a function that will
be called whenever
The
stakalloc() function returns an aligned pointer to space on the
active stack that can be used to hold any object of the given size.
stakalloc() is similar to
The stakcopy() function copies the given string onto the stack and returns a pointer to the string on the stack. stakcopy() causes the offset of the current object to be set to zero.
The stakset() function finds the frame containing the given address, frees all frames that were created after the one containing the given address, and sets the current object to the given address. The top of the current object is set to offset bytes from current object. If address is not the address of an object on the stack the result is undefined.
The remaining functions are used to build the current object incrementally. An object that is built incrementally on the stack will always occupy contiguous memory within a stack frame but until stakfreeze() is called, the location in memory for the object can change. There is a current offset associated with the current object that determines where subsequent operations apply. Initially, this offset is zero, and the offset changes as a result of the operations you specify. The stakseek() function is used set the offset for the current object. The offset argument to stakseek() specifies the new offset for the current object. The frame will be extended or moved if offset causes the new current offset to extend beyond the current frame. stakseek() returns a pointer to the beginning of the current object. The staktell() function gives the offset of the current object.
The stakputc() function adds a given character to the current object on the stack. The current offset is advanced by 1. The stakputs() function appends the given string onto the current object in the stack and returns the length of the string. The current offset is advanced by the length of the string. The stakwrite() function appends the given size byte memory region starting at address onto the current object in the stack and advances the current offset by size. The current offset is returned.
The stakptr() function converts the given offset for the current object into a memory address on the stack. This address is only valid until another stack operation is given. The result is not defined if offset exceeds the size of the current object. The stakfreeze() function terminates the current object on the stack and returns a pointer to the beginning of this object. If extra is non-zero, extra bytes are added to the stack before the current object is terminated. The first added byte will contain zero and the contents of the remaining bytes are undefined.
David Korn
| January 30, 2010 |