CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/MemoryUtil.h
Views: 1401
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
void* AllocateAlignedMemory(size_t size, size_t alignment);
50
void FreeAlignedMemory(void* ptr);
51
52
int GetMemoryProtectPageSize();
53
54
// A buffer that uses aligned memory. Can be useful for image processing.
55
template <typename T, size_t A>
56
class AlignedVector {
57
public:
58
AlignedVector() : buf_(0), size_(0) {}
59
60
AlignedVector(size_t size) : buf_(0) {
61
resize(size);
62
}
63
64
AlignedVector(const AlignedVector &o) : buf_(o.buf_), size_(o.size_) {}
65
66
// Move constructor
67
AlignedVector(AlignedVector &&o) noexcept : buf_(o.buf_), size_(o.size_) { o.buf_ = nullptr; o.size_ = 0; }
68
69
~AlignedVector() {
70
if (buf_ != 0) {
71
FreeAlignedMemory(buf_);
72
}
73
}
74
75
inline T &operator[](size_t index) {
76
return buf_[index];
77
}
78
79
// Doesn't preserve contents.
80
void resize(size_t size) {
81
if (size_ < size) {
82
if (buf_ != 0) {
83
FreeAlignedMemory(buf_);
84
}
85
buf_ = (T *)AllocateAlignedMemory(size * sizeof(T), A);
86
size_ = size;
87
}
88
}
89
90
T *data() {
91
return buf_;
92
}
93
94
size_t size() const {
95
return size_;
96
}
97
98
private:
99
T *buf_;
100
size_t size_;
101
};
102
103