source: libcfa/src/stdlib.hfa @ ada0246d

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since ada0246d was ada0246d, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

create heap.hfa, use it in malloc.h, and cleanup includes with respect to extern "C"

  • Property mode set to 100644
File size: 13.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 May 26 22:50:08 2020
13// Update Count     : 439
14//
15
16#pragma once
17
18#include "bits/defs.hfa"
19#include "bits/align.hfa"
20
21#include <malloc.h>
22#include <stdlib.h>                                                                             // *alloc, strto*, ato*
23
24// Reduce includes by explicitly defining these routines.
25extern "C" {
26        void * memset( void * dest, int fill, size_t size ); // string.h
27        void * memcpy( void * dest, const void * src, size_t size ); // string.h
28} // extern "C"
29
30//---------------------------------------
31
32#ifndef EXIT_FAILURE
33#define EXIT_FAILURE    1                                                               // failing exit status
34#define EXIT_SUCCESS    0                                                               // successful exit status
35#endif // ! EXIT_FAILURE
36
37//---------------------------------------
38
39static inline forall( dtype T | sized(T) ) {
40        // Cforall safe equivalents, i.e., implicit size specification
41
42        T * malloc( void ) {
43                if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
44                else return (T *)memalign( _Alignof(T), sizeof(T) );
45        } // malloc
46
47        T * aalloc( size_t dim ) {
48                if ( _Alignof(T) <= libAlign() ) return (T *)(void *)aalloc( dim, (size_t)sizeof(T) ); // CFA aalloc
49                else return (T *)amemalign( _Alignof(T), dim, sizeof(T) );
50        } // aalloc
51
52        T * calloc( size_t dim ) {
53                if ( _Alignof(T) <= libAlign() )return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc
54                else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) );
55        } // calloc
56
57        T * resize( T * ptr, size_t size ) {                            // CFA realloc, eliminate return-type cast
58                return (T *)(void *)resize( (void *)ptr, size ); // C realloc
59        } // resize
60
61        T * realloc( T * ptr, size_t size ) {                           // CFA realloc, eliminate return-type cast
62                return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
63        } // realloc
64
65        T * memalign( size_t align ) {
66                return (T *)memalign( align, sizeof(T) );               // C memalign
67        } // memalign
68
69        T * amemalign( size_t align, size_t dim ) {
70                return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign
71        } // amemalign
72
73        T * cmemalign( size_t align, size_t dim  ) {
74                return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
75        } // cmemalign
76
77        T * aligned_alloc( size_t align ) {
78                return (T *)aligned_alloc( align, sizeof(T) );  // C aligned_alloc
79        } // aligned_alloc
80
81        int posix_memalign( T ** ptr, size_t align ) {
82                return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
83        } // posix_memalign
84
85        T * valloc( void ) {
86                return (T *)valloc( sizeof(T) );                                // C valloc
87        } // valloc
88
89        T * pvalloc( void ) {
90                return (T *)pvalloc( sizeof(T) );                               // C pvalloc
91        } // pvalloc
92} // distribution
93
94static inline forall( dtype T | sized(T) ) {
95        // Cforall safe general allocation, fill, resize, array
96
97        T * alloc( void ) {
98                return malloc();
99        } // alloc
100
101        T * alloc( size_t dim ) {
102                return aalloc( dim );
103        } // alloc
104
105        forall( dtype S | sized(S) )
106        T * alloc( S ptr[], size_t dim = 1 ) {                          // singleton/array resize
107                size_t len = malloc_usable_size( ptr );                 // current bucket size
108                if ( sizeof(T) * dim > len ) {                                  // not enough space ?
109                        T * temp = alloc( dim );                                        // new storage
110                        free( ptr );                                                            // free old storage
111                        return temp;
112                } else {
113                        return (T *)ptr;
114                } // if
115        } // alloc
116
117        T * alloc( T ptr[], size_t dim, bool copy = true ) {
118                if ( copy ) {                                                                   // realloc
119                        return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
120                } else {
121                        return resize( ptr, dim * sizeof(T) );          // resize
122                } // if
123        } // alloc
124
125        T * alloc_set( char fill ) {
126                return (T *)memset( (T *)alloc(), (int)fill, sizeof(T) ); // initialize with fill value
127        } // alloc
128
129        T * alloc_set( T fill ) {
130                return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value
131        } // alloc
132
133        T * alloc_set( size_t dim, char fill ) {
134                return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
135        } // alloc
136
137        T * alloc_set( size_t dim, T fill ) {
138                T * r = (T *)alloc( dim );
139                for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
140                return r;
141        } // alloc
142
143        T * alloc_set( size_t dim, const T fill[] ) {
144                return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value
145        } // alloc
146} // distribution
147
148forall( dtype T | sized(T) ) {
149        T * alloc_set( T ptr[], size_t dim, char fill );        // realloc array with fill
150        T * alloc_set( T ptr[], size_t dim, T fill );           // realloc array with fill
151} // distribution
152
153static inline forall( dtype T | sized(T) ) {
154        T * alloc_align( size_t align ) {
155                return (T *)memalign( align, sizeof(T) );
156        } // alloc_align
157
158        T * alloc_align( size_t align, size_t dim ) {
159                return (T *)memalign( align, dim * sizeof(T) );
160        } // alloc_align
161
162        T * alloc_align( T * ptr, size_t align ) {                      // aligned realloc array
163                return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
164        } // alloc_align
165
166        forall( dtype S | sized(S) )
167        T * alloc_align( S ptr[], size_t align ) {                      // aligned reuse array
168                return (T *)(void *)resize( (void *)ptr, align, sizeof(T) ); // CFA realloc
169        } // alloc_align
170
171        T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array
172                return (T *)(void *)realloc( (void *)ptr, align, dim * sizeof(T) ); // CFA realloc
173        } // alloc_align
174
175        T * alloc_align_set( size_t align, char fill ) {
176                return (T *)memset( (T *)alloc_align( align ), (int)fill, sizeof(T) ); // initialize with fill value
177        } // alloc_align
178
179        T * alloc_align_set( size_t align, T fill ) {
180                return (T *)memcpy( (T *)alloc_align( align ), &fill, sizeof(T) ); // initialize with fill value
181        } // alloc_align
182
183        T * alloc_align_set( size_t align, size_t dim, char fill ) {
184                return (T *)memset( (T *)alloc_align( align, dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
185        } // alloc_align
186
187        T * alloc_align_set( size_t align, size_t dim, T fill ) {
188                T * r = (T *)alloc_align( align, dim );
189                for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
190                return r;
191        } // alloc_align
192
193        T * alloc_align_set( size_t align, size_t dim, const T fill[] ) {
194                return (T *)memcpy( (T *)alloc_align( align, dim ), fill, dim * sizeof(T) );
195        } // alloc_align
196} // distribution
197
198forall( dtype T | sized(T) ) {
199        T * alloc_align_set( T ptr[], size_t align, char fill ); // aligned realloc with fill
200        T * alloc_align_set( T ptr[], size_t align, T fill ); // aligned realloc with fill
201        T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill
202        T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ); // aligned realloc array with fill
203} // distribution
204
205static inline forall( dtype T | sized(T) ) {
206        // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
207        T * memset( T * dest, char fill ) {
208                return (T *)memset( dest, fill, sizeof(T) );
209        } // memset
210
211        T * memcpy( T * dest, const T * src ) {
212                return (T *)memcpy( dest, src, sizeof(T) );
213        } // memcpy
214} // distribution
215
216static inline forall( dtype T | sized(T) ) {
217        // Cforall safe initialization/copy, i.e., implicit size specification, array types
218        T * amemset( T dest[], char fill, size_t dim ) {
219                return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
220        } // amemset
221
222        T * amemcpy( T dest[], const T src[], size_t dim ) {
223                return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
224        } // amemcpy
225} // distribution
226
227// Cforall allocation/deallocation and constructor/destructor, non-array types
228forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
229forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
230forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
231
232// Cforall allocation/deallocation and constructor/destructor, array types
233forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
234forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
235forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
236
237//---------------------------------------
238
239static inline {
240        int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
241        unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
242        long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
243        unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
244        long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
245        unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
246
247        float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); }
248        double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); }
249        long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); }
250} // distribution
251
252float _Complex strto( const char sptr[], char ** eptr );
253double _Complex strto( const char sptr[], char ** eptr );
254long double _Complex strto( const char sptr[], char ** eptr );
255
256static inline {
257        int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); }
258        unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); }
259        long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); }
260        unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); }
261        long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); }
262        unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); }
263
264        float ato( const char sptr[] ) { return strtof( sptr, 0p ); }
265        double ato( const char sptr[] ) { return strtod( sptr, 0p ); }
266        long double ato( const char sptr[] ) { return strtold( sptr, 0p ); }
267
268        float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
269        double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
270        long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
271} // distribution
272
273//---------------------------------------
274
275forall( otype E | { int ?<?( E, E ); } ) {
276        E * bsearch( E key, const E * vals, size_t dim );
277        size_t bsearch( E key, const E * vals, size_t dim );
278        E * bsearchl( E key, const E * vals, size_t dim );
279        size_t bsearchl( E key, const E * vals, size_t dim );
280        E * bsearchu( E key, const E * vals, size_t dim );
281        size_t bsearchu( E key, const E * vals, size_t dim );
282} // distribution
283
284forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
285        E * bsearch( K key, const E * vals, size_t dim );
286        size_t bsearch( K key, const E * vals, size_t dim );
287        E * bsearchl( K key, const E * vals, size_t dim );
288        size_t bsearchl( K key, const E * vals, size_t dim );
289        E * bsearchu( K key, const E * vals, size_t dim );
290        size_t bsearchu( K key, const E * vals, size_t dim );
291} // distribution
292
293forall( otype E | { int ?<?( E, E ); } ) {
294        void qsort( E * vals, size_t dim );
295} // distribution
296
297//---------------------------------------
298
299extern "C" {                                                                                    // override C version
300        void srandom( unsigned int seed );
301        long int random( void );
302} // extern "C"
303
304static inline {
305        long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
306        long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u)
307        unsigned long int random( void ) { return lrand48(); }
308        unsigned long int random( unsigned long int l, unsigned long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
309        unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u)
310
311        char random( void ) { return (unsigned long int)random(); }
312        char random( char u ) { return random( (unsigned long int)u ); } // [0,u)
313        char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
314        int random( void ) { return (long int)random(); }
315        int random( int u ) { return random( (long int)u ); } // [0,u]
316        int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u)
317        unsigned int random( void ) { return (unsigned long int)random(); }
318        unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u]
319        unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
320} // distribution
321
322float random( void );                                                                   // [0.0, 1.0)
323double random( void );                                                                  // [0.0, 1.0)
324float _Complex random( void );                                                  // [0.0, 1.0)+[0.0, 1.0)i
325double _Complex random( void );                                                 // [0.0, 1.0)+[0.0, 1.0)i
326long double _Complex random( void );                                    // [0.0, 1.0)+[0.0, 1.0)i
327
328//---------------------------------------
329
330#include "common.hfa"
331
332//---------------------------------------
333
334extern bool threading_enabled(void) OPTIONAL_THREAD;
335
336// Local Variables: //
337// mode: c //
338// tab-width: 4 //
339// End: //
Note: See TracBrowser for help on using the repository browser.