Changeset 58fe85a for doc/user/user.tex


Ignore:
Timestamp:
Jan 7, 2021, 3:27:00 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum, stuck-waitfor-destruct
Children:
2b4daf2, 64aeca0
Parents:
3c64c668 (diff), eef8dfb (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into park_unpark

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/user/user.tex

    r3c64c668 r58fe85a  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Sat Jul 13 18:36:18 2019
    14 %% Update Count     : 3876
     13%% Last Modified On : Mon Oct  5 08:57:29 2020
     14%% Update Count     : 3998
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    3030\usepackage{upquote}                                                                    % switch curled `'" to straight
    3131\usepackage{calc}
    32 \usepackage{xspace}
    3332\usepackage{varioref}                                                                   % extended references
    34 \usepackage{listings}                                                                   % format program code
     33\usepackage[labelformat=simple,aboveskip=0pt,farskip=0pt]{subfig}
     34\renewcommand{\thesubfigure}{\alph{subfigure})}
    3535\usepackage[flushmargin]{footmisc}                                              % support label/reference in footnote
    3636\usepackage{latexsym}                                   % \Box glyph
    3737\usepackage{mathptmx}                                   % better math font with "times"
    3838\usepackage[usenames]{color}
    39 \input{common}                                          % common CFA document macros
    40 \usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
    41 \usepackage{breakurl}
    42 
    43 \usepackage[pagewise]{lineno}
    44 \renewcommand{\linenumberfont}{\scriptsize\sffamily}
    45 \usepackage[firstpage]{draftwatermark}
    46 \SetWatermarkLightness{0.9}
    47 
    48 % Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore
    49 % removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR
    50 % AFTER HYPERREF.
    51 \renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}
    52 
    53 \setlength{\topmargin}{-0.45in}                                                 % move running title into header
    54 \setlength{\headsep}{0.25in}
    55 
    56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    57 
    58 \CFAStyle                                                                                               % use default CFA format-style
    59 \lstnewenvironment{C++}[1][]                            % use C++ style
    60 {\lstset{language=C++,moredelim=**[is][\protect\color{red}]{®}{®},#1}}
    61 {}
    62 
     39\newcommand{\CFALatin}{}
    6340% inline code ©...© (copyright symbol) emacs: C-q M-)
    6441% red highlighting ®...® (registered trademark symbol) emacs: C-q M-.
     
    6845% keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^
    6946% math escape $...$ (dollar symbol)
     47\input{common}                                          % common CFA document macros
     48\usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
     49\usepackage{breakurl}
     50
     51\renewcommand\footnoterule{\kern -3pt\rule{0.3\linewidth}{0.15pt}\kern 2pt}
     52
     53\usepackage[pagewise]{lineno}
     54\renewcommand{\linenumberfont}{\scriptsize\sffamily}
     55\usepackage[firstpage]{draftwatermark}
     56\SetWatermarkLightness{0.9}
     57
     58% Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore
     59% removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR
     60% AFTER HYPERREF.
     61\renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}
     62
     63\setlength{\topmargin}{-0.45in}                                                 % move running title into header
     64\setlength{\headsep}{0.25in}
     65
     66%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     67
     68\CFAStyle                                                                                               % use default CFA format-style
     69\lstnewenvironment{C++}[1][]                            % use C++ style
     70{\lstset{language=C++,moredelim=**[is][\protect\color{red}]{®}{®},#1}}
     71{}
     72
     73\newsavebox{\myboxA}
     74\newsavebox{\myboxB}
    7075
    7176%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     
    7984\newcommand{\G}[1]{{\Textbf[OliveGreen]{#1}}}
    8085\newcommand{\KWC}{K-W C\xspace}
    81 
    82 \newsavebox{\LstBox}
    8386
    8487%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     
    211214Even with all its problems, C continues to be popular because it allows writing software at virtually any level in a computer system without restriction.
    212215For system programming, where direct access to hardware, storage management, and real-time issues are a requirement, C is usually the only language of choice.
    213 The TIOBE index~\cite{TIOBE} for July 2018 ranks the top five most \emph{popular} programming languages as \Index*{Java} 16\%, C 14\%, \Index*[C++]{\CC{}} 7.5\%, Python 6\%, Visual Basic 4\% = 47.5\%, where the next 50 languages are less than 4\% each, with a long tail.
    214 The top 3 rankings over the past 30 years are:
     216The TIOBE index~\cite{TIOBE} for February 2020 ranks the top six most \emph{popular} programming languages as \Index*{Java} 17.4\%, C 16.8\%, Python 9.3\%, \Index*[C++]{\CC{}} 6.2\%, \Csharp 5.9\%, Visual Basic 5.9\% = 61.5\%, where the next 50 languages are less than 2\% each, with a long tail.
     217The top 4 rankings over the past 35 years are:
    215218\begin{center}
    216219\setlength{\tabcolsep}{10pt}
    217 \begin{tabular}{@{}rccccccc@{}}
    218                 & 2018  & 2013  & 2008  & 2003  & 1998  & 1993  & 1988  \\ \hline
    219 Java    & 1             & 2             & 1             & 1             & 16    & -             & -             \\
    220 \R{C}   & \R{2} & \R{1} & \R{2} & \R{2} & \R{1} & \R{1} & \R{1} \\
    221 \CC             & 3             & 4             & 3             & 3             & 2             & 2             & 5             \\
     220\begin{tabular}{@{}rcccccccc@{}}
     221                & 2020  & 2015  & 2010  & 2005  & 2000  & 1995  & 1990  & 1985  \\ \hline
     222Java    & 1             & 2             & 1             & 2             & 3             & -             & -             & -             \\
     223\R{C}   & \R{2} & \R{1} & \R{2} & \R{1} & \R{1} & \R{2} & \R{1} & \R{1} \\
     224Python  & 3             & 7             & 6             & 6             & 22    & 21    & -             & -             \\
     225\CC             & 4             & 4             & 4             & 3             & 2             & 1             & 2             & 12    \\
    222226\end{tabular}
    223227\end{center}
     
    252256
    253257The signature feature of \CFA is \emph{\Index{overload}able} \Index{parametric-polymorphic} functions~\cite{forceone:impl,Cormack90,Duggan96} with functions generalized using a ©forall© clause (giving the language its name):
    254 \begin{lstlisting}
     258\begin{cfa}
    255259®forall( otype T )® T identity( T val ) { return val; }
    256260int forty_two = identity( 42 ); §\C{// T is bound to int, forty\_two == 42}§
    257 \end{lstlisting}
     261\end{cfa}
    258262% extending the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC{}} approach of object-oriented extensions.
    259263\CFA{}\hspace{1pt}'s polymorphism was originally formalized by \Index*{Glen Ditchfield}\index{Ditchfield, Glen}~\cite{Ditchfield92}, and first implemented by \Index*{Richard Bilson}\index{Bilson, Richard}~\cite{Bilson03}.
     
    274278\begin{comment}
    275279A simple example is leveraging the existing type-unsafe (©void *©) C ©bsearch© to binary search a sorted floating array:
    276 \begin{lstlisting}
     280\begin{cfa}
    277281void * bsearch( const void * key, const void * base, size_t dim, size_t size,
    278282                                int (* compar)( const void *, const void * ));
     
    283287double key = 5.0, vals[10] = { /* 10 sorted floating values */ };
    284288double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp ); §\C{// search sorted array}§
    285 \end{lstlisting}
     289\end{cfa}
    286290which can be augmented simply with a polymorphic, type-safe, \CFA-overloaded wrappers:
    287 \begin{lstlisting}
     291\begin{cfa}
    288292forall( otype T | { int ?<?( T, T ); } ) T * bsearch( T key, const T * arr, size_t size ) {
    289293        int comp( const void * t1, const void * t2 ) { /* as above with double changed to T */ }
     
    296300double * val = bsearch( 5.0, vals, 10 ); §\C{// selection based on return type}§
    297301int posn = bsearch( 5.0, vals, 10 );
    298 \end{lstlisting}
     302\end{cfa}
    299303The nested function ©comp© provides the hidden interface from typed \CFA to untyped (©void *©) C, plus the cast of the result.
    300304Providing a hidden ©comp© function in \CC is awkward as lambdas do not use C calling-conventions and template declarations cannot appear at block scope.
     
    304308\CFA has replacement libraries condensing hundreds of existing C functions into tens of \CFA overloaded functions, all without rewriting the actual computations.
    305309For example, it is possible to write a type-safe \CFA wrapper ©malloc© based on the C ©malloc©:
    306 \begin{lstlisting}
     310\begin{cfa}
    307311forall( dtype T | sized(T) ) T * malloc( void ) { return (T *)malloc( sizeof(T) ); }
    308312int * ip = malloc(); §\C{// select type and size from left-hand side}§
    309313double * dp = malloc();
    310314struct S {...} * sp = malloc();
    311 \end{lstlisting}
     315\end{cfa}
    312316where the return type supplies the type/size of the allocation, which is impossible in most type systems.
    313317\end{comment}
     
    512516Keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism:
    513517\begin{cfa}
    514 int ®`®otype®`® = 3; §\C{// make keyword an identifier}§
    515 double ®`®forall®`® = 3.5;
     518int ®``®otype = 3; §\C{// make keyword an identifier}§
     519double ®``®forall = 3.5;
    516520\end{cfa}
    517521
     
    524528// include file uses the CFA keyword "with".
    525529#if ! defined( with ) §\C{// nesting ?}§
    526 #define with ®`®with®`® §\C{// make keyword an identifier}§
     530#define with ®``®with §\C{// make keyword an identifier}§
    527531#define __CFA_BFD_H__
    528532#endif
    529 
    530 ®#include_next <bfdlink.h> §\C{// must have internal check for multiple expansion}§
    531 ®
     533§{\color{red}\#\textbf{include\_next} <bfdlink.h>}§ §\C{// must have internal check for multiple expansion}§
    532534#if defined( with ) && defined( __CFA_BFD_H__ ) §\C{// reset only if set}§
    533535#undef with
     
    576578\section{Exponentiation Operator}
    577579
    578 C, \CC, and Java (and many other programming languages) have no exponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, \ie $x^y$, and instead use a routine, like \Indexc{pow}, to perform the exponentiation operation.
    579 \CFA extends the basic operators with the exponentiation operator ©?\?©\index{?\\?@©?\?©} and ©?\=?©\index{?\\=?@©\=?©}, as in, ©x \ y© and ©x \= y©, which means $x^y$ and $x \leftarrow x^y$.
     580C, \CC, and Java (and many other programming languages) have no exponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, \ie $x^y$, and instead use a routine, like \Indexc{pow(x,y)}, to perform the exponentiation operation.
     581\CFA extends the basic operators with the exponentiation operator ©?®\®?©\index{?\\?@©?®\®?©} and ©?\=?©\index{?\\=?@©®\®=?©}, as in, ©x ®\® y© and ©x ®\®= y©, which means $x^y$ and $x \leftarrow x^y$.
    580582The priority of the exponentiation operator is between the cast and multiplicative operators, so that ©w * (int)x \ (int)y * z© is parenthesized as ©((w * (((int)x) \ ((int)y))) * z)©.
    581583
    582 As for \Index{division}, there are exponentiation operators for integral and floating types, including the builtin \Index{complex} types.
     584There are exponentiation operators for integral and floating types, including the builtin \Index{complex} types.
    583585Integral exponentiation\index{exponentiation!unsigned integral} is performed with repeated multiplication\footnote{The multiplication computation is $O(\log y)$.} (or shifting if the exponent is 2).
    584 Overflow from large exponents or negative exponents return zero.
     586Overflow for a large exponent or negative exponent returns zero.
    585587Floating exponentiation\index{exponentiation!floating} is performed using \Index{logarithm}s\index{exponentiation!logarithm}, so the exponent cannot be negative.
    586588\begin{cfa}
     
    5895911 1 256 -64 125 ®0® 3273344365508751233 ®0® ®0® -0.015625 18.3791736799526 0.264715-1.1922i
    590592\end{cfa}
    591 Note, ©5 ®\® 32© and ©5L ®\® 64© overflow, and ©-4 ®\® -3© is a fraction but stored in an integer so all three computations generate an integral zero.
     593Note, ©5 \ 32© and ©5L \ 64© overflow, and ©-4 \ -3© is a fraction but stored in an integer so all three computations generate an integral zero.
    592594Parenthesis are necessary for complex constants or the expression is parsed as ©1.0f+®(®2.0fi \ 3.0f®)®+2.0fi©.
    593595The exponentiation operator is available for all the basic types, but for user-defined types, only the integral-computation version is available.
     
    598600OT ?®\®?( OT ep, unsigned long int y );
    599601\end{cfa}
    600 The user type ©T© must define multiplication, one, ©1©, and, ©*©.
     602The user type ©T© must define multiplication, one (©1©), and ©*©.
    601603
    602604
     
    626628
    627629
    628 \subsection{Loop Control}
    629 
    630 The ©for©/©while©/©do-while© loop-control allows empty or simplified ranges (see Figure~\ref{f:LoopControlExamples}).
    631 \begin{itemize}
    632 \item
    633 An empty conditional implies ©1©.
    634 \item
    635 The up-to range ©~©\index{~@©~©} means exclusive range [M,N).
    636 \item
    637 The up-to range ©~=©\index{~=@©~=©} means inclusive range [M,N].
    638 \item
    639 The down-to range ©-~©\index{-~@©-~©} means exclusive range [N,M).
    640 \item
    641 The down-to range ©-~=©\index{-~=@©-~=©} means inclusive range [N,M].
    642 \item
    643 ©@© means put nothing in this field.
    644 \item
    645 ©0© is the implicit start value;
    646 \item
    647 ©1© is the implicit increment value.
    648 \item
    649 The up-to range uses ©+=© for increment;
    650 \item
    651 The down-to range uses ©-=© for decrement.
    652 \item
    653 The loop index is polymorphic in the type of the start value or comparison value when start is implicitly ©0©.
    654 \end{itemize}
    655 
    656 \begin{figure}
     630%\section{\texorpdfstring{\protect\lstinline@case@ Clause}{case Clause}}
     631\subsection{\texorpdfstring{\LstKeywordStyle{case} Clause}{case Clause}}
     632
     633C restricts the ©case© clause of a ©switch© statement to a single value.
     634For multiple ©case© clauses associated with the same statement, it is necessary to have multiple ©case© clauses rather than multiple values.
     635Requiring a ©case© clause for each value does not seem to be in the spirit of brevity normally associated with C.
     636Therefore, the ©case© clause is extended with a list of values, as in:
    657637\begin{cquote}
    658 \begin{tabular}{@{}l|l@{}}
    659 \multicolumn{1}{c|}{loop control} & \multicolumn{1}{c}{output} \\
    660 \hline
    661 \begin{cfa}
    662 sout | nlOff;
    663 while ®()® { sout | "empty"; break; } sout | nl;
    664 do { sout | "empty"; break; } while ®()®; sout | nl;
    665 for ®()® { sout | "empty"; break; } sout | nl;
    666 for ( ®0® ) { sout | "A"; } sout | "zero" | nl;
    667 for ( ®1® ) { sout | "A"; } sout | nl;
    668 for ( ®10® ) { sout | "A"; } sout | nl;
    669 for ( ®1 ~= 10 ~ 2® ) { sout | "B"; } sout | nl;
    670 for ( ®10 -~= 1 ~ 2® ) { sout | "C"; } sout | nl;
    671 for ( ®0.5 ~ 5.5® ) { sout | "D"; } sout | nl;
    672 for ( ®5.5 -~ 0.5® ) { sout | "E"; } sout | nl;
    673 for ( ®i; 10® ) { sout | i; } sout | nl;
    674 for ( ®i; 1 ~= 10 ~ 2® ) { sout | i; } sout | nl;
    675 for ( ®i; 10 -~= 1 ~ 2® ) { sout | i; } sout | nl;
    676 for ( ®i; 0.5 ~ 5.5® ) { sout | i; } sout | nl;
    677 for ( ®i; 5.5 -~ 0.5® ) { sout | i; } sout | nl;
    678 for ( ®ui; 2u ~= 10u ~ 2u® ) { sout | ui; } sout | nl;
    679 for ( ®ui; 10u -~= 2u ~ 2u® ) { sout | ui; } sout | nl;
    680 enum { N = 10 };
    681 for ( ®N® ) { sout | "N"; } sout | nl;
    682 for ( ®i; N® ) { sout | i; } sout | nl;
    683 for ( ®i; N -~ 0® ) { sout | i; } sout | nl;
    684 const int start = 3, comp = 10, inc = 2;
    685 for ( ®i; start ~ comp ~ inc + 1® ) { sout | i; } sout | nl;
    686 for ( ®i; 1 ~ @® ) { if ( i > 10 ) break;
    687         sout | i; } sout | nl;
    688 for ( ®i; 10 -~ @® ) { if ( i < 0 ) break;
    689         sout | i; } sout | nl;
    690 for ( ®i; 2 ~ @ ~ 2® ) { if ( i > 10 ) break;
    691         sout | i; } sout | nl;
    692 for ( ®i; 2.1 ~ @ ~ @® ) { if ( i > 10.5 ) break;
    693         sout | i; i += 1.7; } sout | nl;
    694 for ( ®i; 10 -~ @ ~ 2® ) { if ( i < 0 ) break;
    695         sout | i; } sout | nl;
    696 for ( ®i; 12.1 ~ @ ~ @® ) { if ( i < 2.5 ) break;
    697         sout | i; i -= 1.7; } sout | nl;
    698 for ( ®i; 5 : j; -5 ~ @® ) { sout | i | j; } sout | nl;
    699 for ( ®i; 5 : j; -5 -~ @® ) { sout | i | j; } sout | nl;
    700 for ( ®i; 5 : j; -5 ~ @ ~ 2® ) { sout | i | j; } sout | nl;
    701 for ( ®i; 5 : j; -5 -~ @ ~ 2® ) { sout | i | j; } sout | nl;
    702 for ( ®j; -5 ~ @ : i; 5® ) { sout | i | j; } sout | nl;
    703 for ( ®j; -5 -~ @ : i; 5® ) { sout | i | j; } sout | nl;
    704 for ( ®j; -5 ~ @ ~ 2 : i; 5® ) { sout | i | j; } sout | nl;
    705 for ( ®j; -5 -~ @ ~ 2 : i; 5® ) { sout | i | j; } sout | nl;
    706 for ( ®j; -5 -~ @ ~ 2 : i; 5 : k; 1.5 ~ @® ) {
    707         sout | i | j | k; } sout | nl;
    708 for ( ®j; -5 -~ @ ~ 2 : k; 1.5 ~ @ : i; 5® ) {
    709         sout | i | j | k; } sout | nl;
    710 for ( ®k; 1.5 ~ @ : j; -5 -~ @ ~ 2 : i; 5® ) {
    711         sout | i | j | k; } sout | nl;
     638\begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}}
     639\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\
     640\begin{cfa}
     641switch ( i ) {
     642  case ®1, 3, 5®:
     643        ...
     644  case ®2, 4, 6®:
     645        ...
     646}
    712647\end{cfa}
    713648&
    714649\begin{cfa}
    715 
    716 empty
    717 empty
    718 empty
    719 zero
    720 A
    721 A A A A A A A A A A
    722 B B B B B
    723 C C C C C
    724 D D D D D
    725 E E E E E
    726 0 1 2 3 4 5 6 7 8 9
    727 1 3 5 7 9
    728 10 8 6 4 2
    729 0.5 1.5 2.5 3.5 4.5
    730 5.5 4.5 3.5 2.5 1.5
    731 2 4 6 8 10
    732 10 8 6 4 2
    733 
    734 N N N N N N N N N N
    735 0 1 2 3 4 5 6 7 8 9
    736 10 9 8 7 6 5 4 3 2 1
    737 
    738 3 6 9
    739 
    740 1 2 3 4 5 6 7 8 9 10
    741 
    742 10 9 8 7 6 5 4 3 2 1 0
    743 
    744 2 4 6 8 10
    745 
    746 2.1 3.8 5.5 7.2 8.9
    747 
    748 10 8 6 4 2 0
    749 
    750 12.1 10.4 8.7 7 5.3 3.6
    751 0 -5 1 -4 2 -3 3 -2 4 -1
    752 0 -5 1 -6 2 -7 3 -8 4 -9
    753 0 -5 1 -3 2 -1 3 1 4 3
    754 0 -5 1 -7 2 -9 3 -11 4 -13
    755 0 -5 1 -4 2 -3 3 -2 4 -1
    756 0 -5 1 -6 2 -7 3 -8 4 -9
    757 0 -5 1 -3 2 -1 3 1 4 3
    758 0 -5 1 -7 2 -9 3 -11 4 -13
    759 
    760 0 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5
    761 
    762 0 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5
    763 
    764 0 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5
     650switch ( i ) {
     651  case 1: case 3 : case 5:
     652        ...
     653  case 2: case 4 : case 6:
     654        ...
     655}
     656\end{cfa}
     657&
     658\begin{cfa}
     659
     660// odd values
     661
     662// even values
     663
     664
    765665\end{cfa}
    766666\end{tabular}
    767667\end{cquote}
    768 \caption{Loop Control Examples}
    769 \label{f:LoopControlExamples}
    770 \end{figure}
     668In addition, subranges are allowed to specify case values.\footnote{
     669gcc has the same mechanism but awkward syntax, \lstinline@2 ...42@, because a space is required after a number, otherwise the period is a decimal point.}
     670\begin{cfa}
     671switch ( i ) {
     672  case ®1~5:® §\C{// 1, 2, 3, 4, 5}§
     673        ...
     674  case ®10~15:® §\C{// 10, 11, 12, 13, 14, 15}§
     675        ...
     676}
     677\end{cfa}
     678Lists of subranges are also allowed.
     679\begin{cfa}
     680case ®1~5, 12~21, 35~42®:
     681\end{cfa}
    771682
    772683
     
    977888
    978889
    979 %\section{\texorpdfstring{\protect\lstinline@case@ Clause}{case Clause}}
    980 \subsection{\texorpdfstring{\LstKeywordStyle{case} Statement}{case Statement}}
    981 
    982 C restricts the ©case© clause of a ©switch© statement to a single value.
    983 For multiple ©case© clauses associated with the same statement, it is necessary to have multiple ©case© clauses rather than multiple values.
    984 Requiring a ©case© clause for each value does not seem to be in the spirit of brevity normally associated with C.
    985 Therefore, the ©case© clause is extended with a list of values, as in:
    986 \begin{cquote}
    987 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}}
    988 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\
    989 \begin{cfa}
    990 switch ( i ) {
    991   case ®1, 3, 5®:
     890\subsection{Non-terminating and Labelled \texorpdfstring{\LstKeywordStyle{fallthrough}}{Non-terminating and Labelled fallthrough}}
     891
     892The ©fallthrough© clause may be non-terminating within a ©case© clause or have a target label to common code from multiple case clauses.
     893\begin{center}
     894\begin{tabular}{@{}lll@{}}
     895\begin{cfa}
     896choose ( ... ) {
     897  case 3:
     898        if ( ... ) {
     899                ... ®fallthru;® // goto case 4
     900        } else {
     901                ...
     902        }
     903        // implicit break
     904  case 4:
     905
     906
     907
     908
     909\end{cfa}
     910&
     911\begin{cfa}
     912choose ( ... ) {
     913  case 3:
     914        ... ®fallthrough common;®
     915  case 4:
     916        ... ®fallthrough common;®
     917
     918  ®common:® // below fallthrough
     919                          // at case-clause level
     920        ...     // common code for cases 3/4
     921        // implicit break
     922  case 4:
     923
     924
     925\end{cfa}
     926&
     927\begin{cfa}
     928choose ( ... ) {
     929  case 3:
     930        choose ( ... ) {
     931          case 4:
     932                for ( ... ) {
     933                        // multi-level transfer
     934                        ... ®fallthru common;®
     935                }
     936                ...
     937        }
    992938        ...
    993   case ®2, 4, 6®:
    994         ...
    995 }
     939  ®common:® // below fallthrough
     940                          // at case-clause level
     941\end{cfa}
     942\end{tabular}
     943\end{center}
     944The target label must be below the ©fallthrough© and may not be nested in a control structure, and
     945the target label must be at the same or higher level as the containing ©case© clause and located at
     946the same level as a ©case© clause; the target label may be case ©default©, but only associated
     947with the current ©switch©/©choose© statement.
     948
     949\begin{figure}
     950\begin{tabular}{@{}l|l@{}}
     951\multicolumn{1}{c|}{loop control} & \multicolumn{1}{c}{output} \\
     952\hline
     953\begin{cfa}[xleftmargin=0pt]
     954while ®()® { sout | "empty"; break; }
     955do { sout | "empty"; break; } while ®()®;
     956for ®()® { sout | "empty"; break; }
     957for ( ®0® ) { sout | "A"; } sout | "zero";
     958for ( ®1® ) { sout | "A"; }
     959for ( ®10® ) { sout | "A"; }
     960for ( ®= 10® ) { sout | "A"; }
     961for ( ®1 ~= 10 ~ 2® ) { sout | "B"; }
     962for ( ®10 -~= 1 ~ 2® ) { sout | "C"; }
     963for ( ®0.5 ~ 5.5® ) { sout | "D"; }
     964for ( ®5.5 -~ 0.5® ) { sout | "E"; }
     965for ( ®i; 10® ) { sout | i; }
     966for ( ®i; = 10® ) { sout | i; }
     967for ( ®i; 1 ~= 10 ~ 2® ) { sout | i; }
     968for ( ®i; 10 -~= 1 ~ 2® ) { sout | i; }
     969for ( ®i; 0.5 ~ 5.5® ) { sout | i; }
     970for ( ®i; 5.5 -~ 0.5® ) { sout | i; }
     971for ( ®ui; 2u ~= 10u ~ 2u® ) { sout | ui; }
     972for ( ®ui; 10u -~= 2u ~ 2u® ) { sout | ui; }
     973enum { N = 10 };
     974for ( ®N® ) { sout | "N"; }
     975for ( ®i; N® ) { sout | i; }
     976for ( ®i; N -~ 0® ) { sout | i; }
     977const int start = 3, comp = 10, inc = 2;
     978for ( ®i; start ~ comp ~ inc + 1® ) { sout | i; }
     979for ( i; 1 ~ ®@® ) { if ( i > 10 ) break; sout | i; }
     980for ( i; 10 -~ ®@® ) { if ( i < 0 ) break; sout | i; }
     981for ( i; 2 ~ ®@® ~ 2 ) { if ( i > 10 ) break; sout | i; }
     982for ( i; 2.1 ~ ®@® ~ ®@® ) { if ( i > 10.5 ) break; sout | i; i += 1.7; }
     983for ( i; 10 -~ ®@® ~ 2 ) { if ( i < 0 ) break; sout | i; }
     984for ( i; 12.1 ~ ®@® ~ ®@® ) { if ( i < 2.5 ) break; sout | i; i -= 1.7; }
     985for ( i; 5 ®:® j; -5 ~ @ ) { sout | i | j; }
     986for ( i; 5 ®:® j; -5 -~ @ ) { sout | i | j; }
     987for ( i; 5 ®:® j; -5 ~ @ ~ 2 ) { sout | i | j; }
     988for ( i; 5 ®:® j; -5 -~ @ ~ 2 ) { sout | i | j; }
     989for ( i; 5 ®:® j; -5 ~ @ ) { sout | i | j; }
     990for ( i; 5 ®:® j; -5 -~ @ ) { sout | i | j; }
     991for ( i; 5 ®:® j; -5 ~ @ ~ 2 ) { sout | i | j; }
     992for ( i; 5 ®:® j; -5 -~ @ ~ 2 ) { sout | i | j; }
     993for ( i; 5 ®:® j; -5 -~ @ ~ 2 ®:® k; 1.5 ~ @ ) { sout | i | j | k; }
     994for ( i; 5 ®:® j; -5 -~ @ ~ 2 ®:® k; 1.5 ~ @ ) { sout | i | j | k; }
     995for ( i; 5 ®:® k; 1.5 ~ @ ®:® j; -5 -~ @ ~ 2 ) { sout | i | j | k; }
    996996\end{cfa}
    997997&
    998998\begin{cfa}
    999 switch ( i ) {
    1000   case 1: case 3 : case 5:
    1001         ...
    1002   case 2: case 4 : case 6:
    1003         ...
    1004 }
    1005 \end{cfa}
    1006 &
    1007 \begin{cfa}
    1008 
    1009 // odd values
    1010 
    1011 // even values
    1012 
    1013 
     999empty
     1000empty
     1001empty
     1002zero
     1003A
     1004A A A A A A A A A A
     1005A A A A A A A A A A A
     1006B B B B B
     1007C C C C C
     1008D D D D D
     1009E E E E E
     10100 1 2 3 4 5 6 7 8 9
     10110 1 2 3 4 5 6 7 8 9 10
     10121 3 5 7 9
     101310 8 6 4 2
     10140.5 1.5 2.5 3.5 4.5
     10155.5 4.5 3.5 2.5 1.5
     10162 4 6 8 10
     101710 8 6 4 2
     1018
     1019N N N N N N N N N N
     10200 1 2 3 4 5 6 7 8 9
     102110 9 8 7 6 5 4 3 2 1
     1022
     10233 6 9
     10241 2 3 4 5 6 7 8 9 10
     102510 9 8 7 6 5 4 3 2 1 0
     10262 4 6 8 10
     10272.1 3.8 5.5 7.2 8.9
     102810 8 6 4 2 0
     102912.1 10.4 8.7 7. 5.3 3.6
     10300 -5 1 -4 2 -3 3 -2 4 -1
     10310 -5 1 -6 2 -7 3 -8 4 -9
     10320 -5 1 -3 2 -1 3 1 4 3
     10330 -5 1 -7 2 -9 3 -11 4 -13
     10340 -5 1 -4 2 -3 3 -2 4 -1
     10350 -5 1 -6 2 -7 3 -8 4 -9
     10360 -5 1 -3 2 -1 3 1 4 3
     10370 -5 1 -7 2 -9 3 -11 4 -13
     10380 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5
     10390 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5
     10400 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5
    10141041\end{cfa}
    10151042\end{tabular}
    1016 \end{cquote}
    1017 In addition, subranges are allowed to specify case values.\footnote{
    1018 gcc has the same mechanism but awkward syntax, \lstinline@2 ...42@, because a space is required after a number, otherwise the period is a decimal point.}
    1019 \begin{cfa}
    1020 switch ( i ) {
    1021   case ®1~5:® §\C{// 1, 2, 3, 4, 5}§
    1022         ...
    1023   case ®10~15:® §\C{// 10, 11, 12, 13, 14, 15}§
    1024         ...
    1025 }
    1026 \end{cfa}
    1027 Lists of subranges are also allowed.
    1028 \begin{cfa}
    1029 case ®1~5, 12~21, 35~42®:
    1030 \end{cfa}
    1031 
     1043\caption{Loop Control Examples}
     1044\label{f:LoopControlExamples}
     1045\end{figure}
    10321046
    10331047% for ()  => for ( ;; )
     
    10401054
    10411055
     1056\subsection{Loop Control}
     1057
     1058The ©for©/©while©/©do-while© loop-control allows empty or simplified ranges (see Figure~\ref{f:LoopControlExamples}).
     1059\begin{itemize}
     1060\item
     1061The loop index is polymorphic in the type of the comparison value N (when the start value is implicit) or the start value M.
     1062\item
     1063An empty conditional implies comparison value of ©1© (true).
     1064\item
     1065A comparison N is implicit up-to exclusive range [0,N©®)®©.
     1066\item
     1067A comparison ©=© N is implicit up-to inclusive range [0,N©®]®©.
     1068\item
     1069The up-to range M ©~©\index{~@©~©} N means exclusive range [M,N©®)®©.
     1070\item
     1071The up-to range M ©~=©\index{~=@©~=©} N means inclusive range [M,N©®]®©.
     1072\item
     1073The down-to range M ©-~©\index{-~@©-~©} N means exclusive range [N,M©®)®©.
     1074\item
     1075The down-to range M ©-~=©\index{-~=@©-~=©} N means inclusive range [N,M©®]®©.
     1076\item
     1077©0© is the implicit start value;
     1078\item
     1079©1© is the implicit increment value.
     1080\item
     1081The up-to range uses operator ©+=© for increment;
     1082\item
     1083The down-to range uses operator ©-=© for decrement.
     1084\item
     1085©@© means put nothing in this field.
     1086\item
     1087©:© means start another index.
     1088\end{itemize}
     1089
     1090
    10421091%\subsection{\texorpdfstring{Labelled \protect\lstinline@continue@ / \protect\lstinline@break@}{Labelled continue / break}}
    10431092\subsection{\texorpdfstring{Labelled \LstKeywordStyle{continue} / \LstKeywordStyle{break} Statement}{Labelled continue / break Statement}}
     
    10491098for ©break©, the target label can also be associated with a ©switch©, ©if© or compound (©{}©) statement.
    10501099\VRef[Figure]{f:MultiLevelExit} shows ©continue© and ©break© indicating the specific control structure, and the corresponding C program using only ©goto© and labels.
    1051 The innermost loop has 7 exit points, which cause continuation or termination of one or more of the 7 \Index{nested control-structure}s.
     1100The innermost loop has 8 exit points, which cause continuation or termination of one or more of the 7 \Index{nested control-structure}s.
    10521101
    10531102\begin{figure}
    1054 \begin{tabular}{@{\hspace{\parindentlnth}}l@{\hspace{\parindentlnth}}l@{\hspace{\parindentlnth}}l@{}}
    1055 \multicolumn{1}{@{\hspace{\parindentlnth}}c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}   & \multicolumn{1}{@{\hspace{\parindentlnth}}c}{\textbf{C}}      \\
    1056 \begin{cfa}
    1057 ®LC:® {
    1058         ... §declarations§ ...
    1059         ®LS:® switch ( ... ) {
    1060           case 3:
    1061                 ®LIF:® if ( ... ) {
    1062                         ®LF:® for ( ... ) {
    1063                                 ®LW:® while ( ... ) {
    1064                                         ... break ®LC®; ...
    1065                                         ... break ®LS®; ...
    1066                                         ... break ®LIF®; ...
    1067                                         ... continue ®LF;® ...
    1068                                         ... break ®LF®; ...
    1069                                         ... continue ®LW®; ...
    1070                                         ... break ®LW®; ...
    1071                                 } // while
    1072                         } // for
    1073                 } else {
    1074                         ... break ®LIF®; ...
    1075                 } // if
    1076         } // switch
     1103\centering
     1104\begin{lrbox}{\myboxA}
     1105\begin{cfa}[tabsize=3]
     1106®Compound:® {
     1107        ®Try:® try {
     1108                ®For:® for ( ... ) {
     1109                        ®While:® while ( ... ) {
     1110                                ®Do:® do {
     1111                                        ®If:® if ( ... ) {
     1112                                                ®Switch:® switch ( ... ) {
     1113                                                        case 3:
     1114                                                                ®break Compound®;
     1115                                                                ®break Try®;
     1116                                                                ®break For®;      /* or */  ®continue For®;
     1117                                                                ®break While®;  /* or */  ®continue While®;
     1118                                                                ®break Do®;      /* or */  ®continue Do®;
     1119                                                                ®break If®;
     1120                                                                ®break Switch®;
     1121                                                        } // switch
     1122                                                } else {
     1123                                                        ... ®break If®; ...     // terminate if
     1124                                                } // if
     1125                                } while ( ... ); // do
     1126                        } // while
     1127                } // for
     1128        } ®finally® { // always executed
     1129        } // try
    10771130} // compound
    10781131\end{cfa}
    1079 &
    1080 \begin{cfa}
     1132\end{lrbox}
     1133
     1134\begin{lrbox}{\myboxB}
     1135\begin{cfa}[tabsize=3]
    10811136{
    1082         ... §declarations§ ...
    1083         switch ( ... ) {
    1084           case 3:
    1085                 if ( ... ) {
    1086                         for ( ... ) {
    1087                                 while ( ... ) {
    1088                                         ... goto ®LC®; ...
    1089                                         ... goto ®LS®; ...
    1090                                         ... goto ®LIF®; ...
    1091                                         ... goto ®LFC®; ...
    1092                                         ... goto ®LFB®; ...
    1093                                         ... goto ®LWC®; ...
    1094                                         ... goto ®LWB®; ...
    1095                                   ®LWC®: ; } ®LWB:® ;
    1096                           ®LFC:® ; } ®LFB:® ;
    1097                 } else {
    1098                         ... goto ®LIF®; ...
    1099                 } ®L3:® ;
    1100         } ®LS:® ;
    1101 } ®LC:® ;
    1102 \end{cfa}
    1103 &
    1104 \begin{cfa}
    1105 
    1106 
    1107 
    1108 
    1109 
    1110 
    1111 
    1112 // terminate compound
    1113 // terminate switch
    1114 // terminate if
    1115 // continue loop
    1116 // terminate loop
    1117 // continue loop
    1118 // terminate loop
    1119 
    1120 
    1121 
    1122 // terminate if
    1123 
    1124 
    1125 
    1126 \end{cfa}
    1127 \end{tabular}
     1137
     1138                ®ForC:® for ( ... ) {
     1139                        ®WhileC:® while ( ... ) {
     1140                                ®DoC:® do {
     1141                                        if ( ... ) {
     1142                                                switch ( ... ) {
     1143                                                        case 3:
     1144                                                                ®goto Compound®;
     1145                                                                ®goto Try®;
     1146                                                                ®goto ForB®;      /* or */  ®goto ForC®;
     1147                                                                ®goto WhileB®;  /* or */  ®goto WhileC®;
     1148                                                                ®goto DoB®;      /* or */  ®goto DoC®;
     1149                                                                ®goto If®;
     1150                                                                ®goto Switch®;
     1151                                                        } ®Switch:® ;
     1152                                                } else {
     1153                                                        ... ®goto If®; ...      // terminate if
     1154                                                } ®If:®;
     1155                                } while ( ... ); ®DoB:® ;
     1156                        } ®WhileB:® ;
     1157                } ®ForB:® ;
     1158
     1159
     1160} ®Compound:® ;
     1161\end{cfa}
     1162\end{lrbox}
     1163
     1164\subfloat[\CFA]{\label{f:CFibonacci}\usebox\myboxA}
     1165\hspace{2pt}
     1166\vrule
     1167\hspace{2pt}
     1168\subfloat[C]{\label{f:CFAFibonacciGen}\usebox\myboxB}
    11281169\caption{Multi-level Exit}
    11291170\label{f:MultiLevelExit}
     
    13801421try {
    13811422        f(...);
    1382 } catch( E e ; §boolean-predicate§ ) {          §\C[8cm]{// termination handler}§
     1423} catch( E e ; §boolean-predicate§ ) {          §\C{// termination handler}§
    13831424        // recover and continue
    1384 } catchResume( E e ; §boolean-predicate§ ) { §\C{// resumption handler}\CRT§
     1425} catchResume( E e ; §boolean-predicate§ ) { §\C{// resumption handler}§
    13851426        // repair and return
    13861427} finally {
     
    34453486For implicit formatted input, the common case is reading a sequence of values separated by whitespace, where the type of an input constant must match with the type of the input variable.
    34463487\begin{cquote}
    3447 \begin{lrbox}{\LstBox}
     3488\begin{lrbox}{\myboxA}
    34483489\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    34493490int x;   double y   char z;
     
    34513492\end{lrbox}
    34523493\begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{3em}}l@{}}
    3453 \multicolumn{1}{@{}l@{}}{\usebox\LstBox} \\
     3494\multicolumn{1}{@{}l@{}}{\usebox\myboxA} \\
    34543495\multicolumn{1}{c@{\hspace{2em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{\CC}}       & \multicolumn{1}{c}{\textbf{Python}}   \\
    34553496\begin{cfa}[aboveskip=0pt,belowskip=0pt]
     
    65476588hence, names in these include files are not mangled\index{mangling!name} (see~\VRef{s:Interoperability}).
    65486589All other C header files must be explicitly wrapped in ©extern "C"© to prevent name mangling.
    6549 For \Index*[C++]{\CC{}}, the name-mangling issue is often handled internally in many C header-files through checks for preprocessor variable ©__cplusplus©, which adds appropriate ©extern "C"© qualifiers.
     6590This approach is different from \Index*[C++]{\CC{}} where the name-mangling issue is handled internally in C header-files through checks for preprocessor variable ©__cplusplus©, which adds appropriate ©extern "C"© qualifiers.
    65506591
    65516592
     
    65616602The storage-management routines extend their C equivalents by overloading, alternate names, providing shallow type-safety, and removing the need to specify the allocation size for non-array types.
    65626603
    6563 Storage management provides the following capabilities:
     6604C storage management provides the following capabilities:
    65646605\begin{description}
    6565 \item[fill]
    6566 after allocation the storage is filled with a specified character.
     6606\item[filled]
     6607after allocation with a specified character or value.
    65676608\item[resize]
    6568 an existing allocation is decreased or increased in size.
    6569 In either case, new storage may or may not be allocated and, if there is a new allocation, as much data from the existing allocation is copied.
     6609an existing allocation to decreased or increased its size.
     6610In either case, new storage may or may not be allocated and, if there is a new allocation, as much data from the existing allocation is copied into the new allocation.
    65706611For an increase in storage size, new storage after the copied data may be filled.
    6571 \item[alignment]
    6572 an allocation starts on a specified memory boundary, \eg, an address multiple of 64 or 128 for cache-line purposes.
     6612\item[align]
     6613an allocation on a specified memory boundary, \eg, an address multiple of 64 or 128 for cache-line purposes.
    65736614\item[array]
    65746615the allocation size is scaled to the specified number of array elements.
    65756616An array may be filled, resized, or aligned.
    65766617\end{description}
    6577 The table shows allocation routines supporting different combinations of storage-management capabilities:
    6578 \begin{center}
    6579 \begin{tabular}{@{}r|r|l|l|l|l@{}}
     6618\VRef[Table]{t:AllocationVersusCapabilities} shows allocation routines supporting different combinations of storage-management capabilities.
     6619\begin{table}
     6620\centering
     6621\begin{minipage}{0.75\textwidth}
     6622\begin{tabular}{@{}r|l|l|l|l|l@{}}
    65806623\multicolumn{1}{c}{}&           & \multicolumn{1}{c|}{fill}     & resize        & alignment     & array \\
    65816624\hline
    65826625C               & ©malloc©                      & no                    & no            & no            & no    \\
    65836626                & ©calloc©                      & yes (0 only)  & no            & no            & yes   \\
    6584                 & ©realloc©                     & no/copy               & yes           & no            & no    \\
     6627                & ©realloc©                     & copy                  & yes           & no            & no    \\
    65856628                & ©memalign©            & no                    & no            & yes           & no    \\
     6629                & ©aligned_alloc©\footnote{Same as ©memalign© but size is an integral multiple of alignment, which is universally ignored.}
     6630                                                        & no                    & no            & yes           & no    \\
    65866631                & ©posix_memalign©      & no                    & no            & yes           & no    \\
     6632                & ©valloc©                      & no                    & no            & yes (page size)& no   \\
     6633                & ©pvalloc©\footnote{Same as ©valloc© but rounds size to multiple of page size.}
     6634                                                        & no                    & no            & yes (page size)& no   \\
    65876635\hline
    6588 C11             & ©aligned_alloc©       & no                    & no            & yes           & no    \\
    6589 \hline
    6590 \CFA    & ©alloc©                       & no/copy/yes   & no/yes        & no            & yes   \\
    6591                 & ©align_alloc©         & no/yes                & no            & yes           & yes   \\
     6636\CFA    & ©cmemalign©           & yes (0 only)  & no            & yes           & yes   \\
     6637                & ©realloc©                     & copy                  & yes           & yes           & no    \\
     6638                & ©alloc©                       & no                    & yes           & no            & yes   \\
     6639                & ©alloc_set©           & yes                   & yes           & no            & yes   \\
     6640                & ©alloc_align©         & no                    & yes           & yes           & yes   \\
     6641                & ©alloc_align_set©     & yes                   & yes           & yes           & yes   \\
    65926642\end{tabular}
    6593 \end{center}
    6594 It is impossible to resize with alignment because the underlying ©realloc© allocates storage if more space is needed, and it does not honour alignment from the original allocation.
     6643\end{minipage}
     6644\caption{Allocation Routines versus Storage-Management Capabilities}
     6645\label{t:AllocationVersusCapabilities}
     6646\end{table}
     6647
     6648\CFA memory management extends the type safety of all allocations by using the type of the left-hand-side type to determine the allocation size and return a matching type for the new storage.
     6649Type-safe allocation is provided for all C allocation routines and new \CFA allocation routines, \eg in
     6650\begin{cfa}
     6651int * ip = (int *)malloc( sizeof(int) );                §\C{// C}§
     6652int * ip = malloc();                                                    §\C{// \CFA type-safe version of C malloc}§
     6653int * ip = alloc();                                                             §\C{// \CFA type-safe uniform alloc}§
     6654\end{cfa}
     6655the latter two allocations determine the allocation size from the type of ©p© (©int©) and cast the pointer to the allocated storage to ©int *©.
     6656
     6657\CFA memory management extends allocation safety by implicitly honouring all alignment requirements, \eg in
     6658\begin{cfa}
     6659struct S { int i; } __attribute__(( aligned( 128 ) )); // cache-line alignment
     6660S * sp = malloc();                                                              §\C{// honour type alignment}§
     6661\end{cfa}
     6662the storage allocation is implicitly aligned to 128 rather than the default 16.
     6663The alignment check is performed at compile time so there is no runtime cost.
     6664
     6665\CFA memory management extends the resize capability with the notion of \newterm{sticky properties}.
     6666Hence, initial allocation capabilities are remembered and maintained when resize requires copying.
     6667For example, an initial alignment and fill capability are preserved during a resize copy so the copy has the same alignment and extended storage is filled.
     6668Without sticky properties it is dangerous to use ©realloc©, resulting in an idiom of manually performing the reallocation to maintain correctness.
     6669\begin{cfa}
     6670
     6671\end{cfa}
     6672
     6673\CFA memory management extends allocation to support constructors for initialization of allocated storage, \eg in
     6674\begin{cfa}
     6675struct S { int i; };                                                    §\C{// cache-line aglinment}§
     6676void ?{}( S & s, int i ) { s.i = i; }
     6677// assume ?|? operator for printing an S
     6678
     6679S & sp = *®new®( 3 );                                                   §\C{// call constructor after allocation}§
     6680sout | sp.i;
     6681®delete®( &sp );
     6682
     6683S * spa = ®anew®( 10, 5 );                                              §\C{// allocate array and initialize each array element}§
     6684for ( i; 10 ) sout | spa[i] | nonl;
     6685sout | nl;
     6686®adelete®( 10, spa );
     6687\end{cfa}
     6688Allocation routines ©new©/©anew© allocate a variable/array and initialize storage using the allocated type's constructor.
     6689Note, the matching deallocation routines ©delete©/©adelete©.
    65956690
    65966691\leavevmode
    65976692\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    6598 // C unsafe allocation
    65996693extern "C" {
    6600 void * malloc( size_t size );§\indexc{memset}§
    6601 void * calloc( size_t dim, size_t size );§\indexc{calloc}§
    6602 void * realloc( void * ptr, size_t size );§\indexc{realloc}§
    6603 void * memalign( size_t align, size_t size );§\indexc{memalign}§
    6604 int posix_memalign( void ** ptr, size_t align, size_t size );§\indexc{posix_memalign}§
    6605 
    6606 // C unsafe initialization/copy
    6607 void * memset( void * dest, int c, size_t size );
    6608 void * memcpy( void * dest, const void * src, size_t size );
    6609 }
     6694        // C unsafe allocation
     6695        void * malloc( size_t size );§\indexc{malloc}§
     6696        void * calloc( size_t dim, size_t size );§\indexc{calloc}§
     6697        void * realloc( void * ptr, size_t size );§\indexc{realloc}§
     6698        void * memalign( size_t align, size_t size );§\indexc{memalign}§
     6699        void * aligned_alloc( size_t align, size_t size );§\indexc{aligned_alloc}§
     6700        int posix_memalign( void ** ptr, size_t align, size_t size );§\indexc{posix_memalign}§
     6701        void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize );§\indexc{cmemalign}§ // CFA
     6702
     6703        // C unsafe initialization/copy
     6704        void * memset( void * dest, int c, size_t size );§\indexc{memset}§
     6705        void * memcpy( void * dest, const void * src, size_t size );§\indexc{memcpy}§
     6706}
     6707
     6708void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap
    66106709
    66116710forall( dtype T | sized(T) ) {
    6612 // §\CFA§ safe equivalents, i.e., implicit size specification
     6711        // §\CFA§ safe equivalents, i.e., implicit size specification
    66136712        T * malloc( void );
    66146713        T * calloc( size_t dim );
    66156714        T * realloc( T * ptr, size_t size );
    66166715        T * memalign( size_t align );
     6716        T * cmemalign( size_t align, size_t dim  );
    66176717        T * aligned_alloc( size_t align );
    66186718        int posix_memalign( T ** ptr, size_t align );
    66196719
    6620 // §\CFA§ safe general allocation, fill, resize, array
    6621         T * alloc( void );§\indexc{alloc}§
    6622         T * alloc( char fill );
    6623         T * alloc( size_t dim );
    6624         T * alloc( size_t dim, char fill );
    6625         T * alloc( T ptr[], size_t dim );
    6626         T * alloc( T ptr[], size_t dim, char fill );
    6627 
    6628 // §\CFA§ safe general allocation, align, fill, array
    6629         T * align_alloc( size_t align );
    6630         T * align_alloc( size_t align, char fill );
    6631         T * align_alloc( size_t align, size_t dim );
    6632         T * align_alloc( size_t align, size_t dim, char fill );
    6633 
    6634 // §\CFA§ safe initialization/copy, i.e., implicit size specification
    6635         T * memset( T * dest, char c );§\indexc{memset}§
     6720        // §\CFA§ safe general allocation, fill, resize, alignment, array
     6721        T * alloc( void );§\indexc{alloc}§                                      §\C[3.5in]{// variable, T size}§
     6722        T * alloc( size_t dim );                                                        §\C{// array[dim], T size elements}§
     6723        T * alloc( T ptr[], size_t dim );                                       §\C{// realloc array[dim], T size elements}§
     6724
     6725        T * alloc_set( char fill );§\indexc{alloc_set}§         §\C{// variable, T size, fill bytes with value}§
     6726        T * alloc_set( T fill );                                                        §\C{// variable, T size, fill with value}§
     6727        T * alloc_set( size_t dim, char fill );                         §\C{// array[dim], T size elements, fill bytes with value}§
     6728        T * alloc_set( size_t dim, T fill );                            §\C{// array[dim], T size elements, fill elements with value}§
     6729        T * alloc_set( size_t dim, const T fill[] );            §\C{// array[dim], T size elements, fill elements with array}§
     6730        T * alloc_set( T ptr[], size_t dim, char fill );        §\C{// realloc array[dim], T size elements, fill bytes with value}§
     6731
     6732        T * alloc_align( size_t align );                                        §\C{// aligned variable, T size}§
     6733        T * alloc_align( size_t align, size_t dim );            §\C{// aligned array[dim], T size elements}§
     6734        T * alloc_align( T ptr[], size_t align );                       §\C{// realloc new aligned array}§
     6735        T * alloc_align( T ptr[], size_t align, size_t dim ); §\C{// realloc new aligned array[dim]}§
     6736
     6737        T * alloc_align_set( size_t align, char fill );         §\C{// aligned variable, T size, fill bytes with value}§
     6738        T * alloc_align_set( size_t align, T fill );            §\C{// aligned variable, T size, fill with value}§
     6739        T * alloc_align_set( size_t align, size_t dim, char fill ); §\C{// aligned array[dim], T size elements, fill bytes with value}§
     6740        T * alloc_align_set( size_t align, size_t dim, T fill ); §\C{// aligned array[dim], T size elements, fill elements with value}§
     6741        T * alloc_align_set( size_t align, size_t dim, const T fill[] ); §\C{// aligned array[dim], T size elements, fill elements with array}§
     6742        T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); §\C{// realloc new aligned array[dim], fill new bytes with value}§
     6743
     6744        // §\CFA§ safe initialization/copy, i.e., implicit size specification
     6745        T * memset( T * dest, char fill );§\indexc{memset}§
    66366746        T * memcpy( T * dest, const T * src );§\indexc{memcpy}§
    66376747
    6638 // §\CFA§ safe initialization/copy array
    6639         T * amemset( T dest[], char c, size_t dim );
     6748        // §\CFA§ safe initialization/copy, i.e., implicit size specification, array types
     6749        T * amemset( T dest[], char fill, size_t dim );
    66406750        T * amemcpy( T dest[], const T src[], size_t dim );
    66416751}
    66426752
    6643 // §\CFA§ allocation/deallocation and constructor/destructor
    6644 forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * new( Params p );§\indexc{new}§
    6645 forall( dtype T | { void ^?{}( T * ); } ) void delete( T * ptr );§\indexc{delete}§
    6646 forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } )
     6753// §\CFA§ allocation/deallocation and constructor/destructor, non-array types
     6754forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );§\indexc{new}§
     6755forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );§\indexc{delete}§
     6756forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } )
    66476757  void delete( T * ptr, Params rest );
    66486758
    6649 // §\CFA§ allocation/deallocation and constructor/destructor, array
    6650 forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * anew( size_t dim, Params p );§\indexc{anew}§
    6651 forall( dtype T | sized(T) | { void ^?{}( T * ); } ) void adelete( size_t dim, T arr[] );§\indexc{adelete}§
    6652 forall( dtype T | sized(T) | { void ^?{}( T * ); }, ttype Params | { void adelete( Params ); } )
     6759// §\CFA§ allocation/deallocation and constructor/destructor, array types
     6760forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );§\indexc{anew}§
     6761forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );§\indexc{adelete}§
     6762forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
    66536763  void adelete( size_t dim, T arr[], Params rest );
    66546764\end{cfa}
Note: See TracChangeset for help on using the changeset viewer.