source: doc/theses/mike_brooks_MMath/background.tex@ b699a61

stuck-waitfor-destruct
Last change on this file since b699a61 was 63cf80e, checked in by Peter A. Buhr <pabuhr@…>, 19 months ago

finish proofreading the array section of the background chapter

  • Property mode set to 100644
File size: 61.7 KB
Line 
1\chapter{Background}
2
3Since this work builds on C, it is necessary to explain the C mechanisms and their shortcomings for array, linked list, and string.
4
5
6\section{Ill-typed expressions}
7
8C reports many ill-typed expressions as warnings.
9For example, these attempts to assign @y@ to @x@ and vice-versa are obviously ill-typed.
10\lstinput{12-15}{bkgd-c-tyerr.c}
11with warnings:
12\begin{cfa}
13warning: assignment to 'float *' from incompatible pointer type 'void (*)(void)'
14warning: assignment to 'void (*)(void)' from incompatible pointer type 'float *'
15\end{cfa}
16Similarly,
17\lstinput{17-19}{bkgd-c-tyerr.c}
18with warning:
19\begin{cfa}
20warning: passing argument 1 of 'f' from incompatible pointer type
21note: expected 'void (*)(void)' but argument is of type 'float *'
22\end{cfa}
23with a segmentation fault at runtime.
24Clearly, @gcc@ understands these ill-typed case, and yet allows the program to compile, which seems inappropriate.
25Compiling with flag @-Werror@, which turns warnings into errors, is often too pervasive, because some warnings are just warnings, \eg an unused variable.
26In the following discussion, ``ill-typed'' means giving a nonzero @gcc@ exit condition with a message that discusses typing.
27Note, \CFA's type-system rejects all these ill-typed cases as type mismatch errors.
28
29% That @f@'s attempt to call @g@ fails is not due to 3.14 being a particularly unlucky choice of value to put in the variable @pi@.
30% Rather, it is because obtaining a program that includes this essential fragment, yet exhibits a behaviour other than "doomed to crash," is a matter for an obfuscated coding competition.
31
32% A "tractable syntactic method for proving the absence of certain program behaviours by classifying phrases according to the kinds of values they compute"*1 rejected the program.
33% The behaviour (whose absence is unprovable) is neither minor nor unlikely.
34% The rejection shows that the program is ill-typed.
35%
36% Yet, the rejection presents as a GCC warning.
37% *1 TAPL-pg1 definition of a type system
38
39
40\section{Reading declarations}
41
42A significant area of confusion for reading C declarations results from:
43\begin{itemize}
44\item
45C is unique in having dimension be higher priority than pointer in declarations.\footnote{
46For consistency, subscript has higher priority than dereference, yielding \lstinline{(*arp)[3]} rather than \lstinline{*arp[3]}.}
47\item
48Embedding a declared variable in a declaration, mimics the way the variable is used in executable statements.
49\end{itemize}
50\begin{cquote}
51\begin{tabular}{@{}ll@{}}
52\multicolumn{1}{@{}c}{\textbf{Array}} & \multicolumn{1}{c@{}}{\textbf{Function Pointer}} \\
53\begin{cfa}
54int @(*@ar@)[@5@]@; // definition
55 ... @(*@ar@)[@3@]@ += 1; // usage
56\end{cfa}
57&
58\begin{cfa}
59int @(*@f@())[@5@]@ { ... }; // definition
60 ... @(*@f@())[@3@]@ += 1; // usage
61\end{cfa}
62\end{tabular}
63\end{cquote}
64The parenthesis are necessary to achieve a pointer to a @T@, and the type is wrapped around the name in successive layers (like an \Index{onion}) to match usage in an expression.
65While attempting to make the two contexts consistent is a laudable goal, it has not worked out in practice, even though Dennis Richie believed otherwise:
66\begin{quote}
67In spite of its difficulties, I believe that the C's approach to declarations remains plausible, and am comfortable with it; it is a useful unifying principle.~\cite[p.~12]{Ritchie93}
68\end{quote}
69After all, reading a C array type is easy: just read it from the inside out, and know when to look left and when to look right!
70
71\CFA provides its own type, variable and routine declarations, using a simpler syntax.
72The new declarations place qualifiers to the left of the base type, while C declarations place qualifiers to the right of the base type.
73The qualifiers have the same syntax and semantics in \CFA as in C.
74Then, a \CFA declaration is read left to right, where a function return type is enclosed in brackets @[@\,@]@.
75\begin{cquote}
76\begin{tabular}{@{}l@{\hspace{3em}}ll@{}}
77\multicolumn{1}{c@{\hspace{3em}}}{\textbf{C}} & \multicolumn{1}{c}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{read left to right}} \\
78\begin{cfa}
79int @*@ x1 @[5]@;
80int @(*@x2@)[5]@;
81int @(*@f( int p )@)[5]@;
82\end{cfa}
83&
84\begin{cfa}
85@[5] *@ int x1;
86@* [5]@ int x2;
87@[ * [5] int ]@ f( int p );
88\end{cfa}
89&
90\begin{cfa}
91// array of 5 pointers to int
92// pointer to array of 5 int
93// function returning pointer to array of 5 ints
94\end{cfa}
95\\
96& &
97\LstCommentStyle{//\ \ \ and taking an int argument}
98\end{tabular}
99\end{cquote}
100As declaration size increases, it becomes corresponding difficult to read and understand the C declaration form, whereas reading and understanding a \CFA declaration has linear complexity as the declaration size increases.
101Note, writing declarations left to right is common in other programming languages, where the function return-type is often placed after the parameter declarations, \eg \CC \lstinline[language=C++]{auto f( int ) -> int}.
102Unfortunately, \CFA cannot interchange the priorities of subscript and dereference in expressions without breaking C compatibility.
103
104\VRef[Table]{bkgd:ar:usr:avp} introduces the many layers of the C and \CFA array story, where the \CFA story is discussion in \VRef[Chapter]{c:Array}.
105The \CFA-thesis column shows the new array declaration form, which is my contribution to safety and ergonomics.
106The table shows there are multiple yet equivalent forms for the array types under discussion, and subsequent discussion shows interactions with orthogonal (but easily confused) language features.
107Each row of the table shows alternate syntactic forms.
108The simplest occurrences of types distinguished in the preceding discussion are marked with $\triangleright$.
109Removing the declared variable @x@, gives the type used for variable, structure field, cast, or error messages.
110Unfortunately, parameter declarations have more syntactic forms and rules.
111
112\begin{table}
113\centering
114\caption{Syntactic Reference for Array vs Pointer. Includes interaction with \lstinline{const}ness.}
115\label{bkgd:ar:usr:avp}
116\begin{tabular}{ll|l|l|l}
117 & Description & \multicolumn{1}{c|}{C} & \multicolumn{1}{c|}{\CFA} & \multicolumn{1}{c}{\CFA-thesis} \\
118 \hline
119$\triangleright$ & value & @T x;@ & @T x;@ & \\
120 \hline
121 & immutable value & @const T x;@ & @const T x;@ & \\
122 & & @T const x;@ & @T const x;@ & \\
123 \hline \hline
124$\triangleright$ & pointer to value & @T * x;@ & @* T x;@ & \\
125 \hline
126 & immutable ptr. to val. & @T * const x;@ & @const * T x;@ & \\
127 \hline
128 & ptr. to immutable val. & @const T * x;@ & @* const T x;@ & \\
129 & & @T const * x;@ & @* T const x;@ & \\
130 \hline \hline
131$\triangleright$ & array of value & @T x[10];@ & @[10] T x@ & @array(T, 10) x@ \\
132 \hline
133 & ar.\ of immutable val. & @const T x[10];@ & @[10] const T x@ & @const array(T, 10) x@ \\
134 & & @T const x[10];@ & @[10] T const x@ & @array(T, 10) const x@ \\
135 \hline
136 & ar.\ of ptr.\ to value & @T * x[10];@ & @[10] * T x@ & @array(T *, 10) x@ \\
137 & & & & @array(* T, 10) x@ \\
138 \hline
139 & ar.\ of imm. ptr.\ to val. & @T * const x[10];@ & @[10] const * T x@ & @array(* const T, 10) x@ \\
140 & & & & @array(const * T, 10) x@ \\
141 \hline
142 & ar.\ of ptr.\ to imm. val. & @const T * x[10];@ & @[10] * const T x@ & @array(const T *, 10) x@ \\
143 & & @T const * x[10];@ & @[10] * T const x@ & @array(* const T, 10) x@ \\
144 \hline \hline
145$\triangleright$ & ptr.\ to ar.\ of value & @T (*x)[10];@ & @* [10] T x@ & @* array(T, 10) x@ \\
146 \hline
147 & imm. ptr.\ to ar.\ of val. & @T (* const x)[10];@ & @const * [10] T x@ & @const * array(T, 10) x@ \\
148 \hline
149 & ptr.\ to ar.\ of imm. val. & @const T (*x)[10];@ & @* [10] const T x@ & @* const array(T, 10) x@ \\
150 & & @T const (*x)[10];@ & @* [10] T const x@ & @* array(T, 10) const x@ \\
151 \hline
152 & ptr.\ to ar.\ of ptr.\ to val. & @T *(*x)[10];@ & @* [10] * T x@ & @* array(T *, 10) x@ \\
153 & & & & @* array(* T, 10) x@ \\
154 \hline
155\end{tabular}
156\end{table}
157
158
159\section{Array}
160\label{s:Array}
161
162At the start, the C language designers made a significant design mistake with respect to arrays.
163\begin{quote}
164In C, there is a strong relationship between pointers and arrays, strong enough that pointers and arrays really should be treated simultaneously.
165Any operation which can be achieved by array subscripting can also be done with pointers.~\cite[p.~93]{C:old}
166\end{quote}
167Accessing any storage requires pointer arithmetic, even if it is just base-displacement addressing in an instruction.
168The conjoining of pointers and arrays could also be applied to structures, where a pointer references a structure field like an array element.
169Finally, while subscripting involves pointer arithmetic (as does a field reference @x.y.z@), the computation is complex for multi-dimensional arrays and requires array descriptors to know stride lengths along dimensions.
170Many C errors result from manually performing pointer arithmetic instead of using language subscripting, letting the compiler performs any arithmetic;
171some C textbooks erroneously suggest manual pointer arithmetic is faster than subscripting.
172A sound and efficient C program does not require explicit pointer arithmetic.
173
174C semantics want a programmer to \emph{believe} an array variable is a ``pointer to its first element.''
175This desire becomes apparent by a detailed inspection of an array declaration.
176\lstinput{34-34}{bkgd-carray-arrty.c}
177The inspection begins by using @sizeof@ to provide program semantics for the intuition of an expression's type.
178\lstinput{35-36}{bkgd-carray-arrty.c}
179Now consider the @sizeof@ expressions derived from @ar@, modified by adding pointer-to and first-element (and including unnecessary parentheses to avoid any confusion about precedence).
180\lstinput{37-40}{bkgd-carray-arrty.c}
181Given that arrays are contiguous and the size of @float@ is 4, then the size of @ar@ with 10 floats being 40 bytes is common reasoning for C programmers.
182Equally, C programmers know the size of a pointer to the first array element is 8 (or 4 depending on the addressing architecture).
183% Now, set aside for a moment the claim that this first assertion is giving information about a type.
184Clearly, an array and a pointer to its first element are different.
185
186In fact, the idea that there is such a thing as a pointer to an array may be surprising and it is not the same thing as a pointer to the first element.
187\lstinput{42-45}{bkgd-carray-arrty.c}
188The first assignment generates:
189\begin{cfa}
190warning: assignment to `float (*)[10]' from incompatible pointer type `float *'
191\end{cfa}
192and the second assignment generates the opposite.
193
194The inspection now refutes any suggestion that @sizeof@ is informing about allocation rather than type information.
195Note, @sizeof@ has two forms, one operating on an expression and the other on a type.
196Using the type form yields the same results as the prior expression form.
197\lstinput{46-49}{bkgd-carray-arrty.c}
198The results are also the same when there is no allocation using a pointer-to-array type.
199\lstinput{51-57}{bkgd-carray-arrty.c}
200Hence, in all cases, @sizeof@ is informing about type information.
201
202Therefore, thinking of an array as a pointer to its first element is too simplistic an analogue and it is not backed up by the type system.
203This misguided analogue works for a single-dimension array but there is no advantage other than possibly teaching beginner programmers about basic runtime array-access.
204
205Continuing, there is a short form for declaring array variables using length information provided implicitly by an initializer.
206\lstinput{59-62}{bkgd-carray-arrty.c}
207The compiler counts the number of initializer elements and uses this value as the first dimension.
208Unfortunately, the implicit element counting does not extend to dimensions beyond the first.
209\lstinput{64-67}{bkgd-carray-arrty.c}
210
211My observation is recognizing:
212\begin{itemize}[leftmargin=*,topsep=0pt,itemsep=0pt]
213 \item There is value in using a type that knows its size.
214 \item The type pointer to the (first) element does not.
215 \item C \emph{has} a type that knows the whole picture: array, \eg @T[10]@.
216 \item This type has all the usual derived forms, which also know the whole picture.
217 A noteworthy example is pointer to array, \eg @T (*)[10]@.
218\end{itemize}
219
220
221\subsection{Arrays decay and pointers diffract}
222
223The last section established the difference among these four types:
224\lstinput{3-6}{bkgd-carray-decay.c}
225But the expression used for obtaining the pointer to the first element is pedantic.
226The root of all C programmer experience with arrays is the shortcut
227\lstinput{8-8}{bkgd-carray-decay.c}
228which reproduces @pa0@, in type and value:
229\lstinput{9-9}{bkgd-carray-decay.c}
230The validity of this initialization is unsettling, in the context of the facts established in \VRef{s:Array}.
231Notably, it initializes name @pa0x@ from expression @ar@, when they are not of the same type:
232\lstinput{10-10}{bkgd-carray-decay.c}
233
234So, C provides an implicit conversion from @float[10]@ to @float *@.
235\begin{quote}
236Except when it is the operand of the @sizeof@ operator, or the unary @&@ operator, or is a string literal used to
237initialize an array an expression that has type ``array of \emph{type}'' is converted to an expression with type
238``pointer to \emph{type}'' that points to the initial element of the array object~\cite[\S~6.3.2.1.3]{C11}
239\end{quote}
240This phenomenon is the famous \newterm{pointer decay}, which is a decay of an array-typed expression into a pointer-typed one.
241It is worthy to note that the list of exceptional cases does not feature the occurrence of @ar@ in @ar[i]@.
242Thus, subscripting happens on pointers not arrays.
243
244Subscripting proceeds first with pointer decay, if needed. Next, \cite[\S~6.5.2.1.2]{C11} explains that @ar[i]@ is treated as if it were @(*((a)+(i)))@.
245\cite[\S~6.5.6.8]{C11} explains that the addition, of a pointer with an integer type, is defined only when the pointer refers to an element that is in an array, with a meaning of ``@i@ elements away from,'' which is valid if @ar@ is big enough and @i@ is small enough.
246Finally, \cite[\S~6.5.3.2.4]{C11} explains that the @*@ operator's result is the referenced element.
247Taken together, these rules illustrate that @ar[i]@ and @i[a]@ mean the same thing!
248
249Subscripting a pointer when the target is standard-inappropriate is still practically well-defined.
250While the standard affords a C compiler freedom about the meaning of an out-of-bound access, or of subscripting a pointer that does not refer to an array element at all,
251the fact that C is famously both generally high-performance, and specifically not bound-checked, leads to an expectation that the runtime handling is uniform across legal and illegal accesses.
252Moreover, consider the common pattern of subscripting on a @malloc@ result:
253\begin{cfa}
254float * fs = malloc( 10 * sizeof(float) );
255fs[5] = 3.14;
256\end{cfa}
257The @malloc@ behaviour is specified as returning a pointer to ``space for an object whose size is'' as requested (\cite[\S~7.22.3.4.2]{C11}).
258But \emph{nothing} more is said about this pointer value, specifically that its referent might \emph{be} an array allowing subscripting.
259
260Under this assumption, a pointer being subscripted (or added to, then dereferenced) by any value (positive, zero, or negative), gives a view of the program's entire address space, centred around the @p@ address, divided into adjacent @sizeof(*p)@ chunks, each potentially (re)interpreted as @typeof(*p)@.
261I call this phenomenon \emph{array diffraction}, which is a diffraction of a single-element pointer into the assumption that its target is in the middle of an array whose size is unlimited in both directions.
262No pointer is exempt from array diffraction.
263No array shows its elements without pointer decay.
264
265A further pointer--array confusion, closely related to decay, occurs in parameter declarations.
266\cite[\S~6.7.6.3.7]{C11} explains that when an array type is written for a parameter,
267the parameter's type becomes a type that can be summarized as the array-decayed type.
268The respective handling of the following two parameter spellings shows that the array and pointer versions are identical.
269\lstinput{12-16}{bkgd-carray-decay.c}
270As the @sizeof(x)@ meaning changed, compared with when run on a similarly-spelled local variable declaration,
271@gcc@ also gives this code the warning for the first assertion:
272\begin{cfa}
273warning: 'sizeof' on array function parameter 'x' will return size of 'float *'
274\end{cfa}
275The caller of such a function is left with the reality that a pointer parameter is a pointer, no matter how it is spelled:
276\lstinput{18-21}{bkgd-carray-decay.c}
277This fragment gives the warning for the first argument, in the second call.
278\begin{cfa}
279warning: 'f' accessing 40 bytes in a region of size 4
280\end{cfa}
281
282The shortened parameter syntax @T x[]@ is a further way to spell ``pointer.''
283Note the opposite meaning of this spelling now, compared with its use in local variable declarations.
284This point of confusion is illustrated in:
285\lstinput{23-30}{bkgd-carray-decay.c}
286Note, \CC gives a warning for the initialization of @cp@.
287\begin{cfa}
288warning: ISO C++ forbids converting a string constant to 'char*'
289\end{cfa}
290and C gives a warning at the call of @edit@, if @const@ is added to the declaration of @cp@.
291\begin{cfa}
292warning: passing argument 1 of 'edit' discards 'const' qualifier from pointer target type
293\end{cfa}
294The basic two meanings, with a syntactic difference helping to distinguish, are illustrated in the declarations of @ca@ \vs @cp@, whose subsequent @edit@ calls behave differently.
295The syntax-caused confusion is in the comparison of the first and last lines, both of which use a literal to initialize an object declared with spelling @T x[]@.
296But these initialized declarations get opposite meanings, depending on whether the object is a local variable or a parameter!
297
298In summary, when a function is written with an array-typed parameter,
299\begin{itemize}[leftmargin=*,topsep=0pt]
300 \item an appearance of passing an array by value is always an incorrect understanding,
301 \item a dimension value, if any is present, is ignored,
302 \item pointer decay is forced at the call site and the callee sees the parameter having the decayed type.
303\end{itemize}
304
305Pointer decay does not affect pointer-to-array types, because these are already pointers, not arrays.
306As a result, a function with a pointer-to-array parameter sees the parameter exactly as the caller does:
307\par\noindent
308\begin{tabular}{@{\hspace*{-0.75\parindentlnth}}l@{}l@{}}
309\lstinput{32-36}{bkgd-carray-decay.c}
310&
311\lstinput{38-42}{bkgd-carray-decay.c}
312\end{tabular}
313\par\noindent
314\VRef[Table]{bkgd:ar:usr:decay-parm} gives the reference for the decay phenomenon seen in parameter declarations.
315
316\begin{table}
317\caption{Syntactic Reference for Decay during Parameter-Passing.
318Includes interaction with \lstinline{const}ness, where ``immutable'' refers to a restriction on the callee's ability.}
319\label{bkgd:ar:usr:decay-parm}
320\centering
321\begin{tabular}{llllll}
322 & Description & Type & Parameter Declaration & \CFA \\
323 \hline
324 & & & @T * x,@ & @* T x,@ \\
325$\triangleright$ & pointer to value & @T *@ & @T x[10],@ & @[10] T x,@ \\
326 & & & @T x[],@ & @[] T x,@ \\
327 \hline
328 & & & @T * const x,@ & @const * T x@ \\
329 & immutable ptr.\ to val. & @T * const@ & @T x[const 10],@ & @[const 10] T x,@ \\
330 & & & @T x[const],@ & @[const] T x,@\\
331 \hline
332 & & & @const T * x,@ & @ * const T x,@ \\
333 & & & @T const * x,@ & @ * T const x,@ \\
334 & ptr.\ to immutable val. & @const T *@ & @const T x[10],@ & @[10] const T x,@ \\
335 & & @T const *@ & @T const x[10],@ & @[10] T const x,@ \\
336 & & & @const T x[],@ & @[] const T x,@ \\
337 & & & @T const x[],@ & @[] T const x,@ \\
338 \hline \hline
339 & & & @T (*x)[10],@ & @* [10] T x,@ \\
340$\triangleright$ & ptr.\ to ar.\ of val. & @T(*)[10]@ & @T x[3][10],@ & @[3][10] T x,@ \\
341 & & & @T x[][10],@ & @[][10] T x,@ \\
342 \hline
343 & & & @T ** x,@ & @** T x,@ \\
344 & ptr.\ to ptr.\ to val. & @T **@ & @T * x[10],@ & @[10] * T x,@ \\
345 & & & @T * x[],@ & @[] * T x,@ \\
346 \hline
347 & ptr.\ to ptr.\ to imm.\ val. & @const char **@ & @const char * argv[],@ & @[] * const char argv,@ \\
348 & & & \emph{others elided} & \emph{others elided} \\
349 \hline
350\end{tabular}
351\end{table}
352
353
354\subsection{Variable Length Arrays}
355
356As of C99, the C standard supports a \newterm{variable length array} (VLA)~\cite[\S~6.7.5.2.5]{C99}, providing a dynamic-fixed array feature \see{\VRef{s:ArrayIntro}}.
357Note, the \CC standard does not support VLAs, but @g++@ provides them.
358A VLA is used when the desired number of array elements is \emph{unknown} at compile time.
359\begin{cfa}
360size_t cols;
361scanf( "%d", &cols );
362double ar[cols];
363\end{cfa}
364The array dimension is read from outside the program and used to create an array of size @cols@ on the stack.
365The VLA is implemented by the @alloca@ routine, which bumps the stack pointer.
366Unfortunately, there is significant misinformation about VLAs, \eg the stack size is limited (small), or VLAs cause stack failures or are inefficient.
367VLAs exist as far back as Algol W~\cite[\S~5.2]{AlgolW} and are a sound and efficient data type.
368For types with a dynamic-fixed stack, \eg coroutines or user-level threads, large VLAs can overflow the stack without appropriately sizing the stack, so heap allocation is used when the array size is unbounded.
369
370
371\subsection{Multidimensional Arrays}
372\label{toc:mdimpl}
373
374% TODO: introduce multidimensional array feature and approaches
375
376When working with arrays, \eg linear algebra, array dimensions are referred to as ``rows'' and ``columns'' for a matrix, adding ``planes'' for a cube.
377(There is little terminology for higher dimensional arrays.)
378For example, an acrostic poem\footnote{A type of poetry where the first, last or other letters in a line spell out a particular word or phrase in a vertical column.}
379can be treated as a grid of characters, where the rows are the text and the columns are the embedded keyword(s).
380Within a poem, there is the concept of a \newterm{slice}, \eg a row is a slice for the poem text, a column is a slice for a keyword.
381In general, the dimensioning and subscripting for multidimensional arrays has two syntactic forms: @m[r,c]@ or @m[r][c]@.
382
383Commonly, an array, matrix, or cube, is visualized (especially in mathematics) as a contiguous row, rectangle, or block.
384This conceptualization is reenforced by subscript ordering, \eg $m_{r,c}$ for a matrix and $c_{p,r,c}$ for a cube.
385Few programming languages differ from the mathematical subscript ordering.
386However, computer memory is flat, and hence, array forms are structured in memory as appropriate for the runtime system.
387The closest representation to the conceptual visualization is for an array object to be contiguous, and the language structures this memory using pointer arithmetic to access the values using various subscripts.
388This approach still has degrees of layout freedom, such as row or column major order, \ie juxtaposed rows or columns in memory, even when the subscript order remains fixed.
389For example, programming languages like MATLAB, Fortran, Julia and R store matrices in column-major order since they are commonly used for processing column-vectors in tabular data sets but retain row-major subscripting to match with mathematical notation.
390In general, storage layout is hidden by subscripting, and only appears when passing arrays among different programming languages or accessing specific hardware.
391
392\VRef[Figure]{f:FixedVariable} shows two C90 approaches for manipulating a contiguous matrix.
393Note, C90 does not support VLAs.
394The fixed-dimension approach (left) uses the type system;
395however, it requires all dimensions except the first to be specified at compile time, \eg @m[][6]@, allowing all subscripting stride calculations to be generated with constants.
396Hence, every matrix passed to @fp1@ must have exactly 6 columns but the row size can vary.
397The variable-dimension approach (right) ignores (violates) the type system, \ie argument and parameters types do not match, and subscripting is performed manually using pointer arithmetic in the macro @sub@.
398
399\begin{figure}
400\begin{tabular}{@{}l@{\hspace{40pt}}l@{}}
401\multicolumn{1}{c}{\textbf{Fixed Dimension}} & \multicolumn{1}{c}{\textbf{Variable Dimension}} \\
402\begin{cfa}
403
404void fp1( int rows, int m[][@6@] ) {
405 ... printf( "%d ", @m[r][c]@ ); ...
406}
407int fm1[4][@6@], fm2[6][@6@]; // no VLA
408// initialize matrixes
409fp1( 4, fm1 ); // implicit 6 columns
410fp1( 6, fm2 );
411\end{cfa}
412&
413\begin{cfa}
414#define sub( m, r, c ) *(m + r * sizeof( m[0] ) + c)
415void fp2( int rows, int cols, int *m ) {
416 ... printf( "%d ", @sub( m, r, c )@ ); ...
417}
418int vm1[@4@][@4@], vm2[@6@][@8@]; // no VLA
419// initialize matrixes
420fp2( 4, 4, vm1 );
421fp2( 6, 8, vm2 );
422\end{cfa}
423\end{tabular}
424\caption{C90 Fixed \vs Variable Contiguous Matrix Styles}
425\label{f:FixedVariable}
426\end{figure}
427
428Many languages allow multidimensional arrays-of-arrays, \eg in Pascal or \CC.
429\begin{cquote}
430\begin{tabular}{@{}ll@{}}
431\begin{pascal}
432var m : array[0..4, 0..4] of Integer; (* matrix *)
433type AT = array[0..4] of Integer; (* array type *)
434type MT = array[0..4] of AT; (* array of array type *)
435var aa : MT; (* array of array variable *)
436m@[1][2]@ := 1; aa@[1][2]@ := 1 (* same subscripting *)
437\end{pascal}
438&
439\begin{c++}
440int m[5][5];
441
442typedef vector< vector<int> > MT;
443MT vm( 5, vector<int>( 5 ) );
444m@[1][2]@ = 1; aa@[1][2]@ = 1;
445\end{c++}
446\end{tabular}
447\end{cquote}
448The language decides if the matrix and array-of-array are laid out the same or differently.
449For example, an array-of-array may be an array of row pointers to arrays of columns, so the rows may not be contiguous in memory nor even the same length (triangular matrix).
450Regardless, there is usually a uniform subscripting syntax masking the memory layout, even though a language could differentiated between the two forms using subscript syntax, \eg @m[1,2]@ \vs @aa[1][2]@.
451Nevertheless, controlling memory layout can make a difference in what operations are allowed and in performance (caching/NUMA effects).
452
453C also provides non-contiguous arrays-of-arrays.
454\begin{cfa}
455int m[5][5]; $\C{// contiguous}$
456int * aa[5]; $\C{// non-contiguous}$
457\end{cfa}
458both with different memory layout using the same subscripting, and both with different degrees of issues.
459The focus of this work is on the contiguous multidimensional arrays in C.
460The reason is that programmers are often forced to use the more complex array-of-array form when a contiguous array would be simpler, faster, and safer.
461Nevertheless, the C array-of-array form is still important for special circumstances.
462
463\VRef[Figure]{f:ContiguousNon-contiguous} shows a powerful extension made in C99 for manipulating contiguous \vs non-contiguous arrays.\footnote{C90 also supported non-contiguous arrays.}
464For contiguous-array (including VLA) arguments, C99 conjoins one or more of the parameters as a downstream dimension(s), \eg @cols@, implicitly using this parameter to compute the row stride of @m@.
465There is now sufficient information to support subscript checking along the columns to prevent buffer-overflow problems, but subscript checking is not provided.
466If the declaration of @fc@ is changed to:
467\begin{cfa}
468void fc( int rows, int cols, int m[@rows@][@cols@] ) ...
469\end{cfa}
470it is possible for C to perform bound checking across all subscripting.
471While this contiguous-array capability is a step forward, it is still the programmer's responsibility to manually manage the number of dimensions and their sizes, both at the function definition and call sites.
472That is, the array does not automatically carry its structure and sizes for use in computing subscripts.
473While the non-contiguous style in @faa@ looks very similar to @fc@, the compiler only understands the unknown-sized array of row pointers, and it relies on the programmer to traverse the columns in a row correctly with a correctly bounded loop index.
474Specifically, there is no requirement that the rows are the same length, like a poem with different length lines.
475
476\begin{figure}
477\begin{tabular}{@{}ll@{}}
478\multicolumn{1}{c}{\textbf{Contiguous}} & \multicolumn{1}{c}{\textbf{ Non-contiguous}} \\
479\begin{cfa}
480void fc( int rows, @int cols@, int m[ /* rows */ ][@cols@] ) {
481 for ( size_t r = 0; r < rows; r += 1 ) {
482 for ( size_t c = 0; c < cols; c += 1 )
483 ... @m[r][c]@ ...
484}
485int m@[5][5]@;
486for ( int r = 0; r < 5; r += 1 ) {
487
488 for ( int c = 0; c < 5; c += 1 )
489 m[r][c] = r + c;
490}
491fc( 5, 5, m );
492\end{cfa}
493&
494\begin{cfa}
495void faa( int rows, int cols, int * m[ @/* cols */@ ] ) {
496 for ( size_t r = 0; r < rows; r += 1 ) {
497 for ( size_t c = 0; c < cols; c += 1 )
498 ... @m[r][c]@ ...
499}
500int @* aa[5]@; // row pointers
501for ( int r = 0; r < 5; r += 1 ) {
502 @aa[r] = malloc( 5 * sizeof(int) );@ // create rows
503 for ( int c = 0; c < 5; c += 1 )
504 aa[r][c] = r + c;
505}
506faa( 5, 5, aa );
507\end{cfa}
508\end{tabular}
509\caption{C99 Contiguous \vs Non-contiguous Matrix Styles}
510\label{f:ContiguousNon-contiguous}
511\end{figure}
512
513
514\subsection{Multi-dimensional arrays decay and pointers diffract}
515
516As for single-dimension arrays, multi-dimensional arrays have similar issues.
517\lstinput{16-18}{bkgd-carray-mdim.c}
518The significant axis of deriving expressions from @ar@ is now ``itself,'' ``first element'' or ``first grand-element (meaning, first element of first element).''
519\PAB{Explain, explain, explain.}
520\lstinput{20-26}{bkgd-carray-mdim.c}
521\PAB{Explain, explain, explain.}
522\lstinput{28-36}{bkgd-carray-mdim.c}
523\PAB{Explain, explain, explain.}
524\lstinput{38-44}{bkgd-carray-mdim.c}
525
526
527\subsection{Array Parameter Declaration}
528
529C has a formal and actual declaration for functions to allow definition-before-use and separate compilation, where formal describes a type and an actual defines the type.
530\begin{cfa}
531int foo( int, float, char ); $\C{// formal, parameter names option}$
532int foo( int i, float f, char c ) { ... } $\C{// actual}$
533\end{cfa}
534For array parameters, a formal parameter array declaration can specify the first dimension with a dimension value, @[10]@ (which is ignored), an empty dimension list, @[ ]@, or a pointer, @*@:
535\begin{cquote}
536\begin{tabular}{@{}llll@{}}
537\begin{cfa}
538double sum( double [5] );
539double sum( double *[5] );
540\end{cfa}
541&
542\begin{cfa}
543double sum( double [ ] );
544double sum( double *[ ] );
545\end{cfa}
546&
547\begin{cfa}
548double sum( double * );
549double sum( double ** );
550\end{cfa}
551&
552\begin{cfa}
553// array
554// matrix
555\end{cfa}
556\end{tabular}
557\end{cquote}
558Good practice uses the middle form as it clearly indicates the parameter is subscripted.
559However, an actual declaration cannot use @[ ]@;
560it must use @*@.
561\begin{cfa}
562double sum( double v[ ] ) { $\C{// formal declaration}$
563double * cv; $\C{// actual declaration, think cv[ ]}$
564sum( cv ); $\C{// address assignment v = cv}$
565\end{cfa}
566
567Given the formal dimension forms @[ ]@ or @[5]@, it raises the question of qualifying the implicit array pointer rather than the array element type.
568For example, the qualifiers after the @*@ apply to the array pointer.
569\begin{cfa}
570void foo( const volatile int * @const volatile@ );
571void foo( const volatile int [ ] @const volatile@ ); // does not parse
572\end{cfa}
573C addressed this shortcoming by moving the pointer qualifiers into the first dimension.
574\begin{cquote}
575@[@ \textit{type-qualifier-list}$_{opt}$ \textit{assignment-expression}$_{opt}$ @]@
576\end{cquote}
577\begin{cfa}
578void foo( int [@const volatile@] );
579void foo( int [@const volatile@ 5] ); $\C{// 5 is ignored}$
580\end{cfa}
581
582To make the first formal dimension size meaningful, C adds this form.
583\begin{cquote}
584@[@ @static@ \textit{type-qualifier-list}$_{opt}$ \textit{assignment-expression} @]@
585\end{cquote}
586\begin{cfa}
587void foo( int [static @3@] );
588int ar[@10@];
589foo( ar ); // check argument dimension 10 > 3
590\end{cfa}
591Here, the @static@ storage qualifier defines the minimum array size for its argument.
592@gcc@ ignores this dimension qualifier, \ie it gives no warning if the argument array size is less than the parameter minimum.
593
594Finally, to handle VLAs, C repurposed the @*@ \emph{within} the dimension in the formal declaration context to mean the argument must be a VLA (contiguous).
595\begin{cquote}
596@[@ \textit{type-qualifier-list$_{opt}$} @* ]@
597\end{cquote}
598\begin{cfa}
599void foo( int [@*@][@*@] ); $\C{// formal}$
600void foo( int ar[10][10] ) { ... } $\C{// actual}$
601int ar[2][10]; $\C{// contiguous}$
602foo( ar ); $\C{// valid}$
603int * arp[10]; $\C{// non-contiguous}$
604foo( arp ); $\C{// invalid}$
605\end{cfa}
606This syntactic form for the formal prototype means the header file does not have to commit to specific dimension values, but the compiler knows the argument is a contiguous array.
607
608
609\subsection{Arrays could be values}
610
611All arrays have a know runtime size at their point of declaration.
612Furthermore, C provides an explicit mechanism to pass an array's dimensions to a function.
613Nevertheless, an array cannot be copied, and hence, not passed by value to a function, even when there is sufficient information to do so.
614
615However, if an array is a structure field (compile-time size), it can be copied and passed by value.
616For example, a C @jmp_buf@ is an array.
617\begin{cfa}
618typedef long int jmp_buf[8];
619\end{cfa}
620A instance of this array can be declared as a structure field.
621\begin{cfa}
622struct Jmp_Buf {
623 @jmp_buf@ jb;
624};
625\end{cfa}
626Now the array can be copied (and passed by value) because structures can be copied.
627\begin{cfa}
628Jmp_Buf jb1, jb2;
629jb1 = jb2;
630void foo( Jmp_Buf );
631foo( jb2 );
632\end{cfa}
633
634This same argument applies to returning arrays from functions.
635There can be sufficient information to return an array by value but it is not supported.
636Again, array wrapping allows an array to be returned from a function and copied into variable.
637
638
639\section{Linked List}
640
641Linked-lists are blocks of storage connected using one or more pointers.
642The storage block is logically divided into data (user payload) and links (list pointers), where the links are the only component used by the list structure.
643Since the data is opaque, list structures are often polymorphic over the data, which is often homogeneous.
644
645Storage linking is used to build data structures, which are a group of nodes, containing data and links, organized in a particular format, with specific operations peculiar to that format, \eg queue, tree, hash table, \etc.
646Because a node's existence is independent of the data structure that organizes it, all nodes are manipulated by address not value;
647hence, all data structure routines take and return pointers to nodes and not the nodes themselves.
648
649
650\subsection{Design issues}
651\label{toc:lst:issue}
652
653This section introduces the design space for linked lists that target \emph{system programmers}.
654Within this restricted target, all design-issue discussions assume the following invariants.
655Alternatives to the assumptions are discussed under Future Work (Section~\ref{toc:lst:futwork}).
656\begin{itemize}
657 \item A doubly-linked list is being designed.
658 Generally, the discussed issues apply similarly for singly-linked lists.
659 Circular \vs ordered linking is discussed under List identity (Section~\ref{toc:lst:issue:ident}).
660 \item Link fields are system-managed.
661 The user works with the system-provided API to query and modify list membership.
662 The system has freedom over how to represent these links.
663 \item The user data must provide storage for the list link-fields.
664 Hence, a list node is \emph{statically} defined as data and links \vs a node that is \emph{dynamically} constructed from data and links \see{\VRef{toc:lst:issue:attach}}.
665\end{itemize}
666
667
668\subsection{Preexisting linked-list libraries}
669\label{s:PreexistingLinked-ListLibraries}
670
671Two preexisting linked-list libraries are used throughout, to show examples of the concepts being defined,
672and further libraries are introduced as needed.
673\begin{enumerate}
674 \item Linux Queue library\cite{lst:linuxq} (LQ) of @<sys/queue.h>@.
675 \item \CC Standard Template Library's (STL)\footnote{The term STL is contentious as some people prefer the term standard library.} @std::list@\cite{lst:stl}
676\end{enumerate}
677%A general comparison of libraries' abilities is given under Related Work (Section~\ref{toc:lst:relwork}).
678For the discussion, assume the fictional type @req@ (request) is the user's payload in examples.
679As well, the list library is helping the user manage (organize) requests, \eg a request can be work on the level of handling a network arrival event or scheduling a thread.
680
681
682\subsection{Link attachment: intrusive vs.\ wrapped}
683\label{toc:lst:issue:attach}
684
685Link attachment deals with the question:
686Where are the libraries' inter-element link fields stored, in relation to the user's payload data fields?
687\VRef[Figure]{fig:lst-issues-attach} shows three basic styles.
688\VRef[Figure]{f:Intrusive} shows the \newterm{intrusive} style, placing the link fields inside the payload structure.
689\VRef[Figures]{f:WrappedRef} and \subref*{f:WrappedValue} show the two \newterm{wrapped} styles, which place the payload inside a generic library-provided structure that then defines the link fields.
690The wrapped style distinguishes between wrapping a reference and wrapping a value, \eg @list<req *>@ or @list<req>@.
691(For this discussion, @list<req &>@ is similar to @list<req *>@.)
692This difference is one of user style, not framework capability.
693Library LQ is intrusive; STL is wrapped with reference and value.
694
695\begin{comment}
696\begin{figure}
697 \begin{tabularx}{\textwidth}{Y|Y|Y}
698 \lstinput[language=C]{20-39}{lst-issues-intrusive.run.c}
699 &\lstinputlisting[language=C++]{20-39}{lst-issues-wrapped-byref.run.cpp}
700 &\lstinputlisting[language=C++]{20-39}{lst-issues-wrapped-emplaced.run.cpp}
701 \\ & &
702 \\
703 \includegraphics[page=1]{lst-issues-attach.pdf}
704 &
705 \includegraphics[page=2]{lst-issues-attach.pdf}
706 &
707 \includegraphics[page=3]{lst-issues-attach.pdf}
708 \\ & &
709 \\
710 (a) & (b) & (c)
711 \end{tabularx}
712\caption{
713 Three styles of link attachment: (a)~intrusive, (b)~wrapped reference, and (c)~wrapped value.
714 The diagrams show the memory layouts that result after the code runs, eliding the head object \lstinline{reqs};
715 head objects are discussed in Section~\ref{toc:lst:issue:ident}.
716 In (a), the field \lstinline{req.x} names a list direction;
717 these are discussed in Section~\ref{toc:lst:issue:simultaneity}.
718 In (b) and (c), the type \lstinline{node} represents a system-internal type,
719 which is \lstinline{std::_List_node} in the GNU implementation.
720 (TODO: cite? found in /usr/include/c++/7/bits/stl\_list.h )
721 }
722 \label{fig:lst-issues-attach}
723\end{figure}
724\end{comment}
725
726\begin{figure}
727\centering
728\newsavebox{\myboxA} % used with subfigure
729\newsavebox{\myboxB}
730\newsavebox{\myboxC}
731
732\begin{lrbox}{\myboxA}
733\begin{tabular}{@{}l@{}}
734\lstinput[language=C]{20-35}{lst-issues-intrusive.run.c} \\
735\includegraphics[page=1]{lst-issues-attach.pdf}
736\end{tabular}
737\end{lrbox}
738
739\begin{lrbox}{\myboxB}
740\begin{tabular}{@{}l@{}}
741\lstinput[language=C++]{20-35}{lst-issues-wrapped-byref.run.cpp} \\
742\includegraphics[page=2]{lst-issues-attach.pdf}
743\end{tabular}
744\end{lrbox}
745
746\begin{lrbox}{\myboxC}
747\begin{tabular}{@{}l@{}}
748\lstinput[language=C++]{20-35}{lst-issues-wrapped-emplaced.run.cpp} \\
749\includegraphics[page=3]{lst-issues-attach.pdf}
750\end{tabular}
751\end{lrbox}
752
753\subfloat[Intrusive]{\label{f:Intrusive}\usebox\myboxA}
754\hspace{6pt}
755\vrule
756\hspace{6pt}
757\subfloat[Wrapped reference]{\label{f:WrappedRef}\usebox\myboxB}
758\hspace{6pt}
759\vrule
760\hspace{6pt}
761\subfloat[Wrapped value]{\label{f:WrappedValue}\usebox\myboxC}
762
763\caption{
764 Three styles of link attachment:
765 % \protect\subref*{f:Intrusive}~intrusive, \protect\subref*{f:WrappedRef}~wrapped reference, and \protect\subref*{f:WrappedValue}~wrapped value.
766 The diagrams show the memory layouts that result after the code runs, eliding the head object \lstinline{reqs};
767 head objects are discussed in Section~\ref{toc:lst:issue:ident}.
768 In \protect\subref*{f:Intrusive}, the field \lstinline{req.d} names a list direction;
769 these are discussed in Section~\ref{toc:lst:issue:simultaneity}.
770 In \protect\subref*{f:WrappedRef} and \protect\subref*{f:WrappedValue}, the type \lstinline{node} represents a
771 library-internal type, which is \lstinline{std::_List_node} in the GNU implementation
772 \see{\lstinline{/usr/include/c++/X/bits/stl_list.h}, where \lstinline{X} is the \lstinline{g++} version number}.
773 }
774 \label{fig:lst-issues-attach}
775\end{figure}
776
777Each diagrammed example is using the fewest dynamic allocations for its respective style:
778in intrusive, here is no dynamic allocation, in wrapped reference only the linked fields are dynamically allocated, and in wrapped value the copied data and linked fields are dynamically allocated.
779The advantage of intrusive is the control in memory layout and storage placement.
780Both wrapped styles have independent storage layout and imply library-induced heap allocations, with lifetime that matches the item's membership in the list.
781In all three cases, a @req@ object can enter and leave a list many times.
782However, in intrusive a @req@ can only be on one list at a time, unless there are separate link-fields for each simultaneous list.
783In wrapped reference, a @req@ can appear multiple times on the same or different lists simultaneously, but since @req@ is shared via the pointer, care must be taken if updating data also occurs simultaneously, \eg concurrency.
784In wrapped value, the @req@ is copied, which increases storage usage, but allows independent simultaneous changes;
785however, knowing which of the @req@ object is the ``true'' object becomes complex.
786\see*{\VRef{toc:lst:issue:simultaneity} for further discussion.}
787
788The implementation of @LIST_ENTRY@ uses a trick to find the links and the node containing the links.
789The macro @LIST_INSERT_HEAD(&reqs, &r2, d);@ takes the list header, a pointer to the node, and the offset of the link fields in the node.
790One of the fields generated by @LIST_ENTRY@ is a pointer to the node, which is set to the node address, \eg @r2@.
791Hence, the offset to the link fields provides an access to the entire node, \ie the node points at itself.
792For list traversal, @LIST_FOREACH(cur, &reqs_pri, by_pri)@, there is the node cursor, the list, and the offset of the link fields within the node.
793The traversal actually moves from link fields to link fields within a node and sets the node cursor from the pointer within the link fields back to the node.
794
795A further aspect of layout control is allowing the user to explicitly specify link fields controlling attributes and placement within the @req@ object.
796LQ allows this ability through the @LIST_ENTRY@ macro;\footnote{It is possible to have multiple named linked fields allowing a node to appear on multiple lists simultaneously.}
797supplying the link fields by inheritance makes them implicit and relies on compiler placement, such as the start or end of @req@.
798An example of an explicit attribute is cache alignment of the link fields in conjunction with other @req@ fields, improving locality and/or avoiding false sharing.
799Wrapped reference has no control over the link fields, but the separate data allows some control;
800wrapped value has no control over data or links.
801
802Another subtle advantage of intrusive arrangement is that a reference to a user-level item (@req@) is sufficient to navigate or manage the item's membership.
803In LQ, the intrusive @req@ pointer is the right argument type for operations @LIST_NEXT@ or @LIST_REMOVE@;
804there is no distinguishing a @req@ from ``a @req@ in a list.''
805The same is not true of STL, wrapped reference or value.
806There, the analogous operations, @iterator::operator++()@, @iterator::operator*()@, and @list::erase(iterator)@, work on a parameter of type @list<T>::iterator@;
807There is no mapping from @req &@ to @list<req>::iterator@. %, for linear search.
808
809The advantage of wrapped is the abstraction of a data item from its list membership(s).
810In the wrapped style, the @req@ type can come from a library that serves many independent uses,
811which generally have no need for listing.
812Then, a novel use can put a @req@ in a list, without requiring any upstream change in the @req@ library.
813In intrusive, the ability to be listed must be planned during the definition of @req@.
814
815\begin{figure}
816 \lstinput[language=C++]{100-117}{lst-issues-attach-reduction.hpp}
817 \lstinput[language=C++]{150-150}{lst-issues-attach-reduction.hpp}
818 \caption{
819 Simulation of wrapped using intrusive.
820 Illustrated by pseudocode implementation of an STL-compatible API fragment using LQ as the underlying implementation.
821 The gap that makes it pseudocode is that
822 the LQ C macros do not expand to valid C++ when instantiated with template parameters---there is no \lstinline{struct El}.
823 When using a custom-patched version of LQ to work around this issue,
824 the programs of Figure~\ref{f:WrappedRef} and wrapped value work with this shim in place of real STL.
825 Their executions lead to the same memory layouts.
826 }
827 \label{fig:lst-issues-attach-reduction}
828\end{figure}
829
830It is possible to simulate wrapped using intrusive, illustrated in Figure~\ref{fig:lst-issues-attach-reduction}.
831This shim layer performs the implicit dynamic allocations that pure intrusion avoids.
832But there is no reduction going the other way.
833No shimming can cancel the allocations to which wrapped membership commits.
834
835Because intrusion is a lower-level listing primitive, the system design choice is not between forcing users to use intrusion or wrapping.
836The choice is whether or not to provide access to an allocation-free layer of functionality.
837An intrusive-primitive library like LQ lets users choose when to make this tradeoff.
838A wrapped-primitive library like STL forces users to incur the costs of wrapping, whether or not they access its benefits.
839
840
841\subsection{Simultaneity: single vs.\ multi-static vs.\ dynamic}
842\label{toc:lst:issue:simultaneity}
843
844\begin{figure}
845 \parbox[t]{3.5in} {
846 \lstinput[language=C++]{20-60}{lst-issues-multi-static.run.c}
847 }\parbox[t]{20in} {
848 ~\\
849 \includegraphics[page=1]{lst-issues-direct.pdf} \\
850 ~\\
851 \hspace*{1.5in}\includegraphics[page=2]{lst-issues-direct.pdf}
852 }
853 \caption{
854 Example of simultaneity using LQ lists.
855 The zoomed-out diagram (right/top) shows the complete multi-linked data structure.
856 This structure can navigate all requests in priority order ({\color{blue}blue}), and navigate among requests with a common request value ({\color{orange}orange}).
857 The zoomed-in diagram (right/bottom) shows how the link fields connect the nodes on different lists.
858 }
859 \label{fig:lst-issues-multi-static}
860\end{figure}
861
862\newterm{Simultaneity} deals with the question:
863In how many different lists can a node be stored, at the same time?
864Figure~\ref{fig:lst-issues-multi-static} shows an example that can traverse all requests in priority order (field @pri@) or navigate among requests with the same request value (field @rqr@).
865Each of ``by priority'' and ``by common request value'' is a separate list.
866For example, there is a single priority-list linked in order [1, 2, 2, 3, 3, 4], where nodes may have the same priority, and there are three common request-value lists combining requests with the same values: [42, 42], [17, 17, 17], and [99], giving four head nodes one for each list.
867The example shows a list can encompass all the nodes (by-priority) or only a subset of the nodes (three request-value lists).
868
869As stated, the limitation of intrusive is knowing apriori how many groups of links are needed for the maximum number of simultaneous lists.
870Thus, the intrusive LQ example supports multiple, but statically many, link lists.
871Note, it is possible to reuse links for different purposes, \eg if a list in linked one way at one time and another way at another time, and these times do not overlap, the two different linkings can use the same link fields.
872This feature is used in the \CFA runtime where a thread node may be on a blocked or running list, both never on both simultaneously.
873
874Now consider the STL in the wrapped-reference arrangement of Figure~\ref{f:WrappedRef}.
875Here it is possible to construct the same simultaneity by creating multiple STL lists, each pointing at the appropriate nodes.
876Each group of intrusive links become the links for each separate STL list.
877The upside is the unlimited number of a lists a node can be associated with simultaneously, any number of STL lists can be created dynamically.
878The downside is the dynamic allocation of the link nodes and managing multiple lists.
879Note, it might be possible to wrap the multiple lists in another type to hide this implementation issue.
880
881Now consider the STL in the wrapped-value arrangement of Figure~\ref{f:WrappedValue}.
882Again, it is possible to construct the same simultaneity by creating multiple STL lists, each copying the appropriate nodes, where the intrusive links become the links for each separate STL list.
883The upside is the same as for wrapped-reference arrangement with an unlimited number of a list bindings.
884The downside is the dynamic allocation and significant storage usage due to node copying.
885As well, it is unclear how node updates work in this scenario, without some notation of ultimately merging node information.
886
887% https://www.geeksforgeeks.org/introduction-to-multi-linked-list/ -- example of building a bespoke multi-linked list out of STL primitives (providing indication that STL doesn't offer one); offers dynamic directionality by embedding `vector<struct node*> pointers;`
888
889% When allowing multiple static directions, frameworks differ in their ergonomics for
890% the typical case: when the user needs only one direction, vs.\ the atypical case, when the user needs several.
891% LQ's ergonomics are well-suited to the uncommon case of multiple list directions.
892% Its intrusion declaration and insertion operation both use a mandatory explicit parameter naming the direction.
893% This decision works well in Figure~\ref{fig:lst-issues-multi-static}, where the names @by_pri@ and @by_rqr@ work well,
894% but it clutters Figure~\ref{f:Intrusive}, where a contrived name must be invented and used.
895% The example uses @x@; @reqs@ would be a more readily ignored choice. \PAB{wording?}
896
897\uCpp is a concurrent extension of \CC, which provides a basic set of intrusive lists~\cite[appx.~F]{uC++}, where the link fields are defined with the data fields using inheritance.
898\begin{cquote}
899\setlength{\tabcolsep}{15pt}
900\begin{tabular}{@{}ll@{}}
901\multicolumn{1}{c}{singly-linked list} & \multicolumn{1}{c}{doubly-linked list} \\
902\begin{c++}
903struct Node : public uColable {
904 int i; // data
905 Node( int i ) : i{ i } {}
906};
907\end{c++}
908&
909\begin{c++}
910struct Node : public uSeqable {
911 int i; // data
912 Node( int i ) : i{ i } {}
913};
914\end{c++}
915\end{tabular}
916\end{cquote}
917A node can be placed in the following data structures depending on its link fields: @uStack@ and @uQueue@ (singly linked), and @uSequence@ (doubly linked).
918A node inheriting from @uSeqable@ can appear in a singly or doubly linked structure.
919Structure operations implicitly know the link-field location through the inheritance.
920\begin{c++}
921uStack<Node> stack;
922Node node;
923stack.push( node ); // link fields at beginning of node
924\end{c++}
925
926Simultaneity cannot be done with multiple inheritance, because there is no mechanism to either know the order of inheritance fields or name each inheritance.
927Instead, a special type is require that contains the link fields and points at the node.
928\begin{cquote}
929\setlength{\tabcolsep}{10pt}
930\begin{tabular}{@{}ll@{}}
931\begin{c++}
932struct NodeDL : public uSeqable {
933 @Node & node;@ // node pointer
934 NodeDL( Node & node ) : node( node ) {}
935 Node & get() const { return node; }
936};
937\end{c++}
938&
939\begin{c++}
940struct Node : public uColable {
941 int i; // data
942 @NodeDL nodeseq;@ // embedded intrusive links
943 Node( int i ) : i{ i }, @nodeseq{ this }@ {}
944};
945\end{c++}
946\end{tabular}
947\end{cquote}
948This node can now be inserted into a doubly-linked list through the embedded intrusive links.
949\begin{c++}
950uSequence<NodeDL> sequence;
951sequence.add_front( @node.nodeseq@ ); $\C{// link fields in embedded type}$
952NodeDL nodedl = sequence.remove( @node.nodeseq@ );
953int i = nodedl.@get()@.i; $\C{// indirection to node}$
954\end{c++}
955Hence, the \uCpp approach optimizes one set of intrusive links through the \CC inheritance mechanism, and falls back onto the LQ approach of explicit declarations for additional intrusive links.
956However, \uCpp cannot apply the LQ trick for finding the links and node.
957
958The major ergonomic difference among the approaches is naming and name usage.
959The intrusive model requires naming each set of intrusive links, \eg @by_pri@ and @by_rqr@ in \VRef[Figure]{fig:lst-issues-multi-static}.
960\uCpp cheats by using inheritance for the first intrusive links, after which explicit naming of intrusive links is required.
961Furthermore, these link names must be used in all list operations, including iterating, whereas wrapped reference and value hide the list names in the implicit dynamically-allocated node-structure.
962At issue is whether an API for simultaneity can support one list (when several are not wanted) to be \emph{implicit}.
963\uCpp allows it, LQ does not, and the STL does not have this question.
964
965
966\subsection{User integration: preprocessed vs.\ type-system mediated}
967
968% example of poor error message due to LQ's preprocessed integration
969% programs/lst-issues-multi-static.run.c:46:1: error: expected identifier or '(' before 'do'
970% 46 | LIST_INSERT_HEAD(&reqs_rtr_42, &r42b, by_rqr);
971% | ^~~~~~~~~~~~~~~~
972%
973% ... not a wonderful example; it was a missing semicolon on the preceding line; but at least real
974
975
976\subsection{List identity: headed vs.\ ad-hoc}
977\label{toc:lst:issue:ident}
978
979All examples so far have used distinct user-facing types:
980an item found in a list (type @req@, of variables like @r1@), and
981a list (type @reql@ or @list<req>@, of variables like @reqs@ or @reqs_rqr_42@).
982\see{Figure~\ref{fig:lst-issues-attach} and Figure~\ref{fig:lst-issues-multi-static}}
983The latter type is a head, and these examples are of headed lists.
984
985A bespoke ``pointer to next @req@'' implementation often omits the latter type.
986The resulting identity model is ad-hoc.
987
988\begin{figure}
989 \centering
990 \includegraphics{lst-issues-ident.pdf}
991 \caption{
992 Comparison of headed and ad-hoc list identities, for various list lengths.
993 Pointers are logical, meaning that no claim is intended about which part of an object is being targeted.
994 }
995 \label{fig:lst-issues-ident}
996\end{figure}
997
998Figure~\ref{fig:lst-issues-ident} shows the identity model's impact on
999the existence of certain conceptual constructs, like zero-lengths lists and unlisted elements.
1000In headed thinking, there are length-zero lists (heads with no elements), and an element can be listed or not listed.
1001In ad-hoc thinking, there are no length-zero lists and every element belongs to a list of length at least one.
1002
1003By omitting the head, elements can enter into an adjacency relationship,
1004without requiring that someone allocate room for the head of the possibly-resulting list,
1005or being able to find a correct existing head.
1006
1007A head defines one or more element roles, among elements that share a transitive adjacency.
1008``First'' and ``last'' are element roles.
1009One moment's ``first'' need not be the next moment's.
1010
1011There is a cost to maintaining these roles.
1012A runtime component of this cost is evident in LQ's offering the choice of type generators @LIST@ vs.~@TAILQ@.
1013Its @LIST@ maintains a ``first,'' but not a ``last;'' its @TAILQ@ maintains both roles.
1014(Both types are doubly linked and an analogous choice is available for singly linked.)
1015
1016TODO: finish making this point
1017
1018See WIP in lst-issues-adhoc-*.ignore.*.
1019
1020The code-complexity component of the cost ...
1021
1022Ability to offer heads is good. Point: Does maintaining a head mean that the user has to provide more state when manipulating the list? Requiring the user to do so is bad, because the user may have lots of "list" typed variables in scope, and detecting that the user passed the wrong one requires testing all the listing edge cases.
1023
1024
1025\subsection{End treatment: cased vs.\ uniform }
1026
1027This issue is implementation-level, relevant to achieving high performance.
1028
1029A linear (non-circular), nonempty linked list has a first element and a last element, whether or not the list is headed.
1030A first element has no predecessor and a last element has no successor.
1031
1032\begin{figure}
1033 \centering
1034 \includegraphics{lst-issues-end.pdf}
1035 \caption{
1036 LQ sub-object-level representation of links and ends.
1037 Each object's memory is pictured as a vertical strip.
1038 Pointers' target locations, within these strips, are significant.
1039 Uniform treatment of the first-end is evident from an assertion like \lstinline{(**this.pred == this)} holding for all nodes \lstinline{this}, including the first one.
1040 Cased treatment of the last-end is evident from the symmetric proposition, \lstinline{(this.succ.pred == &this.succ)}, failing when \lstinline{this} is the last node.
1041 }
1042 \label{fig:lst-issues-end}
1043\end{figure}
1044
1045End treatment refers to how the list represents the lack of a predecessor/successor.
1046The following elaboration refers to the LQ representations, detailed in Figure~\ref{fig:lst-issues-end}.
1047
1048The most obvious representation, a null pointer, mandates a cased end treatment.
1049LQ uses this representation for its successor/last.
1050Consider the operation of inserting after a given element.
1051A doubly-linked list must update the given node's successor, to make its predecessor-pointer to refer to the new node.
1052This step must happen when the given node has a successor (when its successor pointer is non-null),
1053and must be skipped when it does not (when its successor pointer cannot be navigated).
1054So this operation contains a branch, to decide which case is happening.
1055All branches have pathological cases where branch prediction's success rate is low and the execution pipeline is stalling often.
1056
1057This branch is sometimes avoidable; the result is a uniform end treatment.
1058Here is one example of such an implementation; it works for a headed list.
1059LQ uses uses this representation for its predecessor/first. (All LQ offerings are headed at the front.)
1060For predecessor/first navigation, the relevant operation is inserting before a given element.
1061LQ's predecessor representation is not a pointer to a node, but a pointer to a pseudo-successor pointer.
1062When there is a predecessor node, that node contains a real-successor pointer; it is the target of the reference node's predecessor pointer.
1063When there is no predecessor node, the reference node (now known to be first node) acts as the pseudo-successor of the list head.
1064The list head contains a pointer to the first node.
1065When inserting before the first node, the list head's first-pointer is the one that must change.
1066So, the first node's ``predecessor'' pointer (to a pseudo-successor pointer) is set as the list head's first-pointer.
1067Now, inserting before a given element does the same logic in both cases:
1068follow the guaranteed-non-null predecessor pointer, and update what you find there to refer to the new node.
1069Applying this trick makes it possible to have list management routines that are completely free of control flow.
1070Considering a path length of only a few instructions (less than the processor's pipeline length),
1071such list management operations are often practically free,
1072with all the variance being due to the (inevitable) cache status of the nodes being managed.
1073
1074\section{String}
1075
1076A string is a sequence of symbols, where the form of a symbol can vary significantly: 7/8-bit characters (ASCII/Latin-1), or 2/4/8-byte (UNICODE) characters/symbols or variable length (UTF-8/16/32) characters.
1077A string can be read left-to-right, right-to-left, top-to-bottom, and have stacked elements (Arabic).
1078
1079A C character constant is an ASCII/Latin-1 character enclosed in single-quotes, \eg @'x'@, @'@\textsterling@'@.
1080A wide C character constant is the same, except prefixed by the letter @L@, @u@, or @U@, \eg @u'\u25A0'@ (black square), where the @\u@ identifies a universal character name.
1081A character can be formed from an escape sequence, which expresses a non-typable character @'\f'@ form feed, a delimiter character @'\''@ embedded single quote, or a raw character @'\xa3'@ \textsterling.
1082
1083A C character string is zero or more regular, wide, or escape characters enclosed in double-quotes @"xyz\n"@.
1084The kind of characters in the string is denoted by a prefix: UTF-8 characters are prefixed by @u8@, wide characters are prefixed by @L@, @u@, or @U@.
1085
1086For UTF-8 string literals, the array elements have type @char@ and are initialized with the characters of the multi-byte character sequences, \eg @u8"\xe1\x90\x87"@ (Canadian syllabics Y-Cree OO).
1087For wide string literals prefixed by the letter @L@, the array elements have type @wchar_t@ and are initialized with the wide characters corresponding to the multi-byte character sequence, \eg @L"abc@$\mu$@"@ and are read/printed using @wsanf@/@wprintf@.
1088The value of a wide-character is implementation-defined, usually a UTF-16 character.
1089For wide string literals prefixed by the letter @u@ or @U@, the array elements have type @char16_t@ or @char32_t@, respectively, and are initialized with wide characters corresponding to the multi-byte character sequence, \eg @u"abc@$\mu$@"@, @U"abc@$\mu$@"@.
1090The value of a @"u"@ character is an UTF-16 character;
1091the value of a @"U"@ character is an UTF-32 character.
1092The value of a string literal containing a multi-byte character or escape sequence not represented in the execution character set is implementation-defined.
1093
1094C strings are null-terminated rather than maintaining a separate string length.
1095\begin{quote}
1096Technically, a string is an array whose elements are single characters.
1097The compiler automatically places the null character @\0@ at the end of each such string, so programs can conveniently find the end.
1098This representation means that there is no real limit to how long a string can be, but programs have to scan one completely to determine its length.~\cite[p.~36]{C:old}
1099\end{quote}
1100Unfortunately, this design decision is both unsafe and inefficient.
1101It is common error in C to forget the storage in a character array for the terminator or overwrite the terminator, resulting in array overruns in string operations.
1102The need to repeatedly scan an entire string to determine its length can result in significant cost, as it is impossible to cache the length in many cases.
1103
1104C strings are fixed size because arrays are used for the implementation.
1105However, string manipulation commonly results in dynamically-sized temporary and final string values, \eg @strcpy@, @strcat@, @strcmp@, @strlen@, @strstr@, \etc.
1106As a result, storage management for C strings is a nightmare, quickly resulting in array overruns and incorrect results.
1107
1108Collectively, these design decisions make working with strings in C, awkward, time consuming, and unsafe.
1109While there are companion string routines that take the maximum lengths of strings to prevent array overruns, \eg @strncpy@, @strncat@, @strncpy@, that means the semantics of the operation can fail because strings are truncated.
1110Suffice it to say, C is not a go-to language for string applications, which is why \CC introduced the dynamically-sized @string@ type.
Note: See TracBrowser for help on using the repository browser.