Implementation of the LARD Strings Package

A string consists of two bits: the string itself, which is a null-terminated sequence of bytes, and a control record. The control record contains a reference count and a pointer to the data bytes.

A pointer to the control record (i.e. a single word) is passed around by the LARD procedure call mechanisms.

Variables

In a string variable the conventional variable which is allocated by the standard LARD mechanism is a pointer to a control record. At the start and end of the scope of the variable constructor and destructor functions must be called to make the variable point to something meaningful. At present this is the responsibility of the user.

The constructor function must:

The destructor function must:

String literals

A string literal is stored in the code's program area and should never be freed. So its control record should contain a reference count of infinity so that it is not freed however often it is decremented.

Assignment

When a new value is assigned to a string variable

Val parameters

Val parameters are read-only. At the start and end of a function that takes string val parameters constructor and destructor functions have to be called.

The constructor must

The destructor must

Var parameters

Var parameters can be modified, and changes to them are visible back in the caller.

At the start and end of a function that takes string val parameters constructor and destructor functions have to be called.

The constructor must

The destructor must

Strings as the results of functions

There are two cases of this: a string variable is returned, or a string-returning function is returned.

When a function returns a string as its result it must return it via a special ummmm....

Interaction with concurrency

I worry a lot about the interaction of the string functions with concurrency, but I have not yet found any problems.