source: libcfa/src/stdlib.hfa@ c1dfa4e

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since c1dfa4e was 856fe3e, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

walk through allocation code in stdlib.hfa and fix a few problems

  • Property mode set to 100644
File size: 13.5 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 : Wed May 13 17:23:51 2020
13// Update Count : 435
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
23// Reduce includes by explicitly defining these routines.
24extern "C" {
25 void * aalloc( size_t dim, size_t elemSize ); // CFA heap
26 void * resize( void * oaddr, size_t size ); // CFA heap
27 void * memalign( size_t align, size_t size ); // malloc.h
28 void * amemalign( size_t align, size_t dim, size_t elemSize ); // CFA heap
29 void * cmemalign( size_t align, size_t noOfElems, size_t elemSize ); // CFA heap
30 size_t malloc_size( void * addr ); // CFA heap
31 size_t malloc_usable_size( void * ptr ); // malloc.h
32 void * memset( void * dest, int fill, size_t size ); // string.h
33 void * memcpy( void * dest, const void * src, size_t size ); // string.h
34} // extern "C"
35
36void * resize( void * oaddr, size_t nalign, size_t size ); // CFA heap
37void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap
38
39//---------------------------------------
40
41#ifndef EXIT_FAILURE
42#define EXIT_FAILURE 1 // failing exit status
43#define EXIT_SUCCESS 0 // successful exit status
44#endif // ! EXIT_FAILURE
45
46//---------------------------------------
47
48static inline forall( dtype T | sized(T) ) {
49 // Cforall safe equivalents, i.e., implicit size specification
50
51 T * malloc( void ) {
52 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
53 else return (T *)memalign( _Alignof(T), sizeof(T) );
54 } // malloc
55
56 T * aalloc( size_t dim ) {
57 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)aalloc( dim, (size_t)sizeof(T) ); // CFA aalloc
58 else return (T *)amemalign( _Alignof(T), dim, sizeof(T) );
59 } // aalloc
60
61 T * calloc( size_t dim ) {
62 if ( _Alignof(T) <= libAlign() )return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc
63 else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) );
64 } // calloc
65
66 T * resize( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast
67 return (T *)(void *)resize( (void *)ptr, size ); // C realloc
68 } // resize
69
70 T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast
71 return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
72 } // realloc
73
74 T * memalign( size_t align ) {
75 return (T *)memalign( align, sizeof(T) ); // C memalign
76 } // memalign
77
78 T * amemalign( size_t align, size_t dim ) {
79 return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign
80 } // amemalign
81
82 T * cmemalign( size_t align, size_t dim ) {
83 return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
84 } // cmemalign
85
86 T * aligned_alloc( size_t align ) {
87 return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc
88 } // aligned_alloc
89
90 int posix_memalign( T ** ptr, size_t align ) {
91 return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
92 } // posix_memalign
93} // distribution
94
95static inline forall( dtype T | sized(T) ) {
96 // Cforall safe general allocation, fill, resize, array
97
98 T * alloc( void ) {
99 return malloc();
100 } // alloc
101
102 T * alloc( size_t dim ) {
103 return aalloc( dim );
104 } // alloc
105
106 forall( dtype S | sized(S) )
107 T * alloc( S ptr[], size_t dim = 1 ) { // singleton/array resize
108 size_t len = malloc_usable_size( ptr ); // current bucket size
109 if ( sizeof(T) * dim > len ) { // not enough space ?
110 T * temp = alloc( dim ); // new storage
111 free( ptr ); // free old storage
112 return temp;
113 } else {
114 return (T *)ptr;
115 } // if
116 } // alloc
117
118 T * alloc( T ptr[], size_t dim, bool copy = true ) {
119 if ( copy ) { // realloc
120 return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
121 } else {
122 return resize( ptr, dim * sizeof(T) ); // resize
123 } // if
124 } // alloc
125
126 T * alloc_set( char fill ) {
127 return (T *)memset( (T *)alloc(), (int)fill, sizeof(T) ); // initialize with fill value
128 } // alloc
129
130 T * alloc_set( T fill ) {
131 return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value
132 } // alloc
133
134 T * alloc_set( size_t dim, char fill ) {
135 return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
136 } // alloc
137
138 T * alloc_set( size_t dim, T fill ) {
139 T * r = (T *)alloc( dim );
140 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
141 return r;
142 } // alloc
143
144 T * alloc_set( size_t dim, const T fill[] ) {
145 return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value
146 } // alloc
147} // distribution
148
149forall( dtype T | sized(T) ) {
150 T * alloc_set( T ptr[], size_t dim, char fill ); // realloc array with fill
151 T * alloc_set( T ptr[], size_t dim, T fill ); // realloc array with fill
152} // distribution
153
154static inline forall( dtype T | sized(T) ) {
155 T * alloc_align( size_t align ) {
156 return (T *)memalign( align, sizeof(T) );
157 } // alloc_align
158
159 T * alloc_align( size_t align, size_t dim ) {
160 return (T *)memalign( align, dim * sizeof(T) );
161 } // alloc_align
162
163 T * alloc_align( T * ptr, size_t align ) { // aligned realloc array
164 return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
165 } // alloc_align
166
167 forall( dtype S | sized(S) )
168 T * alloc_align( S ptr[], size_t align ) { // aligned reuse array
169 return (T *)(void *)resize( (void *)ptr, align, sizeof(T) ); // CFA realloc
170 } // alloc_align
171
172 T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array
173 return (T *)(void *)realloc( (void *)ptr, align, dim * sizeof(T) ); // CFA realloc
174 } // alloc_align
175
176 T * alloc_align_set( size_t align, char fill ) {
177 return (T *)memset( (T *)alloc_align( align ), (int)fill, sizeof(T) ); // initialize with fill value
178 } // alloc_align
179
180 T * alloc_align_set( size_t align, T fill ) {
181 return (T *)memcpy( (T *)alloc_align( align ), &fill, sizeof(T) ); // initialize with fill value
182 } // alloc_align
183
184 T * alloc_align_set( size_t align, size_t dim, char fill ) {
185 return (T *)memset( (T *)alloc_align( align, dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
186 } // alloc_align
187
188 T * alloc_align_set( size_t align, size_t dim, T fill ) {
189 T * r = (T *)alloc_align( align, dim );
190 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
191 return r;
192 } // alloc_align
193
194 T * alloc_align_set( size_t align, size_t dim, const T fill[] ) {
195 return (T *)memcpy( (T *)alloc_align( align, dim ), fill, dim * sizeof(T) );
196 } // alloc_align
197} // distribution
198
199forall( dtype T | sized(T) ) {
200 T * alloc_align_set( T ptr[], size_t align, char fill ); // aligned realloc with fill
201 T * alloc_align_set( T ptr[], size_t align, T fill ); // aligned realloc with fill
202 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill
203 T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ); // aligned realloc array with fill
204} // distribution
205
206static inline forall( dtype T | sized(T) ) {
207 // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
208 T * memset( T * dest, char fill ) {
209 return (T *)memset( dest, fill, sizeof(T) );
210 } // memset
211
212 T * memcpy( T * dest, const T * src ) {
213 return (T *)memcpy( dest, src, sizeof(T) );
214 } // memcpy
215} // distribution
216
217static inline forall( dtype T | sized(T) ) {
218 // Cforall safe initialization/copy, i.e., implicit size specification, array types
219 T * amemset( T dest[], char fill, size_t dim ) {
220 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
221 } // amemset
222
223 T * amemcpy( T dest[], const T src[], size_t dim ) {
224 return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
225 } // amemcpy
226} // distribution
227
228// Cforall allocation/deallocation and constructor/destructor, non-array types
229forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
230forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
231forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
232
233// Cforall allocation/deallocation and constructor/destructor, array types
234forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
235forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
236forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
237
238//---------------------------------------
239
240static inline {
241 int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
242 unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
243 long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
244 unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
245 long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
246 unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
247
248 float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); }
249 double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); }
250 long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); }
251} // distribution
252
253float _Complex strto( const char sptr[], char ** eptr );
254double _Complex strto( const char sptr[], char ** eptr );
255long double _Complex strto( const char sptr[], char ** eptr );
256
257static inline {
258 int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); }
259 unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); }
260 long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); }
261 unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); }
262 long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); }
263 unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); }
264
265 float ato( const char sptr[] ) { return strtof( sptr, 0p ); }
266 double ato( const char sptr[] ) { return strtod( sptr, 0p ); }
267 long double ato( const char sptr[] ) { return strtold( sptr, 0p ); }
268
269 float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
270 double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
271 long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
272} // distribution
273
274//---------------------------------------
275
276forall( otype E | { int ?<?( E, E ); } ) {
277 E * bsearch( E key, const E * vals, size_t dim );
278 size_t bsearch( E key, const E * vals, size_t dim );
279 E * bsearchl( E key, const E * vals, size_t dim );
280 size_t bsearchl( E key, const E * vals, size_t dim );
281 E * bsearchu( E key, const E * vals, size_t dim );
282 size_t bsearchu( E key, const E * vals, size_t dim );
283} // distribution
284
285forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
286 E * bsearch( K key, const E * vals, size_t dim );
287 size_t bsearch( K key, const E * vals, size_t dim );
288 E * bsearchl( K key, const E * vals, size_t dim );
289 size_t bsearchl( K key, const E * vals, size_t dim );
290 E * bsearchu( K key, const E * vals, size_t dim );
291 size_t bsearchu( K key, const E * vals, size_t dim );
292} // distribution
293
294forall( otype E | { int ?<?( E, E ); } ) {
295 void qsort( E * vals, size_t dim );
296} // distribution
297
298//---------------------------------------
299
300extern "C" { // override C version
301 void srandom( unsigned int seed );
302 long int random( void );
303} // extern "C"
304
305static inline {
306 long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
307 long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u)
308 unsigned long int random( void ) { return lrand48(); }
309 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)
310 unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u)
311
312 char random( void ) { return (unsigned long int)random(); }
313 char random( char u ) { return random( (unsigned long int)u ); } // [0,u)
314 char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
315 int random( void ) { return (long int)random(); }
316 int random( int u ) { return random( (long int)u ); } // [0,u]
317 int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u)
318 unsigned int random( void ) { return (unsigned long int)random(); }
319 unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u]
320 unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
321} // distribution
322
323float random( void ); // [0.0, 1.0)
324double random( void ); // [0.0, 1.0)
325float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
326double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
327long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
328
329//---------------------------------------
330
331#include "common.hfa"
332
333//---------------------------------------
334
335extern bool threading_enabled(void) OPTIONAL_THREAD;
336
337// Local Variables: //
338// mode: c //
339// tab-width: 4 //
340// End: //
Note: See TracBrowser for help on using the repository browser.