Skip to content

Commit 0ff840e

Browse files
committed
export admissioner struct
1 parent 1cce5b2 commit 0ff840e

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ include_directories(${MAIN_PROJECT_SOURCE_DIR}/libCacheSim/bin)
252252
set(PYTHON_MODULE_SOURCES
253253
src/export.cpp
254254
src/export_cache.cpp
255+
src/export_admissioner.cpp
255256
src/export_reader.cpp
256257
src/export_analyzer.cpp
257258
src/export_misc.cpp

libcachesim/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .libcachesim_python import (
66
Cache,
77
Request,
8+
Admissioner,
89
ReqOp,
910
ReaderInitParam,
1011
TraceType,

src/export.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ PYBIND11_MODULE(libcachesim_python, m) {
2121
// methods if the codebase is large enough
2222

2323
export_cache(m);
24+
export_admissioner(m);
2425
export_reader(m);
2526
export_analyzer(m);
2627
export_misc(m);

src/export.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ using py::literals::operator""_a;
2020
void export_cache(py::module &m);
2121
void export_pyplugin_cache(py::module &m);
2222

23+
void export_admissioner(py::module &m);
2324
void export_reader(py::module &m);
2425
void export_analyzer(py::module &m);
2526
void export_misc(py::module &m);

src/export_admissioner.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)