source: src/libcfa/stdlib @ 49c9773

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 49c9773 was 6065b3aa, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

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