Commit b6b9c6db authored by Teng Qin's avatar Teng Qin

Move FileDesc to common.h

parent a09c4913
......@@ -329,7 +329,7 @@ unique_ptr<ExecutionEngine> BPFModule::finalize_rw(unique_ptr<Module> m) {
// load an entire c file as a module
int BPFModule::load_cfile(const string &file, bool in_memory, const char *cflags[], int ncflags) {
clang_loader_ = make_unique<ClangLoader>(&*ctx_, flags_);
clang_loader_ = ebpf::make_unique<ClangLoader>(&*ctx_, flags_);
if (clang_loader_->parse(&mod_, *ts_, file, in_memory, cflags, ncflags, id_))
return -1;
return 0;
......@@ -341,7 +341,7 @@ 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
// build an ExecutionEngine.
int BPFModule::load_includes(const string &text) {
clang_loader_ = make_unique<ClangLoader>(&*ctx_, flags_);
clang_loader_ = ebpf::make_unique<ClangLoader>(&*ctx_, flags_);
if (clang_loader_->parse(&mod_, *ts_, text, true, nullptr, 0, ""))
return -1;
return 0;
......@@ -353,7 +353,7 @@ int BPFModule::annotate() {
fn->addFnAttr(Attribute::AlwaysInline);
// separate module to hold the reader functions
auto m = make_unique<Module>("sscanf", *ctx_);
auto m = ebpf::make_unique<Module>("sscanf", *ctx_);
struct llvmfnpointers {
llvm::Function *key_sscanf;
......@@ -461,7 +461,7 @@ int BPFModule::finalize() {
string err;
EngineBuilder builder(move(mod_));
builder.setErrorStr(&err);
builder.setMCJITMemoryManager(make_unique<MyMemoryManager>(&sections_));
builder.setMCJITMemoryManager(ebpf::make_unique<MyMemoryManager>(&sections_));
builder.setMArch("bpf");
builder.setUseOrcMCJITReplacement(true);
engine_ = unique_ptr<ExecutionEngine>(builder.create());
......
......@@ -15,6 +15,8 @@
*/
#include <fstream>
#include <sstream>
#include <unistd.h>
#include "common.h"
namespace ebpf {
......@@ -47,5 +49,37 @@ std::vector<int> get_possible_cpus() {
return read_cpu_range("/sys/devices/system/cpu/possible");
}
FileDesc::FileDesc(int fd) : fd_(fd) {}
FileDesc::FileDesc(FileDesc &&that) : fd_(-1) { *this = std::move(that); }
FileDesc::~FileDesc() {
if (fd_ >= 0)
::close(fd_);
}
FileDesc &FileDesc::operator=(int fd) {
if (fd_ >= 0)
::close(fd_);
fd_ = fd;
return *this;
}
FileDesc &FileDesc::operator=(FileDesc &&that) {
if (fd_ >= 0)
::close(fd_);
fd_ = that.fd_;
that.fd_ = -1;
return *this;
}
FileDesc FileDesc::dup() const {
int dup_fd = ::dup(fd_);
return FileDesc(dup_fd);
}
FileDesc::operator int() { return fd_; }
FileDesc::operator int() const { return fd_; }
} // namespace ebpf
......@@ -33,4 +33,27 @@ std::vector<int> get_online_cpus();
std::vector<int> get_possible_cpus();
/// FileDesc is a helper class for managing open file descriptors. Copy is
/// disallowed (call dup instead), and cleanup happens automatically.
class FileDesc {
public:
explicit FileDesc(int fd = -1);
FileDesc(FileDesc &&that);
FileDesc(const FileDesc &that) = delete;
~FileDesc();
FileDesc &operator=(int fd);
FileDesc &operator=(FileDesc &&that);
FileDesc &operator=(const FileDesc &that) = delete;
operator int();
operator int() const;
FileDesc dup() const;
private:
int fd_;
};
} // namespace ebpf
......@@ -33,6 +33,7 @@
#include <llvm/IR/Module.h>
#include "bcc_exception.h"
#include "common.h"
#include "codegen_llvm.h"
#include "lexer.h"
#include "libbpf.h"
......@@ -1244,7 +1245,7 @@ StatusTuple CodegenLLVM::visit(Node *root, TableStorage &ts, const string &id) {
map_type = BPF_MAP_TYPE_ARRAY;
ts.Insert(Path({id, table.first->id_->name_}),
{
table.first->id_->name_, table_fds_[table.first], map_type,
table.first->id_->name_, FileDesc(table_fds_[table.first]), map_type,
table.first->key_type_->bit_width_ >> 3, table.first->leaf_type_->bit_width_ >> 3,
table.first->size_, 0,
});
......
......@@ -21,6 +21,8 @@
#include <memory>
#include <string>
#include "common.h"
namespace llvm {
class Function;
}
......@@ -32,41 +34,6 @@ class QualType;
namespace ebpf {
class TableDesc;
/// FileDesc is a helper class for managing open file descriptors. Copy is
/// disallowed (call dup instead), and cleanup happens automatically.
class FileDesc {
friend TableDesc;
private:
FileDesc &operator=(const FileDesc &that) {
fd = ::dup(that.fd);
return *this;
}
FileDesc(const FileDesc &that) { *this = that; }
public:
FileDesc(int fd = -1) : fd(fd) {}
FileDesc &operator=(FileDesc &&that) {
fd = that.fd;
that.fd = -1;
return *this;
}
FileDesc(FileDesc &&that) { *this = std::move(that); }
~FileDesc() {
if (fd >= 0)
::close(fd);
}
FileDesc dup() const { return FileDesc(*this); }
operator int() { return fd; }
operator int() const { return fd; }
private:
int fd;
};
typedef int (*sscanf_fn)(const char *, void *);
typedef int (*snprintf_fn)(char *, size_t, const void *);
......@@ -77,8 +44,22 @@ typedef int (*snprintf_fn)(char *, size_t, const void *);
/// so that objects of this class can reside in stl containers.
class TableDesc {
private:
TableDesc(const TableDesc &) = default;
TableDesc &operator=(const TableDesc &) = default;
TableDesc(const TableDesc &that)
: name(that.name),
fd(that.fd.dup()),
type(that.type),
key_size(that.key_size),
leaf_size(that.leaf_size),
max_entries(that.max_entries),
flags(that.flags),
key_desc(that.key_desc),
leaf_desc(that.leaf_desc),
key_sscanf(that.key_sscanf),
leaf_sscanf(that.leaf_sscanf),
key_snprintf(that.key_snprintf),
leaf_snprintf(that.leaf_snprintf),
is_shared(that.is_shared),
is_extern(that.is_extern) {}
public:
TableDesc()
......@@ -109,7 +90,10 @@ class TableDesc {
is_shared(false),
is_extern(false) {}
TableDesc(TableDesc &&that) = default;
TableDesc &operator=(TableDesc &&that) = default;
TableDesc &operator=(const TableDesc &that) = delete;
TableDesc dup() const { return TableDesc(*this); }
std::string name;
......
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