|
| 1 | +#ifndef S3CPP_BENCHMARK_IMPLEMENTATIONS_H |
| 2 | +#define S3CPP_BENCHMARK_IMPLEMENTATIONS_H |
| 3 | + |
| 4 | +#include "tasks.h" |
| 5 | +#include <cstddef> |
| 6 | +#include <cstdio> |
| 7 | +#include <dlfcn.h> |
| 8 | +#include <iterator> |
| 9 | +#include <print> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +namespace s3b { |
| 13 | + |
| 14 | +#define SO_SYMBOLS "init", "get_object" |
| 15 | + |
| 16 | +constexpr const char *implementations_so[] = { |
| 17 | +#include "build/implementations.inc" |
| 18 | +}; |
| 19 | + |
| 20 | +constexpr std::size_t implementations_count = std::size(implementations_so); |
| 21 | + |
| 22 | +struct implementation { |
| 23 | + const char *name; |
| 24 | + init_f *init; |
| 25 | + get_object_f *get_object; |
| 26 | +}; |
| 27 | + |
| 28 | +inline implementation parse_implementation(const char *so_path) { |
| 29 | + void *so_handle = dlopen(so_path, RTLD_LAZY); |
| 30 | + if (so_handle == NULL) { |
| 31 | + std::println(stderr, "fatal: unable to get handle {}", so_path); |
| 32 | + std::exit(1); |
| 33 | + } |
| 34 | + |
| 35 | + std::string symbol_name = "init"; |
| 36 | + init_f *init = reinterpret_cast<init_f *>(dlsym(so_handle, symbol_name.c_str())); |
| 37 | + if (init == NULL) { |
| 38 | + std::println(stderr, "fatal: {} for {}", ::dlerror(), so_path); |
| 39 | + std::exit(1); |
| 40 | + } |
| 41 | + |
| 42 | + symbol_name = "get_object"; |
| 43 | + get_object_f *get_object = |
| 44 | + reinterpret_cast<get_object_f *>(dlsym(so_handle, symbol_name.c_str())); |
| 45 | + if (get_object == NULL) { |
| 46 | + std::println(stderr, "fatal: {} for {}", ::dlerror(), so_path); |
| 47 | + std::exit(1); |
| 48 | + } |
| 49 | + return implementation{so_path, init, get_object}; |
| 50 | +} |
| 51 | + |
| 52 | +inline std::array<implementation, implementations_count> |
| 53 | +parse_implementations() { |
| 54 | + std::array<implementation, implementations_count> implementations{}; |
| 55 | + for (size_t i = 0; i < implementations_count; i++) { |
| 56 | + implementations[i] = parse_implementation(implementations_so[i]); |
| 57 | + } |
| 58 | + return implementations; |
| 59 | +} |
| 60 | + |
| 61 | +} // namespace s3b |
| 62 | + |
| 63 | +#endif |
0 commit comments