45 lines
1.2 KiB
C++
45 lines
1.2 KiB
C++
#include <memory>
|
|
#include <pslang/jit/executable.hpp>
|
|
|
|
#include <stdexcept>
|
|
#include <system_error>
|
|
|
|
#ifdef __linux__
|
|
#include <sys/mman.h>
|
|
#endif
|
|
|
|
#ifdef __APPLE__
|
|
#include <sys/mman.h>
|
|
#endif
|
|
|
|
namespace pslang::jit
|
|
{
|
|
|
|
blob allocate(std::size_t size)
|
|
{
|
|
#if defined(__linux__) || defined(__APPLE__)
|
|
auto ptr = (std::uint8_t *)mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
|
|
auto shared_ptr = std::shared_ptr<std::uint8_t>(ptr, [size](void * ptr){ munmap(ptr, size); });
|
|
return blob(shared_ptr, size);
|
|
#else
|
|
throw std::runtime_error("Allocate not supported for this platform");
|
|
#endif
|
|
}
|
|
|
|
compiled_module make_host_executable(compiled_module module)
|
|
{
|
|
#if defined(__linux__) || defined(__APPLE__)
|
|
if (module.abi != abi::itanium && module.abi != abi::armv8)
|
|
throw std::runtime_error("Abi mismatch");
|
|
|
|
// Assume the module code memory was obtained via mmap
|
|
if (mprotect(module.code.memory.data.get(), module.code.memory.size, PROT_READ | PROT_EXEC) != 0)
|
|
throw std::system_error(errno, std::generic_category());
|
|
|
|
return module;
|
|
#else
|
|
throw std::runtime_error("Host-executable modules are not supported for this platform");
|
|
#endif
|
|
}
|
|
|
|
}
|