// vim:fileencoding=utf-8:foldmethod=marker

#ifndef MEM_ARENA_H
#define MEM_ARENA_H

#include "../../mem/mem_os.h"
#include "../../../common/aliases/aliases.h"
#include "../../../common/platform/platform.h"

#ifdef WP_PLATFORM_CPP
BEGIN_C_LINKAGE
#endif // !WP_PLATFORM_CPP

typedef struct WpArena WpArena;

#define wpMemArenaInitAllocated(arena_dptr, base_capacity)                                                      \
	(wpMemArenaInitAllocatedCustom(arena_dptr, base_capacity, WP_MEM_ALLOC_RESERVE, false))
#define wpMemArenaInitAllocatedCommit(arena_dptr, base_capacity)                                                \
	(wpMemArenaInitAllocatedCustom(arena_dptr, base_capacity, WP_MEM_ALLOC_RESERVE | WP_MEM_ALLOC_COMMIT, false))
#define wpMemArenaInitAllocatedZero(arena_dptr, base_capacity)                                                  \
	(wpMemArenaInitAllocatedCustom(arena_dptr, base_capacity, WP_MEM_ALLOC_RESERVE, true))
#define wpMemArenaInitAllocatedCommitAndZero(arena_dptr, base_capacity)                                         \
	(wpMemArenaInitAllocatedCustom(arena_dptr, base_capacity, WP_MEM_ALLOC_RESERVE | WP_MEM_ALLOC_COMMIT, true))

/**
 * WpArena initialisation function. `wpMemArenaInitAllocatedCustom` provides the most
 * control over how the WpArena is initialised. Wrapper macros are provided for
 * easier use.
 */
b8    wpMemArenaInitAllocatedCustom(WpArena **arena, u64 base_capacity, WpMemAllocFlags flags, b8 zero_buffer);
b8    wpMemArenaInitBuffer(WpArena **arena, u8 *buffer, u64 buffer_size);
void *wpMemArenaAlloc(WpArena *arena, u64 size);
void *wpMemArenaAllocAligned(WpArena *arena, u64 size, u64 alignment);
void *wpMemArenaRealloc(WpArena *arena, void *ptr, u64 old_size, u64 new_size);
void *wpMemArenaReallocAligned(WpArena *arena, void *ptr, u64 old_size, u64 new_size, u64 alignment);
void  wpMemArenaTempBegin(WpArena *arena);
void  wpMemArenaTempEnd(WpArena *arena);
void  wpMemArenaClear(WpArena *arena);
void  wpMemArenaDestroy(WpArena **arena);

#ifdef WP_PLATFORM_CPP
END_C_LINKAGE
#endif // !WP_PLATFORM_CPP

#endif // !MEM_ARENA_H
