Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.cfa

    r89124ff r2026bb6  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Oct 22 08:57:52 2019
    13 // Update Count     : 478
     12// Last Modified On : Mon Jun 24 17:34:44 2019
     13// Update Count     : 462
    1414//
    1515
     
    2121#include <string.h>                                                                             // memcpy, memset
    2222#include <malloc.h>                                                                             // malloc_usable_size
    23 //#include <math.h>                                                                             // fabsf, fabs, fabsl
     23#include <math.h>                                                                               // fabsf, fabs, fabsl
    2424#include <complex.h>                                                                    // _Complex_I
    2525#include <assert.h>
     
    2727//---------------------------------------
    2828
    29 forall( dtype T | sized(T) ) {
    30         T * alloc_set( T ptr[], size_t dim, char fill ) {       // realloc array with fill
    31                 size_t olen = malloc_usable_size( ptr );                // current allocation
    32                 char * nptr = (char *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
    33                 size_t nlen = malloc_usable_size( nptr );               // new allocation
    34                 if ( nlen > olen ) {                                                    // larger ?
    35                         memset( nptr + olen, (int)fill, nlen - olen ); // initialize added storage
    36                 } // if
    37                 return (T *)nptr;
    38         } // alloc_set
    39 
    40         T * alloc_align( T ptr[], size_t align ) {                      // aligned realloc array
    41                 char * nptr;
    42                 size_t alignment = malloc_alignment( ptr );
    43                 if ( align != alignment && (uintptr_t)ptr % align != 0 ) {
    44                         size_t olen = malloc_usable_size( ptr );        // current allocation
    45                         nptr = (char *)memalign( align, olen );
    46                         size_t nlen = malloc_usable_size( nptr );       // new allocation
    47                         size_t lnth = olen < nlen ? olen : nlen;        // min
    48                         memcpy( nptr, ptr, lnth );                                      // initialize storage
    49                         free( ptr );
    50                 } else {
    51                         nptr = (char *)ptr;
    52                 } // if
    53                 return (T *)nptr;
    54         } // alloc_align
    55 
    56         T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array
    57                 char * nptr;
    58                 size_t alignment = malloc_alignment( ptr );
    59                 if ( align != alignment ) {
    60                         size_t olen = malloc_usable_size( ptr );        // current allocation
    61                         nptr = (char *)memalign( align, dim * sizeof(T) );
    62                         size_t nlen = malloc_usable_size( nptr );       // new allocation
    63                         size_t lnth = olen < nlen ? olen : nlen;        // min
    64                         memcpy( nptr, ptr, lnth );                                      // initialize storage
    65                         free( ptr );
    66                 } else {
    67                         nptr = (char *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
    68                 } // if
    69                 return (T *)nptr;
    70         } // alloc_align
    71 
    72         T * alloc_align_set( T ptr[], size_t align, char fill ) { // aligned realloc with fill
    73                 size_t olen = malloc_usable_size( ptr );                // current allocation
    74                 char * nptr = alloc_align( ptr, align );
    75                 size_t nlen = malloc_usable_size( nptr );               // new allocation
    76                 if ( nlen > olen ) {                                                    // larger ?
    77                         memset( nptr + olen, (int)fill, nlen - olen ); // initialize added storage
    78                 } // if
    79                 return (T *)nptr;
    80         } // alloc_align_set
    81 } // distribution
     29// resize, non-array types
     30forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ) {
     31        size_t olen = malloc_usable_size( ptr );                        // current allocation
     32    char * nptr = (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
     33        size_t nlen = malloc_usable_size( nptr );                       // new allocation
     34        if ( nlen > olen ) {                                                            // larger ?
     35                memset( nptr + olen, (int)fill, nlen - olen );  // initialize added storage
     36        } //
     37    return (T *)nptr;
     38} // alloc
    8239
    8340// allocation/deallocation and constructor/destructor, non-array types
    8441forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
    8542T * new( Params p ) {
    86         return &(*malloc()){ p };                                                       // run constructor
     43        return &(*malloc()){ p };                                                               // run constructor
    8744} // new
    8845
     
    9047void delete( T * ptr ) {
    9148        if ( ptr ) {                                                                            // ignore null
    92                 ^(*ptr){};                                                                              // run destructor
     49                ^(*ptr){};                                                                                      // run destructor
    9350                free( ptr );
    9451        } // if
     
    9855void delete( T * ptr, Params rest ) {
    9956        if ( ptr ) {                                                                            // ignore null
    100                 ^(*ptr){};                                                                              // run destructor
     57                ^(*ptr){};                                                                                      // run destructor
    10158                free( ptr );
    10259        } // if
Note: See TracChangeset for help on using the changeset viewer.