source: libcfa/src/stdlib.hfa @ cb95634

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since cb95634 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
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
[ada0246d]12// Last Modified On : Tue May 26 22:50:08 2020
13// Update Count     : 439
[bd85400]14//
15
[53a6c2a]16#pragma once
[17e5e2b]17
[2026bb6]18#include "bits/defs.hfa"
[d6b03b7]19#include "bits/align.hfa"
[2026bb6]20
[ada0246d]21#include <malloc.h>
[d46ed6e]22#include <stdlib.h>                                                                             // *alloc, strto*, ato*
[d6b03b7]23
[ca7949b]24// Reduce includes by explicitly defining these routines.
[3ce0d440]25extern "C" {
[b9c04946]26        void * memset( void * dest, int fill, size_t size ); // string.h
[57fc7d8]27        void * memcpy( void * dest, const void * src, size_t size ); // string.h
[3ce0d440]28} // extern "C"
[e672372]29
[bd85400]30//---------------------------------------
31
[45161b4d]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
[74b19fb]39static inline forall( dtype T | sized(T) ) {
[ca7949b]40        // Cforall safe equivalents, i.e., implicit size specification
[3ce0d440]41
[74b19fb]42        T * malloc( void ) {
[d6b03b7]43                if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
44                else return (T *)memalign( _Alignof(T), sizeof(T) );
[74b19fb]45        } // malloc
46
[856fe3e]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
[74b19fb]52        T * calloc( size_t dim ) {
[d6b03b7]53                if ( _Alignof(T) <= libAlign() )return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc
54                else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) );
[74b19fb]55        } // calloc
56
[856fe3e]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
[d74369b]61        T * realloc( T * ptr, size_t size ) {                           // CFA realloc, eliminate return-type cast
[cafb687]62                return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
[74b19fb]63        } // realloc
64
65        T * memalign( size_t align ) {
[cafb687]66                return (T *)memalign( align, sizeof(T) );               // C memalign
[74b19fb]67        } // memalign
68
[856fe3e]69        T * amemalign( size_t align, size_t dim ) {
70                return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign
71        } // amemalign
72
[d74369b]73        T * cmemalign( size_t align, size_t dim  ) {
74                return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
75        } // cmemalign
76
[74b19fb]77        T * aligned_alloc( size_t align ) {
[cafb687]78                return (T *)aligned_alloc( align, sizeof(T) );  // C aligned_alloc
[74b19fb]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
[ada0246d]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
[cfbc703d]92} // distribution
[74b19fb]93
[cfbc703d]94static inline forall( dtype T | sized(T) ) {
[ca7949b]95        // Cforall safe general allocation, fill, resize, array
[74b19fb]96
97        T * alloc( void ) {
[d6b03b7]98                return malloc();
[74b19fb]99        } // alloc
100
[cafb687]101        T * alloc( size_t dim ) {
[856fe3e]102                return aalloc( dim );
[74b19fb]103        } // alloc
104
[cfbc703d]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 {
[856fe3e]121                        return resize( ptr, dim * sizeof(T) );          // resize
[cfbc703d]122                } // if
[7df201c]123        } // alloc
124
[cafb687]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
[74b19fb]131        } // alloc
132
[cafb687]133        T * alloc_set( size_t dim, char fill ) {
[d6b03b7]134                return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
[74b19fb]135        } // alloc
136
[cafb687]137        T * alloc_set( size_t dim, T fill ) {
[7df201c]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
[cafb687]143        T * alloc_set( size_t dim, const T fill[] ) {
[7df201c]144                return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value
145        } // alloc
[74b19fb]146} // distribution
[6065b3aa]147
[cafb687]148forall( dtype T | sized(T) ) {
149        T * alloc_set( T ptr[], size_t dim, char fill );        // realloc array with fill
[cfbc703d]150        T * alloc_set( T ptr[], size_t dim, T fill );           // realloc array with fill
[cafb687]151} // distribution
[f3fc631f]152
[3ce0d440]153static inline forall( dtype T | sized(T) ) {
[cafb687]154        T * alloc_align( size_t align ) {
[3ce0d440]155                return (T *)memalign( align, sizeof(T) );
[cafb687]156        } // alloc_align
[3ce0d440]157
[cafb687]158        T * alloc_align( size_t align, size_t dim ) {
[3ce0d440]159                return (T *)memalign( align, dim * sizeof(T) );
[cafb687]160        } // alloc_align
161
[856fe3e]162        T * alloc_align( T * ptr, size_t align ) {                      // aligned realloc array
[d74369b]163                return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
164        } // alloc_align
165
[cfbc703d]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
[d74369b]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
[cafb687]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
[3ce0d440]178
[cafb687]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
[d6b03b7]182
[cafb687]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) ) {
[cfbc703d]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
[cafb687]201        T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill
[cfbc703d]202        T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ); // aligned realloc array with fill
[cafb687]203} // distribution
[3ce0d440]204
205static inline forall( dtype T | sized(T) ) {
[ca7949b]206        // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
[b9c04946]207        T * memset( T * dest, char fill ) {
208                return (T *)memset( dest, fill, sizeof(T) );
[3ce0d440]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) ) {
[ca7949b]217        // Cforall safe initialization/copy, i.e., implicit size specification, array types
[b9c04946]218        T * amemset( T dest[], char fill, size_t dim ) {
219                return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
220        } // amemset
[3ce0d440]221
[b9c04946]222        T * amemcpy( T dest[], const T src[], size_t dim ) {
[3ce0d440]223                return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
[b9c04946]224        } // amemcpy
[3ce0d440]225} // distribution
[f3fc631f]226
[ca7949b]227// Cforall allocation/deallocation and constructor/destructor, non-array types
[aca65621]228forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
[83a071f9]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 );
[627f585]231
[ca7949b]232// Cforall allocation/deallocation and constructor/destructor, array types
[aca65621]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 );
[6065b3aa]236
[bd85400]237//---------------------------------------
238
[57fc7d8]239static inline {
[e3fea42]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 ); }
[57fc7d8]250} // distribution
[e672372]251
[e3fea42]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 );
[bd85400]255
[57fc7d8]256static inline {
[e3fea42]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 ); }
[57fc7d8]271} // distribution
[e672372]272
[bd85400]273//---------------------------------------
274
[3ce0d440]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
[9c47a47]283
[3ce0d440]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
[bd85400]292
[b9c04946]293forall( otype E | { int ?<?( E, E ); } ) {
294        void qsort( E * vals, size_t dim );
295} // distribution
296
[bd85400]297//---------------------------------------
298
[bbe1a87]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
[bd85400]327
328//---------------------------------------
329
[58b6d1b]330#include "common.hfa"
[bd85400]331
[2026bb6]332//---------------------------------------
333
334extern bool threading_enabled(void) OPTIONAL_THREAD;
335
[bd85400]336// Local Variables: //
337// mode: c //
338// tab-width: 4 //
339// End: //
Note: See TracBrowser for help on using the repository browser.