CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/MemoryUtil.h
Views: 1401
// 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).48void* AllocateAlignedMemory(size_t size, size_t alignment);49void FreeAlignedMemory(void* ptr);5051int GetMemoryProtectPageSize();5253// A buffer that uses aligned memory. Can be useful for image processing.54template <typename T, size_t A>55class AlignedVector {56public:57AlignedVector() : buf_(0), size_(0) {}5859AlignedVector(size_t size) : buf_(0) {60resize(size);61}6263AlignedVector(const AlignedVector &o) : buf_(o.buf_), size_(o.size_) {}6465// Move constructor66AlignedVector(AlignedVector &&o) noexcept : buf_(o.buf_), size_(o.size_) { o.buf_ = nullptr; o.size_ = 0; }6768~AlignedVector() {69if (buf_ != 0) {70FreeAlignedMemory(buf_);71}72}7374inline T &operator[](size_t index) {75return buf_[index];76}7778// Doesn't preserve contents.79void resize(size_t size) {80if (size_ < size) {81if (buf_ != 0) {82FreeAlignedMemory(buf_);83}84buf_ = (T *)AllocateAlignedMemory(size * sizeof(T), A);85size_ = size;86}87}8889T *data() {90return buf_;91}9293size_t size() const {94return size_;95}9697private:98T *buf_;99size_t size_;100};101102103