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
RevLine 
[bd85400]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//
[bb82c03]7// stdlib --
[bd85400]8//
9// Author : Peter A. Buhr
10// Created On : Thu Jan 28 17:12:35 2016
11// Last Modified By : Peter A. Buhr
[cfbc703d]12// Last Modified On : Wed Apr 1 18:38:41 2020
13// Update Count : 429
[bd85400]14//
15
[53a6c2a]16#pragma once
[17e5e2b]17
[2026bb6]18#include "bits/defs.hfa"
[d6b03b7]19#include "bits/align.hfa"
[2026bb6]20
[d46ed6e]21#include <stdlib.h> // *alloc, strto*, ato*
[d6b03b7]22
[ca7949b]23// Reduce includes by explicitly defining these routines.
[3ce0d440]24extern "C" {
[57fc7d8]25 void * memalign( size_t align, size_t size ); // malloc.h
[cfbc703d]26 size_t malloc_usable_size( void * ptr ); // malloc.h
27 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap
[b9c04946]28 void * memset( void * dest, int fill, size_t size ); // string.h
[57fc7d8]29 void * memcpy( void * dest, const void * src, size_t size ); // string.h
[cfbc703d]30 void * resize( void * oaddr, size_t size ); // CFA heap
[3ce0d440]31} // extern "C"
[e672372]32
[cfbc703d]33void * resize( void * oaddr, size_t nalign, size_t size ); // CFA heap
[d74369b]34void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap
35
[bd85400]36//---------------------------------------
37
[45161b4d]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
[74b19fb]45static inline forall( dtype T | sized(T) ) {
[ca7949b]46 // Cforall safe equivalents, i.e., implicit size specification
[3ce0d440]47
[74b19fb]48 T * malloc( void ) {
[d6b03b7]49 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
50 else return (T *)memalign( _Alignof(T), sizeof(T) );
[74b19fb]51 } // malloc
52
53 T * calloc( size_t dim ) {
[d6b03b7]54 if ( _Alignof(T) <= libAlign() )return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc
55 else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) );
[74b19fb]56 } // calloc
57
[d74369b]58 T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast
[cafb687]59 return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
[74b19fb]60 } // realloc
61
62 T * memalign( size_t align ) {
[cafb687]63 return (T *)memalign( align, sizeof(T) ); // C memalign
[74b19fb]64 } // memalign
65
[d74369b]66 T * cmemalign( size_t align, size_t dim ) {
67 return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
68 } // cmemalign
69
[74b19fb]70 T * aligned_alloc( size_t align ) {
[cafb687]71 return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc
[74b19fb]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
[cfbc703d]77} // distribution
[74b19fb]78
[cfbc703d]79static inline forall( dtype T | sized(T) ) {
[ca7949b]80 // Cforall safe general allocation, fill, resize, array
[74b19fb]81
82 T * alloc( void ) {
[d6b03b7]83 return malloc();
[74b19fb]84 } // alloc
85
[cafb687]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) );
[74b19fb]89 } // alloc
90
[cfbc703d]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
[7df201c]110 } // alloc
111
[cafb687]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
[74b19fb]118 } // alloc
119
[cafb687]120 T * alloc_set( size_t dim, char fill ) {
[d6b03b7]121 return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
[74b19fb]122 } // alloc
123
[cafb687]124 T * alloc_set( size_t dim, T fill ) {
[7df201c]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
[cafb687]130 T * alloc_set( size_t dim, const T fill[] ) {
[7df201c]131 return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value
132 } // alloc
[74b19fb]133} // distribution
[6065b3aa]134
[cafb687]135forall( dtype T | sized(T) ) {
136 T * alloc_set( T ptr[], size_t dim, char fill ); // realloc array with fill
[cfbc703d]137 T * alloc_set( T ptr[], size_t dim, T fill ); // realloc array with fill
[cafb687]138} // distribution
[f3fc631f]139
[3ce0d440]140static inline forall( dtype T | sized(T) ) {
[cafb687]141 T * alloc_align( size_t align ) {
[3ce0d440]142 return (T *)memalign( align, sizeof(T) );
[cafb687]143 } // alloc_align
[3ce0d440]144
[cafb687]145 T * alloc_align( size_t align, size_t dim ) {
[3ce0d440]146 return (T *)memalign( align, dim * sizeof(T) );
[cafb687]147 } // alloc_align
148
[d74369b]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
[cfbc703d]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
[d74369b]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
[cafb687]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
[3ce0d440]165
[cafb687]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
[d6b03b7]169
[cafb687]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) ) {
[cfbc703d]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
[cafb687]188 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill
[cfbc703d]189 T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ); // aligned realloc array with fill
[cafb687]190} // distribution
[3ce0d440]191
192static inline forall( dtype T | sized(T) ) {
[ca7949b]193 // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
[b9c04946]194 T * memset( T * dest, char fill ) {
195 return (T *)memset( dest, fill, sizeof(T) );
[3ce0d440]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) ) {
[ca7949b]204 // Cforall safe initialization/copy, i.e., implicit size specification, array types
[b9c04946]205 T * amemset( T dest[], char fill, size_t dim ) {
206 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
207 } // amemset
[3ce0d440]208
[b9c04946]209 T * amemcpy( T dest[], const T src[], size_t dim ) {
[3ce0d440]210 return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
[b9c04946]211 } // amemcpy
[3ce0d440]212} // distribution
[f3fc631f]213
[ca7949b]214// Cforall allocation/deallocation and constructor/destructor, non-array types
[aca65621]215forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
[83a071f9]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 );
[627f585]218
[ca7949b]219// Cforall allocation/deallocation and constructor/destructor, array types
[aca65621]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 );
[6065b3aa]223
[bd85400]224//---------------------------------------
225
[57fc7d8]226static inline {
[e3fea42]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 ); }
[57fc7d8]237} // distribution
[e672372]238
[e3fea42]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 );
[bd85400]242
[57fc7d8]243static inline {
[e3fea42]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 ); }
[57fc7d8]258} // distribution
[e672372]259
[bd85400]260//---------------------------------------
261
[3ce0d440]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
[9c47a47]270
[3ce0d440]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
[bd85400]279
[b9c04946]280forall( otype E | { int ?<?( E, E ); } ) {
281 void qsort( E * vals, size_t dim );
282} // distribution
283
[bd85400]284//---------------------------------------
285
[bbe1a87]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
[bd85400]314
315//---------------------------------------
316
[58b6d1b]317#include "common.hfa"
[bd85400]318
[2026bb6]319//---------------------------------------
320
321extern bool threading_enabled(void) OPTIONAL_THREAD;
322
[bd85400]323// Local Variables: //
324// mode: c //
325// tab-width: 4 //
326// End: //
Note: See TracBrowser for help on using the repository browser.