Something went wrong. Try again.
Monorepo for PackTools - tooling for Minecraft Modpacks!
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041#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<Allocator *>(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<std::size_t>::max() - live_; limit_ = live_ + (extra < room ? extra : room); exceeded_ = false;}
void Allocator::unlimit() noexcept { limit_ = std::numeric_limits<std::size_t>::max(); exceeded_ = false;}
} // namespace packluau