source: libcfa/src/stdlib.cfa @ 27273f9

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 27273f9 was cfbc703d, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

add resize and more "alloc" routines

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