2020-05-11 22:04:51 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-05-11 22:04:51 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <vapours.hpp>
|
|
|
|
#include <stratosphere/os.hpp>
|
|
|
|
#include <stratosphere/fs/fs_istorage.hpp>
|
|
|
|
#include <stratosphere/fs/fs_substorage.hpp>
|
2022-03-12 21:03:17 +00:00
|
|
|
#include <stratosphere/fs/fs_i_buffer_manager.hpp>
|
2020-05-11 22:04:51 +00:00
|
|
|
|
2022-03-12 22:08:39 +00:00
|
|
|
namespace ams::fssystem {
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
class BufferedStorage : public ::ams::fs::IStorage {
|
|
|
|
NON_COPYABLE(BufferedStorage);
|
|
|
|
NON_MOVEABLE(BufferedStorage);
|
|
|
|
private:
|
|
|
|
class Cache;
|
|
|
|
class UniqueCache;
|
|
|
|
class SharedCache;
|
|
|
|
private:
|
2021-10-10 07:14:06 +00:00
|
|
|
fs::SubStorage m_base_storage;
|
2022-03-12 21:03:17 +00:00
|
|
|
fs::IBufferManager *m_buffer_manager;
|
2021-10-10 07:14:06 +00:00
|
|
|
size_t m_block_size;
|
|
|
|
s64 m_base_storage_size;
|
|
|
|
std::unique_ptr<Cache[]> m_caches;
|
|
|
|
s32 m_cache_count;
|
|
|
|
Cache *m_next_acquire_cache;
|
|
|
|
Cache *m_next_fetch_cache;
|
|
|
|
os::SdkMutex m_mutex;
|
|
|
|
bool m_bulk_read_enabled;
|
2020-05-11 22:04:51 +00:00
|
|
|
public:
|
|
|
|
BufferedStorage();
|
|
|
|
virtual ~BufferedStorage();
|
|
|
|
|
2022-03-12 21:03:17 +00:00
|
|
|
Result Initialize(fs::SubStorage base_storage, fs::IBufferManager *buffer_manager, size_t block_size, s32 buffer_count);
|
2020-05-11 22:04:51 +00:00
|
|
|
void Finalize();
|
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
bool IsInitialized() const { return m_caches != nullptr; }
|
2020-05-11 22:04:51 +00:00
|
|
|
|
|
|
|
virtual Result Read(s64 offset, void *buffer, size_t size) override;
|
|
|
|
virtual Result Write(s64 offset, const void *buffer, size_t size) override;
|
|
|
|
|
|
|
|
virtual Result GetSize(s64 *out) override;
|
|
|
|
virtual Result SetSize(s64 size) override;
|
|
|
|
|
|
|
|
virtual Result Flush() override;
|
|
|
|
virtual Result OperateRange(void *dst, size_t dst_size, fs::OperationId op_id, s64 offset, s64 size, const void *src, size_t src_size) override;
|
|
|
|
using IStorage::OperateRange;
|
|
|
|
|
|
|
|
void InvalidateCaches();
|
|
|
|
|
2022-03-12 21:03:17 +00:00
|
|
|
fs::IBufferManager *GetBufferManager() const { return m_buffer_manager; }
|
2020-05-11 22:04:51 +00:00
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
void EnableBulkRead() { m_bulk_read_enabled = true; }
|
2020-05-11 22:04:51 +00:00
|
|
|
private:
|
|
|
|
Result PrepareAllocation();
|
|
|
|
Result ControlDirtiness();
|
|
|
|
Result ReadCore(s64 offset, void *buffer, size_t size);
|
|
|
|
|
|
|
|
bool ReadHeadCache(s64 *offset, void *buffer, size_t *size, s64 *buffer_offset);
|
|
|
|
bool ReadTailCache(s64 offset, void *buffer, size_t *size, s64 buffer_offset);
|
|
|
|
|
|
|
|
Result BulkRead(s64 offset, void *buffer, size_t size, bool head_cache_needed, bool tail_cache_needed);
|
|
|
|
|
|
|
|
Result WriteCore(s64 offset, const void *buffer, size_t size);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|