diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/_attrs.h | 7 | ||||
-rw-r--r-- | include/alloc.h | 31 |
2 files changed, 38 insertions, 0 deletions
diff --git a/include/_attrs.h b/include/_attrs.h new file mode 100644 index 0000000..513a5c7 --- /dev/null +++ b/include/_attrs.h @@ -0,0 +1,7 @@ +#ifndef MLIB__ATTRS_H +#define MLIB__ATTRS_H + +#define _mlib_pure __nodiscard__, __unsequenced__ +#define _mlib_inline gnu::__always_inline__, clang::__always_inline__ + +#endif /* !MLIB__ATTRS_H */ diff --git a/include/alloc.h b/include/alloc.h index 6eb2a30..0855e01 100644 --- a/include/alloc.h +++ b/include/alloc.h @@ -3,7 +3,38 @@ #include <stddef.h> +#include "_attrs.h" + +#ifndef MLIB_ARENA_BLKSIZE +# define MLIB_ARENA_BLKSIZE (8 * 1024) +#endif + +struct _region { + size_t len, cap; + void *data; + struct _region *next; +}; + +typedef struct { + struct _region *_head; + size_t _init; +} arena; + [[gnu::__returns_nonnull__]] void *bufalloc(void *, size_t, size_t); void *bufalloc_noterm(void *, size_t, size_t); +[[_mlib_pure, _mlib_inline]] +static inline arena +mkarena(size_t n) +{ + return (arena){._init = n ? n : MLIB_ARENA_BLKSIZE}; +} + +[[gnu::__malloc__, gnu::__alloc_size__(2, 3), gnu::__alloc_align__(4)]] +void *arena_alloc(arena *, size_t, size_t, size_t); +void arena_zero(arena *); +void arena_free(arena *); + +#define arena_new(a, T, n) ((T *)arena_alloc((a), sizeof(T), (n), alignof(T))) + #endif /* !MLIB_ALLOC_H */ |