forked from crosire/reshade
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsyntax_tree.hpp
103 lines (85 loc) · 2.15 KB
/
syntax_tree.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* Copyright (C) 2014 Patrick Mours. All rights reserved.
* License: https://github.com/crosire/reshade#license
*/
#pragma once
#include <list>
#include "syntax_tree_nodes.hpp"
namespace reshadefx
{
class syntax_tree
{
syntax_tree(const syntax_tree &) = delete;
syntax_tree &operator=(const syntax_tree &) = delete;
public:
syntax_tree() { }
template <typename T>
T *make_node(const location &location)
{
const auto node = _pool.add<T>();
node->location = location;
return node;
}
std::vector<nodes::struct_declaration_node *> structs;
std::vector<nodes::variable_declaration_node *> variables;
std::vector<nodes::function_declaration_node *> functions;
std::vector<nodes::technique_declaration_node *> techniques;
private:
class memory_pool
{
struct page
{
explicit page(size_t size) : cursor(0), memory(size, '\0') { }
size_t cursor;
std::vector<unsigned char> memory;
};
struct nodeinfo
{
size_t size;
void(*dtor)(void *);
unsigned char data[sizeof(node)];
};
public:
~memory_pool()
{
clear();
}
template <typename T>
T *add()
{
auto size = sizeof(nodeinfo) - sizeof(node) + sizeof(T);
auto page = std::find_if(_pages.begin(), _pages.end(),
[size](const struct page &page)
{
return page.cursor + size < page.memory.size();
});
if (page == _pages.end())
{
_pages.emplace_back(std::max(size_t(4096), size));
page = std::prev(_pages.end());
}
const auto node = new (&page->memory.at(page->cursor)) nodeinfo;
const auto node_data = new (&node->data) T();
node->size = size;
node->dtor = [](void *object) { reinterpret_cast<T *>(object)->~T(); };
page->cursor += node->size;
return node_data;
}
void clear()
{
for (auto &page : _pages)
{
auto node = reinterpret_cast<nodeinfo *>(&page.memory.front());
do
{
node->dtor(node->data);
node = reinterpret_cast<nodeinfo *>(reinterpret_cast<unsigned char *>(node) + node->size);
}
while (node->size > 0 && (page.cursor -= node->size) > 0);
}
}
private:
std::list<page> _pages;
} _pool;
};
}