source: libcfa/src/stdlib.hfa @ c2051e10

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

check for type alignment and use it for storage allocation

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