/* =====================================
 * Generated by Mod-c 0.26.5
 * HEADER arena MODULE
 * ===================================== */

#ifndef MODC_MODULE_arena_H
#define MODC_MODULE_arena_H

#include <stdint.h>
#include <stdbool.h>
#include <assert.h>

#define nil ((void *)0)
#define NIL nil
typedef uint8_t byte;
typedef const char* string;
typedef int integer;
typedef unsigned int cardinal;
typedef float real;

/**
 * Mod-c
 * By M. Scott Reynolds
 * Date 20 February 2026
 *
 * arena.c - Linear / bump allocator with linked chunks
 * 
 * 25 July 2026
 * Growth never relocates existing slabs (pointer-stable).
 * Old bug: single-slab realloc invalidated interior pointers (AST, symtab).
 */

/* === IMPORT === */
#include "stddef.h"

/* === IMPORT === */
#include "string.h"

/* === IMPORT === */
#include "stdlib.h"

/* === IMPORT === */
#include "stdio.h"

/* === TYPE EXPORT === */
typedef char* pchar;

/* === TYPE EXPORT === */
typedef void* pvoid;

/* === TYPE EXPORT === */
typedef pvoid* ppvoid;

/** One malloc'd slab: header + payload. Never realloc this block */

/* === TYPE EXPORT === */
typedef struct ArenaChunk ArenaChunk;
typedef struct ArenaChunk {
    ArenaChunk* next;
    size_t size;
    size_t used;
} ArenaChunk;

/** 
 * Opaque arena allocator structure.
 */

/* === TYPE EXPORT === */
typedef struct Arena Arena;
typedef struct Arena {
    ArenaChunk* head;
    ArenaChunk* current;
} Arena;

/* === TYPE EXPORT === */
typedef Arena* pArena;

/**
 * Payload starts immediately after the chunk header. 
 */

/**
 * Allocate a new chunk with at least payload_size bytes of payload.
 * Links are left nil; caller attaches to the list.
 */

/**
 * Initialize an arena with given initial capacity.
 * Exits program on allocation failure.
 */

/* === PROC/FUNC EXPORT === */
void arena_init(pArena a, size_t initial_size);

/**
 * Append a new chunk large enough for 'needed' payload bytes.
 * Previous chunks are left intact (pointers into them stay valid).
 */

/**
 * Allocate 'size' bytes with ARENA_ALIGNMENT alignment.
 * Automatically grows arena if needed.
 * Never returns NULL (exits on failure).
 */

/* === PROC/FUNC EXPORT === */
pvoid arena_alloc(pArena a, size_t size);

/**
 * Reset: keep the first chunk, free any extra chunks, bump from head again.
 * Safe for multi-file compiles in one process.
 */

/* === PROC/FUNC EXPORT === */
void arena_reset(pArena a);

/**
 * Free all memory owned the arena.
 * Safe to call multiple times.
 */

/* === PROC/FUNC EXPORT === */
void arena_free(pArena a);

/**
 * arena_ensure_capacity - grow pointer array if needed (doubling strategy)
 */

/* === PROC/FUNC EXPORT === */
void arena_ensure_capacity(Arena* arena, ppvoid array_ptr, size_t* count, size_t* capacity);

/**
 * arena_append_ptr - Append one pointer to a dynamic array of pointers.
 * Grows the array using doubling strategy (min 8) if needed.
 * Never returns NULL - exits on fatal error.
 *
 * @param arena 	Initialized arena
 * @param array_ptr	Pointer to the array base pointer (Node**, TType**, etc.)
 * @param count 	Current element count (will be incremented)
 * @param capacity 	Current allocated slots (will be updated if grown)
 * @param item 		Pointer to value to append
 */

/* === PROC/FUNC EXPORT === */
void arena_append_ptr(Arena* arena, ppvoid array_ptr, size_t* count, size_t* capacity, pvoid item);

void arena(void);

#endif /* MODC_MODULE_arena_H */
