Commit 65c23528 authored by yonghong-song's avatar yonghong-song Committed by GitHub

Merge pull request #1457 from palmtenor/loader

Do not keep Loader instances around
parents c32b845d 5941f2b7
...@@ -101,6 +101,7 @@ class MyMemoryManager : public SectionMemoryManager { ...@@ -101,6 +101,7 @@ class MyMemoryManager : public SectionMemoryManager {
BPFModule::BPFModule(unsigned flags, TableStorage *ts) BPFModule::BPFModule(unsigned flags, TableStorage *ts)
: flags_(flags), : flags_(flags),
used_b_loader_(false),
ctx_(new LLVMContext), ctx_(new LLVMContext),
id_(std::to_string((uintptr_t)this)), id_(std::to_string((uintptr_t)this)),
ts_(ts) { ts_(ts) {
...@@ -464,9 +465,9 @@ unique_ptr<ExecutionEngine> BPFModule::finalize_rw(unique_ptr<Module> m) { ...@@ -464,9 +465,9 @@ unique_ptr<ExecutionEngine> BPFModule::finalize_rw(unique_ptr<Module> m) {
// load an entire c file as a module // load an entire c file as a module
int BPFModule::load_cfile(const string &file, bool in_memory, const char *cflags[], int ncflags) { int BPFModule::load_cfile(const string &file, bool in_memory, const char *cflags[], int ncflags) {
clang_loader_ = ebpf::make_unique<ClangLoader>(&*ctx_, flags_); ClangLoader clang_loader(&*ctx_, flags_);
if (clang_loader_->parse(&mod_, *ts_, file, in_memory, cflags, ncflags, id_, if (clang_loader.parse(&mod_, *ts_, file, in_memory, cflags, ncflags, id_,
*func_src_, mod_src_)) *func_src_, mod_src_))
return -1; return -1;
return 0; return 0;
} }
...@@ -477,9 +478,9 @@ int BPFModule::load_cfile(const string &file, bool in_memory, const char *cflags ...@@ -477,9 +478,9 @@ int BPFModule::load_cfile(const string &file, bool in_memory, const char *cflags
// Load in a pre-built list of functions into the initial Module object, then // Load in a pre-built list of functions into the initial Module object, then
// build an ExecutionEngine. // build an ExecutionEngine.
int BPFModule::load_includes(const string &text) { int BPFModule::load_includes(const string &text) {
clang_loader_ = ebpf::make_unique<ClangLoader>(&*ctx_, flags_); ClangLoader clang_loader(&*ctx_, flags_);
if (clang_loader_->parse(&mod_, *ts_, text, true, nullptr, 0, "", *func_src_, if (clang_loader.parse(&mod_, *ts_, text, true, nullptr, 0, "", *func_src_,
mod_src_)) mod_src_))
return -1; return -1;
return 0; return 0;
} }
...@@ -816,7 +817,7 @@ const char * BPFModule::table_name(size_t id) const { ...@@ -816,7 +817,7 @@ const char * BPFModule::table_name(size_t id) const {
} }
const char * BPFModule::table_key_desc(size_t id) const { const char * BPFModule::table_key_desc(size_t id) const {
if (b_loader_) return nullptr; if (used_b_loader_) return nullptr;
if (id >= tables_.size()) if (id >= tables_.size())
return nullptr; return nullptr;
return tables_[id]->key_desc.c_str(); return tables_[id]->key_desc.c_str();
...@@ -827,7 +828,7 @@ const char * BPFModule::table_key_desc(const string &name) const { ...@@ -827,7 +828,7 @@ const char * BPFModule::table_key_desc(const string &name) const {
} }
const char * BPFModule::table_leaf_desc(size_t id) const { const char * BPFModule::table_leaf_desc(size_t id) const {
if (b_loader_) return nullptr; if (used_b_loader_) return nullptr;
if (id >= tables_.size()) if (id >= tables_.size())
return nullptr; return nullptr;
return tables_[id]->leaf_desc.c_str(); return tables_[id]->leaf_desc.c_str();
...@@ -932,8 +933,9 @@ int BPFModule::load_b(const string &filename, const string &proto_filename) { ...@@ -932,8 +933,9 @@ int BPFModule::load_b(const string &filename, const string &proto_filename) {
if (int rc = load_includes(helpers_h->second)) if (int rc = load_includes(helpers_h->second))
return rc; return rc;
b_loader_.reset(new BLoader(flags_)); BLoader b_loader(flags_);
if (int rc = b_loader_->parse(&*mod_, filename, proto_filename, *ts_, id_)) used_b_loader_ = true;
if (int rc = b_loader.parse(&*mod_, filename, proto_filename, *ts_, id_))
return rc; return rc;
if (int rc = annotate()) if (int rc = annotate())
return rc; return rc;
......
...@@ -120,14 +120,13 @@ class BPFModule { ...@@ -120,14 +120,13 @@ class BPFModule {
private: private:
unsigned flags_; // 0x1 for printing unsigned flags_; // 0x1 for printing
bool used_b_loader_;
std::string filename_; std::string filename_;
std::string proto_filename_; std::string proto_filename_;
std::unique_ptr<llvm::LLVMContext> ctx_; std::unique_ptr<llvm::LLVMContext> ctx_;
std::unique_ptr<llvm::ExecutionEngine> engine_; std::unique_ptr<llvm::ExecutionEngine> engine_;
std::unique_ptr<llvm::ExecutionEngine> rw_engine_; std::unique_ptr<llvm::ExecutionEngine> rw_engine_;
std::unique_ptr<llvm::Module> mod_; std::unique_ptr<llvm::Module> mod_;
std::unique_ptr<BLoader> b_loader_;
std::unique_ptr<ClangLoader> clang_loader_;
std::unique_ptr<FuncSource> func_src_; std::unique_ptr<FuncSource> func_src_;
std::map<std::string, std::tuple<uint8_t *, uintptr_t>> sections_; std::map<std::string, std::tuple<uint8_t *, uintptr_t>> sections_;
std::vector<TableDesc *> tables_; std::vector<TableDesc *> tables_;
......
...@@ -64,15 +64,11 @@ using std::vector; ...@@ -64,15 +64,11 @@ using std::vector;
namespace ebpf { namespace ebpf {
map<string, unique_ptr<llvm::MemoryBuffer>> ClangLoader::remapped_files_;
ClangLoader::ClangLoader(llvm::LLVMContext *ctx, unsigned flags) ClangLoader::ClangLoader(llvm::LLVMContext *ctx, unsigned flags)
: ctx_(ctx), flags_(flags) : ctx_(ctx), flags_(flags)
{ {
if (remapped_files_.empty()) { for (auto f : ExportedFiles::headers())
for (auto f : ExportedFiles::headers()) remapped_files_[f.first] = llvm::MemoryBuffer::getMemBuffer(f.second);
remapped_files_[f.first] = llvm::MemoryBuffer::getMemBuffer(f.second);
}
} }
ClangLoader::~ClangLoader() {} ClangLoader::~ClangLoader() {}
......
...@@ -66,7 +66,7 @@ class ClangLoader { ...@@ -66,7 +66,7 @@ class ClangLoader {
std::string &mod_src, bool use_internal_bpfh); std::string &mod_src, bool use_internal_bpfh);
private: private:
static std::map<std::string, std::unique_ptr<llvm::MemoryBuffer>> remapped_files_; std::map<std::string, std::unique_ptr<llvm::MemoryBuffer>> remapped_files_;
llvm::LLVMContext *ctx_; llvm::LLVMContext *ctx_;
unsigned flags_; unsigned flags_;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment