source: libcfa/src/stdlib.hfa @ 68f0c4e

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 68f0c4e was 68f0c4e, checked in by m3zulfiq <m3zulfiq@…>, 4 years ago

stdlib.hfa: changed CFA malloc, realloc and resize as discussed with Peter. malloc.cfa: corrected CFA posix-memalign test. alloc.txt: corrected expected alloc test output (temporarily, yet to be reviewed by Peter) while old expected output is in alloc-old.txt (temporary file to be removed after review with Peter)

  • Property mode set to 100644
File size: 15.1 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 : Fri Aug 14 23:38:50 2020
13// Update Count     : 504
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#include "common.hfa"
42
43//---------------------------------------
44
45#define $ARRAY_ALLOC( allocation, alignment, dim ) \
46        if ( _Alignof(T) <= libAlign() ) return (T *)(void *)allocation( dim, (size_t)sizeof(T) ); /* C allocation */ \
47        else return (T *)alignment( _Alignof(T), dim, sizeof(T) )
48
49static inline forall( dtype T | sized(T) ) {
50        // Cforall safe equivalents, i.e., implicit size specification
51
52        T * malloc( void ) {
53                if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); /* C allocation */
54                else return (T *)memalign( _Alignof(T), sizeof(T) );
55        } // malloc
56
57        T * aalloc( size_t dim ) {
58                $ARRAY_ALLOC( aalloc, amemalign, dim );
59        } // aalloc
60
61        T * calloc( size_t dim ) {
62                $ARRAY_ALLOC( calloc, cmemalign, dim );
63        } // calloc
64
65        T * resize( T * ptr, size_t size ) {                            // CFA resize, eliminate return-type cast
66                if ( _Alignof(T) <= libAlign() ) return (T *)(void *)resize( (void *)ptr, size ); // CFA resize
67                else return (T *)(void *)resize( (void *)ptr, _Alignof(T), size ); // CFA resize
68        } // resize
69
70        T * realloc( T * ptr, size_t size ) {                           // CFA realloc, eliminate return-type cast
71                if ( _Alignof(T) <= libAlign() ) return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
72                else return (T *)(void *)realloc( (void *)ptr, _Alignof(T), size ); // CFA realloc
73        } // realloc
74
75        T * memalign( size_t align ) {
76                return (T *)memalign( align, sizeof(T) );               // C memalign
77        } // memalign
78
79        T * amemalign( size_t align, size_t dim ) {
80                return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign
81        } // amemalign
82
83        T * cmemalign( size_t align, size_t dim  ) {
84                return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
85        } // cmemalign
86
87        T * aligned_alloc( size_t align ) {
88                return (T *)aligned_alloc( align, sizeof(T) );  // C aligned_alloc
89        } // aligned_alloc
90
91        int posix_memalign( T ** ptr, size_t align ) {
92                return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
93        } // posix_memalign
94
95        T * valloc( void ) {
96                return (T *)valloc( sizeof(T) );                                // C valloc
97        } // valloc
98
99        T * pvalloc( void ) {
100                return (T *)pvalloc( sizeof(T) );                               // C pvalloc
101        } // pvalloc
102} // distribution
103
104/*
105        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.
106        Or, just follow the instructions below for that.
107
108        1. Replace the current forall-block that contains defintions of S_fill and S_realloc with following:
109                forall( dtype T | sized(T) ) {
110                        union  U_fill           { char c; T * a; T t; };
111                        struct S_fill           { char tag; char c; size_t size; T * at; char t[50]; };
112                        struct S_realloc        { inline T *; };
113                }
114
115        2. Replace all current postfix-fill functions with following for updated S_fill:
116                S_fill(T) ?`fill( char a )                                      { S_fill(T) ret = {'c'}; ret.fill.c = a; return ret; }
117                S_fill(T) ?`fill( T    a )                                      { S_fill(T) ret = {'t'}; memcpy(&ret.fill.t, &a, sizeof(T)); return ret; }
118                S_fill(T) ?`fill( T    a[], size_t nmemb )      { S_fill(T) ret = {'a', nmemb}; ret.fill.a = a; return ret; }
119
120        3. Replace the $alloc_internal function which is outside ttype forall-block with following function:
121                T * $alloc_internal( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill) {
122                        T * ptr = NULL;
123                        size_t size = sizeof(T);
124                        size_t copy_end = 0;
125
126                        if(Resize) {
127                                ptr = (T*) (void *) resize( (int *)Resize, Align, Dim * size );
128                        } else if (Realloc) {
129                                if (Fill.tag != '0') copy_end = min(malloc_size( Realloc ), Dim * size);
130                                ptr = (T*) (void *) realloc( (int *)Realloc, Align, Dim * size );
131                        } else {
132                                ptr = (T*) (void *) memalign( Align, Dim * size );
133                        }
134
135                        if(Fill.tag == 'c') {
136                                memset( (char *)ptr + copy_end, (int)Fill.fill.c, Dim * size - copy_end );
137                        } else if(Fill.tag == 't') {
138                                for ( int i = copy_end; i <= Dim * size - size ; i += size ) {
139                                        memcpy( (char *)ptr + i, &Fill.fill.t, size );
140                                }
141                        } else if(Fill.tag == 'a') {
142                                memcpy( (char *)ptr + copy_end, Fill.fill.a, min(Dim * size - copy_end, size * Fill.nmemb) );
143                        }
144
145                        return ptr;
146                } // $alloc_internal
147*/
148
149typedef struct S_align                  { inline size_t;  } T_align;
150typedef struct S_resize                 { inline void *;  }     T_resize;
151
152forall( dtype T ) {
153        struct S_fill           { char tag; char c; size_t size; T * at; char t[50]; };
154        struct S_realloc        { inline T *; };
155}
156
157static inline T_align   ?`align   ( size_t a )  { return (T_align){a}; }
158static inline T_resize  ?`resize  ( void * a )  { return (T_resize){a}; }
159static inline forall( dtype T | sized(T) ) {
160
161        S_fill(T) ?`fill ( T t ) {
162                S_fill(T) ret = { 't' };
163                size_t size = sizeof(T);
164                if(size > sizeof(ret.t)) { printf("ERROR: const object of size greater than 50 bytes given for dynamic memory fill\n"); exit(1); }
165                memcpy( &ret.t, &t, size );
166                return ret;
167        }
168        S_fill(T)               ?`fill ( char c )                               { return (S_fill(T)){ 'c', c }; }
169        S_fill(T)               ?`fill ( T * a )                                { return (S_fill(T)){ 'T', '0', 0, a }; }
170        S_fill(T)               ?`fill ( T a[], size_t nmemb )  { return (S_fill(T)){ 'a', '0', nmemb * sizeof(T), a }; }
171
172        S_realloc(T)    ?`realloc ( T * a )                             { return (S_realloc(T)){a}; }
173
174        T * $alloc_internal( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill) {
175                T * ptr = NULL;
176                size_t size = sizeof(T);
177                size_t copy_end = 0;
178                if(Resize) {
179//printf("1. $alloc_internal got: %p %p %lu %lu\n", Resize, Realloc, Align, Dim); // these prints are temporary
180                        ptr = (T*) (void *) resize( (void *)Resize, Align, Dim * size );
181                } else if (Realloc) {
182                        if (Fill.tag != '0') copy_end = min(malloc_size( Realloc ), Dim * size);
183//printf("2. $alloc_internal got: %p %p %lu %lu\n", Resize, Realloc, Align, Dim);
184                        ptr = (T*) (void *) realloc( (void *)Realloc, Align, Dim * size );
185                } else {
186//printf("3. $alloc_internal got: %p %p %lu %lu\n", Resize, Realloc, Align, Dim);
187                        ptr = (T*) (void *) memalign( Align, Dim * size );
188                }
189
190                if(Fill.tag == 'c') {
191                        memset( (char *)ptr + copy_end, (int)Fill.c, Dim * size - copy_end );
192                } else if(Fill.tag == 't') {
193                        for ( int i = copy_end; i < Dim * size; i += size ) {
194                                memcpy( (char *)ptr + i, &Fill.t, size );
195                        }
196                } else if(Fill.tag == 'a') {
197                        memcpy( (char *)ptr + copy_end, Fill.at, min(Dim * size - copy_end, Fill.size) );
198                } else if(Fill.tag == 'T') {
199                        for ( int i = copy_end; i < Dim * size; i += size ) {
200                                memcpy( (char *)ptr + i, Fill.at, size );
201                        }
202                }
203
204                return ptr;
205        } // $alloc_internal
206
207        forall( ttype TT | { T * $alloc_internal( void *, T *, size_t, size_t, S_fill(T), TT ); } ) {
208
209                T * $alloc_internal( void *       , T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill, T_resize Resize, TT rest) {
210                return $alloc_internal( Resize, (T*)0p, Align, Dim, Fill, rest);
211                }
212
213                T * $alloc_internal( void * Resize, T *        , size_t Align, size_t Dim, S_fill(T) Fill, S_realloc(T) Realloc, TT rest) {
214                return $alloc_internal( (void*)0p, Realloc, Align, Dim, Fill, rest);
215                }
216
217                T * $alloc_internal( void * Resize, T * Realloc, size_t      , size_t Dim, S_fill(T) Fill, T_align Align, TT rest) {
218                return $alloc_internal( Resize, Realloc, Align, Dim, Fill, rest);
219                }
220
221                T * $alloc_internal( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T)     , S_fill(T) Fill, TT rest) {
222                return $alloc_internal( Resize, Realloc, Align, Dim, Fill, rest);
223                }
224
225            T * alloc( TT all ) {
226                return $alloc_internal( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), (size_t)1, (S_fill(T)){'0'}, all);
227            }
228
229            T * alloc( size_t dim, TT all ) {
230                return $alloc_internal( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), dim, (S_fill(T)){'0'}, all);
231            }
232
233        } // distribution TT
234
235} // distribution T
236
237static inline forall( dtype T | sized(T) ) {
238        // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
239        T * memset( T * dest, char fill ) {
240                return (T *)memset( dest, fill, sizeof(T) );
241        } // memset
242
243        T * memcpy( T * dest, const T * src ) {
244                return (T *)memcpy( dest, src, sizeof(T) );
245        } // memcpy
246} // distribution
247
248static inline forall( dtype T | sized(T) ) {
249        // Cforall safe initialization/copy, i.e., implicit size specification, array types
250        T * amemset( T dest[], char fill, size_t dim ) {
251                return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
252        } // amemset
253
254        T * amemcpy( T dest[], const T src[], size_t dim ) {
255                return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
256        } // amemcpy
257} // distribution
258
259// Cforall allocation/deallocation and constructor/destructor, non-array types
260forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
261forall( dtype T | { void ^?{}( T & ); } ) void delete( T * ptr );
262forall( dtype T, ttype Params | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
263
264// Cforall allocation/deallocation and constructor/destructor, array types
265forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
266forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
267forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
268
269//---------------------------------------
270
271static inline {
272        int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
273        unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
274        long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
275        unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
276        long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
277        unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
278
279        float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); }
280        double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); }
281        long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); }
282} // distribution
283
284float _Complex strto( const char sptr[], char ** eptr );
285double _Complex strto( const char sptr[], char ** eptr );
286long double _Complex strto( const char sptr[], char ** eptr );
287
288static inline {
289        int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); }
290        unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); }
291        long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); }
292        unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); }
293        long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); }
294        unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); }
295
296        float ato( const char sptr[] ) { return strtof( sptr, 0p ); }
297        double ato( const char sptr[] ) { return strtod( sptr, 0p ); }
298        long double ato( const char sptr[] ) { return strtold( sptr, 0p ); }
299
300        float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
301        double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
302        long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
303} // distribution
304
305//---------------------------------------
306
307forall( otype E | { int ?<?( E, E ); } ) {
308        E * bsearch( E key, const E * vals, size_t dim );
309        size_t bsearch( E key, const E * vals, size_t dim );
310        E * bsearchl( E key, const E * vals, size_t dim );
311        size_t bsearchl( E key, const E * vals, size_t dim );
312        E * bsearchu( E key, const E * vals, size_t dim );
313        size_t bsearchu( E key, const E * vals, size_t dim );
314} // distribution
315
316forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
317        E * bsearch( K key, const E * vals, size_t dim );
318        size_t bsearch( K key, const E * vals, size_t dim );
319        E * bsearchl( K key, const E * vals, size_t dim );
320        size_t bsearchl( K key, const E * vals, size_t dim );
321        E * bsearchu( K key, const E * vals, size_t dim );
322        size_t bsearchu( K key, const E * vals, size_t dim );
323} // distribution
324
325forall( otype E | { int ?<?( E, E ); } ) {
326        void qsort( E * vals, size_t dim );
327} // distribution
328
329//---------------------------------------
330
331extern "C" {                                                                                    // override C version
332        void srandom( unsigned int seed );
333        long int random( void );                                                        // GENERATES POSITIVE AND NEGATIVE VALUES
334        // For positive values, use unsigned int, e.g., unsigned int r = random() % 100U;
335} // extern "C"
336
337static inline {
338        long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
339        long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u)
340        unsigned long int random( void ) { return lrand48(); }
341        unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u)
342        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)
343
344        char random( void ) { return (unsigned long int)random(); }
345        char random( char u ) { return random( (unsigned long int)u ); } // [0,u)
346        char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
347        int random( void ) { return (long int)random(); }
348        int random( int u ) { return random( (long int)u ); } // [0,u]
349        int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u)
350        unsigned int random( void ) { return (unsigned long int)random(); }
351        unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u]
352        unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
353} // distribution
354
355float random( void );                                                                   // [0.0, 1.0)
356double random( void );                                                                  // [0.0, 1.0)
357float _Complex random( void );                                                  // [0.0, 1.0)+[0.0, 1.0)i
358double _Complex random( void );                                                 // [0.0, 1.0)+[0.0, 1.0)i
359long double _Complex random( void );                                    // [0.0, 1.0)+[0.0, 1.0)i
360
361//---------------------------------------
362
363extern bool threading_enabled(void) OPTIONAL_THREAD;
364
365// Local Variables: //
366// mode: c //
367// tab-width: 4 //
368// End: //
Note: See TracBrowser for help on using the repository browser.