// // 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 Jul 27 07:21:36 2018 // Update Count : 345 // #pragma once #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 } // extern "C" //--------------------------------------- #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 ) { return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc } // malloc // T & malloc( void ) { // int & p = *(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc // printf( "& malloc %p\n", &p ); // return p; // // return (T &)*(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc // } // malloc T * calloc( size_t dim ) { return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc } // calloc T * realloc( T * ptr, size_t size ) { return (T *)(void *)realloc( (void *)ptr, size ); } // realloc T * memalign( size_t align ) { return (T *)memalign( align, sizeof(T) ); } // memalign T * aligned_alloc( size_t align ) { return (T *)aligned_alloc( align, sizeof(T) ); } // 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 (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc } // alloc T * alloc( char fill ) { T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc return (T *)memset( ptr, (int)fill, sizeof(T) ); // initial with fill value } // alloc T * alloc( size_t dim ) { return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc } // alloc T * alloc( size_t dim, char fill ) { T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc return (T *)memset( ptr, (int)fill, dim * sizeof(T) ); // initial with fill value } // alloc T * alloc( T ptr[], size_t dim ) { return (T *)(void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc } // alloc } // distribution forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ); static inline forall( dtype T | sized(T) ) { T * align_alloc( size_t align ) { return (T *)memalign( align, sizeof(T) ); } // align_alloc T * align_alloc( size_t align, char fill ) { T * ptr = (T *)memalign( align, sizeof(T) ); return (T *)memset( ptr, (int)fill, sizeof(T) ); } // align_alloc T * align_alloc( size_t align, size_t dim ) { return (T *)memalign( align, dim * sizeof(T) ); } // align_alloc T * align_alloc( size_t align, size_t dim, char fill ) { T * ptr; if ( fill == '\0' ) { ptr = (T *)cmemalign( align, dim, sizeof(T) ); } else { ptr = (T *)memalign( align, dim * sizeof(T) ); return (T *)memset( ptr, (int)fill, dim * sizeof(T) ); } // if return ptr; } // align_alloc } // 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, 0, 10 ); } unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); } long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); } unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); } long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); } unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); } float ato( const char * sptr ) { return strtof( sptr, 0 ); } double ato( const char * sptr ) { return strtod( sptr, 0 ); } long double ato( const char * sptr ) { return strtold( sptr, 0 ); } float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } } // distribution //--------------------------------------- forall( otype E | { int ?