Changes in / [5bc81e9:4a3eb1c]
- File:
-
- 1 edited
-
doc/user/user.tex (modified) (29 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
r5bc81e9 r4a3eb1c 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : T ue Apr 23 14:13:10202414 %% Update Count : 6 62313 %% Last Modified On : Thu Apr 18 21:53:45 2024 14 %% Update Count : 6502 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 69 69 \lstset{language=CFA} % CFA default lnaguage 70 70 \lstnewenvironment{C++}[1][] % use C++ style 71 {\lstset{language=C++, escapechar=§,moredelim=**[is][\protect\color{red}]{®}{®},#1}}71 {\lstset{language=C++,moredelim=**[is][\protect\color{red}]{®}{®},#1}} 72 72 {} 73 73 … … 2961 2961 Since the strings overlap starting with the open bracket, ©[©, there is an ambiguous interpretation for the string. 2962 2962 2963 {\color{red} 2963 2964 As well, \CFA-style declarations cannot be used to declare parameters for C-style routine-definitions because of the following ambiguity: 2964 2965 \begin{cfa} … … 2966 2967 int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§ 2967 2968 \end{cfa} 2968 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©.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. 2969 2970 The 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. 2970 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. 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.} 2971 2972 2972 2973 C-style declarations can be used to declare parameters for \CFA style routine definitions, \eg: … … 3060 3061 \subsection{Postfix Function} 3061 3062 \label{s:PostfixFunction} 3062 \index{postfix function}3063 3063 3064 3064 \CFA provides an alternative call syntax where the argument appears before the function name. … … 3081 3081 '1'`m; 3082 3082 "123" "456"`m; 3083 [1, 2,3]`t;3083 [1,2,3]`t; 3084 3084 \end{cfa} 3085 3085 & … … 3100 3100 \end{tabular} 3101 3101 \end{cquote} 3102 Note, 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}}.3103 Similarly, 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)©.3104 3102 3105 3103 \VRef[Figure]{f:UnitsComparison} shows a common usage for postfix functions: converting basic literals into user literals. … … 3126 3124 return l.stones + r.stones; 3127 3125 } 3128 Weight ®?`st®( double w ) { return w; }3129 double ®?`st®( Weight w ) { return w.stones; }3130 Weight ®?`lb®( double w ) { return w / 14.0; }3131 double ®?`lb®( Weight w ) { return w.stones * 14.0; }3132 Weight ®?`kg®( double w ) { return w / 6.35; }3133 double ®?`kg®( Weight w ) { return w.stones * 6.35; }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; } 3134 3132 int main() { 3135 3133 Weight w, heavy = { 20 }; // stones 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;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; 3141 3139 } 3142 3140 \end{cfa} … … 3151 3149 return l.stones + r.stones; 3152 3150 } 3153 Weight operator ®""_st®( long double w ) { return w; }3154 Weight operator ®""_lb®( long double w ) { return w / 14.0; }3155 Weight operator ®""_kg®( long double w ) { return w / 6.35; }3156 Weight operator ®""_st®( unsigned long long int w ) { return w; }3157 Weight operator ®""_lb®( unsigned long long int w ) { return w / 14.0; }3158 Weight operator ®""_kg®( unsigned long long int w ) { return w / 6.35; }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; } 3159 3157 int main() { 3160 3158 Weight w, heavy = { 20 }; // stones 3161 w = 155 ®_lb®;3162 w = 0b1111 ®_st®;3163 w = 0'233 ®_lb®; // quote separator3164 w = 0x9b ®_kg®;3165 w = 5.5 ®_st® + 8®_kg® + 25.01®_lb®+ heavy;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; 3166 3164 } 3167 3165 \end{C++} … … 3546 3544 3547 3545 \section{Tuple} 3548 \label{s:Tuple}3549 3546 3550 3547 In C and \CFA, lists of elements appear in several contexts, such as the parameter list of a routine call. … … 4205 4202 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = §\emph{expr}§; 4206 4203 \end{cfa} 4207 The 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. 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. 4208 4206 \LstBasicStyle{\emph{expr}} is any standard arithmetic expression. 4209 4207 Clearly, the types of the entities being assigned must be type compatible with the value of the expression. … … 4243 4241 Multiple assignment has the following form: 4244 4242 \begin{cfa} 4245 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = [ §\emph{expr}§, ... , §\emph{expr}§ ];§ 4246 \end{cfa} 4247 The 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. 4248 Each \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. 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. 4249 4248 An example of multiple assignment is: 4250 4249 \begin{cfa} … … 8143 8142 \section{Standard Library} 8144 8143 \label{s:StandardLibrary} 8145 \index{standard library}8146 8144 8147 8145 The \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. … … 8150 8148 \subsection{Dynamic Storage-Management} 8151 8149 \label{s:DynamicStorageManagement} 8152 \index{dynamic storage-management}\index{storage management}8153 8150 8154 8151 Dynamic storage-management in C is based on explicit allocation and deallocation (©malloc©/©free©). … … 8213 8210 \begin{enumerate}[leftmargin=\parindent] 8214 8211 \item 8215 extendstype 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.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. 8216 8213 \begin{cfa} 8217 8214 int * ip = (int *)malloc( sizeof(int) ); §\C[2.3in]{// C}§ 8218 8215 int * ip = malloc(); §\C{// \CFA type-safe call of C malloc}§ 8219 int * ip = calloc(); §\C{// \CFA type-safe call of C calloc}§8220 struct __attribute__(( aligned(128) )) spinlock { ... }; // cache alignment8216 int * ip = alloc(); §\C{// \CFA type-safe call of \CFA alloc}§ 8217 struct __attribute__(( aligned(128) )) spinlock { ... }; 8221 8218 spinlock * slp = malloc(); §\C{// correct size, alignment, and return type}\CRT§ 8222 8219 \end{cfa} … … 8224 8221 8225 8222 \item 8226 introducesthe notion of \newterm{sticky properties} used in resizing.8223 Introduce the notion of \newterm{sticky properties} used in resizing. 8227 8224 All initial allocation properties are remembered and maintained for use should resize require new storage. 8228 8225 For example, the initial alignment and fill properties in the initial allocation … … 8236 8233 8237 8234 \item 8238 provides 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.8239 A resize can take advantage of unused storage after the data to preventing afree/reallocation step altogether.8240 8241 \item 8242 provides ©free©/©delete© functions that delete a variable number of allocations.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. 8243 8240 \begin{cfa} 8244 8241 int * ip = malloc(), * jp = malloc(), * kp = malloc(); 8245 8242 double * xp = malloc(), * yp = malloc(), * zp = malloc(); 8246 free( ®ip, jp, kp, xp, yp, zp® ); §\C{// multiple deallocations}§8247 \end{cfa} 8248 8249 \item 8250 supports constructors for initialization of allocated storage and destructors for deallocation (like \CC).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. 8251 8248 \begin{cfa} 8252 8249 struct S { int v; }; §\C{// default constructors}§ … … 8264 8261 Allocation routines ©new©/©anew© allocate a variable/array and initialize storage using the allocated type's constructor. 8265 8262 Note, the matching deallocation routines ©delete©/©adelete©. 8266 \CC only supports the default constructor for intializing array elements.8267 \begin{C++}8268 S * sp = new S[10]®{5}®; §\C{// disallowed}§8269 \end{C++}8270 8263 \end{enumerate} 8271 8264 … … 8281 8274 \end{enumerate} 8282 8275 8283 The polymorphic functions 8284 \begin{cfa} 8285 T * alloc( ... ); 8286 T * alloc( size_t dim, ... ); 8287 \end{cfa} 8288 are overloaded with a variable number of allocation properties. 8289 These allocation properties can be passed as named arguments when calling the \Indexc{alloc} routine. 8290 A call without parameters returns an uninitialized dynamically allocated object of type ©T© (\Indexc{malloc}). 8291 A call with only the dimension (dim) parameter returns an uninitialized dynamically allocated array of objects with type ©T© (\Indexc{aalloc}). 8292 The variable number of arguments consist of allocation properties to specialize the allocation. 8293 The properties ©resize© and ©realloc© are associated with an existing allocation variable indicating how its storage is modified. 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. 8294 8283 8295 8284 The following allocation property functions may be combined and appear in any order as arguments to ©alloc©, … … 8297 8286 \item 8298 8287 ©T_align ?`align( size_t alignment )© to align an allocation. 8299 The 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} 8301 int * i0 = alloc( ® 256`align® ); sout | i0 | nl;8302 int * i1 = alloc( 3, ®4096`align® ); for (i; 3 ) sout | &i1[i] | nonl;sout | nl;8303 free( i0, i1 ); 8304 8305 0x55555 65699®00® // 256 alignment8306 0x55555656c®000® 0x5656c004 0x5656c008 // 4K array alignment 8307 \end{cfa} 8308 8309 \item 8310 © T_fill(T) ?`fill( /* various types */ )© to initialize storage.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. 8311 8300 There are three ways to fill storage: 8312 8301 \begin{enumerate} … … 8320 8309 For example: 8321 8310 \begin{cfa}[numbers=left] 8322 int * i0 = alloc( ®0n`fill® ); sout | *i0 | nl; // 0n disambiguates 0 p8311 int * i0 = alloc( ®0n`fill® ); sout | *i0 | nl; // 0n disambiguates 0 8323 8312 int * i1 = alloc( ®5`fill® ); sout | *i1 | nl; 8324 8313 int * i2 = alloc( ®'\xfe'`fill® ); sout | hex( *i2 ) | nl; … … 8327 8316 int * i5 = alloc( 5, ®i3`fill® ); for ( i; 5 ) sout | i5[i] | nonl; sout | nl; // completely fill from i3 8328 8317 int * i6 = alloc( 5, ®[i3, 3]`fill® ); for ( i; 5 ) sout | i6[i] | nonl; sout | nl; // partial fill from i3 8329 free( i0, i1, i2, i3, i4, i5, i6 );8330 8318 \end{cfa} 8331 8319 \begin{lstlisting}[numbers=left] … … 8346 8334 For example: 8347 8335 \begin{cfa}[numbers=left] 8348 int * ip = alloc( ®5`fill® ); sout | ip | *ip; 8349 ip = alloc( ®ip`resize®, ®256`align®, ®7`fill® ); sout | ip | *ip; 8350 double * dp = alloc( ®ip`resize®, ®4096`align®, ®13.5`fill® ); sout | dp | *dp; 8351 free( dp ); // DO NOT FREE ip AS ITS STORAGE IS MOVED TO dp 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; 8352 8339 \end{cfa} 8353 8340 \begin{lstlisting}[numbers=left] 8354 0x5555555 80a80 58355 0x5555555 81100 78356 0x5555555 87000 13.58341 0x55555556d5c0 5 8342 0x555555570000 7 8343 0x555555571000 13.5 8357 8344 \end{lstlisting} 8358 8345 Examples 2 to 3 change the alignment, fill, and size for the initial storage of ©i©. 8359 8346 8360 8347 \begin{cfa}[numbers=left] 8361 int * ia = alloc( 5, ®5`fill® ); sout | ia | nonl; for ( i; 5 ) sout | ia[i] | nonl; sout | nl; 8362 ia = alloc( 10, ®ia`resize®, ®7`fill® ); sout | ia | nonl; for ( i; 10 ) sout | ia[i] | nonl; sout | nl; 8363 ia = alloc( 5, ®ia`resize®, ®512`align®, ®13`fill® ); sout | ia | nonl; for ( i; 5 ) sout | ia[i] | nonl; sout | nl;; 8364 ia = alloc( 3, ®ia`resize®, ®4096`align®, ®2`fill® ); for ( i; 3 ) sout | &ia[i] | ia[i] | nonl; sout | nl; 8365 free( ia ); 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; 8366 8352 \end{cfa} 8367 8353 \begin{lstlisting}[numbers=left] 8368 0x55555656d5405 5 5 5 58369 0x55555656d4807 7 7 7 7 7 7 7 7 78370 0x55555 656fe00 13 13 13 13 138371 0x55555 6570000 2 0x555556570004 2 0x555556570008 28354 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 8372 8358 \end{lstlisting} 8373 Examples 2 to 4 change the array size, alignment , and fill initializes all storage because no data is copied.8359 Examples 2 to 4 change the array size, alignment and fill for the initial storage of ©ia©. 8374 8360 8375 8361 \item … … 8380 8366 For example: 8381 8367 \begin{cfa}[numbers=left] 8382 int * ip = alloc( ®5`fill® ); sout | ip | *ip; 8383 ip = alloc( ®ip`realloc®, ®256`align® ); sout | ip | *ip; 8384 ip = alloc( ®ip`realloc®, ®4096`align®, ®13`fill® ); sout | ip | *ip; 8385 free( ip ); 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; 8386 8371 \end{cfa} 8387 8372 \begin{lstlisting}[numbers=left] … … 8391 8376 \end{lstlisting} 8392 8377 Examples 2 to 3 change the alignment for the initial storage of ©i©. 8393 The ©13`fill© in example 3 does nothing because no new storage is added.8378 The ©13`fill© in example 3 does nothing because no extra space is added. 8394 8379 8395 8380 \begin{cfa}[numbers=left] 8396 int * ia = alloc( 5, ®5`fill® ); sout | ia | nonl; for ( i; 5 ) sout | ia[i] | nonl; sout | nl; 8397 ia = alloc( 10, ®ia`realloc®, ®7`fill® ); sout | ia | nonl; for ( i; 10 ) sout | ia[i] | nonl; sout | nl; 8398 ia = alloc( 5, ®ia`realloc®, ®512`align®, ®13`fill® ); sout | ia | nonl; for ( i; 5 ) sout | ia[i] | nonl; sout | nl;; 8399 ia = alloc( 3, ®ia`realloc®, ®4096`align®, ®2`fill® ); for ( i; 3 ) sout | &ia[i] | ia[i] | nonl; sout | nl; 8400 free( ia ); 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; 8401 8385 \end{cfa} 8402 8386 \begin{lstlisting}[numbers=left] 8403 0x55555656d5405 5 5 5 58404 0x55555656d480 7 7 7 7 77 7 7 7 78405 0x55555 6570e00 5 5 5 558406 0x555555 6571000 5 0x555556571004 5 0x555556571008 58387 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 8407 8391 \end{lstlisting} 8408 Examples 2 to 4 change the array size, alignment, and fill does no initialization after the copied data, as no new storage is added. 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. 8409 8394 \end{itemize} 8410 8395 … … 8412 8397 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 8413 8398 extern "C" { 8414 // New Callocation operations.8399 // New allocation operations. 8415 8400 void * aalloc( size_t dim, size_t elemSize );§\indexc{aalloc}§ 8416 8401 void * resize( void * oaddr, size_t size );§\indexc{resize}§ … … 8421 8406 size_t malloc_size( void * addr );§\indexc{malloc_size}§ 8422 8407 int malloc_stats_fd( int fd );§\indexc{malloc_stats_fd}§ 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}§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}$ 8427 8412 } 8428 8413 … … 8438 8423 T * calloc( size_t dim );§\indexc{calloc}§ 8439 8424 T * resize( T * ptr, size_t size );§\indexc{resize}§ 8440 T * resize( T * ptr, size_t alignment, size_t size );8441 8425 T * realloc( T * ptr, size_t size );§\indexc{realloc}§ 8442 T * realloc( T * ptr, size_t alignment, size_t size );8443 8426 T * reallocarray( T * ptr, size_t dim );§\indexc{reallocarray}§ 8444 T * reallocarray( T * ptr, size_t alignment, size_t dim );8445 8427 T * memalign( size_t align );§\indexc{memalign}§ 8446 8428 T * amemalign( size_t align, size_t dim );§\indexc{amemalign}§ … … 8455 8437 T * alloc( size_t dim, ... ); 8456 8438 T_align ?`align( size_t alignment );§\indexc{align}§ 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 8462 forall( T &, List ... ) void free( T * ptr, ... ) // deallocation list 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 ); 8449 T * amemcpy( T dest[], const T src[], size_t dim ); 8450 } 8463 8451 8464 8452 // §\CFA§ allocation/deallocation and constructor/destructor, non-array types 8465 forall( T &, Parms ... | { void ?{}( T &, Parms ); } ) T * new( Parms ... );§\indexc{new}§ 8466 forall( T &, List ... | { void ^?{}( T & ); void delete( List ... ); } );§\indexc{delete}§ 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 8467 8462 // §\CFA§ allocation/deallocation and constructor/destructor, array types 8468 forall( T & | sized(T), Parms ... | { void ?{}( T &, Parms ); } ) T * anew( size_t dim, Parms ... );§\indexc{anew}§ 8469 forall( 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 8475 Like safe memory allocation, \CFA provides safe block initialization and copy. 8476 While objects should be initialized/copied with constructors/assignment, block operations can be very performant. 8477 In 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] 8480 struct S { int i, j, k; }; 8481 S 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] 8487 memset( s, '\0' ); 8488 memset( sp, '\0' ); 8489 8490 memcpy( s, t ); 8491 memcpy( sp, tp ); 8492 8493 amemset( sa, '\0', 10 ); 8494 amemcpy( sa, ta, 10 ); 8495 \end{cfa} 8496 & 8497 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 8498 memset( &s, '\0', sizeof(s) ); 8499 memset( sp, '\0', sizeof(s) ); 8500 8501 memcpy( &s, &t, sizeof(s) ); 8502 memcpy( sp, tp, sizeof(s) ); 8503 8504 memset( sa, '\0', sizeof(sa) ); 8505 memcpy( sa, ta, sizeof(sa) ); 8506 \end{cfa} 8507 \end{tabular} 8508 \end{cquote} 8509 These operations provide uniformity between reference and pointer, so object dereferencing, '©&©', is unnecessary. 8510 8511 \begin{cfa} 8512 static 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}§ 8524 T * amemcpy( T dest[], const T src[], size_t dim ); 8525 } 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 ); 8526 8469 \end{cfa} 8527 8470
Note:
See TracChangeset
for help on using the changeset viewer.