Changes in / [e95a117:289a21c]
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/heap.cfa
re95a117 r289a21c 10 10 // Created On : Tue Dec 19 21:58:35 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : S at Jun 13 22:42:15202013 // Update Count : 80 512 // Last Modified On : Sun Jul 19 17:37:21 2020 13 // Update Count : 806 14 14 // 15 15 … … 1098 1098 // Returns original total allocation size (not bucket size) => array size is dimension * sizeif(T). 1099 1099 size_t malloc_size( void * addr ) { 1100 if ( unlikely( addr == 0p ) ) return false; // null allocation is not zero fill1100 if ( unlikely( addr == 0p ) ) return 0; // null allocation has zero size 1101 1101 HeapManager.Storage.Header * header = headerAddr( addr ); 1102 1102 if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ? … … 1108 1108 // Set allocation size and return previous size. 1109 1109 size_t $malloc_size_set( void * addr, size_t size ) { 1110 if ( unlikely( addr == 0p ) ) return false; // null allocation is not zero fill1110 if ( unlikely( addr == 0p ) ) return 0; // null allocation has 0 size 1111 1111 HeapManager.Storage.Header * header = headerAddr( addr ); 1112 1112 if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ? -
libcfa/src/stdlib.cfa
re95a117 r289a21c 9 9 // Author : Peter A. Buhr 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 // Last Modified By : Andrew Beach12 // Last Modified On : Tue Jun 2 16:46:00202013 // Update Count : 50 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Jul 19 15:05:28 2020 13 // Update Count : 501 14 14 // 15 15 … … 25 25 26 26 //--------------------------------------- 27 28 forall( dtype T | sized(T) ) {29 T * alloc_set( T ptr[], size_t dim, char fill ) { // realloc array with fill30 size_t olen = malloc_usable_size( ptr ); // current allocation31 void * nptr = (void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc32 size_t nlen = malloc_usable_size( nptr ); // new allocation33 if ( nlen > olen ) { // larger ?34 memset( (char *)nptr + olen, (int)fill, nlen - olen ); // initialize added storage35 } // if36 return (T *)nptr;37 } // alloc_set38 39 T * alloc_set( T ptr[], size_t dim, T fill ) { // realloc array with fill40 size_t olen = malloc_usable_size( ptr ); // current allocation41 void * nptr = (void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc42 size_t nlen = malloc_usable_size( nptr ); // new allocation43 if ( nlen > olen ) { // larger ?44 for ( i; malloc_size( ptr ) / sizeof(T) ~ dim ) {45 memcpy( &ptr[i], &fill, sizeof(T) ); // initialize with fill value46 } // for47 } // if48 return (T *)nptr;49 } // alloc_align_set50 51 T * alloc_align_set( T ptr[], size_t align, char fill ) { // aligned realloc with fill52 size_t olen = malloc_usable_size( ptr ); // current allocation53 void * nptr = (void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc54 // char * nptr = alloc_align( ptr, align );55 size_t nlen = malloc_usable_size( nptr ); // new allocation56 if ( nlen > olen ) { // larger ?57 memset( (char *)nptr + olen, (int)fill, nlen - olen ); // initialize added storage58 } // if59 return (T *)nptr;60 } // alloc_align_set61 62 T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ) { // aligned realloc with fill63 size_t olen = malloc_usable_size( ptr ); // current allocation64 void * nptr = (void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc65 // char * nptr = alloc_align( ptr, align );66 size_t nlen = malloc_usable_size( nptr ); // new allocation67 if ( nlen > olen ) { // larger ?68 for ( i; dim ) { memcpy( &ptr[i], &fill, sizeof(T) ); } // initialize with fill value69 } // if70 return (T *)nptr;71 } // alloc_align_set72 } // distribution73 27 74 28 // allocation/deallocation and constructor/destructor, non-array types -
libcfa/src/stdlib.hfa
re95a117 r289a21c 9 9 // Author : Peter A. Buhr 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 // Last Modified By : Andrew Beach12 // Last Modified On : Tue Jun 2 16:47:00202013 // Update Count : 4 5111 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Jul 19 18:29:34 2020 13 // Update Count : 463 14 14 // 15 15 … … 57 57 } // calloc 58 58 59 T * resize( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast 60 return (T *)(void *)resize( (void *)ptr, size ); // C realloc 59 T * resize( T * ptr, size_t size ) { // CFA resize, eliminate return-type cast 60 if ( unlikely( size == 0 ) || unlikely( ptr == 0p ) ) { // special cases 61 if ( unlikely( size == 0 ) ) free( ptr ); 62 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc 63 else return (T *)memalign( _Alignof(T), sizeof(T) ); 64 } // if 65 return (T *)(void *)resize( (void *)ptr, size ); // CFA resize 61 66 } // resize 62 67 63 68 T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast 69 if ( unlikely( size == 0 ) || unlikely( ptr == 0p ) ) { // special cases 70 if ( unlikely( size == 0 ) ) free( ptr ); 71 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc 72 else return (T *)memalign( _Alignof(T), sizeof(T) ); 73 } // if 64 74 return (T *)(void *)realloc( (void *)ptr, size ); // C realloc 65 75 } // realloc … … 118 128 119 129 T * alloc( T ptr[], size_t dim, bool copy = true ) { 120 if ( copy ) { // realloc121 return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // Crealloc130 if ( copy ) { 131 return realloc( ptr, dim * sizeof(T) ); // CFA realloc 122 132 } else { 123 return resize( ptr, dim * sizeof(T) ); // resize133 return resize( ptr, dim * sizeof(T) ); // CFA resize 124 134 } // if 125 135 } // alloc … … 146 156 return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value 147 157 } // alloc 148 } // distribution 149 150 forall( dtype T | sized(T) ) { 151 T * alloc_set( T ptr[], size_t dim, char fill ); // realloc array with fill 152 T * alloc_set( T ptr[], size_t dim, T fill ); // realloc array with fill 158 159 T * alloc_set( T ptr[], size_t dim, char fill ) { // realloc array with fill 160 size_t osize = malloc_size( ptr ); // current allocation 161 T * nptr = realloc( ptr, dim * sizeof(T) ); // CFA realloc 162 size_t nsize = malloc_size( nptr ); // new allocation 163 if ( nsize > osize ) { // larger ? 164 memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage 165 } // if 166 return (T *)nptr; 167 } // alloc_set 168 169 T * alloc_set( T ptr[], size_t dim, T & fill ) { // realloc array with fill 170 size_t odim = malloc_size( ptr ) / sizeof(T); // current allocation 171 T * nptr = realloc( ptr, dim * sizeof(T) ); // CFA realloc 172 size_t ndim = malloc_size( nptr ) / sizeof(T); // new allocation 173 if ( ndim > odim ) { // larger ? 174 for ( i; odim ~ ndim ) { 175 memcpy( &nptr[i], &fill, sizeof(T) ); // initialize with fill value 176 } // for 177 } // if 178 return (T *)nptr; 179 } // alloc_align_set 153 180 } // distribution 154 181 … … 196 223 return (T *)memcpy( (T *)alloc_align( align, dim ), fill, dim * sizeof(T) ); 197 224 } // alloc_align 198 } // distribution 199 200 forall( dtype T | sized(T) ) { 201 T * alloc_align_set( T ptr[], size_t align, char fill ); // aligned realloc with fill 202 T * alloc_align_set( T ptr[], size_t align, T fill ); // aligned realloc with fill 203 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill 204 T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ); // aligned realloc array with fill 225 226 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ) { 227 size_t osize = malloc_size( ptr ); // current allocation 228 T * nptr = realloc( ptr, align, dim * sizeof(T) ); // CFA realloc 229 size_t nsize = malloc_size( nptr ); // new allocation 230 if ( nsize > osize ) { // larger ? 231 memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage 232 } // if 233 return (T *)nptr; 234 } // alloc_align_set 235 236 T * alloc_align_set( T ptr[], size_t align, size_t dim, T & fill ) { 237 size_t odim = malloc_size( ptr ) / sizeof(T); // current allocation 238 T * nptr = realloc( ptr, align, dim * sizeof(T) ); // CFA realloc 239 size_t ndim = malloc_size( nptr ); // new allocation 240 if ( ndim > odim ) { // larger ? 241 for ( i; odim ~ ndim ) { 242 memcpy( &nptr[i], &fill, sizeof(T) ); // initialize with fill value 243 } // for 244 } // if 245 return (T *)nptr; 246 } // alloc_align_set 205 247 } // distribution 206 248 -
tests/.expect/alloc.txt
re95a117 r289a21c 34 34 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 35 35 CFA realloc array alloc, fill 36 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0x 1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x10101010xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede36 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 37 37 38 38 C memalign 42 42.5
Note: See TracChangeset
for help on using the changeset viewer.