Changeset 58e2ce34


Ignore:
Timestamp:
Apr 23, 2024, 2:26:27 PM (12 days ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
5bc81e9
Parents:
b9b6efb
Message:

update postfix function, storage management, and memory set/copy

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/user/user.tex

    rb9b6efb r58e2ce34  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Thu Apr 18 21:53:45 2024
    14 %% Update Count     : 6502
     13%% Last Modified On : Tue Apr 23 14:13:10 2024
     14%% Update Count     : 6623
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    6969\lstset{language=CFA}                                                                   % CFA default lnaguage
    7070\lstnewenvironment{C++}[1][]                            % use C++ style
    71 {\lstset{language=C++,moredelim=**[is][\protect\color{red}]{®}{®},#1}}
     71{\lstset{language=C++,escapechar=§,moredelim=**[is][\protect\color{red}]{®}{®},#1}}
    7272{}
    7373
     
    29612961Since the strings overlap starting with the open bracket, ©[©, there is an ambiguous interpretation for the string.
    29622962
    2963 {\color{red}
    29642963As well, \CFA-style declarations cannot be used to declare parameters for C-style routine-definitions because of the following ambiguity:
    29652964\begin{cfa}
     
    29672966int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§
    29682967\end{cfa}
    2969 The string ``©int (* foo)©'' declares a C-style named-parameter of type pointer to an integer (the parenthesis are superfluous), while the same string declares a \CFA style unnamed parameter of type routine returning integer with unnamed parameter of type pointer to foo.
     2968The string ``©int (* foo)©'' declares a C-style named-parameter of type pointer to an integer (the parenthesis are superfluous), while the same string declares a \CFA style unnamed parameter of type routine returning integer with unnamed parameter of type pointer to ©foo©.
    29702969The redefinition of a type name in a parameter list is the only context in C where the character ©*© can appear to the left of a type name, and \CFA relies on all type qualifier characters appearing to the right of the type name.
    2971 The inability to use \CFA declarations in these two contexts is probably a blessing because it precludes programmers from arbitrarily switching between declarations forms within a declaration contexts.}
     2970The inability to use \CFA declarations in these two contexts is probably a blessing because it precludes programmers from arbitrarily switching between declarations forms within a declaration contexts.
    29722971
    29732972C-style declarations can be used to declare parameters for \CFA style routine definitions, \eg:
     
    30613060\subsection{Postfix Function}
    30623061\label{s:PostfixFunction}
     3062\index{postfix function}
    30633063
    30643064\CFA provides an alternative call syntax where the argument appears before the function name.
     
    30813081'1'`m;
    30823082"123" "456"`m;
    3083 [1,2,3]`t;
     3083[1, 2, 3]`t;
    30843084\end{cfa}
    30853085&       
     
    31003100\end{tabular}
    31013101\end{cquote}
     3102Note, to pass \emph{multiple} arguments to a postfix function requires a \Index{tuple}, \eg ©[1, 2, 3]`t©, which forms a single argument that is flattened into the multiple arguments \see{\VRef{s:Tuple}}.
     3103Similarly, if the argument is an expression, it must be parenthesized, \eg ©(i + 3)`h©, or only the last operand of the expression is the argument, \eg ©i + (3`h)©.
    31023104
    31033105\VRef[Figure]{f:UnitsComparison} shows a common usage for postfix functions: converting basic literals into user literals.
     
    31243126        return l.stones + r.stones;
    31253127}
    3126 Weight ?`st( double w ) { return w; }
    3127 double ?`st( Weight w ) { return w.stones; }
    3128 Weight ?`lb( double w ) { return w / 14.0; }
    3129 double ?`lb( Weight w ) { return w.stones * 14.0; }
    3130 Weight ?`kg( double w ) { return w / 6.35; }
    3131 double ?`kg( Weight w ) { return w.stones * 6.35; }
     3128Weight ®?`st®( double w ) { return w; }
     3129double ®?`st®( Weight w ) { return w.stones; }
     3130Weight ®?`lb®( double w ) { return w / 14.0; }
     3131double ®?`lb®( Weight w ) { return w.stones * 14.0; }
     3132Weight ®?`kg®( double w ) { return w / 6.35; }
     3133double ®?`kg®( Weight w ) { return w.stones * 6.35; }
    31323134int main() {
    31333135        Weight w, heavy = { 20 }; // stones
    3134         w = 155`lb;
    3135         w = 0b_1111`st;
    3136         w = 0_233`lb;
    3137         w = 0x_9b`kg;
    3138         w = 5.5`st + 8`kg + 25.01`lb + heavy;
     3136        w = 155®`lb®;
     3137        w = 0b_1111®`st®;
     3138        w = 0_233®`lb®;
     3139        w = 0x_9b®`kg®;
     3140        w = 5.5®`st® + 8®`kg® + 25.01®`lb® + heavy;
    31393141}
    31403142\end{cfa}
     
    31493151        return l.stones + r.stones;
    31503152}
    3151 Weight operator""_st( long double w ) { return w; }
    3152 Weight operator""_lb( long double w ) { return w / 14.0; }
    3153 Weight operator""_kg( long double w ) { return w / 6.35; }
    3154 Weight operator""_st( unsigned long long int w ) { return w; }
    3155 Weight operator""_lb( unsigned long long int w ) { return w / 14.0; }
    3156 Weight operator""_kg( unsigned long long int w ) { return w / 6.35; }
     3153Weight operator®""_st®( long double w ) { return w; }
     3154Weight operator®""_lb®( long double w ) { return w / 14.0; }
     3155Weight operator®""_kg®( long double w ) { return w / 6.35; }
     3156Weight operator®""_st®( unsigned long long int w ) { return w; }
     3157Weight operator®""_lb®( unsigned long long int w ) { return w / 14.0; }
     3158Weight operator®""_kg®( unsigned long long int w ) { return w / 6.35; }
    31573159int main() {
    31583160        Weight w, heavy = { 20 }; // stones
    3159         w = 155_lb;
    3160         w = 0b1111_st;
    3161         w = 0'233_lb;           // quote separator
    3162         w = 0x9b_kg;
    3163         w = 5.5_st + 8_kg + 25.01_lb + heavy;
     3161        w = 155®_lb®;
     3162        w = 0b1111®_st®;
     3163        w = 0'233®_lb®;         // quote separator
     3164        w = 0x9b®_kg®;
     3165        w = 5.5®_st® + 8®_kg® + 25.01®_lb® + heavy;
    31643166}
    31653167\end{C++}
     
    35443546
    35453547\section{Tuple}
     3548\label{s:Tuple}
    35463549
    35473550In C and \CFA, lists of elements appear in several contexts, such as the parameter list of a routine call.
     
    42024205[ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = §\emph{expr}§;
    42034206\end{cfa}
    4204 \index{lvalue}
    4205 The left-hand side is a tuple of \LstBasicStyle{\emph{lvalues}}, which is a list of expressions each yielding an address, \ie any data object that can appear on the left-hand side of a conventional assignment statement.
     4207The left-hand side is a tuple of \LstBasicStyle{\emph{\Index{lvalue}}}s, which is a list of expressions each yielding an address, \ie any data object that can appear on the left-hand side of a conventional assignment statement.
    42064208\LstBasicStyle{\emph{expr}} is any standard arithmetic expression.
    42074209Clearly, the types of the entities being assigned must be type compatible with the value of the expression.
     
    42414243Multiple assignment has the following form:
    42424244\begin{cfa}
    4243 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = [ §\emph{expr}§, ... , §\emph{expr}§ ];
    4244 \end{cfa}
    4245 \index{lvalue}
    4246 The left-hand side is a tuple of \emph{lvalues}, and the right-hand side is a tuple of \emph{expr}s.
    4247 Each \emph{expr} appearing on the right-hand side of a multiple assignment statement is assigned to the corresponding \emph{lvalues} on the left-hand side of the statement using parallel semantics for each assignment.
     4245[ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = [ §\emph{expr}§, ... , §\emph{expr}§ ];§
     4246\end{cfa}
     4247The left-hand side is a tuple of \LstBasicStyle{\emph{\Index{lvalue}}}s, and the right-hand side is a tuple of \LstBasicStyle{\emph{expr}}s.
     4248Each \LstBasicStyle{\emph{expr}} appearing on the right-hand side of a multiple assignment statement is assigned to the corresponding \LstBasicStyle{\emph{lvalues}} on the left-hand side of the statement using parallel semantics for each assignment.
    42484249An example of multiple assignment is:
    42494250\begin{cfa}
     
    81428143\section{Standard Library}
    81438144\label{s:StandardLibrary}
     8145\index{standard library}
    81448146
    81458147The \CFA standard-library extends existing C library routines by adding new function, wrapping existing explicitly-polymorphic C routines into implicitly-polymorphic versions, and adding new \CFA extensions.
     
    81488150\subsection{Dynamic Storage-Management}
    81498151\label{s:DynamicStorageManagement}
     8152\index{dynamic storage-management}\index{storage management}
    81508153
    81518154Dynamic storage-management in C is based on explicit allocation and deallocation (©malloc©/©free©).
     
    82108213\begin{enumerate}[leftmargin=\parindent]
    82118214\item
    8212 Extend type safety of all allocation routines by using the left-hand assignment type to determine the allocation size and alignment, and return a matching type for the new storage, which removes many common allocation errors.
     8215extends type safety of all allocation routines by using the left-hand assignment type to determine the allocation size and alignment, and return a matching type for the new storage, which removes many common allocation errors.
    82138216\begin{cfa}
    82148217int * ip = (int *)malloc( sizeof(int) ); §\C[2.3in]{// C}§
    82158218int * ip = malloc();                                    §\C{// \CFA type-safe call of C malloc}§
    8216 int * ip = alloc();                                             §\C{// \CFA type-safe call of \CFA alloc}§
    8217 struct __attribute__(( aligned(128) )) spinlock { ... };
     8219int * ip = calloc();                                    §\C{// \CFA type-safe call of C calloc}§
     8220struct __attribute__(( aligned(128) )) spinlock { ... }; // cache alignment
    82188221spinlock * slp = malloc();                              §\C{// correct size, alignment, and return type}\CRT§
    82198222\end{cfa}
     
    82218224
    82228225\item
    8223 Introduce the notion of \newterm{sticky properties} used in resizing.
     8226introduces the notion of \newterm{sticky properties} used in resizing.
    82248227All initial allocation properties are remembered and maintained for use should resize require new storage.
    82258228For example, the initial alignment and fill properties in the initial allocation
     
    82338236
    82348237\item
    8235 Provide resizing without data copying, which is useful to repurpose an existing block of storage for another purpose, rather than freeing the old storage and performing a new allocation.
    8236 The resize might be able to take advantage of unused storage after the data to preventing the free/reallocation step altogether.
    8237 
    8238 \item
    8239 Provide ©free©/©delete© functions that delete a variable number of pointers.
     8238provides resizing without data copying, which is useful to repurpose an existing block of storage, rather than freeing the old storage and performing a new allocation.
     8239A resize can take advantage of unused storage after the data to preventing a free/reallocation step altogether.
     8240
     8241\item
     8242provides ©free©/©delete© functions that delete a variable number of allocations.
    82408243\begin{cfa}
    82418244int * ip = malloc(), * jp = malloc(), * kp = malloc();
    82428245double * xp = malloc(), * yp = malloc(), * zp = malloc();
    8243 free( ®ip, jp, kp, xp, yp, zp® );
    8244 \end{cfa}
    8245 
    8246 \item
    8247 Support constructors for initialization of allocated storage (like \CC) and destructors for deallocation.
     8246free( ®ip, jp, kp, xp, yp, zp® );               §\C{// multiple deallocations}§
     8247\end{cfa}
     8248
     8249\item
     8250supports constructors for initialization of allocated storage and destructors for deallocation (like \CC).
    82488251\begin{cfa}
    82498252struct S { int v; };                                    §\C{// default constructors}§
     
    82618264Allocation routines ©new©/©anew© allocate a variable/array and initialize storage using the allocated type's constructor.
    82628265Note, the matching deallocation routines ©delete©/©adelete©.
     8266\CC only supports the default constructor for intializing array elements.
     8267\begin{C++}
     8268S * sp = new S[10]®{5}®;                                §\C{// disallowed}§
     8269\end{C++}
    82638270\end{enumerate}
    82648271
     
    82748281\end{enumerate}
    82758282
    8276 The polymorphic functions ©T * alloc( ... )© and ©T * alloc( size_t dim, ... )© are overloaded with a variable number of specific allocation properties, or an integer dimension parameter followed by a variable number of specific allocation properties.
    8277 These allocation properties can be passed as named arguments when calling the \lstinline{alloc} routine.
    8278 A call without parameters returns an uninitialized dynamically allocated object of type ©T© (©malloc©).
    8279 A call with only the dimension (dim) parameter returns an uninitialized dynamically allocated array of objects with type ©T© (©aalloc©).
    8280 The variable number of arguments consist of allocation properties, which can be combined to produce different kinds of allocations.
    8281 The properties ©resize© and ©realloc© are associated with the allocation variable indicating how the existing allocation is modified, without or with data copying.
    8282 Function ©alloc© is used extensively in the \CFA runtime.
     8283The polymorphic functions
     8284\begin{cfa}
     8285T * alloc( ... );
     8286T * alloc( size_t dim, ... );
     8287\end{cfa}
     8288are overloaded with a variable number of allocation properties.
     8289These allocation properties can be passed as named arguments when calling the \Indexc{alloc} routine.
     8290A call without parameters returns an uninitialized dynamically allocated object of type ©T© (\Indexc{malloc}).
     8291A call with only the dimension (dim) parameter returns an uninitialized dynamically allocated array of objects with type ©T© (\Indexc{aalloc}).
     8292The variable number of arguments consist of allocation properties to specialize the allocation.
     8293The properties ©resize© and ©realloc© are associated with an existing allocation variable indicating how its storage is modified.
    82838294
    82848295The following allocation property functions may be combined and appear in any order as arguments to ©alloc©,
     
    82868297\item
    82878298©T_align ?`align( size_t alignment )© to align an allocation.
    8288 The alignment parameter must be $\ge$ the default alignment (©libAlign()© in \CFA) and a power of two, \eg:
    8289 \begin{cfa}
    8290 int * i0 = alloc( ®4096`align® );  sout | i0 | nl;
    8291 int * i1 = alloc( 3, ®4096`align® );  sout | i1; for (i; 3 ) sout | &i1[i] | nonl; sout | nl;
    8292 
    8293 0x555555572000
    8294 0x555555574000 0x555555574000 0x555555574004 0x555555574008
    8295 \end{cfa}
    8296 returns a dynamic object and object array aligned on a 4096-byte boundary.
    8297 
    8298 \item
    8299 ©S_fill(T) ?`fill ( /* various types */ )© to initialize storage.
     8299The alignment parameter must be $\ge$ the default alignment (©libAlign()© in \CFA) and a power of two, \eg the following return a dynamic object and object array aligned on a 256 and 4096-byte boundary.
     8300\begin{cfa}
     8301int * i0 = alloc( ®256`align® );  sout | i0 | nl;
     8302int * i1 = alloc( 3, ®4096`align® );  for (i; 3 ) sout | &i1[i] | nonl; sout | nl;
     8303free( i0, i1 );
     8304
     83050x5555565699®00®  // 256 alignment
     83060x55555656c®000® 0x5656c004 0x5656c008  // 4K array alignment
     8307\end{cfa}
     8308
     8309\item
     8310©T_fill(T) ?`fill( /* various types */ )© to initialize storage.
    83008311There are three ways to fill storage:
    83018312\begin{enumerate}
     
    83098320For example:
    83108321\begin{cfa}[numbers=left]
    8311 int * i0 = alloc( ®0n`fill® );  sout | *i0 | nl;  // 0n disambiguates 0
     8322int * i0 = alloc( ®0n`fill® );  sout | *i0 | nl;  // 0n disambiguates 0p
    83128323int * i1 = alloc( ®5`fill® );  sout | *i1 | nl;
    83138324int * i2 = alloc( ®'\xfe'`fill® ); sout | hex( *i2 ) | nl;
     
    83168327int * i5 = alloc( 5, ®i3`fill® );  for ( i; 5 ) sout | i5[i] | nonl; sout | nl; // completely fill from i3
    83178328int * i6 = alloc( 5, ®[i3, 3]`fill® );  for ( i; 5 ) sout | i6[i] | nonl; sout | nl; // partial fill from i3
     8329free( i0, i1, i2, i3, i4, i5, i6 );
    83188330\end{cfa}
    83198331\begin{lstlisting}[numbers=left]
     
    83348346For example:
    83358347\begin{cfa}[numbers=left]
    8336 int * i = alloc( ®5`fill® );  sout | i | *i;
    8337 i = alloc( ®i`resize®, ®256`align®, ®7`fill® );  sout | i | *i;
    8338 double * d = alloc( ®i`resize®, ®4096`align®, ®13.5`fill® );  sout | d | *d;
     8348int * ip = alloc( ®5`fill® );  sout | ip | *ip;
     8349ip = alloc( ®ip`resize®, ®256`align®, ®7`fill® );  sout | ip | *ip;
     8350double * dp = alloc( ®ip`resize®, ®4096`align®, ®13.5`fill® );  sout | dp | *dp;
     8351free( dp );  // DO NOT FREE ip AS ITS STORAGE IS MOVED TO dp
    83398352\end{cfa}
    83408353\begin{lstlisting}[numbers=left]
    8341 0x55555556d5c0 5
    8342 0x555555570000 7
    8343 0x555555571000 13.5
     83540x555555580a80 5
     83550x555555581100 7
     83560x555555587000 13.5
    83448357\end{lstlisting}
    83458358Examples 2 to 3 change the alignment, fill, and size for the initial storage of ©i©.
    83468359
    83478360\begin{cfa}[numbers=left]
    8348 int * ia = alloc( 5, ®5`fill® );  for ( i; 5 ) sout | ia[i] | nonl; sout | nl;
    8349 ia = alloc( 10, ®ia`resize®, ®7`fill® ); for ( i; 10 ) sout | ia[i] | nonl; sout | nl;
    8350 sout | ia; ia = alloc( 5, ®ia`resize®, ®512`align®, ®13`fill® ); sout | ia; for ( i; 5 ) sout | ia[i] | nonl; sout | nl;;
    8351 ia = alloc( 3, ®ia`resize®, ®4096`align®, ®2`fill® );  sout | ia; for ( i; 3 ) sout | &ia[i] | ia[i] | nonl; sout | nl;
     8361int * ia = alloc( 5, ®5`fill® );  sout | ia | nonl;  for ( i; 5 ) sout | ia[i] | nonl; sout | nl;
     8362ia = alloc( 10, ®ia`resize®, ®7`fill® );  sout | ia | nonl;  for ( i; 10 ) sout | ia[i] | nonl; sout | nl;
     8363ia = alloc( 5, ®ia`resize®, ®512`align®, ®13`fill® ); sout | ia | nonl; for ( i; 5 ) sout | ia[i] | nonl; sout | nl;;
     8364ia = alloc( 3, ®ia`resize®, ®4096`align®, ®2`fill® );  for ( i; 3 ) sout | &ia[i] | ia[i] | nonl; sout | nl;
     8365free( ia );
    83528366\end{cfa}
    83538367\begin{lstlisting}[numbers=left]
    8354 5 5 5 5 5
    8355 7 7 7 7 7 7 7 7 7 7
    8356 0x55555556d560 0x555555571a00 13 13 13 13 13
    8357 0x555555572000 0x555555572000 2 0x555555572004 2 0x555555572008 2
     83680x55555656d540 5 5 5 5 5
     83690x55555656d480 7 7 7 7 7 7 7 7 7 7
     83700x55555656fe00 13 13 13 13 13
     83710x555556570000 2 0x555556570004 2 0x555556570008 2
    83588372\end{lstlisting}
    8359 Examples 2 to 4 change the array size, alignment and fill for the initial storage of ©ia©.
     8373Examples 2 to 4 change the array size, alignment, and fill initializes all storage because no data is copied.
    83608374
    83618375\item
     
    83668380For example:
    83678381\begin{cfa}[numbers=left]
    8368 int * i = alloc( ®5`fill® );  sout | i | *i;
    8369 i = alloc( ®i`realloc®, ®256`align® );  sout | i | *i;
    8370 i = alloc( ®i`realloc®, ®4096`align®, ®13`fill® );  sout | i | *i;
     8382int * ip = alloc( ®5`fill® );  sout | ip | *ip;
     8383ip = alloc( ®ip`realloc®, ®256`align® );  sout | ip | *ip;
     8384ip = alloc( ®ip`realloc®, ®4096`align®, ®13`fill® );  sout | ip | *ip;
     8385free( ip );
    83718386\end{cfa}
    83728387\begin{lstlisting}[numbers=left]
     
    83768391\end{lstlisting}
    83778392Examples 2 to 3 change the alignment for the initial storage of ©i©.
    8378 The ©13`fill© in example 3 does nothing because no extra space is added.
     8393The ©13`fill© in example 3 does nothing because no new storage is added.
    83798394
    83808395\begin{cfa}[numbers=left]
    8381 int * ia = alloc( 5, ®5`fill® );  for ( i; 5 ) sout | ia[i]; sout | nl;
    8382 ia = alloc( 10, ®ia`realloc®, ®7`fill® ); for ( i; 10 ) sout | ia[i]; sout | nl;
    8383 sout | ia; ia = alloc( 1, ®ia`realloc®, ®512`align®, ®13`fill® ); sout | ia; for ( i; 1 ) sout | ia[i]; sout | nl;;
    8384 ia = alloc( 3, ®ia`realloc®, ®4096`align®, ®2`fill® );  sout | ia; for ( i; 3 ) sout | &ia[i] | ia[i]; sout | nl;
     8396int * ia = alloc( 5, ®5`fill® );  sout | ia | nonl;  for ( i; 5 ) sout | ia[i] | nonl; sout | nl;
     8397ia = alloc( 10, ®ia`realloc®, ®7`fill® );  sout | ia | nonl;  for ( i; 10 ) sout | ia[i] | nonl; sout | nl;
     8398ia = alloc( 5, ®ia`realloc®, ®512`align®, ®13`fill® );  sout | ia | nonl; for ( i; 5 ) sout | ia[i] | nonl; sout | nl;;
     8399ia = alloc( 3, ®ia`realloc®, ®4096`align®, ®2`fill® );  for ( i; 3 ) sout | &ia[i] | ia[i] | nonl; sout | nl;
     8400free( ia );
    83858401\end{cfa}
    83868402\begin{lstlisting}[numbers=left]
    8387 5 5 5 5 5
    8388 5 5 5 5 5 7 7 7 7 7
    8389 0x55555556c560 0x555555570a00 5
    8390 0x555555571000 0x555555571000 5 0x555555571004 2 0x555555571008 2
     84030x55555656d540 5 5 5 5 5
     84040x55555656d480 7 7 7 7 7 7 7 7 7 7
     84050x555556570e00 5 5 5 5 5
     84060x5555556571000 5 0x555556571004 5 0x555556571008 5
    83918407\end{lstlisting}
    8392 Examples 2 to 4 change the array size, alignment and fill for the initial storage of ©ia©.
    8393 The ©13`fill© in example 3 does nothing because no extra space is added.
     8408Examples 2 to 4 change the array size, alignment, and fill does no initialization after the copied data, as no new storage is added.
    83948409\end{itemize}
    83958410
     
    83978412\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    83988413extern "C" {
    8399         // New allocation operations.
     8414        // New C allocation operations.
    84008415        void * aalloc( size_t dim, size_t elemSize );§\indexc{aalloc}§
    84018416        void * resize( void * oaddr, size_t size );§\indexc{resize}§
     
    84068421        size_t malloc_size( void * addr );§\indexc{malloc_size}§
    84078422        int malloc_stats_fd( int fd );§\indexc{malloc_stats_fd}§
    8408         size_t malloc_expansion();§\indexc{malloc_expansion}§ $\C{// heap expansion size (bytes)}$
    8409         size_t malloc_mmap_start();§\indexc{malloc_mmap_start}§ $\C{// crossover allocation size from sbrk to mmap}$
    8410         size_t malloc_unfreed();§\indexc{malloc_unfreed()}§     $\C{// heap unfreed size (bytes)}$
    8411         void malloc_stats_clear();§\indexc{malloc_stats_clear}§ $\C{// clear heap statistics}$
     8423        size_t malloc_expansion();§\indexc{malloc_expansion}§ §\C{// heap expansion size (bytes)}§
     8424        size_t malloc_mmap_start();§\indexc{malloc_mmap_start}§ §\C{// crossover allocation size from sbrk to mmap}§
     8425        size_t malloc_unfreed();§\indexc{malloc_unfreed()}§     §\C{// heap unfreed size (bytes)}§
     8426        void malloc_stats_clear();§\indexc{malloc_stats_clear}§ §\C{// clear heap statistics}§
    84128427}
    84138428
     
    84238438        T * calloc( size_t dim );§\indexc{calloc}§
    84248439        T * resize( T * ptr, size_t size );§\indexc{resize}§
     8440        T * resize( T * ptr, size_t alignment, size_t size );
    84258441        T * realloc( T * ptr, size_t size );§\indexc{realloc}§
     8442        T * realloc( T * ptr, size_t alignment, size_t size );
    84268443        T * reallocarray( T * ptr, size_t dim );§\indexc{reallocarray}§
     8444        T * reallocarray( T * ptr, size_t alignment, size_t dim );
    84278445        T * memalign( size_t align );§\indexc{memalign}§
    84288446        T * amemalign( size_t align, size_t dim );§\indexc{amemalign}§
     
    84378455        T * alloc( size_t dim, ... );
    84388456        T_align ?`align( size_t alignment );§\indexc{align}§
    8439         S_fill(T) ?`fill( /* various types */ );§\indexc{fill}§
    8440         S_resize(T) ?`resize( void * oaddr );§\indexc{resize}§
    8441         S_realloc(T) ?`realloc( T * a ));§\indexc{realloc}§
    8442 
    8443         // §\CFA§ safe initialization/copy, i.e., implicit size specification
    8444         T * memset( T * dest, char fill );§\indexc{memset}§
    8445         T * memcpy( T * dest, const T * src );§\indexc{memcpy}§
    8446 
    8447         // §\CFA§ safe initialization/copy, i.e., implicit size specification, array types
    8448         T * amemset( T dest[], char fill, size_t dim );
     8457        T_fill(T) ?`fill( /* various types */ );§\indexc{fill}§
     8458        T_resize ?`resize( void * oaddr );§\indexc{resize}§
     8459        T_realloc ?`realloc( void * oaddr ));§\indexc{realloc}§
     8460}
     8461
     8462forall( T &, List ... ) void free( T * ptr, ... ) // deallocation list
     8463
     8464// §\CFA§ allocation/deallocation and constructor/destructor, non-array types
     8465forall( T &, Parms ... | { void ?{}( T &, Parms ); } ) T * new( Parms ... );§\indexc{new}§
     8466forall( T &, List ... | { void ^?{}( T & ); void delete( List ... ); } );§\indexc{delete}§
     8467// §\CFA§ allocation/deallocation and constructor/destructor, array types
     8468forall( T & | sized(T), Parms ... | { void ?{}( T &, Parms ); } ) T * anew( size_t dim, Parms ... );§\indexc{anew}§
     8469forall( T & | sized(T) | { void ^?{}( T & ); }, List ... } ) void adelete( T arr[], List ... );§\indexc{adelete}§
     8470\end{cfa}
     8471
     8472
     8473\subsection{Memory Set and Copy}
     8474
     8475Like safe memory allocation, \CFA provides safe block initialization and copy.
     8476While objects should be initialized/copied with constructors/assignment, block operations can be very performant.
     8477In certain cases the compiler generates block copy operations, such as assigning structures ©s = t©, however C arrays cannot be assigned.
     8478\begin{cquote}
     8479\begin{cfa}[aboveskip=0pt,belowskip=0pt]
     8480struct S { int i, j, k; };
     8481S s, t, *sp = &s, * tp = &t, sa[10], ta[10];
     8482\end{cfa}
     8483\noindent
     8484\begin{tabular}{@{}l|l@{}}
     8485\multicolumn{1}{@{}c|}{\textbf{\CFA}} & \multicolumn{1}{c@{}}{\textbf{C}} \\
     8486\begin{cfa}[aboveskip=0pt,belowskip=0pt]
     8487memset( s, '\0' );
     8488memset( sp, '\0' );
     8489
     8490memcpy( s, t );
     8491memcpy( sp, tp );
     8492
     8493amemset( sa, '\0', 10 );
     8494amemcpy( sa, ta, 10 );
     8495\end{cfa}
     8496&
     8497\begin{cfa}[aboveskip=0pt,belowskip=0pt]
     8498memset( &s, '\0', sizeof(s) );
     8499memset( sp, '\0', sizeof(s) );
     8500
     8501memcpy( &s, &t, sizeof(s) );
     8502memcpy( sp, tp, sizeof(s) );
     8503
     8504memset( sa, '\0', sizeof(sa) );
     8505memcpy( sa, ta, sizeof(sa) );
     8506\end{cfa}
     8507\end{tabular}
     8508\end{cquote}
     8509These operations provide uniformity between reference and pointer, so object dereferencing, '©&©', is unnecessary.
     8510
     8511\begin{cfa}
     8512static inline forall( T & | sized(T) ) {
     8513        // CFA safe initialization/copy, i.e., implicit size specification, non-array types
     8514        T * memset( T * dest, char fill );§\indexc{memset}§     §\C{// all combinations of pointer/reference}§
     8515        T * memset( T & dest, char fill );
     8516
     8517        T * memcpy( T * dest, const T * src );§\indexc{memcpy}§ §\C{// all combinations of pointer/reference}§
     8518        T * memcpy( T & dest, const T & src );
     8519        T * memcpy( T * dest, const T & src );
     8520        T * memcpy( T & dest, const T * src );
     8521
     8522        // CFA safe initialization/copy, i.e., implicit size specification, array types
     8523        T * amemset( T dest[], char fill, size_t dim );§\indexc{memcpy}§
    84498524        T * amemcpy( T dest[], const T src[], size_t dim );
    84508525}
    8451 
    8452 // §\CFA§ allocation/deallocation and constructor/destructor, non-array types
    8453 forall( T &, TT ... ) void free( T * ptr, ... );
    8454 
    8455 forall( T & | sized(T), Params ... | { void ?{}( T &, Params ); } )
    8456 T * new( Params p );§\indexc{new}§
    8457 forall( T & | { void ^?{}( T & ); } )
    8458 void delete( T * ptr );§\indexc{delete}§
    8459 forall( T &, Params ... | { void ^?{}( T & ); void delete( Params ); } )
    8460 void delete( T * ptr, Params rest );
    8461 
    8462 // §\CFA§ allocation/deallocation and constructor/destructor, array types
    8463 forall( T & | sized(T), Params ... | { void ?{}( T &, Params ); } )
    8464 T * anew( size_t dim, Params p );§\indexc{anew}§
    8465 forall( T & | sized(T) | { void ^?{}( T & ); } )
    8466 void adelete( T arr[] );§\indexc{adelete}§
    8467 forall( T & | sized(T) | { void ^?{}( T & ); }, Params ... | { void adelete( Params ); } )
    8468 void adelete( T arr[], Params rest );
    84698526\end{cfa}
    84708527
Note: See TracChangeset for help on using the changeset viewer.