-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpz_library.cpp
More file actions
149 lines (119 loc) · 3.35 KB
/
pz_library.cpp
File metadata and controls
149 lines (119 loc) · 3.35 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Plasma in-memory representation
* vim: ts=4 sw=4 et
*
* Copyright (C) Plasma Team
* Distributed under the terms of the MIT license, see ../LICENSE.code
*/
#include "pz_common.h"
#include <stdio.h>
#include <string.h>
#include <utility>
#include "pz_closure.h"
#include "pz_util.h"
#include "pz_library.h"
namespace pz {
/*
* LibraryLoading class
**********************/
LibraryLoading::LibraryLoading(unsigned num_structs,
unsigned num_data, unsigned num_procs,
unsigned num_closures, NoGCScope & no_gc)
: m_total_code_size(0)
{
m_structs.reserve(num_structs);
m_datas.reserve(num_data);
m_procs.reserve(num_procs);
m_closures.reserve(num_closures);
for (unsigned i = 0; i < num_closures; i++) {
m_closures.push_back(new (no_gc) Closure());
}
}
Struct * LibraryLoading::new_struct(unsigned num_fields,
GCCapability & gc_cap)
{
NoGCScope nogc(gc_cap);
Struct * struct_ = new (nogc) Struct(nogc, num_fields);
if (nogc.is_oom()) return nullptr;
m_structs.push_back(struct_);
return struct_;
}
void LibraryLoading::add_data(void * data)
{
m_datas.push_back(data);
}
Proc * LibraryLoading::new_proc(String name, unsigned size, bool is_builtin,
GCCapability & gc_cap)
{
// Either the proc object, or the code area within it are untracable
// while the proc is constructed.
NoGCScope no_gc(gc_cap);
Proc * proc = new (no_gc) Proc(no_gc, name, is_builtin, size);
if (no_gc.is_oom()) return nullptr;
m_procs.push_back(proc);
m_total_code_size += proc->size();
return proc;
}
void LibraryLoading::add_symbol(String name, Closure * closure)
{
m_symbols.insert(std::make_pair(name, closure));
}
void LibraryLoading::print_loaded_stats() const
{
printf("Loaded %d procedures with a total of %d bytes.\n",
num_procs(),
m_total_code_size);
}
void LibraryLoading::do_trace(HeapMarkState * marker) const
{
/*
* This is needed in case we GC during loading, we want to keep this
* module until we know we're done loading it.
*/
for (Struct * s : m_structs) {
marker->mark_root(s);
}
for (void * d : m_datas) {
marker->mark_root(d);
}
for (void * p : m_procs) {
marker->mark_root(p);
}
for (void * c : m_closures) {
marker->mark_root(c);
}
for (auto symbol : m_symbols) {
marker->mark_root(symbol.first.ptr());
marker->mark_root(symbol.second);
}
}
/*
* Library class
***************/
Library::Library() : m_entry_closure(nullptr) {}
Library::Library(LibraryLoading & loading)
: m_symbols(loading.m_symbols)
, m_entry_closure(nullptr)
{}
void Library::add_symbol(String name, Closure * closure)
{
m_symbols.insert(std::make_pair(name, closure));
}
Optional<Closure *> Library::lookup_symbol(String name) const
{
auto iter = m_symbols.find(name);
if (iter != m_symbols.end()) {
return iter->second;
} else {
return Optional<Closure *>::Nothing();
}
}
void Library::do_trace(HeapMarkState * marker) const
{
for (auto symbol : m_symbols) {
marker->mark_root(symbol.first.ptr());
marker->mark_root(symbol.second);
}
marker->mark_root(m_entry_closure);
}
} // namespace pz