// // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // stdlib -- // // Author : Peter A. Buhr // Created On : Thu Jan 28 17:12:35 2016 // Last Modified By : Peter A. Buhr // Last Modified On : Fri Nov 29 23:08:02 2019 // Update Count : 400 // #pragma once #include "bits/defs.hfa" #include "bits/align.hfa" #include // *alloc, strto*, ato* extern "C" { void * memalign( size_t align, size_t size ); // malloc.h void * memset( void * dest, int fill, size_t size ); // string.h void * memcpy( void * dest, const void * src, size_t size ); // string.h void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap } // extern "C" void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap //--------------------------------------- #ifndef EXIT_FAILURE #define EXIT_FAILURE 1 // failing exit status #define EXIT_SUCCESS 0 // successful exit status #endif // ! EXIT_FAILURE //--------------------------------------- static inline forall( dtype T | sized(T) ) { // C dynamic allocation T * malloc( void ) { if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc else return (T *)memalign( _Alignof(T), sizeof(T) ); } // malloc T * calloc( size_t dim ) { if ( _Alignof(T) <= libAlign() )return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) ); } // calloc T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast return (T *)(void *)realloc( (void *)ptr, size ); // C realloc } // realloc T * memalign( size_t align ) { return (T *)memalign( align, sizeof(T) ); // C memalign } // memalign T * cmemalign( size_t align, size_t dim ) { return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign } // cmemalign T * aligned_alloc( size_t align ) { return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc } // aligned_alloc int posix_memalign( T ** ptr, size_t align ) { return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign } // posix_memalign // Cforall dynamic allocation T * alloc( void ) { return malloc(); } // alloc T * alloc( size_t dim ) { if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); else return (T *)memalign( _Alignof(T), dim * sizeof(T) ); } // alloc T * alloc( T ptr[], size_t dim ) { // realloc return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc } // alloc T * alloc_set( char fill ) { return (T *)memset( (T *)alloc(), (int)fill, sizeof(T) ); // initialize with fill value } // alloc T * alloc_set( T fill ) { return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value } // alloc T * alloc_set( size_t dim, char fill ) { return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value } // alloc T * alloc_set( size_t dim, T fill ) { T * r = (T *)alloc( dim ); for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value return r; } // alloc T * alloc_set( size_t dim, const T fill[] ) { return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value } // alloc } // distribution forall( dtype T | sized(T) ) { T * alloc_set( T ptr[], size_t dim, char fill ); // realloc array with fill } // distribution static inline forall( dtype T | sized(T) ) { T * alloc_align( size_t align ) { return (T *)memalign( align, sizeof(T) ); } // alloc_align T * alloc_align( size_t align, size_t dim ) { return (T *)memalign( align, dim * sizeof(T) ); } // alloc_align T * alloc_align( T ptr[], size_t align ) { // aligned realloc array return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc } // alloc_align T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array return (T *)(void *)realloc( (void *)ptr, align, dim * sizeof(T) ); // CFA realloc } // alloc_align T * alloc_align_set( size_t align, char fill ) { return (T *)memset( (T *)alloc_align( align ), (int)fill, sizeof(T) ); // initialize with fill value } // alloc_align T * alloc_align_set( size_t align, T fill ) { return (T *)memcpy( (T *)alloc_align( align ), &fill, sizeof(T) ); // initialize with fill value } // alloc_align T * alloc_align_set( size_t align, size_t dim, char fill ) { return (T *)memset( (T *)alloc_align( align, dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value } // alloc_align T * alloc_align_set( size_t align, size_t dim, T fill ) { T * r = (T *)alloc_align( align, dim ); for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value return r; } // alloc_align T * alloc_align_set( size_t align, size_t dim, const T fill[] ) { return (T *)memcpy( (T *)alloc_align( align, dim ), fill, dim * sizeof(T) ); } // alloc_align } // distribution forall( dtype T | sized(T) ) { T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill } // distribution static inline forall( dtype T | sized(T) ) { // data, non-array types T * memset( T * dest, char fill ) { return (T *)memset( dest, fill, sizeof(T) ); } // memset T * memcpy( T * dest, const T * src ) { return (T *)memcpy( dest, src, sizeof(T) ); } // memcpy } // distribution static inline forall( dtype T | sized(T) ) { // data, array types T * amemset( T dest[], char fill, size_t dim ) { return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset } // amemset T * amemcpy( T dest[], const T src[], size_t dim ) { return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy } // amemcpy } // distribution // allocation/deallocation and constructor/destructor, non-array types forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr ); forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest ); // allocation/deallocation and constructor/destructor, array types forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] ); forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest ); //--------------------------------------- static inline { int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); } unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); } long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); } unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); } long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); } unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); } float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); } double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); } long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); } } // distribution float _Complex strto( const char * sptr, char ** eptr ); double _Complex strto( const char * sptr, char ** eptr ); long double _Complex strto( const char * sptr, char ** eptr ); static inline { int ato( const char * sptr ) { return (int)strtol( sptr, 0p, 10 ); } unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0p, 10 ); } long int ato( const char * sptr ) { return strtol( sptr, 0p, 10 ); } unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0p, 10 ); } long long int ato( const char * sptr ) { return strtoll( sptr, 0p, 10 ); } unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0p, 10 ); } float ato( const char * sptr ) { return strtof( sptr, 0p ); } double ato( const char * sptr ) { return strtod( sptr, 0p ); } long double ato( const char * sptr ) { return strtold( sptr, 0p ); } float _Complex ato( const char * sptr ) { return strto( sptr, 0p ); } double _Complex ato( const char * sptr ) { return strto( sptr, 0p ); } long double _Complex ato( const char * sptr ) { return strto( sptr, 0p ); } } // distribution //--------------------------------------- forall( otype E | { int ?