source: libcfa/src/stdlib.hfa @ 43784ac

ADTast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 43784ac was 6c5d92f, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

for private variables and functions, move prefix $ to suffix $

  • Property mode set to 100644
File size: 15.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//
[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
[6c5d92f]12// Last Modified On : Tue Apr 20 21:20:03 2021
13// Update Count     : 575
[bd85400]14//
15
[53a6c2a]16#pragma once
[17e5e2b]17
[94429f8]18#include "bits/defs.hfa"                                                                // OPTIONAL_THREAD
19#include "bits/align.hfa"                                                               // libAlign
[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
[c354108]41#include "common.hfa"
42
43//---------------------------------------
44
[f67b983]45// Macro because of returns
[6c5d92f]46#define ARRAY_ALLOC$( allocation, alignment, dim ) \
[b0a0ee4]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
[fd54fef]50static inline forall( T & | sized(T) ) {
[4803a901]51        // CFA safe equivalents, i.e., implicit size specification
[3ce0d440]52
[74b19fb]53        T * malloc( void ) {
[f67b983]54                if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C allocation
[68f0c4e]55                else return (T *)memalign( _Alignof(T), sizeof(T) );
[74b19fb]56        } // malloc
57
[856fe3e]58        T * aalloc( size_t dim ) {
[6c5d92f]59                ARRAY_ALLOC$( aalloc, amemalign, dim );
[856fe3e]60        } // aalloc
61
[74b19fb]62        T * calloc( size_t dim ) {
[6c5d92f]63                ARRAY_ALLOC$( calloc, cmemalign, dim );
[74b19fb]64        } // calloc
65
[b89c7c2]66        T * resize( T * ptr, size_t size ) {                            // CFA resize, eliminate return-type cast
[60062be]67                if ( _Alignof(T) <= libAlign() ) return (T *)(void *)resize( (void *)ptr, size ); // CFA resize
68                else return (T *)(void *)resize( (void *)ptr, _Alignof(T), size ); // CFA resize
[856fe3e]69        } // resize
70
[d74369b]71        T * realloc( T * ptr, size_t size ) {                           // CFA realloc, eliminate return-type cast
[60062be]72                if ( _Alignof(T) <= libAlign() ) return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
73                else return (T *)(void *)realloc( (void *)ptr, _Alignof(T), size ); // CFA realloc
[74b19fb]74        } // realloc
75
76        T * memalign( size_t align ) {
[cafb687]77                return (T *)memalign( align, sizeof(T) );               // C memalign
[74b19fb]78        } // memalign
79
[856fe3e]80        T * amemalign( size_t align, size_t dim ) {
81                return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign
82        } // amemalign
83
[d74369b]84        T * cmemalign( size_t align, size_t dim  ) {
85                return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
86        } // cmemalign
87
[74b19fb]88        T * aligned_alloc( size_t align ) {
[cafb687]89                return (T *)aligned_alloc( align, sizeof(T) );  // C aligned_alloc
[74b19fb]90        } // aligned_alloc
91
92        int posix_memalign( T ** ptr, size_t align ) {
93                return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
94        } // posix_memalign
[ada0246d]95
96        T * valloc( void ) {
97                return (T *)valloc( sizeof(T) );                                // C valloc
98        } // valloc
99
100        T * pvalloc( void ) {
101                return (T *)pvalloc( sizeof(T) );                               // C pvalloc
102        } // pvalloc
[55acc3a]103} // distribution
104
[ceb7db8]105/*
106        FIX ME : fix alloc interface after Ticker Number 214 is resolved, define and add union to S_fill. Then, modify postfix-fill functions to support T * with nmemb, char, and T object of any size. Finally, change alloc_internal.
107        Or, just follow the instructions below for that.
108
109        1. Replace the current forall-block that contains defintions of S_fill and S_realloc with following:
[fd54fef]110                forall( T & | sized(T) ) {
[ceb7db8]111                        union  U_fill           { char c; T * a; T t; };
[685810e]112                        struct S_fill           { char tag; U_fill(T) fill; };
[ceb7db8]113                        struct S_realloc        { inline T *; };
114                }
115
116        2. Replace all current postfix-fill functions with following for updated S_fill:
117                S_fill(T) ?`fill( char a )                                      { S_fill(T) ret = {'c'}; ret.fill.c = a; return ret; }
118                S_fill(T) ?`fill( T    a )                                      { S_fill(T) ret = {'t'}; memcpy(&ret.fill.t, &a, sizeof(T)); return ret; }
119                S_fill(T) ?`fill( T    a[], size_t nmemb )      { S_fill(T) ret = {'a', nmemb}; ret.fill.a = a; return ret; }
120
[6c5d92f]121        3. Replace the alloc_internal$ function which is outside ttype forall-block with following function:
122                T * alloc_internal$( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill) {
[ceb7db8]123                        T * ptr = NULL;
124                        size_t size = sizeof(T);
125                        size_t copy_end = 0;
126
127                        if(Resize) {
128                                ptr = (T*) (void *) resize( (int *)Resize, Align, Dim * size );
129                        } else if (Realloc) {
130                                if (Fill.tag != '0') copy_end = min(malloc_size( Realloc ), Dim * size);
131                                ptr = (T*) (void *) realloc( (int *)Realloc, Align, Dim * size );
132                        } else {
133                                ptr = (T*) (void *) memalign( Align, Dim * size );
134                        }
135
136                        if(Fill.tag == 'c') {
137                                memset( (char *)ptr + copy_end, (int)Fill.fill.c, Dim * size - copy_end );
138                        } else if(Fill.tag == 't') {
139                                for ( int i = copy_end; i <= Dim * size - size ; i += size ) {
140                                        memcpy( (char *)ptr + i, &Fill.fill.t, size );
141                                }
142                        } else if(Fill.tag == 'a') {
143                                memcpy( (char *)ptr + copy_end, Fill.fill.a, min(Dim * size - copy_end, size * Fill.nmemb) );
144                        }
145
146                        return ptr;
[6c5d92f]147                } // alloc_internal$
[ceb7db8]148*/
149
150typedef struct S_align                  { inline size_t;  } T_align;
151typedef struct S_resize                 { inline void *;  }     T_resize;
152
[fd54fef]153forall( T & ) {
[ceb7db8]154        struct S_fill           { char tag; char c; size_t size; T * at; char t[50]; };
155        struct S_realloc        { inline T *; };
156}
157
158static inline T_align   ?`align   ( size_t a )  { return (T_align){a}; }
159static inline T_resize  ?`resize  ( void * a )  { return (T_resize){a}; }
[74b19fb]160
[fd54fef]161static inline forall( T & | sized(T) ) {
[ceb7db8]162        S_fill(T) ?`fill ( T t ) {
163                S_fill(T) ret = { 't' };
164                size_t size = sizeof(T);
[3d3d75e]165                if ( size > sizeof(ret.t) ) {
166                        abort( "ERROR: const object of size greater than 50 bytes given for dynamic memory fill\n" );
167                } // if
[ceb7db8]168                memcpy( &ret.t, &t, size );
169                return ret;
170        }
171        S_fill(T)               ?`fill ( char c )                               { return (S_fill(T)){ 'c', c }; }
172        S_fill(T)               ?`fill ( T * a )                                { return (S_fill(T)){ 'T', '0', 0, a }; }
173        S_fill(T)               ?`fill ( T a[], size_t nmemb )  { return (S_fill(T)){ 'a', '0', nmemb * sizeof(T), a }; }
174
175        S_realloc(T)    ?`realloc ( T * a )                             { return (S_realloc(T)){a}; }
176
[6c5d92f]177        T * alloc_internal$( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill ) {
[ceb7db8]178                T * ptr = NULL;
179                size_t size = sizeof(T);
180                size_t copy_end = 0;
[f67b983]181
182                if ( Resize ) {
[68f0c4e]183                        ptr = (T*) (void *) resize( (void *)Resize, Align, Dim * size );
[f67b983]184                } else if ( Realloc ) {
[3d3d75e]185                        if ( Fill.tag != '0' ) copy_end = min(malloc_size( Realloc ), Dim * size );
186                        ptr = (T *) (void *) realloc( (void *)Realloc, Align, Dim * size );
[cfbc703d]187                } else {
[3d3d75e]188                        ptr = (T *) (void *) memalign( Align, Dim * size );
[ceb7db8]189                }
190
[3d3d75e]191                if ( Fill.tag == 'c' ) {
[ceb7db8]192                        memset( (char *)ptr + copy_end, (int)Fill.c, Dim * size - copy_end );
[3d3d75e]193                } else if ( Fill.tag == 't' ) {
[191a190]194                        for ( int i = copy_end; i < Dim * size; i += size ) {
[3d3d75e]195                                #pragma GCC diagnostic push
196                                #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
[d1b70d4]197                                assert( size <= sizeof(Fill.t) );
198                                memcpy( (char *)ptr + i, &Fill.t, size );
[3d3d75e]199                                #pragma GCC diagnostic pop
[ceb7db8]200                        }
[3d3d75e]201                } else if ( Fill.tag == 'a' ) {
[ceb7db8]202                        memcpy( (char *)ptr + copy_end, Fill.at, min(Dim * size - copy_end, Fill.size) );
[3d3d75e]203                } else if ( Fill.tag == 'T' ) {
204                        memcpy( (char *)ptr + copy_end, Fill.at, Dim * size );
[ceb7db8]205                }
206
207                return ptr;
[6c5d92f]208        } // alloc_internal$
[ceb7db8]209
[6c5d92f]210        forall( TT... | { T * alloc_internal$( void *, T *, size_t, size_t, S_fill(T), TT ); } ) {
[ceb7db8]211
[6c5d92f]212                T * alloc_internal$( void *       , T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill, T_resize Resize, TT rest) {
213                return alloc_internal$( Resize, (T*)0p, Align, Dim, Fill, rest);
[ceb7db8]214                }
215
[6c5d92f]216                T * alloc_internal$( void * Resize, T *        , size_t Align, size_t Dim, S_fill(T) Fill, S_realloc(T) Realloc, TT rest) {
217                return alloc_internal$( (void*)0p, Realloc, Align, Dim, Fill, rest);
[ceb7db8]218                }
219
[6c5d92f]220                T * alloc_internal$( void * Resize, T * Realloc, size_t      , size_t Dim, S_fill(T) Fill, T_align Align, TT rest) {
221                return alloc_internal$( Resize, Realloc, Align, Dim, Fill, rest);
[ceb7db8]222                }
223
[6c5d92f]224                T * alloc_internal$( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T)     , S_fill(T) Fill, TT rest) {
225                return alloc_internal$( Resize, Realloc, Align, Dim, Fill, rest);
[ceb7db8]226                }
227
228            T * alloc( TT all ) {
[6c5d92f]229                return alloc_internal$( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), (size_t)1, (S_fill(T)){'0'}, all);
[ceb7db8]230            }
231
232            T * alloc( size_t dim, TT all ) {
[6c5d92f]233                return alloc_internal$( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), dim, (S_fill(T)){'0'}, all);
[ceb7db8]234            }
235
236        } // distribution TT
237} // distribution T
[3ce0d440]238
[fd54fef]239static inline forall( T & | sized(T) ) {
[4803a901]240        // CFA safe initialization/copy, i.e., implicit size specification, non-array types
[b9c04946]241        T * memset( T * dest, char fill ) {
242                return (T *)memset( dest, fill, sizeof(T) );
[3ce0d440]243        } // memset
244
245        T * memcpy( T * dest, const T * src ) {
246                return (T *)memcpy( dest, src, sizeof(T) );
247        } // memcpy
248
[4803a901]249        // CFA safe initialization/copy, i.e., implicit size specification, array types
[b9c04946]250        T * amemset( T dest[], char fill, size_t dim ) {
251                return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
252        } // amemset
[3ce0d440]253
[b9c04946]254        T * amemcpy( T dest[], const T src[], size_t dim ) {
[3ce0d440]255                return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
[b9c04946]256        } // amemcpy
[3ce0d440]257} // distribution
[f3fc631f]258
[4803a901]259// CFA deallocation for multiple objects
[fd54fef]260static inline forall( T & )                                                     // FIX ME, problems with 0p in list
[4803a901]261void free( T * ptr ) {
262        free( (void *)ptr );                                                            // C free
263} // free
[fd54fef]264static inline forall( T &, TT... | { void free( TT ); } )
[4803a901]265void free( T * ptr, TT rest ) {
266        free( ptr );
[94429f8]267        free( rest );
268} // free
269
[4803a901]270// CFA allocation/deallocation and constructor/destructor, non-array types
[fd54fef]271static inline forall( T & | sized(T), TT... | { void ?{}( T &, TT ); } )
[94429f8]272T * new( TT p ) {
[09ee131]273        return &(*(T *)malloc()){ p };                                          // run constructor
[94429f8]274} // new
275
[fd54fef]276static inline forall( T & | { void ^?{}( T & ); } )
[94429f8]277void delete( T * ptr ) {
[0f7a0ea]278        // special case for 0-sized object => always call destructor
279        if ( ptr || sizeof(ptr) == 0 ) {                                        // ignore null but not 0-sized objects
[94429f8]280                ^(*ptr){};                                                                              // run destructor
281        } // if
[4803a901]282        free( ptr );                                                                            // always call free
[94429f8]283} // delete
[fd54fef]284static inline forall( T &, TT... | { void ^?{}( T & ); void delete( TT ); } )
[94429f8]285void delete( T * ptr, TT rest ) {
286        delete( ptr );
287        delete( rest );
288} // delete
[627f585]289
[4803a901]290// CFA allocation/deallocation and constructor/destructor, array types
[fd54fef]291forall( T & | sized(T), TT... | { void ?{}( T &, TT ); } ) T * anew( size_t dim, TT p );
292forall( T & | sized(T) | { void ^?{}( T & ); } ) void adelete( T arr[] );
293forall( T & | sized(T) | { void ^?{}( T & ); }, TT... | { void adelete( TT ); } ) void adelete( T arr[], TT rest );
[6065b3aa]294
[bd85400]295//---------------------------------------
296
[57fc7d8]297static inline {
[e3fea42]298        int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
299        unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
300        long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
301        unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
302        long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
303        unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
304
305        float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); }
306        double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); }
307        long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); }
[57fc7d8]308} // distribution
[e672372]309
[e3fea42]310float _Complex strto( const char sptr[], char ** eptr );
311double _Complex strto( const char sptr[], char ** eptr );
312long double _Complex strto( const char sptr[], char ** eptr );
[bd85400]313
[57fc7d8]314static inline {
[e3fea42]315        int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); }
316        unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); }
317        long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); }
318        unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); }
319        long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); }
320        unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); }
321
322        float ato( const char sptr[] ) { return strtof( sptr, 0p ); }
323        double ato( const char sptr[] ) { return strtod( sptr, 0p ); }
324        long double ato( const char sptr[] ) { return strtold( sptr, 0p ); }
325
326        float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
327        double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
328        long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
[57fc7d8]329} // distribution
[e672372]330
[bd85400]331//---------------------------------------
332
[fd54fef]333forall( E | { int ?<?( E, E ); } ) {
[3ce0d440]334        E * bsearch( E key, const E * vals, size_t dim );
335        size_t bsearch( E key, const E * vals, size_t dim );
336        E * bsearchl( E key, const E * vals, size_t dim );
337        size_t bsearchl( E key, const E * vals, size_t dim );
338        E * bsearchu( E key, const E * vals, size_t dim );
339        size_t bsearchu( E key, const E * vals, size_t dim );
340} // distribution
[9c47a47]341
[fd54fef]342forall( K, E | { int ?<?( K, K ); K getKey( const E & ); } ) {
[3ce0d440]343        E * bsearch( K key, const E * vals, size_t dim );
344        size_t bsearch( K key, const E * vals, size_t dim );
345        E * bsearchl( K key, const E * vals, size_t dim );
346        size_t bsearchl( K key, const E * vals, size_t dim );
347        E * bsearchu( K key, const E * vals, size_t dim );
348        size_t bsearchu( K key, const E * vals, size_t dim );
349} // distribution
[bd85400]350
[fd54fef]351forall( E | { int ?<?( E, E ); } ) {
[b9c04946]352        void qsort( E * vals, size_t dim );
353} // distribution
354
[bd85400]355//---------------------------------------
356
[bbe1a87]357extern "C" {                                                                                    // override C version
358        void srandom( unsigned int seed );
[4e7c0fc0]359        long int random( void );                                                        // GENERATES POSITIVE AND NEGATIVE VALUES
360        // For positive values, use unsigned int, e.g., unsigned int r = random() % 100U;
[bbe1a87]361} // extern "C"
362
363static inline {
364        long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
365        long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u)
366        unsigned long int random( void ) { return lrand48(); }
367        unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u)
[4e7c0fc0]368        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]369
370        char random( void ) { return (unsigned long int)random(); }
371        char random( char u ) { return random( (unsigned long int)u ); } // [0,u)
372        char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
373        int random( void ) { return (long int)random(); }
374        int random( int u ) { return random( (long int)u ); } // [0,u]
375        int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u)
376        unsigned int random( void ) { return (unsigned long int)random(); }
377        unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u]
378        unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
379} // distribution
380
381float random( void );                                                                   // [0.0, 1.0)
382double random( void );                                                                  // [0.0, 1.0)
383float _Complex random( void );                                                  // [0.0, 1.0)+[0.0, 1.0)i
384double _Complex random( void );                                                 // [0.0, 1.0)+[0.0, 1.0)i
385long double _Complex random( void );                                    // [0.0, 1.0)+[0.0, 1.0)i
[bd85400]386
387//---------------------------------------
388
[94429f8]389extern bool threading_enabled( void ) OPTIONAL_THREAD;
[2026bb6]390
[bd85400]391// Local Variables: //
392// mode: c //
393// tab-width: 4 //
394// End: //
Note: See TracBrowser for help on using the repository browser.