source: libcfa/src/stdlib.cfa @ eb5a115

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

start allocation updates for arrays and alignment

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