2020-03-28 16:21:10 +01:00
|
|
|
// Copyright 2020 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2020-01-04 23:39:54 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
2023-04-21 09:14:55 +02:00
|
|
|
#include <span>
|
2020-01-04 23:39:54 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <boost/serialization/export.hpp>
|
|
|
|
#include <boost/serialization/shared_ptr.hpp>
|
|
|
|
#include <boost/serialization/vector.hpp>
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
|
|
|
|
/// Abstract host-side memory - for example a static buffer, or local vector
|
|
|
|
class BackingMem {
|
|
|
|
public:
|
|
|
|
virtual ~BackingMem() = default;
|
|
|
|
virtual u8* GetPtr() = 0;
|
2020-03-29 17:14:36 +02:00
|
|
|
virtual const u8* GetPtr() const = 0;
|
|
|
|
virtual std::size_t GetSize() const = 0;
|
2020-01-08 23:13:56 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
template <class Archive>
|
2023-05-01 21:38:58 +02:00
|
|
|
void serialize(Archive&, const unsigned int) {}
|
2020-01-08 23:13:56 +01:00
|
|
|
friend class boost::serialization::access;
|
2020-01-04 23:39:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Backing memory implemented by a local buffer
|
|
|
|
class BufferMem : public BackingMem {
|
|
|
|
public:
|
|
|
|
BufferMem() = default;
|
2020-03-29 12:39:46 +02:00
|
|
|
explicit BufferMem(std::size_t size) : data(size) {}
|
2020-01-04 23:39:54 +01:00
|
|
|
|
2020-03-28 17:28:07 +01:00
|
|
|
u8* GetPtr() override {
|
2020-01-04 23:39:54 +01:00
|
|
|
return data.data();
|
|
|
|
}
|
|
|
|
|
2020-03-29 17:14:36 +02:00
|
|
|
const u8* GetPtr() const override {
|
|
|
|
return data.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t GetSize() const override {
|
2020-03-31 18:54:28 +02:00
|
|
|
return data.size();
|
2020-01-04 23:39:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8>& Vector() {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-03-31 18:54:28 +02:00
|
|
|
const std::vector<u8>& Vector() const {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-01-04 23:39:54 +01:00
|
|
|
private:
|
|
|
|
std::vector<u8> data;
|
|
|
|
|
|
|
|
template <class Archive>
|
|
|
|
void serialize(Archive& ar, const unsigned int) {
|
2020-01-08 23:13:56 +01:00
|
|
|
ar& boost::serialization::base_object<BackingMem>(*this);
|
2020-01-04 23:39:54 +01:00
|
|
|
ar& data;
|
|
|
|
}
|
|
|
|
friend class boost::serialization::access;
|
|
|
|
};
|
|
|
|
|
|
|
|
BOOST_CLASS_EXPORT_KEY(BufferMem);
|
|
|
|
|
2022-11-04 23:32:57 +01:00
|
|
|
/**
|
|
|
|
* A managed reference to host-side memory.
|
|
|
|
* Fast enough to be used everywhere instead of u8*
|
|
|
|
* Supports serialization.
|
|
|
|
*/
|
2020-01-04 23:39:54 +01:00
|
|
|
class MemoryRef {
|
|
|
|
public:
|
|
|
|
MemoryRef() = default;
|
|
|
|
MemoryRef(std::nullptr_t) {}
|
2022-11-04 23:32:57 +01:00
|
|
|
|
2020-01-04 23:39:54 +01:00
|
|
|
MemoryRef(std::shared_ptr<BackingMem> backing_mem_)
|
|
|
|
: backing_mem(std::move(backing_mem_)), offset(0) {
|
|
|
|
Init();
|
|
|
|
}
|
2020-03-29 17:14:36 +02:00
|
|
|
MemoryRef(std::shared_ptr<BackingMem> backing_mem_, u64 offset_)
|
2020-01-04 23:39:54 +01:00
|
|
|
: backing_mem(std::move(backing_mem_)), offset(offset_) {
|
2022-10-01 12:37:27 +02:00
|
|
|
ASSERT(offset <= backing_mem->GetSize());
|
2020-01-04 23:39:54 +01:00
|
|
|
Init();
|
|
|
|
}
|
2022-11-04 23:32:57 +01:00
|
|
|
|
2020-03-29 17:14:36 +02:00
|
|
|
explicit operator bool() const {
|
|
|
|
return cptr != nullptr;
|
|
|
|
}
|
2022-11-04 23:32:57 +01:00
|
|
|
|
2020-03-29 17:14:36 +02:00
|
|
|
operator u8*() {
|
2020-01-04 23:39:54 +01:00
|
|
|
return cptr;
|
|
|
|
}
|
2022-11-04 23:32:57 +01:00
|
|
|
|
2020-03-29 17:14:36 +02:00
|
|
|
u8* GetPtr() {
|
2020-01-04 23:39:54 +01:00
|
|
|
return cptr;
|
|
|
|
}
|
2022-11-04 23:32:57 +01:00
|
|
|
|
2020-03-29 17:14:36 +02:00
|
|
|
operator const u8*() const {
|
|
|
|
return cptr;
|
2020-01-04 23:39:54 +01:00
|
|
|
}
|
2022-11-04 23:32:57 +01:00
|
|
|
|
2020-03-29 17:14:36 +02:00
|
|
|
const u8* GetPtr() const {
|
2020-01-04 23:39:54 +01:00
|
|
|
return cptr;
|
|
|
|
}
|
2022-11-04 23:32:57 +01:00
|
|
|
|
2023-04-21 09:14:55 +02:00
|
|
|
std::span<u8> GetWriteBytes(std::size_t size) {
|
|
|
|
return std::span{cptr, std::min(size, csize)};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::span<const u8> GetReadBytes(std::size_t size) const {
|
|
|
|
return std::span{cptr, std::min(size, csize)};
|
|
|
|
}
|
|
|
|
|
2020-03-29 17:14:36 +02:00
|
|
|
std::size_t GetSize() const {
|
2020-01-04 23:39:54 +01:00
|
|
|
return csize;
|
|
|
|
}
|
2022-11-04 23:32:57 +01:00
|
|
|
|
2020-03-29 17:14:36 +02:00
|
|
|
MemoryRef& operator+=(u32 offset_by) {
|
2020-01-04 23:39:54 +01:00
|
|
|
ASSERT(offset_by < csize);
|
|
|
|
offset += offset_by;
|
|
|
|
Init();
|
2020-03-29 17:14:36 +02:00
|
|
|
return *this;
|
2020-01-04 23:39:54 +01:00
|
|
|
}
|
2022-11-04 23:32:57 +01:00
|
|
|
|
2020-03-29 17:14:36 +02:00
|
|
|
MemoryRef operator+(u32 offset_by) const {
|
2020-01-04 23:39:54 +01:00
|
|
|
ASSERT(offset_by < csize);
|
|
|
|
return MemoryRef(backing_mem, offset + offset_by);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-03-29 17:14:36 +02:00
|
|
|
std::shared_ptr<BackingMem> backing_mem{};
|
|
|
|
u64 offset{};
|
2020-01-04 23:39:54 +01:00
|
|
|
// Cached values for speed
|
2020-03-29 17:14:36 +02:00
|
|
|
u8* cptr{};
|
|
|
|
std::size_t csize{};
|
2020-01-04 23:39:54 +01:00
|
|
|
|
|
|
|
void Init() {
|
2020-01-05 14:26:16 +01:00
|
|
|
if (backing_mem) {
|
|
|
|
cptr = backing_mem->GetPtr() + offset;
|
2020-03-29 17:14:36 +02:00
|
|
|
csize = static_cast<std::size_t>(backing_mem->GetSize() - offset);
|
2020-01-05 14:26:16 +01:00
|
|
|
} else {
|
|
|
|
cptr = nullptr;
|
|
|
|
csize = 0;
|
|
|
|
}
|
2020-01-04 23:39:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Archive>
|
|
|
|
void serialize(Archive& ar, const unsigned int) {
|
|
|
|
ar& backing_mem;
|
|
|
|
ar& offset;
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
friend class boost::serialization::access;
|
|
|
|
};
|