cslib.h

This interface defines a basic set of definitions that are shared among all interfaces in the package. These basic definitions include:
  1. Declarations for several new “primitive” types used throughout the libraries as fundamental types.
  2. A function for error handling.
  3. A flexible set of functions for memory allocation.
Types
bool This type defines the space of Boolean data using the constants false and true.
string This type is defined to be identical with char *.
proc This function type represents an arbitrary procedure that can be passed to an abstraction and then called back from the implementation.
Functions
getBlock(nbytes) Allocates a block of memory of the given size.
freeBlock(ptr) Frees the memory associated with ptr, which must have been allocated using getBlock, newBlock, or newArray.
getBlockType(ptr) Returns a string indicating the type of the block.
setBlockData(ptr, value) Sets the data field inside the block to the specified value.
getBlockData(ptr) Returns the data field inside the block.
error(msg, . . .) Generates an error string, expanding % constructions appearing in the error message string just as printf does.

Type detail



This type defines the space of Boolean data using the constants false and true.

typedef char *string;
This type is defined to be identical with char *.

typedef void proc(*proc)();
This function type represents an arbitrary procedure that can be passed to an abstraction and then called back from the implementation.

Function detail


void *getBlock(size_t nbytes);
Allocates a block of memory of the given size. If no memory is available, getBlock generates an error.

Usage:

ptr = getBlock(nbytes);

void freeBlock(void *ptr);
Frees the memory associated with ptr, which must have been allocated using getBlock, newBlock, or newArray. If the block appears to be in static memory or allocated by malloc, the call to freeBlock has no effect.

Usage:

freeBlock(ptr);

string getBlockType(void *ptr);
Returns a string indicating the type of the block. If the block is created using the newBlock macro, the string is the type argument. If the block is created using newArray, the string consists of the base type followed by the string "[]". In all other cases, the type is returned as "?". This string is constant and should not be freed.

Usage:

type = getBlockType(ptr);

void setBlockData(void *ptr, void *value);
Sets the data field inside the block to the specified value.

Usage:

setBlockData(ptr, value);

void *getBlockData(void *ptr);
Returns the data field inside the block.

Usage:

value = getBlockData(ptr);

void error(string msg, ...);
Generates an error string, expanding % constructions appearing in the error message string just as printf does. The behavior depends on whether the call is compiled in C or C++.

In C, calling error first checks to see if there is a handler for ErrorException. If so, calling error throws an ErrorException exception with the expanded error string as argument. If there is no ErrorException handler, the program prints the message to the standard error channel and exits with a status code indicating failure (as given by the constant ERROR_EXIT_STATUS).

In C++, calling error throws an exception with the expanded error string (defined as a C++ string) as its value.

Usage:

error(msg, . . .);