2023-02-11 14:17:02 +01:00
|
|
|
#include "debug.h"
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdlib.h>
|
2023-02-12 18:15:08 +01:00
|
|
|
#include "common.h"
|
2023-02-11 14:17:02 +01:00
|
|
|
#include "log.h"
|
|
|
|
|
2023-02-11 14:40:27 +01:00
|
|
|
/**
|
|
|
|
* 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 NULL in case of error
|
|
|
|
*
|
|
|
|
* @errno see malloc(3)
|
|
|
|
*
|
|
|
|
* @usage the header `debug.h` defines a macro `malloc(size)` that
|
|
|
|
* automatically inserts filename and line.
|
|
|
|
*/
|
2023-02-11 14:17:02 +01:00
|
|
|
void *
|
|
|
|
debug_malloc (size_t size, const char *file, size_t line)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
ptr = malloc (size);
|
|
|
|
if (ptr == NULL)
|
|
|
|
log_print_fl (LOG_LEVEL_DEBUG, file, line, "malloc of size %zu \e[31mreturned null\e[0m\n", size);
|
|
|
|
else
|
|
|
|
log_print_fl (LOG_LEVEL_DEBUG, file, line, "malloc of size %zu to ptr %p\n", size, ptr);
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2023-02-11 14:40:27 +01:00
|
|
|
/**
|
|
|
|
* 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 NULL 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.
|
|
|
|
*/
|
2023-02-11 14:17:02 +01:00
|
|
|
void *
|
|
|
|
debug_realloc (void *ptr, size_t size, const char *file, size_t line)
|
|
|
|
{
|
|
|
|
void *new_ptr;
|
|
|
|
uintptr_t old_ptr;
|
|
|
|
|
|
|
|
old_ptr = (uintptr_t) ptr;
|
|
|
|
new_ptr = realloc (ptr, size);
|
|
|
|
if (new_ptr == NULL && size != 0)
|
|
|
|
log_print_fl (LOG_LEVEL_DEBUG, file, line, "realloc of ptr %#" PRIxPTR " to size %zu \e[31mreturned null\e[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;
|
|
|
|
}
|
|
|
|
|
2023-02-11 14:40:27 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2023-02-11 14:17:02 +01:00
|
|
|
void
|
|
|
|
debug_free (void *ptr, const char *file, size_t line)
|
|
|
|
{
|
|
|
|
log_print_fl (LOG_LEVEL_DEBUG, file, line, "free ptr %p\n", ptr);
|
|
|
|
free (ptr);
|
|
|
|
}
|
2023-02-12 18:15:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 NULL in case of error
|
|
|
|
*
|
|
|
|
* @usage the header `debug.h` defines a macro `smalloc(size)` that
|
|
|
|
* automatically inserts filename and line.
|
|
|
|
*/
|
|
|
|
void *
|
2023-02-12 18:28:44 +01:00
|
|
|
debug_smalloc (size_t size, const char *file, size_t line)
|
2023-02-12 18:15:08 +01:00
|
|
|
{
|
|
|
|
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 NULL in case of error
|
|
|
|
*
|
2023-02-12 18:28:44 +01:00
|
|
|
* @usage the header `debug.h` defines a macro `srealloc(ptr, size)` that
|
2023-02-12 18:15:08 +01:00
|
|
|
* automatically inserts filename and line.
|
|
|
|
*/
|
|
|
|
void *
|
2023-02-12 18:28:44 +01:00
|
|
|
debug_srealloc (void *ptr, size_t size, const char *file, size_t line)
|
2023-02-12 18:15:08 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|