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
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
[b89c7c2]11// Last Modified By : Peter A. Buhr
[ded61bf2]12// Last Modified On : Tue Aug 11 21:11:46 2020
13// Update Count : 495
[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*
[4e7c0fc0]22#include <heap.hfa>
[d6b03b7]23
[ca7949b]24// Reduce includes by explicitly defining these routines.
[3ce0d440]25extern "C" {
[4e7c0fc0]26 void * memalign( size_t alignment, size_t size ); // malloc.h
27 void * pvalloc( size_t size ); // malloc.h
[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
[3ce0d440]30} // extern "C"
[e672372]31
[bd85400]32//---------------------------------------
33
[45161b4d]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
[b0a0ee4]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
[74b19fb]56static inline forall( dtype T | sized(T) ) {
[ca7949b]57 // Cforall safe equivalents, i.e., implicit size specification
[3ce0d440]58
[74b19fb]59 T * malloc( void ) {
[b0a0ee4]60 $VAR_ALLOC( malloc, memalign );
[74b19fb]61 } // malloc
62
[856fe3e]63 T * aalloc( size_t dim ) {
[b0a0ee4]64 $ARRAY_ALLOC( aalloc, amemalign, dim );
[856fe3e]65 } // aalloc
66
[74b19fb]67 T * calloc( size_t dim ) {
[b0a0ee4]68 $ARRAY_ALLOC( calloc, cmemalign, dim );
[74b19fb]69 } // calloc
70
[b89c7c2]71 T * resize( T * ptr, size_t size ) { // CFA resize, eliminate return-type cast
[b0a0ee4]72 $RE_SPECIALS( ptr, size, malloc, memalign );
[60062be]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
[856fe3e]75 } // resize
76
[d74369b]77 T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast
[b0a0ee4]78 $RE_SPECIALS( ptr, size, malloc, memalign );
[60062be]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
[74b19fb]81 } // realloc
82
83 T * memalign( size_t align ) {
[cafb687]84 return (T *)memalign( align, sizeof(T) ); // C memalign
[74b19fb]85 } // memalign
86
[856fe3e]87 T * amemalign( size_t align, size_t dim ) {
88 return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign
89 } // amemalign
90
[d74369b]91 T * cmemalign( size_t align, size_t dim ) {
92 return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
93 } // cmemalign
94
[74b19fb]95 T * aligned_alloc( size_t align ) {
[cafb687]96 return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc
[74b19fb]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
[ada0246d]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
[cfbc703d]110} // distribution
[74b19fb]111
[cfbc703d]112static inline forall( dtype T | sized(T) ) {
[ca7949b]113 // Cforall safe general allocation, fill, resize, array
[74b19fb]114
115 T * alloc( void ) {
[d6b03b7]116 return malloc();
[74b19fb]117 } // alloc
118
[cafb687]119 T * alloc( size_t dim ) {
[856fe3e]120 return aalloc( dim );
[74b19fb]121 } // alloc
122
[cfbc703d]123 forall( dtype S | sized(S) )
124 T * alloc( S ptr[], size_t dim = 1 ) { // singleton/array resize
[60062be]125 return resize( (T *)ptr, dim * sizeof(T) ); // CFA resize
[cfbc703d]126 } // alloc
127
[60062be]128 T * alloc( T ptr[], size_t dim = 1, bool copy = true ) {
[b89c7c2]129 if ( copy ) {
130 return realloc( ptr, dim * sizeof(T) ); // CFA realloc
[cfbc703d]131 } else {
[b89c7c2]132 return resize( ptr, dim * sizeof(T) ); // CFA resize
[cfbc703d]133 } // if
[7df201c]134 } // alloc
135
[cafb687]136 T * alloc_set( char fill ) {
137 return (T *)memset( (T *)alloc(), (int)fill, sizeof(T) ); // initialize with fill value
[ded61bf2]138 } // alloc_set
[cafb687]139
[ded61bf2]140 T * alloc_set( const T & fill ) {
[cafb687]141 return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value
[ded61bf2]142 } // alloc_set
[74b19fb]143
[cafb687]144 T * alloc_set( size_t dim, char fill ) {
[d6b03b7]145 return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
[ded61bf2]146 } // alloc_set
[74b19fb]147
[ded61bf2]148 T * alloc_set( size_t dim, const T & fill ) {
[7df201c]149 T * r = (T *)alloc( dim );
150 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
151 return r;
[ded61bf2]152 } // alloc_set
[7df201c]153
[cafb687]154 T * alloc_set( size_t dim, const T fill[] ) {
[7df201c]155 return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value
[ded61bf2]156 } // alloc_set
[6065b3aa]157
[b89c7c2]158 T * alloc_set( T ptr[], size_t dim, char fill ) { // realloc array with fill
159 size_t osize = malloc_size( ptr ); // current allocation
[d8d8f20]160 size_t nsize = dim * sizeof(T); // new allocation
161 T * nptr = realloc( ptr, nsize ); // CFA realloc
[b89c7c2]162 if ( nsize > osize ) { // larger ?
163 memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage
164 } // if
[60062be]165 return nptr;
[b89c7c2]166 } // alloc_set
167
[ded61bf2]168 T * alloc_set( T ptr[], size_t dim, const T & fill ) { // realloc array with fill
[d8d8f20]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
[b89c7c2]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
[60062be]178 return nptr;
[ded61bf2]179 } // alloc_set
[cafb687]180} // distribution
[f3fc631f]181
[3ce0d440]182static inline forall( dtype T | sized(T) ) {
[cafb687]183 T * alloc_align( size_t align ) {
[3ce0d440]184 return (T *)memalign( align, sizeof(T) );
[cafb687]185 } // alloc_align
[3ce0d440]186
[cafb687]187 T * alloc_align( size_t align, size_t dim ) {
[3ce0d440]188 return (T *)memalign( align, dim * sizeof(T) );
[cafb687]189 } // alloc_align
190
[856fe3e]191 T * alloc_align( T * ptr, size_t align ) { // aligned realloc array
[60062be]192 return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA C realloc
[d74369b]193 } // alloc_align
194
[cfbc703d]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
[d74369b]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
[cafb687]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
[ded61bf2]206 } // alloc_align_set
[3ce0d440]207
[ded61bf2]208 T * alloc_align_set( size_t align, const T & fill ) {
[cafb687]209 return (T *)memcpy( (T *)alloc_align( align ), &fill, sizeof(T) ); // initialize with fill value
[ded61bf2]210 } // alloc_align_set
[d6b03b7]211
[cafb687]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
[ded61bf2]214 } // alloc_align_set
[cafb687]215
[ded61bf2]216 T * alloc_align_set( size_t align, size_t dim, const T & fill ) {
[cafb687]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;
[ded61bf2]220 } // alloc_align_set
[cafb687]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) );
[ded61bf2]224 } // alloc_align_set
[cafb687]225
[b89c7c2]226 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ) {
227 size_t osize = malloc_size( ptr ); // current allocation
[d8d8f20]228 size_t nsize = dim * sizeof(T); // new allocation
[60062be]229 T * nptr = alloc_align( ptr, align, nsize );
[b89c7c2]230 if ( nsize > osize ) { // larger ?
231 memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage
232 } // if
[60062be]233 return nptr;
[b89c7c2]234 } // alloc_align_set
235
[ded61bf2]236 T * alloc_align_set( T ptr[], size_t align, size_t dim, const T & fill ) {
[d8d8f20]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
[60062be]240 T * nptr = alloc_align( ptr, align, nsize );
[b89c7c2]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
[60062be]246 return nptr;
[b89c7c2]247 } // alloc_align_set
[cafb687]248} // distribution
[3ce0d440]249
250static inline forall( dtype T | sized(T) ) {
[ca7949b]251 // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
[b9c04946]252 T * memset( T * dest, char fill ) {
253 return (T *)memset( dest, fill, sizeof(T) );
[3ce0d440]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) ) {
[ca7949b]262 // Cforall safe initialization/copy, i.e., implicit size specification, array types
[b9c04946]263 T * amemset( T dest[], char fill, size_t dim ) {
264 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
265 } // amemset
[3ce0d440]266
[b9c04946]267 T * amemcpy( T dest[], const T src[], size_t dim ) {
[3ce0d440]268 return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
[b9c04946]269 } // amemcpy
[3ce0d440]270} // distribution
[f3fc631f]271
[ca7949b]272// Cforall allocation/deallocation and constructor/destructor, non-array types
[aca65621]273forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
[aabb846]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 );
[627f585]276
[ca7949b]277// Cforall allocation/deallocation and constructor/destructor, array types
[aca65621]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 );
[6065b3aa]281
[bd85400]282//---------------------------------------
283
[57fc7d8]284static inline {
[e3fea42]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 ); }
[57fc7d8]295} // distribution
[e672372]296
[e3fea42]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 );
[bd85400]300
[57fc7d8]301static inline {
[e3fea42]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 ); }
[57fc7d8]316} // distribution
[e672372]317
[bd85400]318//---------------------------------------
319
[3ce0d440]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
[9c47a47]328
[3ce0d440]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
[bd85400]337
[b9c04946]338forall( otype E | { int ?<?( E, E ); } ) {
339 void qsort( E * vals, size_t dim );
340} // distribution
341
[bd85400]342//---------------------------------------
343
[bbe1a87]344extern "C" { // override C version
345 void srandom( unsigned int seed );
[4e7c0fc0]346 long int random( void ); // GENERATES POSITIVE AND NEGATIVE VALUES
347 // For positive values, use unsigned int, e.g., unsigned int r = random() % 100U;
[bbe1a87]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)
[4e7c0fc0]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)
[bbe1a87]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
[bd85400]373
374//---------------------------------------
375
[58b6d1b]376#include "common.hfa"
[bd85400]377
[2026bb6]378//---------------------------------------
379
380extern bool threading_enabled(void) OPTIONAL_THREAD;
381
[bd85400]382// Local Variables: //
383// mode: c //
384// tab-width: 4 //
385// End: //
Note: See TracBrowser for help on using the repository browser.