|
1 | 1 | #ifndef PYLIBBPF_BPF_MAP_H |
2 | 2 | #define PYLIBBPF_BPF_MAP_H |
3 | 3 |
|
| 4 | +#include <algorithm> |
| 5 | +#include <array> |
| 6 | +#include <cerrno> |
| 7 | +#include <cstring> |
4 | 8 | #include <libbpf.h> |
5 | 9 | #include <pybind11/pybind11.h> |
6 | | -#include <vector> |
7 | | -#include <string> |
8 | 10 | #include <span> |
9 | | -#include <array> |
10 | | -#include <algorithm> |
11 | | -#include <cstring> |
12 | | -#include <cerrno> |
| 11 | +#include <string> |
| 12 | +#include <vector> |
13 | 13 |
|
14 | 14 | class BpfObject; |
15 | 15 |
|
16 | 16 | namespace py = pybind11; |
17 | 17 |
|
18 | 18 | class BpfMap { |
19 | 19 | private: |
20 | | - std::weak_ptr<BpfObject> parent_obj_; |
21 | | - struct bpf_map *map_; |
22 | | - int map_fd_; |
23 | | - std::string map_name_; |
24 | | - __u32 key_size_, value_size_; |
| 20 | + std::weak_ptr<BpfObject> parent_obj_; |
| 21 | + struct bpf_map *map_; |
| 22 | + int map_fd_; |
| 23 | + std::string map_name_; |
| 24 | + __u32 key_size_, value_size_; |
| 25 | + |
| 26 | + template <size_t StackSize = 64> struct BufferManager { |
| 27 | + std::array<uint8_t, StackSize> stack_buf; |
| 28 | + std::vector<uint8_t> heap_buf; |
25 | 29 |
|
26 | | - template<size_t StackSize = 64> |
27 | | - struct BufferManager { |
28 | | - std::array<uint8_t, StackSize> stack_buf; |
29 | | - std::vector<uint8_t> heap_buf; |
30 | | - |
31 | | - std::span<uint8_t> get_span(size_t size) { |
32 | | - if (size <= StackSize) { |
33 | | - return std::span<uint8_t>(stack_buf.data(), size); |
34 | | - } else { |
35 | | - heap_buf.resize(size); |
36 | | - return std::span<uint8_t>(heap_buf); |
37 | | - } |
38 | | - } |
39 | | - }; |
| 30 | + std::span<uint8_t> get_span(size_t size) { |
| 31 | + if (size <= StackSize) { |
| 32 | + return std::span<uint8_t>(stack_buf.data(), size); |
| 33 | + } else { |
| 34 | + heap_buf.resize(size); |
| 35 | + return std::span<uint8_t>(heap_buf); |
| 36 | + } |
| 37 | + } |
| 38 | + }; |
40 | 39 |
|
41 | 40 | public: |
42 | | - BpfMap(std::shared_ptr<BpfObject> parent, struct bpf_map *raw_map, const std::string &map_name); |
| 41 | + BpfMap(std::shared_ptr<BpfObject> parent, struct bpf_map *raw_map, |
| 42 | + const std::string &map_name); |
43 | 43 |
|
44 | | - ~BpfMap() = default; |
| 44 | + ~BpfMap() = default; |
45 | 45 |
|
46 | | - BpfMap(const BpfMap&) = delete; |
47 | | - BpfMap& operator=(const BpfMap&) = delete; |
48 | | - BpfMap(BpfMap&&) noexcept = default; |
49 | | - BpfMap& operator=(BpfMap&&) noexcept = default; |
| 46 | + BpfMap(const BpfMap &) = delete; |
| 47 | + BpfMap &operator=(const BpfMap &) = delete; |
| 48 | + BpfMap(BpfMap &&) noexcept = default; |
| 49 | + BpfMap &operator=(BpfMap &&) noexcept = default; |
50 | 50 |
|
51 | | - [[nodiscard]] py::object lookup(const py::object &key) const; |
52 | | - void update(const py::object &key, const py::object &value) const; |
53 | | - void delete_elem(const py::object &key) const; |
54 | | - py::object get_next_key(const py::object &key = py::none()) const; |
55 | | - py::dict items() const; |
56 | | - py::list keys() const; |
57 | | - py::list values() const; |
| 51 | + [[nodiscard]] py::object lookup(const py::object &key) const; |
| 52 | + void update(const py::object &key, const py::object &value) const; |
| 53 | + void delete_elem(const py::object &key) const; |
| 54 | + py::object get_next_key(const py::object &key = py::none()) const; |
| 55 | + py::dict items() const; |
| 56 | + py::list keys() const; |
| 57 | + py::list values() const; |
58 | 58 |
|
59 | | - [[nodiscard]] std::string get_name() const { return map_name_; } |
60 | | - [[nodiscard]] int get_fd() const { return map_fd_; } |
61 | | - [[nodiscard]] int get_type() const; |
62 | | - [[nodiscard]] int get_key_size() const { return key_size_; }; |
63 | | - [[nodiscard]] int get_value_size() const { return value_size_; }; |
64 | | - [[nodiscard]] int get_max_entries() const; |
| 59 | + [[nodiscard]] std::string get_name() const { return map_name_; } |
| 60 | + [[nodiscard]] int get_fd() const { return map_fd_; } |
| 61 | + [[nodiscard]] int get_type() const; |
| 62 | + [[nodiscard]] int get_key_size() const { return key_size_; }; |
| 63 | + [[nodiscard]] int get_value_size() const { return value_size_; }; |
| 64 | + [[nodiscard]] int get_max_entries() const; |
65 | 65 |
|
66 | 66 | private: |
67 | | - static void python_to_bytes_inplace(const py::object &obj, std::span<uint8_t> buffer); |
68 | | - static py::object bytes_to_python(std::span<const uint8_t> data); |
| 67 | + static void python_to_bytes_inplace(const py::object &obj, |
| 68 | + std::span<uint8_t> buffer); |
| 69 | + static py::object bytes_to_python(std::span<const uint8_t> data); |
69 | 70 | }; |
70 | 71 |
|
71 | | -#endif //PYLIBBPF_MAPS_H |
| 72 | +#endif // PYLIBBPF_MAPS_H |
0 commit comments