smbase: A Utility Library
Introduction
"smbase" stands for Scott McPeak's Base Library (sorry, naming things
is not my specialty). It's a bunch of
utility modules I use in virtually all of my projects. The entire
library is in the public domain.
Some of the links below refer to generated documentation files. If
you are reading this from your local filesystem, you may have to
say "make gendoc" (after "./configure") to get them.
Build Instructions
$ ./configure
$ make
$ make check
./configure understands
these options. You can also
look at the Makefile.
Module List
- array.h:
Several array-like template classes, including growable arrays.
- arraymap.h:
ArrayMap, a map from integers to object pointers.
- arrayqueue.h:
ArrayQueue, a template class implementing a queue with an array.
- astlist.h:
ASTList, a list class for use in abstract syntax trees.
- autofile.h,
autofile.cc:
AutoFILE, a simple wrapper around FILE* to open it or throw
an exception, and automatically close it.
- bflatten.h,
bflatten.cc:
Implementation of the Flatten interface (flatten.h)
for reading/writing binary files.
- bit2d.h,
bit2d.cc:
Two-dimensional array of bits.
- bitarray.h
bitarray.cc:
One-dimensional array of bits.
- boxprint.h
boxprint.cc:
BoxPrint functions as an output stream (sort of like cout)
with operations to indicate structure within the emitted text, so that
it can break lines intelligently. It's used as part of a source-code
pretty-printer.
- breaker.h
breaker.cpp:
Function for putting a breakpoint in, to get debugger control just
before an exception is thrown.
- ckheap.h:
Interface to check heap integrity. The underlying malloc implementation
must support these entry points for it to work. I've extended Doug
Lea's malloc (malloc.c) to do so.
- crc.h
crc.cpp:
32-bit cyclic redundancy check.
- cycles.h
cycles.c:
Report total number of processor cycles since the machine was turned on.
Uses the RDTSC instruction on x86.
- datablok.h,
datablok.cpp:
DataBlock, an array of characters of a given length. Useful when the
data may contain NUL ('\0') bytes.
- exc.h,
exc.cpp:
Various exception classes. The intent is derive everything from xBase,
so a program can catch this one exception type in main() and be assured
no exception will propagate out of the program (or any other unit of
granularity you want).
- flatten.h,
flatten.cc:
Generic interface for serializing in-memory data structures to files.
Similar to Java's Serializable, but independently conceived, and has
superior version control facilities.
- gprintf.h,
gprintf.c:
General printf; calls a function to emit each piece.
- growbuf.h,
growbuf.cc:
Extension of DataBlock (datablok.h) that
provides an append() function.
- hashline.h,
hashline.cc:
HashLineMap, a mechanism for keeping track of #line directives in C
source files. Provides efficient queries with respect to a set of
such directives.
- hashtbl.h,
hashtbl.cc:
HashTable, a hash table.
- macros.h:
A bunch of useful macros.
- malloc.c:
Version 2.7.0 of
Doug Lea's malloc.
I've made some modifications to help with debugging of memory errors
in client code.
- missing.h,
missing.cpp:
Implementations of a few C library functions that are not present
on all platforms.
- mypopen.h,
mypopen.c:
Open a process, yielding two pipes: one for writing, one for reading.
- mysig.h,
mysig.cc:
Some signal-related utilities.
- nonport.h,
nonport.cpp:
A library of utility functions whose implementation is system-specific.
Generally, I try to encapsulate all system depenencies as functions
defined in nonport.
- objlist.h:
ObjList, a general linked list of objects. ObjList considers itself
to "own" (be responsible for deallocating) the things on its list.
See also sobjlist.h.
- objmap.h:
Variant of PtrMap (ptrmap.h) that owns the values.
- objpool.h:
ObjPool, a custom allocator for fixed-size objects with embedded
'next' links.
- objstack.h:
ObjStack, a stack of owned objects. Built with a linked list.
- ohashtbl.h
OwnerHashTable, a hash table that owns the values.
- okhasharr.h:
OwnerKHashArray, a combination of an owner hash table and an array/stack.
- okhashtbl.h:
OwnerKHashTable, a version of okhasharr.h
with type-safe keys ("k" for keys).
- owner.h:
Owner, a pointer that deallocates its referrent in its destructor.
Similar to auto_ptr in the C++ Standard.
- point.h,
point.cc:
Point, a pair of integers.
- ptrmap.h:
Template class built on top of VoidPtrMap (vptrmap.h).
- smregexp.h,
smregexp.cc:
Regular expression matching.
- sobjlist.h:
SObjList, a general linked list of objects. SObjList does not
consider itself the owner of the list elements. The "s" in the
name stands for "serf", which I use to mean the opposite of "owner".
See also objlist.h.
- sobjset.h:
SObjSet, a non-owning set of objects implemented with a hashtable.
- sobjstack.h:
SObjStack, a stack of non-owned objects. Built with a linked list.
- srcloc.h,
srcloc.cc:
This module maintains a one-word data type called SourceLoc.
SourceLoc is a location within some file, e.g. line/col or character
offset information. SourceLoc also encodes which file it
refers to. This type is very useful for language processors (like
compilers) because it efficiently encodes location formation.
Decoding this into human-readable form is slower than incrementally
updating it, but decoding is made somewhat efficient with some
appropriate index structures.
- str.h,
str.cpp:
A string class, predating the C++ standard's version.
See also string.txt.
- strdict.h,
strdict.cc:
StringDict, a case-sensitive map from strings to void* pointers.
Built with a linked list. Obsolete; use StringHash or StringVoidDict
instead.
- strhash.h
strhash.cc:
StringHash, a case-sensitive map from strings to void* pointers.
Built with a hash table.
See also svdict.h.
- stringset.h,
stringset.cc:
StringSet, a set of strings.
- strobjdict.h:
StringObjDict, a case-sensitive map from strings to object pointers.
The dictionary owns the referred-to objects.
- strsobjdict.h:
StringSObjDict, a case-sensitive map from strings to object pointers.
The dictionary does not own the referred-to objects.
- strtokp.h,
strtokp.cpp:
StrtokParse, a class that parses a string similar to how strtok()
works, but provides a more convenient (and thread-safe) interface.
Similar to Java's StringTokenizer.
- strutil.h,
strutil.cc:
A set of generic string utilities, including replace(), translate(),
trimWhitespace(), encodeWithEscapes(), etc.
- svdict.h,
svdict.cc:
StringVoidDict, a case-sensitive map from strings to void* pointers.
Built on top of StringHash (strhash.h), it
differs in that it supports a slightly more powerful interface,
including query-then-modify-result.
- syserr.h,
syserr.cpp:
Intended to be a portable encapsulation of system-dependent error
facilities like UNIX's errno and Win32's GetLastError(). It's not
very complete right now.
- taillist.h:
Template class built on top of VoidTailList (vdtllist.h).
- thashtbl.h:
Template class built on top of HashTable (hashtbl.h).
- test.h:
A few test-harness macros.
- trace.h,
trace.cc:
Module for recording and querying a set of debug tracing flags.
It is documented in trace.html.
- trdelete.h,
trdelete.cc:
An operator delete which overwrites the deallocated memory with
0xAA before deallocating it.
- typ.h:
Some type definitions like byte and bool, plus a few
utility macros. Not clearly distinguished from macros.h
in purpose.
- unixutil.h,
unixutil.c:
Some utilities on top of unix functions: writeAll(), readString().
- vdtllist.h,
vdtllist.cc:
VoidTailList, the core of a linked list implementation which maintains
a pointer to the last node for O(1) appends.
Used by astlist.h and taillist.h.
- voidlist.h,
voidlist.cc:
The core of the linked list implementation used by
objlist.h and sobjlist.h.
- vptrmap.h,
vptrmap.cc:
Hashtable-based map from void* to void*.
Used by ptrmap.h and objmap.h.
- warn.h,
warn.cpp:
Intended to provide a general interface for user-level warnings; the
design never really worked well.
- xassert.h:
xassert is an assert()-like macro that throws an exception when it
fails, instead of calling abort().
- xobjlist.h:
This file is processed by M4
to make objlist.h and sobjlist.h.
Test drivers. Below are the modules that are purely test drivers
for other modules. They're separated out from the list above to
avoid the clutter.
Utility scripts.
- run-flex.pl:
Perl script to run flex and massage its output for portability.
- sm_config.pm:
This is a Perl module, intended to be used by configure scripts.
It is mostly a library of useful routines, but also reads and writes
some of the main script's global variables.
Module Dependencies
The scan-depends.pl script is capable
of generating a
module dependency description in the
Dot
format. Not all the modules appear; I try to show the most important
modules, and try to avoid making Dot do weird things.
Below is its output.

There's also a Postscript version.