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:
- Allocate two words.
- Put 1 in the first word. This is the reference count.
- Set the data pointer to NULL.
- Update the variable to point to this control record.
The destructor function must:
- Decrement the reference count.
- If the reference count is zero, free both the data (if it is
non-null) and the control record.
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
- The reference count for whatever the variable previously pointed
to is decremented. If it reaches zero, the control record and data
are freed.
- The reference count for the new value is incremented.
- The variable is set to point to the new value's control record.
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.