// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details1#pragma once23#include "Luau/CodeAllocationData.h"4#include "Luau/CodeGenOptions.h"56#include <vector>78#include <stddef.h>9#include <stdint.h>1011namespace Luau12{13namespace CodeGen14{1516constexpr uint32_t kCodeAlignment = 32;1718struct CodeAllocator19{20CodeAllocator(size_t blockSize, size_t maxTotalSize);21CodeAllocator(size_t blockSize, size_t maxTotalSize, AllocationCallback* allocationCallback, void* allocationCallbackContext);22~CodeAllocator();2324// Places data and code into the executable page area25// To allow allocation while previously allocated code is already running, allocation has page granularity26// It's important to group functions together so that page alignment won't result in a lot of wasted space27bool allocate_DEPRECATED(28const uint8_t* data,29size_t dataSize,30const uint8_t* code,31size_t codeSize,32uint8_t*& result,33size_t& resultSize,34uint8_t*& resultCodeStart35);3637// Places data and code into the executable page area38// To allow allocation while previously allocated code is already running, allocation has page granularity39// It's important to group functions together so that page alignment won't result in a lot of wasted space40CodeAllocationData allocate(const uint8_t* data, size_t dataSize, const uint8_t* code, size_t codeSize);4142// Marks executable page area as no longer executable43// Freed allocation area can be reused for future allocations44void deallocate(CodeAllocationData codeAllocationData);4546// Provided to unwind info callbacks47void* context = nullptr;4849// Called when new block is created to create and setup the unwinding information for all the code in the block50// 'startOffset' reserves space for data at the beginning of the page51void* (*createBlockUnwindInfo)(void* context, uint8_t* block, size_t blockSize, size_t& startOffset) = nullptr;5253// Called to destroy unwinding information returned by 'createBlockUnwindInfo'54void (*destroyBlockUnwindInfo)(void* context, void* unwindData) = nullptr;5556// Rounds 'size' up to the nearest OS page boundary57static size_t alignToPageSize(size_t size);5859private:60// Unwind information can be placed inside the block with some implementation-specific reservations at the beginning61// But to simplify block space checks, we limit the max size of all that data62static const size_t kMaxReservedDataSize = 256;6364bool allocateNewBlock(size_t& unwindInfoSize);6566uint8_t* allocatePages(size_t size) const;67void freePages(uint8_t* mem, size_t size) const;6869// Current block we use for allocations70uint8_t* blockPos = nullptr;71uint8_t* blockEnd = nullptr;7273// All allocated blocks74std::vector<uint8_t*> blocks;75std::vector<void*> unwindInfos;7677size_t blockSize = 0;78size_t maxTotalSize = 0;79size_t liveAllocations = 0;8081AllocationCallback* allocationCallback = nullptr;82void* allocationCallbackContext = nullptr;83};8485} // namespace CodeGen86} // namespace Luau878889