source: src/libcfa/stdlib @ cdbfab0

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since cdbfab0 was cdbfab0, checked in by Rob Schluntz <rschlunt@…>, 4 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
Line 
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//
7// stdlib --
8//
9// Author           : Peter A. Buhr
10// Created On       : Thu Jan 28 17:12:35 2016
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Oct 31 13:47:24 2017
13// Update Count     : 245
14//
15
16#pragma once
17
18//---------------------------------------
19
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
27// allocation, non-array types
28static inline forall( dtype T | sized(T) ) T * malloc( void ) {
29        // printf( "* malloc\n" );
30        return (T *)(void *)malloc( (size_t)sizeof(T) );        // C malloc
31} // malloc
32
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
40extern "C" { void * calloc( size_t dim, size_t size ); } // default C routine
41static inline forall( dtype T | sized(T) ) T * calloc( size_t dim ) {
42        //printf( "X2\n" );
43        return (T *)(void *)calloc( dim, sizeof(T) );           // C cmalloc
44}
45
46extern "C" { void * realloc( void * ptr, size_t size ); } // default C routine for void *
47static inline forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) {
48        //printf( "X3\n" );
49        return (T *)(void *)realloc( (void *)ptr, size );
50}
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 ) {
73        //printf( "X7\n" );
74        return (T *)(void *)malloc( (size_t)sizeof(T) );        // C malloc
75} // alloc
76static inline forall( dtype T | sized(T) ) T * alloc( char fill ) {
77        //printf( "X8\n" );
78        T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );     // C malloc
79    return (T *)memset( ptr, (int)fill, sizeof(T) );                    // initial with fill value
80} // alloc
81
82static inline forall( dtype T | sized(T) ) T * alloc( size_t dim ) {
83        //printf( "X9\n" );
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 ) {
87        //printf( "X10\n" );
88        T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
89    return (T *)memset( ptr, (int)fill, dim * sizeof(T) );
90} // alloc
91
92static inline forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim ) {
93        //printf( "X11\n" );
94        return (T *)(void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
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 ) {
99        //printf( "X13\n" );
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 ) {
103        //printf( "X14\n" );
104    T * ptr = (T *)memalign( align, sizeof(T) );
105    return (T *)memset( ptr, (int)fill, sizeof(T) );
106} // align_alloc
107
108static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim ) {
109        //printf( "X15\n" );
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 ) {
113        //printf( "X16\n" );
114    T * ptr = (T *)memalign( align, dim * sizeof(T) );
115    return (T *)memset( ptr, (int)fill, dim * sizeof(T) );
116} // align_alloc
117
118
119// data, non-array types
120static inline forall( dtype T | sized(T) ) T * memset( T * dest, char c ) {
121        //printf( "X17\n" );
122        return (T *)memset( dest, c, sizeof(T) );
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" );
127        return (T *)memcpy( dest, src, sizeof(T) );
128} // memcpy
129
130// data, array types
131static inline forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ) {
132        //printf( "X19\n" );
133        return (T *)(void *)memset( dest, c, dim * sizeof(T) ); // C memset
134} // memset
135static inline forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ) {
136        //printf( "X20\n" );
137        return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
138} // memcpy
139
140// allocation/deallocation and constructor/destructor, non-array types
141forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
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 );
144
145// allocation/deallocation and constructor/destructor, array types
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 );
149
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
180forall( otype T | { int ?<?( T, T ); } )
181T * bsearch( T key, const T * arr, size_t dim );
182
183forall( otype T | { int ?<?( T, T ); } )
184unsigned int bsearch( T key, const T * arr, size_t dim );
185
186
187forall( otype T | { int ?<?( T, T ); } )
188void qsort( const T * arr, size_t dim );
189
190//---------------------------------------
191
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 );
195forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
196[ T, T ] div( T num, T demon );
197
198//---------------------------------------
199
200unsigned char abs( signed char );
201extern "C" { int abs( int ); }                                                  // use default C routine for int
202unsigned long int abs( long int );
203unsigned long long int abs( long long int );
204float abs( float );
205double abs( double );
206long double abs( long double );
207float abs( float _Complex );
208double abs( double _Complex );
209long double abs( long double _Complex );
210forall( otype T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } )
211T abs( T );
212
213//---------------------------------------
214
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 );
231
232//---------------------------------------
233
234forall( otype T | { int ?<?( T, T ); } )
235T min( T t1, T t2 );
236
237forall( otype T | { int ?>?( T, T ); } )
238T max( T t1, T t2 );
239
240forall( otype T | { T min( T, T ); T max( T, T ); } )
241T clamp( T value, T min_val, T max_val );
242
243forall( otype T )
244void swap( T & t1, T & t2 );
245
246// Local Variables: //
247// mode: c //
248// tab-width: 4 //
249// End: //
Note: See TracBrowser for help on using the repository browser.