Changeset bab42de
- Timestamp:
- Apr 23, 2024, 3:55:44 PM (7 months ago)
- Branches:
- master
- Children:
- 03b1815
- Parents:
- 89da3a9 (diff), 5bc81e9 (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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
r89da3a9 rbab42de 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : T hu Apr 18 21:53:45202414 %% Update Count : 6 50213 %% Last Modified On : Tue Apr 23 14:13:10 2024 14 %% Update Count : 6623 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 69 69 \lstset{language=CFA} % CFA default lnaguage 70 70 \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}} 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}2964 2963 As well, \CFA-style declarations cannot be used to declare parameters for C-style routine-definitions because of the following ambiguity: 2965 2964 \begin{cfa} … … 2967 2966 int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§ 2968 2967 \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.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©. 2970 2969 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. 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. }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. 2972 2971 2973 2972 C-style declarations can be used to declare parameters for \CFA style routine definitions, \eg: … … 3061 3060 \subsection{Postfix Function} 3062 3061 \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)©. 3102 3104 3103 3105 \VRef[Figure]{f:UnitsComparison} shows a common usage for postfix functions: converting basic literals into user literals. … … 3124 3126 return l.stones + r.stones; 3125 3127 } 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; }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; } 3132 3134 int main() { 3133 3135 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; 3139 3141 } 3140 3142 \end{cfa} … … 3149 3151 return l.stones + r.stones; 3150 3152 } 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; }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; } 3157 3159 int main() { 3158 3160 Weight w, heavy = { 20 }; // stones 3159 w = 155 _lb;3160 w = 0b1111 _st;3161 w = 0'233 _lb; // quote separator3162 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; 3164 3166 } 3165 3167 \end{C++} … … 3544 3546 3545 3547 \section{Tuple} 3548 \label{s:Tuple} 3546 3549 3547 3550 In C and \CFA, lists of elements appear in several contexts, such as the parameter list of a routine call. … … 4202 4205 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = §\emph{expr}§; 4203 4206 \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. 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. 4206 4208 \LstBasicStyle{\emph{expr}} is any standard arithmetic expression. 4207 4209 Clearly, the types of the entities being assigned must be type compatible with the value of the expression. … … 4241 4243 Multiple assignment has the following form: 4242 4244 \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} 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. 4248 4249 An example of multiple assignment is: 4249 4250 \begin{cfa} … … 8142 8143 \section{Standard Library} 8143 8144 \label{s:StandardLibrary} 8145 \index{standard library} 8144 8146 8145 8147 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. … … 8148 8150 \subsection{Dynamic Storage-Management} 8149 8151 \label{s:DynamicStorageManagement} 8152 \index{dynamic storage-management}\index{storage management} 8150 8153 8151 8154 Dynamic storage-management in C is based on explicit allocation and deallocation (©malloc©/©free©). … … 8210 8213 \begin{enumerate}[leftmargin=\parindent] 8211 8214 \item 8212 Extendtype 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.8215 extends 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. 8213 8216 \begin{cfa} 8214 8217 int * ip = (int *)malloc( sizeof(int) ); §\C[2.3in]{// C}§ 8215 8218 int * ip = malloc(); §\C{// \CFA type-safe call of C malloc}§ 8216 int * ip = alloc(); §\C{// \CFA type-safe call of \CFAalloc}§8217 struct __attribute__(( aligned(128) )) spinlock { ... }; 8219 int * ip = calloc(); §\C{// \CFA type-safe call of C calloc}§ 8220 struct __attribute__(( aligned(128) )) spinlock { ... }; // cache alignment 8218 8221 spinlock * slp = malloc(); §\C{// correct size, alignment, and return type}\CRT§ 8219 8222 \end{cfa} … … 8221 8224 8222 8225 \item 8223 Introducethe notion of \newterm{sticky properties} used in resizing.8226 introduces the notion of \newterm{sticky properties} used in resizing. 8224 8227 All initial allocation properties are remembered and maintained for use should resize require new storage. 8225 8228 For example, the initial alignment and fill properties in the initial allocation … … 8233 8236 8234 8237 \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 thefree/reallocation step altogether.8237 8238 \item 8239 Provide ©free©/©delete© functions that delete a variable number of pointers.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 a free/reallocation step altogether. 8240 8241 \item 8242 provides ©free©/©delete© functions that delete a variable number of allocations. 8240 8243 \begin{cfa} 8241 8244 int * ip = malloc(), * jp = malloc(), * kp = malloc(); 8242 8245 double * 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.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). 8248 8251 \begin{cfa} 8249 8252 struct S { int v; }; §\C{// default constructors}§ … … 8261 8264 Allocation routines ©new©/©anew© allocate a variable/array and initialize storage using the allocated type's constructor. 8262 8265 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++} 8263 8270 \end{enumerate} 8264 8271 … … 8274 8281 \end{enumerate} 8275 8282 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. 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. 8283 8294 8284 8295 The following allocation property functions may be combined and appear in any order as arguments to ©alloc©, … … 8286 8297 \item 8287 8298 ©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 0x55555 5574000 0x555555574000 0x555555574004 0x5555555740088295 \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.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 0x5555565699®00® // 256 alignment 8306 0x55555656c®000® 0x5656c004 0x5656c008 // 4K array alignment 8307 \end{cfa} 8308 8309 \item 8310 ©T_fill(T) ?`fill( /* various types */ )© to initialize storage. 8300 8311 There are three ways to fill storage: 8301 8312 \begin{enumerate} … … 8309 8320 For example: 8310 8321 \begin{cfa}[numbers=left] 8311 int * i0 = alloc( ®0n`fill® ); sout | *i0 | nl; // 0n disambiguates 0 8322 int * i0 = alloc( ®0n`fill® ); sout | *i0 | nl; // 0n disambiguates 0p 8312 8323 int * i1 = alloc( ®5`fill® ); sout | *i1 | nl; 8313 8324 int * i2 = alloc( ®'\xfe'`fill® ); sout | hex( *i2 ) | nl; … … 8316 8327 int * i5 = alloc( 5, ®i3`fill® ); for ( i; 5 ) sout | i5[i] | nonl; sout | nl; // completely fill from i3 8317 8328 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 ); 8318 8330 \end{cfa} 8319 8331 \begin{lstlisting}[numbers=left] … … 8334 8346 For example: 8335 8347 \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; 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 8339 8352 \end{cfa} 8340 8353 \begin{lstlisting}[numbers=left] 8341 0x5555555 6d5c0 58342 0x5555555 70000 78343 0x5555555 71000 13.58354 0x555555580a80 5 8355 0x555555581100 7 8356 0x555555587000 13.5 8344 8357 \end{lstlisting} 8345 8358 Examples 2 to 3 change the alignment, fill, and size for the initial storage of ©i©. 8346 8359 8347 8360 \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; 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 ); 8352 8366 \end{cfa} 8353 8367 \begin{lstlisting}[numbers=left] 8354 5 5 5 5 58355 7 7 7 7 7 7 7 7 7 78356 0x55555 556d560 0x555555571a00 13 13 13 13 138357 0x55555 5572000 0x555555572000 2 0x555555572004 2 0x555555572008 28368 0x55555656d540 5 5 5 5 5 8369 0x55555656d480 7 7 7 7 7 7 7 7 7 7 8370 0x55555656fe00 13 13 13 13 13 8371 0x555556570000 2 0x555556570004 2 0x555556570008 2 8358 8372 \end{lstlisting} 8359 Examples 2 to 4 change the array size, alignment and fill for the initial storage of ©ia©.8373 Examples 2 to 4 change the array size, alignment, and fill initializes all storage because no data is copied. 8360 8374 8361 8375 \item … … 8366 8380 For example: 8367 8381 \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; 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 ); 8371 8386 \end{cfa} 8372 8387 \begin{lstlisting}[numbers=left] … … 8376 8391 \end{lstlisting} 8377 8392 Examples 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.8393 The ©13`fill© in example 3 does nothing because no new storage is added. 8379 8394 8380 8395 \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; 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 ); 8385 8401 \end{cfa} 8386 8402 \begin{lstlisting}[numbers=left] 8387 5 5 5 5 58388 5 5 5 5 57 7 7 7 78389 0x55555 556c560 0x555555570a0058390 0x555555 571000 0x555555571000 5 0x555555571004 2 0x555555571008 28403 0x55555656d540 5 5 5 5 5 8404 0x55555656d480 7 7 7 7 7 7 7 7 7 7 8405 0x555556570e00 5 5 5 5 5 8406 0x5555556571000 5 0x555556571004 5 0x555556571008 5 8391 8407 \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. 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. 8394 8409 \end{itemize} 8395 8410 … … 8397 8412 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 8398 8413 extern "C" { 8399 // New allocation operations.8414 // New C allocation operations. 8400 8415 void * aalloc( size_t dim, size_t elemSize );§\indexc{aalloc}§ 8401 8416 void * resize( void * oaddr, size_t size );§\indexc{resize}§ … … 8406 8421 size_t malloc_size( void * addr );§\indexc{malloc_size}§ 8407 8422 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}§ 8412 8427 } 8413 8428 … … 8423 8438 T * calloc( size_t dim );§\indexc{calloc}§ 8424 8439 T * resize( T * ptr, size_t size );§\indexc{resize}§ 8440 T * resize( T * ptr, size_t alignment, size_t size ); 8425 8441 T * realloc( T * ptr, size_t size );§\indexc{realloc}§ 8442 T * realloc( T * ptr, size_t alignment, size_t size ); 8426 8443 T * reallocarray( T * ptr, size_t dim );§\indexc{reallocarray}§ 8444 T * reallocarray( T * ptr, size_t alignment, size_t dim ); 8427 8445 T * memalign( size_t align );§\indexc{memalign}§ 8428 8446 T * amemalign( size_t align, size_t dim );§\indexc{amemalign}§ … … 8437 8455 T * alloc( size_t dim, ... ); 8438 8456 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 8462 forall( T &, List ... ) void free( T * ptr, ... ) // deallocation list 8463 8464 // §\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}§ 8467 // §\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}§ 8449 8524 T * amemcpy( T dest[], const T src[], size_t dim ); 8450 8525 } 8451 8452 // §\CFA§ allocation/deallocation and constructor/destructor, non-array types8453 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 types8463 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 );8469 8526 \end{cfa} 8470 8527
Note: See TracChangeset
for help on using the changeset viewer.