diff --git a/src/core/bpf_object.cpp b/src/core/bpf_object.cpp index e2340f9..c9fe31f 100644 --- a/src/core/bpf_object.cpp +++ b/src/core/bpf_object.cpp @@ -206,7 +206,7 @@ std::shared_ptr BpfObject::get_map(const std::string& name) { // Create and cache struct bpf_map *raw_map = find_map_by_name(name); - auto map = std::make_shared(this, raw_map, name); + auto map = std::make_shared(shared_from_this(), raw_map, name); maps_cache_[name] = map; return map; @@ -227,7 +227,7 @@ std::shared_ptr BpfObject::_get_or_create_map(struct bpf_map *map) { } // Create and cache - auto bpf_map = std::make_shared(this, map, map_name); + auto bpf_map = std::make_shared(shared_from_this(), map, map_name); maps_cache_[map_name] = bpf_map; return bpf_map; diff --git a/src/core/bpf_object.h b/src/core/bpf_object.h index d6df1bf..a82474d 100644 --- a/src/core/bpf_object.h +++ b/src/core/bpf_object.h @@ -19,7 +19,7 @@ class BpfMap; * This is the main entry point for loading BPF programs. * Owns the bpf_object* and manages all programs and maps within it. */ -class BpfObject { +class BpfObject : public std::enable_shared_from_this { private: struct bpf_object *obj_; std::string object_path_; diff --git a/src/core/bpf_program.h b/src/core/bpf_program.h index 93532da..f55b1c4 100644 --- a/src/core/bpf_program.h +++ b/src/core/bpf_program.h @@ -3,36 +3,35 @@ #include #include +#include #include -namespace py = pybind11; +class BpfObject; class BpfProgram { private: - struct bpf_object *obj_; + std::weak_ptr parent_obj_; struct bpf_program *prog_; struct bpf_link *link_; - std::string object_path_; std::string program_name_; - std::vector > programs; public: - explicit BpfProgram(std::string object_path, std::string program_name = ""); + explicit BpfProgram(std::shared_ptr parent, struct bpf_program *raw_prog, std::string program_name = ""); ~BpfProgram(); - struct bpf_object *get_obj() const; - - bool load(); + BpfProgram(const BpfProgram&) = delete; + BpfProgram& operator=(const BpfProgram&) = delete; + BpfProgram(BpfProgram&&) noexcept; + BpfProgram& operator=(BpfProgram&&) noexcept; bool attach(); - - bool destroy(); + bool detach(); void load_and_attach(); - [[nodiscard]] bool is_loaded() const { return obj_ != nullptr; } [[nodiscard]] bool is_attached() const { return link_ != nullptr; } + [[nodiscard]] std::string get_name() const { return program_name_; } }; #endif //PYLIBBPF_BPF_PROGRAM_H