Skip to content

Commit 2b99f01

Browse files
committed
Rework BpfProgram.h, pass BpfObject as shared_ptr to BpfPrograms
1 parent 763c188 commit 2b99f01

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

src/core/bpf_object.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ std::shared_ptr<BpfMap> BpfObject::get_map(const std::string& name) {
206206

207207
// Create and cache
208208
struct bpf_map *raw_map = find_map_by_name(name);
209-
auto map = std::make_shared<BpfMap>(this, raw_map, name);
209+
auto map = std::make_shared<BpfMap>(shared_from_this(), raw_map, name);
210210
maps_cache_[name] = map;
211211

212212
return map;
@@ -227,7 +227,7 @@ std::shared_ptr<BpfMap> BpfObject::_get_or_create_map(struct bpf_map *map) {
227227
}
228228

229229
// Create and cache
230-
auto bpf_map = std::make_shared<BpfMap>(this, map, map_name);
230+
auto bpf_map = std::make_shared<BpfMap>(shared_from_this(), map, map_name);
231231
maps_cache_[map_name] = bpf_map;
232232

233233
return bpf_map;

src/core/bpf_object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class BpfMap;
1919
* This is the main entry point for loading BPF programs.
2020
* Owns the bpf_object* and manages all programs and maps within it.
2121
*/
22-
class BpfObject {
22+
class BpfObject : public std::enable_shared_from_this<BpfObject> {
2323
private:
2424
struct bpf_object *obj_;
2525
std::string object_path_;

src/core/bpf_program.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,35 @@
33

44
#include <libbpf.h>
55
#include <pybind11/stl.h>
6+
#include <memory>
67
#include <string>
78

8-
namespace py = pybind11;
9+
class BpfObject;
910

1011
class BpfProgram {
1112
private:
12-
struct bpf_object *obj_;
13+
std::weak_ptr<BpfObject> parent_obj_;
1314
struct bpf_program *prog_;
1415
struct bpf_link *link_;
15-
std::string object_path_;
1616
std::string program_name_;
17-
std::vector<std::pair<bpf_program *, bpf_link *> > programs;
1817

1918
public:
20-
explicit BpfProgram(std::string object_path, std::string program_name = "");
19+
explicit BpfProgram(std::shared_ptr<BpfObject> parent, struct bpf_program *raw_prog, std::string program_name = "");
2120

2221
~BpfProgram();
2322

24-
struct bpf_object *get_obj() const;
25-
26-
bool load();
23+
BpfProgram(const BpfProgram&) = delete;
24+
BpfProgram& operator=(const BpfProgram&) = delete;
25+
BpfProgram(BpfProgram&&) noexcept;
26+
BpfProgram& operator=(BpfProgram&&) noexcept;
2727

2828
bool attach();
29-
30-
bool destroy();
29+
bool detach();
3130

3231
void load_and_attach();
3332

34-
[[nodiscard]] bool is_loaded() const { return obj_ != nullptr; }
3533
[[nodiscard]] bool is_attached() const { return link_ != nullptr; }
34+
[[nodiscard]] std::string get_name() const { return program_name_; }
3635
};
3736

3837
#endif //PYLIBBPF_BPF_PROGRAM_H

0 commit comments

Comments
 (0)