source: libcfa/src/stdlib.hfa @ b81fd95

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since b81fd95 was b81fd95, checked in by Michael Brooks <mlbrooks@…>, 4 years ago

Fix bug where pointer and reference types allow unsound initialization and return. Fixes #189

There are two instances of the same basic change, which is using conversionCost instead of castCost for resolving...
A: an InitExpr?, always; affects variable initializations
B: a CastExpr?, for type-system-generated casts only; affects function returns

Changing the behaviour of the typechecker on initialization (do A) and cast (do B):
src/ResolvExpr/AlternativeFinder.cc
src/SynTree/Expression.h
testsinit1.*

Making type of string literal consistent with how C defines it (accommodate A):
src/Parser/ExpressionNode.cc

Making type system happy with incumbent use of void* (accommodate A):
libcfa/src/concurrency/kernel.cfa
libcfa/src/containers/list.hfa
tests/bugs/66.cfa
tests/avltree/avl1.cfa
tests/concurrent/signal/block.cfa
tests/searchsort.cfa

Making type system happy with incumbent plan-9 downcast (accommodate B):
libcfa/src/containers/list.hfa

Fixing previously incorrect constness of declarations (accommodate A):
tests/exceptions/defaults.cfa
libcfa/src/iostream.hfa

Fixing previously incorrect isGenerated classification of casts that desugaring introduces (accommodate B):
src/Concurrency/Keywords.cc
src/Concurrency/Waitfor.cc

Working around trac #207 (revealed by A):
tests/io2.cfa

Working around trac #208 (speculatively created by B):
libcfa/src/bits/locks.hfa
libcfa/src/concurrency/preemption.cfa

Misc:
tests/exceptions/conditional.cfa (accommodate A)

a _msg function for an exception was declared with wrong return type, so it was not compatible for assignment into the vtable instance

libcfa/src/stdlib.hfa

the compiler now prohibits a prior attempt to call a nonexistent realloc overload; calling alloc_align in its place

  • Property mode set to 100644
File size: 15.0 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 --
8//
9// Author           : Peter A. Buhr
10// Created On       : Thu Jan 28 17:12:35 2016
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Jul 21 07:58:05 2020
13// Update Count     : 475
14//
15
16#pragma once
17
18#include "bits/defs.hfa"
19#include "bits/align.hfa"
20
21#include <stdlib.h>                                                                             // *alloc, strto*, ato*
22#include <heap.hfa>
23
24// Reduce includes by explicitly defining these routines.
25extern "C" {
26        void * memalign( size_t alignment, size_t size );       // malloc.h
27        void * pvalloc( size_t size );                                          // malloc.h
28        void * memset( void * dest, int fill, size_t size ); // string.h
29        void * memcpy( void * dest, const void * src, size_t size ); // string.h
30} // extern "C"
31
32//---------------------------------------
33
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
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
56static inline forall( dtype T | sized(T) ) {
57        // Cforall safe equivalents, i.e., implicit size specification
58
59        T * malloc( void ) {
60                $VAR_ALLOC( malloc, memalign );
61        } // malloc
62
63        T * aalloc( size_t dim ) {
64                $ARRAY_ALLOC( aalloc, amemalign, dim );
65        } // aalloc
66
67        T * calloc( size_t dim ) {
68                $ARRAY_ALLOC( calloc, cmemalign, dim );
69        } // calloc
70
71        T * resize( T * ptr, size_t size ) {                            // CFA resize, eliminate return-type cast
72                $RE_SPECIALS( ptr, size, malloc, memalign );
73                return (T *)(void *)resize( (void *)ptr, size ); // CFA resize
74        } // resize
75
76        T * realloc( T * ptr, size_t size ) {                           // CFA realloc, eliminate return-type cast
77                $RE_SPECIALS( ptr, size, malloc, memalign );
78                return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
79        } // realloc
80
81        T * memalign( size_t align ) {
82                return (T *)memalign( align, sizeof(T) );               // C memalign
83        } // memalign
84
85        T * amemalign( size_t align, size_t dim ) {
86                return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign
87        } // amemalign
88
89        T * cmemalign( size_t align, size_t dim  ) {
90                return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
91        } // cmemalign
92
93        T * aligned_alloc( size_t align ) {
94                return (T *)aligned_alloc( align, sizeof(T) );  // C aligned_alloc
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
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
108} // distribution
109
110static inline forall( dtype T | sized(T) ) {
111        // Cforall safe general allocation, fill, resize, array
112
113        T * alloc( void ) {
114                return malloc();
115        } // alloc
116
117        T * alloc( size_t dim ) {
118                return aalloc( dim );
119        } // alloc
120
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 ) {
134                if ( copy ) {
135                        return realloc( ptr, dim * sizeof(T) );         // CFA realloc
136                } else {
137                        return resize( ptr, dim * sizeof(T) );          // CFA resize
138                } // if
139        } // alloc
140
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
147        } // alloc
148
149        T * alloc_set( size_t dim, char fill ) {
150                return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
151        } // alloc
152
153        T * alloc_set( size_t dim, T fill ) {
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
159        T * alloc_set( size_t dim, const T fill[] ) {
160                return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value
161        } // alloc
162
163        T * alloc_set( T ptr[], size_t dim, char fill ) {       // realloc array with fill
164                size_t osize = malloc_size( ptr );                              // current allocation
165                size_t nsize = dim * sizeof(T);                                 // new allocation
166                T * nptr = realloc( ptr, nsize );                               // CFA realloc
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
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
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
185} // distribution
186
187static inline forall( dtype T | sized(T) ) {
188        T * alloc_align( size_t align ) {
189                return (T *)memalign( align, sizeof(T) );
190        } // alloc_align
191
192        T * alloc_align( size_t align, size_t dim ) {
193                return (T *)memalign( align, dim * sizeof(T) );
194        } // alloc_align
195
196        T * alloc_align( T * ptr, size_t align ) {                      // aligned realloc array
197                return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
198        } // alloc_align
199
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
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
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
212
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
216
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
231        T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ) {
232                size_t osize = malloc_size( ptr );                              // current allocation
233                size_t nsize = dim * sizeof(T);                                 // new allocation
234                T * nptr = alloc_align( ptr, align, nsize );    // CFA alloc_align
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 ) {
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 = alloc_align( ptr, align, nsize );            // CFA alloc_align
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
253} // distribution
254
255static inline forall( dtype T | sized(T) ) {
256        // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
257        T * memset( T * dest, char fill ) {
258                return (T *)memset( dest, fill, sizeof(T) );
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) ) {
267        // Cforall safe initialization/copy, i.e., implicit size specification, array types
268        T * amemset( T dest[], char fill, size_t dim ) {
269                return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
270        } // amemset
271
272        T * amemcpy( T dest[], const T src[], size_t dim ) {
273                return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
274        } // amemcpy
275} // distribution
276
277// Cforall allocation/deallocation and constructor/destructor, non-array types
278forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
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 );
281
282// Cforall allocation/deallocation and constructor/destructor, array types
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 );
286
287//---------------------------------------
288
289static inline {
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 ); }
300} // distribution
301
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 );
305
306static inline {
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 ); }
321} // distribution
322
323//---------------------------------------
324
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
333
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
342
343forall( otype E | { int ?<?( E, E ); } ) {
344        void qsort( E * vals, size_t dim );
345} // distribution
346
347//---------------------------------------
348
349extern "C" {                                                                                    // override C version
350        void srandom( unsigned int seed );
351        long int random( void );                                                        // GENERATES POSITIVE AND NEGATIVE VALUES
352        // For positive values, use unsigned int, e.g., unsigned int r = random() % 100U;
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)
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)
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
378
379//---------------------------------------
380
381#include "common.hfa"
382
383//---------------------------------------
384
385extern bool threading_enabled(void) OPTIONAL_THREAD;
386
387// Local Variables: //
388// mode: c //
389// tab-width: 4 //
390// End: //
Note: See TracBrowser for help on using the repository browser.