\chapter{Vector Space}
\hypertarget{md_workspace_2post-scarcity_2docs_2_vector-space}{}\label{md_workspace_2post-scarcity_2docs_2_vector-space}\index{Vector Space@{Vector Space}}
\label{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md160}%
\Hypertarget{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md160}%
 Vector space is what in conventional computer languages is known as \textquotesingle{}the heap\textquotesingle{}. Because objects allocated in vector space are of variable size, vector space will fragment over time. Objects in vector space will become unreferenced, making them available for garbage collection and reallocation; but ultimately you will arrive at the situation where there are a number of small free spaces in vector space but you need a large one. Therefore there must ultimately be a mark-\/and-\/sweep garbage collector for vector space.

To facilitate this, reference to every vector space object will be indirected through exactly one VECP object in \href{Cons-space.html}{\texttt{ cons space}}. If a live vector space object has to be moved in memory in order to compact the heap and to allocate a new object, only one pointer need be updated. This saves enormously on mark-\/and-\/sweep time, at the expense of a small overhead on access to vector space objects.

Every vector space object must have a header, indicating that it is a vector space object and what sort of a vector space object it is. Each vector space object must have a fixed size, which is declared in its header. Beyond the header, the payload of a vector space object is undetermined.

Note that, if cons-\/pointers are implemented simply as memory addresses, the cost of moving a cons page becomes huge, so a rational garbage collector would know about cons pages and do everything possible to avoid moving them.\hypertarget{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md161}{}\doxysection{\texorpdfstring{The header}{The header}}\label{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md161}
As each vector space object has an associated VECP object in cons space, a vector space object does not need to contain either a reference count or an access control list. It does need a cons-\/pointer to its associated VECP object; it does need a tag (actually it doesn\textquotesingle{}t, since we could put all the tags in cons space, but it is convenient for memory allocation debugging that each should have a tag). It\textquotesingle{}s probably convenient for it to have a mark bit, since if garbage collection of vector space is implemented at all it needs to be mark-\/and-\/sweep.

So the header looks like this \begin{DoxyVerb}+-----+--------------+------+------+--------------+
| tag | vecp-pointer | size | mark | payload...  /
+-----+--------------+------+------+------------+
\end{DoxyVerb}
 {\bfseries{TODO\+:}} I\textquotesingle{}m not satisfied with this header design. I think it should be a multiple of 64 bits, so that it is word aligned, for efficiency of fetch. Possibly it would be better to make the {\itshape size} field 31 bits with {\itshape mark} size one bit, and instead of having the value of {\itshape size} being the size of the object in bytes, it should be the size in 64 bit words, even though that makes the maximum allocatable object only 17 gigabytes. It should also be ordered {\itshape tag, size, mark, vecp-\/pointer}, in order to word align the {\itshape vecp-\/pointer} field.\hypertarget{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md162}{}\doxysubsection{\texorpdfstring{Tag}{Tag}}\label{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md162}
The tag will be a 32 bit unsigned integer in the same way and for the same reasons that it is in \href{Cons-space.html}{\texttt{ cons space}}\+: i.\+e., because it will be alternately readable as a four character ASCII string, which will aid memory debugging.\hypertarget{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md163}{}\doxysubsection{\texorpdfstring{Vecp-\/pointer}{Vecp-pointer}}\label{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md163}
The vecp pointer is a back pointer to the VECP object in cons space which points to this vector space object. It is, therefore, obviously, the size of a \href{consspaceobject_8h.html\#structcons__pointer}{\texttt{ cons pointer}}, which is to say 64 bits.\hypertarget{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md164}{}\doxysubsection{\texorpdfstring{Size}{Size}}\label{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md164}
Obviously a single vector space object cannot occupy the whole of memory, since there are other housekeeping things we need to get the system up and running. But there really should not be a reason why a program should not allocate all the remaining available memory as a single object if that\textquotesingle{}s what it wants to do. So the size field should be the width of the address bus of the underlying machine; for the present, 64 bits. The value of the size field will be the whole size, in bytes, of the object including the header.\hypertarget{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md165}{}\doxysubsection{\texorpdfstring{Mark}{Mark}}\label{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md165}
It\textquotesingle{}s probable that in version zero we won\textquotesingle{}t implement garbage collection of vector space. C programs do not normally have any mechanism for compacting their heap; and vector space objects are much less likely than cons space objects to be transient. However, respecting the fact that in the long term we are going to want to be able to compact our vector space, I\textquotesingle{}ll provide a mark field. This really only needs to be one bit, but, again for word alignment, we\textquotesingle{}ll give it a byte.

So the header now looks like this\+: \begin{DoxyVerb}+-----+--------------+------+------+------------------------+
| 0   | 32           | 96   | 160  | 168  ...(167 + size) /
| tag | vecp-pointer | size | mark | payload...          /
+-----+--------------+------+------+--------------------+
\end{DoxyVerb}
 \hypertarget{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md166}{}\doxysubsubsection{\texorpdfstring{Alternative mark-\/bit strategy}{Alternative mark-bit strategy}}\label{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md166}
A thought which has recently occurred to me is that the mark bit could be the case bit of the least significant byte of the tag. So that if the tag read \textquotesingle{}IMAG\textquotesingle{} (a raster image), for example, when marked it would read \textquotesingle{}IMAg\textquotesingle{}. This saves having a separately allocated mark in the header, but retains debugging clarity.\hypertarget{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md167}{}\doxysection{\texorpdfstring{Tags}{Tags}}\label{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md167}
I really don\textquotesingle{}t at this point have any idea what sorts of things we\textquotesingle{}ll want to store in vector space. This is a non-\/exhaustive list of things I can think of just now.\hypertarget{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md168}{}\doxysubsection{\texorpdfstring{BMAP}{BMAP}}\label{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md168}
A bitmap; a monochrome raster; a two dimensional array of bits.\hypertarget{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md169}{}\doxysubsection{\texorpdfstring{EXEC}{EXEC}}\label{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md169}
We definitely need chunks of executable code -\/ compiled functions.\hypertarget{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md170}{}\doxysubsection{\texorpdfstring{HASH}{HASH}}\label{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md170}
We definitely need hashtables. A hashtable is implemented as a pointer to a hashing function, and an array of N cons-\/pointers each of which points to an \href{Hybrid-assoc-lists.html}{\texttt{ assoc list}} acting as a hash bucket. A hashtable is immutable. Any function which \textquotesingle{}adds a new key/value pair to\textquotesingle{} a hashtable in fact returns a new hashtable containing all the key value bindings from the old one, with the new one added. Any function which \textquotesingle{}changes a key/value pair\textquotesingle{} in a hashtable in fact returns a new value with the same bindings of all the keys except the one which has changed as the old one.

In either case, anything which held a pointer to the old version still sees the old version, which continues to exist until everything which pointed to it has been deallocated. Only things which access the hashtable via a binding in a current namespace will see the new version.\hypertarget{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md171}{}\doxysubsection{\texorpdfstring{NMSP}{NMSP}}\label{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md171}
A namespace. A namespace is a hashtable with some extra features. It has a parent pointer\+: NIL in the case of a namespace which was not created by \textquotesingle{}adding to\textquotesingle{} or \textquotesingle{}modifying\textquotesingle{} a pre-\/existing one, but where a pre-\/existing one was acted on, then that pre-\/existing one. It also must have an additional access control list, for users entitled to create new canonical versions of this namespace.

A lot of thinking needs to be done here. It\textquotesingle{}s tricky. If I get it wrong, the cost to either performance or security or both will be horrible.\hypertarget{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md172}{}\doxysubsection{\texorpdfstring{RSTR}{RSTR}}\label{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md172}
A raster; a two dimensional array of 32 bit integers, typically interpreted as RGBA colour values.\hypertarget{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md173}{}\doxysubsection{\texorpdfstring{VECT}{VECT}}\label{md_workspace_2post-scarcity_2docs_2_vector-space_autotoc_md173}
An actual vector; an array with cells of a fixed type (where, obviously, a cons pointer is one type). Has a finite number of dimensions, but probably not more than 4,294,967,296 will be supported (i.\+e. 32 bits for {\ttfamily dimensions}). 