diff options
| author | Mosder <xmosder12@gmail.com> | 2026-07-31 14:50:59 +0200 |
|---|---|---|
| committer | Mosder <xmosder12@gmail.com> | 2026-07-31 14:50:59 +0200 |
| commit | 4f4466155b6d9b5259ccd956551a675ad693e773 (patch) | |
| tree | a6d1f2ebe2c8ac169b675e2ab6d4d8a3c3a09075 /include | |
| parent | c5ed6021ca7715adafb5cac1a5d5581d69eb5589 (diff) | |
feat: created safe_malloc and safe_realloc
Diffstat (limited to 'include')
| -rw-r--r-- | include/moslib/mem/alloc.h | 38 |
1 files changed, 38 insertions, 0 deletions
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 <stddef.h> + +#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 |
