source: src/libcfa/stdlib @ 759908f

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 759908f was f3fc631f, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

first attempt new storage management routines

  • Property mode set to 100644
File size: 8.4 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 : Tue May 30 09:07:35 2017
13// Update Count     : 164
14//
15
16#ifndef STDLIB_H
17#define STDLIB_H
18
19//---------------------------------------
20
21extern "C" {
22#ifndef EXIT_FAILURE
23#define EXIT_FAILURE    1                                                               // failing exit status
24#define EXIT_SUCCESS    0                                                               // successful exit status
25#endif // ! EXIT_FAILURE
26} // extern "C"
27
28//---------------------------------------
29
30extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void *
31
32// allocation, non-array types
33static inline forall( dtype T | sized(T) ) T * malloc( void ) {
34        //printf( "X1\n" );
35        return (T *)(void *)malloc( (size_t)sizeof(T) );        // C malloc
36} // malloc
37static inline forall( dtype T | sized(T) ) T * malloc( char fill ) {
38        //printf( "X2\n" );
39        T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );     // C malloc
40    return memset( ptr, (int)fill, sizeof(T) );                 // initial with fill value
41} // malloc
42
43// allocation, array types
44extern "C" { void * calloc( size_t dim, size_t size ); } // use default C routine for void *
45static inline forall( dtype T | sized(T) ) T * calloc( size_t dim ) {
46        //printf( "X3\n" );
47        return (T *)(void *)calloc( dim, sizeof(T) );           // C cmalloc
48}
49static inline forall( dtype T | sized(T) ) T * amalloc( size_t dim ) { // alternative name
50        //printf( "X4\n" );
51        return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
52} // amalloc
53static inline forall( dtype T | sized(T) ) T * amalloc( size_t dim, char fill ) { // alternative name
54        //printf( "X5\n" );
55        T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
56    return memset( ptr, (int)fill, dim * sizeof(T) );
57} // amalloc
58
59// resize, non-array types
60extern "C" { void * realloc( void * ptr, size_t size ); } // use default C routine for void *
61static inline forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) {
62        //printf( "X5.5\n" );
63        return (T *)(void *)realloc( (void *)ptr, size );
64}
65forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, char fill );
66static inline forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size ) { // alternative name
67        //printf( "X7\n" );
68        return realloc( ptr, size );
69} // malloc
70static inline forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size, char fill ) { // alternative name
71        //printf( "X8\n" );
72        return realloc( ptr, size, fill );
73} // malloc
74
75// resize, array types
76static inline forall( dtype T | sized(T) ) T * amalloc( T * ptr, size_t dim ) {
77        //printf( "X9\n" );
78        return malloc( ptr, dim * (size_t)sizeof(T) );
79} // amalloc
80static inline forall( dtype T | sized(T) ) T * amalloc( T * ptr, size_t dim, char fill ) {
81        //printf( "X10\n" );
82        return malloc( ptr, dim * (size_t)sizeof(T), fill );
83} // amalloc
84
85// alignment, non-array types
86extern "C" { void * memalign( size_t alignment, size_t size ); } // use default C routine for void *
87static inline forall( dtype T | sized(T) ) T * memalign( size_t alignment ) {
88        //printf( "X11\n" );
89        return (T *)memalign( alignment, sizeof(T) );
90} // memalign
91static inline forall( dtype T | sized(T) ) T * memalign( size_t alignment, char fill ) {
92        //printf( "X12\n" );
93    T * ptr = (T *)memalign( alignment, sizeof(T) );
94    return memset( ptr, (int)fill, sizeof(T) );
95} // memalign
96static inline forall( dtype T | sized(T) ) T * aligned_alloc( size_t alignment ) {
97        //printf( "X13\n" );
98        return (T *)memalign( alignment, sizeof(T) );
99} // aligned_alloc
100extern "C" { int posix_memalign( void ** ptr, size_t alignment, size_t size ); } // use default C routine for void *
101static inline forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t alignment ) {
102        //printf( "X14\n" );
103        return posix_memalign( (void **)ptr, alignment, sizeof(T) );
104} // posix_memalign
105
106// alignment, array types
107static inline forall( dtype T | sized(T) ) T * amemalign( size_t alignment, size_t dim ) {
108        //printf( "X15\n" );
109        return (T *)memalign( alignment, dim * sizeof(T) );
110} // amemalign
111static inline forall( dtype T | sized(T) ) T * amemalign( size_t alignment, size_t dim, char fill ) {
112        //printf( "X16\n" );
113    T * ptr = (T *)memalign( alignment, dim * sizeof(T) );
114    return memset( ptr, (int)fill, dim * sizeof(T) );
115} // amemalign
116
117// data, non-array types
118static inline forall( dtype T | sized(T) ) T * memset( T * dest, char c ) {
119        //printf( "X17\n" );
120        return memset( dest, c, sizeof(T) );
121} // memset
122extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } // use default C routine for void *
123static inline forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src ) {
124        //printf( "X18\n" );
125        return memcpy( dest, src, sizeof(T) );
126} // memcpy
127
128// data, array types
129static inline forall( dtype T | sized(T) ) T * amemset( T * dest, size_t dim, char c ) {
130        //printf( "X19\n" );
131        return memset( dest, c, dim * sizeof(T) );
132} // amemset
133static inline forall( dtype T | sized(T) ) T * amemcpy( T * dest, const T * src, size_t dim ) {
134        //printf( "X20\n" );
135        return memcpy( dest, src, dim * sizeof(T) );
136} // amemcpy
137
138// allocation/deallocation and constructor/destructor
139forall( dtype T, ttype Params | sized(T) | { void ?{}(T *, Params); } ) T * new( Params p );
140forall( dtype T | { void ^?{}( T * ); } ) void delete( T * ptr );
141forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } ) void delete( T * ptr, Params rest );
142
143//---------------------------------------
144
145int ato( const char * ptr );
146unsigned int ato( const char * ptr );
147long int ato( const char * ptr );
148unsigned long int ato( const char * ptr );
149long long int ato( const char * ptr );
150unsigned long long int ato( const char * ptr );
151float ato( const char * ptr );
152double ato( const char * ptr );
153long double ato( const char * ptr );
154float _Complex ato( const char * ptr );
155double _Complex ato( const char * ptr );
156long double _Complex ato( const char * ptr );
157
158int strto( const char * sptr, char ** eptr, int base );
159unsigned int strto( const char * sptr, char ** eptr, int base );
160long int strto( const char * sptr, char ** eptr, int base );
161unsigned long int strto( const char * sptr, char ** eptr, int base );
162long long int strto( const char * sptr, char ** eptr, int base );
163unsigned long long int strto( const char * sptr, char ** eptr, int base );
164float strto( const char * sptr, char ** eptr );
165double strto( const char * sptr, char ** eptr );
166long double strto( const char * sptr, char ** eptr );
167float _Complex strto( const char * sptr, char ** eptr );
168double _Complex strto( const char * sptr, char ** eptr );
169long double _Complex strto( const char * sptr, char ** eptr );
170
171//---------------------------------------
172
173forall( otype T | { int ?<?( T, T ); } )
174T * bsearch( T key, const T * arr, size_t dim );
175
176forall( otype T | { int ?<?( T, T ); } )
177unsigned int bsearch( T key, const T * arr, size_t dim );
178
179
180forall( otype T | { int ?<?( T, T ); } )
181void qsort( const T * arr, size_t dim );
182
183//---------------------------------------
184
185forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
186[ T, T ] div( T t1, T t2 );
187
188//---------------------------------------
189
190unsigned char abs( signed char );
191extern "C" { int abs( int ); }                                                  // use default C routine for int
192unsigned long int abs( long int );
193unsigned long long int abs( long long int );
194float abs( float );
195double abs( double );
196long double abs( long double );
197float abs( float _Complex );
198double abs( double _Complex );
199long double abs( long double _Complex );
200forall( otype T | { void ?{}( T *, zero_t ); int ?<?( T, T ); T -?( T ); } )
201T abs( T );
202
203//---------------------------------------
204
205void rand48seed( long int s );
206char rand48( void );
207int rand48( void );
208unsigned int rand48( void );
209long int rand48( void );
210unsigned long int rand48( void );
211float rand48( void );
212double rand48( void );
213float _Complex rand48( void );
214double _Complex rand48( void );
215long double _Complex rand48( void );
216
217//---------------------------------------
218
219forall( otype T | { int ?<?( T, T ); } )
220T min( T t1, T t2 );
221
222forall( otype T | { int ?>?( T, T ); } )
223T max( T t1, T t2 );
224
225forall( otype T | { T min( T, T ); T max( T, T ); } )
226T clamp( T value, T min_val, T max_val );
227
228forall( otype T )
229void swap( T * t1, T * t2 );
230
231#endif // STDLIB_H
232
233// Local Variables: //
234// mode: c //
235// tab-width: 4 //
236// End: //
Note: See TracBrowser for help on using the repository browser.