Add other useful page-size-related fields to System module

This commit is contained in:
Josh Holtrop 2016-07-14 17:51:53 -04:00
parent 48e82a11a3
commit ef6abd8ed7
2 changed files with 36 additions and 7 deletions

View File

@ -2,17 +2,42 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdint.h>
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;
}

View File

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