74 lines
1.6 KiB
C++
74 lines
1.6 KiB
C++
#ifdef __linux__
|
|
|
|
#include "SystemCalls.h"
|
|
|
|
#include <sys/sysinfo.h>
|
|
#include <unistd.h>
|
|
#include <dlfcn.h>
|
|
|
|
#include <exception>
|
|
#include <stdexcept>
|
|
|
|
int System::getCountCPU()
|
|
{
|
|
int num_cores = sysconf(_SC_NPROCESSORS_ONLN);
|
|
if(num_cores <= 0)
|
|
throw std::runtime_error("Fail get number of cores");
|
|
|
|
return num_cores;
|
|
}
|
|
|
|
unsigned long long System::getMemorySize()
|
|
{
|
|
struct sysinfo info{};
|
|
if(sysinfo(&info) == -1)
|
|
std::runtime_error("Fail get system info");
|
|
return info.totalram;
|
|
}
|
|
|
|
void* System::InitModule(const char* ModuleName, CoreInstance& core)
|
|
{
|
|
std::string fileName = "./lib";
|
|
fileName += ModuleName;
|
|
fileName += ".so";
|
|
|
|
void* handler = dlopen(fileName.c_str(), RTLD_NOW);
|
|
if(handler == nullptr)
|
|
throw std::runtime_error("Module not found");
|
|
|
|
void(*init)(CoreInstance&) = reinterpret_cast<void(*)(CoreInstance&)>(dlsym(handler, "InitModule"));
|
|
if(init == nullptr)
|
|
throw std::runtime_error("Module not contains InitModule");
|
|
init(core);
|
|
|
|
return handler;
|
|
}
|
|
|
|
void System::StartModule(void* handler)
|
|
{
|
|
void(*start)() = reinterpret_cast<void(*)()>(dlsym(handler, "StartModule"));
|
|
if(start == nullptr)
|
|
throw std::runtime_error("Module not contins StartModule");
|
|
start();
|
|
}
|
|
|
|
void System::StopModule(void* handler)
|
|
{
|
|
void(*stop)() = reinterpret_cast<void(*)()>(dlsym(handler, "StopModule"));
|
|
if(stop == nullptr)
|
|
std::runtime_error("Module mot contins StopModule");
|
|
stop();
|
|
dlclose(handler);
|
|
}
|
|
|
|
void System::QuitModule(void* handler)
|
|
{
|
|
void(*quit)() = reinterpret_cast<void(*)()>(dlsym(handler, "QuitModule"));
|
|
if(quit == nullptr)
|
|
throw std::runtime_error("Module not contains QuitModule");
|
|
quit();
|
|
dlclose(handler);
|
|
}
|
|
|
|
#endif
|