From 54acc2c15dac6ec62f738db7e146fe1570f8e7ce Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Sat, 18 Oct 2025 14:51:11 +0530 Subject: [PATCH] Redesign BpfMap --- src/core/bpf_map.h | 36 +++++++++++++----------------------- src/core/bpf_program.h | 1 - 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/core/bpf_map.h b/src/core/bpf_map.h index e6ad13a..550e25d 100644 --- a/src/core/bpf_map.h +++ b/src/core/bpf_map.h @@ -1,54 +1,44 @@ -#ifndef PYLIBBPF_MAPS_H -#define PYLIBBPF_MAPS_H +#ifndef PYLIBBPF_BPF_MAP_H +#define PYLIBBPF_BPF_MAP_H #include #include #include #include -#include "bpf_program.h" +class BpfObject; namespace py = pybind11; class BpfMap { private: + std::weak_ptr parent_obj_; struct bpf_map *map_; - int map_fd = -1; - //TODO: turn below into a shared pointer and ref count it so that there is no resource leakage - BpfProgram *bpf_program; + int map_fd_; + std::string map_name_; public: - BpfMap(BpfProgram *program_, const py::object &map_from_python); + BpfMap(std::shared_ptr, struct bpf_map *raw_map, const std::string &map_name); ~BpfMap() = default; [[nodiscard]] py::object lookup(const py::object &key) const; - void update(const py::object &key, const py::object &value) const; - void delete_elem(const py::object &key) const; - py::list get_next_key(const py::object &key = py::none()) const; - py::dict items() const; - py::list keys() const; - py::list values() const; - [[nodiscard]] std::string get_name() const; - - int get_type() const; - - int get_key_size() const; - - int get_value_size() const; - - int get_max_entries() const; + [[nodiscard]] std::string get_name() const { return map_name_; } + [[nodiscard]] int get_fd() const { return map_fd_; } + [[nodiscard]] int get_type() const; + [[nodiscard]] int get_key_size() const; + [[nodiscard]] int get_value_size() const; + [[nodiscard]] int get_max_entries() const; private: static std::vector python_to_bytes(const py::object &obj, size_t size); - static py::object bytes_to_python(const std::vector &data); }; diff --git a/src/core/bpf_program.h b/src/core/bpf_program.h index fdbea4d..dd0dce4 100644 --- a/src/core/bpf_program.h +++ b/src/core/bpf_program.h @@ -2,7 +2,6 @@ #define PYLIBBPF_BPF_PROGRAM_H #include -#include #include #include