From 4f4466155b6d9b5259ccd956551a675ad693e773 Mon Sep 17 00:00:00 2001 From: Mosder Date: Fri, 31 Jul 2026 14:50:59 +0200 Subject: feat: created safe_malloc and safe_realloc --- include/moslib/mem/alloc.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'include') diff --git a/include/moslib/mem/alloc.h b/include/moslib/mem/alloc.h index c542ea8..b3f4885 100644 --- a/include/moslib/mem/alloc.h +++ b/include/moslib/mem/alloc.h @@ -1,2 +1,40 @@ // moslib/mem/alloc.h // Helper functions for general memory management + +#include + +#ifndef MOSLIB_MEM_ALLOC_H +#define MOSLIB_MEM_ALLOC_H + +#define MOSLIB_MAX_ALLOC_ATTEMPTS 3 + +// Tries to malloc MOSLIB_MAX_ALLOC_ATTEMPTS times +// If all attempts fail - exits with EXIT_FAILURE code +// +// Arguments: +// size +// count of bytes to malloc +// +// Returns: +// pointer to the allocated memory +#define safe_malloc(size) safe_malloc_fn(size, __FILE__, __LINE__, __func__) + +// Tries to realloc MOSLIB_MAX_ALLOC_ATTEMPTS times +// If all attempts fail - exits with EXIT_FAILURE code +// +// Arguments: +// ptr +// pointer to the memory to realloc +// +// size +// new desired count of bytes of memory +// +// Returns: +// pointer to the reallocated memory +#define safe_realloc(ptr, size) safe_realloc_fn(ptr, size, __FILE__, __LINE__, __func__) + +// Function prototypes for macros +extern void *safe_malloc_fn(size_t size, const char *file, int line, const char *func); +extern void *safe_realloc_fn(void *ptr, size_t size, const char *file, int line, const char *func); + +#endif // MOSLIB_MEM_ALLOC_H -- cgit v1.2.3