Rename BpfPerfBuffer to PerfEventArray, add struct_parser to BpfObject as a shared_ptr

This commit is contained in:
Pragyansh Chaturvedi
2025-10-19 22:34:10 +05:30
parent 05d5bba4f7
commit cbfe6ae95e
7 changed files with 80 additions and 22 deletions

View File

@ -2,11 +2,12 @@
#include "bpf_exception.h"
#include "bpf_map.h"
#include "bpf_program.h"
#include "utils/struct_parser.h"
#include <cerrno>
BpfObject::BpfObject(std::string object_path, py::dict structs)
: obj_(nullptr), object_path_(std::move(object_path)), loaded_(false),
struct_defs_(structs) {}
struct_defs_(structs), struct_parser_(nullptr) {}
BpfObject::~BpfObject() {
// Clear caches first (order matters!)
@ -256,3 +257,11 @@ py::dict BpfObject::get_cached_maps() const {
}
return maps;
}
std::shared_ptr<StructParser> BpfObject::get_struct_parser() const {
if (!struct_parser_ && !struct_defs_.empty()) {
// Create parser on first access
struct_parser_ = std::make_shared<StructParser>(struct_defs_);
}
return struct_parser_;
}

View File

@ -12,6 +12,7 @@ namespace py = pybind11;
class BpfProgram;
class BpfMap;
class StructParser;
/**
* BpfObject - Represents a loaded BPF object file.
@ -29,6 +30,7 @@ private:
mutable std::unordered_map<std::string, std::shared_ptr<BpfProgram>>
prog_cache_;
py::dict struct_defs_;
mutable std::shared_ptr<StructParser> struct_parser_;
std::shared_ptr<BpfProgram> _get_or_create_program(struct bpf_program *prog);
std::shared_ptr<BpfMap> _get_or_create_map(struct bpf_map *map);
@ -78,6 +80,10 @@ public:
[[nodiscard]] std::shared_ptr<BpfMap> get_map(const std::string &name);
[[nodiscard]] struct bpf_map *find_map_by_name(const std::string &name) const;
[[nodiscard]] py::dict get_cached_maps() const;
// Struct parsing
[[nodiscard]] py::dict get_struct_defs() const { return struct_defs_; }
[[nodiscard]] std::shared_ptr<StructParser> get_struct_parser() const;
};
#endif // PYLIBBPF_BPF_OBJECT_H