167 lines
4.6 KiB
C
167 lines
4.6 KiB
C
#include "debug.h"
|
|
#include <inttypes.h>
|
|
#include <stdlib.h>
|
|
#include "common.h"
|
|
#include "log.h"
|
|
|
|
/**
|
|
* allocate memory using malloc() and print a fitting debug message
|
|
*
|
|
* @param size: the number of bytes to allocate
|
|
* @param file: the file in which malloc() was called
|
|
* @param line: the source code line where malloc() was called
|
|
*
|
|
* @return the pointer to the allocated memory, or nullptr in case of error
|
|
*
|
|
* @errno see malloc(3)
|
|
*
|
|
* @usage the header `debug.h` defines a macro `malloc(size)` that
|
|
* automatically inserts filename and line.
|
|
*/
|
|
void *
|
|
debug_malloc (usize size, const char *file, usize line)
|
|
{
|
|
void *ptr;
|
|
|
|
ptr = malloc (size);
|
|
if (ptr == nullptr) {
|
|
log_print_fl (
|
|
LOG_LEVEL_DEBUG, file, line,
|
|
"malloc of size %zu \x1b[31mreturned null\x1b[0m\n", size
|
|
);
|
|
} else {
|
|
log_print_fl (
|
|
LOG_LEVEL_DEBUG, file, line,
|
|
"malloc of size %zu to ptr %p\n", size, ptr
|
|
);
|
|
}
|
|
|
|
return ptr;
|
|
}
|
|
|
|
/**
|
|
* reallocate memory using realloc() and print a fitting debug message
|
|
*
|
|
* @param ptr: the pointer to reallocate
|
|
* @param size: the new number of bytes
|
|
* @param file: the file in which realloc() was called
|
|
* @param line: the source code line where realloc() was called
|
|
*
|
|
* @return the pointer to the reallocated memory, or nullptr in case of error
|
|
*
|
|
* @errno see realloc(3)
|
|
*
|
|
* @usage the header `debug.h` defines a macro `realloc(ptr, size)` that
|
|
* automatically inserts filename and line.
|
|
*/
|
|
void *
|
|
debug_realloc (void *ptr, usize size, const char *file, usize line)
|
|
{
|
|
void *new_ptr;
|
|
uintptr_t old_ptr;
|
|
|
|
old_ptr = (uintptr_t) ptr;
|
|
new_ptr = realloc (ptr, size);
|
|
if (new_ptr == nullptr && size != 0) {
|
|
log_print_fl (
|
|
LOG_LEVEL_DEBUG, file, line,
|
|
"realloc of ptr %#" PRIxPTR " to size %zu \x1b[31mreturned null\x1b[0m\n",
|
|
old_ptr, size
|
|
);
|
|
} else if (new_ptr == ptr) {
|
|
log_print_fl (
|
|
LOG_LEVEL_DEBUG, file, line,
|
|
"realloc of ptr %p to size %zu\n", new_ptr, size
|
|
);
|
|
} else {
|
|
log_print_fl (
|
|
LOG_LEVEL_DEBUG, file, line,
|
|
"realloc of ptr %#" PRIxPTR " to new ptr %p with size %zu\n",
|
|
old_ptr, new_ptr, size
|
|
);
|
|
}
|
|
|
|
return new_ptr;
|
|
}
|
|
|
|
/**
|
|
* free memory using free() and print a fitting debug message
|
|
*
|
|
* @param ptr: the pointer to free
|
|
* @param file: the file in which free() was called
|
|
* @param line: the source code line where free() was called
|
|
*
|
|
* @usage the header `debug.h` defines a macro `free(ptr)` that automatically
|
|
* inserts filename and line.
|
|
*/
|
|
void
|
|
debug_free (void *ptr, const char *file, usize line)
|
|
{
|
|
log_print_fl (LOG_LEVEL_DEBUG, file, line, "free ptr %p\n", ptr);
|
|
free (ptr);
|
|
}
|
|
|
|
/**
|
|
* allocate memory using smalloc() (see header `common.h`) and print a fitting
|
|
* debug message
|
|
*
|
|
* @param size: the number of bytes to allocate
|
|
* @param file: the file in which malloc() was called
|
|
* @param line: the source code line where malloc() was called
|
|
*
|
|
* @return the pointer to the allocated memory, or nullptr in case of error
|
|
*
|
|
* @usage the header `debug.h` defines a macro `smalloc(size)` that
|
|
* automatically inserts filename and line.
|
|
*/
|
|
void *
|
|
debug_smalloc (usize size, const char *file, usize line)
|
|
{
|
|
void *ptr;
|
|
|
|
ptr = smalloc (size);
|
|
log_print_fl (LOG_LEVEL_DEBUG, file, line,
|
|
"smalloc of size %zu to ptr %p\n", size, ptr);
|
|
|
|
return ptr;
|
|
}
|
|
|
|
/**
|
|
* reallocate memory using srealloc() (see header `common.h`) and print a
|
|
* fitting debug message
|
|
*
|
|
* @param ptr: the pointer to reallocate
|
|
* @param size: the new number of bytes
|
|
* @param file: the file in which realloc() was called
|
|
* @param line: the source code line where realloc() was called
|
|
*
|
|
* @return the pointer to the reallocated memory, or nullptr in case of error
|
|
*
|
|
* @usage the header `debug.h` defines a macro `srealloc(ptr, size)` that
|
|
* automatically inserts filename and line.
|
|
*/
|
|
void *
|
|
debug_srealloc (void *ptr, usize size, const char *file, usize line)
|
|
{
|
|
void *new_ptr;
|
|
uintptr_t old_ptr;
|
|
|
|
old_ptr = (uintptr_t) ptr;
|
|
new_ptr = srealloc (ptr, size);
|
|
if (new_ptr == ptr) {
|
|
log_print_fl (
|
|
LOG_LEVEL_DEBUG, file, line,
|
|
"srealloc of ptr %p to size %zu\n",
|
|
new_ptr, size
|
|
);
|
|
} else {
|
|
log_print_fl (
|
|
LOG_LEVEL_DEBUG, file, line,
|
|
"srealloc of ptr %#" PRIxPTR " to new ptr %p with size %zu\n",
|
|
old_ptr, new_ptr, size
|
|
);
|
|
}
|
|
|
|
return new_ptr;
|
|
}
|