// // 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 : Wed May 16 07:53:10 2018 // Update Count : 300 // #pragma once #include // strto*, *abs //--------------------------------------- #ifndef EXIT_FAILURE #define EXIT_FAILURE 1 // failing exit status #define EXIT_SUCCESS 0 // successful exit status #endif // ! EXIT_FAILURE //--------------------------------------- // C dynamic allocation static inline forall( dtype T | sized(T) ) { T * malloc( void ) { // printf( "* malloc\n" ); 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 ) { //printf( "X2\n" ); return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc } // calloc T * realloc( T * ptr, size_t size ) { //printf( "X3\n" ); return (T *)(void *)realloc( (void *)ptr, size ); } // realloc extern "C" { void * memalign( size_t align, size_t size ); } // use default C routine for void * T * memalign( size_t align ) { //printf( "X4\n" ); return (T *)memalign( align, sizeof(T) ); } // memalign extern "C" { void * aligned_alloc( size_t align, size_t size ); } // use default C routine for void * T * aligned_alloc( size_t align ) { //printf( "X5\n" ); return (T *)aligned_alloc( align, sizeof(T) ); } // aligned_alloc int posix_memalign( T ** ptr, size_t align ) { //printf( "X6\n" ); return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign } // posix_memalign // Cforall dynamic allocation extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void * T * alloc( void ) { //printf( "X7\n" ); return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc } // alloc T * alloc( char fill ) { //printf( "X8\n" ); 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 ) { //printf( "X9\n" ); return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc } // alloc T * alloc( size_t dim, char fill ) { //printf( "X10\n" ); 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 ) { //printf( "X11\n" ); 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 ) { //printf( "X13\n" ); return (T *)memalign( align, sizeof(T) ); } // align_alloc static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill ) { //printf( "X14\n" ); T * ptr = (T *)memalign( align, sizeof(T) ); return (T *)memset( ptr, (int)fill, sizeof(T) ); } // align_alloc static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim ) { //printf( "X15\n" ); return (T *)memalign( align, dim * sizeof(T) ); } // align_alloc static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim, char fill ) { //printf( "X16\n" ); T * ptr = (T *)memalign( align, dim * sizeof(T) ); return (T *)memset( ptr, (int)fill, dim * sizeof(T) ); } // align_alloc // data, non-array types static inline forall( dtype T | sized(T) ) T * memset( T * dest, char c ) { //printf( "X17\n" ); return (T *)memset( dest, c, sizeof(T) ); } // memset extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } // use default C routine for void * static inline forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src ) { //printf( "X18\n" ); return (T *)memcpy( dest, src, sizeof(T) ); } // memcpy // data, array types static inline forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ) { //printf( "X19\n" ); return (T *)(void *)memset( dest, c, dim * sizeof(T) ); // C memset } // memset static inline forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ) { //printf( "X20\n" ); return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy } // memcpy // 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 ); } static inline unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); } static inline long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); } static inline unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); } static inline long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); } static inline unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); } static inline float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); } static inline double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); } static inline long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); } 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 ); } static inline unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); } static inline long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); } static inline unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); } static inline long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); } static inline unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); } static inline float ato( const char * sptr ) { return strtof( sptr, 0 ); } static inline double ato( const char * sptr ) { return strtod( sptr, 0 ); } static inline long double ato( const char * sptr ) { return strtold( sptr, 0 ); } static inline float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } static inline double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } static inline long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); } //--------------------------------------- forall( otype E | { int ??( T, T ); } ) static inline T max( T t1, T t2 ) { return t1 > t2 ? t1 : t2; } forall( otype T | { T min( T, T ); T max( T, T ); } ) static inline T clamp( T value, T min_val, T max_val ) { return max( min_val, min( value, max_val ) ); } forall( otype T ) static inline void swap( T & v1, T & v2 ) { T temp = v1; v1 = v2; v2 = temp; } // Local Variables: // // mode: c // // tab-width: 4 // // End: //