#include "allocator.hpp" namespace packluau { Allocator::Allocator(const pl_host &host) noexcept : host_realloc_(host.realloc), host_context_(host.ctx) {} void *Allocator::allocate(void *user_data, void *block, std::size_t old_size, std::size_t new_size) noexcept { auto &self = *static_cast(user_data); // Luau passes old_size 0 with a null block; the counts below rely on that. const std::size_t held = block == nullptr ? 0 : old_size; if (new_size > held && new_size - held > self.limit_ - self.live_) { self.exceeded_ = true; return nullptr; } void *result = self.host_realloc_(self.host_context_, block, held, new_size); if (new_size == 0) { self.live_ -= held; return nullptr; } if (result == nullptr) { return nullptr; } self.live_ = self.live_ - held + new_size; return result; } void Allocator::limit_growth(std::size_t extra) noexcept { const std::size_t room = std::numeric_limits::max() - live_; limit_ = live_ + (extra < room ? extra : room); exceeded_ = false; } void Allocator::unlimit() noexcept { limit_ = std::numeric_limits::max(); exceeded_ = false; } } // namespace packluau