Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/CodeGen/include/Luau/CodeAllocator.h
2727 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#pragma once
3
4
#include "Luau/CodeAllocationData.h"
5
#include "Luau/CodeGenOptions.h"
6
7
#include <vector>
8
9
#include <stddef.h>
10
#include <stdint.h>
11
12
namespace Luau
13
{
14
namespace CodeGen
15
{
16
17
constexpr uint32_t kCodeAlignment = 32;
18
19
struct CodeAllocator
20
{
21
CodeAllocator(size_t blockSize, size_t maxTotalSize);
22
CodeAllocator(size_t blockSize, size_t maxTotalSize, AllocationCallback* allocationCallback, void* allocationCallbackContext);
23
~CodeAllocator();
24
25
// Places data and code into the executable page area
26
// To allow allocation while previously allocated code is already running, allocation has page granularity
27
// It's important to group functions together so that page alignment won't result in a lot of wasted space
28
bool allocate_DEPRECATED(
29
const uint8_t* data,
30
size_t dataSize,
31
const uint8_t* code,
32
size_t codeSize,
33
uint8_t*& result,
34
size_t& resultSize,
35
uint8_t*& resultCodeStart
36
);
37
38
// Places data and code into the executable page area
39
// To allow allocation while previously allocated code is already running, allocation has page granularity
40
// It's important to group functions together so that page alignment won't result in a lot of wasted space
41
CodeAllocationData allocate(const uint8_t* data, size_t dataSize, const uint8_t* code, size_t codeSize);
42
43
// Marks executable page area as no longer executable
44
// Freed allocation area can be reused for future allocations
45
void deallocate(CodeAllocationData codeAllocationData);
46
47
// Provided to unwind info callbacks
48
void* context = nullptr;
49
50
// Called when new block is created to create and setup the unwinding information for all the code in the block
51
// 'startOffset' reserves space for data at the beginning of the page
52
void* (*createBlockUnwindInfo)(void* context, uint8_t* block, size_t blockSize, size_t& startOffset) = nullptr;
53
54
// Called to destroy unwinding information returned by 'createBlockUnwindInfo'
55
void (*destroyBlockUnwindInfo)(void* context, void* unwindData) = nullptr;
56
57
// Rounds 'size' up to the nearest OS page boundary
58
static size_t alignToPageSize(size_t size);
59
60
private:
61
// Unwind information can be placed inside the block with some implementation-specific reservations at the beginning
62
// But to simplify block space checks, we limit the max size of all that data
63
static const size_t kMaxReservedDataSize = 256;
64
65
bool allocateNewBlock(size_t& unwindInfoSize);
66
67
uint8_t* allocatePages(size_t size) const;
68
void freePages(uint8_t* mem, size_t size) const;
69
70
// Current block we use for allocations
71
uint8_t* blockPos = nullptr;
72
uint8_t* blockEnd = nullptr;
73
74
// All allocated blocks
75
std::vector<uint8_t*> blocks;
76
std::vector<void*> unwindInfos;
77
78
size_t blockSize = 0;
79
size_t maxTotalSize = 0;
80
size_t liveAllocations = 0;
81
82
AllocationCallback* allocationCallback = nullptr;
83
void* allocationCallbackContext = nullptr;
84
};
85
86
} // namespace CodeGen
87
} // namespace Luau
88
89