Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/MemoryUtil.h
5656 views
1
// Copyright (C) 2003 Dolphin Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official SVN repository and contact information can be found at
16
// http://code.google.com/p/dolphin-emu/
17
18
#pragma once
19
20
#ifndef _WIN32
21
#ifndef __SWITCH__
22
#include <sys/mman.h>
23
#else
24
#include <switch.h>
25
#endif // !__SWITCH__
26
#endif
27
#include <stdint.h>
28
29
// Returns true if we need to avoid setting both writable and executable at the same time (W^X)
30
bool PlatformIsWXExclusive();
31
32
#define MEM_PROT_READ 1
33
#define MEM_PROT_WRITE 2
34
#define MEM_PROT_EXEC 4
35
36
// Note that some platforms go through special contortions to allocate executable memory. So for memory
37
// that's intended for execution, allocate it first using AllocateExecutableMemory, then modify protection as desired.
38
// AllocateMemoryPages is simpler and more generic.
39
// Note that on W^X platforms, this will return writable memory that can later be changed to executable!
40
void* AllocateExecutableMemory(size_t size);
41
void FreeExecutableMemory(void *ptr, size_t size);
42
43
void* AllocateMemoryPages(size_t size, uint32_t memProtFlags);
44
// Note that on platforms returning PlatformIsWXExclusive, you cannot set a page to be both readable and writable at the same time.
45
bool ProtectMemoryPages(const void* ptr, size_t size, uint32_t memProtFlags);
46
void FreeMemoryPages(void* ptr, size_t size);
47
48
// 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).
49
// No longer asserts, will return nullptr on failure.
50
// WARNING: Not necessarily malloc-compatible!
51
void *AllocateAlignedMemory(size_t size, size_t alignment);
52
void FreeAlignedMemory(void* ptr);
53
54
int GetMemoryProtectPageSize();
55
56
// A buffer that uses aligned memory. Can be useful for image processing.
57
template <typename T, size_t A>
58
class AlignedVector {
59
public:
60
AlignedVector() : buf_(0), size_(0) {}
61
62
AlignedVector(size_t size) : buf_(0) {
63
resize(size);
64
}
65
66
AlignedVector(const AlignedVector &o) : buf_(o.buf_), size_(o.size_) {}
67
68
// Move constructor
69
AlignedVector(AlignedVector &&o) noexcept : buf_(o.buf_), size_(o.size_) { o.buf_ = nullptr; o.size_ = 0; }
70
71
~AlignedVector() {
72
if (buf_ != 0) {
73
FreeAlignedMemory(buf_);
74
}
75
}
76
77
inline T &operator[](size_t index) {
78
return buf_[index];
79
}
80
81
// Doesn't preserve contents.
82
void resize(size_t size) {
83
if (size_ < size) {
84
if (buf_ != 0) {
85
FreeAlignedMemory(buf_);
86
}
87
buf_ = (T *)AllocateAlignedMemory(size * sizeof(T), A);
88
size_ = size;
89
}
90
}
91
92
T *data() {
93
return buf_;
94
}
95
96
size_t size() const {
97
return size_;
98
}
99
100
private:
101
T *buf_;
102
size_t size_;
103
};
104
105