// Copyright (C) 2003 Dolphin Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official SVN repository and contact information can be found at15// http://code.google.com/p/dolphin-emu/1617#pragma once1819#ifndef _WIN3220#ifndef __SWITCH__21#include <sys/mman.h>22#else23#include <switch.h>24#endif // !__SWITCH__25#endif26#include <stdint.h>2728// Returns true if we need to avoid setting both writable and executable at the same time (W^X)29bool PlatformIsWXExclusive();3031#define MEM_PROT_READ 132#define MEM_PROT_WRITE 233#define MEM_PROT_EXEC 43435// Note that some platforms go through special contortions to allocate executable memory. So for memory36// that's intended for execution, allocate it first using AllocateExecutableMemory, then modify protection as desired.37// AllocateMemoryPages is simpler and more generic.38// Note that on W^X platforms, this will return writable memory that can later be changed to executable!39void* AllocateExecutableMemory(size_t size);40void FreeExecutableMemory(void *ptr, size_t size);4142void* AllocateMemoryPages(size_t size, uint32_t memProtFlags);43// Note that on platforms returning PlatformIsWXExclusive, you cannot set a page to be both readable and writable at the same time.44bool ProtectMemoryPages(const void* ptr, size_t size, uint32_t memProtFlags);45void FreeMemoryPages(void* ptr, size_t size);4647// Regular aligned memory. Don't try to apply memory protection willy-nilly to memory allocated this way as in-page alignment is unknown (though could be checked).48// No longer asserts, will return nullptr on failure.49// WARNING: Not necessarily malloc-compatible!50void *AllocateAlignedMemory(size_t size, size_t alignment);51void FreeAlignedMemory(void* ptr);5253int GetMemoryProtectPageSize();5455// A buffer that uses aligned memory. Can be useful for image processing.56template <typename T, size_t A>57class AlignedVector {58public:59AlignedVector() : buf_(0), size_(0) {}6061AlignedVector(size_t size) : buf_(0) {62resize(size);63}6465AlignedVector(const AlignedVector &o) : buf_(o.buf_), size_(o.size_) {}6667// Move constructor68AlignedVector(AlignedVector &&o) noexcept : buf_(o.buf_), size_(o.size_) { o.buf_ = nullptr; o.size_ = 0; }6970~AlignedVector() {71if (buf_ != 0) {72FreeAlignedMemory(buf_);73}74}7576inline T &operator[](size_t index) {77return buf_[index];78}7980// Doesn't preserve contents.81void resize(size_t size) {82if (size_ < size) {83if (buf_ != 0) {84FreeAlignedMemory(buf_);85}86buf_ = (T *)AllocateAlignedMemory(size * sizeof(T), A);87size_ = size;88}89}9091T *data() {92return buf_;93}9495size_t size() const {96return size_;97}9899private:100T *buf_;101size_t size_;102};103104105