summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/lib/osdlib.h
diff options
context:
space:
mode:
author Vas Crabb <vas@vastheman.com>2021-01-06 01:25:58 +1100
committer Vas Crabb <vas@vastheman.com>2021-01-06 02:18:04 +1100
commit4eca05fe67e6a61a30af9d168590f3f3aaf9f639 (patch)
tree8ca9d1878d84d54ffaa11d0bafe43ea28b638f33 /src/osd/modules/lib/osdlib.h
parent7b22d972aea2dca442de9b8fb58687504e4444bc (diff)
cpu: Allow recompilers to work with W^X policy
Diffstat (limited to 'src/osd/modules/lib/osdlib.h')
-rw-r--r--src/osd/modules/lib/osdlib.h88
1 files changed, 86 insertions, 2 deletions
diff --git a/src/osd/modules/lib/osdlib.h b/src/osd/modules/lib/osdlib.h
index 0095244d6fe..59b5291b0a8 100644
--- a/src/osd/modules/lib/osdlib.h
+++ b/src/osd/modules/lib/osdlib.h
@@ -16,6 +16,7 @@
#ifndef __OSDLIB__
#define __OSDLIB__
+#include <initializer_list>
#include <string>
#include <type_traits>
#include <vector>
@@ -58,6 +59,90 @@ int osd_setenv(const char *name, const char *value, int overwrite);
-----------------------------------------------------------------------------*/
std::string osd_get_clipboard_text(void);
+namespace osd {
+
+bool invalidate_instruction_cache(void const *start, std::size_t size);
+
+
+class virtual_memory_allocation
+{
+public:
+ enum : unsigned
+ {
+ NONE = 0x00,
+ READ = 0x01,
+ WRITE = 0x02,
+ EXECUTE = 0x04,
+ READ_WRITE = READ | WRITE,
+ READ_EXECUTE = READ | EXECUTE
+ };
+
+ virtual_memory_allocation(virtual_memory_allocation const &) = delete;
+ virtual_memory_allocation &operator=(virtual_memory_allocation const &) = delete;
+
+ virtual_memory_allocation() { }
+ virtual_memory_allocation(std::initializer_list<std::size_t> blocks)
+ {
+ m_memory = do_alloc(blocks, m_size, m_page_size);
+ }
+ virtual_memory_allocation(virtual_memory_allocation &&that) : m_memory(that.m_memory), m_size(that.m_size), m_page_size(that.m_page_size)
+ {
+ that.m_memory = nullptr;
+ that.m_size = that.m_page_size = 0U;
+ }
+ ~virtual_memory_allocation()
+ {
+ if (m_memory)
+ do_free(m_memory, m_size);
+ }
+
+ explicit operator bool() const { return bool(m_memory); }
+ void *get() { return m_memory; }
+ std::size_t size() const { return m_size; }
+ std::size_t page_size() const { return m_page_size; }
+
+ bool set_access(std::size_t start, std::size_t size, unsigned access)
+ {
+ if ((start % m_page_size) || (size % m_page_size) || (start > m_size) || ((m_size - start) < size))
+ return false;
+ else
+ return do_set_access(reinterpret_cast<std::uint8_t *>(m_memory) + start, size, access);
+ }
+
+ virtual_memory_allocation &operator=(std::nullptr_t)
+ {
+ if (m_memory)
+ do_free(m_memory, m_size);
+ m_memory = nullptr;
+ m_size = m_page_size = 0U;
+ return *this;
+ }
+
+ virtual_memory_allocation &operator=(virtual_memory_allocation &&that)
+ {
+ if (&that != this)
+ {
+ if (m_memory)
+ do_free(m_memory, m_size);
+ m_memory = that.m_memory;
+ m_size = that.m_size;
+ m_page_size = that.m_page_size;
+ that.m_memory = nullptr;
+ that.m_size = that.m_page_size = 0U;
+ }
+ return *this;
+ }
+
+private:
+ static void *do_alloc(std::initializer_list<std::size_t> blocks, std::size_t &size, std::size_t &page_size);
+ static void do_free(void *start, std::size_t size);
+ static bool do_set_access(void *start, std::size_t size, unsigned access);
+
+ void *m_memory = nullptr;
+ std::size_t m_size = 0U, m_page_size = 0U;
+};
+
+
/*-----------------------------------------------------------------------------
dynamic_module: load functions from optional shared libraries
@@ -69,7 +154,6 @@ std::string osd_get_clipboard_text(void);
revisions of a same library)
-----------------------------------------------------------------------------*/
-namespace osd {
class dynamic_module
{
public:
@@ -80,7 +164,7 @@ public:
virtual ~dynamic_module() { };
template <typename T>
- typename std::enable_if<std::is_pointer<T>::value, T>::type bind(char const *symbol)
+ typename std::enable_if_t<std::is_pointer_v<T>, T> bind(char const *symbol)
{
return reinterpret_cast<T>(get_symbol_address(symbol));
}