source: libcfa/src/stdlib.hfa @ 191a190

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

Removed a fill bug from alloc interface, changed pervious alloc tests (alloc.cfa) to comply with new alloc interface, added new tests for memory allocation (malloc.cfa and alloc2.cfa).

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