source: src/libcfa/stdlib.c@ b1e63ac5

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since b1e63ac5 was 6065b3aa, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

second attempt at memory-allocation routines

  • Property mode set to 100644
File size: 8.9 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// algorithm.c --
8//
9// Author : Peter A. Buhr
10// Created On : Thu Jan 28 17:10:29 2016
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Jun 1 21:52:57 2017
13// Update Count : 280
14//
15
16#include "stdlib"
17
18//---------------------------------------
19
20extern "C" {
21#define _XOPEN_SOURCE 600 // posix_memalign, *rand48
22#include <stdlib.h> // malloc, free, calloc, realloc, memalign, posix_memalign, bsearch
23#include <string.h> // memcpy, memset
24#include <malloc.h> // malloc_usable_size
25#include <math.h> // fabsf, fabs, fabsl
26#include <complex.h> // _Complex_I
27} // extern "C"
28
29// resize, non-array types
30forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ) {
31 size_t olen = malloc_usable_size( ptr ); // current allocation
32 char * nptr = (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
33 size_t nlen = malloc_usable_size( nptr ); // new allocation
34 if ( nlen > olen ) { // larger ?
35 memset( nptr + olen, (int)fill, nlen - olen ); // initialize added storage
36 } //
37 return (T *)nptr;
38} // alloc
39
40// allocation/deallocation and constructor/destructor, non-array types
41forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } )
42T * new( Params p ) {
43 return (malloc()){ p }; // run constructor
44} // new
45
46forall( dtype T | { void ^?{}( T * ); } )
47void delete( T * ptr ) {
48 if ( ptr ) { // ignore null
49 ^ptr{}; // run destructor
50 free( ptr );
51 } // if
52} // delete
53
54forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } )
55void delete( T * ptr, Params rest ) {
56 if ( ptr ) { // ignore null
57 ^ptr{}; // run destructor
58 free( ptr );
59 } // if
60 delete( rest );
61} // delete
62
63
64// allocation/deallocation and constructor/destructor, array types
65forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } )
66T * anew( size_t dim, Params p ) {
67 T *arr = alloc( dim );
68 for ( unsigned int i = 0; i < dim; i += 1 ) {
69 (&arr[i]){ p }; // run constructor
70 } // for
71 return arr;
72} // anew
73
74forall( dtype T | sized(T) | { void ^?{}( T * ); } )
75void adelete( size_t dim, T arr[] ) {
76 if ( arr ) { // ignore null
77 for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned
78 ^(&arr[i]){}; // run destructor
79 } // for
80 free( arr );
81 } // if
82} // adelete
83
84forall( dtype T | sized(T) | { void ^?{}( T * ); }, ttype Params | { void adelete( Params ); } )
85void adelete( size_t dim, T arr[], Params rest ) {
86 if ( arr ) { // ignore null
87 for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned
88 ^(&arr[i]){}; // run destructor
89 } // for
90 free( arr );
91 } // if
92 adelete( rest );
93} // adelete
94
95//---------------------------------------
96
97int ato( const char * ptr ) {
98 int i;
99 if ( sscanf( ptr, "%d", &i ) == EOF ) {}
100 return i;
101} // ato
102
103unsigned int ato( const char * ptr ) {
104 unsigned int ui;
105 if ( sscanf( ptr, "%u", &ui ) == EOF ) {}
106 return ui;
107} // ato
108
109long int ato( const char * ptr ) {
110 long int li;
111 if ( sscanf( ptr, "%ld", &li ) == EOF ) {}
112 return li;
113} // ato
114
115unsigned long int ato( const char * ptr ) {
116 unsigned long int uli;
117 if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}
118 return uli;
119} // ato
120
121long long int ato( const char * ptr ) {
122 long long int lli;
123 if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}
124 return lli;
125} // ato
126
127unsigned long long int ato( const char * ptr ) {
128 unsigned long long int ulli;
129 if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {}
130 return ulli;
131} // ato
132
133
134float ato( const char * ptr ) {
135 float f;
136 if ( sscanf( ptr, "%f", &f ) == EOF ) {}
137 return f;
138} // ato
139
140double ato( const char * ptr ) {
141 double d;
142 if ( sscanf( ptr, "%lf", &d ) == EOF ) {}
143 return d;
144} // ato
145
146long double ato( const char * ptr ) {
147 long double ld;
148 if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {}
149 return ld;
150} // ato
151
152
153float _Complex ato( const char * ptr ) {
154 float re, im;
155 if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {}
156 return re + im * _Complex_I;
157} // ato
158
159double _Complex ato( const char * ptr ) {
160 double re, im;
161 if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {}
162 return re + im * _Complex_I;
163} // ato
164
165long double _Complex ato( const char * ptr ) {
166 long double re, im;
167 if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {}
168 return re + im * _Complex_I;
169} // ato
170
171
172int strto( const char * sptr, char ** eptr, int base ) {
173 return (int)strtol( sptr, eptr, base );
174} // strto
175
176unsigned int strto( const char * sptr, char ** eptr, int base ) {
177 return (unsigned int)strtoul( sptr, eptr, base );
178} // strto
179
180long int strto( const char * sptr, char ** eptr, int base ) {
181 return strtol( sptr, eptr, base );
182} // strto
183
184unsigned long int strto( const char * sptr, char ** eptr, int base ) {
185 return strtoul( sptr, eptr, base );
186} // strto
187
188long long int strto( const char * sptr, char ** eptr, int base ) {
189 return strtoll( sptr, eptr, base );
190} // strto
191
192unsigned long long int strto( const char * sptr, char ** eptr, int base ) {
193 return strtoull( sptr, eptr, base );
194} // strto
195
196
197float strto( const char * sptr, char ** eptr ) {
198 return strtof( sptr, eptr );
199} // strto
200
201double strto( const char * sptr, char ** eptr ) {
202 return strtod( sptr, eptr );
203} // strto
204
205long double strto( const char * sptr, char ** eptr ) {
206 return strtold( sptr, eptr );
207} // strto
208
209
210float _Complex strto( const char * sptr, char ** eptr ) {
211 float re, im;
212 re = strtof( sptr, eptr );
213 if ( sptr == *eptr ) return 0.0;
214 im = strtof( sptr, eptr );
215 if ( sptr == *eptr ) return 0.0;
216 return re + im * _Complex_I;
217} // strto
218
219double _Complex strto( const char * sptr, char ** eptr ) {
220 double re, im;
221 re = strtod( sptr, eptr );
222 if ( sptr == *eptr ) return 0.0;
223 im = strtod( sptr, eptr );
224 if ( sptr == *eptr ) return 0.0;
225 return re + im * _Complex_I;
226} // strto
227
228long double _Complex strto( const char * sptr, char ** eptr ) {
229 long double re, im;
230 re = strtold( sptr, eptr );
231 if ( sptr == *eptr ) return 0.0;
232 im = strtold( sptr, eptr );
233 if ( sptr == *eptr ) return 0.0;
234 return re + im * _Complex_I;
235} // strto
236
237//---------------------------------------
238
239forall( otype T | { int ?<?( T, T ); } )
240T * bsearch( T key, const T * arr, size_t dim ) {
241 int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
242 return (T *)bsearch( &key, arr, dim, sizeof(T), comp );
243} // bsearch
244
245forall( otype T | { int ?<?( T, T ); } )
246unsigned int bsearch( T key, const T * arr, size_t dim ) {
247 T *result = bsearch( key, arr, dim );
248 return result ? result - arr : dim; // pointer subtraction includes sizeof(T)
249} // bsearch
250
251forall( otype T | { int ?<?( T, T ); } )
252void qsort( const T * arr, size_t dim ) {
253 int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
254 qsort( arr, dim, sizeof(T), comp );
255} // qsort
256
257//---------------------------------------
258
259forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
260[ T, T ] div( T t1, T t2 ) { return [ t1 / t2, t1 % t2 ]; }
261
262//---------------------------------------
263
264unsigned char abs( signed char v ) { return abs( (int)v ); }
265unsigned long int abs( long int v ) { return labs( v ); }
266unsigned long long int abs( long long int v ) { return llabs( v ); }
267float abs( float x ) { return fabsf( x ); }
268double abs( double x ) { return fabs( x ); }
269long double abs( long double x ) { return fabsl( x ); }
270float abs( float _Complex x ) { return cabsf( x ); }
271double abs( double _Complex x ) { return cabs( x ); }
272long double abs( long double _Complex x ) { return cabsl( x ); }
273
274//---------------------------------------
275
276void rand48seed( long int s ) { srand48( s ); }
277char rand48( void ) { return mrand48(); }
278int rand48( void ) { return mrand48(); }
279unsigned int rand48( void ) { return lrand48(); }
280long int rand48( void ) { return mrand48(); }
281unsigned long int rand48( void ) { return lrand48(); }
282float rand48( void ) { return (float)drand48(); } // otherwise float uses lrand48
283double rand48( void ) { return drand48(); }
284float _Complex rand48( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
285double _Complex rand48( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
286long double _Complex rand48( void) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
287
288//---------------------------------------
289
290forall( otype T | { int ?<?( T, T ); } )
291T min( T t1, T t2 ) {
292 return t1 < t2 ? t1 : t2;
293} // min
294
295forall( otype T | { int ?>?( T, T ); } )
296T max( T t1, T t2 ) {
297 return t1 > t2 ? t1 : t2;
298} // max
299
300forall( otype T | { T min( T, T ); T max( T, T ); } )
301T clamp( T value, T min_val, T max_val ) {
302 return max( min_val, min( value, max_val ) );
303} // clamp
304
305forall( otype T )
306void swap( T * t1, T * t2 ) {
307 T temp = *t1;
308 *t1 = *t2;
309 *t2 = temp;
310} // swap
311
312// Local Variables: //
313// tab-width: 4 //
314// End: //
Note: See TracBrowser for help on using the repository browser.