source: libcfa/src/stdlib.hfa@ d2b5d2d

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 d2b5d2d was ded61bf2, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

formatting, replace "T fill" with "const T & fill"

  • 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 Aug 11 21:11:46 2020
13// Update Count : 495
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 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)resize( (void *)ptr, size ); // CFA resize
74 else return (T *)(void *)resize( (void *)ptr, _Alignof(T), size ); // CFA resize
75 } // resize
76
77 T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast
78 $RE_SPECIALS( ptr, size, malloc, memalign );
79 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
80 else return (T *)(void *)realloc( (void *)ptr, _Alignof(T), size ); // CFA realloc
81 } // realloc
82
83 T * memalign( size_t align ) {
84 return (T *)memalign( align, sizeof(T) ); // C memalign
85 } // memalign
86
87 T * amemalign( size_t align, size_t dim ) {
88 return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign
89 } // amemalign
90
91 T * cmemalign( size_t align, size_t dim ) {
92 return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
93 } // cmemalign
94
95 T * aligned_alloc( size_t align ) {
96 return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc
97 } // aligned_alloc
98
99 int posix_memalign( T ** ptr, size_t align ) {
100 return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
101 } // posix_memalign
102
103 T * valloc( void ) {
104 return (T *)valloc( sizeof(T) ); // C valloc
105 } // valloc
106
107 T * pvalloc( void ) {
108 return (T *)pvalloc( sizeof(T) ); // C pvalloc
109 } // pvalloc
110} // distribution
111
112static inline forall( dtype T | sized(T) ) {
113 // Cforall safe general allocation, fill, resize, array
114
115 T * alloc( void ) {
116 return malloc();
117 } // alloc
118
119 T * alloc( size_t dim ) {
120 return aalloc( dim );
121 } // alloc
122
123 forall( dtype S | sized(S) )
124 T * alloc( S ptr[], size_t dim = 1 ) { // singleton/array resize
125 return resize( (T *)ptr, dim * sizeof(T) ); // CFA resize
126 } // alloc
127
128 T * alloc( T ptr[], size_t dim = 1, bool copy = true ) {
129 if ( copy ) {
130 return realloc( ptr, dim * sizeof(T) ); // CFA realloc
131 } else {
132 return resize( ptr, dim * sizeof(T) ); // CFA resize
133 } // if
134 } // alloc
135
136 T * alloc_set( char fill ) {
137 return (T *)memset( (T *)alloc(), (int)fill, sizeof(T) ); // initialize with fill value
138 } // alloc_set
139
140 T * alloc_set( const T & fill ) {
141 return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value
142 } // alloc_set
143
144 T * alloc_set( size_t dim, char fill ) {
145 return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
146 } // alloc_set
147
148 T * alloc_set( size_t dim, const T & fill ) {
149 T * r = (T *)alloc( dim );
150 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
151 return r;
152 } // alloc_set
153
154 T * alloc_set( size_t dim, const T fill[] ) {
155 return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value
156 } // alloc_set
157
158 T * alloc_set( T ptr[], size_t dim, char fill ) { // realloc array with fill
159 size_t osize = malloc_size( ptr ); // current allocation
160 size_t nsize = dim * sizeof(T); // new allocation
161 T * nptr = realloc( ptr, nsize ); // CFA realloc
162 if ( nsize > osize ) { // larger ?
163 memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage
164 } // if
165 return nptr;
166 } // alloc_set
167
168 T * alloc_set( T ptr[], size_t dim, const T & fill ) { // realloc array with fill
169 size_t odim = malloc_size( ptr ) / sizeof(T); // current dimension
170 size_t nsize = dim * sizeof(T); // new allocation
171 size_t ndim = nsize / sizeof(T); // new dimension
172 T * nptr = realloc( ptr, nsize ); // CFA realloc
173 if ( ndim > odim ) { // larger ?
174 for ( i; odim ~ ndim ) {
175 memcpy( &nptr[i], &fill, sizeof(T) ); // initialize with fill value
176 } // for
177 } // if
178 return nptr;
179 } // alloc_set
180} // distribution
181
182static inline forall( dtype T | sized(T) ) {
183 T * alloc_align( size_t align ) {
184 return (T *)memalign( align, sizeof(T) );
185 } // alloc_align
186
187 T * alloc_align( size_t align, size_t dim ) {
188 return (T *)memalign( align, dim * sizeof(T) );
189 } // alloc_align
190
191 T * alloc_align( T * ptr, size_t align ) { // aligned realloc array
192 return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA C realloc
193 } // alloc_align
194
195 forall( dtype S | sized(S) )
196 T * alloc_align( S ptr[], size_t align ) { // aligned reuse array
197 return (T *)(void *)resize( (void *)ptr, align, sizeof(T) ); // CFA realloc
198 } // alloc_align
199
200 T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array
201 return (T *)(void *)realloc( (void *)ptr, align, dim * sizeof(T) ); // CFA realloc
202 } // alloc_align
203
204 T * alloc_align_set( size_t align, char fill ) {
205 return (T *)memset( (T *)alloc_align( align ), (int)fill, sizeof(T) ); // initialize with fill value
206 } // alloc_align_set
207
208 T * alloc_align_set( size_t align, const T & fill ) {
209 return (T *)memcpy( (T *)alloc_align( align ), &fill, sizeof(T) ); // initialize with fill value
210 } // alloc_align_set
211
212 T * alloc_align_set( size_t align, size_t dim, char fill ) {
213 return (T *)memset( (T *)alloc_align( align, dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
214 } // alloc_align_set
215
216 T * alloc_align_set( size_t align, size_t dim, const T & fill ) {
217 T * r = (T *)alloc_align( align, dim );
218 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
219 return r;
220 } // alloc_align_set
221
222 T * alloc_align_set( size_t align, size_t dim, const T fill[] ) {
223 return (T *)memcpy( (T *)alloc_align( align, dim ), fill, dim * sizeof(T) );
224 } // alloc_align_set
225
226 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ) {
227 size_t osize = malloc_size( ptr ); // current allocation
228 size_t nsize = dim * sizeof(T); // new allocation
229 T * nptr = alloc_align( ptr, align, nsize );
230 if ( nsize > osize ) { // larger ?
231 memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage
232 } // if
233 return nptr;
234 } // alloc_align_set
235
236 T * alloc_align_set( T ptr[], size_t align, size_t dim, const T & fill ) {
237 size_t odim = malloc_size( ptr ) / sizeof(T); // current dimension
238 size_t nsize = dim * sizeof(T); // new allocation
239 size_t ndim = nsize / sizeof(T); // new dimension
240 T * nptr = alloc_align( ptr, align, nsize );
241 if ( ndim > odim ) { // larger ?
242 for ( i; odim ~ ndim ) {
243 memcpy( &nptr[i], &fill, sizeof(T) ); // initialize with fill value
244 } // for
245 } // if
246 return nptr;
247 } // alloc_align_set
248} // distribution
249
250static inline forall( dtype T | sized(T) ) {
251 // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
252 T * memset( T * dest, char fill ) {
253 return (T *)memset( dest, fill, sizeof(T) );
254 } // memset
255
256 T * memcpy( T * dest, const T * src ) {
257 return (T *)memcpy( dest, src, sizeof(T) );
258 } // memcpy
259} // distribution
260
261static inline forall( dtype T | sized(T) ) {
262 // Cforall safe initialization/copy, i.e., implicit size specification, array types
263 T * amemset( T dest[], char fill, size_t dim ) {
264 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
265 } // amemset
266
267 T * amemcpy( T dest[], const T src[], size_t dim ) {
268 return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
269 } // amemcpy
270} // distribution
271
272// Cforall allocation/deallocation and constructor/destructor, non-array types
273forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
274forall( dtype T | { void ^?{}( T & ); } ) void delete( T * ptr );
275forall( dtype T, ttype Params | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
276
277// Cforall allocation/deallocation and constructor/destructor, array types
278forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
279forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
280forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
281
282//---------------------------------------
283
284static inline {
285 int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
286 unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
287 long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
288 unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
289 long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
290 unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
291
292 float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); }
293 double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); }
294 long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); }
295} // distribution
296
297float _Complex strto( const char sptr[], char ** eptr );
298double _Complex strto( const char sptr[], char ** eptr );
299long double _Complex strto( const char sptr[], char ** eptr );
300
301static inline {
302 int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); }
303 unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); }
304 long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); }
305 unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); }
306 long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); }
307 unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); }
308
309 float ato( const char sptr[] ) { return strtof( sptr, 0p ); }
310 double ato( const char sptr[] ) { return strtod( sptr, 0p ); }
311 long double ato( const char sptr[] ) { return strtold( sptr, 0p ); }
312
313 float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
314 double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
315 long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
316} // distribution
317
318//---------------------------------------
319
320forall( otype E | { int ?<?( E, E ); } ) {
321 E * bsearch( E key, const E * vals, size_t dim );
322 size_t bsearch( E key, const E * vals, size_t dim );
323 E * bsearchl( E key, const E * vals, size_t dim );
324 size_t bsearchl( E key, const E * vals, size_t dim );
325 E * bsearchu( E key, const E * vals, size_t dim );
326 size_t bsearchu( E key, const E * vals, size_t dim );
327} // distribution
328
329forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
330 E * bsearch( K key, const E * vals, size_t dim );
331 size_t bsearch( K key, const E * vals, size_t dim );
332 E * bsearchl( K key, const E * vals, size_t dim );
333 size_t bsearchl( K key, const E * vals, size_t dim );
334 E * bsearchu( K key, const E * vals, size_t dim );
335 size_t bsearchu( K key, const E * vals, size_t dim );
336} // distribution
337
338forall( otype E | { int ?<?( E, E ); } ) {
339 void qsort( E * vals, size_t dim );
340} // distribution
341
342//---------------------------------------
343
344extern "C" { // override C version
345 void srandom( unsigned int seed );
346 long int random( void ); // GENERATES POSITIVE AND NEGATIVE VALUES
347 // For positive values, use unsigned int, e.g., unsigned int r = random() % 100U;
348} // extern "C"
349
350static inline {
351 long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
352 long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u)
353 unsigned long int random( void ) { return lrand48(); }
354 unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u)
355 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)
356
357 char random( void ) { return (unsigned long int)random(); }
358 char random( char u ) { return random( (unsigned long int)u ); } // [0,u)
359 char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
360 int random( void ) { return (long int)random(); }
361 int random( int u ) { return random( (long int)u ); } // [0,u]
362 int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u)
363 unsigned int random( void ) { return (unsigned long int)random(); }
364 unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u]
365 unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
366} // distribution
367
368float random( void ); // [0.0, 1.0)
369double random( void ); // [0.0, 1.0)
370float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
371double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
372long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
373
374//---------------------------------------
375
376#include "common.hfa"
377
378//---------------------------------------
379
380extern bool threading_enabled(void) OPTIONAL_THREAD;
381
382// Local Variables: //
383// mode: c //
384// tab-width: 4 //
385// End: //
Note: See TracBrowser for help on using the repository browser.