From b6b7a01987dca604a161c8c73a240ec4430baed0 Mon Sep 17 00:00:00 2001 From: Mosder Date: Fri, 21 Aug 2026 11:50:51 +0200 Subject: refactor: renamed mem/alloc.h to safe.h and made it be a collection of "safe" wrappers for "unsafe" functions --- include/moslib.h | 2 +- include/moslib/ds/array.h | 2 +- include/moslib/mem/alloc.h | 50 ---------------------------------------------- include/moslib/safe.h | 37 ++++++++++++++++++++++++++++++++++ include/moslib/string.h | 2 +- 5 files changed, 40 insertions(+), 53 deletions(-) delete mode 100644 include/moslib/mem/alloc.h create mode 100644 include/moslib/safe.h (limited to 'include') diff --git a/include/moslib.h b/include/moslib.h index 777d951..fa549da 100644 --- a/include/moslib.h +++ b/include/moslib.h @@ -10,6 +10,6 @@ // This way you can, for example, avoid collisions #include "moslib/ds/array.h" -#include "moslib/mem/alloc.h" +#include "moslib/safe.h" #include "moslib/string.h" #include "moslib/tester.h" diff --git a/include/moslib/ds/array.h b/include/moslib/ds/array.h index 8f76901..b4c80f5 100644 --- a/include/moslib/ds/array.h +++ b/include/moslib/ds/array.h @@ -148,7 +148,7 @@ // ---------------------------------------------------------------------------------------------------- // ===================================== END OF QUICK USAGE GUIDE ===================================== -#include "moslib/mem/alloc.h" +#include "moslib/safe.h" #ifndef MOSLIB_DS_ARRAY_H #define MOSLIB_DS_ARRAY_H diff --git a/include/moslib/mem/alloc.h b/include/moslib/mem/alloc.h deleted file mode 100644 index 564c70f..0000000 --- a/include/moslib/mem/alloc.h +++ /dev/null @@ -1,50 +0,0 @@ -// moslib/mem/alloc.h -// Helper functions for general memory management - -#include - -#ifndef MOSLIB_MEM_ALLOC_H -#define MOSLIB_MEM_ALLOC_H - -// Attempt count for alloc functions -#ifndef MOS_MAX_ALLOC_ATTEMPTS -#define MOS_MAX_ALLOC_ATTEMPTS 3 -#endif // MOS_MAX_ALLOC_ATTEMPTS - -#ifndef MOS_FORCE_PREFIXES - -#define safe_malloc mos_safe_malloc -#define safe_realloc mos_safe_realloc - -#endif // MOS_FORCE_PREFIXES - -// Tries to malloc MOS_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 mos_safe_malloc(size) mos_safe_malloc_fn(size, MOS_MAX_ALLOC_ATTEMPTS, __FILE__, __LINE__, __func__) - -// Tries to realloc MOS_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 mos_safe_realloc(ptr, size) mos_safe_realloc_fn(ptr, size, MOS_MAX_ALLOC_ATTEMPTS, __FILE__, __LINE__, __func__) - -// Function prototypes for macros -extern void *mos_safe_malloc_fn(size_t size, size_t att, const char *file, int line, const char *func); -extern void *mos_safe_realloc_fn(void *ptr, size_t size, size_t att, const char *file, int line, const char *func); - -#endif // MOSLIB_MEM_ALLOC_H diff --git a/include/moslib/safe.h b/include/moslib/safe.h new file mode 100644 index 0000000..8074c31 --- /dev/null +++ b/include/moslib/safe.h @@ -0,0 +1,37 @@ +// moslib/safe.h +// "Safe" wrappers for some "unsafe" functions +// aka "I don't want to check for NULL every single time" header + +#include + +#ifndef MOSLIB_SAFE_H +#define MOSLIB_SAFE_H + +#ifndef MOS_FORCE_PREFIXES + +#define safe_malloc mos_safe_malloc +#define safe_calloc mos_safe_calloc +#define safe_realloc mos_safe_realloc + +#endif // MOS_FORCE_PREFIXES + +typedef struct { + const char *file; + int line; + const char *func; +} MosSafeErrInfo; + +#define mos_err_info ((MosSafeErrInfo){.file = __FILE__, .line = __LINE__, .func = __func__}) + +// All of the below function-like macros do the same thing the original functions do +// They check whether the operation suceeded and if not - they exit with an error message +#define mos_safe_malloc(size) mos_safe_malloc_fn(size, mos_err_info) +#define mos_safe_calloc(count, size) mos_safe_calloc_fn(count, size, mos_err_info) +#define mos_safe_realloc(ptr, size) mos_safe_realloc_fn(ptr, size, mos_err_info) + +// Function prototypes for macros +extern void *mos_safe_malloc_fn(size_t size, MosSafeErrInfo info); +extern void *mos_safe_calloc_fn(size_t count, size_t size, MosSafeErrInfo info); +extern void *mos_safe_realloc_fn(void *ptr, size_t size, MosSafeErrInfo info); + +#endif // MOSLIB_SAFE_H diff --git a/include/moslib/string.h b/include/moslib/string.h index e01c975..bc9efff 100644 --- a/include/moslib/string.h +++ b/include/moslib/string.h @@ -549,4 +549,4 @@ extern MosStringSlice mos_ss_find_str(MosStringSlice ss, const char *target); // utf-8 length extern size_t mos_ss_utf8_len(MosStringSlice ss); -#endif // MOSLIB_MEM_ALLOC_H +#endif // MOSLIB_STRING_H -- cgit v1.2.3