source: src/libcfa/stdlib.c@ ad1a8dd

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 ad1a8dd was 02d241f, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

changes to malloc and bsearch

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