MemoryUtil.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <cstddef>
  5. #include <string>
  6. namespace Common
  7. {
  8. void* AllocateExecutableMemory(size_t size);
  9. // These two functions control the executable/writable state of the W^X memory
  10. // allocations. More detailed documentation about them is in the .cpp file.
  11. // In general where applicable the ScopedJITPageWriteAndNoExecute wrapper
  12. // should be used to prevent bugs from not pairing up the calls properly.
  13. // Allows a thread to write to executable memory, but not execute the data.
  14. void JITPageWriteEnableExecuteDisable();
  15. // Allows a thread to execute memory allocated for execution, but not write to it.
  16. void JITPageWriteDisableExecuteEnable();
  17. // RAII Wrapper around JITPageWrite*Execute*(). When this is in scope the thread can
  18. // write to executable memory but not execute it.
  19. struct ScopedJITPageWriteAndNoExecute
  20. {
  21. ScopedJITPageWriteAndNoExecute() { JITPageWriteEnableExecuteDisable(); }
  22. ~ScopedJITPageWriteAndNoExecute() { JITPageWriteDisableExecuteEnable(); }
  23. };
  24. void* AllocateMemoryPages(size_t size);
  25. bool FreeMemoryPages(void* ptr, size_t size);
  26. void* AllocateAlignedMemory(size_t size, size_t alignment);
  27. void FreeAlignedMemory(void* ptr);
  28. bool ReadProtectMemory(void* ptr, size_t size);
  29. bool WriteProtectMemory(void* ptr, size_t size, bool executable = false);
  30. bool UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute = false);
  31. size_t MemPhysical();
  32. } // namespace Common