source: libcfa/src/stdlib.hfa@ 65e10b2

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 65e10b2 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
Line 
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//
7// stdlib --
8//
9// Author : Peter A. Buhr
10// Created On : Thu Jan 28 17:12:35 2016
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Sep 27 12:09:18 2019
13// Update Count : 381
14//
15
16#pragma once
17
18#include "bits/defs.hfa"
19#include "bits/align.hfa"
20
21#include <stdlib.h> // *alloc, strto*, ato*
22
23extern "C" {
24 void * memalign( size_t align, size_t size ); // malloc.h
25 void * memset( void * dest, int fill, size_t size ); // string.h
26 void * memcpy( void * dest, const void * src, size_t size ); // string.h
27 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA
28} // extern "C"
29
30//---------------------------------------
31
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
39static inline forall( dtype T | sized(T) ) {
40 // C dynamic allocation
41
42 T * malloc( void ) {
43 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
44 else return (T *)memalign( _Alignof(T), sizeof(T) );
45 } // malloc
46
47 T * calloc( size_t dim ) {
48 if ( _Alignof(T) <= libAlign() )return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc
49 else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) );
50 } // calloc
51
52 T * realloc( T * ptr, size_t size ) {
53 if ( unlikely( ptr == 0 ) ) return malloc();
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
69
70 // Cforall dynamic allocation
71
72 T * alloc( void ) {
73 return malloc();
74 } // alloc
75
76 T * alloc( char fill ) {
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) );
80 return (T *)memset( ptr, (int)fill, sizeof(T) ); // initialize with fill value
81 } // alloc
82
83 T * alloc( T & fill ) {
84 return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value
85 } // alloc
86
87 T * alloc( size_t dim ) {
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) );
90 } // alloc
91
92 T * alloc( size_t dim, char fill ) {
93 return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
94 } // alloc
95
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
106 T * alloc( T ptr[], size_t dim ) {
107 return realloc( ptr, dim * sizeof(T) );
108 } // alloc
109} // distribution
110
111
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 ) {
127 if ( fill == '\0' ) {
128 return (T *)cmemalign( align, dim, sizeof(T) );
129 } else {
130 return (T *)memset( (T *)memalign( align, dim * sizeof(T) ), (int)fill, dim * sizeof(T) );
131 } // if
132 } // align_alloc
133} // distribution
134
135forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );
136
137
138static inline forall( dtype T | sized(T) ) {
139 // data, non-array types
140
141 T * memset( T * dest, char fill ) {
142 return (T *)memset( dest, fill, sizeof(T) );
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
153 T * amemset( T dest[], char fill, size_t dim ) {
154 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
155 } // amemset
156
157 T * amemcpy( T dest[], const T src[], size_t dim ) {
158 return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
159 } // amemcpy
160} // distribution
161
162// allocation/deallocation and constructor/destructor, non-array types
163forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
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 );
166
167// allocation/deallocation and constructor/destructor, array types
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 );
171
172//---------------------------------------
173
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
186
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
191static inline {
192 int ato( const char * sptr ) { return (int)strtol( sptr, 0, 10 ); }
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
207
208//---------------------------------------
209
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
218
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
227
228forall( otype E | { int ?<?( E, E ); } ) {
229 void qsort( E * vals, size_t dim );
230} // distribution
231
232//---------------------------------------
233
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
262
263//---------------------------------------
264
265#include "common.hfa"
266
267//---------------------------------------
268
269extern bool threading_enabled(void) OPTIONAL_THREAD;
270
271// Local Variables: //
272// mode: c //
273// tab-width: 4 //
274// End: //
Note: See TracBrowser for help on using the repository browser.