source: libcfa/src/stdlib.hfa @ 8bb239d

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 8bb239d was aabb846, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Added a first draft of the memory management library module.

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