Skip to content

Commit 54acc2c

Browse files
committed
Redesign BpfMap
1 parent 5c74be0 commit 54acc2c

File tree

2 files changed

+13
-24
lines changed

2 files changed

+13
-24
lines changed

src/core/bpf_map.h

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,44 @@
1-
#ifndef PYLIBBPF_MAPS_H
2-
#define PYLIBBPF_MAPS_H
1+
#ifndef PYLIBBPF_BPF_MAP_H
2+
#define PYLIBBPF_BPF_MAP_H
33

44
#include <libbpf.h>
55
#include <pybind11/pybind11.h>
66
#include <vector>
77
#include <string>
88

9-
#include "bpf_program.h"
9+
class BpfObject;
1010

1111
namespace py = pybind11;
1212

1313
class BpfMap {
1414
private:
15+
std::weak_ptr<BpfObject> parent_obj_;
1516
struct bpf_map *map_;
16-
int map_fd = -1;
17-
//TODO: turn below into a shared pointer and ref count it so that there is no resource leakage
18-
BpfProgram *bpf_program;
17+
int map_fd_;
18+
std::string map_name_;
1919

2020
public:
21-
BpfMap(BpfProgram *program_, const py::object &map_from_python);
21+
BpfMap(std::shared_ptr<BpfObject>, struct bpf_map *raw_map, const std::string &map_name);
2222

2323
~BpfMap() = default;
2424

2525
[[nodiscard]] py::object lookup(const py::object &key) const;
26-
2726
void update(const py::object &key, const py::object &value) const;
28-
2927
void delete_elem(const py::object &key) const;
30-
3128
py::list get_next_key(const py::object &key = py::none()) const;
32-
3329
py::dict items() const;
34-
3530
py::list keys() const;
36-
3731
py::list values() const;
3832

39-
[[nodiscard]] std::string get_name() const;
40-
41-
int get_type() const;
42-
43-
int get_key_size() const;
44-
45-
int get_value_size() const;
46-
47-
int get_max_entries() const;
33+
[[nodiscard]] std::string get_name() const { return map_name_; }
34+
[[nodiscard]] int get_fd() const { return map_fd_; }
35+
[[nodiscard]] int get_type() const;
36+
[[nodiscard]] int get_key_size() const;
37+
[[nodiscard]] int get_value_size() const;
38+
[[nodiscard]] int get_max_entries() const;
4839

4940
private:
5041
static std::vector<uint8_t> python_to_bytes(const py::object &obj, size_t size);
51-
5242
static py::object bytes_to_python(const std::vector<uint8_t> &data);
5343
};
5444

src/core/bpf_program.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define PYLIBBPF_BPF_PROGRAM_H
33

44
#include <libbpf.h>
5-
#include <pybind11/stl.h>
65
#include <memory>
76
#include <string>
87

0 commit comments

Comments
 (0)