#pragma once
#include <cstddef>
#include <cassert>
#include <cstdint>

namespace fetchml::common {

// Simple bump allocator for hot-path operations.
// NOT thread-safe - use one per thread.
class ArenaAllocator {
    static constexpr size_t BUFFER_SIZE = 256 * 1024;  // 256KB per thread
    alignas(64) char buffer_[BUFFER_SIZE];
    size_t offset_ = 0;
    bool in_use_ = false;

public:
    // Allocate size bytes with given alignment
    void* allocate(size_t size, size_t align = 8) {
        size_t aligned = (offset_ + align - 1) & ~(align - 1);
        if (aligned + size > BUFFER_SIZE) {
            return nullptr;  // Arena exhausted
        }
        void* ptr = buffer_ + aligned;
        offset_ = aligned + size;
        return ptr;
    }

    void reset() { offset_ = 0; }
    
    void begin() { in_use_ = true; reset(); }
    void end() { in_use_ = false; }
    bool in_use() const { return in_use_; }
};

// Thread-local arena access
ArenaAllocator* thread_local_arena();
void begin_arena_scope();
void end_arena_scope();

} // namespace fetchml::common
