diff --git a/src/core/System.cc b/src/core/System.cc index a4e6660..c653b7c 100644 --- a/src/core/System.cc +++ b/src/core/System.cc @@ -2,17 +2,42 @@ #include #include #include +#include -long System::page_size; +unsigned long System::page_size; +unsigned int System::page_size_log; +unsigned long System::page_base_mask; +unsigned long System::page_offset_mask; bool System::init() { - System::page_size = sysconf(_SC_PAGESIZE); - if ((System::page_size == 0) || - (System::page_size > (2 * 1024 * 1024)) || - ((System::page_size & (System::page_size - 1)) != 0)) + long sc_page_size = sysconf(_SC_PAGESIZE); + if (sc_page_size < 0) { - printf("Unsupported page size detected: %ld\n", System::page_size); + fprintf(stderr, "Unable to determine operating system page size.\n"); + return false; + } + + System::page_size = (unsigned long)sc_page_size; + System::page_size_log = 0u; + System::page_offset_mask = System::page_size - 1u; + System::page_base_mask = ~System::page_offset_mask; + + for (uint_fast8_t i = 0u; i < 32u; i++) + { + unsigned long v = 1u << i; + if (v == System::page_size) + { + System::page_size_log = i; + break; + } + } + + if ((System::page_size < 256u) || + (System::page_size > (4u * 1024u * 1024u)) || + (System::page_size_log == 0u)) + { + fprintf(stderr, "Unsupported page size detected: 0x%lx\n", System::page_size); return false; } diff --git a/src/core/System.h b/src/core/System.h index b0727b8..a37aeb2 100644 --- a/src/core/System.h +++ b/src/core/System.h @@ -5,8 +5,12 @@ class System { public: static bool init(); - static long page_size; static void * alloc_pages(int num); + + static unsigned long page_size; + static unsigned int page_size_log; + static unsigned long page_base_mask; + static unsigned long page_offset_mask; }; #endif