From b5adc8156fd826972f1feea7739df98e6894e90a Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sun, 26 Apr 2020 22:29:57 +0200 Subject: [PATCH] Fix DynafuncMaker::clear() and some other improvements. --- src/wren/dynafunc_maker.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/wren/dynafunc_maker.cpp b/src/wren/dynafunc_maker.cpp index c2e2b64..d3edf8a 100644 --- a/src/wren/dynafunc_maker.cpp +++ b/src/wren/dynafunc_maker.cpp @@ -12,9 +12,14 @@ #include #include //for aligned_alloc() #include //for mprotect() +#include +#include namespace wren { namespace { + struct MemFree { void operator()(void* ptr) { std::free(ptr); }}; + typedef std::unique_ptr unique_ptr_free_t; + //see src/dynafunc_amd64_gnu.asm extern "C" const char g_dynafunc[]; extern "C" const char g_dynafunc_end[]; @@ -75,13 +80,17 @@ namespace wren { std::free(page); } m_pages.clear(); + m_used_bytes = m_page_size; } unsigned char* DynafuncMaker::allocate_chunk() { if (m_page_size - m_used_bytes < g_dynafunc_len) { - void* const page = std::aligned_alloc(m_page_size, m_page_size); - m_pages.push_back(page); - mprotect(page, m_page_size, PROT_EXEC | PROT_READ | PROT_WRITE); + unique_ptr_free_t page(std::aligned_alloc(m_page_size, m_page_size)); + if (not page) + 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; }