Code review - general gardening

This commit is contained in:
Hamish Milne 2020-03-29 16:14:36 +01:00
parent 8f059ae398
commit 04aa351c40
10 changed files with 125 additions and 88 deletions

View file

@ -6,28 +6,29 @@
#include <boost/serialization/serialization.hpp>
/// Allows classes to define `save_construct` and `load_construct` methods for serialization
/// This is used where we don't call the default constructor during deserialization, as a shortcut
/// instead of using load_construct_data directly
class construct_access {
public:
template <class Archive, class T>
static inline void save_construct(Archive& ar, const T* t, const unsigned int file_version) {
static void save_construct(Archive& ar, const T* t, const unsigned int file_version) {
t->save_construct(ar, file_version);
}
template <class Archive, class T>
static inline void load_construct(Archive& ar, T* t, const unsigned int file_version) {
static void load_construct(Archive& ar, T* t, const unsigned int file_version) {
T::load_construct(ar, t, file_version);
}
};
#define BOOST_SERIALIZATION_CONSTRUCT(T) \
namespace boost { \
namespace serialization { \
namespace boost::serialization { \
template <class Archive> \
inline void save_construct_data(Archive& ar, const T* t, const unsigned int file_version) { \
void save_construct_data(Archive& ar, const T* t, const unsigned int file_version) { \
construct_access::save_construct(ar, t, file_version); \
} \
template <class Archive> \
inline void load_construct_data(Archive& ar, T* t, const unsigned int file_version) { \
void load_construct_data(Archive& ar, T* t, const unsigned int file_version) { \
construct_access::load_construct(ar, t, file_version); \
} \
} \
}

View file

@ -17,7 +17,8 @@ class BackingMem {
public:
virtual ~BackingMem() = default;
virtual u8* GetPtr() = 0;
virtual u32 GetSize() const = 0;
virtual const u8* GetPtr() const = 0;
virtual std::size_t GetSize() const = 0;
private:
template <class Archive>
@ -35,7 +36,11 @@ public:
return data.data();
}
u32 GetSize() const override {
const u8* GetPtr() const override {
return data.data();
}
std::size_t GetSize() const override {
return static_cast<u32>(data.size());
}
@ -66,51 +71,51 @@ public:
: backing_mem(std::move(backing_mem_)), offset(0) {
Init();
}
MemoryRef(std::shared_ptr<BackingMem> backing_mem_, u32 offset_)
MemoryRef(std::shared_ptr<BackingMem> backing_mem_, u64 offset_)
: backing_mem(std::move(backing_mem_)), offset(offset_) {
ASSERT(offset < backing_mem->GetSize());
Init();
}
inline operator u8*() {
return cptr;
}
inline u8* GetPtr() {
return cptr;
}
explicit operator bool() const {
return cptr != nullptr;
}
inline const u8* GetPtr() const {
operator u8*() {
return cptr;
}
inline u32 GetSize() const {
u8* GetPtr() {
return cptr;
}
operator const u8*() const {
return cptr;
}
const u8* GetPtr() const {
return cptr;
}
std::size_t GetSize() const {
return csize;
}
inline void operator+=(u32 offset_by) {
MemoryRef& operator+=(u32 offset_by) {
ASSERT(offset_by < csize);
offset += offset_by;
Init();
return *this;
}
inline MemoryRef operator+(u32 offset_by) const {
MemoryRef operator+(u32 offset_by) const {
ASSERT(offset_by < csize);
return MemoryRef(backing_mem, offset + offset_by);
}
inline u8* operator+(std::size_t offset_by) const {
ASSERT(offset_by < csize);
return cptr + offset_by;
}
private:
std::shared_ptr<BackingMem> backing_mem = nullptr;
u32 offset = 0;
std::shared_ptr<BackingMem> backing_mem{};
u64 offset{};
// Cached values for speed
u8* cptr = nullptr;
u32 csize = 0;
u8* cptr{};
std::size_t csize{};
void Init() {
if (backing_mem) {
cptr = backing_mem->GetPtr() + offset;
csize = static_cast<u32>(backing_mem->GetSize() - offset);
csize = static_cast<std::size_t>(backing_mem->GetSize() - offset);
} else {
cptr = nullptr;
csize = 0;

View file

@ -161,7 +161,7 @@ private:
// The priority level queues of thread ids.
std::array<Queue, NUM_QUEUES> queues;
s32 ToIndex(Queue* q) const {
s64 ToIndex(const Queue* q) const {
if (q == nullptr) {
return -2;
} else if (q == UnlinkedTag()) {
@ -171,7 +171,7 @@ private:
}
}
Queue* ToPointer(s32 idx) {
Queue* ToPointer(s64 idx) {
if (idx == -1) {
return UnlinkedTag();
} else if (idx < 0) {
@ -184,10 +184,10 @@ private:
friend class boost::serialization::access;
template <class Archive>
void save(Archive& ar, const unsigned int file_version) const {
s32 idx = ToIndex(first);
const s64 idx = ToIndex(first);
ar << idx;
for (size_t i = 0; i < NUM_QUEUES; i++) {
const s32 idx1 = ToIndex(queues[i].next_nonempty);
const s64 idx1 = ToIndex(queues[i].next_nonempty);
ar << idx1;
ar << queues[i].data;
}
@ -195,7 +195,7 @@ private:
template <class Archive>
void load(Archive& ar, const unsigned int file_version) {
s32 idx;
s64 idx;
ar >> idx;
first = ToPointer(idx);
for (auto i = 0; i < NUM_QUEUES; i++) {