Changeset cfbc703d for libcfa/src/stdlib.hfa
- Timestamp:
- Apr 1, 2020, 9:32:21 PM (5 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
- Children:
- 6d43cdde
- Parents:
- 5137f9f
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.hfa
r5137f9f rcfbc703d 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Mar 5 11:29:06202013 // Update Count : 4 0712 // Last Modified On : Wed Apr 1 18:38:41 2020 13 // Update Count : 429 14 14 // 15 15 … … 24 24 extern "C" { 25 25 void * memalign( size_t align, size_t size ); // malloc.h 26 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap 26 size_t malloc_usable_size( void * ptr ); // malloc.h 27 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap 27 28 void * memset( void * dest, int fill, size_t size ); // string.h 28 29 void * memcpy( void * dest, const void * src, size_t size ); // string.h 30 void * resize( void * oaddr, size_t size ); // CFA heap 29 31 } // extern "C" 30 32 33 void * resize( void * oaddr, size_t nalign, size_t size ); // CFA heap 31 34 void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap 32 35 … … 72 75 return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign 73 76 } // posix_memalign 74 77 } // distribution 78 79 static inline forall( dtype T | sized(T) ) { 75 80 // Cforall safe general allocation, fill, resize, array 76 81 … … 84 89 } // alloc 85 90 86 T * alloc( T ptr[], size_t dim ) { // realloc 87 return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc 91 forall( dtype S | sized(S) ) 92 T * alloc( S ptr[], size_t dim = 1 ) { // singleton/array resize 93 size_t len = malloc_usable_size( ptr ); // current bucket size 94 if ( sizeof(T) * dim > len ) { // not enough space ? 95 T * temp = alloc( dim ); // new storage 96 free( ptr ); // free old storage 97 return temp; 98 } else { 99 return (T *)ptr; 100 } // if 101 } // alloc 102 103 T * alloc( T ptr[], size_t dim, bool copy = true ) { 104 if ( copy ) { // realloc 105 return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc 106 } else { 107 struct __Unknown {}; 108 return alloc( (__Unknown *)ptr, dim ); // reuse, cheat making T/S different types 109 } // if 88 110 } // alloc 89 111 … … 113 135 forall( dtype T | sized(T) ) { 114 136 T * alloc_set( T ptr[], size_t dim, char fill ); // realloc array with fill 137 T * alloc_set( T ptr[], size_t dim, T fill ); // realloc array with fill 115 138 } // distribution 116 139 … … 126 149 T * alloc_align( T ptr[], size_t align ) { // aligned realloc array 127 150 return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc 151 } // alloc_align 152 153 forall( dtype S | sized(S) ) 154 T * alloc_align( S ptr[], size_t align ) { // aligned reuse array 155 return (T *)(void *)resize( (void *)ptr, align, sizeof(T) ); // CFA realloc 128 156 } // alloc_align 129 157 … … 156 184 157 185 forall( dtype T | sized(T) ) { 186 T * alloc_align_set( T ptr[], size_t align, char fill ); // aligned realloc with fill 187 T * alloc_align_set( T ptr[], size_t align, T fill ); // aligned realloc with fill 158 188 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill 189 T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ); // aligned realloc array with fill 159 190 } // distribution 160 191
Note: See TracChangeset
for help on using the changeset viewer.