#include #include #include #ifdef __linux__ #include #endif #ifdef __APPLE__ #include #endif namespace pslang::jit { blob make_host_executable(std::vector const & code) { #if defined(__linux__) || defined(__APPLE__) auto size = code.size(); auto ptr = (std::uint8_t *)mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); if (ptr == MAP_FAILED) throw std::system_error(errno, std::generic_category()); std::copy(code.begin(), code.end(), ptr); if (mprotect(ptr, size, PROT_READ | PROT_EXEC) != 0) throw std::system_error(errno, std::generic_category()); return blob(std::shared_ptr(ptr, [size](void * ptr){ munmap(ptr, size); }), size); #else throw std::runtime_error("Host-executable modules are not supported for this platform"); #endif } }