Changeset 7030dab for libcfa/src/stdlib.cfa
- Timestamp:
- Apr 6, 2020, 4:46:28 PM (6 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum, stuck-waitfor-destruct
- Children:
- e3bc51c
- Parents:
- 71d6bd8 (diff), 057298e (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - File:
-
- 1 edited
-
libcfa/src/stdlib.cfa (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.cfa
r71d6bd8 r7030dab 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Oct 22 08:57:52 201913 // Update Count : 4 7812 // Last Modified On : Tue Mar 31 13:26:46 2020 13 // Update Count : 495 14 14 // 15 15 … … 20 20 #define _XOPEN_SOURCE 600 // posix_memalign, *rand48 21 21 #include <string.h> // memcpy, memset 22 #include <malloc.h> // malloc_usable_size23 22 //#include <math.h> // fabsf, fabs, fabsl 24 23 #include <complex.h> // _Complex_I … … 30 29 T * alloc_set( T ptr[], size_t dim, char fill ) { // realloc array with fill 31 30 size_t olen = malloc_usable_size( ptr ); // current allocation 32 char * nptr = (char*)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc33 size_t nlen = malloc_usable_size( nptr ); // new allocation 34 if ( nlen > olen ) { // larger ? 35 memset( nptr + olen, (int)fill, nlen - olen ); // initialize added storage31 void * nptr = (void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc 32 size_t nlen = malloc_usable_size( nptr ); // new allocation 33 if ( nlen > olen ) { // larger ? 34 memset( (char *)nptr + olen, (int)fill, nlen - olen ); // initialize added storage 36 35 } // if 37 36 return (T *)nptr; 38 37 } // alloc_set 39 38 40 T * alloc_align( T ptr[], size_t align ) { // aligned realloc array 41 char * nptr; 42 size_t alignment = malloc_alignment( ptr ); 43 if ( align != alignment && (uintptr_t)ptr % align != 0 ) { 44 size_t olen = malloc_usable_size( ptr ); // current allocation 45 nptr = (char *)memalign( align, olen ); 46 size_t nlen = malloc_usable_size( nptr ); // new allocation 47 size_t lnth = olen < nlen ? olen : nlen; // min 48 memcpy( nptr, ptr, lnth ); // initialize storage 49 free( ptr ); 50 } else { 51 nptr = (char *)ptr; 52 } // if 53 return (T *)nptr; 54 } // alloc_align 55 56 T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array 57 char * nptr; 58 size_t alignment = malloc_alignment( ptr ); 59 if ( align != alignment ) { 60 size_t olen = malloc_usable_size( ptr ); // current allocation 61 nptr = (char *)memalign( align, dim * sizeof(T) ); 62 size_t nlen = malloc_usable_size( nptr ); // new allocation 63 size_t lnth = olen < nlen ? olen : nlen; // min 64 memcpy( nptr, ptr, lnth ); // initialize storage 65 free( ptr ); 66 } else { 67 nptr = (char *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc 68 } // if 69 return (T *)nptr; 70 } // alloc_align 39 T * alloc_set( T ptr[], size_t dim, T fill ) { // realloc array with fill 40 size_t olen = malloc_usable_size( ptr ); // current allocation 41 void * nptr = (void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc 42 size_t nlen = malloc_usable_size( nptr ); // new allocation 43 if ( nlen > olen ) { // larger ? 44 for ( i; dim ) { memcpy( &ptr[i], &fill, sizeof(T) ); } // initialize with fill value 45 } // if 46 return (T *)nptr; 47 } // alloc_align_set 71 48 72 49 T * alloc_align_set( T ptr[], size_t align, char fill ) { // aligned realloc with fill 73 50 size_t olen = malloc_usable_size( ptr ); // current allocation 74 char * nptr = alloc_align( ptr, align ); 75 size_t nlen = malloc_usable_size( nptr ); // new allocation 76 if ( nlen > olen ) { // larger ? 77 memset( nptr + olen, (int)fill, nlen - olen ); // initialize added storage 51 void * nptr = (void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc 52 // char * nptr = alloc_align( ptr, align ); 53 size_t nlen = malloc_usable_size( nptr ); // new allocation 54 if ( nlen > olen ) { // larger ? 55 memset( (char *)nptr + olen, (int)fill, nlen - olen ); // initialize added storage 56 } // if 57 return (T *)nptr; 58 } // alloc_align_set 59 60 T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ) { // aligned realloc with fill 61 size_t olen = malloc_usable_size( ptr ); // current allocation 62 void * nptr = (void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc 63 // char * nptr = alloc_align( ptr, align ); 64 size_t nlen = malloc_usable_size( nptr ); // new allocation 65 if ( nlen > olen ) { // larger ? 66 for ( i; dim ) { memcpy( &ptr[i], &fill, sizeof(T) ); } // initialize with fill value 78 67 } // if 79 68 return (T *)nptr; … … 138 127 //--------------------------------------- 139 128 140 float _Complex strto( const char * sptr, char ** eptr ) {129 float _Complex strto( const char sptr[], char ** eptr ) { 141 130 float re, im; 142 131 char * eeptr; … … 149 138 } // strto 150 139 151 double _Complex strto( const char * sptr, char ** eptr ) {140 double _Complex strto( const char sptr[], char ** eptr ) { 152 141 double re, im; 153 142 char * eeptr; … … 160 149 } // strto 161 150 162 long double _Complex strto( const char * sptr, char ** eptr ) {151 long double _Complex strto( const char sptr[], char ** eptr ) { 163 152 long double re, im; 164 153 char * eeptr;
Note:
See TracChangeset
for help on using the changeset viewer.