2018-02-27 16:14:14 +03:00
|
|
|
#ifdef LOG_MEMORY
|
|
|
|
|
2019-09-28 02:05:30 +03:00
|
|
|
Long malloc_balance_counter = 0;
|
2018-02-27 16:14:14 +03:00
|
|
|
bool log_memory_balance = false;
|
|
|
|
|
|
|
|
void *logged_malloc(size_t size) {
|
|
|
|
void *ptr = malloc(size);
|
2019-10-30 13:07:32 +03:00
|
|
|
if (log_memory_balance) {
|
2020-04-23 11:42:35 +03:00
|
|
|
printf("MALLOC: %p (%zu bytes)\n", ptr, size);
|
2019-10-30 13:07:32 +03:00
|
|
|
}
|
2018-02-27 16:14:14 +03:00
|
|
|
malloc_balance_counter++;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void logged_free(void *ptr) {
|
2019-10-30 13:07:32 +03:00
|
|
|
if (log_memory_balance) {
|
|
|
|
printf("FREE: %p\n", ptr);
|
|
|
|
}
|
2018-02-27 16:14:14 +03:00
|
|
|
free(ptr);
|
|
|
|
malloc_balance_counter--;
|
|
|
|
/* if(malloc_balance_counter == 0) { */
|
2019-10-30 10:47:36 +03:00
|
|
|
/* printf("malloc is balanced! (this should be the last thing you
|
|
|
|
* see)\n"); */
|
2018-02-27 16:14:14 +03:00
|
|
|
/* } */
|
|
|
|
/* else if(malloc_balance_counter < 0) { */
|
|
|
|
/* printf("malloc is %ld, that is bad!\n", malloc_balance_counter); */
|
|
|
|
/* } */
|
|
|
|
}
|
|
|
|
|
|
|
|
void Debug_log_MINUS_memory_MINUS_balance_BANG_(bool value) {
|
|
|
|
log_memory_balance = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CARP_MALLOC(size) logged_malloc(size)
|
|
|
|
#define CARP_FREE(ptr) logged_free(ptr)
|
2019-10-13 11:40:54 +03:00
|
|
|
#define CARP_REALLOC(ptr, size) realloc(ptr, size)
|
2018-02-27 16:14:14 +03:00
|
|
|
|
2019-09-28 02:05:30 +03:00
|
|
|
Long Debug_memory_MINUS_balance() {
|
2019-10-30 13:07:32 +03:00
|
|
|
return malloc_balance_counter;
|
|
|
|
}
|
2018-02-27 16:14:14 +03:00
|
|
|
|
|
|
|
void Debug_reset_MINUS_memory_MINUS_balance_BANG_() {
|
|
|
|
malloc_balance_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2019-06-17 10:17:20 +03:00
|
|
|
#ifdef CHECK_ALLOCATIONS
|
|
|
|
void* CARP_MALLOC(size_t size) {
|
2019-10-30 10:47:36 +03:00
|
|
|
void* res = malloc(size);
|
|
|
|
if (!res) abort();
|
|
|
|
return res;
|
2019-06-17 10:17:20 +03:00
|
|
|
}
|
2019-10-30 10:47:36 +03:00
|
|
|
void* CARP_REALLOC(void* ptr, size_t size) {
|
|
|
|
void* res = realloc(ptr, size);
|
|
|
|
if (!res) abort();
|
|
|
|
return res;
|
2019-10-13 11:40:54 +03:00
|
|
|
}
|
2019-06-17 10:17:20 +03:00
|
|
|
#else
|
2018-02-27 16:14:14 +03:00
|
|
|
#define CARP_MALLOC(size) malloc(size)
|
2019-10-13 11:40:54 +03:00
|
|
|
#define CARP_REALLOC(ptr, size) realloc(ptr, size)
|
2019-06-17 10:17:20 +03:00
|
|
|
#endif
|
|
|
|
|
2018-02-27 16:14:14 +03:00
|
|
|
#define CARP_FREE(ptr) free(ptr)
|
|
|
|
|
2019-09-28 02:05:30 +03:00
|
|
|
Long Debug_memory_MINUS_balance() {
|
2019-10-30 10:47:36 +03:00
|
|
|
printf(
|
|
|
|
"Error - calling 'memory-balance' without compiling with LOG_MEMORY "
|
|
|
|
"enabled (--log-memory).\n");
|
2018-02-27 16:14:14 +03:00
|
|
|
exit(1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Debug_reset_MINUS_memory_MINUS_balance_BANG_() {
|
2019-10-30 10:47:36 +03:00
|
|
|
printf(
|
|
|
|
"Error - calling 'reset-memory-balance!' without compiling with "
|
|
|
|
"LOG_MEMORY enabled (--log-memory).\n");
|
2018-02-27 16:14:14 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Debug_log_MINUS_memory_MINUS_balance_BANG_(bool value) {
|
2019-10-30 10:47:36 +03:00
|
|
|
printf(
|
|
|
|
"Error - calling 'log-memory-balance!' without compiling with "
|
|
|
|
"LOG_MEMORY enabled (--log-memory).\n");
|
2018-02-27 16:14:14 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|