citra/src/common/memory_ref.h

149 lines
3.3 KiB
C++
Raw Normal View History

2020-03-28 15:21:10 +00:00
// Copyright 2020 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
2020-01-04 22:39:54 +00:00
#pragma once
#include <memory>
#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 16:14:36 +01:00
virtual const u8* GetPtr() const = 0;
virtual std::size_t GetSize() const = 0;
private:
template <class Archive>
void serialize(Archive& ar, const unsigned int) {}
friend class boost::serialization::access;
2020-01-04 22:39:54 +00:00
};
/// Backing memory implemented by a local buffer
class BufferMem : public BackingMem {
public:
BufferMem() = default;
explicit BufferMem(std::size_t size) : data(size) {}
2020-01-04 22:39:54 +00:00
u8* GetPtr() override {
2020-01-04 22:39:54 +00:00
return data.data();
}
2020-03-29 16:14:36 +01:00
const u8* GetPtr() const override {
return data.data();
}
std::size_t GetSize() const override {
return data.size();
2020-01-04 22:39:54 +00:00
}
std::vector<u8>& Vector() {
return data;
}
const std::vector<u8>& Vector() const {
return data;
}
2020-01-04 22:39:54 +00:00
private:
std::vector<u8> data;
template <class Archive>
void serialize(Archive& ar, const unsigned int) {
ar& boost::serialization::base_object<BackingMem>(*this);
2020-01-04 22:39:54 +00:00
ar& data;
}
friend class boost::serialization::access;
};
BOOST_CLASS_EXPORT_KEY(BufferMem);
/**
* A managed reference to host-side memory.
* Fast enough to be used everywhere instead of u8*
* Supports serialization.
*/
2020-01-04 22:39:54 +00:00
class MemoryRef {
public:
MemoryRef() = default;
MemoryRef(std::nullptr_t) {}
2020-01-04 22:39:54 +00:00
MemoryRef(std::shared_ptr<BackingMem> backing_mem_)
: backing_mem(std::move(backing_mem_)), offset(0) {
Init();
}
2020-03-29 16:14:36 +01:00
MemoryRef(std::shared_ptr<BackingMem> backing_mem_, u64 offset_)
2020-01-04 22:39:54 +00:00
: backing_mem(std::move(backing_mem_)), offset(offset_) {
ASSERT(offset <= backing_mem->GetSize());
2020-01-04 22:39:54 +00:00
Init();
}
2020-03-29 16:14:36 +01:00
explicit operator bool() const {
return cptr != nullptr;
}
2020-03-29 16:14:36 +01:00
operator u8*() {
2020-01-04 22:39:54 +00:00
return cptr;
}
2020-03-29 16:14:36 +01:00
u8* GetPtr() {
2020-01-04 22:39:54 +00:00
return cptr;
}
2020-03-29 16:14:36 +01:00
operator const u8*() const {
return cptr;
2020-01-04 22:39:54 +00:00
}
2020-03-29 16:14:36 +01:00
const u8* GetPtr() const {
2020-01-04 22:39:54 +00:00
return cptr;
}
2020-03-29 16:14:36 +01:00
std::size_t GetSize() const {
2020-01-04 22:39:54 +00:00
return csize;
}
2020-03-29 16:14:36 +01:00
MemoryRef& operator+=(u32 offset_by) {
2020-01-04 22:39:54 +00:00
ASSERT(offset_by < csize);
offset += offset_by;
Init();
2020-03-29 16:14:36 +01:00
return *this;
2020-01-04 22:39:54 +00:00
}
2020-03-29 16:14:36 +01:00
MemoryRef operator+(u32 offset_by) const {
2020-01-04 22:39:54 +00:00
ASSERT(offset_by < csize);
return MemoryRef(backing_mem, offset + offset_by);
}
private:
2020-03-29 16:14:36 +01:00
std::shared_ptr<BackingMem> backing_mem{};
u64 offset{};
2020-01-04 22:39:54 +00:00
// Cached values for speed
2020-03-29 16:14:36 +01:00
u8* cptr{};
std::size_t csize{};
2020-01-04 22:39:54 +00:00
void Init() {
2020-01-05 13:26:16 +00:00
if (backing_mem) {
cptr = backing_mem->GetPtr() + offset;
2020-03-29 16:14:36 +01:00
csize = static_cast<std::size_t>(backing_mem->GetSize() - offset);
2020-01-05 13:26:16 +00:00
} else {
cptr = nullptr;
csize = 0;
}
2020-01-04 22:39:54 +00:00
}
template <class Archive>
void serialize(Archive& ar, const unsigned int) {
ar& backing_mem;
ar& offset;
Init();
}
friend class boost::serialization::access;
};