Changes in libcfa/src/heap.cfa [032234bd:433905a]
- File:
-
- 1 edited
-
libcfa/src/heap.cfa (modified) (33 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/heap.cfa
r032234bd r433905a 36 36 static bool traceHeap = false; 37 37 38 inline bool traceHeap() libcfa_public{ return traceHeap; }39 40 bool traceHeapOn() libcfa_public{38 inline bool traceHeap() { return traceHeap; } 39 40 bool traceHeapOn() { 41 41 bool temp = traceHeap; 42 42 traceHeap = true; … … 44 44 } // traceHeapOn 45 45 46 bool traceHeapOff() libcfa_public{46 bool traceHeapOff() { 47 47 bool temp = traceHeap; 48 48 traceHeap = false; … … 50 50 } // traceHeapOff 51 51 52 bool traceHeapTerm() libcfa_public{ return false; }52 bool traceHeapTerm() { return false; } 53 53 54 54 55 55 static bool prtFree = false; 56 56 57 staticbool prtFree() {57 bool prtFree() { 58 58 return prtFree; 59 59 } // prtFree 60 60 61 staticbool prtFreeOn() {61 bool prtFreeOn() { 62 62 bool temp = prtFree; 63 63 prtFree = true; … … 65 65 } // prtFreeOn 66 66 67 staticbool prtFreeOff() {67 bool prtFreeOff() { 68 68 bool temp = prtFree; 69 69 prtFree = false; … … 388 388 static unsigned int maxBucketsUsed; // maximum number of buckets in use 389 389 // extern visibility, used by runtime kernel 390 // would be cool to remove libcfa_public but it's needed for libcfathread 391 libcfa_public size_t __page_size; // architecture pagesize 392 libcfa_public int __map_prot; // common mmap/mprotect protection 390 size_t __page_size; // architecture pagesize 391 int __map_prot; // common mmap/mprotect protection 393 392 394 393 … … 728 727 729 728 730 s tatic size_t prtFree( Heap & manager ) with( manager ) {729 size_t prtFree( Heap & manager ) with( manager ) { 731 730 size_t total = 0; 732 731 #ifdef __STATISTICS__ … … 880 879 // Allocates size bytes and returns a pointer to the allocated memory. The contents are undefined. If size is 0, 881 880 // then malloc() returns a unique pointer value that can later be successfully passed to free(). 882 void * malloc( size_t size ) libcfa_public{881 void * malloc( size_t size ) { 883 882 #ifdef __STATISTICS__ 884 883 if ( likely( size > 0 ) ) { … … 895 894 896 895 // Same as malloc() except size bytes is an array of dim elements each of elemSize bytes. 897 void * aalloc( size_t dim, size_t elemSize ) libcfa_public{896 void * aalloc( size_t dim, size_t elemSize ) { 898 897 size_t size = dim * elemSize; 899 898 #ifdef __STATISTICS__ … … 911 910 912 911 // Same as aalloc() with memory set to zero. 913 void * calloc( size_t dim, size_t elemSize ) libcfa_public{912 void * calloc( size_t dim, size_t elemSize ) { 914 913 size_t size = dim * elemSize; 915 914 if ( unlikely( size ) == 0 ) { // 0 BYTE ALLOCATION RETURNS NULL POINTER … … 952 951 // not 0p, then the call is equivalent to free(oaddr). Unless oaddr is 0p, it must have been returned by an earlier 953 952 // call to malloc(), alloc(), calloc() or realloc(). If the area pointed to was moved, a free(oaddr) is done. 954 void * resize( void * oaddr, size_t size ) libcfa_public{953 void * resize( void * oaddr, size_t size ) { 955 954 // If size is equal to 0, either NULL or a pointer suitable to be passed to free() is returned. 956 955 if ( unlikely( size == 0 ) ) { // special cases … … 997 996 // Same as resize() but the contents are unchanged in the range from the start of the region up to the minimum of 998 997 // the old and new sizes. 999 void * realloc( void * oaddr, size_t size ) libcfa_public{998 void * realloc( void * oaddr, size_t size ) { 1000 999 // If size is equal to 0, either NULL or a pointer suitable to be passed to free() is returned. 1001 1000 if ( unlikely( size == 0 ) ) { // special cases … … 1061 1060 1062 1061 // Same as realloc() except the new allocation size is large enough for an array of nelem elements of size elsize. 1063 void * reallocarray( void * oaddr, size_t dim, size_t elemSize ) libcfa_public{1062 void * reallocarray( void * oaddr, size_t dim, size_t elemSize ) { 1064 1063 return realloc( oaddr, dim * elemSize ); 1065 1064 } // reallocarray … … 1067 1066 1068 1067 // Same as malloc() except the memory address is a multiple of alignment, which must be a power of two. (obsolete) 1069 void * memalign( size_t alignment, size_t size ) libcfa_public{1068 void * memalign( size_t alignment, size_t size ) { 1070 1069 #ifdef __STATISTICS__ 1071 1070 if ( likely( size > 0 ) ) { … … 1082 1081 1083 1082 // Same as aalloc() with memory alignment. 1084 void * amemalign( size_t alignment, size_t dim, size_t elemSize ) libcfa_public{1083 void * amemalign( size_t alignment, size_t dim, size_t elemSize ) { 1085 1084 size_t size = dim * elemSize; 1086 1085 #ifdef __STATISTICS__ … … 1098 1097 1099 1098 // Same as calloc() with memory alignment. 1100 void * cmemalign( size_t alignment, size_t dim, size_t elemSize ) libcfa_public{1099 void * cmemalign( size_t alignment, size_t dim, size_t elemSize ) { 1101 1100 size_t size = dim * elemSize; 1102 1101 if ( unlikely( size ) == 0 ) { // 0 BYTE ALLOCATION RETURNS NULL POINTER … … 1137 1136 // Same as memalign(), but ISO/IEC 2011 C11 Section 7.22.2 states: the value of size shall be an integral multiple 1138 1137 // of alignment. This requirement is universally ignored. 1139 void * aligned_alloc( size_t alignment, size_t size ) libcfa_public{1138 void * aligned_alloc( size_t alignment, size_t size ) { 1140 1139 return memalign( alignment, size ); 1141 1140 } // aligned_alloc … … 1146 1145 // is 0, then posix_memalign() returns either 0p, or a unique pointer value that can later be successfully passed to 1147 1146 // free(3). 1148 int posix_memalign( void ** memptr, size_t alignment, size_t size ) libcfa_public{1147 int posix_memalign( void ** memptr, size_t alignment, size_t size ) { 1149 1148 if ( unlikely( alignment < libAlign() || ! is_pow2( alignment ) ) ) return EINVAL; // check alignment 1150 1149 *memptr = memalign( alignment, size ); … … 1155 1154 // Allocates size bytes and returns a pointer to the allocated memory. The memory address shall be a multiple of the 1156 1155 // page size. It is equivalent to memalign(sysconf(_SC_PAGESIZE),size). 1157 void * valloc( size_t size ) libcfa_public{1156 void * valloc( size_t size ) { 1158 1157 return memalign( __page_size, size ); 1159 1158 } // valloc … … 1161 1160 1162 1161 // Same as valloc but rounds size to multiple of page size. 1163 void * pvalloc( size_t size ) libcfa_public{1162 void * pvalloc( size_t size ) { 1164 1163 return memalign( __page_size, ceiling2( size, __page_size ) ); // round size to multiple of page size 1165 1164 } // pvalloc … … 1169 1168 // or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behaviour occurs. If ptr is 1170 1169 // 0p, no operation is performed. 1171 void free( void * addr ) libcfa_public{1170 void free( void * addr ) { 1172 1171 if ( unlikely( addr == 0p ) ) { // special case 1173 1172 #ifdef __STATISTICS__ … … 1190 1189 1191 1190 // Returns the alignment of an allocation. 1192 size_t malloc_alignment( void * addr ) libcfa_public{1191 size_t malloc_alignment( void * addr ) { 1193 1192 if ( unlikely( addr == 0p ) ) return libAlign(); // minimum alignment 1194 1193 Heap.Storage.Header * header = HeaderAddr( addr ); … … 1202 1201 1203 1202 // Returns true if the allocation is zero filled, e.g., allocated by calloc(). 1204 bool malloc_zero_fill( void * addr ) libcfa_public{1203 bool malloc_zero_fill( void * addr ) { 1205 1204 if ( unlikely( addr == 0p ) ) return false; // null allocation is not zero fill 1206 1205 Heap.Storage.Header * header = HeaderAddr( addr ); … … 1213 1212 1214 1213 // Returns original total allocation size (not bucket size) => array size is dimension * sizeof(T). 1215 size_t malloc_size( void * addr ) libcfa_public{1214 size_t malloc_size( void * addr ) { 1216 1215 if ( unlikely( addr == 0p ) ) return 0; // null allocation has zero size 1217 1216 Heap.Storage.Header * header = HeaderAddr( addr ); … … 1225 1224 // Returns the number of usable bytes in the block pointed to by ptr, a pointer to a block of memory allocated by 1226 1225 // malloc or a related function. 1227 size_t malloc_usable_size( void * addr ) libcfa_public{1226 size_t malloc_usable_size( void * addr ) { 1228 1227 if ( unlikely( addr == 0p ) ) return 0; // null allocation has 0 size 1229 1228 Heap.Storage.Header * header; … … 1237 1236 1238 1237 // Prints (on default standard error) statistics about memory allocated by malloc and related functions. 1239 void malloc_stats( void ) libcfa_public{1238 void malloc_stats( void ) { 1240 1239 #ifdef __STATISTICS__ 1241 1240 printStats(); … … 1246 1245 1247 1246 // Changes the file descriptor where malloc_stats() writes statistics. 1248 int malloc_stats_fd( int fd __attribute__(( unused )) ) libcfa_public{1247 int malloc_stats_fd( int fd __attribute__(( unused )) ) { 1249 1248 #ifdef __STATISTICS__ 1250 1249 int temp = stats_fd; … … 1260 1259 // The string is printed on the file stream stream. The exported string includes information about all arenas (see 1261 1260 // malloc). 1262 int malloc_info( int options, FILE * stream __attribute__(( unused )) ) libcfa_public{1261 int malloc_info( int options, FILE * stream __attribute__(( unused )) ) { 1263 1262 if ( options != 0 ) { errno = EINVAL; return -1; } 1264 1263 #ifdef __STATISTICS__ … … 1272 1271 // Adjusts parameters that control the behaviour of the memory-allocation functions (see malloc). The param argument 1273 1272 // specifies the parameter to be modified, and value specifies the new value for that parameter. 1274 int mallopt( int option, int value ) libcfa_public{1273 int mallopt( int option, int value ) { 1275 1274 if ( value < 0 ) return 0; 1276 1275 choose( option ) { … … 1286 1285 1287 1286 // Attempt to release free memory at the top of the heap (by calling sbrk with a suitable argument). 1288 int malloc_trim( size_t ) libcfa_public{1287 int malloc_trim( size_t ) { 1289 1288 return 0; // => impossible to release memory 1290 1289 } // malloc_trim … … 1295 1294 // structure dynamically allocated via malloc, and a pointer to that data structure is returned as the function 1296 1295 // result. (The caller must free this memory.) 1297 void * malloc_get_state( void ) libcfa_public{1296 void * malloc_get_state( void ) { 1298 1297 return 0p; // unsupported 1299 1298 } // malloc_get_state … … 1302 1301 // Restores the state of all malloc internal bookkeeping variables to the values recorded in the opaque data 1303 1302 // structure pointed to by state. 1304 int malloc_set_state( void * ) libcfa_public{1303 int malloc_set_state( void * ) { 1305 1304 return 0; // unsupported 1306 1305 } // malloc_set_state … … 1308 1307 1309 1308 // Sets the amount (bytes) to extend the heap when there is insufficent free storage to service an allocation. 1310 __attribute__((weak)) size_t malloc_expansion() libcfa_public{ return __CFA_DEFAULT_HEAP_EXPANSION__; }1309 __attribute__((weak)) size_t malloc_expansion() { return __CFA_DEFAULT_HEAP_EXPANSION__; } 1311 1310 1312 1311 // Sets the crossover point between allocations occuring in the sbrk area or separately mmapped. 1313 __attribute__((weak)) size_t malloc_mmap_start() libcfa_public{ return __CFA_DEFAULT_MMAP_START__; }1312 __attribute__((weak)) size_t malloc_mmap_start() { return __CFA_DEFAULT_MMAP_START__; } 1314 1313 1315 1314 // Amount subtracted to adjust for unfreed program storage (debug only). 1316 __attribute__((weak)) size_t malloc_unfreed() libcfa_public{ return __CFA_DEFAULT_HEAP_UNFREED__; }1315 __attribute__((weak)) size_t malloc_unfreed() { return __CFA_DEFAULT_HEAP_UNFREED__; } 1317 1316 } // extern "C" 1318 1317 1319 1318 1320 1319 // Must have CFA linkage to overload with C linkage realloc. 1321 void * resize( void * oaddr, size_t nalign, size_t size ) libcfa_public{1320 void * resize( void * oaddr, size_t nalign, size_t size ) { 1322 1321 // If size is equal to 0, either NULL or a pointer suitable to be passed to free() is returned. 1323 1322 if ( unlikely( size == 0 ) ) { // special cases … … 1381 1380 1382 1381 1383 void * realloc( void * oaddr, size_t nalign, size_t size ) libcfa_public{1382 void * realloc( void * oaddr, size_t nalign, size_t size ) { 1384 1383 // If size is equal to 0, either NULL or a pointer suitable to be passed to free() is returned. 1385 1384 if ( unlikely( size == 0 ) ) { // special cases
Note:
See TracChangeset
for help on using the changeset viewer.