memoserv/subprojects/memcard/include/memcard/memorycard.hpp
King_DuckZ 7af7ce540d Remove index property, store it packed in the pointer instead
To achieve this I force memory to a 16-bytes alignment, to
achieve this code will allocate 15 extra bytes on the heap
but each Block and Frame object will be 8 bytes smaller. Given
they are created on the fly and passed around a lot, I think
this is a good tradeoff. Not super urgent, but I got it done
now, mostly to make sure index is used in a sane way and doesn't
take too large values, which would prevent me from making this
change later in the future.
2025-02-24 18:56:39 +00:00

80 lines
2.1 KiB
C++

/* Copyright 2020-2025, Michele Santullo
* This file is part of memoserv.
*
* Memoserv is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Memoserv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Memoserv. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "memcard/block.hpp"
#include "memcard/content_info.hpp"
#include "memcard/block_iterator.hpp"
#include <vector>
#include <cstdint>
#include <array>
#include <cstddef>
#include <algorithm>
//see: https://www.psdevwiki.com/ps3/PS1_Savedata#Memory_Card_Formats_PS1
namespace mc::psx {
class MemoryCard {
public:
typedef BlockIterator<false> iterator;
typedef BlockIterator<true> const_iterator;
static const constexpr std::size_t GameBlocks = 16 - 1;
MemoryCard();
template <typename IT> explicit MemoryCard (IT beg, IT end);
~MemoryCard();
Block operator[] (std::size_t index);
Block block (std::size_t index);
Block toc_block();
ConstBlock operator[] (std::size_t index) const;
ConstBlock block (std::size_t index) const;
ConstBlock toc_block() const;
ContentInfo content_info() const;
std::size_t size() const;
iterator begin();
iterator end();
const_iterator cbegin() const;
const_iterator begin() const;
const_iterator cend() const;
const_iterator end() const;
private:
const std::uint8_t* data_ptr() const;
std::vector<std::uint8_t> m_data;
};
std::vector<std::size_t> block_group (const MemoryCard& mc, std::size_t index);
template <typename IT>
MemoryCard::MemoryCard (IT beg, IT end) :
MemoryCard()
{
std::copy_if(
beg,
end,
m_data.begin(),
[count = m_data.size()](auto&&) mutable {return static_cast<bool>(count--);}
);
}
} //namespace mc::psx