source: libcfa/src/stdlib.hfa@ 5453237

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

check for type alignment and use it for storage allocation

  • Property mode set to 100644
File size: 9.8 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
[d6b03b7]12// Last Modified On : Tue Jul 23 14:14:59 2019
13// Update Count : 373
[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
[3ce0d440]23extern "C" {
[57fc7d8]24 void * memalign( size_t align, size_t size ); // malloc.h
[b9c04946]25 void * memset( void * dest, int fill, size_t size ); // string.h
[57fc7d8]26 void * memcpy( void * dest, const void * src, size_t size ); // string.h
[d46ed6e]27 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA
[3ce0d440]28} // extern "C"
[e672372]29
[bd85400]30//---------------------------------------
31
[45161b4d]32#ifndef EXIT_FAILURE
33#define EXIT_FAILURE 1 // failing exit status
34#define EXIT_SUCCESS 0 // successful exit status
35#endif // ! EXIT_FAILURE
36
37//---------------------------------------
38
[74b19fb]39static inline forall( dtype T | sized(T) ) {
[3ce0d440]40 // C dynamic allocation
41
[74b19fb]42 T * malloc( void ) {
[d6b03b7]43 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
44 else return (T *)memalign( _Alignof(T), sizeof(T) );
[74b19fb]45 } // malloc
46
47 T * calloc( size_t dim ) {
[d6b03b7]48 if ( _Alignof(T) <= libAlign() )return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc
49 else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) );
[74b19fb]50 } // calloc
51
52 T * realloc( T * ptr, size_t size ) {
[d6b03b7]53 if ( unlikely( ptr == 0 ) ) return malloc();
[74b19fb]54 return (T *)(void *)realloc( (void *)ptr, size );
55 } // realloc
56
57 T * memalign( size_t align ) {
58 return (T *)memalign( align, sizeof(T) );
59 } // memalign
60
61 T * aligned_alloc( size_t align ) {
62 return (T *)aligned_alloc( align, sizeof(T) );
63 } // aligned_alloc
64
65 int posix_memalign( T ** ptr, size_t align ) {
66 return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
67 } // posix_memalign
68
[3ce0d440]69
70 // Cforall dynamic allocation
[74b19fb]71
72 T * alloc( void ) {
[d6b03b7]73 return malloc();
[74b19fb]74 } // alloc
75
76 T * alloc( char fill ) {
[d6b03b7]77 T * ptr;
78 if ( _Alignof(T) <= libAlign() ) ptr = (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
79 else ptr = (T *)memalign( _Alignof(T), sizeof(T) );
[ffaedcd]80 return (T *)memset( ptr, (int)fill, sizeof(T) ); // initialize with fill value
[74b19fb]81 } // alloc
82
83 T * alloc( size_t dim ) {
[d6b03b7]84 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
85 else return (T *)memalign( _Alignof(T), dim * sizeof(T) );
[74b19fb]86 } // alloc
87
88 T * alloc( size_t dim, char fill ) {
[d6b03b7]89 return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
[74b19fb]90 } // alloc
91
92 T * alloc( T ptr[], size_t dim ) {
[d6b03b7]93 return realloc( ptr, dim * sizeof(T) );
[74b19fb]94 } // alloc
95} // distribution
[6065b3aa]96
[f3fc631f]97
[3ce0d440]98static inline forall( dtype T | sized(T) ) {
99 T * align_alloc( size_t align ) {
100 return (T *)memalign( align, sizeof(T) );
101 } // align_alloc
102
103 T * align_alloc( size_t align, char fill ) {
104 T * ptr = (T *)memalign( align, sizeof(T) );
105 return (T *)memset( ptr, (int)fill, sizeof(T) );
106 } // align_alloc
107
108 T * align_alloc( size_t align, size_t dim ) {
109 return (T *)memalign( align, dim * sizeof(T) );
110 } // align_alloc
111
112 T * align_alloc( size_t align, size_t dim, char fill ) {
[d46ed6e]113 if ( fill == '\0' ) {
[d6b03b7]114 return (T *)cmemalign( align, dim, sizeof(T) );
[d46ed6e]115 } else {
[d6b03b7]116 return (T *)memset( (T *)memalign( align, dim * sizeof(T) ), (int)fill, dim * sizeof(T) );
[d46ed6e]117 } // if
[3ce0d440]118 } // align_alloc
119} // distribution
120
[d6b03b7]121forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );
122
[3ce0d440]123
124static inline forall( dtype T | sized(T) ) {
125 // data, non-array types
126
[b9c04946]127 T * memset( T * dest, char fill ) {
128 return (T *)memset( dest, fill, sizeof(T) );
[3ce0d440]129 } // memset
130
131 T * memcpy( T * dest, const T * src ) {
132 return (T *)memcpy( dest, src, sizeof(T) );
133 } // memcpy
134} // distribution
135
136static inline forall( dtype T | sized(T) ) {
137 // data, array types
138
[b9c04946]139 T * amemset( T dest[], char fill, size_t dim ) {
140 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
141 } // amemset
[3ce0d440]142
[b9c04946]143 T * amemcpy( T dest[], const T src[], size_t dim ) {
[3ce0d440]144 return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
[b9c04946]145 } // amemcpy
[3ce0d440]146} // distribution
[f3fc631f]147
[6065b3aa]148// allocation/deallocation and constructor/destructor, non-array types
[aca65621]149forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
[83a071f9]150forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
151forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
[627f585]152
[6065b3aa]153// allocation/deallocation and constructor/destructor, array types
[aca65621]154forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
155forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
156forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
[6065b3aa]157
[bd85400]158//---------------------------------------
159
[57fc7d8]160static inline {
161 int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
162 unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
163 long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
164 unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
165 long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
166 unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
167
168 float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); }
169 double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); }
170 long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); }
171} // distribution
[e672372]172
[bd85400]173float _Complex strto( const char * sptr, char ** eptr );
174double _Complex strto( const char * sptr, char ** eptr );
175long double _Complex strto( const char * sptr, char ** eptr );
176
[57fc7d8]177static inline {
[5ea5b28]178 int ato( const char * sptr ) { return (int)strtol( sptr, 0, 10 ); }
[57fc7d8]179 unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); }
180 long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); }
181 unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); }
182 long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); }
183 unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); }
184
185 float ato( const char * sptr ) { return strtof( sptr, 0 ); }
186 double ato( const char * sptr ) { return strtod( sptr, 0 ); }
187 long double ato( const char * sptr ) { return strtold( sptr, 0 ); }
188
189 float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
190 double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
191 long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
192} // distribution
[e672372]193
[bd85400]194//---------------------------------------
195
[3ce0d440]196forall( otype E | { int ?<?( E, E ); } ) {
197 E * bsearch( E key, const E * vals, size_t dim );
198 size_t bsearch( E key, const E * vals, size_t dim );
199 E * bsearchl( E key, const E * vals, size_t dim );
200 size_t bsearchl( E key, const E * vals, size_t dim );
201 E * bsearchu( E key, const E * vals, size_t dim );
202 size_t bsearchu( E key, const E * vals, size_t dim );
203} // distribution
[9c47a47]204
[3ce0d440]205forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
206 E * bsearch( K key, const E * vals, size_t dim );
207 size_t bsearch( K key, const E * vals, size_t dim );
208 E * bsearchl( K key, const E * vals, size_t dim );
209 size_t bsearchl( K key, const E * vals, size_t dim );
210 E * bsearchu( K key, const E * vals, size_t dim );
211 size_t bsearchu( K key, const E * vals, size_t dim );
212} // distribution
[bd85400]213
[b9c04946]214forall( otype E | { int ?<?( E, E ); } ) {
215 void qsort( E * vals, size_t dim );
216} // distribution
217
[bd85400]218//---------------------------------------
219
[bbe1a87]220extern "C" { // override C version
221 void srandom( unsigned int seed );
222 long int random( void );
223} // extern "C"
224
225static inline {
226 long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
227 long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u)
228 unsigned long int random( void ) { return lrand48(); }
229 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)
230 unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u)
231
232 char random( void ) { return (unsigned long int)random(); }
233 char random( char u ) { return random( (unsigned long int)u ); } // [0,u)
234 char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
235 int random( void ) { return (long int)random(); }
236 int random( int u ) { return random( (long int)u ); } // [0,u]
237 int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u)
238 unsigned int random( void ) { return (unsigned long int)random(); }
239 unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u]
240 unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
241} // distribution
242
243float random( void ); // [0.0, 1.0)
244double random( void ); // [0.0, 1.0)
245float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
246double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
247long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
[bd85400]248
249//---------------------------------------
250
[58b6d1b]251#include "common.hfa"
[bd85400]252
[2026bb6]253//---------------------------------------
254
255extern bool threading_enabled(void) OPTIONAL_THREAD;
256
[bd85400]257// Local Variables: //
258// mode: c //
259// tab-width: 4 //
260// End: //
Note: See TracBrowser for help on using the repository browser.