Changes in doc/user/user.tex [128c4e4:c6dc7f2]
- File:
-
- 1 edited
-
doc/user/user.tex (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
r128c4e4 rc6dc7f2 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Mon Oct 5 08:57:29 202014 %% Update Count : 3 99813 %% Last Modified On : Sat Jul 13 18:36:18 2019 14 %% Update Count : 3876 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 30 30 \usepackage{upquote} % switch curled `'" to straight 31 31 \usepackage{calc} 32 \usepackage{xspace} 32 33 \usepackage{varioref} % extended references 33 \usepackage[labelformat=simple,aboveskip=0pt,farskip=0pt]{subfig} 34 \renewcommand{\thesubfigure}{\alph{subfigure})} 34 \usepackage{listings} % format program code 35 35 \usepackage[flushmargin]{footmisc} % support label/reference in footnote 36 36 \usepackage{latexsym} % \Box glyph 37 37 \usepackage{mathptmx} % better math font with "times" 38 38 \usepackage[usenames]{color} 39 \newcommand{\CFALatin}{} 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 40 63 % inline code ©...© (copyright symbol) emacs: C-q M-) 41 64 % red highlighting ®...® (registered trademark symbol) emacs: C-q M-. … … 45 68 % keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^ 46 69 % math escape $...$ (dollar symbol) 47 \input{common} % common CFA document macros48 \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 underscore59 % removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR60 % AFTER HYPERREF.61 \renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}62 63 \setlength{\topmargin}{-0.45in} % move running title into header64 \setlength{\headsep}{0.25in}65 66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%67 68 \CFAStyle % use default CFA format-style69 \lstnewenvironment{C++}[1][] % use C++ style70 {\lstset{language=C++,moredelim=**[is][\protect\color{red}]{®}{®},#1}}71 {}72 73 \newsavebox{\myboxA}74 \newsavebox{\myboxB}75 70 76 71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% … … 84 79 \newcommand{\G}[1]{{\Textbf[OliveGreen]{#1}}} 85 80 \newcommand{\KWC}{K-W C\xspace} 81 82 \newsavebox{\LstBox} 86 83 87 84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% … … 214 211 Even with all its problems, C continues to be popular because it allows writing software at virtually any level in a computer system without restriction. 215 212 For system programming, where direct access to hardware, storage management, and real-time issues are a requirement, C is usually the only language of choice. 216 The 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.217 The top 4 rankings over the past 35years are: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: 218 215 \begin{center} 219 216 \setlength{\tabcolsep}{10pt} 220 \begin{tabular}{@{}rcccccccc@{}} 221 & 2020 & 2015 & 2010 & 2005 & 2000 & 1995 & 1990 & 1985 \\ \hline 222 Java & 1 & 2 & 1 & 2 & 3 & - & - & - \\ 223 \R{C} & \R{2} & \R{1} & \R{2} & \R{1} & \R{1} & \R{2} & \R{1} & \R{1} \\ 224 Python & 3 & 7 & 6 & 6 & 22 & 21 & - & - \\ 225 \CC & 4 & 4 & 4 & 3 & 2 & 1 & 2 & 12 \\ 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 \\ 226 222 \end{tabular} 227 223 \end{center} … … 256 252 257 253 The 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): 258 \begin{ cfa}254 \begin{lstlisting} 259 255 ®forall( otype T )® T identity( T val ) { return val; } 260 256 int forty_two = identity( 42 ); §\C{// T is bound to int, forty\_two == 42}§ 261 \end{ cfa}257 \end{lstlisting} 262 258 % extending the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC{}} approach of object-oriented extensions. 263 259 \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}. … … 278 274 \begin{comment} 279 275 A simple example is leveraging the existing type-unsafe (©void *©) C ©bsearch© to binary search a sorted floating array: 280 \begin{ cfa}276 \begin{lstlisting} 281 277 void * bsearch( const void * key, const void * base, size_t dim, size_t size, 282 278 int (* compar)( const void *, const void * )); … … 287 283 double key = 5.0, vals[10] = { /* 10 sorted floating values */ }; 288 284 double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp ); §\C{// search sorted array}§ 289 \end{ cfa}285 \end{lstlisting} 290 286 which can be augmented simply with a polymorphic, type-safe, \CFA-overloaded wrappers: 291 \begin{ cfa}287 \begin{lstlisting} 292 288 forall( otype T | { int ?<?( T, T ); } ) T * bsearch( T key, const T * arr, size_t size ) { 293 289 int comp( const void * t1, const void * t2 ) { /* as above with double changed to T */ } … … 300 296 double * val = bsearch( 5.0, vals, 10 ); §\C{// selection based on return type}§ 301 297 int posn = bsearch( 5.0, vals, 10 ); 302 \end{ cfa}298 \end{lstlisting} 303 299 The nested function ©comp© provides the hidden interface from typed \CFA to untyped (©void *©) C, plus the cast of the result. 304 300 Providing a hidden ©comp© function in \CC is awkward as lambdas do not use C calling-conventions and template declarations cannot appear at block scope. … … 308 304 \CFA has replacement libraries condensing hundreds of existing C functions into tens of \CFA overloaded functions, all without rewriting the actual computations. 309 305 For example, it is possible to write a type-safe \CFA wrapper ©malloc© based on the C ©malloc©: 310 \begin{ cfa}306 \begin{lstlisting} 311 307 forall( dtype T | sized(T) ) T * malloc( void ) { return (T *)malloc( sizeof(T) ); } 312 308 int * ip = malloc(); §\C{// select type and size from left-hand side}§ 313 309 double * dp = malloc(); 314 310 struct S {...} * sp = malloc(); 315 \end{ cfa}311 \end{lstlisting} 316 312 where the return type supplies the type/size of the allocation, which is impossible in most type systems. 317 313 \end{comment} … … 516 512 Keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism: 517 513 \begin{cfa} 518 int ®` `®otype= 3; §\C{// make keyword an identifier}§519 double ®` `®forall= 3.5;514 int ®`®otype®`® = 3; §\C{// make keyword an identifier}§ 515 double ®`®forall®`® = 3.5; 520 516 \end{cfa} 521 517 … … 528 524 // include file uses the CFA keyword "with". 529 525 #if ! defined( with ) §\C{// nesting ?}§ 530 #define with ®` `®with§\C{// make keyword an identifier}§526 #define with ®`®with®`® §\C{// make keyword an identifier}§ 531 527 #define __CFA_BFD_H__ 532 528 #endif 533 §{\color{red}\#\textbf{include\_next} <bfdlink.h>}§ §\C{// must have internal check for multiple expansion}§ 529 530 ®#include_next <bfdlink.h> §\C{// must have internal check for multiple expansion}§ 531 ® 534 532 #if defined( with ) && defined( __CFA_BFD_H__ ) §\C{// reset only if set}§ 535 533 #undef with … … 578 576 \section{Exponentiation Operator} 579 577 580 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 (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$.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$. 582 580 The 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)©. 583 581 584 There are exponentiation operators for integral and floating types, including the builtin \Index{complex} types.582 As for \Index{division}, there are exponentiation operators for integral and floating types, including the builtin \Index{complex} types. 585 583 Integral 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). 586 Overflow f or a large exponent or negative exponent returnszero.584 Overflow from large exponents or negative exponents return zero. 587 585 Floating exponentiation\index{exponentiation!floating} is performed using \Index{logarithm}s\index{exponentiation!logarithm}, so the exponent cannot be negative. 588 586 \begin{cfa} … … 591 589 1 1 256 -64 125 ®0® 3273344365508751233 ®0® ®0® -0.015625 18.3791736799526 0.264715-1.1922i 592 590 \end{cfa} 593 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.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. 594 592 Parenthesis are necessary for complex constants or the expression is parsed as ©1.0f+®(®2.0fi \ 3.0f®)®+2.0fi©. 595 593 The exponentiation operator is available for all the basic types, but for user-defined types, only the integral-computation version is available. … … 600 598 OT ?®\®?( OT ep, unsigned long int y ); 601 599 \end{cfa} 602 The user type ©T© must define multiplication, one (©1©), and©*©.600 The user type ©T© must define multiplication, one, ©1©, and, ©*©. 603 601 604 602 … … 628 626 629 627 630 %\section{\texorpdfstring{\protect\lstinline@case@ Clause}{case Clause}} 631 \subsection{\texorpdfstring{\LstKeywordStyle{case} Clause}{case Clause}} 632 633 C restricts the ©case© clause of a ©switch© statement to a single value. 634 For multiple ©case© clauses associated with the same statement, it is necessary to have multiple ©case© clauses rather than multiple values. 635 Requiring a ©case© clause for each value does not seem to be in the spirit of brevity normally associated with C. 636 Therefore, the ©case© clause is extended with a list of values, as in: 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} 637 657 \begin{cquote} 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} 641 switch ( i ) { 642 case ®1, 3, 5®: 643 ... 644 case ®2, 4, 6®: 645 ... 646 } 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; 647 712 \end{cfa} 648 713 & 649 714 \begin{cfa} 650 switch ( 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 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 665 765 \end{cfa} 666 766 \end{tabular} 667 767 \end{cquote} 668 In addition, subranges are allowed to specify case values.\footnote{ 669 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.} 670 \begin{cfa} 671 switch ( 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} 678 Lists of subranges are also allowed. 679 \begin{cfa} 680 case ®1~5, 12~21, 35~42®: 681 \end{cfa} 768 \caption{Loop Control Examples} 769 \label{f:LoopControlExamples} 770 \end{figure} 682 771 683 772 … … 888 977 889 978 890 \subsection{Non-terminating and Labelled \texorpdfstring{\LstKeywordStyle{fallthrough}}{Non-terminating and Labelled fallthrough}} 891 892 The ©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} 896 choose ( ... ) { 897 case 3: 898 if ( ... ) { 899 ... ®fallthru;® // goto case 4 900 } else { 901 ... 902 } 903 // implicit break 904 case 4: 905 906 907 908 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®: 992 ... 993 case ®2, 4, 6®: 994 ... 995 } 909 996 \end{cfa} 910 997 & 911 998 \begin{cfa} 912 choose ( ... ) { 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 999 switch ( i ) { 1000 case 1: case 3 : case 5: 1001 ... 1002 case 2: case 4 : case 6: 1003 ... 1004 } 925 1005 \end{cfa} 926 1006 & 927 1007 \begin{cfa} 928 choose ( ... ) { 929 case 3: 930 choose ( ... ) { 931 case 4: 932 for ( ... ) { 933 // multi-level transfer 934 ... ®fallthru common;® 935 } 936 ... 937 } 1008 1009 // odd values 1010 1011 // even values 1012 1013 1014 \end{cfa} 1015 \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}§ 938 1022 ... 939 ®common:® // below fallthrough 940 // at case-clause level 941 \end{cfa} 942 \end{tabular} 943 \end{center} 944 The target label must be below the ©fallthrough© and may not be nested in a control structure, and 945 the target label must be at the same or higher level as the containing ©case© clause and located at 946 the same level as a ©case© clause; the target label may be case ©default©, but only associated 947 with 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] 954 while ®()® { sout | "empty"; break; } 955 do { sout | "empty"; break; } while ®()®; 956 for ®()® { sout | "empty"; break; } 957 for ( ®0® ) { sout | "A"; } sout | "zero"; 958 for ( ®1® ) { sout | "A"; } 959 for ( ®10® ) { sout | "A"; } 960 for ( ®= 10® ) { sout | "A"; } 961 for ( ®1 ~= 10 ~ 2® ) { sout | "B"; } 962 for ( ®10 -~= 1 ~ 2® ) { sout | "C"; } 963 for ( ®0.5 ~ 5.5® ) { sout | "D"; } 964 for ( ®5.5 -~ 0.5® ) { sout | "E"; } 965 for ( ®i; 10® ) { sout | i; } 966 for ( ®i; = 10® ) { sout | i; } 967 for ( ®i; 1 ~= 10 ~ 2® ) { sout | i; } 968 for ( ®i; 10 -~= 1 ~ 2® ) { sout | i; } 969 for ( ®i; 0.5 ~ 5.5® ) { sout | i; } 970 for ( ®i; 5.5 -~ 0.5® ) { sout | i; } 971 for ( ®ui; 2u ~= 10u ~ 2u® ) { sout | ui; } 972 for ( ®ui; 10u -~= 2u ~ 2u® ) { sout | ui; } 973 enum { N = 10 }; 974 for ( ®N® ) { sout | "N"; } 975 for ( ®i; N® ) { sout | i; } 976 for ( ®i; N -~ 0® ) { sout | i; } 977 const int start = 3, comp = 10, inc = 2; 978 for ( ®i; start ~ comp ~ inc + 1® ) { sout | i; } 979 for ( i; 1 ~ ®@® ) { if ( i > 10 ) break; sout | i; } 980 for ( i; 10 -~ ®@® ) { if ( i < 0 ) break; sout | i; } 981 for ( i; 2 ~ ®@® ~ 2 ) { if ( i > 10 ) break; sout | i; } 982 for ( i; 2.1 ~ ®@® ~ ®@® ) { if ( i > 10.5 ) break; sout | i; i += 1.7; } 983 for ( i; 10 -~ ®@® ~ 2 ) { if ( i < 0 ) break; sout | i; } 984 for ( i; 12.1 ~ ®@® ~ ®@® ) { if ( i < 2.5 ) break; sout | i; i -= 1.7; } 985 for ( i; 5 ®:® j; -5 ~ @ ) { sout | i | j; } 986 for ( i; 5 ®:® j; -5 -~ @ ) { sout | i | j; } 987 for ( i; 5 ®:® j; -5 ~ @ ~ 2 ) { sout | i | j; } 988 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ) { sout | i | j; } 989 for ( i; 5 ®:® j; -5 ~ @ ) { sout | i | j; } 990 for ( i; 5 ®:® j; -5 -~ @ ) { sout | i | j; } 991 for ( i; 5 ®:® j; -5 ~ @ ~ 2 ) { sout | i | j; } 992 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ) { sout | i | j; } 993 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ®:® k; 1.5 ~ @ ) { sout | i | j | k; } 994 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ®:® k; 1.5 ~ @ ) { sout | i | j | k; } 995 for ( i; 5 ®:® k; 1.5 ~ @ ®:® j; -5 -~ @ ~ 2 ) { sout | i | j | k; } 996 \end{cfa} 997 & 998 \begin{cfa} 999 empty 1000 empty 1001 empty 1002 zero 1003 A 1004 A A A A A A A A A A 1005 A A A A A A A A A A A 1006 B B B B B 1007 C C C C C 1008 D D D D D 1009 E E E E E 1010 0 1 2 3 4 5 6 7 8 9 1011 0 1 2 3 4 5 6 7 8 9 10 1012 1 3 5 7 9 1013 10 8 6 4 2 1014 0.5 1.5 2.5 3.5 4.5 1015 5.5 4.5 3.5 2.5 1.5 1016 2 4 6 8 10 1017 10 8 6 4 2 1018 1019 N N N N N N N N N N 1020 0 1 2 3 4 5 6 7 8 9 1021 10 9 8 7 6 5 4 3 2 1 1022 1023 3 6 9 1024 1 2 3 4 5 6 7 8 9 10 1025 10 9 8 7 6 5 4 3 2 1 0 1026 2 4 6 8 10 1027 2.1 3.8 5.5 7.2 8.9 1028 10 8 6 4 2 0 1029 12.1 10.4 8.7 7. 5.3 3.6 1030 0 -5 1 -4 2 -3 3 -2 4 -1 1031 0 -5 1 -6 2 -7 3 -8 4 -9 1032 0 -5 1 -3 2 -1 3 1 4 3 1033 0 -5 1 -7 2 -9 3 -11 4 -13 1034 0 -5 1 -4 2 -3 3 -2 4 -1 1035 0 -5 1 -6 2 -7 3 -8 4 -9 1036 0 -5 1 -3 2 -1 3 1 4 3 1037 0 -5 1 -7 2 -9 3 -11 4 -13 1038 0 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5 1039 0 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5 1040 0 -5 1.5 1 -7 2.5 2 -9 3.5 3 -11 4.5 4 -13 5.5 1041 \end{cfa} 1042 \end{tabular} 1043 \caption{Loop Control Examples} 1044 \label{f:LoopControlExamples} 1045 \end{figure} 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 1046 1032 1047 1033 % for () => for ( ;; ) … … 1054 1040 1055 1041 1056 \subsection{Loop Control}1057 1058 The ©for©/©while©/©do-while© loop-control allows empty or simplified ranges (see Figure~\ref{f:LoopControlExamples}).1059 \begin{itemize}1060 \item1061 The loop index is polymorphic in the type of the comparison value N (when the start value is implicit) or the start value M.1062 \item1063 An empty conditional implies comparison value of ©1© (true).1064 \item1065 A comparison N is implicit up-to exclusive range [0,N©®)®©.1066 \item1067 A comparison ©=© N is implicit up-to inclusive range [0,N©®]®©.1068 \item1069 The up-to range M ©~©\index{~@©~©} N means exclusive range [M,N©®)®©.1070 \item1071 The up-to range M ©~=©\index{~=@©~=©} N means inclusive range [M,N©®]®©.1072 \item1073 The down-to range M ©-~©\index{-~@©-~©} N means exclusive range [N,M©®)®©.1074 \item1075 The down-to range M ©-~=©\index{-~=@©-~=©} N means inclusive range [N,M©®]®©.1076 \item1077 ©0© is the implicit start value;1078 \item1079 ©1© is the implicit increment value.1080 \item1081 The up-to range uses operator ©+=© for increment;1082 \item1083 The down-to range uses operator ©-=© for decrement.1084 \item1085 ©@© means put nothing in this field.1086 \item1087 ©:© means start another index.1088 \end{itemize}1089 1090 1091 1042 %\subsection{\texorpdfstring{Labelled \protect\lstinline@continue@ / \protect\lstinline@break@}{Labelled continue / break}} 1092 1043 \subsection{\texorpdfstring{Labelled \LstKeywordStyle{continue} / \LstKeywordStyle{break} Statement}{Labelled continue / break Statement}} … … 1098 1049 for ©break©, the target label can also be associated with a ©switch©, ©if© or compound (©{}©) statement. 1099 1050 \VRef[Figure]{f:MultiLevelExit} shows ©continue© and ©break© indicating the specific control structure, and the corresponding C program using only ©goto© and labels. 1100 The innermost loop has 8exit points, which cause continuation or termination of one or more of the 7 \Index{nested control-structure}s.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. 1101 1052 1102 1053 \begin{figure} 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 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 1130 1077 } // compound 1131 1078 \end{cfa} 1132 \end{lrbox} 1133 1134 \begin{lrbox}{\myboxB} 1135 \begin{cfa}[tabsize=3] 1079 & 1080 \begin{cfa} 1136 1081 { 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} 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} 1169 1128 \caption{Multi-level Exit} 1170 1129 \label{f:MultiLevelExit} … … 1421 1380 try { 1422 1381 f(...); 1423 } catch( E e ; §boolean-predicate§ ) { §\C {// termination handler}§1382 } catch( E e ; §boolean-predicate§ ) { §\C[8cm]{// termination handler}§ 1424 1383 // recover and continue 1425 } catchResume( E e ; §boolean-predicate§ ) { §\C{// resumption handler} §1384 } catchResume( E e ; §boolean-predicate§ ) { §\C{// resumption handler}\CRT§ 1426 1385 // repair and return 1427 1386 } finally { … … 3486 3445 For 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. 3487 3446 \begin{cquote} 3488 \begin{lrbox}{\ myboxA}3447 \begin{lrbox}{\LstBox} 3489 3448 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 3490 3449 int x; double y char z; … … 3492 3451 \end{lrbox} 3493 3452 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{3em}}l@{}} 3494 \multicolumn{1}{@{}l@{}}{\usebox\ myboxA} \\3453 \multicolumn{1}{@{}l@{}}{\usebox\LstBox} \\ 3495 3454 \multicolumn{1}{c@{\hspace{2em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{\CC}} & \multicolumn{1}{c}{\textbf{Python}} \\ 3496 3455 \begin{cfa}[aboveskip=0pt,belowskip=0pt] … … 6588 6547 hence, names in these include files are not mangled\index{mangling!name} (see~\VRef{s:Interoperability}). 6589 6548 All other C header files must be explicitly wrapped in ©extern "C"© to prevent name mangling. 6590 This approach is different from \Index*[C++]{\CC{}} where the name-mangling issue is handled internally inC header-files through checks for preprocessor variable ©__cplusplus©, which adds appropriate ©extern "C"© qualifiers.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. 6591 6550 6592 6551 … … 6602 6561 The 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. 6603 6562 6604 C storage management provides the following capabilities:6563 Storage management provides the following capabilities: 6605 6564 \begin{description} 6606 \item[fill ed]6607 after allocation with a specified character or value.6565 \item[fill] 6566 after allocation the storage is filled with a specified character. 6608 6567 \item[resize] 6609 an existing allocation to decreased or increased itssize.6610 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 into the new allocation.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. 6611 6570 For an increase in storage size, new storage after the copied data may be filled. 6612 \item[align ]6613 an allocation on a specified memory boundary, \eg, an address multiple of 64 or 128 for cache-line purposes.6571 \item[alignment] 6572 an allocation starts on a specified memory boundary, \eg, an address multiple of 64 or 128 for cache-line purposes. 6614 6573 \item[array] 6615 6574 the allocation size is scaled to the specified number of array elements. 6616 6575 An array may be filled, resized, or aligned. 6617 6576 \end{description} 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@{}} 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@{}} 6623 6580 \multicolumn{1}{c}{}& & \multicolumn{1}{c|}{fill} & resize & alignment & array \\ 6624 6581 \hline 6625 6582 C & ©malloc© & no & no & no & no \\ 6626 6583 & ©calloc© & yes (0 only) & no & no & yes \\ 6627 & ©realloc© & copy& yes & no & no \\6584 & ©realloc© & no/copy & yes & no & no \\ 6628 6585 & ©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 \\6631 6586 & ©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 \\6635 6587 \hline 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 \\ 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 \\ 6642 6592 \end{tabular} 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. 6649 Type-safe allocation is provided for all C allocation routines and new \CFA allocation routines, \eg in 6650 \begin{cfa} 6651 int * ip = (int *)malloc( sizeof(int) ); §\C{// C}§ 6652 int * ip = malloc(); §\C{// \CFA type-safe version of C malloc}§ 6653 int * ip = alloc(); §\C{// \CFA type-safe uniform alloc}§ 6654 \end{cfa} 6655 the 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} 6659 struct S { int i; } __attribute__(( aligned( 128 ) )); // cache-line alignment 6660 S * sp = malloc(); §\C{// honour type alignment}§ 6661 \end{cfa} 6662 the storage allocation is implicitly aligned to 128 rather than the default 16. 6663 The 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}. 6666 Hence, initial allocation capabilities are remembered and maintained when resize requires copying. 6667 For 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. 6668 Without 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} 6675 struct S { int i; }; §\C{// cache-line aglinment}§ 6676 void ?{}( S & s, int i ) { s.i = i; } 6677 // assume ?|? operator for printing an S 6678 6679 S & sp = *®new®( 3 ); §\C{// call constructor after allocation}§ 6680 sout | sp.i; 6681 ®delete®( &sp ); 6682 6683 S * spa = ®anew®( 10, 5 ); §\C{// allocate array and initialize each array element}§ 6684 for ( i; 10 ) sout | spa[i] | nonl; 6685 sout | nl; 6686 ®adelete®( 10, spa ); 6687 \end{cfa} 6688 Allocation routines ©new©/©anew© allocate a variable/array and initialize storage using the allocated type's constructor. 6689 Note, the matching deallocation routines ©delete©/©adelete©. 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. 6690 6595 6691 6596 \leavevmode 6692 6597 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6598 // C unsafe allocation 6693 6599 extern "C" { 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 6708 void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap 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 } 6709 6610 6710 6611 forall( dtype T | sized(T) ) { 6711 // §\CFA§ safe equivalents, i.e., implicit size specification6612 // §\CFA§ safe equivalents, i.e., implicit size specification 6712 6613 T * malloc( void ); 6713 6614 T * calloc( size_t dim ); 6714 6615 T * realloc( T * ptr, size_t size ); 6715 6616 T * memalign( size_t align ); 6716 T * cmemalign( size_t align, size_t dim );6717 6617 T * aligned_alloc( size_t align ); 6718 6618 int posix_memalign( T ** ptr, size_t align ); 6719 6619 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}§ 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}§ 6746 6636 T * memcpy( T * dest, const T * src );§\indexc{memcpy}§ 6747 6637 6748 // §\CFA§ safe initialization/copy, i.e., implicit size specification, array types 6749 T * amemset( T dest[], char fill, size_t dim );6638 // §\CFA§ safe initialization/copy array 6639 T * amemset( T dest[], char c, size_t dim ); 6750 6640 T * amemcpy( T dest[], const T src[], size_t dim ); 6751 6641 } 6752 6642 6753 // §\CFA§ allocation/deallocation and constructor/destructor , non-array types6754 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );§\indexc{new}§6755 forall( dtype T | sized(T) | { void ^?{}( T &); } ) void delete( T * ptr );§\indexc{delete}§6756 forall( dtype T, ttype Params | sized(T) | { void ^?{}( T &); void delete( Params ); } )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 ); } ) 6757 6647 void delete( T * ptr, Params rest ); 6758 6648 6759 // §\CFA§ allocation/deallocation and constructor/destructor, array types6760 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );§\indexc{anew}§6761 forall( dtype T | sized(T) | { void ^?{}( T &); } ) void adelete( size_t dim, T arr[] );§\indexc{adelete}§6762 forall( dtype T | sized(T) | { void ^?{}( T &); }, ttype Params | { void adelete( Params ); } )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 ); } ) 6763 6653 void adelete( size_t dim, T arr[], Params rest ); 6764 6654 \end{cfa}
Note:
See TracChangeset
for help on using the changeset viewer.