source: libcfa/src/stdlib.hfa @ 1c40091

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

start allocation updates for arrays and alignment

  • Property mode set to 100644
File size: 11.0 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 : Sun Oct 20 22:57:33 2019
13// Update Count     : 390
14//
15
16#pragma once
17
18#include "bits/defs.hfa"
19#include "bits/align.hfa"
20
21#include <stdlib.h>                                                                             // *alloc, strto*, ato*
22
23extern "C" {
24        void * memalign( size_t align, size_t size );           // malloc.h
25        void * memset( void * dest, int fill, size_t size ); // string.h
26        void * memcpy( void * dest, const void * src, size_t size ); // string.h
27    void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap
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        // C dynamic allocation
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 * calloc( size_t dim ) {
48                if ( _Alignof(T) <= libAlign() )return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc
49                else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) );
50        } // calloc
51
52        T * realloc( T * ptr, size_t size ) {
53                if ( unlikely( ptr == 0 ) ) return malloc();
54                return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
55        } // realloc
56
57        T * memalign( size_t align ) {
58                return (T *)memalign( align, sizeof(T) );               // C memalign
59        } // memalign
60
61        T * aligned_alloc( size_t align ) {
62                return (T *)aligned_alloc( align, sizeof(T) );  // C aligned_alloc
63        } // aligned_alloc
64
65        int posix_memalign( T ** ptr, size_t align ) {
66                return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
67        } // posix_memalign
68
69        // Cforall dynamic allocation
70
71        T * alloc( void ) {
72                return malloc();
73        } // alloc
74
75        T * alloc( size_t dim ) {
76                if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( dim * (size_t)sizeof(T) );
77                else return (T *)memalign( _Alignof(T), dim * sizeof(T) );
78        } // alloc
79
80        T * alloc( T ptr[], size_t dim ) {                                      // realloc
81                return realloc( ptr, dim * sizeof(T) );
82        } // alloc
83
84        T * alloc_set( char fill ) {
85                return (T *)memset( (T *)alloc(), (int)fill, sizeof(T) ); // initialize with fill value
86        } // alloc
87
88        T * alloc_set( T fill ) {
89                return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value
90        } // alloc
91
92        T * alloc_set( size_t dim, char fill ) {
93                return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
94        } // alloc
95
96        T * alloc_set( size_t dim, T fill ) {
97                T * r = (T *)alloc( dim );
98                for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
99                return r;
100        } // alloc
101
102        T * alloc_set( size_t dim, const T fill[] ) {
103                return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value
104        } // alloc
105} // distribution
106
107forall( dtype T | sized(T) ) {
108        T * alloc_set( T ptr[], size_t dim, char fill );        // realloc array with fill
109} // distribution
110
111static inline forall( dtype T | sized(T) ) {
112        T * alloc_align( size_t align ) {
113                return (T *)memalign( align, sizeof(T) );
114        } // alloc_align
115
116        T * alloc_align( size_t align, size_t dim ) {
117                return (T *)memalign( align, dim * sizeof(T) );
118        } // alloc_align
119
120        T * alloc_align_set( size_t align, char fill ) {
121                return (T *)memset( (T *)alloc_align( align ), (int)fill, sizeof(T) ); // initialize with fill value
122        } // alloc_align
123
124        T * alloc_align_set( size_t align, T fill ) {
125                return (T *)memcpy( (T *)alloc_align( align ), &fill, sizeof(T) ); // initialize with fill value
126        } // alloc_align
127
128        T * alloc_align_set( size_t align, size_t dim, char fill ) {
129                return (T *)memset( (T *)alloc_align( align, dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
130        } // alloc_align
131
132        T * alloc_align_set( size_t align, size_t dim, T fill ) {
133                T * r = (T *)alloc_align( align, dim );
134                for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
135                return r;
136        } // alloc_align
137
138        T * alloc_align_set( size_t align, size_t dim, const T fill[] ) {
139                return (T *)memcpy( (T *)alloc_align( align, dim ), fill, dim * sizeof(T) );
140        } // alloc_align
141} // distribution
142
143forall( dtype T | sized(T) ) {
144        T * alloc_align( T ptr[], size_t align );                       // realign
145        T * alloc_align( T ptr[], size_t align, size_t dim ); // aligned realloc array
146        T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill
147} // distribution
148
149static inline forall( dtype T | sized(T) ) {
150        // data, non-array types
151        T * memset( T * dest, char fill ) {
152                return (T *)memset( dest, fill, sizeof(T) );
153        } // memset
154
155        T * memcpy( T * dest, const T * src ) {
156                return (T *)memcpy( dest, src, sizeof(T) );
157        } // memcpy
158} // distribution
159
160static inline forall( dtype T | sized(T) ) {
161        // data, array types
162        T * amemset( T dest[], char fill, size_t dim ) {
163                return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
164        } // amemset
165
166        T * amemcpy( T dest[], const T src[], size_t dim ) {
167                return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
168        } // amemcpy
169} // distribution
170
171// allocation/deallocation and constructor/destructor, non-array types
172forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
173forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
174forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
175
176// allocation/deallocation and constructor/destructor, array types
177forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
178forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
179forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
180
181//---------------------------------------
182
183static inline {
184        int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
185        unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
186        long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
187        unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
188        long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
189        unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
190
191        float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); }
192        double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); }
193        long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); }
194} // distribution
195
196float _Complex strto( const char * sptr, char ** eptr );
197double _Complex strto( const char * sptr, char ** eptr );
198long double _Complex strto( const char * sptr, char ** eptr );
199
200static inline {
201        int ato( const char * sptr ) { return (int)strtol( sptr, 0, 10 ); }
202        unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); }
203        long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); }
204        unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); }
205        long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); }
206        unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); }
207
208        float ato( const char * sptr ) { return strtof( sptr, 0 ); }
209        double ato( const char * sptr ) { return strtod( sptr, 0 ); }
210        long double ato( const char * sptr ) { return strtold( sptr, 0 ); }
211
212        float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
213        double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
214        long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
215} // distribution
216
217//---------------------------------------
218
219forall( otype E | { int ?<?( E, E ); } ) {
220        E * bsearch( E key, const E * vals, size_t dim );
221        size_t bsearch( E key, const E * vals, size_t dim );
222        E * bsearchl( E key, const E * vals, size_t dim );
223        size_t bsearchl( E key, const E * vals, size_t dim );
224        E * bsearchu( E key, const E * vals, size_t dim );
225        size_t bsearchu( E key, const E * vals, size_t dim );
226} // distribution
227
228forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
229        E * bsearch( K key, const E * vals, size_t dim );
230        size_t bsearch( K key, const E * vals, size_t dim );
231        E * bsearchl( K key, const E * vals, size_t dim );
232        size_t bsearchl( K key, const E * vals, size_t dim );
233        E * bsearchu( K key, const E * vals, size_t dim );
234        size_t bsearchu( K key, const E * vals, size_t dim );
235} // distribution
236
237forall( otype E | { int ?<?( E, E ); } ) {
238        void qsort( E * vals, size_t dim );
239} // distribution
240
241//---------------------------------------
242
243extern "C" {                                                                                    // override C version
244        void srandom( unsigned int seed );
245        long int random( void );
246} // extern "C"
247
248static inline {
249        long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
250        long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u)
251        unsigned long int random( void ) { return lrand48(); }
252        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)
253        unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u)
254
255        char random( void ) { return (unsigned long int)random(); }
256        char random( char u ) { return random( (unsigned long int)u ); } // [0,u)
257        char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
258        int random( void ) { return (long int)random(); }
259        int random( int u ) { return random( (long int)u ); } // [0,u]
260        int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u)
261        unsigned int random( void ) { return (unsigned long int)random(); }
262        unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u]
263        unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
264} // distribution
265
266float random( void );                                                                   // [0.0, 1.0)
267double random( void );                                                                  // [0.0, 1.0)
268float _Complex random( void );                                                  // [0.0, 1.0)+[0.0, 1.0)i
269double _Complex random( void );                                                 // [0.0, 1.0)+[0.0, 1.0)i
270long double _Complex random( void );                                    // [0.0, 1.0)+[0.0, 1.0)i
271
272//---------------------------------------
273
274#include "common.hfa"
275
276//---------------------------------------
277
278extern bool threading_enabled(void) OPTIONAL_THREAD;
279
280// Local Variables: //
281// mode: c //
282// tab-width: 4 //
283// End: //
Note: See TracBrowser for help on using the repository browser.