add: [system] (TBD) sys::conf page_size

This commit is contained in:
mutouyun 2022-08-14 21:55:44 +08:00
parent 64a3ba24e7
commit c604fadbea
3 changed files with 34 additions and 0 deletions

View File

@ -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_

View File

@ -6,6 +6,7 @@
#include <errno.h>
#include <string.h>
#include <unistd.h>
#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_

View File

@ -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);
}