The package `objstack' is based on package `allocate' and implements efficient work with stacks of objects (OS). Work with the object on the stack top is analogous to one with a variable length object. One motivation for the package is the problem of growing char strings in symbol tables. Memory for OS is allocated by segments. A segment may contain more one objects. The most recently allocated segment contains object on the top of OS. If there is not sufficient free memory for the top object than new segment is created and the top object is transferred into the new segment, i.e. there is not any memory reallocation. Therefore the top object may change its address. But other objects never change address.
The package uses package `allocate'. The interface part of the package is file `objstack.h'. Defining macro `NDEBUG' (e.g. by option `-D' in C++ compiler command line) before inclusion of the interface file disables fixing some internal errors and errors of usage of the package. The implementation part is file `objstack.cpp'. The interface contains the following objects:
describes a descriptor of stack of objects. The type is simply synonym of `class os'. All work with stack of objects is executed through the descriptors. It should remember that work with the stack through several descriptors is not safe.
has value which is default size of memory segments which will be allocated for OS when the stack is created (with not given initial segment size). This is also minimal size of all segments. Original value of the macros is equal to 512. This macro can be redefined in C++ compiler command line or with the aid of directive `#undef' before including the interface file.
The class contains the following functions:
creates OS which contains the single zero length object. Minimum size of memory segments which will be allocated for OS is given as the parameter. If the parameter is absent the allocated memory segments length is equal to `OS_DEFAULT_SEGMENT_LENGTH'. But in any case the segment length is always not less than maximum alignment.
`os (size_t initial_segment_length = OS_DEFAULT_SEGMENT_LENGTH)'
is used for deleting the descritor of OS and freeing memory used by the OS.
is used for removing all objects on OS and freeing all memory allocated for OS except for the first segment.
`void empty (void)'
creates new variable length object with initial zero length on the top of OS of given descriptor. The work (analogous to one with variable length object) with object which was on the top of OS is finished, i.e. the object will never more change address.
`void top_finish (void)'
makes that length of variable length object on the top of OS of given descriptor will be equal to zero.
`void top_nullify (void)'
returns current length of variable length object on the top of OS of given descriptor.
`size_t top_length (os)'
return pointer to correspondingly the first, the last byte of variable length object on the top of OS of given descriptor, and pointer to the last byte plus one. Remember that the top object may change own place after any addition.
`void *top_begin (void)', `void *top_end (void)', `void *top_bound (void)'
removes n bytes from the end of variable length object on the top of OS of given descriptor. The top variable length object is nullified if its length is less than n.
`void top_shorten (size_t n)'
increases length of variable length object on the top of OS of given descriptor on number of bytes given as the parameter. The values of bytes added to the end of variable length object on the top of OS will be not defined.
`void top_expand (size_t length)'
adds byte given as the parameter to the end of variable length object on the top of OS of given descriptor.
`void top_add_byte (int b)'
adds memory starting with address given as the first parameter and with length given as the second parameter to the end of variable length object on the top of OS of given descriptor.
`void top_add_memory (void *str, size_t length)'
adds C++ string (with end marker 0) given as the parameter to the end of variable length string on the top of OS of the descriptor. Before the addition the macro deletes last character of the top variable length object. The last character is suggested to be C++ string end marker 0.
`void top_add_string (const char *str)'