2022-09-09 06:14:25 +08:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#pragma once
|
|
|
|
|
2023-08-11 22:42:37 +08:00
|
|
|
#include "Luau/CodeGen.h"
|
|
|
|
|
2022-09-09 06:14:25 +08:00
|
|
|
#include <vector>
|
2022-10-15 03:48:41 +08:00
|
|
|
|
2022-09-09 06:14:25 +08:00
|
|
|
#include <stddef.h>
|
2022-10-15 03:48:41 +08:00
|
|
|
#include <stdint.h>
|
2022-09-09 06:14:25 +08:00
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
namespace CodeGen
|
|
|
|
{
|
|
|
|
|
2022-10-22 01:54:01 +08:00
|
|
|
constexpr uint32_t kCodeAlignment = 32;
|
|
|
|
|
2022-09-09 06:14:25 +08:00
|
|
|
struct CodeAllocator
|
|
|
|
{
|
|
|
|
CodeAllocator(size_t blockSize, size_t maxTotalSize);
|
2023-08-11 22:42:37 +08:00
|
|
|
CodeAllocator(size_t blockSize, size_t maxTotalSize, AllocationCallback* allocationCallback, void* allocationCallbackContext);
|
2022-09-09 06:14:25 +08:00
|
|
|
~CodeAllocator();
|
|
|
|
|
|
|
|
// Places data and code into the executable page area
|
|
|
|
// To allow allocation while previously allocated code is already running, allocation has page granularity
|
|
|
|
// It's important to group functions together so that page alignment won't result in a lot of wasted space
|
2023-03-18 03:20:37 +08:00
|
|
|
bool allocate(
|
|
|
|
const uint8_t* data, size_t dataSize, const uint8_t* code, size_t codeSize, uint8_t*& result, size_t& resultSize, uint8_t*& resultCodeStart);
|
2022-09-09 06:14:25 +08:00
|
|
|
|
2023-08-11 22:42:37 +08:00
|
|
|
// Provided to unwind info callbacks
|
2022-09-09 06:14:25 +08:00
|
|
|
void* context = nullptr;
|
|
|
|
|
|
|
|
// Called when new block is created to create and setup the unwinding information for all the code in the block
|
2022-10-07 08:23:29 +08:00
|
|
|
// 'startOffset' reserves space for data at the beginning of the page
|
|
|
|
void* (*createBlockUnwindInfo)(void* context, uint8_t* block, size_t blockSize, size_t& startOffset) = nullptr;
|
2022-09-09 06:14:25 +08:00
|
|
|
|
|
|
|
// Called to destroy unwinding information returned by 'createBlockUnwindInfo'
|
|
|
|
void (*destroyBlockUnwindInfo)(void* context, void* unwindData) = nullptr;
|
|
|
|
|
2023-08-11 22:42:37 +08:00
|
|
|
private:
|
2022-10-07 08:23:29 +08:00
|
|
|
// Unwind information can be placed inside the block with some implementation-specific reservations at the beginning
|
|
|
|
// But to simplify block space checks, we limit the max size of all that data
|
|
|
|
static const size_t kMaxReservedDataSize = 256;
|
2022-09-09 06:14:25 +08:00
|
|
|
|
|
|
|
bool allocateNewBlock(size_t& unwindInfoSize);
|
|
|
|
|
2023-08-11 22:42:37 +08:00
|
|
|
uint8_t* allocatePages(size_t size) const;
|
|
|
|
void freePages(uint8_t* mem, size_t size) const;
|
|
|
|
|
2022-09-09 06:14:25 +08:00
|
|
|
// Current block we use for allocations
|
|
|
|
uint8_t* blockPos = nullptr;
|
|
|
|
uint8_t* blockEnd = nullptr;
|
|
|
|
|
|
|
|
// All allocated blocks
|
|
|
|
std::vector<uint8_t*> blocks;
|
|
|
|
std::vector<void*> unwindInfos;
|
|
|
|
|
|
|
|
size_t blockSize = 0;
|
|
|
|
size_t maxTotalSize = 0;
|
2023-08-11 22:42:37 +08:00
|
|
|
|
|
|
|
AllocationCallback* allocationCallback = nullptr;
|
|
|
|
void* allocationCallbackContext = nullptr;
|
2022-09-09 06:14:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace CodeGen
|
|
|
|
} // namespace Luau
|