source: libcfa/src/stdlib.hfa@ c20ba169

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

add resize and more "alloc" routines

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