source: libcfa/src/stdlib.hfa@ f4530d7

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 f4530d7 was 856fe3e, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

walk through allocation code in stdlib.hfa and fix a few problems

  • Property mode set to 100644
File size: 13.5 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
[856fe3e]12// Last Modified On : Wed May 13 17:23:51 2020
13// Update Count : 435
[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
[ca7949b]23// Reduce includes by explicitly defining these routines.
[3ce0d440]24extern "C" {
[856fe3e]25 void * aalloc( size_t dim, size_t elemSize ); // CFA heap
26 void * resize( void * oaddr, size_t size ); // CFA heap
[57fc7d8]27 void * memalign( size_t align, size_t size ); // malloc.h
[856fe3e]28 void * amemalign( size_t align, size_t dim, size_t elemSize ); // CFA heap
29 void * cmemalign( size_t align, size_t noOfElems, size_t elemSize ); // CFA heap
[76e2113]30 size_t malloc_size( void * addr ); // CFA heap
[856fe3e]31 size_t malloc_usable_size( void * ptr ); // malloc.h
[b9c04946]32 void * memset( void * dest, int fill, size_t size ); // string.h
[57fc7d8]33 void * memcpy( void * dest, const void * src, size_t size ); // string.h
[3ce0d440]34} // extern "C"
[e672372]35
[cfbc703d]36void * resize( void * oaddr, size_t nalign, size_t size ); // CFA heap
[d74369b]37void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap
38
[bd85400]39//---------------------------------------
40
[45161b4d]41#ifndef EXIT_FAILURE
42#define EXIT_FAILURE 1 // failing exit status
43#define EXIT_SUCCESS 0 // successful exit status
44#endif // ! EXIT_FAILURE
45
46//---------------------------------------
47
[74b19fb]48static inline forall( dtype T | sized(T) ) {
[ca7949b]49 // Cforall safe equivalents, i.e., implicit size specification
[3ce0d440]50
[74b19fb]51 T * malloc( void ) {
[d6b03b7]52 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
53 else return (T *)memalign( _Alignof(T), sizeof(T) );
[74b19fb]54 } // malloc
55
[856fe3e]56 T * aalloc( size_t dim ) {
57 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)aalloc( dim, (size_t)sizeof(T) ); // CFA aalloc
58 else return (T *)amemalign( _Alignof(T), dim, sizeof(T) );
59 } // aalloc
60
[74b19fb]61 T * calloc( size_t dim ) {
[d6b03b7]62 if ( _Alignof(T) <= libAlign() )return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc
63 else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) );
[74b19fb]64 } // calloc
65
[856fe3e]66 T * resize( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast
67 return (T *)(void *)resize( (void *)ptr, size ); // C realloc
68 } // resize
69
[d74369b]70 T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast
[cafb687]71 return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
[74b19fb]72 } // realloc
73
74 T * memalign( size_t align ) {
[cafb687]75 return (T *)memalign( align, sizeof(T) ); // C memalign
[74b19fb]76 } // memalign
77
[856fe3e]78 T * amemalign( size_t align, size_t dim ) {
79 return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign
80 } // amemalign
81
[d74369b]82 T * cmemalign( size_t align, size_t dim ) {
83 return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
84 } // cmemalign
85
[74b19fb]86 T * aligned_alloc( size_t align ) {
[cafb687]87 return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc
[74b19fb]88 } // aligned_alloc
89
90 int posix_memalign( T ** ptr, size_t align ) {
91 return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
92 } // posix_memalign
[cfbc703d]93} // distribution
[74b19fb]94
[cfbc703d]95static inline forall( dtype T | sized(T) ) {
[ca7949b]96 // Cforall safe general allocation, fill, resize, array
[74b19fb]97
98 T * alloc( void ) {
[d6b03b7]99 return malloc();
[74b19fb]100 } // alloc
101
[cafb687]102 T * alloc( size_t dim ) {
[856fe3e]103 return aalloc( dim );
[74b19fb]104 } // alloc
105
[cfbc703d]106 forall( dtype S | sized(S) )
107 T * alloc( S ptr[], size_t dim = 1 ) { // singleton/array resize
108 size_t len = malloc_usable_size( ptr ); // current bucket size
109 if ( sizeof(T) * dim > len ) { // not enough space ?
110 T * temp = alloc( dim ); // new storage
111 free( ptr ); // free old storage
112 return temp;
113 } else {
114 return (T *)ptr;
115 } // if
116 } // alloc
117
118 T * alloc( T ptr[], size_t dim, bool copy = true ) {
119 if ( copy ) { // realloc
120 return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
121 } else {
[856fe3e]122 return resize( ptr, dim * sizeof(T) ); // resize
[cfbc703d]123 } // if
[7df201c]124 } // alloc
125
[cafb687]126 T * alloc_set( char fill ) {
127 return (T *)memset( (T *)alloc(), (int)fill, sizeof(T) ); // initialize with fill value
128 } // alloc
129
130 T * alloc_set( T fill ) {
131 return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value
[74b19fb]132 } // alloc
133
[cafb687]134 T * alloc_set( size_t dim, char fill ) {
[d6b03b7]135 return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
[74b19fb]136 } // alloc
137
[cafb687]138 T * alloc_set( size_t dim, T fill ) {
[7df201c]139 T * r = (T *)alloc( dim );
140 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
141 return r;
142 } // alloc
143
[cafb687]144 T * alloc_set( size_t dim, const T fill[] ) {
[7df201c]145 return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value
146 } // alloc
[74b19fb]147} // distribution
[6065b3aa]148
[cafb687]149forall( dtype T | sized(T) ) {
150 T * alloc_set( T ptr[], size_t dim, char fill ); // realloc array with fill
[cfbc703d]151 T * alloc_set( T ptr[], size_t dim, T fill ); // realloc array with fill
[cafb687]152} // distribution
[f3fc631f]153
[3ce0d440]154static inline forall( dtype T | sized(T) ) {
[cafb687]155 T * alloc_align( size_t align ) {
[3ce0d440]156 return (T *)memalign( align, sizeof(T) );
[cafb687]157 } // alloc_align
[3ce0d440]158
[cafb687]159 T * alloc_align( size_t align, size_t dim ) {
[3ce0d440]160 return (T *)memalign( align, dim * sizeof(T) );
[cafb687]161 } // alloc_align
162
[856fe3e]163 T * alloc_align( T * ptr, size_t align ) { // aligned realloc array
[d74369b]164 return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
165 } // alloc_align
166
[cfbc703d]167 forall( dtype S | sized(S) )
168 T * alloc_align( S ptr[], size_t align ) { // aligned reuse array
169 return (T *)(void *)resize( (void *)ptr, align, sizeof(T) ); // CFA realloc
170 } // alloc_align
171
[d74369b]172 T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array
173 return (T *)(void *)realloc( (void *)ptr, align, dim * sizeof(T) ); // CFA realloc
174 } // alloc_align
175
[cafb687]176 T * alloc_align_set( size_t align, char fill ) {
177 return (T *)memset( (T *)alloc_align( align ), (int)fill, sizeof(T) ); // initialize with fill value
178 } // alloc_align
[3ce0d440]179
[cafb687]180 T * alloc_align_set( size_t align, T fill ) {
181 return (T *)memcpy( (T *)alloc_align( align ), &fill, sizeof(T) ); // initialize with fill value
182 } // alloc_align
[d6b03b7]183
[cafb687]184 T * alloc_align_set( size_t align, size_t dim, char fill ) {
185 return (T *)memset( (T *)alloc_align( align, dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
186 } // alloc_align
187
188 T * alloc_align_set( size_t align, size_t dim, T fill ) {
189 T * r = (T *)alloc_align( align, dim );
190 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
191 return r;
192 } // alloc_align
193
194 T * alloc_align_set( size_t align, size_t dim, const T fill[] ) {
195 return (T *)memcpy( (T *)alloc_align( align, dim ), fill, dim * sizeof(T) );
196 } // alloc_align
197} // distribution
198
199forall( dtype T | sized(T) ) {
[cfbc703d]200 T * alloc_align_set( T ptr[], size_t align, char fill ); // aligned realloc with fill
201 T * alloc_align_set( T ptr[], size_t align, T fill ); // aligned realloc with fill
[cafb687]202 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill
[cfbc703d]203 T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ); // aligned realloc array with fill
[cafb687]204} // distribution
[3ce0d440]205
206static inline forall( dtype T | sized(T) ) {
[ca7949b]207 // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
[b9c04946]208 T * memset( T * dest, char fill ) {
209 return (T *)memset( dest, fill, sizeof(T) );
[3ce0d440]210 } // memset
211
212 T * memcpy( T * dest, const T * src ) {
213 return (T *)memcpy( dest, src, sizeof(T) );
214 } // memcpy
215} // distribution
216
217static inline forall( dtype T | sized(T) ) {
[ca7949b]218 // Cforall safe initialization/copy, i.e., implicit size specification, array types
[b9c04946]219 T * amemset( T dest[], char fill, size_t dim ) {
220 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
221 } // amemset
[3ce0d440]222
[b9c04946]223 T * amemcpy( T dest[], const T src[], size_t dim ) {
[3ce0d440]224 return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
[b9c04946]225 } // amemcpy
[3ce0d440]226} // distribution
[f3fc631f]227
[ca7949b]228// Cforall allocation/deallocation and constructor/destructor, non-array types
[aca65621]229forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
[83a071f9]230forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
231forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
[627f585]232
[ca7949b]233// Cforall allocation/deallocation and constructor/destructor, array types
[aca65621]234forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
235forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
236forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
[6065b3aa]237
[bd85400]238//---------------------------------------
239
[57fc7d8]240static inline {
[e3fea42]241 int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
242 unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
243 long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
244 unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
245 long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
246 unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
247
248 float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); }
249 double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); }
250 long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); }
[57fc7d8]251} // distribution
[e672372]252
[e3fea42]253float _Complex strto( const char sptr[], char ** eptr );
254double _Complex strto( const char sptr[], char ** eptr );
255long double _Complex strto( const char sptr[], char ** eptr );
[bd85400]256
[57fc7d8]257static inline {
[e3fea42]258 int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); }
259 unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); }
260 long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); }
261 unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); }
262 long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); }
263 unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); }
264
265 float ato( const char sptr[] ) { return strtof( sptr, 0p ); }
266 double ato( const char sptr[] ) { return strtod( sptr, 0p ); }
267 long double ato( const char sptr[] ) { return strtold( sptr, 0p ); }
268
269 float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
270 double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
271 long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
[57fc7d8]272} // distribution
[e672372]273
[bd85400]274//---------------------------------------
275
[3ce0d440]276forall( otype E | { int ?<?( E, E ); } ) {
277 E * bsearch( E key, const E * vals, size_t dim );
278 size_t bsearch( E key, const E * vals, size_t dim );
279 E * bsearchl( E key, const E * vals, size_t dim );
280 size_t bsearchl( E key, const E * vals, size_t dim );
281 E * bsearchu( E key, const E * vals, size_t dim );
282 size_t bsearchu( E key, const E * vals, size_t dim );
283} // distribution
[9c47a47]284
[3ce0d440]285forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
286 E * bsearch( K key, const E * vals, size_t dim );
287 size_t bsearch( K key, const E * vals, size_t dim );
288 E * bsearchl( K key, const E * vals, size_t dim );
289 size_t bsearchl( K key, const E * vals, size_t dim );
290 E * bsearchu( K key, const E * vals, size_t dim );
291 size_t bsearchu( K key, const E * vals, size_t dim );
292} // distribution
[bd85400]293
[b9c04946]294forall( otype E | { int ?<?( E, E ); } ) {
295 void qsort( E * vals, size_t dim );
296} // distribution
297
[bd85400]298//---------------------------------------
299
[bbe1a87]300extern "C" { // override C version
301 void srandom( unsigned int seed );
302 long int random( void );
303} // extern "C"
304
305static inline {
306 long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
307 long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u)
308 unsigned long int random( void ) { return lrand48(); }
309 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)
310 unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u)
311
312 char random( void ) { return (unsigned long int)random(); }
313 char random( char u ) { return random( (unsigned long int)u ); } // [0,u)
314 char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
315 int random( void ) { return (long int)random(); }
316 int random( int u ) { return random( (long int)u ); } // [0,u]
317 int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u)
318 unsigned int random( void ) { return (unsigned long int)random(); }
319 unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u]
320 unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
321} // distribution
322
323float random( void ); // [0.0, 1.0)
324double random( void ); // [0.0, 1.0)
325float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
326double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
327long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
[bd85400]328
329//---------------------------------------
330
[58b6d1b]331#include "common.hfa"
[bd85400]332
[2026bb6]333//---------------------------------------
334
335extern bool threading_enabled(void) OPTIONAL_THREAD;
336
[bd85400]337// Local Variables: //
338// mode: c //
339// tab-width: 4 //
340// End: //
Note: See TracBrowser for help on using the repository browser.