add initial System module

This commit is contained in:
Josh Holtrop 2016-07-13 21:41:30 -04:00
parent 5bcd55ba97
commit 48e82a11a3
3 changed files with 49 additions and 0 deletions

31
src/core/System.cc Normal file
View File

@ -0,0 +1,31 @@
#include "System.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
long System::page_size;
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))
{
printf("Unsupported page size detected: %ld\n", System::page_size);
return false;
}
return true;
}
void * System::alloc_pages(int num)
{
return mmap(
NULL,
num * System::page_size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS,
-1,
0);
}

12
src/core/System.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef SYSTEM_H
#define SYSTEM_H
class System
{
public:
static bool init();
static long page_size;
static void * alloc_pages(int num);
};
#endif

View File

@ -2,9 +2,15 @@
#include "Window.h"
#include "Runtime.h"
#include <memory>
#include "System.h"
int main(int argc, char * argv[])
{
if (!System::init())
{
return 1;
}
Runtime::init(argv[0], APPNAME);
std::shared_ptr<Buffer> b = std::make_shared<Buffer>();