Changeset cafb687 for libcfa/src
- Timestamp:
- Oct 21, 2019, 10:18:15 AM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- eb5a115
- Parents:
- 1aa6ecb
- Location:
- libcfa/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.cfa
r1aa6ecb rcafb687 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Jun 24 17:34:44201913 // Update Count : 4 6212 // Last Modified On : Sun Oct 20 18:41:23 2019 13 // Update Count : 473 14 14 // 15 15 … … 27 27 //--------------------------------------- 28 28 29 // resize, non-array types 30 forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ) { 31 size_t olen = malloc_usable_size( ptr ); // current allocation 32 char * nptr = (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc 33 size_t nlen = malloc_usable_size( nptr ); // new allocation 34 if ( nlen > olen ) { // larger ? 35 memset( nptr + olen, (int)fill, nlen - olen ); // initialize added storage 36 } // 37 return (T *)nptr; 38 } // alloc 29 forall( dtype T | sized(T) ) { 30 T * alloc_set( T ptr[], size_t dim, char fill ) { // realloc array with fill 31 size_t olen = malloc_usable_size( ptr ); // current allocation 32 char * nptr = (char *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc 33 size_t nlen = malloc_usable_size( nptr ); // new allocation 34 if ( nlen > olen ) { // larger ? 35 memset( nptr + olen, (int)fill, nlen - olen ); // initialize added storage 36 } // if 37 return (T *)nptr; 38 } // alloc_set 39 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 ) { 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 } else { 50 nptr = (char *)ptr; 51 } // if 52 return (T *)nptr; 53 } // alloc_align 54 55 T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array 56 char * nptr; 57 size_t alignment = malloc_alignment( ptr ); 58 if ( align != alignment ) { 59 size_t olen = malloc_usable_size( ptr ); // current allocation 60 nptr = (char *)memalign( align, dim * sizeof(T) ); 61 size_t nlen = malloc_usable_size( nptr ); // new allocation 62 size_t lnth = olen < nlen ? olen : nlen; // min 63 memcpy( nptr, ptr, lnth ); // initialize storage 64 } else { 65 nptr = (char *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc 66 } // if 67 return (T *)nptr; 68 } // alloc_align 69 70 T * alloc_align_set( T ptr[], size_t align, char fill ) { // aligned realloc with fill 71 size_t olen = malloc_usable_size( ptr ); // current allocation 72 char * nptr = alloc_align( ptr, align ); 73 size_t nlen = malloc_usable_size( nptr ); // new allocation 74 if ( nlen > olen ) { // larger ? 75 memset( nptr + olen, (int)fill, nlen - olen ); // initialize added storage 76 } // if 77 return (T *)nptr; 78 } // alloc_align_set 79 } // distribution 39 80 40 81 // allocation/deallocation and constructor/destructor, non-array types 41 82 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) 42 83 T * new( Params p ) { 43 return &(*malloc()){ p }; 84 return &(*malloc()){ p }; // run constructor 44 85 } // new 45 86 … … 47 88 void delete( T * ptr ) { 48 89 if ( ptr ) { // ignore null 49 ^(*ptr){}; 90 ^(*ptr){}; // run destructor 50 91 free( ptr ); 51 92 } // if … … 55 96 void delete( T * ptr, Params rest ) { 56 97 if ( ptr ) { // ignore null 57 ^(*ptr){}; 98 ^(*ptr){}; // run destructor 58 99 free( ptr ); 59 100 } // if -
libcfa/src/stdlib.hfa
r1aa6ecb rcafb687 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Sep 27 12:09:18201913 // Update Count : 3 8112 // Last Modified On : Sun Oct 20 22:57:33 2019 13 // Update Count : 390 14 14 // 15 15 … … 25 25 void * memset( void * dest, int fill, size_t size ); // string.h 26 26 void * memcpy( void * dest, const void * src, size_t size ); // string.h 27 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA 27 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap 28 28 } // extern "C" 29 29 … … 52 52 T * realloc( T * ptr, size_t size ) { 53 53 if ( unlikely( ptr == 0 ) ) return malloc(); 54 return (T *)(void *)realloc( (void *)ptr, size ); 54 return (T *)(void *)realloc( (void *)ptr, size ); // C realloc 55 55 } // realloc 56 56 57 57 T * memalign( size_t align ) { 58 return (T *)memalign( align, sizeof(T) ); 58 return (T *)memalign( align, sizeof(T) ); // C memalign 59 59 } // memalign 60 60 61 61 T * aligned_alloc( size_t align ) { 62 return (T *)aligned_alloc( align, sizeof(T) ); 62 return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc 63 63 } // aligned_alloc 64 64 … … 67 67 } // posix_memalign 68 68 69 70 69 // Cforall dynamic allocation 71 70 … … 74 73 } // alloc 75 74 76 T * alloc( char fill ) { 77 T * ptr; 78 if ( _Alignof(T) <= libAlign() ) ptr = (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc 79 else ptr = (T *)memalign( _Alignof(T), sizeof(T) ); 80 return (T *)memset( ptr, (int)fill, sizeof(T) ); // initialize with fill value 81 } // alloc 82 83 T * alloc( T & fill ) { 75 T * alloc( size_t dim ) { 76 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); 77 else return (T *)memalign( _Alignof(T), dim * sizeof(T) ); 78 } // alloc 79 80 T * alloc( T ptr[], size_t dim ) { // realloc 81 return realloc( ptr, dim * sizeof(T) ); 82 } // alloc 83 84 T * alloc_set( char fill ) { 85 return (T *)memset( (T *)alloc(), (int)fill, sizeof(T) ); // initialize with fill value 86 } // alloc 87 88 T * alloc_set( T fill ) { 84 89 return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value 85 90 } // alloc 86 91 87 T * alloc( size_t dim ) { 88 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc 89 else return (T *)memalign( _Alignof(T), dim * sizeof(T) ); 90 } // alloc 91 92 T * alloc( size_t dim, char fill ) { 92 T * alloc_set( size_t dim, char fill ) { 93 93 return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value 94 94 } // alloc 95 95 96 T * alloc ( size_t dim, T &fill ) {96 T * alloc_set( size_t dim, T fill ) { 97 97 T * r = (T *)alloc( dim ); 98 98 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value … … 100 100 } // alloc 101 101 102 T * alloc ( size_t dim,T fill[] ) {102 T * alloc_set( size_t dim, const T fill[] ) { 103 103 return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value 104 104 } // alloc 105 106 T * alloc( T ptr[], size_t dim ) { 107 return realloc( ptr, dim * sizeof(T) ); 108 } // alloc 109 } // distribution 110 111 112 static inline forall( dtype T | sized(T) ) { 113 T * align_alloc( size_t align ) { 105 } // distribution 106 107 forall( dtype T | sized(T) ) { 108 T * alloc_set( T ptr[], size_t dim, char fill ); // realloc array with fill 109 } // distribution 110 111 static inline forall( dtype T | sized(T) ) { 112 T * alloc_align( size_t align ) { 114 113 return (T *)memalign( align, sizeof(T) ); 115 } // align_alloc 116 117 T * align_alloc( size_t align, char fill ) { 118 T * ptr = (T *)memalign( align, sizeof(T) ); 119 return (T *)memset( ptr, (int)fill, sizeof(T) ); 120 } // align_alloc 121 122 T * align_alloc( size_t align, size_t dim ) { 114 } // alloc_align 115 116 T * alloc_align( size_t align, size_t dim ) { 123 117 return (T *)memalign( align, dim * sizeof(T) ); 124 } // align_alloc 125 126 T * align_alloc( size_t align, size_t dim, char fill ) { 127 if ( fill == '\0' ) { 128 return (T *)cmemalign( align, dim, sizeof(T) ); 129 } else { 130 return (T *)memset( (T *)memalign( align, dim * sizeof(T) ), (int)fill, dim * sizeof(T) ); 131 } // if 132 } // align_alloc 133 } // distribution 134 135 forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ); 136 118 } // alloc_align 119 120 T * alloc_align_set( size_t align, char fill ) { 121 return (T *)memset( (T *)alloc_align( align ), (int)fill, sizeof(T) ); // initialize with fill value 122 } // alloc_align 123 124 T * alloc_align_set( size_t align, T fill ) { 125 return (T *)memcpy( (T *)alloc_align( align ), &fill, sizeof(T) ); // initialize with fill value 126 } // alloc_align 127 128 T * alloc_align_set( size_t align, size_t dim, char fill ) { 129 return (T *)memset( (T *)alloc_align( align, dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value 130 } // alloc_align 131 132 T * alloc_align_set( size_t align, size_t dim, T fill ) { 133 T * r = (T *)alloc_align( align, dim ); 134 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value 135 return r; 136 } // alloc_align 137 138 T * alloc_align_set( size_t align, size_t dim, const T fill[] ) { 139 return (T *)memcpy( (T *)alloc_align( align, dim ), fill, dim * sizeof(T) ); 140 } // alloc_align 141 } // distribution 142 143 forall( dtype T | sized(T) ) { 144 T * alloc_align( T ptr[], size_t align ); // realign 145 T * alloc_align( T ptr[], size_t align, size_t dim ); // aligned realloc array 146 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill 147 } // distribution 137 148 138 149 static inline forall( dtype T | sized(T) ) { 139 150 // data, non-array types 140 141 151 T * memset( T * dest, char fill ) { 142 152 return (T *)memset( dest, fill, sizeof(T) ); … … 150 160 static inline forall( dtype T | sized(T) ) { 151 161 // data, array types 152 153 162 T * amemset( T dest[], char fill, size_t dim ) { 154 163 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
Note: See TracChangeset
for help on using the changeset viewer.