Changes in / [bedb40e:0cf9ffd]


Ignore:
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • doc/bibliography/pl.bib

    rbedb40e r0cf9ffd  
    939939    title       = {\textsf{C}$\mathbf{\forall}$ : Adding Modern Programming Language Features to C},
    940940    year        = 2018,
    941     month       = aug,
    942941    journal     = spe,
    943     note        = {http://dx.doi.org/10.1002/spe.2624},
     942    note        = {Accepted, to appear},
    944943}
    945944
     
    963962    comment     = {
    964963        The evidence given is thin.
    965     },
     964        }
    966965}
    967966
     
    18281827    key         = {Peter Buhr},
    18291828    title       = {CS343},
    1830     year        = 2018,
     1829    year        = 2017,
    18311830    howpublished= {\href{https://www.student.cs.uwaterloo.ca/~cs343}{https://\-www.student.cs.uwaterloo.ca/\-~cs343}},
    18321831}
     
    33633362    author      = {Peter Buhr and David Dice and Wim H. Hesselink},
    33643363    journal     = ccpe,
    3365     volume      = 30,
    3366     number      = 18,
     3364    volumeopt   = 30,
     3365    numberopt   = 4,
    33673366    year        = 2018,
    3368     month       = sep,
     3367    month       = may,
    33693368    publisher   = {John Wiley \& Sons},
    33703369    note        = {\url{https://doi-org.proxy.lib.uwaterloo.ca/10.1002/cpe.4475}}
     
    38503849    keywords    = {concurrency, critical section},
    38513850    contributer = {pabuhr@plg},
    3852     author      = {Dominic Duggan and Gordon V. Cormack and John Ophel},
     3851    author      = {Dominic Duggan and G. V. Cormack and John Ophel},
    38533852    title       = {Kinded Type Inference for Parametric Overloading},
    38543853    journal     = acta,
     
    58565855    keywords    = {Cyclone, existential types, polymorphism, type variables},
    58575856    contributer = {a3moss@plg},
    5858     author      = {Dan Grossman},
     5857    author      = {D. Grossman},
    58595858    title       = {Quantified Types in an Imperative Language},
    58605859    journal     = toplas,
  • doc/user/user.tex

    rbedb40e r0cf9ffd  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Fri Aug 31 07:54:50 2018
    14 %% Update Count     : 3396
     13%% Last Modified On : Thu Jul 26 17:29:05 2018
     14%% Update Count     : 3366
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    210210Even with all its problems, C continues to be popular because it allows writing software at virtually any level in a computer system without restriction.
    211211For system programming, where direct access to hardware, storage management, and real-time issues are a requirement, C is usually the only language of choice.
    212 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.
     212The TIOBE index~\cite{TIOBE} for July 2018 ranks the top 5 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.
    213213The top 3 rankings over the past 30 years are:
    214214\begin{center}
     
    351351The 2011 C standard plus GNU extensions.
    352352\item
    353 \Indexc[deletekeywords=inline]{-fgnu89-inline}\index{compilation option!-fgnu89-inline@{\lstinline[deletekeywords=inline]$-fgnu89-inline$}}
     353\Indexc[deletekeywords=inline]{-fgnu89-inline}\index{compilation option!-fgnu89-inline@{\lstinline[deletekeywords=inline]@-fgnu89-inline@}}
    354354Use the traditional GNU semantics for inline routines in C11 mode, which allows inline routines in header files.
    355355\end{description}
     
    455455#endif
    456456
    457 ®#include_next <bfdlink.h>                      §\C{// must have internal check for multiple expansion}§
     457®#include_next <bfdlink.h>                                      §\C{// must have internal check for multiple expansion}§
    458458®
    459459#if defined( with ) && defined( __CFA_BFD_H__ ) §\C{// reset only if set}§
     
    504504
    505505C, \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.
    506 \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$.
     506\CFA extends the basic operators with the exponentiation operator ©?\?©\index{?\\?@\lstinline@?\?@} and ©?\=?©\index{?\\=?@\lstinline@?\=?@}, as in, ©x \ y© and ©x \= y©, which means $x^y$ and $x \leftarrow x^y$.
    507507The 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)©.
    508508
     
    516516256 64 -64 0.015625 -0.015625 18.3791736799526 0.264715-1.1922i
    517517\end{cfa}
    518 Parenthesis are necessary for complex constants or the expression is parsed as ©1.0f+®(®2.0fi \ 3.0f®)®+2.0fi©.
     518Parenthesis are necessary for the complex constants or the expression is parsed as ©1.0f+(2.0fi \ 3.0f)+2.0fi©.
    519519The exponentiation operator is available for all the basic types, but for user-defined types, only the integral-computation versions are available.
    520520For returning an integral value, the user type ©T© must define multiplication, ©*©, and one, ©1©;
     
    527527
    528528
    529 %\subsection{\texorpdfstring{\protect\lstinline@if@/\protect\lstinline@while@ Statement}{if Statement}}
    530 \subsection{\texorpdfstring{\LstKeywordStyle{if}/\LstKeywordStyle{while} Statement}{if/while Statement}}
    531 
    532 The ©if©/©while© expression allows declarations, similar to ©for© declaration expression.
    533 (Does not make sense for ©do©-©while©.)
    534 \begin{cfa}
    535 if ( ®int x = f()® ) ...                                        §\C{// x != 0}§
    536 if ( ®int x = f(), y = g()® ) ...                       §\C{// x != 0 \&\& y != 0}§
    537 if ( ®int x = f(), y = g(); x < y® ) ...        §\C{// relational expression}§
    538 if ( ®struct S { int i; } x = { f() }; x.i < 4® ) §\C{// relational expression}§
    539 
    540 while ( ®int x = f()® ) ...                                     §\C{// x != 0}§
    541 while ( ®int x = f(), y = g()® ) ...            §\C{// x != 0 \&\& y != 0}§
    542 while ( ®int x = f(), y = g(); x < y® ) ... §\C{// relational expression}§
    543 while ( ®struct S { int i; } x = { f() }; x.i < 4® ) ... §\C{// relational expression}§
    544 \end{cfa}
    545 Unless a relational expression is specified, each variable is compared not equal to 0, which is the standard semantics for the ©if©/©while© expression, and the results are combined using the logical ©&&© operator.\footnote{\CC only provides a single declaration always compared not equal to 0.}
     529%\subsection{\texorpdfstring{\protect\lstinline@if@ Statement}{if Statement}}
     530\subsection{\texorpdfstring{\LstKeywordStyle{if} Statement}{if Statement}}
     531
     532The ©if© expression allows declarations, similar to ©for© declaration expression:
     533\begin{cfa}
     534if ( int x = f() ) ...                                          §\C{// x != 0}§
     535if ( int x = f(), y = g() ) ...                         §\C{// x != 0 \&\& y != 0}§
     536if ( int x = f(), y = g(); ®x < y® ) ...        §\C{// relational expression}§
     537\end{cfa}
     538Unless a relational expression is specified, each variable is compared not equal to 0, which is the standard semantics for the ©if© expression, and the results are combined using the logical ©&&© operator.\footnote{\CC only provides a single declaration always compared not equal to 0.}
    546539The scope of the declaration(s) is local to the @if@ statement but exist within both the ``then'' and ``else'' clauses.
    547 
    548 
    549 %\subsection{\texorpdfstring{\protect\lstinline@for@ Statement}{for Statement}}
    550 \subsection{\texorpdfstring{\LstKeywordStyle{for} Statement}{for Statement}}
    551 
    552 The ©for©/©while©/©do-while© loop-control allows empty or simplified ranges.
    553 An empty conditional implies ©1©.
    554 The up-to range ©~©\index{~@©~©} means exclusive range [M,N);
    555 the up-to range ©~=©\index{~=@©~=©} means inclusive range [M,N].
    556 The down-to range ©-~©\index{-~@©-~©} means exclusive range [N,M);
    557 the down-to range ©-~=©\index{-~=@©-~=©} means inclusive range [N,M].
    558 ©0© is the implicit start value;
    559 ©1© is the implicit increment value for an up-to range and ©-1© for an implicit down-to range.
    560 The loop index is polymorphic in the type of the start value or comparison value when start is implicitly ©0©.
    561 \begin{cquote}
    562 \begin{tabular}{@{}ll|l@{}}
    563 \multicolumn{2}{c|}{for control} & \multicolumn{1}{c}{output} \\
    564 \hline
    565 \begin{cfa}
    566 while ®()® { sout | "empty"; break; }
    567 do { sout | "empty"; break; } while ®()®;
    568 for ®()® { sout | "empty"; break; }
    569 for ( ®0® ) { sout | "A"; }
    570 for ( ®1® ) { sout | "A"; }
    571 for ( ®10® ) { sout | "A"; }
    572 for ( ®1 ~= 10 ~ 2® ) { sout | "B"; }
    573 for ( ®10 -~= 1 ~ -2® ) { sout | "C"; }
    574 for ( ®0.5 ~ 5.5® ) { sout | "D"; }
    575 for ( ®5.5 -~ 0.5® ) { sout | "E"; }
    576 for ( ®i; 10® ) { sout | i; }
    577 for ( ®i; 1 ~= 10 ~ 2® ) { sout | i; }
    578 for ( ®i; 10 -~= 1 ~ -2® ) { sout | i; }
    579 for ( ®i; 0.5 ~ 5.5® ) { sout | i; }
    580 for ( ®i; 5.5 -~ 0.5® ) { sout | i; }
    581 for ( ®ui; 2u ~= 10u ~ 2u® ) { sout | ui; }
    582 for ( ®ui; 10u -~= 2u ~ -2u® ) { sout | ui; }
    583 int start = 3, comp = 10, inc = 2;
    584 for ( ®i; start ~ comp ~ inc + 1® ) { sout | i; }
    585 \end{cfa}
    586 &
    587 \begin{cfa}
    588 sout | endl;
    589 sout | endl;
    590 sout | endl;
    591 sout | endl;
    592 sout | endl;
    593 sout | endl;
    594 sout | endl;
    595 sout | endl;
    596 sout | endl;
    597 sout | endl;
    598 sout | endl;
    599 sout | endl;
    600 sout | endl;
    601 sout | endl;
    602 sout | endl;
    603 sout | endl;
    604 sout | endl;
    605 
    606 sout | endl;
    607 \end{cfa}
    608 &
    609 \begin{cfa}
    610 empty
    611 empty
    612 empty
    613 
    614 A
    615 A A A A A A A A A A
    616 B B B B B
    617 C C C C C
    618 D D D D D
    619 E E E E E
    620 0 1 2 3 4 5 6 7 8 9
    621 1 3 5 7 9
    622 10 8 6 4 2
    623 0.5 1.5 2.5 3.5 4.5
    624 5.5 4.5 3.5 2.5 1.5
    625 2 4 6 8 10
    626 10 8 6 4 2
    627 
    628 3 6 9
    629 \end{cfa}
    630 \end{tabular}
    631 \end{cquote}
    632540
    633541
     
    892800
    893801
    894 % for ()  => for ( ;; )
    895 % for ( 10 - t ) => for ( typeof(10 - t) ? = 0 ; ? < 10 - t; ? += 1 ) // using 0 and 1
    896 % for ( i ; 10 - t ) => for ( typeof(10 - t) i = 0 ; i < 10 - t; i += 1 ) // using 0 and 1
    897 % for ( T i ; 10 - t ) => for ( T i = 0 ; i < 10 - t; i += 1 ) // using 0 and 1
    898 % for ( 3~9 ) => for ( int ? = 3 ; ? < 9; ? += 1 ) // using 1
    899 % for ( i ; 3~9 ) => for ( int i = 3 ; i < 9; i += 1 ) // using 1
    900 % for ( T i ; 3~9 ) => for ( T i = 3 ; i < 9; i += 1 ) // using 1
    901 
    902 
    903802%\subsection{\texorpdfstring{Labelled \protect\lstinline@continue@ / \protect\lstinline@break@}{Labelled continue / break}}
    904803\subsection{\texorpdfstring{Labelled \LstKeywordStyle{continue} / \LstKeywordStyle{break} Statement}{Labelled continue / break Statement}}
     
    906805While C provides ©continue© and ©break© statements for altering control flow, both are restricted to one level of nesting for a particular control structure.
    907806Unfortunately, this restriction forces programmers to use \Indexc{goto} to achieve the equivalent control-flow for more than one level of nesting.
    908 To prevent having to switch to the ©goto©, \CFA extends the \Indexc{continue}\index{continue@©continue©!labelled}\index{labelled!continue@©continue©} and \Indexc{break}\index{break@©break©!labelled}\index{labelled!break@©break©} with a target label to support static multi-level exit\index{multi-level exit}\index{static multi-level exit}~\cite{Buhr85}, as in Java.
     807To prevent having to switch to the ©goto©, \CFA extends the \Indexc{continue}\index{continue@\lstinline@continue@!labelled}\index{labelled!continue@©continue©} and \Indexc{break}\index{break@\lstinline@break@!labelled}\index{labelled!break@©break©} with a target label to support static multi-level exit\index{multi-level exit}\index{static multi-level exit}~\cite{Buhr85}, as in Java.
    909808For both ©continue© and ©break©, the target label must be directly associated with a ©for©, ©while© or ©do© statement;
    910809for ©break©, the target label can also be associated with a ©switch©, ©if© or compound (©{}©) statement.
     
    991890\end{figure}
    992891
    993 Both labelled ©continue© and ©break© are a ©goto©\index{goto@©goto©!restricted} restricted in the following ways:
     892Both labelled ©continue© and ©break© are a ©goto©\index{goto@\lstinline@goto@!restricted} restricted in the following ways:
    994893\begin{itemize}
    995894\item
  • driver/as.cc

    rbedb40e r0cf9ffd  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // as.c -- map assembler file, scan for debug information. If found, expand file by one character and insert Cforall
    8 //         language code on the N line from the start of the debug information.
     7// as.c --
    98//
    109// Author           : Peter A. Buhr
    1110// Created On       : Wed Aug  1 10:49:42 2018
    1211// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Sat Sep  8 08:40:16 2018
    14 // Update Count     : 97
     12// Last Modified On : Wed Aug 22 17:30:24 2018
     13// Update Count     : 93
    1514//
    1615
     
    4443        off_t size = mystat.st_size;
    4544
    46         if ( size ) {                                                                           // cannot map 0 sized file
    47                 char * start = (char *)mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
    48                 if ( start == (void *)-1 ) { perror( "mmap" ); exit( EXIT_FAILURE ); };
     45        char * start = (char *)mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
     46        if ( start == (void *)-1 ) { perror( "mmap" ); exit( EXIT_FAILURE ); };
    4947
    50                 if ( char * cursor = strstr( start, ".Ldebug_info0:" ) ) { // debug information ?
    51                         // Expand file by one byte to hold 2 character Cforall language code.
    52                         if ( ftruncate( fd, size + 1 ) ) { perror( "ftruncate" ); exit( EXIT_FAILURE ); };
     48        if ( char * cursor = strstr( start, ".Ldebug_info0:" ) ) { // debug information ?
     49                // Expand file by one byte to hold 2 character Cforall language code.
     50                if ( ftruncate( fd, size + 1 ) ) { perror( "ftruncate" ); exit( EXIT_FAILURE ); };
    5351
    54                         for ( int i = 0; i < 8; i += 1 ) {                      // move N (magic) lines forward
    55                                 cursor = strstr( cursor, "\n" ) + 1;
    56                         } // for
     52                for ( int i = 0; i < 8; i += 1 ) {                              // move N (magic) lines forward
     53                        cursor = strstr( cursor, "\n" ) + 1;
     54                } // for
    5755
    58                         cursor -= 2;                                                            // backup over "c\n" language value
    59                         if ( *(cursor - 1) != 'x' ) { fprintf( stderr, "invalid C language code\n" ); exit( EXIT_FAILURE ); };
     56                cursor -= 2;                                                                    // backup over "c\n" language value
     57                if ( *(cursor - 1) != 'x' ) { fprintf( stderr, "invalid C language code\n" ); exit( EXIT_FAILURE ); };
    6058
    61                         memmove( cursor + 2, cursor + 1, start + size - cursor - 1 ); // move remaining text 1 character right
     59                memmove( cursor + 2, cursor + 1, start + size - cursor - 1 ); // move remaining text 1 character right
    6260
    63                         *(cursor) = '2';                                                        // replace C language value with CFA
    64                         *(cursor + 1) = '5';
    65                 } // if
     61                *(cursor) = '2';                                                                // replace C language value with CFA
     62                *(cursor + 1) = '5';
     63        } // if
    6664
    67                 if ( munmap( start, size ) ) { perror( "munmap" ); exit( EXIT_FAILURE ); }; // update on disk
    68         } // if
     65        if ( munmap( start, size ) ) { perror( "munmap" ); exit( EXIT_FAILURE ); }; // update on disk
    6966
    7067        argv[0] = "as";
  • driver/cc1.cc

    rbedb40e r0cf9ffd  
    1010// Created On       : Fri Aug 26 14:23:51 2005
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Sep  3 16:57:05 2018
    13 // Update Count     : 125
     12// Last Modified On : Thu Aug 23 09:48:40 2018
     13// Update Count     : 122
    1414//
    1515
     
    3232string compiler_name( CFA_BACKEND_CC );                                 // path/name of C compiler
    3333
     34string D__GCC_X__( "-D__GCC_X__=" );
    3435string D__GCC_BPREFIX__( "-D__GCC_BPREFIX__=" );
    3536string D__CFA_FLAGPREFIX__( "-D__CFA_FLAG__=" );
     
    4546enum { NumSuffixes = 2 };
    4647const string suffixes[NumSuffixes] = { "cfa", "hfa", };
     48
    4749
    4850void suffix( string arg, const char * args[], int & nargs ) {
     
    114116void Stage1( const int argc, const char * const argv[] ) {
    115117        int code;
     118        int i;
    116119
    117120        string arg;
     
    136139        cerr << "Stage1" << endl;
    137140        #endif // __DEBUG_H__
     141
     142        // process all the arguments
     143
    138144        checkEnv( args, nargs );                                                        // arguments passed via environment variables
    139         #ifdef __DEBUG_H__
    140         for ( int i = 1; i < argc; i += 1 ) {
     145
     146        for ( i = 1; i < argc; i += 1 ) {
     147                #ifdef __DEBUG_H__
    141148                cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
    142         } // for
    143         #endif // __DEBUG_H__
    144 
    145         // process all the arguments
    146 
    147         for ( int i = 1; i < argc; i += 1 ) {
     149                #endif // __DEBUG_H__
    148150                arg = argv[i];
     151                #ifdef __DEBUG_H__
     152                cerr << "arg:\"" << arg << "\"" << endl;
     153                #endif // __DEBUG_H__
    149154                if ( prefix( arg, "-" ) ) {
    150155                        // strip g++ flags that are inappropriate or cause duplicates in subsequent passes
     
    180185                                ncargs += 1;
    181186                                i += 1;                                                                 // and the argument
     187                        // } else if ( prefix( arg, D__GCC_X__ ) ) {
     188                        //      args[nargs] = "-x";
     189                        //      nargs += 1;
     190                        //      args[nargs] = ( *new string( arg.substr( D__GCC_X__.size() ) ) ).c_str(); // pass the flag along
     191                        //      nargs += 1;
     192                        // } else if ( arg == "-D" && prefix( argv[i + 1], D__GCC_X__.substr(2) ) ) {
     193                        //      args[nargs] = "-x";
     194                        //      nargs += 1;
     195                        //      args[nargs] = ( *new string( string( argv[i + 1] ).substr( D__GCC_X__.size() - 2 ) ) ).c_str(); // pass the flag along
     196                        //      nargs += 1;
     197                        //      i += 1;                                                                 // and the argument
    182198                        } else if ( prefix( arg, D__GCC_BPREFIX__ ) ) {
    183199                                bprefix = arg.substr( D__GCC_BPREFIX__.size() );
     
    235251        #ifdef __DEBUG_H__
    236252        cerr << "args:";
    237         for ( int i = 1; i < nargs; i += 1 ) {
     253        for ( i = 1; i < nargs; i += 1 ) {
    238254                cerr << " " << args[i];
    239255        } // for
     
    266282                #ifdef __DEBUG_H__
    267283                cerr << "nargs: " << nargs << endl;
    268                 for ( int i = 0; args[i] != NULL; i += 1 ) {
     284                for ( i = 0; args[i] != NULL; i += 1 ) {
    269285                        cerr << args[i] << " ";
    270286                } // for
     
    308324                #ifdef __DEBUG_H__
    309325                cerr << "cpp nargs: " << nargs << endl;
    310                 for ( int i = 0; args[i] != NULL; i += 1 ) {
     326                for ( i = 0; args[i] != NULL; i += 1 ) {
    311327                        cerr << args[i] << " ";
    312328                } // for
     
    361377                #ifdef __DEBUG_H__
    362378                cerr << "cfa-cpp ncargs: " << o_name << " " << CFA_flag << " " << ncargs << endl;
    363                 for ( int i = 0; cargs[i] != NULL; i += 1 ) {
     379                for ( i = 0; cargs[i] != NULL; i += 1 ) {
    364380                        cerr << cargs[i] << " ";
    365381                } // for
     
    391407
    392408void Stage2( const int argc, const char * const * argv ) {
     409        int i;
     410
    393411        string arg;
    394412
     
    401419        cerr << "Stage2" << endl;
    402420        #endif // __DEBUG_H__
     421
     422        // process all the arguments
     423
    403424        checkEnv( args, nargs );                                                        // arguments passed via environment variables
    404         #ifdef __DEBUG_H__
    405         for ( int i = 1; i < argc; i += 1 ) {
     425
     426        for ( i = 1; i < argc; i += 1 ) {
     427                #ifdef __DEBUG_H__
    406428                cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
    407         } // for
    408         #endif // __DEBUG_H__
    409 
    410         // process all the arguments
    411 
    412         for ( int i = 1; i < argc; i += 1 ) {
     429                #endif // __DEBUG_H__
    413430                arg = argv[i];
     431                #ifdef __DEBUG_H__
     432                cerr << "arg:\"" << arg << "\"" << endl;
     433                #endif // __DEBUG_H__
    414434                if ( prefix( arg, "-" ) ) {
    415435                        // strip inappropriate flags
     
    456476        #ifdef __DEBUG_H__
    457477        cerr << "args:";
    458         for ( int i = 1; i < nargs; i += 1 ) {
     478        for ( i = 1; i < nargs; i += 1 ) {
    459479                cerr << " " << args[i];
    460480        } // for
     
    472492        #ifdef __DEBUG_H__
    473493        cerr << "stage2 nargs: " << nargs << endl;
    474         for ( int i = 0; args[i] != NULL; i += 1 ) {
     494        for ( i = 0; args[i] != NULL; i += 1 ) {
    475495                cerr << args[i] << " ";
    476496        } // for
     
    486506int main( const int argc, const char * const argv[], __attribute__((unused)) const char * const env[] ) {
    487507        #ifdef __DEBUG_H__
    488         for ( int int i = 0; env[i] != NULL; i += 1 ) {
     508        for ( int i = 0; env[i] != NULL; i += 1 ) {
    489509                cerr << env[i] << endl;
    490510        } // for
  • driver/cfa.cc

    rbedb40e r0cf9ffd  
    1010// Created On       : Tue Aug 20 13:44:49 2002
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Sep  3 16:47:59 2018
    13 // Update Count     : 275
     12// Last Modified On : Thu Aug 23 15:41:55 2018
     13// Update Count     : 270
    1414//
    1515
     
    4343const string suffixes[NumSuffixes] = { "cfa", "hfa", };
    4444
    45 bool suffix( string arg, const char * args[], int & nargs ) {
     45void suffix( string arg, const char * args[], int & nargs ) {
    4646        //std::cerr << arg << std::endl;
    4747        size_t dot = arg.find_last_of( "." );
    4848        //std::cerr << dot << " " << (dot != string::npos ? arg.substr( dot + 1 ) : "fred" ) << std::endl;
    49         if ( dot == string::npos ) return false;
     49        if ( dot == string::npos ) return;
    5050        string sx = arg.substr( dot + 1 );
    5151        for ( int i = 0; i < NumSuffixes; i += 1 ) {
     
    5555                        args[nargs] = "c";
    5656                        nargs += 1;
    57                         return true;
     57                        return;
    5858                } // if
    5959        } // for
    60         return false;
    6160} // suffix
    6261
     
    129128        #ifdef __DEBUG_H__
    130129        cerr << "CFA:" << endl;
     130        #endif // __DEBUG_H__
     131
     132        // process command-line arguments
     133
    131134        for ( int i = 1; i < argc; i += 1 ) {
    132             cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
    133         } // for
    134         #endif // __DEBUG_H__
    135 
    136         // process command-line arguments
    137 
    138         for ( int i = 1; i < argc; i += 1 ) {
     135                #ifdef __DEBUG_H__
     136                cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
     137                #endif // __DEBUG_H__
    139138                arg = argv[i];                                                                  // convert to string value
     139                #ifdef __DEBUG_H__
     140                cerr << "arg:\"" << arg << "\"" << endl;
     141                #endif // __DEBUG_H__
    140142                if ( prefix( arg, "-" ) ) {
    141143                        // pass through arguments
     
    200202                                args[nargs] = argv[i];                                  // pass the argument along
    201203                                nargs += 1;
     204                        } else if ( arg == "-x" ) {
     205                                xflag = true;
     206                                args[nargs] = argv[i];                                  // pass the argument along
     207                                nargs += 1;
     208                                i += 1;                                                                 // advance to argument
     209                                args[nargs] = argv[i];                                  // pass the argument along
     210                                nargs += 1;
     211                                // args[nargs] = ( *new string( string("-D__GCC_X__=") + argv[i] ) ).c_str(); // add the argument for -x
     212                                // nargs += 1;
     213                        } else if ( prefix( arg, "-x" ) ) {
     214                                xflag = true;
     215                                args[nargs] = argv[i];                                  // pass the argument along
     216                                nargs += 1;
     217                                // args[nargs] = ( *new string( string("-D__GCC_X__=") + arg.substr(2) ) ).c_str(); // add the argument for -x
     218                                // nargs += 1;
    202219                        } else if ( arg == "-w" ) {
    203220                                args[nargs] = argv[i];                                  // pass the argument along
     
    281298                        } // if
    282299                } else {
    283                         bool cfa = suffix( arg, args, nargs );          // check suffix
    284                         args[nargs] = argv[i];                                          // concatenate file
     300                        bool opt = false;
     301                        if ( ! xflag ) {
     302                                suffix( arg, args, nargs );                             // check suffix
     303                                // args[nargs] = ( *new string( string("-D__GCC_X__=c") ) ).c_str(); // add the argument for -x
     304                                // nargs += 1;
     305                                opt = true;
     306                        } // if
     307                        // concatenate other arguments
     308                        args[nargs] = argv[i];
    285309                        nargs += 1;
    286                         if ( cfa ) {
     310                        if ( opt ) {
    287311                                args[nargs] = "-x";
    288312                                nargs += 1;
    289313                                args[nargs] = "none";
    290314                                nargs += 1;
     315                                // args[nargs] = ( *new string( string("-D__GCC_X__=none") ) ).c_str(); // add the argument for -x
     316                                // nargs += 1;
    291317                        } // if
    292318                        nonoptarg = true;
     
    294320                } // if
    295321        } // for
    296 
    297     args[nargs] = "-x";                                 // turn off language
    298     nargs += 1;
    299     args[nargs] = "none";
    300     nargs += 1;
    301322
    302323        #ifdef __x86_64__
  • libcfa/src/heap.cfa

    rbedb40e r0cf9ffd  
     1// #comment TD : this file uses both spaces and tabs for indentation
     2
    13//
    24// Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo
     
    1012// Created On       : Tue Dec 19 21:58:35 2017
    1113// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Sep  6 09:01:30 2018
    13 // Update Count     : 513
     14// Last Modified On : Sat Aug 11 08:22:16 2018
     15// Update Count     : 495
    1416//
    1517
     
    2325
    2426// #comment TD : Many of these should be merged into math I believe
    25 #include "bits/align.hfa"                                                               // libPow2
    26 #include "bits/defs.hfa"                                                                // likely, unlikely
    27 #include "bits/locks.hfa"                                                               // __spinlock_t
     27#include "bits/align.hfa"                                                                       // libPow2
     28#include "bits/defs.hfa"                                                                        // likely, unlikely
     29#include "bits/locks.hfa"                                                                       // __spinlock_t
    2830#include "startup.hfa"                                                                  // STARTUP_PRIORITY_MEMORY
    29 #include "stdlib.hfa"                                                                   // bsearchl
     31#include "stdlib.hfa"                                                                           // bsearchl
    3032#include "malloc.h"
    3133
     
    149151
    150152extern "C" {
    151         void heapAppStart() {                                                           // called by __cfaabi_appready_startup
    152                 allocFree = 0;
    153         } // heapAppStart
    154 
    155         void heapAppStop() {                                                            // called by __cfaabi_appready_startdown
    156                 fclose( stdin ); fclose( stdout );
    157                 checkUnfreed();
    158         } // heapAppStop
     153void heapAppStart() {                                                                   // called by __cfaabi_appready_startup
     154        allocFree = 0;
     155} // heapAppStart
     156
     157void heapAppStop() {                                                                    // called by __cfaabi_appready_startdown
     158        fclose( stdin ); fclose( stdout );
     159        checkUnfreed();
     160} // heapAppStop
    159161} // extern "C"
    160162#endif // __CFA_DEBUG__
     
    165167
    166168        struct Storage {
    167                 struct Header {                                                                 // header
     169            struct Header {                                                                     // header
    168170                        union Kind {
    169171                                struct RealHeader {
    170172                                        union {
    171                                                 struct {                                                // 4-byte word => 8-byte header, 8-byte word => 16-byte header
     173                                                // #comment TD : this code use byte size but the comment uses bit size
     174
     175                                                struct {                                                // 32-bit word => 64-bit header, 64-bit word => 128-bit header
    172176                                                        #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ && __SIZEOF_POINTER__ == 4
    173177                                                        uint32_t padding;                       // unused, force home/blocksize to overlay alignment in fake header
    174                                                         #endif // __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ && __SIZEOF_POINTER__ == 4
     178                                                        #endif // __ORDER_BIG_ENDIAN__ && __U_WORDSIZE__ == 32
    175179
    176180                                                        union {
     
    185189                                                        #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && __SIZEOF_POINTER__ == 4
    186190                                                        uint32_t padding;                       // unused, force home/blocksize to overlay alignment in fake header
    187                                                         #endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && __SIZEOF_POINTER__ == 4
     191                                                        #endif // __ORDER_LITTLE_ENDIAN__ && __U_WORDSIZE__ == 32
     192
    188193                                                };
    189                                                 // future code
     194
     195                                                // #comment TD : C++ code
    190196                                                #if BUCKLOCK == LOCKFREE
    191197                                                Stack<Storage>::Link next;              // freed block points next freed block of same size (double-wide)
     
    205211                                } fake; // FakeHeader
    206212                        } kind; // Kind
    207                 } header; // Header
    208                 char pad[ALIGN - sizeof( Header )];
    209                 char data[0];                                                                   // storage
     213            } header; // Header
     214            char pad[ALIGN - sizeof( Header )];
     215            char data[0];                                                                       // storage
    210216        }; // Storage
    211217
     
    214220        struct FreeHeader {
    215221                #if BUCKLOCK == SPINLOCK
    216                 __spinlock_t lock;                                                              // must be first field for alignment
    217                 Storage * freeList;
     222            __spinlock_t lock;                                                          // must be first field for alignment
     223            Storage * freeList;
    218224                #elif BUCKLOCK == LOCKFREE
    219                 // future code
    220                 StackLF<Storage> freeList;
     225                // #comment TD : C++ code
     226            StackLF<Storage> freeList;
    221227                #else
    222                 #error undefined lock type for bucket lock
     228                        #error undefined lock type for bucket lock
    223229                #endif // SPINLOCK
    224                 size_t blockSize;                                                               // size of allocations on this list
     230            size_t blockSize;                                                           // size of allocations on this list
    225231        }; // FreeHeader
    226232
     
    243249static unsigned int maxBucketsUsed;                                             // maximum number of buckets in use
    244250
     251// #comment TD : This array is not const but it feels like it should be
    245252// Powers of 2 are common allocation sizes, so make powers of 2 generate the minimum required size.
    246 static const unsigned int bucketSizes[NoBucketSizes] @= { // different bucket sizes
     253static unsigned int bucketSizes[NoBucketSizes] @= {             // different bucket sizes
    247254        16, 32, 48, 64,
    248255        64 + sizeof(HeapManager.Storage), 96, 112, 128, 128 + sizeof(HeapManager.Storage), 160, 192, 224,
     
    272279// #comment TD : The return type of this function should be commented
    273280static inline bool setMmapStart( size_t value ) {
    274   if ( value < pageSize || bucketSizes[NoBucketSizes - 1] < value ) return true;
     281        if ( value < pageSize || bucketSizes[NoBucketSizes - 1] < value ) return true;
    275282        mmapStart = value;                                                                      // set global
    276283
     
    306313        sbrk( (char *)libCeiling( (long unsigned int)End, libAlign() ) - End ); // move start of heap to multiple of alignment
    307314        heapBegin = heapEnd = sbrk( 0 );                                        // get new start point
    308                            } // HeapManager
     315} // HeapManager
    309316
    310317
     
    316323        // } // if
    317324        #endif // __STATISTICS__
    318                                 } // ~HeapManager
     325} // ~HeapManager
    319326
    320327
     
    322329void memory_startup( void ) {
    323330        #ifdef __CFA_DEBUG__
    324         if ( unlikely( heapBoot ) ) {                                           // check for recursion during system boot
     331        if ( unlikely( heapBoot ) ) {                                   // check for recursion during system boot
    325332                // DO NOT USE STREAMS AS THEY MAY BE UNAVAILABLE AT THIS POINT.
    326333                abort( "boot() : internal error, recursively invoked during system boot." );
     
    329336        #endif // __CFA_DEBUG__
    330337
    331         //assert( heapManager.heapBegin != 0 );
    332         //heapManager{};
    333         if ( heapManager.heapBegin == 0 ) heapManager{};
     338        // #comment TD : This assertion seems redundent with the above code
     339        assert( heapManager.heapBegin == 0 );
     340        heapManager{};
    334341} // memory_startup
    335342
     
    367374        char helpText[512];
    368375        __cfaabi_dbg_bits_print_buffer( helpText, sizeof(helpText),
    369                                                                         "\nHeap statistics:\n"
    370                                                                         "  malloc: calls %u / storage %llu\n"
    371                                                                         "  calloc: calls %u / storage %llu\n"
    372                                                                         "  memalign: calls %u / storage %llu\n"
    373                                                                         "  cmemalign: calls %u / storage %llu\n"
    374                                                                         "  realloc: calls %u / storage %llu\n"
    375                                                                         "  free: calls %u / storage %llu\n"
    376                                                                         "  mmap: calls %u / storage %llu\n"
    377                                                                         "  munmap: calls %u / storage %llu\n"
    378                                                                         "  sbrk: calls %u / storage %llu\n",
    379                                                                         malloc_calls, malloc_storage,
    380                                                                         calloc_calls, calloc_storage,
    381                                                                         memalign_calls, memalign_storage,
    382                                                                         cmemalign_calls, cmemalign_storage,
    383                                                                         realloc_calls, realloc_storage,
    384                                                                         free_calls, free_storage,
    385                                                                         mmap_calls, mmap_storage,
    386                                                                         munmap_calls, munmap_storage,
    387                                                                         sbrk_calls, sbrk_storage
     376                        "\nHeap statistics:\n"
     377                        "  malloc: calls %u / storage %llu\n"
     378                        "  calloc: calls %u / storage %llu\n"
     379                        "  memalign: calls %u / storage %llu\n"
     380                        "  cmemalign: calls %u / storage %llu\n"
     381                        "  realloc: calls %u / storage %llu\n"
     382                        "  free: calls %u / storage %llu\n"
     383                        "  mmap: calls %u / storage %llu\n"
     384                        "  munmap: calls %u / storage %llu\n"
     385                        "  sbrk: calls %u / storage %llu\n",
     386                        malloc_calls, malloc_storage,
     387                        calloc_calls, calloc_storage,
     388                        memalign_calls, memalign_storage,
     389                        cmemalign_calls, cmemalign_storage,
     390                        realloc_calls, realloc_storage,
     391                        free_calls, free_storage,
     392                        mmap_calls, mmap_storage,
     393                        munmap_calls, munmap_storage,
     394                        sbrk_calls, sbrk_storage
    388395                );
    389396} // printStats
    390397
    391 static int printStatsXML( FILE * stream ) {                             // see malloc_info
     398// #comment TD : Why do we have this?
     399static int printStatsXML( FILE * stream ) {
    392400        char helpText[512];
    393401        int len = snprintf( helpText, sizeof(helpText),
     
    423431static inline void noMemory() {
    424432        abort( "Heap memory exhausted at %zu bytes.\n"
    425                    "Possible cause is very large memory allocation and/or large amount of unfreed storage allocated by the program or system/library routines.",
    426                    ((char *)(sbrk( 0 )) - (char *)(heapManager.heapBegin)) );
     433                        "Possible cause is very large memory allocation and/or large amount of unfreed storage allocated by the program or system/library routines.",
     434                        ((char *)(sbrk( 0 )) - (char *)(heapManager.heapBegin)) );
    427435} // noMemory
    428436
     
    436444
    437445static inline bool setHeapExpand( size_t value ) {
    438   if ( heapExpand < pageSize ) return true;
     446        if ( heapExpand < pageSize ) return true;
    439447        heapExpand = value;
    440448        return false;
     
    445453        if ( unlikely( check ) ) {                                                      // bad address ?
    446454                abort( "Attempt to %s storage %p with address outside the heap.\n"
    447                            "Possible cause is duplicate free on same block or overwriting of memory.",
    448                            name, addr );
     455                                "Possible cause is duplicate free on same block or overwriting of memory.",
     456                                name, addr );
    449457        } // if
    450458} // checkHeader
     
    476484
    477485        #ifdef __CFA_DEBUG__
    478         checkHeader( addr < heapBegin || header < (HeapManager.Storage.Header *)heapBegin, name, addr ); // bad low address ?
     486                        checkHeader( addr < heapBegin || header < (HeapManager.Storage.Header *)heapBegin, name, addr ); // bad low address ?
    479487        #endif // __CFA_DEBUG__
    480488
     
    482490        //               It's called as the first statement of both branches of the last if, with the same parameters in all cases
    483491
    484         // header may be safe to dereference
    485         fakeHeader( header, size, alignment );
     492                // header may be safe to dereference
     493                fakeHeader( header, size, alignment );
    486494        #ifdef __CFA_DEBUG__
    487         checkHeader( header < (HeapManager.Storage.Header *)heapBegin || (HeapManager.Storage.Header *)heapEnd < header, name, addr ); // bad address ? (offset could be + or -)
     495                        checkHeader( header < (HeapManager.Storage.Header *)heapBegin || (HeapManager.Storage.Header *)heapEnd < header, name, addr ); // bad address ? (offset could be + or -)
    488496        #endif // __CFA_DEBUG__
    489497
    490         freeElem = (HeapManager.FreeHeader *)((size_t)header->kind.real.home & -3);
     498                freeElem = (HeapManager.FreeHeader *)((size_t)header->kind.real.home & -3);
    491499        #ifdef __CFA_DEBUG__
    492         if ( freeElem < &freeLists[0] || &freeLists[NoBucketSizes] <= freeElem ) {
    493                 abort( "Attempt to %s storage %p with corrupted header.\n"
    494                           "Possible cause is duplicate free on same block or overwriting of header information.",
    495                            name, addr );
    496         } // if
     500                        if ( freeElem < &freeLists[0] || &freeLists[NoBucketSizes] <= freeElem ) {
     501                        abort( "Attempt to %s storage %p with corrupted header.\n"
     502                                "Possible cause is duplicate free on same block or overwriting of header information.",
     503                                        name, addr );
     504                        } // if
    497505        #endif // __CFA_DEBUG__
    498         size = freeElem->blockSize;
    499         return false;
     506                size = freeElem->blockSize;
     507                return false;
    500508} // headers
    501509
     
    513521                        return 0;
    514522                } // if
    515                 #ifdef __STATISTICS__
     523#ifdef __STATISTICS__
    516524                sbrk_calls += 1;
    517525                sbrk_storage += increase;
    518                 #endif // __STATISTICS__
    519                 #ifdef __CFA_DEBUG__
     526#endif // __STATISTICS__
     527#ifdef __CFA_DEBUG__
    520528                // Set new memory to garbage so subsequent uninitialized usages might fail.
    521529                memset( (char *)heapEnd + heapRemaining, '\377', increase );
    522                 #endif // __CFA_DEBUG__
     530#endif // __CFA_DEBUG__
    523531                rem = heapRemaining + increase - size;
    524532        } // if
     
    552560
    553561                #if defined( SPINLOCK )
    554                 lock( freeElem->lock __cfaabi_dbg_ctx2 );
    555                 block = freeElem->freeList;                                             // remove node from stack
     562                        lock( freeElem->lock __cfaabi_dbg_ctx2 );
     563                        block = freeElem->freeList;                                             // remove node from stack
    556564                #else
    557                 block = freeElem->freeList.pop();
     565                        block = freeElem->freeList.pop();
    558566                #endif // SPINLOCK
    559567                if ( unlikely( block == 0 ) ) {                                 // no free block ?
     
    561569                        unlock( freeElem->lock );
    562570                        #endif // SPINLOCK
    563 
    564571                        // Freelist for that size was empty, so carve it out of the heap if there's enough left, or get some more
    565572                        // and then carve it off.
    566573
    567574                        block = (HeapManager.Storage *)extend( tsize ); // mutual exclusion on call
    568   if ( unlikely( block == 0 ) ) return 0;
     575                        if ( unlikely( block == 0 ) ) return 0;
    569576                        #if defined( SPINLOCK )
    570577                } else {
     
    575582
    576583                block->header.kind.real.home = freeElem;                // pointer back to free list of apropriate size
    577         } else {                                                                                        // large size => mmap
     584                } else {                                                                                        // large size => mmap
    578585                tsize = libCeiling( tsize, pageSize );                  // must be multiple of page size
    579586                #ifdef __STATISTICS__
    580                 __atomic_add_fetch( &mmap_calls, 1, __ATOMIC_SEQ_CST );
    581                 __atomic_add_fetch( &mmap_storage, tsize, __ATOMIC_SEQ_CST );
     587                        __atomic_add_fetch( &mmap_calls, 1, __ATOMIC_SEQ_CST );
     588                        __atomic_add_fetch( &mmap_storage, tsize, __ATOMIC_SEQ_CST );
    582589                #endif // __STATISTICS__
    583590                block = (HeapManager.Storage *)mmap( 0, tsize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, mmapFd, 0 );
     
    586593                        abort( "(HeapManager &)0x%p.doMalloc() : internal error, mmap failure, size:%zu error:%d.", &heapManager, tsize, errno );
    587594                } // if
    588                 #ifdef __CFA_DEBUG__
     595#ifdef __CFA_DEBUG__
    589596                // Set new memory to garbage so subsequent uninitialized usages might fail.
    590597                memset( block, '\377', tsize );
    591                 #endif // __CFA_DEBUG__
     598#endif // __CFA_DEBUG__
    592599                block->header.kind.real.blockSize = tsize;              // storage size for munmap
    593         } // if
    594 
    595         void * area = &(block->data);                                           // adjust off header to user bytes
     600                } // if
     601
     602                void * area = &(block->data);                                           // adjust off header to user bytes
    596603
    597604        #ifdef __CFA_DEBUG__
    598         assert( ((uintptr_t)area & (libAlign() - 1)) == 0 ); // minimum alignment ?
    599         __atomic_add_fetch( &allocFree, tsize, __ATOMIC_SEQ_CST );
    600         if ( traceHeap() ) {
    601                 enum { BufferSize = 64 };
    602                 char helpText[BufferSize];
    603                 int len = snprintf( helpText, BufferSize, "%p = Malloc( %zu ) (allocated %zu)\n", area, size, tsize );
    604                 // int len = snprintf( helpText, BufferSize, "Malloc %p %zu\n", area, size );
    605                 __cfaabi_dbg_bits_write( helpText, len );
    606         } // if
     605                        assert( ((uintptr_t)area & (libAlign() - 1)) == 0 ); // minimum alignment ?
     606                        __atomic_add_fetch( &allocFree, tsize, __ATOMIC_SEQ_CST );
     607                if ( traceHeap() ) {
     608                        enum { BufferSize = 64 };
     609                        char helpText[BufferSize];
     610                        int len = snprintf( helpText, BufferSize, "%p = Malloc( %zu ) (allocated %zu)\n", area, size, tsize );
     611                        // int len = snprintf( helpText, BufferSize, "Malloc %p %zu\n", area, size );
     612                        __cfaabi_dbg_bits_write( helpText, len );
     613                } // if
    607614        #endif // __CFA_DEBUG__
    608615
     
    613620static inline void doFree( void * addr ) with ( heapManager ) {
    614621        #ifdef __CFA_DEBUG__
    615         if ( unlikely( heapManager.heapBegin == 0 ) ) {
    616                 abort( "doFree( %p ) : internal error, called before heap is initialized.", addr );
    617         } // if
     622                if ( unlikely( heapManager.heapBegin == 0 ) ) {
     623                        abort( "doFree( %p ) : internal error, called before heap is initialized.", addr );
     624                } // if
    618625        #endif // __CFA_DEBUG__
    619626
     
    624631        if ( headers( "free", addr, header, freeElem, size, alignment ) ) { // mmapped ?
    625632                #ifdef __STATISTICS__
    626                 __atomic_add_fetch( &munmap_calls, 1, __ATOMIC_SEQ_CST );
    627                 __atomic_add_fetch( &munmap_storage, size, __ATOMIC_SEQ_CST );
     633                        __atomic_add_fetch( &munmap_calls, 1, __ATOMIC_SEQ_CST );
     634                        __atomic_add_fetch( &munmap_storage, size, __ATOMIC_SEQ_CST );
    628635                #endif // __STATISTICS__
    629636                if ( munmap( header, size ) == -1 ) {
    630637                        #ifdef __CFA_DEBUG__
    631638                        abort( "Attempt to deallocate storage %p not allocated or with corrupt header.\n"
    632                                    "Possible cause is invalid pointer.",
    633                                    addr );
     639                                        "Possible cause is invalid pointer.",
     640                                        addr );
    634641                        #endif // __CFA_DEBUG__
    635642                } // if
    636         } else {
     643                } else {
    637644                #ifdef __CFA_DEBUG__
    638                 // Set free memory to garbage so subsequent usages might fail.
    639                 memset( ((HeapManager.Storage *)header)->data, '\377', freeElem->blockSize - sizeof( HeapManager.Storage ) );
     645                        // Set free memory to garbage so subsequent usages might fail.
     646                        memset( ((HeapManager.Storage *)header)->data, '\377', freeElem->blockSize - sizeof( HeapManager.Storage ) );
    640647                #endif // __CFA_DEBUG__
    641648
    642649                #ifdef __STATISTICS__
    643                 free_storage += size;
     650                        free_storage += size;
    644651                #endif // __STATISTICS__
    645652                #if defined( SPINLOCK )
    646                 lock( freeElem->lock __cfaabi_dbg_ctx2 );               // acquire spin lock
    647                 header->kind.real.next = freeElem->freeList;    // push on stack
    648                 freeElem->freeList = (HeapManager.Storage *)header;
    649                 unlock( freeElem->lock );                                               // release spin lock
     653                        lock( freeElem->lock __cfaabi_dbg_ctx2 );               // acquire spin lock
     654                        header->kind.real.next = freeElem->freeList;    // push on stack
     655                        freeElem->freeList = (HeapManager.Storage *)header;
     656                        unlock( freeElem->lock );                                               // release spin lock
    650657                #else
    651                 freeElem->freeList.push( *(HeapManager.Storage *)header );
     658                        freeElem->freeList.push( *(HeapManager.Storage *)header );
    652659                #endif // SPINLOCK
    653         } // if
     660                } // if
    654661
    655662        #ifdef __CFA_DEBUG__
    656         __atomic_add_fetch( &allocFree, -size, __ATOMIC_SEQ_CST );
    657         if ( traceHeap() ) {
    658                 char helpText[64];
    659                 int len = snprintf( helpText, sizeof(helpText), "Free( %p ) size:%zu\n", addr, size );
    660                 __cfaabi_dbg_bits_write( helpText, len );
    661         } // if
     663                 __atomic_add_fetch( &allocFree, -size, __ATOMIC_SEQ_CST );
     664                if ( traceHeap() ) {
     665                        char helpText[64];
     666                        int len = snprintf( helpText, sizeof(helpText), "Free( %p ) size:%zu\n", addr, size );
     667                        __cfaabi_dbg_bits_write( helpText, len );
     668                } // if
    662669        #endif // __CFA_DEBUG__
    663670} // doFree
     
    667674        size_t total = 0;
    668675        #ifdef __STATISTICS__
    669         __cfaabi_dbg_bits_acquire();
    670         __cfaabi_dbg_bits_print_nolock( "\nBin lists (bin size : free blocks on list)\n" );
     676                __cfaabi_dbg_bits_acquire();
     677                __cfaabi_dbg_bits_print_nolock( "\nBin lists (bin size : free blocks on list)\n" );
    671678        #endif // __STATISTICS__
    672679        for ( unsigned int i = 0; i < maxBucketsUsed; i += 1 ) {
     
    688695
    689696                #ifdef __STATISTICS__
    690                 __cfaabi_dbg_bits_print_nolock( "%7zu, %-7u  ", size, N );
    691                 if ( (i + 1) % 8 == 0 ) __cfaabi_dbg_bits_print_nolock( "\n" );
     697                        __cfaabi_dbg_bits_print_nolock( "%7zu, %-7u  ", size, N );
     698                        if ( (i + 1) % 8 == 0 ) __cfaabi_dbg_bits_print_nolock( "\n" );
    692699                #endif // __STATISTICS__
    693700        } // for
    694701        #ifdef __STATISTICS__
    695         __cfaabi_dbg_bits_print_nolock( "\ntotal free blocks:%zu\n", total );
    696         __cfaabi_dbg_bits_release();
     702                __cfaabi_dbg_bits_print_nolock( "\ntotal free blocks:%zu\n", total );
     703                __cfaabi_dbg_bits_release();
    697704        #endif // __STATISTICS__
    698705        return (char *)heapEnd - (char *)heapBegin - total;
    699706} // checkFree
    700707
    701 
    702 static inline void * mallocNoStats( size_t size ) {             // necessary for malloc statistics
    703         //assert( heapManager.heapBegin != 0 );
    704         if ( unlikely( heapManager.heapBegin == 0 ) ) heapManager{}; // called before memory_startup ?
     708// #comment TD : This is not a good name, plus this feels like it could easily be folded into doMalloc
     709static inline void * malloc2( size_t size ) {                   // necessary for malloc statistics
     710        assert( heapManager.heapBegin != 0 );
    705711        void * area = doMalloc( size );
    706712        if ( unlikely( area == 0 ) ) errno = ENOMEM;            // POSIX
    707713        return area;
    708 } // mallocNoStats
    709 
    710 
    711 static inline void * memalignNoStats( size_t alignment, size_t size ) { // necessary for malloc statistics
    712         #ifdef __CFA_DEBUG__
     714} // malloc2
     715
     716
     717static inline void * memalign2( size_t alignment, size_t size ) { // necessary for malloc statistics
     718#ifdef __CFA_DEBUG__
    713719        checkAlign( alignment );                                                        // check alignment
    714         #endif // __CFA_DEBUG__
     720#endif // __CFA_DEBUG__
    715721
    716722        // if alignment <= default alignment, do normal malloc as two headers are unnecessary
    717   if ( unlikely( alignment <= libAlign() ) ) return mallocNoStats( size );
     723        if ( unlikely( alignment <= libAlign() ) ) return malloc2( size );
    718724
    719725        // Allocate enough storage to guarantee an address on the alignment boundary, and sufficient space before it for
     
    726732        // subtract libAlign() because it is already the minimum alignment
    727733        // add sizeof(Storage) for fake header
    728         // #comment TD : this is the only place that calls doMalloc without calling mallocNoStats, why ?
     734        // #comment TD : this is the only place that calls doMalloc without calling malloc2, why ?
    729735        char * area = (char *)doMalloc( size + alignment - libAlign() + sizeof(HeapManager.Storage) );
    730   if ( unlikely( area == 0 ) ) return area;
     736        if ( unlikely( area == 0 ) ) return area;
    731737
    732738        // address in the block of the "next" alignment address
     
    743749
    744750        return user;
    745 } // memalignNoStats
     751} // memalign2
    746752
    747753
    748754extern "C" {
    749         // The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not
    750         // initialized. If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be
    751         // successfully passed to free().
     755        // The malloc() function allocates size bytes and returns a pointer to the
     756        // allocated memory. The memory is not initialized. If size is 0, then malloc()
     757        // returns either NULL, or a unique pointer value that can later be successfully
     758        // passed to free().
    752759        void * malloc( size_t size ) {
    753760                #ifdef __STATISTICS__
    754                 __atomic_add_fetch( &malloc_calls, 1, __ATOMIC_SEQ_CST );
    755                 __atomic_add_fetch( &malloc_storage, size, __ATOMIC_SEQ_CST );
    756                 #endif // __STATISTICS__
    757 
    758                 return mallocNoStats( size );
    759         } // malloc
    760 
    761         // The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to
    762         // the allocated memory. The memory is set to zero. If nmemb or size is 0, then calloc() returns either NULL, or a
    763         // unique pointer value that can later be successfully passed to free().
    764         void * calloc( size_t noOfElems, size_t elemSize ) {
     761                        __atomic_add_fetch( &malloc_calls, 1, __ATOMIC_SEQ_CST );
     762                        __atomic_add_fetch( &malloc_storage, size, __ATOMIC_SEQ_CST );
     763                #endif // __STATISTICS__
     764
     765                return malloc2( size );
     766                } // malloc
     767
     768        // The calloc() function allocates memory for an array of nmemb elements of
     769        // size bytes each and returns a pointer to the allocated memory. The memory
     770        // is set to zero. If nmemb or size is 0, then calloc() returns either NULL,
     771        // or a unique pointer value that can later be successfully passed to free().
     772                void * calloc( size_t noOfElems, size_t elemSize ) {
    765773                size_t size = noOfElems * elemSize;
    766774                #ifdef __STATISTICS__
    767                 __atomic_add_fetch( &calloc_calls, 1, __ATOMIC_SEQ_CST );
    768                 __atomic_add_fetch( &calloc_storage, size, __ATOMIC_SEQ_CST );
    769                 #endif // __STATISTICS__
    770 
    771                 char * area = (char *)mallocNoStats( size );
    772           if ( unlikely( area == 0 ) ) return 0;
     775                        __atomic_add_fetch( &calloc_calls, 1, __ATOMIC_SEQ_CST );
     776                        __atomic_add_fetch( &calloc_storage, size, __ATOMIC_SEQ_CST );
     777                #endif // __STATISTICS__
     778
     779                char * area = (char *)malloc2( size );
     780                if ( unlikely( area == 0 ) ) return 0;
    773781
    774782                HeapManager.Storage.Header * header;
     
    782790                        memset( area, '\0', asize - sizeof(HeapManager.Storage) ); // set to zeros
    783791
    784                 header->kind.real.blockSize |= 2;                               // mark as zero filled
     792                header->kind.real.blockSize |= 2;               // mark as zero filled
    785793                return area;
    786         } // calloc
     794                } // calloc
    787795
    788796        // #comment TD : Document this function
     
    790798                size_t size = noOfElems * elemSize;
    791799                #ifdef __STATISTICS__
    792                 __atomic_add_fetch( &cmemalign_calls, 1, __ATOMIC_SEQ_CST );
    793                 __atomic_add_fetch( &cmemalign_storage, size, __ATOMIC_SEQ_CST );
    794                 #endif // __STATISTICS__
    795 
    796                 char * area = (char *)memalignNoStats( alignment, size );
    797           if ( unlikely( area == 0 ) ) return 0;
     800                        __atomic_add_fetch( &cmemalign_calls, 1, __ATOMIC_SEQ_CST );
     801                        __atomic_add_fetch( &cmemalign_storage, size, __ATOMIC_SEQ_CST );
     802                #endif // __STATISTICS__
     803
     804                char * area = (char *)memalign2( alignment, size );
     805                if ( unlikely( area == 0 ) ) return 0;
    798806                HeapManager.Storage.Header * header;
    799807                HeapManager.FreeHeader * freeElem;
     
    803811                // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
    804812                if ( ! mapped )
    805                         #endif // __CFA_DEBUG__
     813                #endif // __CFA_DEBUG__
    806814                        memset( area, '\0', asize - ( (char *)area - (char *)header ) ); // set to zeros
    807815                header->kind.real.blockSize |= 2;                               // mark as zero filled
    808816
    809817                return area;
    810         } // cmemalign
    811 
    812         // The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be
    813         // unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size
    814         // is larger than the old size, the added memory will not be initialized.  If ptr is NULL, then the call is
    815         // equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call
    816         // is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(),
    817         // calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done.
    818         void * realloc( void * addr, size_t size ) {
    819                 #ifdef __STATISTICS__
    820                 __atomic_add_fetch( &realloc_calls, 1, __ATOMIC_SEQ_CST );
    821                 #endif // __STATISTICS__
    822 
    823           if ( unlikely( addr == 0 ) ) return mallocNoStats( size ); // special cases
    824           if ( unlikely( size == 0 ) ) { free( addr ); return 0; }
     818                } // cmemalign
     819
     820        // The realloc() function changes the size of the memory block pointed to by
     821        // ptr to size bytes. The contents will be unchanged in the range from the
     822        // start of the region up to the minimum of the old and new sizes. If the new
     823        // size is larger than the old size, the added memory will not be initialized.
     824        // If ptr is NULL, then the call is equivalent to malloc(size), for all values
     825        // of size; if size is equal to zero, and ptr is not NULL, then the call is
     826        // equivalent to free(ptr). Unless ptr is NULL, it must have been returned by
     827        // an earlier call to malloc(), calloc() or realloc(). If the area pointed to
     828        // was moved, a free(ptr) is done.
     829                void * realloc( void * addr, size_t size ) {
     830                #ifdef __STATISTICS__
     831                        __atomic_add_fetch( &realloc_calls, 1, __ATOMIC_SEQ_CST );
     832                #endif // __STATISTICS__
     833
     834                if ( unlikely( addr == 0 ) ) return malloc2( size ); // special cases
     835                if ( unlikely( size == 0 ) ) { free( addr ); return 0; }
    825836
    826837                HeapManager.Storage.Header * header;
     
    837848
    838849                #ifdef __STATISTICS__
    839                 __atomic_add_fetch( &realloc_storage, size, __ATOMIC_SEQ_CST );
     850                        __atomic_add_fetch( &realloc_storage, size, __ATOMIC_SEQ_CST );
    840851                #endif // __STATISTICS__
    841852
     
    844855                        area = memalign( alignment, size );                     // create new area
    845856                } else {
    846                         area = mallocNoStats( size );                           // create new area
    847                 } // if
    848           if ( unlikely( area == 0 ) ) return 0;
     857                        area = malloc2( size ); // create new area
     858                } // if
     859                if ( unlikely( area == 0 ) ) return 0;
    849860                if ( unlikely( header->kind.real.blockSize & 2 ) ) { // previous request zero fill (calloc/cmemalign) ?
    850861                        assert( (header->kind.real.blockSize & 1) == 0 );
     
    853864                        // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
    854865                        if ( ! mapped )
    855                                 #endif // __CFA_DEBUG__
     866                        #endif // __CFA_DEBUG__
    856867                                memset( (char *)area + usize, '\0', asize - ( (char *)area - (char *)header ) - usize ); // zero-fill back part
    857868                        header->kind.real.blockSize |= 2;                       // mark new request as zero fill
     
    863874
    864875
    865         // The obsolete function memalign() allocates size bytes and returns a pointer to the allocated memory. The memory
    866         // address will be a multiple of alignment, which must be a power of two.
    867         void * memalign( size_t alignment, size_t size ) {
     876        // The obsolete function memalign() allocates size bytes and returns
     877        // a pointer to the allocated memory. The memory address will be a
     878        // multiple of alignment, which must be a power of two.
     879        void * memalign( size_t alignment, size_t size ) __attribute__ ((deprecated));
     880                void * memalign( size_t alignment, size_t size ) {
    868881                #ifdef __STATISTICS__
    869882                __atomic_add_fetch( &memalign_calls, 1, __ATOMIC_SEQ_CST );
     
    871884                #endif // __STATISTICS__
    872885
    873                 void * area = memalignNoStats( alignment, size );
     886                void * area = memalign2( alignment, size );
    874887
    875888                return area;
    876         } // memalign
    877 
    878         // The function aligned_alloc() is the same as memalign(), except for the added restriction that size should be a
    879         // multiple of alignment.
     889                } // memalign
     890
     891        // The function aligned_alloc() is the same as memalign(), except for
     892        // the added restriction that size should be a multiple of alignment.
    880893        void * aligned_alloc( size_t alignment, size_t size ) {
    881894                return memalign( alignment, size );
     
    883896
    884897
    885         // The function posix_memalign() allocates size bytes and places the address of the allocated memory in *memptr. The
    886         // address of the allocated memory will be a multiple of alignment, which must be a power of two and a multiple of
    887         // sizeof(void *). If size is 0, then posix_memalign() returns either NULL, or a unique pointer value that can later
    888         // be successfully passed to free(3).
     898        // The function posix_memalign() allocates size bytes and places the address
     899        // of the allocated memory in *memptr. The address of the allocated memory
     900        // will be a multiple of alignment, which must be a power of two and a multiple
     901        // of sizeof(void *). If size is 0, then posix_memalign() returns either NULL,
     902        // or a unique pointer value that can later be successfully passed to free(3).
    889903        int posix_memalign( void ** memptr, size_t alignment, size_t size ) {
    890           if ( alignment < sizeof(void *) || ! libPow2( alignment ) ) return EINVAL; // check alignment
     904                if ( alignment < sizeof(void *) || ! libPow2( alignment ) ) return EINVAL; // check alignment
    891905                * memptr = memalign( alignment, size );
    892           if ( unlikely( * memptr == 0 ) ) return ENOMEM;
     906                if ( unlikely( * memptr == 0 ) ) return ENOMEM;
    893907                return 0;
    894908        } // posix_memalign
    895909
    896         // The obsolete function valloc() allocates size bytes and returns a pointer to the allocated memory. The memory
    897         // address will be a multiple of the page size.  It is equivalent to memalign(sysconf(_SC_PAGESIZE),size).
     910        // The obsolete function valloc() allocates size bytes and returns a pointer
     911        // to the allocated memory. The memory address will be a multiple of the page size.
     912        // It is equivalent to memalign(sysconf(_SC_PAGESIZE),size).
     913        void * valloc( size_t size ) __attribute__ ((deprecated));
    898914        void * valloc( size_t size ) {
    899915                return memalign( pageSize, size );
     
    901917
    902918
    903         // The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to
    904         // malloc(), calloc() or realloc().  Otherwise, or if free(ptr) has already been called before, undefined behavior
    905         // occurs. If ptr is NULL, no operation is performed.
     919        // The free() function frees the memory space pointed to by ptr, which must
     920        // have been returned by a previous call to malloc(), calloc() or realloc().
     921        // Otherwise, or if free(ptr) has already been called before, undefined
     922        // behavior occurs. If ptr is NULL, no operation is performed.
    906923        void free( void * addr ) {
    907924                #ifdef __STATISTICS__
    908                 __atomic_add_fetch( &free_calls, 1, __ATOMIC_SEQ_CST );
     925                        __atomic_add_fetch( &free_calls, 1, __ATOMIC_SEQ_CST );
    909926                #endif // __STATISTICS__
    910927
     
    914931                if ( unlikely( addr == 0 ) ) {                                  // special case
    915932                        #ifdef __CFA_DEBUG__
    916                         if ( traceHeap() ) {
    917                                 #define nullmsg "Free( 0x0 ) size:0\n"
    918                                 // Do not debug print free( 0 ), as it can cause recursive entry from sprintf.
    919                                 __cfaabi_dbg_bits_write( nullmsg, sizeof(nullmsg) - 1 );
    920                         } // if
     933                                if ( traceHeap() ) {
     934                                        #define nullmsg "Free( 0x0 ) size:0\n"
     935                                        // Do not debug print free( 0 ), as it can cause recursive entry from sprintf.
     936                                        __cfaabi_dbg_bits_write( nullmsg, sizeof(nullmsg) - 1 );
     937                                } // if
    921938                        #endif // __CFA_DEBUG__
    922939                        return;
     
    926943        } // free
    927944
    928         // The mallopt() function adjusts parameters that control the behavior of the memory-allocation functions (see
    929         // malloc(3)). The param argument specifies the parameter to be modified, and value specifies the new value for that
     945        // The mallopt() function adjusts parameters that control the behavior of the
     946        // memory-allocation functions (see malloc(3)). The param argument specifies
     947        // the parameter to be modified, and value specifies the new value for that
    930948        // parameter.
    931         int mallopt( int option, int value ) {
     949                int mallopt( int option, int value ) {
    932950                choose( option ) {
    933                   case M_TOP_PAD:
    934                         if ( setHeapExpand( value ) ) fallthru default;
    935                   case M_MMAP_THRESHOLD:
    936                         if ( setMmapStart( value ) ) fallthru default;
    937                   default:
    938                         // #comment TD : 1 for unsopported feels wrong
    939                         return 1;                                                                       // success, or unsupported
     951                        case M_TOP_PAD:
     952                                if ( setHeapExpand( value ) ) fallthru default;
     953                        case M_MMAP_THRESHOLD:
     954                                if ( setMmapStart( value ) ) fallthru default;
     955                        default:
     956                                // #comment TD : 1 for unsopported feels wrong
     957                                return 1;                                                                       // success, or unsupported
    940958                } // switch
    941959                return 0;                                                                               // error
    942960        } // mallopt
    943961
    944         // The malloc_trim() function attempts to release free memory at the top of the heap (by calling sbrk(2) with a
    945         // suitable argument).
     962        // The malloc_trim() function attempts to release free memory at the top
     963        // of the heap (by calling sbrk(2) with a suitable argument).
    946964        int malloc_trim( size_t ) {
    947965                return 0;                                                                               // => impossible to release memory
    948966        } // malloc_trim
    949967
    950         // The malloc_usable_size() function returns the number of usable bytes in the block pointed to by ptr, a pointer to
    951         // a block of memory allocated by malloc(3) or a related function.
    952         size_t malloc_usable_size( void * addr ) {
    953           if ( unlikely( addr == 0 ) ) return 0;                        // null allocation has 0 size
     968        // The malloc_usable_size() function returns the number of usable bytes in the
     969        // block pointed to by ptr, a pointer to a block of memory allocated by
     970        // malloc(3) or a related function.
     971                size_t malloc_usable_size( void * addr ) {
     972                if ( unlikely( addr == 0 ) ) return 0;                  // null allocation has 0 size
    954973
    955974                HeapManager.Storage.Header * header;
     
    963982
    964983
    965     // The malloc_alignment() function returns the alignment of the allocation.
     984                // #comment TD : Document this function
    966985        size_t malloc_alignment( void * addr ) {
    967           if ( unlikely( addr == 0 ) ) return libAlign();       // minimum alignment
     986                if ( unlikely( addr == 0 ) ) return libAlign(); // minimum alignment
    968987                HeapManager.Storage.Header * header = (HeapManager.Storage.Header *)( (char *)addr - sizeof(HeapManager.Storage) );
    969988                if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
     
    972991                        return libAlign ();                                                     // minimum alignment
    973992                } // if
    974         } // malloc_alignment
    975 
    976 
    977     // The malloc_zero_fill() function returns true if the allocation is zero filled, i.e., initially allocated by calloc().
     993                } // malloc_alignment
     994
     995
     996                // #comment TD : Document this function
    978997        bool malloc_zero_fill( void * addr ) {
    979           if ( unlikely( addr == 0 ) ) return false;            // null allocation is not zero fill
     998                if ( unlikely( addr == 0 ) ) return false;              // null allocation is not zero fill
    980999
    9811000                HeapManager.Storage.Header * header = (HeapManager.Storage.Header *)( (char *)addr - sizeof(HeapManager.Storage) );
     
    9841003                } // if
    9851004                return (header->kind.real.blockSize & 2) != 0;  // zero filled (calloc/cmemalign) ?
    986         } // malloc_zero_fill
    987 
    988 
    989     // The malloc_stats() function prints (on default standard error) statistics about memory allocated by malloc(3) and
    990     // related functions.
     1005                } // malloc_zero_fill
     1006
     1007
     1008        // #comment TD : Document this function
    9911009        void malloc_stats( void ) {
    9921010                #ifdef __STATISTICS__
    993                 printStats();
    994                 if ( checkFree() ) checkFree( heapManager );
    995                 #endif // __STATISTICS__
    996         } // malloc_stats
    997 
    998         // The malloc_stats_fd() function changes the file descripter where malloc_stats() writes the statistics.
    999         int malloc_stats_fd( int fd ) {
    1000                 #ifdef __STATISTICS__
    1001                 int temp = statfd;
    1002                 statfd = fd;
    1003                 return temp;
     1011                        printStats();
     1012                        if ( checkFree() ) checkFree( heapManager );
     1013                #endif // __STATISTICS__
     1014                } // malloc_stats
     1015
     1016        // #comment TD : Document this function
     1017                int malloc_stats_fd( int fd ) {
     1018                #ifdef __STATISTICS__
     1019                        int temp = statfd;
     1020                        statfd = fd;
     1021                        return temp;
    10041022                #else
    1005                 return -1;
    1006                 #endif // __STATISTICS__
    1007         } // malloc_stats_fd
    1008 
    1009         // The malloc_info() function exports an XML string that describes the current state of the memory-allocation
    1010         // implementation in the caller.  The string is printed on the file stream stream.  The exported string includes
    1011         // information about all arenas (see malloc(3)).
     1023                        return -1;
     1024                #endif // __STATISTICS__
     1025                } // malloc_stats_fd
     1026
     1027
     1028        // #comment TD : Document this function
    10121029        int malloc_info( int options, FILE * stream ) {
    10131030                return printStatsXML( stream );
     
    10151032
    10161033
    1017         // The malloc_get_state() function records the current state of all malloc(3) internal bookkeeping variables (but
    1018         // not the actual contents of the heap or the state of malloc_hook(3) functions pointers).  The state is recorded in
    1019         // a system-dependent opaque data structure dynamically allocated via malloc(3), and a pointer to that data
    1020         // structure is returned as the function result.  (It is the caller's responsibility to free(3) this memory.)
     1034        // #comment TD : What are these two functions for?
    10211035        void * malloc_get_state( void ) {
    1022                 return 0;                                                                               // unsupported
     1036                return 0;
    10231037        } // malloc_get_state
    10241038
    1025 
    1026         // The malloc_set_state() function restores the state of all malloc(3) internal bookkeeping variables to the values
    1027         // recorded in the opaque data structure pointed to by state.
    10281039        int malloc_set_state( void * ptr ) {
    1029                 return 0;                                                                               // unsupported
     1040                return 0;
    10301041        } // malloc_set_state
    10311042} // extern "C"
  • src/Parser/lex.ll

    rbedb40e r0cf9ffd  
    1010 * Created On       : Sat Sep 22 08:58:10 2001
    1111 * Last Modified By : Peter A. Buhr
    12  * Last Modified On : Wed Aug 29 15:02:41 2018
    13  * Update Count     : 686
     12 * Last Modified On : Wed Aug  8 17:23:17 2018
     13 * Update Count     : 685
    1414 */
    1515
     
    410410">>="                   { NAMEDOP_RETURN(RSassign); }
    411411
     412"~="                    { NAMEDOP_RETURN(Erange); }                             // CFA
    412413"@="                    { NAMEDOP_RETURN(ATassign); }                   // CFA
    413 "~="                    { NAMEDOP_RETURN(ErangeUpEq); }                 // CFA
    414 "-~"                    { NAMEDOP_RETURN(ErangeDown); }                 // CFA
    415 "-~="                   { NAMEDOP_RETURN(ErangeDownEq); }               // CFA
    416414
    417415                                /* CFA, operator identifier */
  • src/Parser/parser.yy

    rbedb40e r0cf9ffd  
    1010// Created On       : Sat Sep  1 20:22:55 2001
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 30 17:02:25 2018
    13 // Update Count     : 4029
     12// Last Modified On : Wed Aug  8 17:50:07 2018
     13// Update Count     : 3998
    1414//
    1515
     
    186186} // fieldDecl
    187187
    188 ExpressionNode *forInc( const OperKinds op ) {
    189         return new ExpressionNode( build_constantInteger( *new string( op == OperKinds::LThan || op == OperKinds::LEThan ? "1" : "-1" ) ) );
    190 } // forInc
    191 
    192188ForCtrl * forCtrl( ExpressionNode * type, string * index, ExpressionNode * start, enum OperKinds compop, ExpressionNode * comp, ExpressionNode * inc ) {
    193         ConstantExpr *constant = dynamic_cast<ConstantExpr *>(type->get_expr());
    194         if ( constant && (constant->get_constant()->get_value() == "0" || constant->get_constant()->get_value() == "1") ) {
    195         type = new ExpressionNode( new CastExpr( maybeMoveBuild< Expression >(type), new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ) );
    196         } // if
    197189        return new ForCtrl(
    198190                distAttr( DeclarationNode::newTypeof( type ), DeclarationNode::newName( index )->addInitializer( new InitializerNode( start ) ) ),
     
    222214
    223215// Types declaration for productions
    224 %union {
     216%union
     217{
    225218        Token tok;
    226219        ParseNode * pn;
     
    297290%token ANDassign        ERassign        ORassign                                // &=   ^=      |=
    298291
    299 %token ErangeUpEq       ErangeDown      ErangeDownEq                    // ~=   -~      -~=
     292%token Erange                                                                                   // ~=
    300293%token ATassign                                                                                 // @=
    301294
     
    11451138                                $$ = new ForCtrl( (ExpressionNode * )nullptr, (ExpressionNode * )nullptr, (ExpressionNode * )nullptr );
    11461139                        } else {
    1147                                 $$ = forCtrl( $1, new string( DeclarationNode::anonymous.newName() ), new ExpressionNode( build_constantInteger( *new string( "0" ) ) ),
    1148                                                           OperKinds::LThan, $1->clone(), forInc( OperKinds::LThan ) );
     1140                                $$ = forCtrl( $1, new string( DeclarationNode::anonymous.newName() ), new ExpressionNode( build_constantInteger( *new string( "0" ) ) ), OperKinds::LThan, $1->clone(),
     1141                                                         new ExpressionNode( build_constantInteger( *new string( "1" ) ) ) );
    11491142                        } // if
    11501143                }
    11511144        | constant_expression inclexcl constant_expression      // CFA
    1152                 { $$ = forCtrl( $1, new string( DeclarationNode::anonymous.newName() ), $1->clone(), $2, $3, forInc( $2 ) ); }
     1145                { $$ = forCtrl( $1, new string( DeclarationNode::anonymous.newName() ), $1->clone(), $2, $3, new ExpressionNode( build_constantInteger( *new string( "1" ) ) ) ); }
    11531146        | constant_expression inclexcl constant_expression '~' constant_expression // CFA
    11541147                { $$ = forCtrl( $1, new string( DeclarationNode::anonymous.newName() ), $1->clone(), $2, $3, $5 ); }
     
    11611154                        } else {
    11621155                                if ( NameExpr *identifier = dynamic_cast<NameExpr *>($1->get_expr()) ) {
    1163                                         $$ = forCtrl( $3, new string( identifier->name ), new ExpressionNode( build_constantInteger( *new string( "0" ) ) ),
    1164                                                                   OperKinds::LThan, $3->clone(), forInc( OperKinds::LThan ) );
     1156                                        $$ = forCtrl( $3, new string( identifier->name ), new ExpressionNode( build_constantInteger( *new string( "0" ) ) ), OperKinds::LThan, $3->clone(),
     1157                                                                 new ExpressionNode( build_constantInteger( *new string( "1" ) ) ) );
    11651158                                } else {
    11661159                                        SemanticError( yylloc, "Expression disallowed. Only loop-index name allowed" ); $$ = nullptr;
     
    11741167                        } else {
    11751168                                if ( NameExpr *identifier = dynamic_cast<NameExpr *>($1->get_expr()) ) {
    1176                                         $$ = forCtrl( $3, new string( identifier->name ), $3->clone(), $4, $5, forInc( $4 ) );
     1169                                        $$ = forCtrl( $3, new string( identifier->name ), $3->clone(), $4, $5, new ExpressionNode( build_constantInteger( *new string( "1" ) ) ) );
    11771170                                } else {
    11781171                                        SemanticError( yylloc, "Expression disallowed. Only loop-index name allowed" ); $$ = nullptr;
     
    12011194        '~'
    12021195                { $$ = OperKinds::LThan; }
    1203         | ErangeUpEq
     1196        | Erange
    12041197                { $$ = OperKinds::LEThan; }
    1205         | ErangeDown
    1206                 { $$ = OperKinds::GThan; }
    1207         | ErangeDownEq
    1208                 { $$ = OperKinds::GEThan; }
    12091198        ;
    12101199
  • src/SymTab/Validate.cc

    rbedb40e r0cf9ffd  
    403403                        assert( aggr ); // TODO: need to handle forward declarations
    404404                        for ( Declaration * member : aggr->members ) {
    405                                 if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( child ) ) {
     405                                if ( StructInstType * inst = dynamic_cast< StructInstType * >( child ) ) {
     406                                        if ( StructDecl * aggr = dynamic_cast< StructDecl * >( member ) ) {
     407                                                if ( aggr->name == inst->name ) {
     408                                                        // TODO: is this case, and other non-TypeInstType cases, necessary?
     409                                                        return new StructInstType( qualType->get_qualifiers(), aggr );
     410                                                }
     411                                        }
     412                                } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( child ) ) {
     413                                        if ( UnionDecl * aggr = dynamic_cast< UnionDecl * > ( member ) ) {
     414                                                if ( aggr->name == inst->name ) {
     415                                                        return new UnionInstType( qualType->get_qualifiers(), aggr );
     416                                                }
     417                                        }
     418                                } else if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( child ) ) {
     419                                        if ( EnumDecl * aggr = dynamic_cast< EnumDecl * > ( member ) ) {
     420                                                if ( aggr->name == inst->name ) {
     421                                                        return new EnumInstType( qualType->get_qualifiers(), aggr );
     422                                                }
     423                                        }
     424                                } else if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( child ) ) {
    406425                                        // name on the right is a typedef
    407426                                        if ( NamedTypeDecl * aggr = dynamic_cast< NamedTypeDecl * > ( member ) ) {
     
    410429                                                        Type * ret = aggr->base->clone();
    411430                                                        ret->get_qualifiers() = qualType->get_qualifiers();
    412                                                         TypeSubstitution sub = parent->genericSubstitution();
    413                                                         sub.apply(ret);
    414431                                                        return ret;
    415432                                                }
  • tests/.expect/forctrl.txt

    rbedb40e r0cf9ffd  
    22empty
    33empty
    4 
    5 A
    6 A A
    7 A A A A A A A A A A
    8 B B B B B
    9 C C C C C
    10 D D D D D
    11 E E E E E
     4X X X X X X X X X X
     5Y Y Y Y Y
     6Z Z Z Z Z
    1270 1 2 3 4 5 6 7 8 9
    1380 1 2 3 4 5 6 7 8 9
    14 1 3 5 7 9
    15 10 8 6 4 2
    1690.5 1.5 2.5 3.5 4.5
    17 5.5 4.5 3.5 2.5 1.5
    18102 4 6 8 10
    19 10 8 6 4 2
     112 4 6 8 10
    20123 6 9
    2113(0 0)(1 1)(2 2)(3 3)(4 4)(5 5)(6 6)(7 7)(8 8)(9 9)
  • tests/forctrl.c

    rbedb40e r0cf9ffd  
    1010// Created On       : Wed Aug  8 18:32:59 2018
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 30 17:12:12 2018
    13 // Update Count     : 43
     12// Last Modified On : Thu Aug 16 09:25:47 2018
     13// Update Count     : 6
    1414//
    1515
     
    3333        do { sout | "empty"; break; } while (); sout | endl;
    3434        for () { sout | "empty"; break; }               sout | endl;
    35 
    36         for ( 0 ) { sout | "A"; }                               sout | endl;
    37         for ( 1 ) { sout | "A"; }                               sout | endl;
    38         for ( 2 ) { sout | "A"; }                               sout | endl;
    39         for ( 10 ) { sout | "A"; }                              sout | endl;
    40 
    41         for ( 1 ~= 10 ~ 2 ) { sout | "B"; }             sout | endl;
    42         for ( 10 -~= 1 ~ -2 ) { sout | "C"; }   sout | endl;
    43         for ( 0.5 ~ 5.5 ) { sout | "D"; }               sout | endl;
    44         for ( 5.5 -~ 0.5 ) { sout | "E"; }              sout | endl;
    45 
     35        for ( 10 ) { sout | "X"; }                              sout | endl;
     36        for ( 0.5 ~ 5.5 ) { sout | "Y"; }               sout | endl;
     37        for ( 2 ~= 10 ~ 2 ) { sout | "Z"; }             sout | endl;
    4638        for ( i; 10 ) { sout | i; }                             sout | endl;
    4739        for ( j; 10 ) { sout | j; }                             sout | endl;
    48 
    49         for ( i; 1 ~= 10 ~ 2 ) { sout | i; }    sout | endl;
    50         for ( i; 10 -~= 1 ~ -2 ) { sout | i; }  sout | endl;
    5140        for ( i; 0.5 ~ 5.5 ) { sout | i; }              sout | endl;
    52         for ( i; 5.5 -~ 0.5 ) { sout | i; }             sout | endl;
    53 
     41        for ( i; 2 ~= 10 ~ 2 ) { sout | i; }    sout | endl;
    5442        for ( ui; 2u ~= 10u ~ 2u ) { sout | ui; } sout | endl;
    55         for ( ui; 10u -~= 2u ~ -2u ) { sout | ui; } sout | endl;
    56 
    5743        int start = 3, comp = 10, inc = 2;
    5844        for ( i; start ~ comp ~ inc + 1 ) { sout | i; } sout | endl;
Note: See TracChangeset for help on using the changeset viewer.