source: libcfa/src/stdlib.hfa @ 4a32319

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

remove one unnecessary call to malloc_size in alloc_set and alloc_align_set

  • Property mode set to 100644
File size: 14.9 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
[b89c7c2]11// Last Modified By : Peter A. Buhr
[d8d8f20]12// Last Modified On : Tue Jul 21 07:58:05 2020
13// Update Count     : 475
[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
[b0a0ee4]41// Macro because of returns
42#define $VAR_ALLOC( allocation, alignment ) \
43        if ( _Alignof(T) <= libAlign() ) return (T *)(void *)allocation( (size_t)sizeof(T) ); /* C allocation */ \
44        else return (T *)alignment( _Alignof(T), sizeof(T) )
45
46#define $ARRAY_ALLOC( allocation, alignment, dim ) \
47        if ( _Alignof(T) <= libAlign() ) return (T *)(void *)allocation( dim, (size_t)sizeof(T) ); /* C allocation */ \
48        else return (T *)alignment( _Alignof(T), dim, sizeof(T) )
49
50#define $RE_SPECIALS( ptr, size, allocation, alignment ) \
51        if ( unlikely( size == 0 ) || unlikely( ptr == 0p ) ) { \
52                if ( unlikely( size == 0 ) ) free( ptr ); \
53                $VAR_ALLOC( malloc, memalign ); \
54        } /* if */
55
[74b19fb]56static inline forall( dtype T | sized(T) ) {
[ca7949b]57        // Cforall safe equivalents, i.e., implicit size specification
[3ce0d440]58
[74b19fb]59        T * malloc( void ) {
[b0a0ee4]60                $VAR_ALLOC( malloc, memalign );
[74b19fb]61        } // malloc
62
[856fe3e]63        T * aalloc( size_t dim ) {
[b0a0ee4]64                $ARRAY_ALLOC( aalloc, amemalign, dim );
[856fe3e]65        } // aalloc
66
[74b19fb]67        T * calloc( size_t dim ) {
[b0a0ee4]68                $ARRAY_ALLOC( calloc, cmemalign, dim );
[74b19fb]69        } // calloc
70
[b89c7c2]71        T * resize( T * ptr, size_t size ) {                            // CFA resize, eliminate return-type cast
[b0a0ee4]72                $RE_SPECIALS( ptr, size, malloc, memalign );
[b89c7c2]73                return (T *)(void *)resize( (void *)ptr, size ); // CFA resize
[856fe3e]74        } // resize
75
[d74369b]76        T * realloc( T * ptr, size_t size ) {                           // CFA realloc, eliminate return-type cast
[b0a0ee4]77                $RE_SPECIALS( ptr, size, malloc, memalign );
[cafb687]78                return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
[74b19fb]79        } // realloc
80
81        T * memalign( size_t align ) {
[cafb687]82                return (T *)memalign( align, sizeof(T) );               // C memalign
[74b19fb]83        } // memalign
84
[856fe3e]85        T * amemalign( size_t align, size_t dim ) {
86                return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign
87        } // amemalign
88
[d74369b]89        T * cmemalign( size_t align, size_t dim  ) {
90                return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
91        } // cmemalign
92
[74b19fb]93        T * aligned_alloc( size_t align ) {
[cafb687]94                return (T *)aligned_alloc( align, sizeof(T) );  // C aligned_alloc
[74b19fb]95        } // aligned_alloc
96
97        int posix_memalign( T ** ptr, size_t align ) {
98                return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
99        } // posix_memalign
[ada0246d]100
101        T * valloc( void ) {
102                return (T *)valloc( sizeof(T) );                                // C valloc
103        } // valloc
104
105        T * pvalloc( void ) {
106                return (T *)pvalloc( sizeof(T) );                               // C pvalloc
107        } // pvalloc
[cfbc703d]108} // distribution
[74b19fb]109
[cfbc703d]110static inline forall( dtype T | sized(T) ) {
[ca7949b]111        // Cforall safe general allocation, fill, resize, array
[74b19fb]112
113        T * alloc( void ) {
[d6b03b7]114                return malloc();
[74b19fb]115        } // alloc
116
[cafb687]117        T * alloc( size_t dim ) {
[856fe3e]118                return aalloc( dim );
[74b19fb]119        } // alloc
120
[cfbc703d]121        forall( dtype S | sized(S) )
122        T * alloc( S ptr[], size_t dim = 1 ) {                          // singleton/array resize
123                size_t len = malloc_usable_size( ptr );                 // current bucket size
124                if ( sizeof(T) * dim > len ) {                                  // not enough space ?
125                        T * temp = alloc( dim );                                        // new storage
126                        free( ptr );                                                            // free old storage
127                        return temp;
128                } else {
129                        return (T *)ptr;
130                } // if
131        } // alloc
132
133        T * alloc( T ptr[], size_t dim, bool copy = true ) {
[b89c7c2]134                if ( copy ) {
135                        return realloc( ptr, dim * sizeof(T) );         // CFA realloc
[cfbc703d]136                } else {
[b89c7c2]137                        return resize( ptr, dim * sizeof(T) );          // CFA resize
[cfbc703d]138                } // if
[7df201c]139        } // alloc
140
[cafb687]141        T * alloc_set( char fill ) {
142                return (T *)memset( (T *)alloc(), (int)fill, sizeof(T) ); // initialize with fill value
143        } // alloc
144
145        T * alloc_set( T fill ) {
146                return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value
[74b19fb]147        } // alloc
148
[cafb687]149        T * alloc_set( size_t dim, char fill ) {
[d6b03b7]150                return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
[74b19fb]151        } // alloc
152
[cafb687]153        T * alloc_set( size_t dim, T fill ) {
[7df201c]154                T * r = (T *)alloc( dim );
155                for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
156                return r;
157        } // alloc
158
[cafb687]159        T * alloc_set( size_t dim, const T fill[] ) {
[7df201c]160                return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value
161        } // alloc
[6065b3aa]162
[b89c7c2]163        T * alloc_set( T ptr[], size_t dim, char fill ) {       // realloc array with fill
164                size_t osize = malloc_size( ptr );                              // current allocation
[d8d8f20]165                size_t nsize = dim * sizeof(T);                                 // new allocation
166                T * nptr = realloc( ptr, nsize );                               // CFA realloc
[b89c7c2]167                if ( nsize > osize ) {                                                  // larger ?
168                        memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage
169                } // if
170                return (T *)nptr;
171        } // alloc_set
172
173        T * alloc_set( T ptr[], size_t dim, T & fill ) {        // realloc array with fill
[d8d8f20]174                size_t odim = malloc_size( ptr ) / sizeof(T);   // current dimension
175                size_t nsize = dim * sizeof(T);                                 // new allocation
176                size_t ndim = nsize / sizeof(T);                                // new dimension
177                T * nptr = realloc( ptr, nsize );                               // CFA realloc
[b89c7c2]178                if ( ndim > odim ) {                                                    // larger ?
179                        for ( i; odim ~ ndim ) {
180                                memcpy( &nptr[i], &fill, sizeof(T) );   // initialize with fill value
181                        } // for
182                } // if
183                return (T *)nptr;
184        } // alloc_align_set
[cafb687]185} // distribution
[f3fc631f]186
[3ce0d440]187static inline forall( dtype T | sized(T) ) {
[cafb687]188        T * alloc_align( size_t align ) {
[3ce0d440]189                return (T *)memalign( align, sizeof(T) );
[cafb687]190        } // alloc_align
[3ce0d440]191
[cafb687]192        T * alloc_align( size_t align, size_t dim ) {
[3ce0d440]193                return (T *)memalign( align, dim * sizeof(T) );
[cafb687]194        } // alloc_align
195
[856fe3e]196        T * alloc_align( T * ptr, size_t align ) {                      // aligned realloc array
[d74369b]197                return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
198        } // alloc_align
199
[cfbc703d]200        forall( dtype S | sized(S) )
201        T * alloc_align( S ptr[], size_t align ) {                      // aligned reuse array
202                return (T *)(void *)resize( (void *)ptr, align, sizeof(T) ); // CFA realloc
203        } // alloc_align
204
[d74369b]205        T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array
206                return (T *)(void *)realloc( (void *)ptr, align, dim * sizeof(T) ); // CFA realloc
207        } // alloc_align
208
[cafb687]209        T * alloc_align_set( size_t align, char fill ) {
210                return (T *)memset( (T *)alloc_align( align ), (int)fill, sizeof(T) ); // initialize with fill value
211        } // alloc_align
[3ce0d440]212
[cafb687]213        T * alloc_align_set( size_t align, T fill ) {
214                return (T *)memcpy( (T *)alloc_align( align ), &fill, sizeof(T) ); // initialize with fill value
215        } // alloc_align
[d6b03b7]216
[cafb687]217        T * alloc_align_set( size_t align, size_t dim, char fill ) {
218                return (T *)memset( (T *)alloc_align( align, dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
219        } // alloc_align
220
221        T * alloc_align_set( size_t align, size_t dim, T fill ) {
222                T * r = (T *)alloc_align( align, dim );
223                for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
224                return r;
225        } // alloc_align
226
227        T * alloc_align_set( size_t align, size_t dim, const T fill[] ) {
228                return (T *)memcpy( (T *)alloc_align( align, dim ), fill, dim * sizeof(T) );
229        } // alloc_align
230
[b89c7c2]231        T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ) {
232                size_t osize = malloc_size( ptr );                              // current allocation
[d8d8f20]233                size_t nsize = dim * sizeof(T);                                 // new allocation
234                T * nptr = realloc( ptr, align, nsize );                // CFA realloc
[b89c7c2]235                if ( nsize > osize ) {                                                  // larger ?
236                        memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage
237                } // if
238                return (T *)nptr;
239        } // alloc_align_set
240
241        T * alloc_align_set( T ptr[], size_t align, size_t dim, T & fill ) {
[d8d8f20]242                size_t odim = malloc_size( ptr ) / sizeof(T);   // current dimension
243                size_t nsize = dim * sizeof(T);                                 // new allocation
244                size_t ndim = nsize / sizeof(T);                                // new dimension
245                T * nptr = realloc( ptr, align, nsize );                // CFA realloc
[b89c7c2]246                if ( ndim > odim ) {                                                    // larger ?
247                        for ( i; odim ~ ndim ) {
248                                memcpy( &nptr[i], &fill, sizeof(T) );   // initialize with fill value
249                        } // for
250                } // if
251                return (T *)nptr;
252        } // alloc_align_set
[cafb687]253} // distribution
[3ce0d440]254
255static inline forall( dtype T | sized(T) ) {
[ca7949b]256        // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
[b9c04946]257        T * memset( T * dest, char fill ) {
258                return (T *)memset( dest, fill, sizeof(T) );
[3ce0d440]259        } // memset
260
261        T * memcpy( T * dest, const T * src ) {
262                return (T *)memcpy( dest, src, sizeof(T) );
263        } // memcpy
264} // distribution
265
266static inline forall( dtype T | sized(T) ) {
[ca7949b]267        // Cforall safe initialization/copy, i.e., implicit size specification, array types
[b9c04946]268        T * amemset( T dest[], char fill, size_t dim ) {
269                return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
270        } // amemset
[3ce0d440]271
[b9c04946]272        T * amemcpy( T dest[], const T src[], size_t dim ) {
[3ce0d440]273                return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
[b9c04946]274        } // amemcpy
[3ce0d440]275} // distribution
[f3fc631f]276
[ca7949b]277// Cforall allocation/deallocation and constructor/destructor, non-array types
[aca65621]278forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
[aabb846]279forall( dtype T | { void ^?{}( T & ); } ) void delete( T * ptr );
280forall( dtype T, ttype Params | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
[627f585]281
[ca7949b]282// Cforall allocation/deallocation and constructor/destructor, array types
[aca65621]283forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
284forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
285forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
[6065b3aa]286
[bd85400]287//---------------------------------------
288
[57fc7d8]289static inline {
[e3fea42]290        int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
291        unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
292        long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
293        unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
294        long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
295        unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
296
297        float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); }
298        double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); }
299        long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); }
[57fc7d8]300} // distribution
[e672372]301
[e3fea42]302float _Complex strto( const char sptr[], char ** eptr );
303double _Complex strto( const char sptr[], char ** eptr );
304long double _Complex strto( const char sptr[], char ** eptr );
[bd85400]305
[57fc7d8]306static inline {
[e3fea42]307        int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); }
308        unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); }
309        long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); }
310        unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); }
311        long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); }
312        unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); }
313
314        float ato( const char sptr[] ) { return strtof( sptr, 0p ); }
315        double ato( const char sptr[] ) { return strtod( sptr, 0p ); }
316        long double ato( const char sptr[] ) { return strtold( sptr, 0p ); }
317
318        float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
319        double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
320        long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
[57fc7d8]321} // distribution
[e672372]322
[bd85400]323//---------------------------------------
324
[3ce0d440]325forall( otype E | { int ?<?( E, E ); } ) {
326        E * bsearch( E key, const E * vals, size_t dim );
327        size_t bsearch( E key, const E * vals, size_t dim );
328        E * bsearchl( E key, const E * vals, size_t dim );
329        size_t bsearchl( E key, const E * vals, size_t dim );
330        E * bsearchu( E key, const E * vals, size_t dim );
331        size_t bsearchu( E key, const E * vals, size_t dim );
332} // distribution
[9c47a47]333
[3ce0d440]334forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
335        E * bsearch( K key, const E * vals, size_t dim );
336        size_t bsearch( K key, const E * vals, size_t dim );
337        E * bsearchl( K key, const E * vals, size_t dim );
338        size_t bsearchl( K key, const E * vals, size_t dim );
339        E * bsearchu( K key, const E * vals, size_t dim );
340        size_t bsearchu( K key, const E * vals, size_t dim );
341} // distribution
[bd85400]342
[b9c04946]343forall( otype E | { int ?<?( E, E ); } ) {
344        void qsort( E * vals, size_t dim );
345} // distribution
346
[bd85400]347//---------------------------------------
348
[bbe1a87]349extern "C" {                                                                                    // override C version
350        void srandom( unsigned int seed );
[4e7c0fc0]351        long int random( void );                                                        // GENERATES POSITIVE AND NEGATIVE VALUES
352        // For positive values, use unsigned int, e.g., unsigned int r = random() % 100U;
[bbe1a87]353} // extern "C"
354
355static inline {
356        long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
357        long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u)
358        unsigned long int random( void ) { return lrand48(); }
359        unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u)
[4e7c0fc0]360        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]361
362        char random( void ) { return (unsigned long int)random(); }
363        char random( char u ) { return random( (unsigned long int)u ); } // [0,u)
364        char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
365        int random( void ) { return (long int)random(); }
366        int random( int u ) { return random( (long int)u ); } // [0,u]
367        int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u)
368        unsigned int random( void ) { return (unsigned long int)random(); }
369        unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u]
370        unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
371} // distribution
372
373float random( void );                                                                   // [0.0, 1.0)
374double random( void );                                                                  // [0.0, 1.0)
375float _Complex random( void );                                                  // [0.0, 1.0)+[0.0, 1.0)i
376double _Complex random( void );                                                 // [0.0, 1.0)+[0.0, 1.0)i
377long double _Complex random( void );                                    // [0.0, 1.0)+[0.0, 1.0)i
[bd85400]378
379//---------------------------------------
380
[58b6d1b]381#include "common.hfa"
[bd85400]382
[2026bb6]383//---------------------------------------
384
385extern bool threading_enabled(void) OPTIONAL_THREAD;
386
[bd85400]387// Local Variables: //
388// mode: c //
389// tab-width: 4 //
390// End: //
Note: See TracBrowser for help on using the repository browser.