source: libcfa/src/stdlib.hfa@ 24ff3b0

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 24ff3b0 was 7df201c, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add 3 new alloc routines to safely bulk initialize storage

  • Property mode set to 100644
File size: 10.3 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
[7df201c]12// Last Modified On : Fri Sep 27 12:09:18 2019
13// Update Count : 381
[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
[7df201c]83 T * alloc( T & fill ) {
84 return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value
85 } // alloc
86
[74b19fb]87 T * alloc( size_t dim ) {
[d6b03b7]88 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
89 else return (T *)memalign( _Alignof(T), dim * sizeof(T) );
[74b19fb]90 } // alloc
91
92 T * alloc( size_t dim, char fill ) {
[d6b03b7]93 return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
[74b19fb]94 } // alloc
95
[7df201c]96 T * alloc( size_t dim, T & fill ) {
97 T * r = (T *)alloc( dim );
98 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
99 return r;
100 } // alloc
101
102 T * alloc( size_t dim, T fill[] ) {
103 return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value
104 } // alloc
105
[74b19fb]106 T * alloc( T ptr[], size_t dim ) {
[d6b03b7]107 return realloc( ptr, dim * sizeof(T) );
[74b19fb]108 } // alloc
109} // distribution
[6065b3aa]110
[f3fc631f]111
[3ce0d440]112static inline forall( dtype T | sized(T) ) {
113 T * align_alloc( size_t align ) {
114 return (T *)memalign( align, sizeof(T) );
115 } // align_alloc
116
117 T * align_alloc( size_t align, char fill ) {
118 T * ptr = (T *)memalign( align, sizeof(T) );
119 return (T *)memset( ptr, (int)fill, sizeof(T) );
120 } // align_alloc
121
122 T * align_alloc( size_t align, size_t dim ) {
123 return (T *)memalign( align, dim * sizeof(T) );
124 } // align_alloc
125
126 T * align_alloc( size_t align, size_t dim, char fill ) {
[d46ed6e]127 if ( fill == '\0' ) {
[d6b03b7]128 return (T *)cmemalign( align, dim, sizeof(T) );
[d46ed6e]129 } else {
[d6b03b7]130 return (T *)memset( (T *)memalign( align, dim * sizeof(T) ), (int)fill, dim * sizeof(T) );
[d46ed6e]131 } // if
[3ce0d440]132 } // align_alloc
133} // distribution
134
[d6b03b7]135forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );
136
[3ce0d440]137
138static inline forall( dtype T | sized(T) ) {
139 // data, non-array types
140
[b9c04946]141 T * memset( T * dest, char fill ) {
142 return (T *)memset( dest, fill, sizeof(T) );
[3ce0d440]143 } // memset
144
145 T * memcpy( T * dest, const T * src ) {
146 return (T *)memcpy( dest, src, sizeof(T) );
147 } // memcpy
148} // distribution
149
150static inline forall( dtype T | sized(T) ) {
151 // data, array types
152
[b9c04946]153 T * amemset( T dest[], char fill, size_t dim ) {
154 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
155 } // amemset
[3ce0d440]156
[b9c04946]157 T * amemcpy( T dest[], const T src[], size_t dim ) {
[3ce0d440]158 return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
[b9c04946]159 } // amemcpy
[3ce0d440]160} // distribution
[f3fc631f]161
[6065b3aa]162// allocation/deallocation and constructor/destructor, non-array types
[aca65621]163forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
[83a071f9]164forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
165forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
[627f585]166
[6065b3aa]167// allocation/deallocation and constructor/destructor, array types
[aca65621]168forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
169forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
170forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
[6065b3aa]171
[bd85400]172//---------------------------------------
173
[57fc7d8]174static inline {
175 int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
176 unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
177 long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
178 unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
179 long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
180 unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
181
182 float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); }
183 double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); }
184 long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); }
185} // distribution
[e672372]186
[bd85400]187float _Complex strto( const char * sptr, char ** eptr );
188double _Complex strto( const char * sptr, char ** eptr );
189long double _Complex strto( const char * sptr, char ** eptr );
190
[57fc7d8]191static inline {
[5ea5b28]192 int ato( const char * sptr ) { return (int)strtol( sptr, 0, 10 ); }
[57fc7d8]193 unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); }
194 long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); }
195 unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); }
196 long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); }
197 unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); }
198
199 float ato( const char * sptr ) { return strtof( sptr, 0 ); }
200 double ato( const char * sptr ) { return strtod( sptr, 0 ); }
201 long double ato( const char * sptr ) { return strtold( sptr, 0 ); }
202
203 float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
204 double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
205 long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
206} // distribution
[e672372]207
[bd85400]208//---------------------------------------
209
[3ce0d440]210forall( otype E | { int ?<?( E, E ); } ) {
211 E * bsearch( E key, const E * vals, size_t dim );
212 size_t bsearch( E key, const E * vals, size_t dim );
213 E * bsearchl( E key, const E * vals, size_t dim );
214 size_t bsearchl( E key, const E * vals, size_t dim );
215 E * bsearchu( E key, const E * vals, size_t dim );
216 size_t bsearchu( E key, const E * vals, size_t dim );
217} // distribution
[9c47a47]218
[3ce0d440]219forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
220 E * bsearch( K key, const E * vals, size_t dim );
221 size_t bsearch( K key, const E * vals, size_t dim );
222 E * bsearchl( K key, const E * vals, size_t dim );
223 size_t bsearchl( K key, const E * vals, size_t dim );
224 E * bsearchu( K key, const E * vals, size_t dim );
225 size_t bsearchu( K key, const E * vals, size_t dim );
226} // distribution
[bd85400]227
[b9c04946]228forall( otype E | { int ?<?( E, E ); } ) {
229 void qsort( E * vals, size_t dim );
230} // distribution
231
[bd85400]232//---------------------------------------
233
[bbe1a87]234extern "C" { // override C version
235 void srandom( unsigned int seed );
236 long int random( void );
237} // extern "C"
238
239static inline {
240 long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
241 long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u)
242 unsigned long int random( void ) { return lrand48(); }
243 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)
244 unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u)
245
246 char random( void ) { return (unsigned long int)random(); }
247 char random( char u ) { return random( (unsigned long int)u ); } // [0,u)
248 char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
249 int random( void ) { return (long int)random(); }
250 int random( int u ) { return random( (long int)u ); } // [0,u]
251 int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u)
252 unsigned int random( void ) { return (unsigned long int)random(); }
253 unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u]
254 unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
255} // distribution
256
257float random( void ); // [0.0, 1.0)
258double random( void ); // [0.0, 1.0)
259float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
260double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
261long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
[bd85400]262
263//---------------------------------------
264
[58b6d1b]265#include "common.hfa"
[bd85400]266
[2026bb6]267//---------------------------------------
268
269extern bool threading_enabled(void) OPTIONAL_THREAD;
270
[bd85400]271// Local Variables: //
272// mode: c //
273// tab-width: 4 //
274// End: //
Note: See TracBrowser for help on using the repository browser.