Fix DynafuncMaker::clear() and some other improvements.

This commit is contained in:
King_DuckZ 2020-04-26 22:29:57 +02:00
parent 0545f26990
commit b5adc8156f

View file

@ -12,9 +12,14 @@
#include <string> #include <string>
#include <cstdlib> //for aligned_alloc() #include <cstdlib> //for aligned_alloc()
#include <sys/mman.h> //for mprotect() #include <sys/mman.h> //for mprotect()
#include <memory>
#include <new>
namespace wren { namespace wren {
namespace { namespace {
struct MemFree { void operator()(void* ptr) { std::free(ptr); }};
typedef std::unique_ptr<void, MemFree> unique_ptr_free_t;
//see src/dynafunc_amd64_gnu.asm //see src/dynafunc_amd64_gnu.asm
extern "C" const char g_dynafunc[]; extern "C" const char g_dynafunc[];
extern "C" const char g_dynafunc_end[]; extern "C" const char g_dynafunc_end[];
@ -75,13 +80,17 @@ namespace wren {
std::free(page); std::free(page);
} }
m_pages.clear(); m_pages.clear();
m_used_bytes = m_page_size;
} }
unsigned char* DynafuncMaker::allocate_chunk() { unsigned char* DynafuncMaker::allocate_chunk() {
if (m_page_size - m_used_bytes < g_dynafunc_len) { if (m_page_size - m_used_bytes < g_dynafunc_len) {
void* const page = std::aligned_alloc(m_page_size, m_page_size); unique_ptr_free_t page(std::aligned_alloc(m_page_size, m_page_size));
m_pages.push_back(page); if (not page)
mprotect(page, m_page_size, PROT_EXEC | PROT_READ | PROT_WRITE); throw std::bad_alloc();
mprotect(page.get(), m_page_size, PROT_EXEC | PROT_READ | PROT_WRITE);
m_pages.push_back(page.get());
page.release();
m_used_bytes = 0; m_used_bytes = 0;
} }