xref: /titanic_41/usr/src/lib/libast/common/man/stk.3 (revision ed5289f91b9bf164dccd6c75398362be77a4478d)
.fp 5 CW
STK 3
NAME
stk - data stack storage library
SYNOPSIS

\f5
#include <stk.h>

Stk_t *stkopen(int flags);
Stk_t *stkinstall(Stk_t *stack, char *(overflow)(int));
int stkclose(Stk_t *stack);
void stklink(Stk_t *stack)

char *stkalloc(Stk_t *stack, unsigned size);
char *stkcopy(Stk_t *stack, const char *string);
char *stkset(Stk_t *stack, char *address, unsigned offset);

char *stkseek(Stk_t *stack, unsigned offset);
int stktell(Stk_t *stack);
char *stkptr(Stk_t *stack, unsigned offset);
char *stkfreeze(Stk_t *stack, unsigned extra);

DESCRIPTION

\f5stk 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 \f5Stk_t defined in header \f5<stk.h>. The type \f5Stk_t is compatible with the type \f5Sfio_t defined by the \f5sfio(3) library. At any instant there is one active stack which can be referenced by the constant \f5stkstd. 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 \f5stkopen() function. A flags argument of \f5STK_SMALL indicates that unused space on the stack should be freed whenever this stack ceases to be the active stack. If successful, \f5stkopen() returns a pointer to a stack whose reference count is 1. Otherwise, \f5stkopen() returns a null pointer. The \f5stklink() function increases the reference count for the given stack. The \f5stkinstall() 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 \f5malloc(3) fails while trying to grow the stack. The overflow function will be called with the size that was passed to \f5malloc(3). The overflow function can call \f5exit(3), call \f5longjmp(3) or return. If the \f5overflow function returns, it must return a pointer to a memory region of the given size. The default action is to write an error to standard error and to call \f5exit(2) with a non-zero exit value. When stack is a null pointer, the active stack is not changed but the overflow function for the active stack can be changed and a pointer to the active stack is returned. The \f5stkclose() function decrements the reference count and frees the memory associated with the specified stack when the reference count is zero. The effect of subsequent references to objects on the stack are undefined.

The \f5stkalloc() function returns an aligned pointer to space on the active stack that can be used to hold any object of the given size. \f5stkalloc() is similar to \f5malloc(3) except that individual items returned by \f5stkalloc() can not be freed. \f5stkalloc() causes the offset of the current object to be set to zero.

The \f5stkcopy() function copies the given string onto the stack and returns a pointer to the string on the stack. \f5stkcopy() causes the offset of the current object to be set to zero.

The \f5stkset() 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 \f5sfio(3) output functions can be used to build current object incrementally. An object that is built incrementally on the stack will always occupy contiguous memory within a stack frame but until \f5stkfreeze() 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 \f5stkseek() function is used set the offset for the current object. The offset argument to \f5stkseek() specifies the new offset for the current object. The frame will be extended or moved if \f5offset causes the new current offset to extend beyond the current frame. \f5stkseek() returns a pointer to the beginning of the current object. The \f5stktell() function gives the offset of the current object.

The \f5stkptr() function converts the given \f5offset 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 \f5stkfreeze() 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.

HISTORY
The \f5stk interface was derived from similar routines in the KornShell code that is used for building parse trees and carrying out expansions. It provides an efficient mechanism for grouping dynamically allocated objects so that they can be freed all at once rather than individually.
AUTHOR
David Korn
SEE ALSO
\f5exit(2) \f5longjmp(3) \f5malloc(3) \f5sfio(3)