36 #define THREAD_SAFE_GCC_INTRINSICS STXXL_HAVE_SYNC_ADD_AND_FETCH
43 typedef void* (* malloc_type)(size_t);
45 typedef void* (* realloc_type)(
void*, size_t);
55 #define INIT_HEAP_SIZE 1024 * 1024
61 #define PPREFIX "malloc_count ### "
75 #if THREAD_SAFE_GCC_INTRINSICS
76 long long mycurr = __sync_add_and_fetch(&
curr, inc);
90 #if THREAD_SAFE_GCC_INTRINSICS
91 long long mycurr = __sync_sub_and_fetch(&
curr, dec);
120 fprintf(stderr,
PPREFIX "current %'lld, peak %'lld\n",
137 extern void *
malloc(
size_t size)
throw ()
141 if (size == 0)
return NULL;
150 fprintf(stderr,
PPREFIX "malloc(%'lld) = %p (current %'lld)\n",
155 *(
size_t*)ret = size;
163 fprintf(stderr,
PPREFIX "init heap full !!!\n");
171 *(
size_t*)ret = size;
175 fprintf(stderr,
PPREFIX "malloc(%'lld) = %p on init heap\n",
176 (
long long)size, (
char*)ret +
alignment);
184 extern void free(
void* ptr)
throw ()
194 fprintf(stderr,
PPREFIX "free(%p) on init heap\n", ptr);
201 "free(%p) outside init heap and without real_free !!!\n", ptr);
209 "free(%p) has no sentinel !!! memory corruption?\n", ptr);
212 size = *(
size_t*)ptr;
216 fprintf(stderr,
PPREFIX "free(%p) -> %'lld (current %'lld)\n",
217 ptr, (
long long)size,
curr);
225 extern void *
calloc(
size_t nmemb,
size_t size)
throw ()
229 if (!size)
return NULL;
231 memset(ret, 0, size);
236 extern void *
realloc(
void* ptr,
size_t size)
throw ()
245 fprintf(stderr,
PPREFIX "realloc(%p) = on init heap\n", ptr);
252 "realloc(%p) has no sentinel !!! memory corruption?\n",
256 oldsize = *(
size_t*)ptr;
258 if (oldsize >= size) {
260 *(
size_t*)ptr = size;
267 memcpy(newptr, ptr, oldsize);
286 "free(%p) has no sentinel !!! memory corruption?\n", ptr);
289 oldsize = *(
size_t*)ptr;
294 newptr = (*real_realloc)(ptr,
alignment + size);
300 "realloc(%'lld -> %'lld) = %p (current %'lld)\n",
301 (
long long)oldsize, (
long long)size, newptr,
curr);
304 "realloc(%'lld -> %'lld) = %p -> %p (current %'lld)\n",
305 (
long long)oldsize, (
long long)size, ptr, newptr,
curr);
308 *(
size_t*)newptr = size;
313 static __attribute__ ((constructor)) void init(
void)
317 setlocale(LC_NUMERIC,
"");
322 if ((error = dlerror()) != NULL) {
323 fprintf(stderr,
PPREFIX "error %s\n", error);
328 if ((error = dlerror()) != NULL) {
329 fprintf(stderr,
PPREFIX "error %s\n", error);
334 if ((error = dlerror()) != NULL) {
335 fprintf(stderr,
PPREFIX "error %s\n", error);
340 static __attribute__ ((destructor)) void finish(
void)
343 "exiting, total: %'lld, peak: %'lld, current: %'lld\n",
void * malloc(size_t size)
void *(* realloc_type)(void *, size_t)
void malloc_count_reset_peak(void)
void *(* malloc_type)(size_t)
static realloc_type real_realloc
static size_t init_heap_use
void * realloc(void *ptr, size_t size)
static void inc_count(size_t inc)
static malloc_count_callback_type callback
static void * callback_cookie
static free_type real_free
void(* free_type)(void *)
size_t malloc_count_current(void)
void malloc_count_set_callback(malloc_count_callback_type cb, void *cookie)
static const size_t alignment
void malloc_count_print_status(void)
void(* malloc_count_callback_type)(void *cookie, size_t current)
static void dec_count(size_t dec)
void * calloc(size_t nmemb, size_t size)
static const size_t log_operations_threshold
static malloc_type real_malloc
static const size_t sentinel
static const int log_operations_init_heap
size_t malloc_count_peak(void)
static char init_heap[1024 *1024]
static const int log_operations