diff --git a/include/libimp/system.h b/include/libimp/system.h index 7264682..ba542d5 100644 --- a/include/libimp/system.h +++ b/include/libimp/system.h @@ -31,5 +31,13 @@ LIBIMP_EXPORT std::string error_str(result_code) noexcept; */ LIBIMP_EXPORT std::string error_msg(result_code) noexcept; +/** + * @brief Get system configuration information at run time + */ +enum class info : std::int32_t { + page_size, +}; +LIBIMP_EXPORT std::int64_t conf(info) noexcept; + } // namespace sys LIBIMP_NAMESPACE_END_ diff --git a/src/libimp/platform/posix/system.h b/src/libimp/platform/posix/system.h index b05d76e..44d917a 100644 --- a/src/libimp/platform/posix/system.h +++ b/src/libimp/platform/posix/system.h @@ -6,6 +6,7 @@ #include #include +#include #include "libimp/system.h" #include "libimp/log.h" @@ -50,5 +51,26 @@ std::string error_str(result_code code) noexcept { #endif } +/** + * @brief Gets configuration information at run time + * https://man7.org/linux/man-pages/man2/getpagesize.2.html + * https://man7.org/linux/man-pages/man3/sysconf.3.html + */ +std::int64_t conf(info r) noexcept { + LIBIMP_LOG_(); + switch (r) { + case info::page_size: { + auto val = ::sysconf(_SC_PAGESIZE); + if (val >= 0) return (std::int64_t)val; + } + break; + default: + log.error("invalid info = {}", enum_cast(r)); + return -1; + } + log.error("info = {}, error = {}", enum_cast(r), error_msg(error_code())); + return -1; +} + } // namespace sys LIBIMP_NAMESPACE_END_ diff --git a/test/test_imp_system.cpp b/test/test_imp_system.cpp index 73c7568..7b85fc4 100644 --- a/test/test_imp_system.cpp +++ b/test/test_imp_system.cpp @@ -56,4 +56,8 @@ TEST(system, error_str) { EXPECT_EQ(imp::sys::error_str({}), "Success"); EXPECT_EQ(imp::sys::error_str({false, EINVAL}), "Invalid argument"); #endif +} + +TEST(system, conf) { + EXPECT_EQ(imp::sys::conf(imp::sys::info::page_size), 4096); } \ No newline at end of file