Changes in / [e93cbfa:0d52c6f]
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/heap.cfa
re93cbfa r0d52c6f 10 10 // Created On : Tue Dec 19 21:58:35 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : S un Jul 19 17:37:21202013 // Update Count : 80 612 // Last Modified On : Sat Jun 13 22:42:15 2020 13 // Update Count : 805 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 0; // null allocation has zero size1100 if ( unlikely( addr == 0p ) ) return false; // null allocation is not zero fill 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 0; // null allocation has 0 size1110 if ( unlikely( addr == 0p ) ) return false; // null allocation is not zero fill 1111 1111 HeapManager.Storage.Header * header = headerAddr( addr ); 1112 1112 if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ? -
libcfa/src/stdlib.cfa
re93cbfa r0d52c6f 9 9 // Author : Peter A. Buhr 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Sun Jul 19 15:05:28202013 // Update Count : 50 111 // Last Modified By : Andrew Beach 12 // Last Modified On : Tue Jun 2 16:46:00 2020 13 // Update Count : 500 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 fill 30 size_t olen = malloc_usable_size( ptr ); // current allocation 31 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 35 } // if 36 return (T *)nptr; 37 } // alloc_set 38 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; malloc_size( ptr ) / sizeof(T) ~ dim ) { 45 memcpy( &ptr[i], &fill, sizeof(T) ); // initialize with fill value 46 } // for 47 } // if 48 return (T *)nptr; 49 } // alloc_align_set 50 51 T * alloc_align_set( T ptr[], size_t align, char fill ) { // aligned realloc with fill 52 size_t olen = malloc_usable_size( ptr ); // current allocation 53 void * nptr = (void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc 54 // char * nptr = alloc_align( ptr, align ); 55 size_t nlen = malloc_usable_size( nptr ); // new allocation 56 if ( nlen > olen ) { // larger ? 57 memset( (char *)nptr + olen, (int)fill, nlen - olen ); // initialize added storage 58 } // if 59 return (T *)nptr; 60 } // alloc_align_set 61 62 T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ) { // aligned realloc with fill 63 size_t olen = malloc_usable_size( ptr ); // current allocation 64 void * nptr = (void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc 65 // char * nptr = alloc_align( ptr, align ); 66 size_t nlen = malloc_usable_size( nptr ); // new allocation 67 if ( nlen > olen ) { // larger ? 68 for ( i; dim ) { memcpy( &ptr[i], &fill, sizeof(T) ); } // initialize with fill value 69 } // if 70 return (T *)nptr; 71 } // alloc_align_set 72 } // distribution 27 73 28 74 // allocation/deallocation and constructor/destructor, non-array types -
libcfa/src/stdlib.hfa
re93cbfa r0d52c6f 9 9 // Author : Peter A. Buhr 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Sun Jul 19 18:29:34202013 // Update Count : 4 6311 // Last Modified By : Andrew Beach 12 // Last Modified On : Tue Jun 2 16:47:00 2020 13 // Update Count : 451 14 14 // 15 15 … … 57 57 } // calloc 58 58 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 59 T * resize( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast 60 return (T *)(void *)resize( (void *)ptr, size ); // C realloc 66 61 } // resize 67 62 68 63 T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast 69 if ( unlikely( size == 0 ) || unlikely( ptr == 0p ) ) { // special cases70 if ( unlikely( size == 0 ) ) free( ptr );71 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc72 else return (T *)memalign( _Alignof(T), sizeof(T) );73 } // if74 64 return (T *)(void *)realloc( (void *)ptr, size ); // C realloc 75 65 } // realloc … … 128 118 129 119 T * alloc( T ptr[], size_t dim, bool copy = true ) { 130 if ( copy ) { 131 return realloc( ptr, dim * sizeof(T) ); // CFArealloc120 if ( copy ) { // realloc 121 return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc 132 122 } else { 133 return resize( ptr, dim * sizeof(T) ); // CFAresize123 return resize( ptr, dim * sizeof(T) ); // resize 134 124 } // if 135 125 } // alloc … … 156 146 return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value 157 147 } // alloc 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 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 180 153 } // distribution 181 154 … … 223 196 return (T *)memcpy( (T *)alloc_align( align, dim ), fill, dim * sizeof(T) ); 224 197 } // alloc_align 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 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 247 205 } // distribution 248 206 -
tests/.expect/alloc.txt
re93cbfa r0d52c6f 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 dededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede 0xdededede36 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0xdeadbeef 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 0x1010101 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.