Skip to content

Commit

Permalink
Modernize C++ usage (Atmosphere-NX#144)
Browse files Browse the repository at this point in the history
* Stratosphere: Use modern C++ idioms in some places

* algorithms like std::for_each are used instead of raw loops

* Stratosphere: Replace more raw loops with algorithms

* Stratosphere: Add a utility predicate function to test for equality with a reference element

This can be used to rewrite some common raw loops using algorithms instead

* fs.mitm: Use variant

* fs.mitm: Use enum class

* fs.mitm: Turn RomFSSourceInfo::Cleanup into a destructor

This obsoletes the need for a custom deleter in other places

* fs.mitm: Use enum class some more

* fs.mitm: Use unique_ptr

* fs.mitm: Simplify initialization

* Stratosphere: Simplify initialization

* fs.mitm: Use unique_ptr (fix memory leak along the way)

The previous code was using "delete" rather than "delete[]"

* fs.mitm: Use vector::emplace_back rather than push_back

emplace_back constructs elements in-place, hence avoiding a redundant element copy.

* Stratosphere: Replace more raw loops with algorithms

* Stratosphere: Use unique_ptr

* fs.mitm: Replace more raw loops with algorithms

* Stratosphere: Prefer move-construction over copy-construction when moving sink parameters around
  • Loading branch information
neobrain authored and SciresM committed Jun 19, 2018
1 parent f1c326a commit ad636f7
Show file tree
Hide file tree
Showing 28 changed files with 348 additions and 369 deletions.
26 changes: 13 additions & 13 deletions stratosphere/fs_mitm/source/fs_istorage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

#include "debug.hpp"

enum FsIStorageCmd {
FsIStorage_Cmd_Read = 0,
FsIStorage_Cmd_Write = 1,
FsIStorage_Cmd_Flush = 2,
FsIStorage_Cmd_SetSize = 3,
FsIStorage_Cmd_GetSize = 4,
FsIStorage_Cmd_OperateRange = 5,
enum class FsIStorageCmd {
Read = 0,
Write = 1,
Flush = 2,
SetSize = 3,
GetSize = 4,
OperateRange = 5,
};

class IStorage {
Expand Down Expand Up @@ -49,22 +49,22 @@ class IStorageInterface : public IServiceObject {
Result dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) final {
Result rc = 0xF601;
switch ((FsIStorageCmd)cmd_id) {
case FsIStorage_Cmd_Read:
case FsIStorageCmd::Read:
rc = WrapIpcCommandImpl<&IStorageInterface::read>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
case FsIStorage_Cmd_Write:
case FsIStorageCmd::Write:
rc = WrapIpcCommandImpl<&IStorageInterface::write>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
case FsIStorage_Cmd_Flush:
case FsIStorageCmd::Flush:
rc = WrapIpcCommandImpl<&IStorageInterface::flush>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
case FsIStorage_Cmd_SetSize:
case FsIStorageCmd::SetSize:
rc = WrapIpcCommandImpl<&IStorageInterface::set_size>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
case FsIStorage_Cmd_GetSize:
case FsIStorageCmd::GetSize:
rc = WrapIpcCommandImpl<&IStorageInterface::get_size>(this, r, out_c, pointer_buffer, pointer_buffer_size);
break;
case FsIStorage_Cmd_OperateRange:
case FsIStorageCmd::OperateRange:
if (kernelAbove400()) {
rc = WrapIpcCommandImpl<&IStorageInterface::operate_range>(this, r, out_c, pointer_buffer, pointer_buffer_size);
}
Expand Down
83 changes: 32 additions & 51 deletions stratosphere/fs_mitm/source/fsmitm_layeredrom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,19 @@
LayeredRomFS::LayeredRomFS(std::shared_ptr<RomInterfaceStorage> s_r, std::shared_ptr<RomFileStorage> f_r, u64 tid) : storage_romfs(s_r), file_romfs(f_r), title_id(tid) {
/* Start building the new virtual romfs. */
RomFSBuildContext build_ctx(this->title_id);
this->p_source_infos = std::shared_ptr<std::vector<RomFSSourceInfo>>(new std::vector<RomFSSourceInfo>(), [](std::vector<RomFSSourceInfo> *to_delete) {
for (unsigned int i = 0; i < to_delete->size(); i++) {
(*to_delete)[i].Cleanup();
}
delete to_delete;
});
this->p_source_infos = std::make_shared<std::vector<RomFSSourceInfo>>();
if (Utils::IsSdInitialized()) {
build_ctx.MergeSdFiles();
}
if (this->file_romfs) {
build_ctx.MergeRomStorage(this->file_romfs.get(), RomFSDataSource_FileRomFS);
build_ctx.MergeRomStorage(this->file_romfs.get(), RomFSDataSource::FileRomFS);
}
if (this->storage_romfs) {
build_ctx.MergeRomStorage(this->storage_romfs.get(), RomFSDataSource_BaseRomFS);
build_ctx.MergeRomStorage(this->storage_romfs.get(), RomFSDataSource::BaseRomFS);
}
build_ctx.Build(this->p_source_infos.get());
}

LayeredRomFS::~LayeredRomFS() {
/* ... */
}


Result LayeredRomFS::Read(void *buffer, size_t size, u64 offset) {
/* Validate size. */
Expand Down Expand Up @@ -60,7 +51,6 @@ Result LayeredRomFS::Read(void *buffer, size_t size, u64 offset) {
}
}

Result rc;
size_t read_so_far = 0;
while (read_so_far < size) {
RomFSSourceInfo *cur_source = &((*this->p_source_infos)[cur_source_ind]);
Expand All @@ -69,47 +59,38 @@ Result LayeredRomFS::Read(void *buffer, size_t size, u64 offset) {
if (cur_read_size > cur_source->size - (offset - cur_source->virtual_offset)) {
cur_read_size = cur_source->size - (offset - cur_source->virtual_offset);
}
switch (cur_source->type) {
case RomFSDataSource_LooseFile:
{
FsFile file;
if (R_FAILED((rc = Utils::OpenRomFSSdFile(this->title_id, cur_source->loose_source_info.path, FS_OPEN_READ, &file)))) {
fatalSimple(rc);
}
size_t out_read;
if (R_FAILED((rc = fsFileRead(&file, (offset - cur_source->virtual_offset), (void *)((uintptr_t)buffer + read_so_far), cur_read_size, &out_read)))) {
fatalSimple(rc);
}
if (out_read != cur_read_size) {
Reboot();
}
fsFileClose(&file);
auto source_info_visitor = [&](auto& info) -> Result {
Result rc = 0;
if constexpr (std::is_same_v<decltype(info), RomFSBaseSourceInfo>) {
FsFile file;
if (R_FAILED((rc = Utils::OpenRomFSSdFile(this->title_id, info.path, FS_OPEN_READ, &file)))) {
fatalSimple(rc);
}
break;
case RomFSDataSource_Memory:
{
memcpy((void *)((uintptr_t)buffer + read_so_far), cur_source->memory_source_info.data + (offset - cur_source->virtual_offset), cur_read_size);
size_t out_read;
if (R_FAILED((rc = fsFileRead(&file, (offset - cur_source->virtual_offset), (void *)((uintptr_t)buffer + read_so_far), cur_read_size, &out_read)))) {
fatalSimple(rc);
}
break;
case RomFSDataSource_BaseRomFS:
{
if (R_FAILED((rc = this->storage_romfs->Read((void *)((uintptr_t)buffer + read_so_far), cur_read_size, cur_source->base_source_info.offset + (offset - cur_source->virtual_offset))))) {
/* TODO: Can this ever happen? */
/* fatalSimple(rc); */
return rc;
}
if (out_read != cur_read_size) {
Reboot();
}
break;
case RomFSDataSource_FileRomFS:
{
if (R_FAILED((rc = this->file_romfs->Read((void *)((uintptr_t)buffer + read_so_far), cur_read_size, cur_source->base_source_info.offset + (offset - cur_source->virtual_offset))))) {
fatalSimple(rc);
}
fsFileClose(&file);
} else if constexpr (std::is_same_v<decltype(info), RomFSFileSourceInfo>) {
memcpy((void *)((uintptr_t)buffer + read_so_far), info.data + (offset - cur_source->virtual_offset), cur_read_size);
} else if constexpr (std::is_same_v<decltype(info), RomFSLooseSourceInfo>) {
if (R_FAILED((rc = this->storage_romfs->Read((void *)((uintptr_t)buffer + read_so_far), cur_read_size, info.offset + (offset - cur_source->virtual_offset))))) {
/* TODO: Can this ever happen? */
/* fatalSimple(rc); */
return rc;
}
break;
default:
fatalSimple(0xF601);
}
} else if constexpr (std::is_same_v<decltype(info), RomFSMemorySourceInfo>) {
if (R_FAILED((rc = this->file_romfs->Read((void *)((uintptr_t)buffer + read_so_far), cur_read_size, info.offset + (offset - cur_source->virtual_offset))))) {
fatalSimple(rc);
}
}
return rc;
};
Result rc = std::visit(source_info_visitor, cur_source->info);

read_so_far += cur_read_size;
} else {
/* Handle padding explicitly. */
Expand All @@ -132,4 +113,4 @@ Result LayeredRomFS::OperateRange(u32 operation_type, u64 offset, u64 size, FsRa
*out_range_info = {0};
}
return 0;
}
}
2 changes: 1 addition & 1 deletion stratosphere/fs_mitm/source/fsmitm_layeredrom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class LayeredRomFS : public IROStorage {

public:
LayeredRomFS(std::shared_ptr<RomInterfaceStorage> s_r, std::shared_ptr<RomFileStorage> f_r, u64 tid);
virtual ~LayeredRomFS();
virtual ~LayeredRomFS() = default;

Result Read(void *buffer, size_t size, u64 offset) override;
Result GetSize(u64 *out_size) override;
Expand Down
10 changes: 4 additions & 6 deletions stratosphere/fs_mitm/source/fsmitm_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ int main(int argc, char **argv)
}

/* TODO: What's a good timeout value to use here? */
MultiThreadedWaitableManager *server_manager = new MultiThreadedWaitableManager(1, U64_MAX, 0x20000);
//WaitableManager *server_manager = new WaitableManager(U64_MAX);
auto server_manager = std::make_unique<MultiThreadedWaitableManager>(1, U64_MAX, 0x20000);
//auto server_manager = std::make_unique<WaitableManager>(U64_MAX);

/* Create fsp-srv mitm. */
ISession<MitMQueryService<FsMitMService>> *fs_query_srv = NULL;
Expand All @@ -110,9 +110,7 @@ int main(int argc, char **argv)

/* Loop forever, servicing our services. */
server_manager->process();

/* Cleanup. */
delete server_manager;
return 0;

return 0;
}

32 changes: 15 additions & 17 deletions stratosphere/fs_mitm/source/fsmitm_romfsbuild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void RomFSBuildContext::MergeSdFiles() {
if (R_FAILED(fsMountSdcard(&sd_filesystem))) {
return;
}
this->cur_source_type = RomFSDataSource_LooseFile;
this->cur_source_type = RomFSDataSource::LooseFile;
this->VisitDirectory(&sd_filesystem, this->root);
fsFsClose(&sd_filesystem);
}
Expand Down Expand Up @@ -158,19 +158,17 @@ void RomFSBuildContext::MergeRomStorage(IROStorage *storage, RomFSDataSource sou
}

/* Read tables. */
u8 *dir_table = new u8[header.dir_table_size];
u8 *file_table = new u8[header.file_table_size];
if (R_FAILED((rc = storage->Read(dir_table, header.dir_table_size, header.dir_table_ofs)))) {
auto dir_table = std::make_unique<u8[]>(header.dir_table_size);
auto file_table = std::make_unique<u8[]>(header.file_table_size);
if (R_FAILED((rc = storage->Read(dir_table.get(), header.dir_table_size, header.dir_table_ofs)))) {
fatalSimple(rc);
}
if (R_FAILED((rc = storage->Read(file_table, header.file_table_size, header.file_table_ofs)))) {
if (R_FAILED((rc = storage->Read(file_table.get(), header.file_table_size, header.file_table_ofs)))) {
fatalSimple(rc);
}

this->cur_source_type = source;
this->VisitDirectory(this->root, 0x0, dir_table, (size_t)header.dir_table_size, file_table, (size_t)header.file_table_size);
delete dir_table;
delete file_table;
this->VisitDirectory(this->root, 0x0, dir_table.get(), (size_t)header.dir_table_size, file_table.get(), (size_t)header.file_table_size);
}

bool RomFSBuildContext::AddDirectory(RomFSBuildDirectoryContext *parent_dir_ctx, RomFSBuildDirectoryContext *dir_ctx, RomFSBuildDirectoryContext **out_dir_ctx) {
Expand Down Expand Up @@ -310,7 +308,7 @@ void RomFSBuildContext::Build(std::vector<RomFSSourceInfo> *out_infos) {
}

out_infos->clear();
out_infos->push_back(RomFSSourceInfo(0, sizeof(*header), header, RomFSDataSource_Memory));
out_infos->emplace_back(0, sizeof(*header), header, RomFSDataSource::Memory);

/* Determine file offsets. */
cur_file = this->files;
Expand Down Expand Up @@ -356,20 +354,20 @@ void RomFSBuildContext::Build(std::vector<RomFSSourceInfo> *out_infos) {


switch (cur_file->source) {
case RomFSDataSource_BaseRomFS:
case RomFSDataSource_FileRomFS:
case RomFSDataSource::BaseRomFS:
case RomFSDataSource::FileRomFS:
/* Try to compact, if possible. */
if (out_infos->back().type == cur_file->source) {
if (out_infos->back().GetType() == cur_file->source) {
out_infos->back().size = cur_file->offset + ROMFS_FILEPARTITION_OFS + cur_file->size - out_infos->back().virtual_offset;
} else {
out_infos->push_back(RomFSSourceInfo(cur_file->offset + ROMFS_FILEPARTITION_OFS, cur_file->size, cur_file->orig_offset + ROMFS_FILEPARTITION_OFS, cur_file->source));
out_infos->emplace_back(cur_file->offset + ROMFS_FILEPARTITION_OFS, cur_file->size, cur_file->orig_offset + ROMFS_FILEPARTITION_OFS, cur_file->source);
}
break;
case RomFSDataSource_LooseFile:
case RomFSDataSource::LooseFile:
{
char *path = new char[cur_file->path_len + 1];
strcpy(path, cur_file->path);
out_infos->push_back(RomFSSourceInfo(cur_file->offset + ROMFS_FILEPARTITION_OFS, cur_file->size, path, cur_file->source));
out_infos->emplace_back(cur_file->offset + ROMFS_FILEPARTITION_OFS, cur_file->size, path, cur_file->source);
}
break;
default:
Expand Down Expand Up @@ -416,5 +414,5 @@ void RomFSBuildContext::Build(std::vector<RomFSSourceInfo> *out_infos) {
header->file_hash_table_ofs = header->dir_table_ofs + header->dir_table_size;
header->file_table_ofs = header->file_hash_table_ofs + header->file_hash_table_size;

out_infos->push_back(RomFSSourceInfo(header->dir_hash_table_ofs, this->dir_hash_table_size + this->dir_table_size + this->file_hash_table_size + this->file_table_size, metadata, RomFSDataSource_Memory));
}
out_infos->emplace_back(header->dir_hash_table_ofs, this->dir_hash_table_size + this->dir_table_size + this->file_hash_table_size + this->file_table_size, metadata, RomFSDataSource::Memory);
}
Loading

0 comments on commit ad636f7

Please sign in to comment.