Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.cfa

    r1911f37 rc0363be  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Sep 24 18:14:20 2025
    13 // Update Count     : 760
     12// Last Modified On : Sun Apr 21 16:17:22 2024
     13// Update Count     : 700
    1414//
    1515
     
    3333forall( T & | sized(T), Parms ... | { void ?{}( T &, Parms ); } )
    3434T * anew( size_t dim, Parms p ) {
    35         T * arr;
    36         if ( sizeof( T ) == 0 ) {                                                       // empty element size ?
    37                 arr = (T *)(size_t *)alloc();                                   // create fake array
    38                 *((size_t *)arr) = dim;                                                 // remember dimension
    39         } else arr = alloc( dim );
     35        T * arr = alloc( dim );
    4036        for ( i; dim ) {
    4137                (arr[i]){ p };                                                                  // run constructor
     
    4642forall( T & | sized(T) | { void ^?{}( T & ); } )
    4743void adelete( T arr[] ) {
    48   if ( arr == 0p ) return;                                                              // 0p ? special case
    49         size_t dim = sizeof( T ) == 0 ? (size_t)(*(size_t *)arr) : malloc_size( arr ) / sizeof( T ); // compute dimension
    50         for ( i; 0 -~= dim - 1 ) {                                                      // reverse allocation order, must be signed
    51                 ^(arr[i]){};                                                                    // run destructor
    52         } // for
    53         if ( sizeof( T ) == 0 ) free( (size_t *)arr ); else free( arr );
     44        if ( arr ) {                                                                            // ignore null
     45                size_t dim = malloc_size( arr ) / sizeof( T );
     46                for ( i; 0 -~= dim - 1 ) {                                              // reverse allocation order, must be unsigned
     47                        ^(arr[i]){};                                                            // run destructor
     48                } // for
     49                free( arr );
     50        } // if
    5451} // adelete
    5552
    5653forall( T & | sized(T) | { void ^?{}( T & ); }, List ... | { void adelete( List ); } )
    5754void adelete( T arr[], List rest ) {
    58         adelete( arr );
     55        if ( arr ) {                                                                            // ignore null
     56                size_t dim = malloc_size( arr ) / sizeof( T );
     57                for ( i; 0 -~= dim - 1 ) {                                              // reverse allocation order, must be unsigned
     58                        ^(arr[i]){};                                                            // run destructor
     59                } // for
     60                free( arr );
     61        } // if
    5962        adelete( rest );
    6063} // adelete
     
    6568
    6669bool checkif( const char s[], int (* kind)( int ) ) {
    67         for () {                                                                                        // at least 1 character '\0'
    68           if ( *s == '\0' ) return true;
    69           if ( ! kind( *s ) ) return false;
     70        for () {
     71                if ( *s == '\0' ) return true;
     72                if ( ! kind( *s ) ) return false;
    7073                s += 1;
    7174        } // for
     
    7376
    7477bool checkif( const char s[], int (* kind)( int, locale_t ), locale_t locale ) {
    75         for () {                                                                                        // at least 1 character '\0'
    76           if ( *s == '\0' ) return true;
    77           if ( ! kind( *s, locale ) ) return false;
     78        for () {
     79                if ( *s == '\0' ) return true;
     80                if ( ! kind( *s, locale ) ) return false;
    7881                s += 1;
    7982        } // for
Note: See TracChangeset for help on using the changeset viewer.