source: libcfa/src/stdlib.hfa@ 289a21c

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 289a21c was b89c7c2, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

move realloc special cases into inline routines to access _Alignof, rewrite realloc routines to use malloc_size versus malloc_usable_size

  • Property mode set to 100644
File size: 15.0 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 : Sun Jul 19 18:29:34 2020
13// Update Count : 463
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#include <heap.hfa>
23
24// Reduce includes by explicitly defining these routines.
25extern "C" {
26 void * memalign( size_t alignment, size_t size ); // malloc.h
27 void * pvalloc( size_t size ); // malloc.h
28 void * memset( void * dest, int fill, size_t size ); // string.h
29 void * memcpy( void * dest, const void * src, size_t size ); // string.h
30} // extern "C"
31
32//---------------------------------------
33
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
41static inline forall( dtype T | sized(T) ) {
42 // Cforall safe equivalents, i.e., implicit size specification
43
44 T * malloc( void ) {
45 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
46 else return (T *)memalign( _Alignof(T), sizeof(T) );
47 } // malloc
48
49 T * aalloc( size_t dim ) {
50 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)aalloc( dim, (size_t)sizeof(T) ); // CFA aalloc
51 else return (T *)amemalign( _Alignof(T), dim, sizeof(T) );
52 } // aalloc
53
54 T * calloc( size_t dim ) {
55 if ( _Alignof(T) <= libAlign() )return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc
56 else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) );
57 } // calloc
58
59 T * resize( T * ptr, size_t size ) { // CFA resize, eliminate return-type cast
60 if ( unlikely( size == 0 ) || unlikely( ptr == 0p ) ) { // special cases
61 if ( unlikely( size == 0 ) ) free( ptr );
62 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
63 else return (T *)memalign( _Alignof(T), sizeof(T) );
64 } // if
65 return (T *)(void *)resize( (void *)ptr, size ); // CFA resize
66 } // resize
67
68 T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast
69 if ( unlikely( size == 0 ) || unlikely( ptr == 0p ) ) { // special cases
70 if ( unlikely( size == 0 ) ) free( ptr );
71 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
72 else return (T *)memalign( _Alignof(T), sizeof(T) );
73 } // if
74 return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
75 } // realloc
76
77 T * memalign( size_t align ) {
78 return (T *)memalign( align, sizeof(T) ); // C memalign
79 } // memalign
80
81 T * amemalign( size_t align, size_t dim ) {
82 return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign
83 } // amemalign
84
85 T * cmemalign( size_t align, size_t dim ) {
86 return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
87 } // cmemalign
88
89 T * aligned_alloc( size_t align ) {
90 return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc
91 } // aligned_alloc
92
93 int posix_memalign( T ** ptr, size_t align ) {
94 return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
95 } // posix_memalign
96
97 T * valloc( void ) {
98 return (T *)valloc( sizeof(T) ); // C valloc
99 } // valloc
100
101 T * pvalloc( void ) {
102 return (T *)pvalloc( sizeof(T) ); // C pvalloc
103 } // pvalloc
104} // distribution
105
106static inline forall( dtype T | sized(T) ) {
107 // Cforall safe general allocation, fill, resize, array
108
109 T * alloc( void ) {
110 return malloc();
111 } // alloc
112
113 T * alloc( size_t dim ) {
114 return aalloc( dim );
115 } // alloc
116
117 forall( dtype S | sized(S) )
118 T * alloc( S ptr[], size_t dim = 1 ) { // singleton/array resize
119 size_t len = malloc_usable_size( ptr ); // current bucket size
120 if ( sizeof(T) * dim > len ) { // not enough space ?
121 T * temp = alloc( dim ); // new storage
122 free( ptr ); // free old storage
123 return temp;
124 } else {
125 return (T *)ptr;
126 } // if
127 } // alloc
128
129 T * alloc( T ptr[], size_t dim, bool copy = true ) {
130 if ( copy ) {
131 return realloc( ptr, dim * sizeof(T) ); // CFA realloc
132 } else {
133 return resize( ptr, dim * sizeof(T) ); // CFA resize
134 } // if
135 } // alloc
136
137 T * alloc_set( char fill ) {
138 return (T *)memset( (T *)alloc(), (int)fill, sizeof(T) ); // initialize with fill value
139 } // alloc
140
141 T * alloc_set( T fill ) {
142 return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value
143 } // alloc
144
145 T * alloc_set( size_t dim, char fill ) {
146 return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
147 } // alloc
148
149 T * alloc_set( size_t dim, T fill ) {
150 T * r = (T *)alloc( dim );
151 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
152 return r;
153 } // alloc
154
155 T * alloc_set( size_t dim, const T fill[] ) {
156 return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value
157 } // alloc
158
159 T * alloc_set( T ptr[], size_t dim, char fill ) { // realloc array with fill
160 size_t osize = malloc_size( ptr ); // current allocation
161 T * nptr = realloc( ptr, dim * sizeof(T) ); // CFA realloc
162 size_t nsize = malloc_size( nptr ); // new allocation
163 if ( nsize > osize ) { // larger ?
164 memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage
165 } // if
166 return (T *)nptr;
167 } // alloc_set
168
169 T * alloc_set( T ptr[], size_t dim, T & fill ) { // realloc array with fill
170 size_t odim = malloc_size( ptr ) / sizeof(T); // current allocation
171 T * nptr = realloc( ptr, dim * sizeof(T) ); // CFA realloc
172 size_t ndim = malloc_size( nptr ) / sizeof(T); // new allocation
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
178 return (T *)nptr;
179 } // alloc_align_set
180} // distribution
181
182static inline forall( dtype T | sized(T) ) {
183 T * alloc_align( size_t align ) {
184 return (T *)memalign( align, sizeof(T) );
185 } // alloc_align
186
187 T * alloc_align( size_t align, size_t dim ) {
188 return (T *)memalign( align, dim * sizeof(T) );
189 } // alloc_align
190
191 T * alloc_align( T * ptr, size_t align ) { // aligned realloc array
192 return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
193 } // alloc_align
194
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
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
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
206 } // alloc_align
207
208 T * alloc_align_set( size_t align, T fill ) {
209 return (T *)memcpy( (T *)alloc_align( align ), &fill, sizeof(T) ); // initialize with fill value
210 } // alloc_align
211
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
214 } // alloc_align
215
216 T * alloc_align_set( size_t align, size_t dim, T fill ) {
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;
220 } // alloc_align
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) );
224 } // alloc_align
225
226 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ) {
227 size_t osize = malloc_size( ptr ); // current allocation
228 T * nptr = realloc( ptr, align, dim * sizeof(T) ); // CFA realloc
229 size_t nsize = malloc_size( nptr ); // new allocation
230 if ( nsize > osize ) { // larger ?
231 memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage
232 } // if
233 return (T *)nptr;
234 } // alloc_align_set
235
236 T * alloc_align_set( T ptr[], size_t align, size_t dim, T & fill ) {
237 size_t odim = malloc_size( ptr ) / sizeof(T); // current allocation
238 T * nptr = realloc( ptr, align, dim * sizeof(T) ); // CFA realloc
239 size_t ndim = malloc_size( nptr ); // new allocation
240 if ( ndim > odim ) { // larger ?
241 for ( i; odim ~ ndim ) {
242 memcpy( &nptr[i], &fill, sizeof(T) ); // initialize with fill value
243 } // for
244 } // if
245 return (T *)nptr;
246 } // alloc_align_set
247} // distribution
248
249static inline forall( dtype T | sized(T) ) {
250 // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
251 T * memset( T * dest, char fill ) {
252 return (T *)memset( dest, fill, sizeof(T) );
253 } // memset
254
255 T * memcpy( T * dest, const T * src ) {
256 return (T *)memcpy( dest, src, sizeof(T) );
257 } // memcpy
258} // distribution
259
260static inline forall( dtype T | sized(T) ) {
261 // Cforall safe initialization/copy, i.e., implicit size specification, array types
262 T * amemset( T dest[], char fill, size_t dim ) {
263 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
264 } // amemset
265
266 T * amemcpy( T dest[], const T src[], size_t dim ) {
267 return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
268 } // amemcpy
269} // distribution
270
271// Cforall allocation/deallocation and constructor/destructor, non-array types
272forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
273forall( dtype T | { void ^?{}( T & ); } ) void delete( T * ptr );
274forall( dtype T, ttype Params | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
275
276// Cforall allocation/deallocation and constructor/destructor, array types
277forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
278forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
279forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
280
281//---------------------------------------
282
283static inline {
284 int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
285 unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
286 long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
287 unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
288 long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
289 unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
290
291 float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); }
292 double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); }
293 long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); }
294} // distribution
295
296float _Complex strto( const char sptr[], char ** eptr );
297double _Complex strto( const char sptr[], char ** eptr );
298long double _Complex strto( const char sptr[], char ** eptr );
299
300static inline {
301 int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); }
302 unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); }
303 long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); }
304 unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); }
305 long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); }
306 unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); }
307
308 float ato( const char sptr[] ) { return strtof( sptr, 0p ); }
309 double ato( const char sptr[] ) { return strtod( sptr, 0p ); }
310 long double ato( const char sptr[] ) { return strtold( sptr, 0p ); }
311
312 float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
313 double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
314 long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
315} // distribution
316
317//---------------------------------------
318
319forall( otype E | { int ?<?( E, E ); } ) {
320 E * bsearch( E key, const E * vals, size_t dim );
321 size_t bsearch( E key, const E * vals, size_t dim );
322 E * bsearchl( E key, const E * vals, size_t dim );
323 size_t bsearchl( E key, const E * vals, size_t dim );
324 E * bsearchu( E key, const E * vals, size_t dim );
325 size_t bsearchu( E key, const E * vals, size_t dim );
326} // distribution
327
328forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
329 E * bsearch( K key, const E * vals, size_t dim );
330 size_t bsearch( K key, const E * vals, size_t dim );
331 E * bsearchl( K key, const E * vals, size_t dim );
332 size_t bsearchl( K key, const E * vals, size_t dim );
333 E * bsearchu( K key, const E * vals, size_t dim );
334 size_t bsearchu( K key, const E * vals, size_t dim );
335} // distribution
336
337forall( otype E | { int ?<?( E, E ); } ) {
338 void qsort( E * vals, size_t dim );
339} // distribution
340
341//---------------------------------------
342
343extern "C" { // override C version
344 void srandom( unsigned int seed );
345 long int random( void ); // GENERATES POSITIVE AND NEGATIVE VALUES
346 // For positive values, use unsigned int, e.g., unsigned int r = random() % 100U;
347} // extern "C"
348
349static inline {
350 long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
351 long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u)
352 unsigned long int random( void ) { return lrand48(); }
353 unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u)
354 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)
355
356 char random( void ) { return (unsigned long int)random(); }
357 char random( char u ) { return random( (unsigned long int)u ); } // [0,u)
358 char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
359 int random( void ) { return (long int)random(); }
360 int random( int u ) { return random( (long int)u ); } // [0,u]
361 int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u)
362 unsigned int random( void ) { return (unsigned long int)random(); }
363 unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u]
364 unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
365} // distribution
366
367float random( void ); // [0.0, 1.0)
368double random( void ); // [0.0, 1.0)
369float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
370double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
371long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
372
373//---------------------------------------
374
375#include "common.hfa"
376
377//---------------------------------------
378
379extern bool threading_enabled(void) OPTIONAL_THREAD;
380
381// Local Variables: //
382// mode: c //
383// tab-width: 4 //
384// End: //
Note: See TracBrowser for help on using the repository browser.