source: libcfa/src/stdlib.hfa@ 97cba9f

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

change alloc_set with array initialization to have old and new dimension arguments

  • Property mode set to 100644
File size: 15.1 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
[c354108]12// Last Modified On : Fri Aug 14 23:38:50 2020
13// Update Count : 504
[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
[c354108]41#include "common.hfa"
42
43//---------------------------------------
44
[b0a0ee4]45// Macro because of returns
46#define $VAR_ALLOC( allocation, alignment ) \
47 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)allocation( (size_t)sizeof(T) ); /* C allocation */ \
48 else return (T *)alignment( _Alignof(T), sizeof(T) )
49
50#define $ARRAY_ALLOC( allocation, alignment, dim ) \
51 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)allocation( dim, (size_t)sizeof(T) ); /* C allocation */ \
52 else return (T *)alignment( _Alignof(T), dim, sizeof(T) )
53
54#define $RE_SPECIALS( ptr, size, allocation, alignment ) \
55 if ( unlikely( size == 0 ) || unlikely( ptr == 0p ) ) { \
56 if ( unlikely( size == 0 ) ) free( ptr ); \
57 $VAR_ALLOC( malloc, memalign ); \
58 } /* if */
59
[74b19fb]60static inline forall( dtype T | sized(T) ) {
[ca7949b]61 // Cforall safe equivalents, i.e., implicit size specification
[3ce0d440]62
[74b19fb]63 T * malloc( void ) {
[b0a0ee4]64 $VAR_ALLOC( malloc, memalign );
[74b19fb]65 } // malloc
66
[856fe3e]67 T * aalloc( size_t dim ) {
[b0a0ee4]68 $ARRAY_ALLOC( aalloc, amemalign, dim );
[856fe3e]69 } // aalloc
70
[74b19fb]71 T * calloc( size_t dim ) {
[b0a0ee4]72 $ARRAY_ALLOC( calloc, cmemalign, dim );
[74b19fb]73 } // calloc
74
[b89c7c2]75 T * resize( T * ptr, size_t size ) { // CFA resize, eliminate return-type cast
[b0a0ee4]76 $RE_SPECIALS( ptr, size, malloc, memalign );
[60062be]77 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)resize( (void *)ptr, size ); // CFA resize
78 else return (T *)(void *)resize( (void *)ptr, _Alignof(T), size ); // CFA resize
[856fe3e]79 } // resize
80
[d74369b]81 T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast
[b0a0ee4]82 $RE_SPECIALS( ptr, size, malloc, memalign );
[60062be]83 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
84 else return (T *)(void *)realloc( (void *)ptr, _Alignof(T), size ); // CFA realloc
[74b19fb]85 } // realloc
86
87 T * memalign( size_t align ) {
[cafb687]88 return (T *)memalign( align, sizeof(T) ); // C memalign
[74b19fb]89 } // memalign
90
[856fe3e]91 T * amemalign( size_t align, size_t dim ) {
92 return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign
93 } // amemalign
94
[d74369b]95 T * cmemalign( size_t align, size_t dim ) {
96 return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
97 } // cmemalign
98
[74b19fb]99 T * aligned_alloc( size_t align ) {
[cafb687]100 return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc
[74b19fb]101 } // aligned_alloc
102
103 int posix_memalign( T ** ptr, size_t align ) {
104 return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
105 } // posix_memalign
[ada0246d]106
107 T * valloc( void ) {
108 return (T *)valloc( sizeof(T) ); // C valloc
109 } // valloc
110
111 T * pvalloc( void ) {
112 return (T *)pvalloc( sizeof(T) ); // C pvalloc
113 } // pvalloc
[cfbc703d]114} // distribution
[74b19fb]115
[cfbc703d]116static inline forall( dtype T | sized(T) ) {
[ca7949b]117 // Cforall safe general allocation, fill, resize, array
[74b19fb]118
119 T * alloc( void ) {
[d6b03b7]120 return malloc();
[74b19fb]121 } // alloc
122
[cafb687]123 T * alloc( size_t dim ) {
[856fe3e]124 return aalloc( dim );
[74b19fb]125 } // alloc
126
[cfbc703d]127 forall( dtype S | sized(S) )
128 T * alloc( S ptr[], size_t dim = 1 ) { // singleton/array resize
[60062be]129 return resize( (T *)ptr, dim * sizeof(T) ); // CFA resize
[cfbc703d]130 } // alloc
131
[60062be]132 T * alloc( T ptr[], size_t dim = 1, bool copy = true ) {
[b89c7c2]133 if ( copy ) {
134 return realloc( ptr, dim * sizeof(T) ); // CFA realloc
[cfbc703d]135 } else {
[b89c7c2]136 return resize( ptr, dim * sizeof(T) ); // CFA resize
[cfbc703d]137 } // if
[7df201c]138 } // alloc
139
[cafb687]140 T * alloc_set( char fill ) {
141 return (T *)memset( (T *)alloc(), (int)fill, sizeof(T) ); // initialize with fill value
[ded61bf2]142 } // alloc_set
[cafb687]143
[ded61bf2]144 T * alloc_set( const T & fill ) {
[cafb687]145 return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value
[ded61bf2]146 } // alloc_set
[74b19fb]147
[cafb687]148 T * alloc_set( size_t dim, char fill ) {
[d6b03b7]149 return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
[ded61bf2]150 } // alloc_set
[74b19fb]151
[ded61bf2]152 T * alloc_set( size_t dim, const T & fill ) {
[7df201c]153 T * r = (T *)alloc( dim );
154 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
155 return r;
[ded61bf2]156 } // alloc_set
[7df201c]157
[c354108]158 T * alloc_set( size_t dimNew, const T fill[], size_t dimOld ) {
159 return (T *)memcpy( (T *)alloc( dimNew ), fill, min( dimNew, dimOld ) * sizeof(T) ); // initialize with fill value
[ded61bf2]160 } // alloc_set
[6065b3aa]161
[b89c7c2]162 T * alloc_set( T ptr[], size_t dim, char fill ) { // realloc array with fill
163 size_t osize = malloc_size( ptr ); // current allocation
[d8d8f20]164 size_t nsize = dim * sizeof(T); // new allocation
165 T * nptr = realloc( ptr, nsize ); // CFA realloc
[b89c7c2]166 if ( nsize > osize ) { // larger ?
167 memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage
168 } // if
[60062be]169 return nptr;
[b89c7c2]170 } // alloc_set
171
[ded61bf2]172 T * alloc_set( T ptr[], size_t dim, const T & fill ) { // realloc array with fill
[d8d8f20]173 size_t odim = malloc_size( ptr ) / sizeof(T); // current dimension
174 size_t nsize = dim * sizeof(T); // new allocation
175 size_t ndim = nsize / sizeof(T); // new dimension
176 T * nptr = realloc( ptr, nsize ); // CFA realloc
[b89c7c2]177 if ( ndim > odim ) { // larger ?
178 for ( i; odim ~ ndim ) {
179 memcpy( &nptr[i], &fill, sizeof(T) ); // initialize with fill value
180 } // for
181 } // if
[60062be]182 return nptr;
[ded61bf2]183 } // alloc_set
[cafb687]184} // distribution
[f3fc631f]185
[3ce0d440]186static inline forall( dtype T | sized(T) ) {
[cafb687]187 T * alloc_align( size_t align ) {
[3ce0d440]188 return (T *)memalign( align, sizeof(T) );
[cafb687]189 } // alloc_align
[3ce0d440]190
[cafb687]191 T * alloc_align( size_t align, size_t dim ) {
[3ce0d440]192 return (T *)memalign( align, dim * sizeof(T) );
[cafb687]193 } // alloc_align
194
[856fe3e]195 T * alloc_align( T * ptr, size_t align ) { // aligned realloc array
[60062be]196 return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA C realloc
[d74369b]197 } // alloc_align
198
[cfbc703d]199 forall( dtype S | sized(S) )
200 T * alloc_align( S ptr[], size_t align ) { // aligned reuse array
201 return (T *)(void *)resize( (void *)ptr, align, sizeof(T) ); // CFA realloc
202 } // alloc_align
203
[d74369b]204 T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array
205 return (T *)(void *)realloc( (void *)ptr, align, dim * sizeof(T) ); // CFA realloc
206 } // alloc_align
207
[cafb687]208 T * alloc_align_set( size_t align, char fill ) {
209 return (T *)memset( (T *)alloc_align( align ), (int)fill, sizeof(T) ); // initialize with fill value
[ded61bf2]210 } // alloc_align_set
[3ce0d440]211
[ded61bf2]212 T * alloc_align_set( size_t align, const T & fill ) {
[cafb687]213 return (T *)memcpy( (T *)alloc_align( align ), &fill, sizeof(T) ); // initialize with fill value
[ded61bf2]214 } // alloc_align_set
[d6b03b7]215
[cafb687]216 T * alloc_align_set( size_t align, size_t dim, char fill ) {
217 return (T *)memset( (T *)alloc_align( align, dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
[ded61bf2]218 } // alloc_align_set
[cafb687]219
[ded61bf2]220 T * alloc_align_set( size_t align, size_t dim, const T & fill ) {
[cafb687]221 T * r = (T *)alloc_align( align, dim );
222 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
223 return r;
[ded61bf2]224 } // alloc_align_set
[cafb687]225
[c354108]226 T * alloc_align_set( size_t align, size_t dimNew, const T fill[], size_t dimOld ) {
227 return (T *)memcpy( (T *)alloc_align( align, dimNew ), fill, min( dimNew, dimOld ) * sizeof(T) );
[ded61bf2]228 } // alloc_align_set
[cafb687]229
[b89c7c2]230 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ) {
231 size_t osize = malloc_size( ptr ); // current allocation
[d8d8f20]232 size_t nsize = dim * sizeof(T); // new allocation
[60062be]233 T * nptr = alloc_align( ptr, align, nsize );
[b89c7c2]234 if ( nsize > osize ) { // larger ?
235 memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage
236 } // if
[60062be]237 return nptr;
[b89c7c2]238 } // alloc_align_set
239
[ded61bf2]240 T * alloc_align_set( T ptr[], size_t align, size_t dim, const T & fill ) {
[d8d8f20]241 size_t odim = malloc_size( ptr ) / sizeof(T); // current dimension
242 size_t nsize = dim * sizeof(T); // new allocation
243 size_t ndim = nsize / sizeof(T); // new dimension
[60062be]244 T * nptr = alloc_align( ptr, align, nsize );
[b89c7c2]245 if ( ndim > odim ) { // larger ?
246 for ( i; odim ~ ndim ) {
247 memcpy( &nptr[i], &fill, sizeof(T) ); // initialize with fill value
248 } // for
249 } // if
[60062be]250 return nptr;
[b89c7c2]251 } // alloc_align_set
[cafb687]252} // distribution
[3ce0d440]253
254static inline forall( dtype T | sized(T) ) {
[ca7949b]255 // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
[b9c04946]256 T * memset( T * dest, char fill ) {
257 return (T *)memset( dest, fill, sizeof(T) );
[3ce0d440]258 } // memset
259
260 T * memcpy( T * dest, const T * src ) {
261 return (T *)memcpy( dest, src, sizeof(T) );
262 } // memcpy
263} // distribution
264
265static inline forall( dtype T | sized(T) ) {
[ca7949b]266 // Cforall safe initialization/copy, i.e., implicit size specification, array types
[b9c04946]267 T * amemset( T dest[], char fill, size_t dim ) {
268 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
269 } // amemset
[3ce0d440]270
[b9c04946]271 T * amemcpy( T dest[], const T src[], size_t dim ) {
[3ce0d440]272 return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
[b9c04946]273 } // amemcpy
[3ce0d440]274} // distribution
[f3fc631f]275
[ca7949b]276// Cforall allocation/deallocation and constructor/destructor, non-array types
[aca65621]277forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
[aabb846]278forall( dtype T | { void ^?{}( T & ); } ) void delete( T * ptr );
279forall( dtype T, ttype Params | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
[627f585]280
[ca7949b]281// Cforall allocation/deallocation and constructor/destructor, array types
[aca65621]282forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
283forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
284forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
[6065b3aa]285
[bd85400]286//---------------------------------------
287
[57fc7d8]288static inline {
[e3fea42]289 int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
290 unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
291 long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
292 unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
293 long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
294 unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
295
296 float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); }
297 double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); }
298 long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); }
[57fc7d8]299} // distribution
[e672372]300
[e3fea42]301float _Complex strto( const char sptr[], char ** eptr );
302double _Complex strto( const char sptr[], char ** eptr );
303long double _Complex strto( const char sptr[], char ** eptr );
[bd85400]304
[57fc7d8]305static inline {
[e3fea42]306 int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); }
307 unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); }
308 long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); }
309 unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); }
310 long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); }
311 unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); }
312
313 float ato( const char sptr[] ) { return strtof( sptr, 0p ); }
314 double ato( const char sptr[] ) { return strtod( sptr, 0p ); }
315 long double ato( const char sptr[] ) { return strtold( sptr, 0p ); }
316
317 float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
318 double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
319 long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
[57fc7d8]320} // distribution
[e672372]321
[bd85400]322//---------------------------------------
323
[3ce0d440]324forall( otype E | { int ?<?( E, E ); } ) {
325 E * bsearch( E key, const E * vals, size_t dim );
326 size_t bsearch( E key, const E * vals, size_t dim );
327 E * bsearchl( E key, const E * vals, size_t dim );
328 size_t bsearchl( E key, const E * vals, size_t dim );
329 E * bsearchu( E key, const E * vals, size_t dim );
330 size_t bsearchu( E key, const E * vals, size_t dim );
331} // distribution
[9c47a47]332
[3ce0d440]333forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
334 E * bsearch( K key, const E * vals, size_t dim );
335 size_t bsearch( K key, const E * vals, size_t dim );
336 E * bsearchl( K key, const E * vals, size_t dim );
337 size_t bsearchl( K key, const E * vals, size_t dim );
338 E * bsearchu( K key, const E * vals, size_t dim );
339 size_t bsearchu( K key, const E * vals, size_t dim );
340} // distribution
[bd85400]341
[b9c04946]342forall( otype E | { int ?<?( E, E ); } ) {
343 void qsort( E * vals, size_t dim );
344} // distribution
345
[bd85400]346//---------------------------------------
347
[bbe1a87]348extern "C" { // override C version
349 void srandom( unsigned int seed );
[4e7c0fc0]350 long int random( void ); // GENERATES POSITIVE AND NEGATIVE VALUES
351 // For positive values, use unsigned int, e.g., unsigned int r = random() % 100U;
[bbe1a87]352} // extern "C"
353
354static inline {
355 long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
356 long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u)
357 unsigned long int random( void ) { return lrand48(); }
358 unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u)
[4e7c0fc0]359 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]360
361 char random( void ) { return (unsigned long int)random(); }
362 char random( char u ) { return random( (unsigned long int)u ); } // [0,u)
363 char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
364 int random( void ) { return (long int)random(); }
365 int random( int u ) { return random( (long int)u ); } // [0,u]
366 int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u)
367 unsigned int random( void ) { return (unsigned long int)random(); }
368 unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u]
369 unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
370} // distribution
371
372float random( void ); // [0.0, 1.0)
373double random( void ); // [0.0, 1.0)
374float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
375double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
376long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
[bd85400]377
378//---------------------------------------
379
[2026bb6]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.