fix(libhectic): arena allocators

This commit is contained in:
2025-03-21 02:54:47 +00:00
parent 4f2391e9b5
commit 9438280750
7 changed files with 78 additions and 99 deletions

View File

@@ -14,13 +14,7 @@
// Helper macros for argument counting
// NOTE(yukkop): this ugly macroses for avoid all posible warnings
#define PP_CAT(a, b) PP_CAT_I(a, b)
#define PP_CAT_I(a, b) a##b
#define PP_NARG(...) PP_NARG_(__VA_ARGS__, PP_RSEQ_N())
#define PP_NARG_(...) PP_ARG_N(__VA_ARGS__)
#define PP_ARG_N(_1,_2,_3,_4,_5,_6,_7,_8,_9,N,...) N
#define PP_RSEQ_N() 9,8,7,6,5,4,3,2,1,0
#define PP_CAT(a, b) a##b
// ------------
// -- Colors --
@@ -51,20 +45,13 @@ void set_output_color_mode(ColorMode mode);
// ------------
// Define color macros based on output type
//#define ERROR_PREFIX PP_CAT_I(COLOR_RED, "Error: ")
//#define ERROR_SUFFIX PP_CAT_I(COLOR_RESET, "\n")
//#define ERROR_PREFIX PP_CAT(COLOR_RED, "Error: ")
//#define ERROR_SUFFIX PP_CAT(COLOR_RESET, "\n")
#define ERROR_PREFIX (USE_COLOR() ? "\033[1;31mError: " : "Error: ")
#define ERROR_SUFFIX (USE_COLOR() ? "\033[0m\n" : "\n")
// eprintf handling 1 or more arguments
#define eprintf_1(fmt) \
fprintf(stderr, "%s" fmt "%s", ERROR_PREFIX, ERROR_SUFFIX)
#define eprintf_2(fmt, ...) \
fprintf(stderr, "%s" fmt "%s", ERROR_PREFIX, __VA_ARGS__, ERROR_SUFFIX)
#define eprintf(...) \
PP_CAT(eprintf_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
#define eprintf(fmt, ...) "%s" fmt "%s", ERROR_PREFIX, ##__VA_ARGS__, ERROR_SUFFIX
#define todo fprintf(stderr, "%sNot implimented yet%s", COLOR_RED, COLOR_RESET);exit(1)
@@ -87,55 +74,14 @@ void logger_level(LogLevel level);
LogLevel log_level_from_string(const char *level_str);
char* log_message(LogLevel level, int line, const char *format, ...);
char* log_message(LogLevel level, char *file, int line, const char *format, ...);
// DEBUG level
#define raise_debug_1(fmt) \
log_message(LOG_LEVEL_DEBUG, __LINE__, fmt)
#define raise_debug_2(fmt, ...) \
log_message(LOG_LEVEL_DEBUG, __LINE__, fmt, __VA_ARGS__)
#define raise_debug(...) \
PP_CAT(raise_debug_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
// LOG level
#define raise_log_1(fmt) \
log_message(LOG_LEVEL_LOG, __LINE__, fmt)
#define raise_log_2(fmt, ...) \
log_message(LOG_LEVEL_LOG, __LINE__, fmt, __VA_ARGS__)
#define raise_log(...) \
PP_CAT(raise_log_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
// INFO level
#define raise_info_1(fmt) \
log_message(LOG_LEVEL_INFO, __LINE__, fmt)
#define raise_info_2(fmt, ...) \
log_message(LOG_LEVEL_INFO, __LINE__, fmt, __VA_ARGS__)
#define raise_info(...) \
PP_CAT(raise_info_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
// NOTICE level
#define raise_notice_1(fmt) \
log_message(LOG_LEVEL_NOTICE, __LINE__, fmt)
#define raise_notice_2(fmt, ...) \
log_message(LOG_LEVEL_NOTICE, __LINE__, fmt, __VA_ARGS__)
#define raise_notice(...) \
PP_CAT(raise_notice_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
// WARN level
#define raise_warn_1(fmt) \
log_message(LOG_LEVEL_WARN, __LINE__, fmt)
#define raise_warn_2(fmt, ...) \
log_message(LOG_LEVEL_WARN, __LINE__, fmt, __VA_ARGS__)
#define raise_warn(...) \
PP_CAT(raise_warn_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
// EXCEPTION level
#define raise_exception_1(fmt) \
log_message(LOG_LEVEL_EXCEPTION, __LINE__, fmt)
#define raise_exception_2(fmt, ...) \
log_message(LOG_LEVEL_EXCEPTION, __LINE__, fmt, __VA_ARGS__)
#define raise_exception(...) \
PP_CAT(raise_exception_, PP_NARG(__VA_ARGS__))(__VA_ARGS__)
#define raise_debug(fmt, ...) log_message(LOG_LEVEL_DEBUG, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
#define raise_log(fmt, ...) log_message(LOG_LEVEL_LOG, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
#define raise_info(fmt, ...) log_message(LOG_LEVEL_INFO, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
#define raise_notice(fmt, ...) log_message(LOG_LEVEL_NOTICE, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
#define raise_warn(fmt, ...) log_message(LOG_LEVEL_WARN, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
#define raise_exception(fmt, ...) log_message(LOG_LEVEL_EXCEPTION, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
#endif // EPRINTF_H
@@ -151,15 +97,43 @@ typedef struct {
size_t capacity;
} Arena;
Arena arena_init(size_t size);
// NOTE(yukkop): This macro is used to define procedures so that `__LINE__` and `__FILE__`
// in `raise_debug` reflect the location where the macro is called, not where it's defined.
#define arena_alloc_or_null(arena, size) __extension__ ({ \
void *mem__ = NULL; \
if ((arena)->begin == 0) { \
*(arena) = arena_init(ARENA_DEFAULT_SIZE); \
} \
size_t current__ = (size_t)(arena)->current - (size_t)(arena)->begin; \
if ((arena)->capacity <= current__ || (arena)->capacity - current__ < (size)) {\
raise_debug("Arena from %d with capacity %d allocated on %d cannot be allocated on %d", \
(arena)->begin, (arena)->capacity, \
(arena)->current - (arena)->begin, (size)); \
} else { \
raise_debug("Arena from %d with capacity %d allocated on %d will allocate on %d", \
(arena)->begin, (arena)->capacity, \
(arena)->begin, (arena)->capacity, (size)); \
mem__ = (arena)->current; \
(arena)->current = (char*)(arena)->current + (size); \
} \
mem__; \
})
void *arena_alloc_or_null(Arena *arena, size_t size);
Arena arena_init(size_t size);
void arena_reset(Arena *arena);
void arena_free(Arena *arena);
void *arena_alloc(Arena *arena, size_t size);
#define arena_alloc(arena, size) __extension__ ({ \
void *mem__ = arena_alloc_or_null((arena), (size)); \
if (!mem__) { \
raise_exception("Arena out of memory"); \
exit(1); \
} \
mem__; \
})
// TODO: mmap
// TODO: dynamic array style