source: libcfa/src/stdlib.cfa @ 398e8e9

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 398e8e9 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: 9.5 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//
[f4a6101]7// stdlib.c --
[bd85400]8//
9// Author           : Peter A. Buhr
10// Created On       : Thu Jan 28 17:10:29 2016
[aabb846]11// Last Modified By : Andrew Beach
12// Last Modified On : Tue Jun  2 16:46:00 2020
13// Update Count     : 500
[bd85400]14//
15
[58b6d1b]16#include "stdlib.hfa"
[bd85400]17
18//---------------------------------------
19
20#define _XOPEN_SOURCE 600                                                               // posix_memalign, *rand48
[f3fc631f]21#include <string.h>                                                                             // memcpy, memset
[89124ff]22//#include <math.h>                                                                             // fabsf, fabs, fabsl
[6e991d6]23#include <complex.h>                                                                    // _Complex_I
[e672372]24#include <assert.h>
[bd85400]25
[f4a6101]26//---------------------------------------
27
[cafb687]28forall( dtype T | sized(T) ) {
29        T * alloc_set( T ptr[], size_t dim, char fill ) {       // realloc array with fill
30                size_t olen = malloc_usable_size( ptr );                // current allocation
[d74369b]31                void * nptr = (void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
[cafb687]32                size_t nlen = malloc_usable_size( nptr );               // new allocation
33                if ( nlen > olen ) {                                                    // larger ?
[d74369b]34                        memset( (char *)nptr + olen, (int)fill, nlen - olen ); // initialize added storage
[cafb687]35                } // if
36                return (T *)nptr;
37        } // alloc_set
38
[76e2113]39        T * alloc_set( T ptr[], size_t dim, T fill ) {          // realloc array with fill
[cfbc703d]40                size_t olen = malloc_usable_size( ptr );                // current allocation
41                void * nptr = (void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
42                size_t nlen = malloc_usable_size( nptr );               // new allocation
43                if ( nlen > olen ) {                                                    // larger ?
[76e2113]44                        for ( i; malloc_size( ptr ) / sizeof(T) ~ dim ) {
45                                memcpy( &ptr[i], &fill, sizeof(T) );    // initialize with fill value
46                        } // for
[cfbc703d]47                } // if
48                return (T *)nptr;
49        } // alloc_align_set
50
[cafb687]51        T * alloc_align_set( T ptr[], size_t align, char fill ) { // aligned realloc with fill
52                size_t olen = malloc_usable_size( ptr );                // current allocation
[d74369b]53                void * nptr = (void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
54                // char * nptr = alloc_align( ptr, align );
[cafb687]55                size_t nlen = malloc_usable_size( nptr );               // new allocation
56                if ( nlen > olen ) {                                                    // larger ?
[d74369b]57                        memset( (char *)nptr + olen, (int)fill, nlen - olen ); // initialize added storage
[cafb687]58                } // if
59                return (T *)nptr;
60        } // alloc_align_set
[cfbc703d]61
62        T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ) { // aligned realloc with fill
63                size_t olen = malloc_usable_size( ptr );                // current allocation
64                void * nptr = (void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
65                // char * nptr = alloc_align( ptr, align );
66                size_t nlen = malloc_usable_size( nptr );               // new allocation
67                if ( nlen > olen ) {                                                    // larger ?
68                        for ( i; dim ) { memcpy( &ptr[i], &fill, sizeof(T) ); } // initialize with fill value
69                } // if
70                return (T *)nptr;
71        } // alloc_align_set
[cafb687]72} // distribution
[f3ddc21]73
[6065b3aa]74// allocation/deallocation and constructor/destructor, non-array types
[aca65621]75forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
[627f585]76T * new( Params p ) {
[cafb687]77        return &(*malloc()){ p };                                                       // run constructor
[f3ddc21]78} // new
[627f585]79
[aabb846]80forall( dtype T | { void ^?{}( T & ); } )
[627f585]81void delete( T * ptr ) {
[6065b3aa]82        if ( ptr ) {                                                                            // ignore null
[cafb687]83                ^(*ptr){};                                                                              // run destructor
[f3ddc21]84                free( ptr );
[f3fc631f]85        } // if
[f3ddc21]86} // delete
[627f585]87
[aabb846]88forall( dtype T, ttype Params | { void ^?{}( T & ); void delete( Params ); } )
[bf76eab]89void delete( T * ptr, Params rest ) {
[aabb846]90        delete( ptr );
[bf76eab]91        delete( rest );
[f3ddc21]92} // delete
[bf76eab]93
[6065b3aa]94
95// allocation/deallocation and constructor/destructor, array types
[aca65621]96forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
[6065b3aa]97T * anew( size_t dim, Params p ) {
[6887a99]98        T * arr = alloc( dim );
[6065b3aa]99        for ( unsigned int i = 0; i < dim; i += 1 ) {
[a493682]100                (arr[i]){ p };                                                                  // run constructor
[6065b3aa]101        } // for
102        return arr;
103} // anew
104
[aca65621]105forall( dtype T | sized(T) | { void ^?{}( T & ); } )
[6065b3aa]106void adelete( size_t dim, T arr[] ) {
107        if ( arr ) {                                                                            // ignore null
108                for ( int i = dim - 1; i >= 0; i -= 1 ) {               // reverse allocation order, must be unsigned
[a493682]109                        ^(arr[i]){};                                                            // run destructor
[6065b3aa]110                } // for
111                free( arr );
112        } // if
113} // adelete
114
[aca65621]115forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
[6065b3aa]116void adelete( size_t dim, T arr[], Params rest ) {
117        if ( arr ) {                                                                            // ignore null
118                for ( int i = dim - 1; i >= 0; i -= 1 ) {               // reverse allocation order, must be unsigned
[a493682]119                        ^(arr[i]){};                                                            // run destructor
[6065b3aa]120                } // for
121                free( arr );
122        } // if
123        adelete( rest );
124} // adelete
125
[bd85400]126//---------------------------------------
127
[e3fea42]128float _Complex strto( const char sptr[], char ** eptr ) {
[bd85400]129        float re, im;
[e672372]130        char * eeptr;
131        re = strtof( sptr, &eeptr );
[bbf3fda]132        if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
[e672372]133        im = strtof( eeptr, &eeptr );
[bbf3fda]134        if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
[e672372]135        if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
[bd85400]136        return re + im * _Complex_I;
[f3ddc21]137} // strto
138
[e3fea42]139double _Complex strto( const char sptr[], char ** eptr ) {
[bd85400]140        double re, im;
[e672372]141        char * eeptr;
142        re = strtod( sptr, &eeptr );
[bbf3fda]143        if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
[e672372]144        im = strtod( eeptr, &eeptr );
[bbf3fda]145        if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
[e672372]146        if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
[bd85400]147        return re + im * _Complex_I;
[f3ddc21]148} // strto
149
[e3fea42]150long double _Complex strto( const char sptr[], char ** eptr ) {
[bd85400]151        long double re, im;
[e672372]152        char * eeptr;
153        re = strtold( sptr, &eeptr );
[bbf3fda]154        if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
[e672372]155        im = strtold( eeptr, &eeptr );
[bbf3fda]156        if ( sptr == eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
[e672372]157        if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
[bd85400]158        return re + im * _Complex_I;
[f3ddc21]159} // strto
[bd85400]160
161//---------------------------------------
162
[3ce0d440]163forall( otype E | { int ?<?( E, E ); } ) {
164        E * bsearch( E key, const E * vals, size_t dim ) {
165                int cmp( const void * t1, const void * t2 ) {
166                        return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
167                } // cmp
168                return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
169        } // bsearch
170
171        size_t bsearch( E key, const E * vals, size_t dim ) {
172                E * result = bsearch( key, vals, dim );
173                return result ? result - vals : dim;                    // pointer subtraction includes sizeof(E)
174        } // bsearch
175
176        size_t bsearchl( E key, const E * vals, size_t dim ) {
177                size_t l = 0, m, h = dim;
178                while ( l < h ) {
179                        m = (l + h) / 2;
180                        if ( (E &)(vals[m]) < key ) {                           // cast away const
181                                l = m + 1;
182                        } else {
183                                h = m;
184                        } // if
185                } // while
186                return l;
187        } // bsearchl
188
189        E * bsearchl( E key, const E * vals, size_t dim ) {
190                size_t posn = bsearchl( key, vals, dim );
191                return (E *)(&vals[posn]);                                              // cast away const
192        } // bsearchl
193
194        size_t bsearchu( E key, const E * vals, size_t dim ) {
195                size_t l = 0, m, h = dim;
196                while ( l < h ) {
197                        m = (l + h) / 2;
198                        if ( ! ( key < (E &)(vals[m]) ) ) {                     // cast away const
199                                l = m + 1;
200                        } else {
201                                h = m;
202                        } // if
203                } // while
204                return l;
205        } // bsearchu
206
207        E * bsearchu( E key, const E * vals, size_t dim ) {
208                size_t posn = bsearchu( key, vals, dim );
209                return (E *)(&vals[posn]);
210        } // bsearchu
211
212
213        void qsort( E * vals, size_t dim ) {
214                int cmp( const void * t1, const void * t2 ) {
215                        return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
216                } // cmp
217                qsort( vals, dim, sizeof(E), cmp );
218        } // qsort
219} // distribution
220
221
222forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
223        E * bsearch( K key, const E * vals, size_t dim ) {
224                int cmp( const void * t1, const void * t2 ) {
225                        return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0;
226                } // cmp
227                return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
228        } // bsearch
229
230        size_t bsearch( K key, const E * vals, size_t dim ) {
231                E * result = bsearch( key, vals, dim );
232                return result ? result - vals : dim;                    // pointer subtraction includes sizeof(E)
233        } // bsearch
234
235        size_t bsearchl( K key, const E * vals, size_t dim ) {
236                size_t l = 0, m, h = dim;
237                while ( l < h ) {
238                        m = (l + h) / 2;
239                        if ( getKey( vals[m] ) < key ) {
240                                l = m + 1;
241                        } else {
242                                h = m;
243                        } // if
244                } // while
245                return l;
246        } // bsearchl
247
248        E * bsearchl( K key, const E * vals, size_t dim ) {
249                size_t posn = bsearchl( key, vals, dim );
250                return (E *)(&vals[posn]);                                              // cast away const
251        } // bsearchl
252
253        size_t bsearchu( K key, const E * vals, size_t dim ) {
254                size_t l = 0, m, h = dim;
255                while ( l < h ) {
256                        m = (l + h) / 2;
257                        if ( ! ( key < getKey( vals[m] ) ) ) {
258                                l = m + 1;
259                        } else {
260                                h = m;
261                        } // if
262                } // while
263                return l;
264        } // bsearchu
265
266        E * bsearchu( K key, const E * vals, size_t dim ) {
267                size_t posn = bsearchu( key, vals, dim );
268                return (E *)(&vals[posn]);
269        } // bsearchu
270} // distribution
[bd85400]271
272//---------------------------------------
273
[bbe1a87]274extern "C" {                                                                                    // override C version
275        void srandom( unsigned int seed ) { srand48( (long int)seed ); }
[4e7c0fc0]276        long int random( void ) { return mrand48(); }           // GENERATES POSITIVE AND NEGATIVE VALUES
[bbe1a87]277} // extern "C"
278
[e672372]279float random( void ) { return (float)drand48(); }               // cast otherwise float uses lrand48
[70e4895d]280double random( void ) { return drand48(); }
281float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
282double _Complex random( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
[e672372]283long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
[a9f2c13]284
[2026bb6]285//---------------------------------------
286
287bool threading_enabled(void) __attribute__((weak)) {
288        return false;
289}
[bd85400]290
291// Local Variables: //
292// tab-width: 4 //
293// End: //
Note: See TracBrowser for help on using the repository browser.