Skip to content

Commit b6b9c6d

Browse files
committed
Move FileDesc to common.h
1 parent a09c491 commit b6b9c6d

5 files changed

Lines changed: 84 additions & 42 deletions

File tree

‎src/cc/bpf_module.cc‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ unique_ptr<ExecutionEngine> BPFModule::finalize_rw(unique_ptr<Module> m) {
329329

330330
// load an entire c file as a module
331331
int BPFModule::load_cfile(const string &file, bool in_memory, const char *cflags[], int ncflags) {
332-
clang_loader_ = make_unique<ClangLoader>(&*ctx_, flags_);
332+
clang_loader_ = ebpf::make_unique<ClangLoader>(&*ctx_, flags_);
333333
if (clang_loader_->parse(&mod_, *ts_, file, in_memory, cflags, ncflags, id_))
334334
return -1;
335335
return 0;
@@ -341,7 +341,7 @@ int BPFModule::load_cfile(const string &file, bool in_memory, const char *cflags
341341
// Load in a pre-built list of functions into the initial Module object, then
342342
// build an ExecutionEngine.
343343
int BPFModule::load_includes(const string &text) {
344-
clang_loader_ = make_unique<ClangLoader>(&*ctx_, flags_);
344+
clang_loader_ = ebpf::make_unique<ClangLoader>(&*ctx_, flags_);
345345
if (clang_loader_->parse(&mod_, *ts_, text, true, nullptr, 0, ""))
346346
return -1;
347347
return 0;
@@ -353,7 +353,7 @@ int BPFModule::annotate() {
353353
fn->addFnAttr(Attribute::AlwaysInline);
354354

355355
// separate module to hold the reader functions
356-
auto m = make_unique<Module>("sscanf", *ctx_);
356+
auto m = ebpf::make_unique<Module>("sscanf", *ctx_);
357357

358358
struct llvmfnpointers {
359359
llvm::Function *key_sscanf;
@@ -461,7 +461,7 @@ int BPFModule::finalize() {
461461
string err;
462462
EngineBuilder builder(move(mod_));
463463
builder.setErrorStr(&err);
464-
builder.setMCJITMemoryManager(make_unique<MyMemoryManager>(&sections_));
464+
builder.setMCJITMemoryManager(ebpf::make_unique<MyMemoryManager>(&sections_));
465465
builder.setMArch("bpf");
466466
builder.setUseOrcMCJITReplacement(true);
467467
engine_ = unique_ptr<ExecutionEngine>(builder.create());

‎src/cc/common.cc‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
#include <fstream>
1717
#include <sstream>
18+
#include <unistd.h>
19+
1820
#include "common.h"
1921

2022
namespace ebpf {
@@ -47,5 +49,37 @@ std::vector<int> get_possible_cpus() {
4749
return read_cpu_range("/sys/devices/system/cpu/possible");
4850
}
4951

52+
FileDesc::FileDesc(int fd) : fd_(fd) {}
53+
54+
FileDesc::FileDesc(FileDesc &&that) : fd_(-1) { *this = std::move(that); }
55+
56+
FileDesc::~FileDesc() {
57+
if (fd_ >= 0)
58+
::close(fd_);
59+
}
60+
61+
FileDesc &FileDesc::operator=(int fd) {
62+
if (fd_ >= 0)
63+
::close(fd_);
64+
fd_ = fd;
65+
return *this;
66+
}
67+
68+
FileDesc &FileDesc::operator=(FileDesc &&that) {
69+
if (fd_ >= 0)
70+
::close(fd_);
71+
fd_ = that.fd_;
72+
that.fd_ = -1;
73+
return *this;
74+
}
75+
76+
FileDesc FileDesc::dup() const {
77+
int dup_fd = ::dup(fd_);
78+
return FileDesc(dup_fd);
79+
}
80+
81+
FileDesc::operator int() { return fd_; }
82+
83+
FileDesc::operator int() const { return fd_; }
5084

5185
} // namespace ebpf

‎src/cc/common.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,27 @@ std::vector<int> get_online_cpus();
3333

3434
std::vector<int> get_possible_cpus();
3535

36+
/// FileDesc is a helper class for managing open file descriptors. Copy is
37+
/// disallowed (call dup instead), and cleanup happens automatically.
38+
class FileDesc {
39+
public:
40+
explicit FileDesc(int fd = -1);
41+
FileDesc(FileDesc &&that);
42+
FileDesc(const FileDesc &that) = delete;
43+
44+
~FileDesc();
45+
46+
FileDesc &operator=(int fd);
47+
FileDesc &operator=(FileDesc &&that);
48+
FileDesc &operator=(const FileDesc &that) = delete;
49+
50+
operator int();
51+
operator int() const;
52+
53+
FileDesc dup() const;
54+
55+
private:
56+
int fd_;
57+
};
58+
3659
} // namespace ebpf

‎src/cc/frontends/b/codegen_llvm.cc‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <llvm/IR/Module.h>
3434

3535
#include "bcc_exception.h"
36+
#include "common.h"
3637
#include "codegen_llvm.h"
3738
#include "lexer.h"
3839
#include "libbpf.h"
@@ -1244,7 +1245,7 @@ StatusTuple CodegenLLVM::visit(Node *root, TableStorage &ts, const string &id) {
12441245
map_type = BPF_MAP_TYPE_ARRAY;
12451246
ts.Insert(Path({id, table.first->id_->name_}),
12461247
{
1247-
table.first->id_->name_, table_fds_[table.first], map_type,
1248+
table.first->id_->name_, FileDesc(table_fds_[table.first]), map_type,
12481249
table.first->key_type_->bit_width_ >> 3, table.first->leaf_type_->bit_width_ >> 3,
12491250
table.first->size_, 0,
12501251
});

‎src/cc/table_desc.h‎

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <memory>
2222
#include <string>
2323

24+
#include "common.h"
25+
2426
namespace llvm {
2527
class Function;
2628
}
@@ -32,41 +34,6 @@ class QualType;
3234

3335
namespace ebpf {
3436

35-
class TableDesc;
36-
37-
/// FileDesc is a helper class for managing open file descriptors. Copy is
38-
/// disallowed (call dup instead), and cleanup happens automatically.
39-
class FileDesc {
40-
friend TableDesc;
41-
42-
private:
43-
FileDesc &operator=(const FileDesc &that) {
44-
fd = ::dup(that.fd);
45-
return *this;
46-
}
47-
FileDesc(const FileDesc &that) { *this = that; }
48-
49-
public:
50-
FileDesc(int fd = -1) : fd(fd) {}
51-
FileDesc &operator=(FileDesc &&that) {
52-
fd = that.fd;
53-
that.fd = -1;
54-
return *this;
55-
}
56-
FileDesc(FileDesc &&that) { *this = std::move(that); }
57-
~FileDesc() {
58-
if (fd >= 0)
59-
::close(fd);
60-
}
61-
FileDesc dup() const { return FileDesc(*this); }
62-
63-
operator int() { return fd; }
64-
operator int() const { return fd; }
65-
66-
private:
67-
int fd;
68-
};
69-
7037
typedef int (*sscanf_fn)(const char *, void *);
7138
typedef int (*snprintf_fn)(char *, size_t, const void *);
7239

@@ -77,8 +44,22 @@ typedef int (*snprintf_fn)(char *, size_t, const void *);
7744
/// so that objects of this class can reside in stl containers.
7845
class TableDesc {
7946
private:
80-
TableDesc(const TableDesc &) = default;
81-
TableDesc &operator=(const TableDesc &) = default;
47+
TableDesc(const TableDesc &that)
48+
: name(that.name),
49+
fd(that.fd.dup()),
50+
type(that.type),
51+
key_size(that.key_size),
52+
leaf_size(that.leaf_size),
53+
max_entries(that.max_entries),
54+
flags(that.flags),
55+
key_desc(that.key_desc),
56+
leaf_desc(that.leaf_desc),
57+
key_sscanf(that.key_sscanf),
58+
leaf_sscanf(that.leaf_sscanf),
59+
key_snprintf(that.key_snprintf),
60+
leaf_snprintf(that.leaf_snprintf),
61+
is_shared(that.is_shared),
62+
is_extern(that.is_extern) {}
8263

8364
public:
8465
TableDesc()
@@ -109,7 +90,10 @@ class TableDesc {
10990
is_shared(false),
11091
is_extern(false) {}
11192
TableDesc(TableDesc &&that) = default;
93+
11294
TableDesc &operator=(TableDesc &&that) = default;
95+
TableDesc &operator=(const TableDesc &that) = delete;
96+
11397
TableDesc dup() const { return TableDesc(*this); }
11498

11599
std::string name;

0 commit comments

Comments
 (0)