source: src/libcfa/stdlib @ 523232d

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 523232d was cdbfab0, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Remove unsafe void * constructors and assignment operators from prelude [closes #24] [fixes #51]

  • Property mode set to 100644
File size: 9.1 KB
RevLine 
[bd85400]1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[bb82c03]7// stdlib --
[bd85400]8//
9// Author           : Peter A. Buhr
10// Created On       : Thu Jan 28 17:12:35 2016
11// Last Modified By : Peter A. Buhr
[0dc954b]12// Last Modified On : Tue Oct 31 13:47:24 2017
13// Update Count     : 245
[bd85400]14//
15
[53a6c2a]16#pragma once
[17e5e2b]17
[bd85400]18//---------------------------------------
19
[45161b4d]20#ifndef EXIT_FAILURE
21#define EXIT_FAILURE    1                                                               // failing exit status
22#define EXIT_SUCCESS    0                                                               // successful exit status
23#endif // ! EXIT_FAILURE
24
25//---------------------------------------
26
[f3fc631f]27// allocation, non-array types
28static inline forall( dtype T | sized(T) ) T * malloc( void ) {
[bfc0f40]29        // printf( "* malloc\n" );
[f3fc631f]30        return (T *)(void *)malloc( (size_t)sizeof(T) );        // C malloc
31} // malloc
32
[bfc0f40]33// static inline forall( dtype T | sized(T) ) T & malloc( void ) {
34//      int & p = *(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
35//      printf( "& malloc %p\n", &p );
36//      return p;
37// //   return (T &)*(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
38// } // malloc
39
[6065b3aa]40extern "C" { void * calloc( size_t dim, size_t size ); } // default C routine
[f3fc631f]41static inline forall( dtype T | sized(T) ) T * calloc( size_t dim ) {
[6065b3aa]42        //printf( "X2\n" );
[f3fc631f]43        return (T *)(void *)calloc( dim, sizeof(T) );           // C cmalloc
44}
45
[6065b3aa]46extern "C" { void * realloc( void * ptr, size_t size ); } // default C routine for void *
[f3fc631f]47static inline forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) {
[6065b3aa]48        //printf( "X3\n" );
[f3fc631f]49        return (T *)(void *)realloc( (void *)ptr, size );
50}
[6065b3aa]51
52extern "C" { void * memalign( size_t align, size_t size ); } // use default C routine for void *
53static inline forall( dtype T | sized(T) ) T * memalign( size_t align ) {
54        //printf( "X4\n" );
55        return (T *)memalign( align, sizeof(T) );
56} // memalign
57
58static inline forall( dtype T | sized(T) ) T * aligned_alloc( size_t align ) {
59        //printf( "X5\n" );
60        return (T *)memalign( align, sizeof(T) );
61} // aligned_alloc
62
63extern "C" { int posix_memalign( void ** ptr, size_t align, size_t size ); } // use default C routine for void *
64static inline forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t align ) {
65        //printf( "X6\n" );
66        return posix_memalign( (void **)ptr, align, sizeof(T) );
67} // posix_memalign
68
69
70extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void *
71
72static inline forall( dtype T | sized(T) ) T * alloc( void ) {
[f3fc631f]73        //printf( "X7\n" );
[6065b3aa]74        return (T *)(void *)malloc( (size_t)sizeof(T) );        // C malloc
75} // alloc
76static inline forall( dtype T | sized(T) ) T * alloc( char fill ) {
[f3fc631f]77        //printf( "X8\n" );
[6065b3aa]78        T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );     // C malloc
[cdbfab0]79    return (T *)memset( ptr, (int)fill, sizeof(T) );                    // initial with fill value
[6065b3aa]80} // alloc
[f3fc631f]81
[6065b3aa]82static inline forall( dtype T | sized(T) ) T * alloc( size_t dim ) {
[f3fc631f]83        //printf( "X9\n" );
[6065b3aa]84        return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
85} // alloc
86static inline forall( dtype T | sized(T) ) T * alloc( size_t dim, char fill ) {
[f3fc631f]87        //printf( "X10\n" );
[6065b3aa]88        T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
[cdbfab0]89    return (T *)memset( ptr, (int)fill, dim * sizeof(T) );
[6065b3aa]90} // alloc
[f3fc631f]91
[6065b3aa]92static inline forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim ) {
[f3fc631f]93        //printf( "X11\n" );
[cdbfab0]94        return (T *)(void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
[6065b3aa]95} // alloc
96forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );
97
98static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align ) {
[f3fc631f]99        //printf( "X13\n" );
[6065b3aa]100        return (T *)memalign( align, sizeof(T) );
101} // align_alloc
102static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill ) {
[f3fc631f]103        //printf( "X14\n" );
[6065b3aa]104    T * ptr = (T *)memalign( align, sizeof(T) );
[cdbfab0]105    return (T *)memset( ptr, (int)fill, sizeof(T) );
[6065b3aa]106} // align_alloc
[f3fc631f]107
[6065b3aa]108static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim ) {
[f3fc631f]109        //printf( "X15\n" );
[6065b3aa]110        return (T *)memalign( align, dim * sizeof(T) );
111} // align_alloc
112static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim, char fill ) {
[f3fc631f]113        //printf( "X16\n" );
[6065b3aa]114    T * ptr = (T *)memalign( align, dim * sizeof(T) );
[cdbfab0]115    return (T *)memset( ptr, (int)fill, dim * sizeof(T) );
[6065b3aa]116} // align_alloc
117
[f3fc631f]118
119// data, non-array types
120static inline forall( dtype T | sized(T) ) T * memset( T * dest, char c ) {
121        //printf( "X17\n" );
[cdbfab0]122        return (T *)memset( dest, c, sizeof(T) );
[f3fc631f]123} // memset
124extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } // use default C routine for void *
125static inline forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src ) {
126        //printf( "X18\n" );
[cdbfab0]127        return (T *)memcpy( dest, src, sizeof(T) );
[f3fc631f]128} // memcpy
129
130// data, array types
[6065b3aa]131static inline forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ) {
[f3fc631f]132        //printf( "X19\n" );
[cdbfab0]133        return (T *)(void *)memset( dest, c, dim * sizeof(T) ); // C memset
[6065b3aa]134} // memset
135static inline forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ) {
[f3fc631f]136        //printf( "X20\n" );
[cdbfab0]137        return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
[6065b3aa]138} // memcpy
[f3fc631f]139
[6065b3aa]140// allocation/deallocation and constructor/destructor, non-array types
[aca65621]141forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
[83a071f9]142forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
143forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
[627f585]144
[6065b3aa]145// allocation/deallocation and constructor/destructor, array types
[aca65621]146forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
147forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
148forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
[6065b3aa]149
[bd85400]150//---------------------------------------
151
152int ato( const char * ptr );
153unsigned int ato( const char * ptr );
154long int ato( const char * ptr );
155unsigned long int ato( const char * ptr );
156long long int ato( const char * ptr );
157unsigned long long int ato( const char * ptr );
158float ato( const char * ptr );
159double ato( const char * ptr );
160long double ato( const char * ptr );
161float _Complex ato( const char * ptr );
162double _Complex ato( const char * ptr );
163long double _Complex ato( const char * ptr );
164
165int strto( const char * sptr, char ** eptr, int base );
166unsigned int strto( const char * sptr, char ** eptr, int base );
167long int strto( const char * sptr, char ** eptr, int base );
168unsigned long int strto( const char * sptr, char ** eptr, int base );
169long long int strto( const char * sptr, char ** eptr, int base );
170unsigned long long int strto( const char * sptr, char ** eptr, int base );
171float strto( const char * sptr, char ** eptr );
172double strto( const char * sptr, char ** eptr );
173long double strto( const char * sptr, char ** eptr );
174float _Complex strto( const char * sptr, char ** eptr );
175double _Complex strto( const char * sptr, char ** eptr );
176long double _Complex strto( const char * sptr, char ** eptr );
177
178//---------------------------------------
179
[4040425]180forall( otype T | { int ?<?( T, T ); } )
[f3fc631f]181T * bsearch( T key, const T * arr, size_t dim );
[f3ddc21]182
[707446a]183forall( otype T | { int ?<?( T, T ); } )
[f3fc631f]184unsigned int bsearch( T key, const T * arr, size_t dim );
[bd85400]185
[f3ddc21]186
[4040425]187forall( otype T | { int ?<?( T, T ); } )
[f3fc631f]188void qsort( const T * arr, size_t dim );
[bd85400]189
190//---------------------------------------
191
[5ea26ed]192[ int, int ] div( int num, int denom );
193[ long int, long int ] div( long int num, long int denom );
194[ long long int, long long int ] div( long long int num, long long int denom );
[4040425]195forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
[5ea26ed]196[ T, T ] div( T num, T demon );
[bd85400]197
198//---------------------------------------
199
[c3ebf37]200unsigned char abs( signed char );
[6e991d6]201extern "C" { int abs( int ); }                                                  // use default C routine for int
[c3ebf37]202unsigned long int abs( long int );
203unsigned long long int abs( long long int );
[bd85400]204float abs( float );
205double abs( double );
206long double abs( long double );
[6e991d6]207float abs( float _Complex );
208double abs( double _Complex );
209long double abs( long double _Complex );
[aca65621]210forall( otype T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } )
[f3ddc21]211T abs( T );
[53ba273]212
213//---------------------------------------
214
[70e4895d]215void random_seed( long int s );
216char random( void );
217char random( char l, char u );
218int random( void );
219unsigned int random( void );
220unsigned int random( unsigned int u );
221unsigned int random( unsigned int l, unsigned int u );
222//long int random( void );
223unsigned long int random( void );
224unsigned long int random( unsigned long int u );
225unsigned long int random( unsigned long int l, unsigned long int u );
226float random( void );
227double random( void );
228float _Complex random( void );
229double _Complex random( void );
230long double _Complex random( void );
[bd85400]231
232//---------------------------------------
233
[4040425]234forall( otype T | { int ?<?( T, T ); } )
[a797e2b1]235T min( T t1, T t2 );
[bd85400]236
[4040425]237forall( otype T | { int ?>?( T, T ); } )
[a797e2b1]238T max( T t1, T t2 );
[bd85400]239
[a9f2c13]240forall( otype T | { T min( T, T ); T max( T, T ); } )
[a797e2b1]241T clamp( T value, T min_val, T max_val );
[a9f2c13]242
[4040425]243forall( otype T )
[cb811ac]244void swap( T & t1, T & t2 );
[bd85400]245
246// Local Variables: //
247// mode: c //
248// tab-width: 4 //
249// End: //
Note: See TracBrowser for help on using the repository browser.