Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/Ast/src/Allocator.cpp
2725 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
3
#include "Luau/Allocator.h"
4
5
namespace Luau
6
{
7
8
Allocator::Allocator()
9
: root(static_cast<Page*>(operator new(sizeof(Page))))
10
, offset(0)
11
{
12
root->next = nullptr;
13
}
14
15
Allocator::Allocator(Allocator&& rhs)
16
: root(rhs.root)
17
, offset(rhs.offset)
18
{
19
rhs.root = nullptr;
20
rhs.offset = 0;
21
}
22
23
Allocator::~Allocator()
24
{
25
Page* page = root;
26
27
while (page)
28
{
29
Page* next = page->next;
30
31
operator delete(page);
32
33
page = next;
34
}
35
}
36
37
void* Allocator::allocate(size_t size)
38
{
39
constexpr size_t align = alignof(void*) > alignof(double) ? alignof(void*) : alignof(double);
40
41
if (root)
42
{
43
uintptr_t data = reinterpret_cast<uintptr_t>(root->data);
44
uintptr_t result = (data + offset + align - 1) & ~(align - 1);
45
if (result + size <= data + sizeof(root->data))
46
{
47
offset = result - data + size;
48
return reinterpret_cast<void*>(result);
49
}
50
}
51
52
// allocate new page
53
size_t pageSize = size > sizeof(root->data) ? size : sizeof(root->data);
54
void* pageData = operator new(offsetof(Page, data) + pageSize);
55
56
Page* page = static_cast<Page*>(pageData);
57
58
page->next = root;
59
60
root = page;
61
offset = size;
62
63
return page->data;
64
}
65
66
} // namespace Luau
67
68