#include "guest_heap.h" #include int factorio_guest_heap_init(FactorioGuestHeap *heap, uint8_t *memory, uint64_t memory_size, uint64_t begin, uint64_t end) { if (!heap || !memory || !begin || begin > end || end > memory_size || memory_size > SIZE_MAX || begin % 16) return 2; *heap = (FactorioGuestHeap){memory, memory_size, begin, begin, end, 0}; return 0; } int factorio_guest_heap_allocate(FactorioGuestHeap *heap, uint64_t size, uint64_t *out) { if (!heap || !out || !heap->memory || !heap->begin || heap->begin % 16 || heap->cursor < heap->begin || heap->cursor > heap->end || heap->end > heap->memory_size || heap->memory_size > SIZE_MAX || heap->allocations == UINT_MAX) return 2; uint64_t padding = (16 - heap->cursor % 16) % 16; if (padding > heap->end - heap->cursor) return 1; uint64_t address = heap->cursor + padding; if (!size) size = 1; /* operator new(0) still returns a distinct allocation. */ if (size > heap->end - address) return 1; heap->cursor = address + size; ++heap->allocations; *out = address; return 0; }