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

#ifndef MEM_ARENA_ALLOCATOR_H
#define MEM_ARENA_ALLOCATOR_H

#include "../../mem/mem_os.h"
#include "../../../common/aliases/aliases.h"
#include "../../../common/platform/platform.h"
#include "../../../base/mem/allocator/mem_allocator.h"

#ifdef WP_PLATFORM_CPP
BEGIN_C_LINKAGE
#endif // !WP_PLATFORM_CPP

#define wpMemArenaAllocatorInit(base_capacity)                                                     \
	(wpMemArenaAllocatorInitCustom(base_capacity, WP_MEM_ALLOC_RESERVE, false))
#define wpMemArenaAllocatorInitCommit(base_capacity)                                               \
	(wpMemArenaAllocatorInitCustom(base_capacity, WP_MEM_ALLOC_RESERVE | WP_MEM_ALLOC_COMMIT, false))
#define wpMemArenaAllocatorInitZero(base_capacity)                                                 \
	(wpMemArenaAllocatorInitCustom(base_capacity, WP_MEM_ALLOC_RESERVE, true))
#define wpMemArenaAllocatorInitCommitAndZero(base_capacity)                                        \
	(wpMemArenaAllocatorInitCustom(base_capacity, WP_MEM_ALLOC_RESERVE | WP_MEM_ALLOC_COMMIT, true))

/**
 * Wraps a WpArena in a WpAllocator object. It attempts to initialise the WpArena
 * and, if successful, defines the operations supported by it to be used by the
 * WpAllocator.
 *
 * An WpArena allocator only supports normal allocation and aligned allocation.
 * Reallocation, aligned reallocation and freeing aren't implemented.
 *
 * The `wpMemArenaAllocatorInitCustom` provides the most control over how
 * the WpArena is initialised. Wrapper macros are provided for easier use.
 */
WpAllocator wpMemArenaAllocatorInitCustom(u64 base_capacity, WpMemAllocFlags flags, b8 zero_buffer);
WpAllocator wpMemArenaAllocatorInitWithBuffer(u8 *buffer, u64 buffer_size);
void        wpMemArenaAllocatorTempBegin(const WpAllocator *allocator);
void        wpMemArenaAllocatorTempEnd(const WpAllocator *allocator);
void        wpMemArenaAllocatorClear(WpAllocator *allocator);
void        wpMemArenaAllocatorDestroy(WpAllocator *allocator);

#ifdef WP_PLATFORM_CPP
END_C_LINKAGE
#endif // !WP_PLATFORM_CPP

#endif // !MEM_ARENA_ALLOCATOR_H
