Changes in / [49c9773:b1d4d60]
- Files:
-
- 6 edited
-
doc/user/user.tex (modified) (2 diffs)
-
src/Parser/lex.ll (modified) (2 diffs)
-
src/libcfa/stdlib (modified) (4 diffs)
-
src/libcfa/stdlib.c (modified) (5 diffs)
-
src/tests/.expect/alloc.txt (modified) (1 diff)
-
src/tests/alloc.c (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
r49c9773 rb1d4d60 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : T hu Jun 1 22:46:09201714 %% Update Count : 2 12613 %% Last Modified On : Tue May 30 11:45:46 2017 14 %% Update Count : 2098 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 5242 5242 \begin{description} 5243 5243 \item[fill] 5244 after allocation the storage is filled with a specified character.5244 after allocation the storage is or is not filled with a specified character. 5245 5245 \item[resize] 5246 5246 an existing allocation is decreased or increased in size. 5247 5247 In either case, new storage may or may not be allocated and, if there is a new allocation, as much data from the existing allocation is copied. 5248 For an increase in storage size, new storage after the copied data may be filled.5248 For an increase in storage size, new storage after the copied data may or may not be filled. 5249 5249 \item[alignment] 5250 5250 an allocation starts on a specified memory boundary, e.g., an address multiple of 64 or 128 for cache-line purposes. 5251 5251 \item[array] 5252 5252 the allocation size is scaled to the specified number of array elements. 5253 An array may be filled, resized, or aligned.5253 An array may or not be filled, resized, or aligned. 5254 5254 \end{description} 5255 The table shows allocation routines supporting different combinations of storage-management capabilities: 5255 5256 The following table show the allocation routines supporting different combinations of storage-management capabilities: 5256 5257 \begin{center} 5257 \begin{tabular}{@{} lr|l|l|l|l@{}}5258 & & \multicolumn{1}{c|}{fill}& resize & alignment & array \\5258 \begin{tabular}{@{}r|l|l|l|l@{}} 5259 & fill & resize & alignment & array \\ 5259 5260 \hline 5260 C & ©malloc© & no & no& no & no \\5261 & ©calloc© & yes (0 only) & no& no & yes \\5262 & ©realloc© & no/copy & yes & no & no\\5263 & ©memalign© & no & no & yes& no \\5264 & ©posix_memalign© & no& no & yes & no \\5265 C11 & ©aligned_alloc© & no & no & yes & no\\5266 \CFA & ©alloc© & no/copy/yes & no/yes & no & yes\\5267 & ©align_alloc© & no/yes & no & yes & yes\\5261 ©malloc© & no/yes & no/yes & no & no \\ 5262 ©amalloc© & no/copy data/yes & no/yes & no & yes \\ 5263 ©calloc© & yes (0 only) & no & no & yes \\ 5264 ©realloc© & no/copy data & yes & no & no \\ 5265 ©memalign© & no/yes & no & yes & no \\ 5266 ©amemalign© & no/yes & no & yes & yes \\ 5267 ©align_alloc© & no & no & yes & no \\ 5268 ©posix_memalign© & no & no & yes & no \\ 5268 5269 \end{tabular} 5269 5270 \end{center} 5271 % When ©amalloc© resizes and fills, the space after the copied data from the source is set to the fill character. 5270 5272 It is impossible to resize with alignment because the underlying ©realloc© allocates storage if more space is needed, and it does not honour alignment from the original allocation. 5271 5273 5272 5274 \leavevmode 5273 5275 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5274 // C unsafe allocation 5275 extern "C" { void * mallac( size_t size ); }§\indexc{memset}§ 5276 extern "C" { void * calloc( size_t dim, size_t size ); }§\indexc{calloc}§ 5277 extern "C" { void * realloc( void * ptr, size_t size ); }§\indexc{realloc}§ 5278 extern "C" { void * memalign( size_t align, size_t size ); }§\indexc{memalign}§ 5279 extern "C" { int posix_memalign( void ** ptr, size_t align, size_t size ); }§\indexc{posix_memalign}§ 5280 5281 // §\CFA§ safe equivalents, i.e., implicit size specification 5282 forall( dtype T | sized(T) ) T * malloc( void ); 5283 forall( dtype T | sized(T) ) T * calloc( size_t dim ); 5284 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ); 5285 forall( dtype T | sized(T) ) T * memalign( size_t align ); 5286 forall( dtype T | sized(T) ) T * aligned_alloc( size_t align ); 5287 forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t align ); 5288 5289 // §\CFA§ safe general allocation, fill, resize, array 5290 forall( dtype T | sized(T) ) T * alloc( void );§\indexc{alloc}§ 5291 forall( dtype T | sized(T) ) T * alloc( char fill ); 5292 forall( dtype T | sized(T) ) T * alloc( size_t dim ); 5293 forall( dtype T | sized(T) ) T * alloc( size_t dim, char fill ); 5294 forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim ); 5295 forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ); 5296 5297 // §\CFA§ safe general allocation, align, fill, array 5298 forall( dtype T | sized(T) ) T * align_alloc( size_t align ); 5299 forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill ); 5300 forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim ); 5301 forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim, char fill ); 5302 5303 // C unsafe initialization/copy 5304 extern "C" { void * memset( void * dest, int c, size_t size ); } 5305 extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } 5306 5307 // §\CFA§ safe initialization/copy 5276 // allocation, non-array types 5277 forall( dtype T | sized(T) ) T * malloc( void );§\indexc{malloc}§ 5278 forall( dtype T | sized(T) ) T * malloc( char fill ); 5279 5280 // allocation, array types 5281 forall( dtype T | sized(T) ) T * calloc( size_t dim );§\indexc{cmalloc}§ 5282 forall( dtype T | sized(T) ) T * amalloc( size_t dim );§\indexc{amalloc}§ // alternate name for calloc 5283 forall( dtype T | sized(T) ) T * amalloc( size_t dim, char fill ); 5284 5285 // resize, non-array types 5286 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size );§\indexc{realloc}§ 5287 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, char fill ); 5288 forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size ); // alternate name for realloc 5289 forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size, char fill ); 5290 5291 // resize, array types 5292 forall( dtype T | sized(T) ) T * amalloc( T * ptr, size_t dim ); 5293 forall( dtype T | sized(T) ) T * amalloc( T * ptr, size_t dim, char fill ); 5294 5295 // alignment, non-array types 5296 forall( dtype T | sized(T) ) T * memalign( size_t alignment );§\indexc{memalign}§ 5297 forall( dtype T | sized(T) ) T * memalign( size_t alignment, char fill ); 5298 forall( dtype T | sized(T) ) T * aligned_alloc( size_t alignment );§\indexc{aligned_alloc}§ 5299 forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t alignment );§\indexc{posix_memalign}§ 5300 5301 // alignment, array types 5302 forall( dtype T | sized(T) ) T * amemalign( size_t alignment, size_t dim );§\indexc{amemalign}§ 5303 forall( dtype T | sized(T) ) T * amemalign( size_t alignment, size_t dim, char fill ); 5304 5305 // data, non-array types 5308 5306 forall( dtype T | sized(T) ) T * memset( T * dest, char c );§\indexc{memset}§ 5309 5307 forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src );§\indexc{memcpy}§ 5310 5308 5311 // §\CFA§ safe initialization/copy array5312 forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c );5313 forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim );5314 5315 // §\CFA§allocation/deallocation and constructor/destructor5316 forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params); } ) T * new( Params p );§\indexc{new}§5309 // data, array types 5310 forall( dtype T | sized(T) ) T * amemset( T * dest, size_t dim, char c );§\indexc{amemset}§ 5311 forall( dtype T | sized(T) ) T * amemcpy( T * dest, const T * src, size_t dim );§\indexc{amemcpy}§ 5312 5313 // allocation/deallocation and constructor/destructor 5314 forall( dtype T, ttype Params | sized(T) | { void ?{}(T *, Params); } ) T * new( Params p );§\indexc{new}§ 5317 5315 forall( dtype T | { void ^?{}( T * ); } ) void delete( T * ptr );§\indexc{delete}§ 5318 forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } ) 5319 void delete( T * ptr, Params rest ); 5320 5321 // §\CFA§ allocation/deallocation and constructor/destructor, array 5322 forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * anew( size_t dim, Params p );§\indexc{anew}§ 5323 forall( dtype T | sized(T) | { void ^?{}( T * ); } ) void adelete( size_t dim, T arr[] );§\indexc{adelete}§ 5324 forall( dtype T | sized(T) | { void ^?{}( T * ); }, ttype Params | { void adelete( Params ); } ) 5325 void adelete( size_t dim, T arr[], Params rest ); 5316 forall( dtype T, ttype Params | { void ^?{}( T * ); void delete(Params); } ) void delete( T * ptr, Params rest ); 5326 5317 \end{cfa} 5327 5318 -
src/Parser/lex.ll
r49c9773 rb1d4d60 5 5 * file "LICENCE" distributed with Cforall. 6 6 * 7 * lex.l l--7 * lex.l -- 8 8 * 9 9 * Author : Peter A. Buhr 10 10 * Created On : Sat Sep 22 08:58:10 2001 11 11 * Last Modified By : Peter A. Buhr 12 * Last Modified On : Tue May 30 22:00:48201713 * Update Count : 52 712 * Last Modified On : Mon May 22 07:46:30 2017 13 * Update Count : 525 14 14 */ 15 15 … … 235 235 long { KEYWORD_RETURN(LONG); } 236 236 lvalue { KEYWORD_RETURN(LVALUE); } // CFA 237 monitor { KEYWORD_RETURN(MONITOR); } // CFA237 monitor { KEYWORD_RETURN(MONITOR); } // CFA 238 238 mutex { KEYWORD_RETURN(MUTEX); } // CFA 239 239 _Noreturn { KEYWORD_RETURN(NORETURN); } // C11 -
src/libcfa/stdlib
r49c9773 rb1d4d60 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T hu Jun 1 22:46:43201713 // Update Count : 21612 // Last Modified On : Tue May 30 09:07:35 2017 13 // Update Count : 164 14 14 // 15 15 … … 28 28 //--------------------------------------- 29 29 30 extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void * 31 30 32 // allocation, non-array types 31 33 static inline forall( dtype T | sized(T) ) T * malloc( void ) { … … 33 35 return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc 34 36 } // malloc 35 36 extern "C" { void * calloc( size_t dim, size_t size ); } // default C routine 37 static 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 44 extern "C" { void * calloc( size_t dim, size_t size ); } // use default C routine for void * 37 45 static inline forall( dtype T | sized(T) ) T * calloc( size_t dim ) { 38 //printf( "X 2\n" );46 //printf( "X3\n" ); 39 47 return (T *)(void *)calloc( dim, sizeof(T) ); // C cmalloc 40 48 } 41 42 extern "C" { void * realloc( void * ptr, size_t size ); } // default C routine for void * 49 static 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 53 static 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 60 extern "C" { void * realloc( void * ptr, size_t size ); } // use default C routine for void * 43 61 static inline forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) { 44 //printf( "X 3\n" );62 //printf( "X5.5\n" ); 45 63 return (T *)(void *)realloc( (void *)ptr, size ); 46 64 } 47 48 extern "C" { void * memalign( size_t align, size_t size ); } // use default C routine for void * 49 static inline forall( dtype T | sized(T) ) T * memalign( size_t align ) { 50 //printf( "X4\n" ); 51 return (T *)memalign( align, sizeof(T) ); 65 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, char fill ); 66 static 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 70 static 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 76 static 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 80 static 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 86 extern "C" { void * memalign( size_t alignment, size_t size ); } // use default C routine for void * 87 static inline forall( dtype T | sized(T) ) T * memalign( size_t alignment ) { 88 //printf( "X11\n" ); 89 return (T *)memalign( alignment, sizeof(T) ); 52 90 } // memalign 53 54 static inline forall( dtype T | sized(T) ) T * aligned_alloc( size_t align ) { 55 //printf( "X5\n" ); 56 return (T *)memalign( align, sizeof(T) ); 91 static 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 96 static inline forall( dtype T | sized(T) ) T * aligned_alloc( size_t alignment ) { 97 //printf( "X13\n" ); 98 return (T *)memalign( alignment, sizeof(T) ); 57 99 } // aligned_alloc 58 59 extern "C" { int posix_memalign( void ** ptr, size_t align, size_t size ); } // use default C routine for void * 60 static 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) ); 100 extern "C" { int posix_memalign( void ** ptr, size_t alignment, size_t size ); } // use default C routine for void * 101 static 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) ); 63 104 } // posix_memalign 64 105 65 66 extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void * 67 68 static 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 72 static 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 78 static 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 82 static 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 106 // alignment, array types 107 static 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 111 static 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) ); 85 114 return memset( ptr, (int)fill, dim * sizeof(T) ); 86 } // alloc 87 88 static 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 92 forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ); 93 94 static 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 98 static 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 104 static 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 108 static 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 115 } // amemalign 114 116 115 117 // data, non-array types … … 125 127 126 128 // data, array types 127 static inline forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ) {129 static inline forall( dtype T | sized(T) ) T * amemset( T * dest, size_t dim, char c ) { 128 130 //printf( "X19\n" ); 129 131 return memset( dest, c, dim * sizeof(T) ); 130 } // memset131 static inline forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ) {132 } // amemset 133 static inline forall( dtype T | sized(T) ) T * amemcpy( T * dest, const T * src, size_t dim ) { 132 134 //printf( "X20\n" ); 133 return (void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy134 } // memcpy135 136 // allocation/deallocation and constructor/destructor , non-array types137 forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params); } ) T * new( Params p );135 return memcpy( dest, src, dim * sizeof(T) ); 136 } // amemcpy 137 138 // allocation/deallocation and constructor/destructor 139 forall( dtype T, ttype Params | sized(T) | { void ?{}(T *, Params); } ) T * new( Params p ); 138 140 forall( dtype T | { void ^?{}( T * ); } ) void delete( T * ptr ); 139 141 forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } ) void delete( T * ptr, Params rest ); 140 141 // allocation/deallocation and constructor/destructor, array types142 forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * anew( size_t dim, Params p );143 forall( dtype T | sized(T) | { void ^?{}( T * ); } ) void adelete( size_t dim, T arr[] );144 forall( dtype T | sized(T) | { void ^?{}( T * ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );145 142 146 143 //--------------------------------------- -
src/libcfa/stdlib.c
r49c9773 rb1d4d60 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T hu Jun 1 21:52:57201713 // Update Count : 2 8012 // Last Modified On : Tue May 30 09:07:56 2017 13 // Update Count : 237 14 14 // 15 15 … … 28 28 29 29 // resize, non-array types 30 forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ) { 30 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, char fill ) { // alternative realloc with fill value 31 //printf( "X6\n" ); 31 32 size_t olen = malloc_usable_size( ptr ); // current allocation 32 char * nptr = (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) );// C realloc33 char * nptr = (void *)realloc( (void *)ptr, size ); // C realloc 33 34 size_t nlen = malloc_usable_size( nptr ); // new allocation 34 35 if ( nlen > olen ) { // larger ? … … 36 37 } // 37 38 return (T *)nptr; 38 } // alloc39 40 // allocation/deallocation and constructor/destructor , non-array types41 forall( dtype T | sized(T), ttype Params| { void ?{}( T *, Params ); } )39 } // realloc 40 41 // allocation/deallocation and constructor/destructor 42 forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } ) 42 43 T * new( Params p ) { 43 return ( malloc()){ p }; // run constructor44 return ((T *)malloc()){ p }; 44 45 } // new 45 46 46 47 forall( dtype T | { void ^?{}( T * ); } ) 47 48 void delete( T * ptr ) { 48 if ( ptr ) { // ignore null49 if ( ptr ) { 49 50 ^ptr{}; // run destructor 50 51 free( ptr ); … … 54 55 forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } ) 55 56 void delete( T * ptr, Params rest ) { 56 if ( ptr ) { // ignore null57 if ( ptr ) { 57 58 ^ptr{}; // run destructor 58 59 free( ptr ); … … 60 61 delete( rest ); 61 62 } // delete 62 63 64 // allocation/deallocation and constructor/destructor, array types65 forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } )66 T * 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 constructor70 } // for71 return arr;72 } // anew73 74 forall( dtype T | sized(T) | { void ^?{}( T * ); } )75 void adelete( size_t dim, T arr[] ) {76 if ( arr ) { // ignore null77 for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned78 ^(&arr[i]){}; // run destructor79 } // for80 free( arr );81 } // if82 } // adelete83 84 forall( dtype T | sized(T) | { void ^?{}( T * ); }, ttype Params | { void adelete( Params ); } )85 void adelete( size_t dim, T arr[], Params rest ) {86 if ( arr ) { // ignore null87 for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned88 ^(&arr[i]){}; // run destructor89 } // for90 free( arr );91 } // if92 adelete( rest );93 } // adelete94 63 95 64 //--------------------------------------- -
src/tests/.expect/alloc.txt
r49c9773 rb1d4d60 1 C malloc 0xdeadbeef 2 CFA malloc 0xdeadbeef 3 CFA alloc 0xdeadbeef 4 CFA alloc, fill 01010101 1 C malloc deadbeef 2 CFA malloc 0 3 CFA malloc, fill 01010101 5 4 6 C array calloc, fill 05 C calloc 7 6 0 0 0 0 0 0 0 0 0 0 8 CFA array calloc, fill 07 CFA calloc 9 8 0 0 0 0 0 0 0 0 0 0 10 CFA array alloc, no fill11 0 xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef12 CFA array alloc, fill 0x113 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x10101019 CFA array malloc 10 0 0 deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef 11 CFA array malloc 12 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 14 13 15 C realloc 40 16 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 17 CFA realloc 88 18 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 14 C realloc 15 deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef 16 CFA realloc 17 deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef 0 0 0 0 0 0 0 0 0 0 18 CFA realloc 19 deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef 20 CFA resize malloc 21 deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef 22 CFA resize malloc, fill 23 deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 24 CFA resize malloc, fill 25 deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef 19 26 20 CFA resize alloc 40 21 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 22 CFA resize array alloc 88 23 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 24 CFA resize array alloc 40 25 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 26 CFA resize array alloc 40, fill 27 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 28 CFA resize array alloc 88, fill 29 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 30 CFA resize array alloc 40, fill 31 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 27 CFA resize array malloc 28 deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef 0 0 0 0 0 0 0 0 0 0 29 CFA resize array malloc 30 deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef 31 CFA resize array malloc, fill 32 deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 1010101 33 CFA resize array malloc, fill 34 deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef deadbeef 32 35 33 C memalign42 42.536 CFA aligned_alloc 42 42.5 34 37 CFA memalign 42 42.5 35 38 CFA posix_memalign 42 42.5 36 CFA posix_memalign 42 42.5 37 CFA aligned_alloc 42 42.5 38 CFA align_alloc 42 42.5 39 CFA align_alloc fill 0x1010101 0x1.1010101010101p-1007 39 CFA memalign fill 16843009 7.7486e-304 40 40 41 CFA array align_alloc42 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5,43 CFA array align_alloc, fill44 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007,41 CFA memalign array 42 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, 0 0, 43 CFA memalign array 44 1010101 7.7486e-304, 1010101 7.7486e-304, 1010101 7.7486e-304, 1010101 7.7486e-304, 1010101 7.7486e-304, 1010101 7.7486e-304, 1010101 7.7486e-304, 1010101 7.7486e-304, 1010101 7.7486e-304, 1010101 7.7486e-304, 45 45 46 CFA memset 0x1010101 0x1.1010101010101p-100747 CFA memcpy 0x1010101 0x1.1010101010101p-100746 CFA memset ffffffff -nan 47 CFA memcpy ffffffff -nan 48 48 49 49 CFA array memset 50 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007,50 ffffffff -nan, ffffffff -nan, ffffffff -nan, ffffffff -nan, ffffffff -nan, ffffffff -nan, ffffffff -nan, ffffffff -nan, ffffffff -nan, ffffffff -nan, 51 51 CFA memcpy 52 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 0x1010101 0x1.1010101010101p-1007, 53 54 CFA new initialize 55 42 42.5 42 42.5 56 CFA array new initialize 57 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 58 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 42 42.5, 59 52 deadbeef -17.2, deadbeef -17.2, deadbeef -17.2, deadbeef -17.2, deadbeef -17.2, deadbeef -17.2, deadbeef -17.2, deadbeef -17.2, deadbeef -17.2, deadbeef -17.2, 60 53 pointer arithmetic 0 61 CFA deep malloc 0xdeadbeef54 CFA deep malloc deadbeef 62 55 63 56 SHOULD FAIL -
src/tests/alloc.c
r49c9773 rb1d4d60 10 10 // Created On : Wed Feb 3 07:56:22 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Jun 1 21:33:49201713 // Update Count : 31512 // Last Modified On : Mon May 29 11:33:15 2017 13 // Update Count : 228 14 14 // 15 15 … … 18 18 #include <malloc.h> // malloc_usable_size 19 19 #include <stdint.h> // uintptr_t 20 #include <stdlib.h> // posix_memalign21 20 } // extern 22 21 #include <fstream> … … 29 28 int main( void ) { 30 29 size_t dim = 10; 30 struct S { int x; double y; } * s; 31 31 int * p; 32 char fill = '\1';33 32 34 33 // allocation, non-array types … … 36 35 p = (void *)malloc( sizeof(*p) ); // C malloc, type unsafe 37 36 *p = 0xdeadbeef; 38 printf( "C malloc % #x\n", *p );37 printf( "C malloc %x\n", *p ); 39 38 free( p ); 40 39 41 40 p = malloc(); // CFA malloc, type safe 42 *p = 0xdeadbeef; 43 printf( "CFA malloc %#x\n", *p ); 44 free( p ); 45 46 p = alloc(); // CFA alloc, type safe 47 *p = 0xdeadbeef; 48 printf( "CFA alloc %#x\n", *p ); 49 free( p ); 50 51 p = alloc( fill ); // CFA alloc, fill 52 printf( "CFA alloc, fill %08x\n", *p ); 41 printf( "CFA malloc %d\n", *p ); 42 free( p ); 43 44 p = malloc( '\1' ); // CFA malloc, fill 45 printf( "CFA malloc, fill %08x\n", *p ); 53 46 54 47 … … 57 50 58 51 p = calloc( dim, sizeof( *p ) ); // C array calloc, type unsafe 59 printf( "C array calloc, fill 0\n" );60 for ( int i = 0; i < dim; i += 1 ) { printf( "% #x ", p[i] ); }52 printf( "C calloc\n" ); 53 for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; } 61 54 printf( "\n" ); 62 55 free( p ); 63 56 64 57 p = calloc( dim ); // CFA array calloc, type safe 65 printf( "CFA array calloc, fill 0\n" ); 66 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } 67 printf( "\n" ); 68 free( p ); 69 70 p = alloc( dim ); // CFA array alloc, type safe 71 for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; } 72 printf( "CFA array alloc, no fill\n" ); 73 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } 74 printf( "\n" ); 75 free( p ); 76 77 p = alloc( 2 * dim, fill ); // CFA array alloc, fill 78 printf( "CFA array alloc, fill %#x\n", fill ); 79 for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); } 58 printf( "CFA calloc\n" ); 59 for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; } 60 printf( "\n" ); 61 free( p ); 62 63 p = amalloc( dim ); // CFA array malloc, type safe 64 printf( "CFA array malloc\n" ); 65 for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; } 66 printf( "\n" ); 67 free( p ); 68 69 p = amalloc( 2 * dim, '\1' ); // CFA array malloc, fill 70 printf( "CFA array malloc\n" ); 71 for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; } 80 72 printf( "\n" ); 81 73 // do not free … … 85 77 printf( "\n" ); 86 78 87 p = (void *)realloc( p, dim * sizeof(*p) ); // C realloc 88 for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; } 89 printf( "C realloc %ld\n", malloc_usable_size( p ) ); 90 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } 79 p = (void *)realloc( p, dim * sizeof(*p) ); // CFA realloc 80 printf( "C realloc\n" ); 81 for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; } 91 82 printf( "\n" ); 92 83 93 84 p = realloc( p, 2 * dim * sizeof(*p) ); // CFA realloc 94 for ( int i = dim; i < 2 * dim; i += 1 ) { p[i] = 0x1010101; } 95 printf( "CFA realloc %ld\n", malloc_usable_size( p ) ); 96 for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); } 85 printf( "CFA realloc\n" ); 86 for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; } 87 printf( "\n" ); 88 89 p = realloc( p, dim * sizeof(*p), '\1' ); // CFA realloc 90 printf( "CFA realloc\n" ); 91 for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; } 92 printf( "\n" ); 93 94 p = malloc( p, dim * sizeof(*p) ); // CFA malloc 95 printf( "CFA resize malloc\n" ); 96 for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; } 97 printf( "\n" ); 98 99 p = malloc( p, 2 * dim * sizeof(*p), '\1' ); // CFA malloc, fill 100 printf( "CFA resize malloc, fill\n" ); 101 for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; } 102 printf( "\n" ); 103 104 p = malloc( p, dim * sizeof(*p), '\1' ); // CFA malloc, fill 105 printf( "CFA resize malloc, fill\n" ); 106 for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; } 97 107 printf( "\n" ); 98 108 // do not free … … 102 112 printf( "\n" ); 103 113 104 p = alloc( p, dim ); // CFA resize array alloc 105 for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; } 106 printf( "CFA resize alloc %ld\n", malloc_usable_size( p ) ); 107 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } 108 printf( "\n" ); 109 110 p = alloc( p, 2 * dim ); // CFA resize array alloc 111 for ( int i = dim; i < 2 * dim; i += 1 ) { p[i] = 0x1010101; } 112 printf( "CFA resize array alloc %ld\n", malloc_usable_size( p ) ); 113 for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); } 114 printf( "\n" ); 115 116 p = alloc( p, dim ); // CFA array alloc 117 printf( "CFA resize array alloc %ld\n", malloc_usable_size( p ) ); 118 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } 119 printf( "\n" ); 120 114 p = amalloc( p, 2 * dim ); // CFA array malloc 115 printf( "CFA resize array malloc\n" ); 116 for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; } 117 printf( "\n" ); 118 119 p = amalloc( p, dim ); // CFA array malloc 120 printf( "CFA resize array malloc\n" ); 121 for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; } 122 printf( "\n" ); 123 124 p = amalloc( p, 2 * dim, '\1' ); // CFA array malloc, fill 125 printf( "CFA resize array malloc, fill\n" ); 126 for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; } 127 printf( "\n" ); 128 129 p = amalloc( p, dim, '\1' ); // CFA array malloc, fill 130 printf( "CFA resize array malloc, fill\n" ); 131 for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; } 121 132 free( p ); 122 p = 0; 123 124 p = alloc( p, dim, fill ); // CFA array alloc, fill 125 printf( "CFA resize array alloc %ld, fill\n", malloc_usable_size( p ) ); 126 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } 127 printf( "\n" ); 128 129 p = alloc( p, 2 * dim, fill ); // CFA array alloc, fill 130 printf( "CFA resize array alloc %ld, fill\n", malloc_usable_size( p ) ); 131 for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); } 132 printf( "\n" ); 133 134 p = alloc( p, dim, fill ); // CFA array alloc, fill 135 printf( "CFA resize array alloc %ld, fill\n", malloc_usable_size( p ) ); 136 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] );; } 137 printf( "\n" ); 138 free( p ); 139 133 printf( "\n" ); 140 134 141 135 struct Struct { int x; double y; }; 142 Struct st, st1, sta[dim], sta1[dim], * stp, * stp1; 136 Struct st, st1, sta[dim], sta1[dim], * stp; 137 143 138 144 139 // alignment, non-array types 145 140 printf( "\n" ); 141 146 142 enum { Alignment = 128 }; 147 148 stp = (memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign 149 assert( (uintptr_t)stp % Alignment == 0 ); 150 printf( "C memalign %d %g\n", stp->x, stp->y ); 151 free( stp ); 152 153 stp = (memalign( Alignment )){ 42, 42.5 }; // CFA memalign 143 stp = aligned_alloc( Alignment ); // CFA aligned_alloc 144 *stp = (Struct){ 42, 42.5 }; 145 assert( (uintptr_t)stp % Alignment == 0 ); 146 printf( "CFA aligned_alloc %d %g\n", stp->x, stp->y ); 147 free( stp ); 148 149 stp = memalign( Alignment ); // CFA memalign 150 *stp = (Struct){ 42, 42.5 }; 154 151 assert( (uintptr_t)stp % Alignment == 0 ); 155 152 printf( "CFA memalign %d %g\n", stp->x, stp->y ); 156 free( stp );157 158 posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign159 *stp = (Struct){ 42, 42.5 };160 assert( (uintptr_t)stp % Alignment == 0 );161 printf( "CFA posix_memalign %d %g\n", stp->x, stp->y );162 153 free( stp ); 163 154 … … 168 159 free( stp ); 169 160 170 stp = (aligned_alloc( Alignment )){ 42, 42.5 }; // CFA aligned_alloc 171 assert( (uintptr_t)stp % Alignment == 0 ); 172 printf( "CFA aligned_alloc %d %g\n", stp->x, stp->y ); 173 free( stp ); 174 175 stp = (align_alloc( Alignment )){ 42, 42.5 }; // CFA align_alloc 176 assert( (uintptr_t)stp % Alignment == 0 ); 177 printf( "CFA align_alloc %d %g\n", stp->x, stp->y ); 178 free( stp ); 179 180 stp = align_alloc( Alignment, fill ); // CFA memalign, fill 181 assert( (uintptr_t)stp % Alignment == 0 ); 182 printf( "CFA align_alloc fill %#x %a\n", stp->x, stp->y ); 161 stp = memalign( Alignment, '\1' ); // CFA memalign, fill 162 assert( (uintptr_t)stp % Alignment == 0 ); 163 printf( "CFA memalign fill %d %g\n", stp->x, stp->y ); 183 164 free( stp ); 184 165 … … 187 168 printf( "\n" ); 188 169 189 stp = align_alloc( Alignment, dim ); // CFA array memalign 190 assert( (uintptr_t)stp % Alignment == 0 ); 191 for ( int i = 0; i < dim; i += 1 ) { stp[i] = (Struct){ 42, 42.5 }; } 192 printf( "CFA array align_alloc\n" ); 193 for ( int i = 0; i < dim; i += 1 ) { printf( "%d %g, ", stp[i].x, stp[i].y ); } 194 printf( "\n" ); 195 free( stp ); 196 197 stp = align_alloc( Alignment, dim, fill ); // CFA array memalign, fill 198 assert( (uintptr_t)stp % Alignment == 0 ); 199 printf( "CFA array align_alloc, fill\n" ); 200 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x %a, ", stp[i].x, stp[i].y ); } 170 stp = amemalign( Alignment, 2 * dim ); // CFA array memalign 171 assert( (uintptr_t)stp % Alignment == 0 ); 172 printf( "CFA memalign array\n" ); 173 for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%x %g, ", stp[i].x, stp[i].y ); stp[i].x = 0Xdeadbeef, stp[i].y = -17.2; } 174 printf( "\n" ); 175 free( stp ); 176 177 stp = amemalign( Alignment, dim, '\1' ); // CFA array memalign, fill 178 assert( (uintptr_t)stp % Alignment == 0 ); 179 printf( "CFA memalign array\n" ); 180 for ( int i = 0; i < dim; i += 1 ) { printf( "%x %g, ", stp[i].x, stp[i].y ); stp[i].x = 0Xdeadbeef, stp[i].y = -17.2; } 201 181 printf( "\n" ); 202 182 free( stp ); … … 206 186 printf( "\n" ); 207 187 208 memset( &st, fill );// CFA memset, type safe209 printf( "CFA memset % #x %a\n", st.x, st.y );210 memcpy( &st1, &st );// CFA memcpy, type safe211 printf( "CFA memcpy % #x %a\n", st1.x, st1.y );188 stp = memset( &st, '\xff' ); // CFA memset, type safe 189 printf( "CFA memset %x %g\n", st.x, st.y ); 190 stp = memcpy( &st1, &st ); // CFA memcpy, type safe 191 printf( "CFA memcpy %x %g\n", st1.x, st1.y ); 212 192 213 193 … … 215 195 printf( "\n" ); 216 196 217 memset( sta, dim, fill );// CFA array memset, type safe197 stp = amemset( sta, dim, '\xff' ); // CFA array memset, type safe 218 198 printf( "CFA array memset\n" ); 219 for ( int i = 0; i < dim; i += 1 ) { printf( "% #x %a, ", sta[i].x, sta[i].y ); }220 printf( "\n" ); 221 222 memcpy( sta1, sta, dim );// CFA array memcpy, type safe199 for ( int i = 0; i < dim; i += 1 ) { printf( "%x %g, ", sta[i].x, sta[i].y ); sta[i].x = 0Xdeadbeef, sta[i].y = -17.2; } 200 printf( "\n" ); 201 202 stp = amemcpy( sta1, sta, dim ); // CFA array memcpy, type safe 223 203 printf( "CFA memcpy\n" ); 224 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x %a, ", sta1[i].x, sta1[i].y ); } 225 printf( "\n" ); 226 227 228 // new, non-array types 229 printf( "\n" ); 230 231 stp = new( 42, 42.5 ); 232 stp1 = new( 42, 42.5 ); 233 printf( "CFA new initialize\n%d %g %d %g\n", stp->x, stp->y, stp1->x, stp1->y ); 234 delete( stp, stp1 ); 235 236 // new, array types 237 stp = anew( dim, 42, 42.5 ); 238 printf( "CFA array new initialize\n" ); 239 for ( int i = 0; i < dim; i += 1 ) { printf( "%d %g, ", stp[i].x, stp[i].y ); } 240 printf( "\n" ); 241 stp1 = anew( dim, 42, 42.5 ); 242 for ( int i = 0; i < dim; i += 1 ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); } 243 printf( "\n" ); 244 adelete( dim, stp, dim, stp1 ); 245 246 // extras 204 for ( int i = 0; i < dim; i += 1 ) { printf( "%x %g, ", sta1[i].x, sta1[i].y ); sta1[i].x = 0Xdeadbeef, sta1[i].y = -17.2; } 247 205 printf( "\n" ); 248 206 … … 253 211 p = foo( bar( baz( malloc(), 0 ), 0 ), 0 ); 254 212 *p = 0xdeadbeef; 255 printf( "CFA deep malloc % #x\n", *p );213 printf( "CFA deep malloc %x\n", *p ); 256 214 free( p ); 257 215 258 216 stp = malloc(); 259 217 printf( "\nSHOULD FAIL\n" ); 260 p = alloc( stp, dim * sizeof(*stp) );218 p = malloc( stp, dim * sizeof(*stp) ); 261 219 p = memset( stp, 10 ); 262 220 p = memcpy( &st1, &st );
Note:
See TracChangeset
for help on using the changeset viewer.