fetch_ml/native/common/include/arena_allocator.h
Jeremie Fraeys 43d241c28d
feat: implement C++ native libraries for performance-critical operations
- Add arena allocator for zero-allocation hot paths
- Add thread pool for parallel operations
- Add mmap utilities for memory-mapped I/O
- Implement queue_index with heap-based priority queue
- Implement dataset_hash with SIMD support (SHA-NI, ARMv8)
- Add runtime SIMD detection for cross-platform correctness
- Add comprehensive tests and benchmarks
2026-02-16 20:38:04 -05:00

40 lines
1 KiB
C++

#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