feat: (common, debug) safe allocation functions

This commit is contained in:
thetek 2023-02-12 18:15:08 +01:00
parent c87149338c
commit 0732dc4717
4 changed files with 111 additions and 0 deletions

42
lib/common.c Normal file
View File

@ -0,0 +1,42 @@
#include "common.h"
#include <stdlib.h>
#include "log.h"
/**
* safe malloc. instead of returning null on error, the program will exit.
*
* @param size: the number of bytes to allocate
*/
void *
smalloc (size_t size)
{
void *ptr;
ptr = malloc (size);
if (!ptr)
{
log_error ("failed to allocate %zu bytes of memory\n", size);
exit (EXIT_FAILURE);
}
return ptr;
}
/**
* safe realloc. instead of returning null on error, the program will exit.
*
* @param ptr: the pointer to reallocate
* @param size: the new number of bytes
*/
void *
srealloc (void *ptr, size_t size)
{
ptr = realloc (ptr, size);
if (!ptr)
{
log_error ("failed to allocate %zu bytes of memory\n", size);
exit (EXIT_FAILURE);
}
return ptr;
}

View File

@ -1,6 +1,7 @@
#include "debug.h"
#include <inttypes.h>
#include <stdlib.h>
#include "common.h"
#include "log.h"
/**
@ -80,3 +81,59 @@ 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);
}
/**
* 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 *
debug_malloc (size_t size, const char *file, size_t 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 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.
*/
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 = 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;
}

7
lib/inc/common.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef CUTILS_COMMON_H_
#define CUTILS_COMMON_H_
void *smalloc (size_t size);
void *srealloc (void *ptr, size_t size);
#endif // CUTILS_COMMON_H_

View File

@ -3,15 +3,20 @@
#include <stddef.h>
#include <stdlib.h>
#include "common.h"
#ifdef CUTILS_DEBUG_TRACE
# define malloc(size) debug_malloc (size, __FILE__, __LINE__) /* macro wrapper for debug_malloc() */
# define realloc(ptr, size) debug_realloc (ptr, size, __FILE__, __LINE__) /* macro wrapper for debug_realloc() */
# define free(ptr) debug_free (ptr, __FILE__, __LINE__) /* macro wrapper for debug_free() */
# define smalloc(size) debug_smalloc (size, __FILE__, __LINE__) /* macro wrapper for debug_malloc() */
# define srealloc(ptr, size) debug_srealloc (ptr, size, __FILE__, __LINE__) /* macro wrapper for debug_realloc() */
#endif
void *debug_malloc (size_t size, const char *file, size_t line); /* allocate memory using malloc() and print a fitting debug message */
void *debug_realloc (void *ptr, size_t size, const char *file, size_t line); /* reallocate memory using realloc() and print a fitting debug message */
void debug_free (void *ptr, const char *file, size_t line); /* free memory using free() and print a fitting debug message */
void *debug_smalloc (size_t size, const char *file, size_t line); /* allocate memory using smalloc() and print a fitting debug message */
void *debug_srealloc (void *ptr, size_t size, const char *file, size_t line); /* reallocate memory using srealloc() and print a fitting debug message */
#endif // CUTILS_DEBUG_H_