|
| 1 | +// libcachesim_python - libCacheSim Python bindings |
| 2 | +// Copyright 2025 The libcachesim Authors. All rights reserved. |
| 3 | +// |
| 4 | +// Use of this source code is governed by a GPL-3.0 |
| 5 | +// license that can be found in the LICENSE file or at |
| 6 | +// https://github.com/1a1a11a/libcachesim/blob/develop/LICENSE |
| 7 | + |
| 8 | +#include <pybind11/functional.h> |
| 9 | +#include <pybind11/pybind11.h> |
| 10 | +#include <pybind11/stl.h> |
| 11 | + |
| 12 | +#include "../libCacheSim/include/libCacheSim/admissionAlgo.h" |
| 13 | +#include "export.h" |
| 14 | + |
| 15 | +namespace libcachesim { |
| 16 | + |
| 17 | +namespace py = pybind11; |
| 18 | + |
| 19 | +void export_admissioner(py::module &m) { |
| 20 | + py::class_<admissioner_t>(m, "Admissioner") |
| 21 | + .def(py::init<>()) |
| 22 | + .def_readwrite("params", &admissioner_t::params) |
| 23 | + |
| 24 | + .def_property( |
| 25 | + "admissioner_name", |
| 26 | + [](const admissioner_t &self) { |
| 27 | + return std::string(self.admissioner_name); |
| 28 | + }, |
| 29 | + [](admissioner_t &self, const std::string &val) { |
| 30 | + strncpy(self.admissioner_name, val.c_str(), CACHE_NAME_LEN); |
| 31 | + self.admissioner_name[CACHE_NAME_LEN - 1] = '\0'; |
| 32 | + }) |
| 33 | + |
| 34 | + .def_property( |
| 35 | + "init_params", |
| 36 | + [](const admissioner_t &self) { |
| 37 | + return self.init_params ? std::string(self.init_params) |
| 38 | + : std::string{}; |
| 39 | + }, |
| 40 | + [](admissioner_t &self, const std::string &val) { |
| 41 | + if (self.init_params) free(self.init_params); |
| 42 | + self.init_params = strdup(val.c_str()); |
| 43 | + }) |
| 44 | + |
| 45 | + .def("admit", |
| 46 | + [](admissioner_t &self, uintptr_t req_ptr) { |
| 47 | + if (!self.admit) |
| 48 | + throw std::runtime_error("admit function pointer is NULL"); |
| 49 | + request_t *req = reinterpret_cast<request_t *>(req_ptr); |
| 50 | + return self.admit(&self, req); |
| 51 | + }) |
| 52 | + |
| 53 | + .def("clone", |
| 54 | + [](admissioner_t &self) { |
| 55 | + if (!self.clone) |
| 56 | + throw std::runtime_error("clone function pointer is NULL"); |
| 57 | + return self.clone(&self); |
| 58 | + }) |
| 59 | + |
| 60 | + .def("update", |
| 61 | + [](admissioner_t &self, uintptr_t req_ptr, uint64_t cache_size) { |
| 62 | + if (!self.update) |
| 63 | + throw std::runtime_error("update function pointer is NULL"); |
| 64 | + request_t *req = reinterpret_cast<request_t *>(req_ptr); |
| 65 | + self.update(&self, req, cache_size); |
| 66 | + }) |
| 67 | + |
| 68 | + .def("free", [](admissioner_t &self) { |
| 69 | + if (!self.free) |
| 70 | + throw std::runtime_error("free function pointer is NULL"); |
| 71 | + self.free(&self); |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +} // namespace libcachesim |
0 commit comments