diff --git a/src/large_pages/node_large_page.cc b/src/large_pages/node_large_page.cc index e72c55310f2db3..789fbc6b93e1f1 100644 --- a/src/large_pages/node_large_page.cc +++ b/src/large_pages/node_large_page.cc @@ -257,24 +257,30 @@ struct text_region FindNodeTextRegion() { } #if defined(__linux__) -bool IsTransparentHugePagesEnabled() { - std::ifstream ifs; +std::istream *GetTransparentHugePagesConfiguration() { + std::ifstream *ifs = new std::ifstream(); - ifs.open("/sys/kernel/mm/transparent_hugepage/enabled"); - if (!ifs) { + ifs->open("/sys/kernel/mm/transparent_hugepage/enabled"); + if (!*ifs) { PrintWarning("could not open /sys/kernel/mm/transparent_hugepage/enabled"); + return nullptr; + } + + return ifs; +} + +bool IsTransparentHugePagesEnabled() { + std::istream *configuration = GetTransparentHugePagesConfiguration(); + if (configuration == nullptr) { return false; } bool enabled = false; - if (ifs.is_open()) { - std::string token; - while (ifs >> token) { - enabled = enabled || token == "[always]" || token == "[madvise]"; - } + std::string token; + while (*configuration >> token) { + enabled = enabled || token == "[always]" || token == "[madvise]"; } - ifs.close(); - + delete configuration; return enabled; } #elif defined(__FreeBSD__)