unique_ptr'ing things and removing some XECLEANUP.

This commit is contained in:
Ben Vanik
2014-08-20 23:26:46 -07:00
parent 244e8a8745
commit c59d053404
43 changed files with 271 additions and 347 deletions

View File

@@ -10,6 +10,7 @@
#ifndef XENIA_KERNEL_FS_DEVICE_H_
#define XENIA_KERNEL_FS_DEVICE_H_
#include <memory>
#include <string>
#include <xenia/core.h>
@@ -26,7 +27,7 @@ class Device {
const std::string& path() const { return path_; }
virtual Entry* ResolvePath(const char* path) = 0;
virtual std::unique_ptr<Entry> ResolvePath(const char* path) = 0;
virtual X_STATUS QueryVolume(XVolumeInfo* out_info, size_t length) = 0;
virtual X_STATUS QueryFileSystemAttributes(XFileSystemAttributeInfo* out_info,

View File

@@ -42,7 +42,7 @@ int DiscImageDevice::Init() {
return 0;
}
Entry* DiscImageDevice::ResolvePath(const char* path) {
std::unique_ptr<Entry> DiscImageDevice::ResolvePath(const char* path) {
// The filesystem will have stripped our prefix off already, so the path will
// be in the form:
// some\PATH.foo
@@ -64,7 +64,8 @@ Entry* DiscImageDevice::ResolvePath(const char* path) {
Entry::Type type = gdfx_entry->attributes & X_FILE_ATTRIBUTE_DIRECTORY
? Entry::Type::DIRECTORY
: Entry::Type::FILE;
return new DiscImageEntry(type, this, path, mmap_.get(), gdfx_entry);
return std::make_unique<DiscImageEntry>(type, this, path, mmap_.get(),
gdfx_entry);
}
X_STATUS DiscImageDevice::QueryVolume(XVolumeInfo* out_info, size_t length) {

View File

@@ -31,7 +31,7 @@ class DiscImageDevice : public Device {
int Init();
Entry* ResolvePath(const char* path) override;
std::unique_ptr<Entry> ResolvePath(const char* path) override;
X_STATUS QueryVolume(XVolumeInfo* out_info, size_t length) override;
X_STATUS QueryFileSystemAttributes(XFileSystemAttributeInfo* out_info,

View File

@@ -96,23 +96,22 @@ X_STATUS DiscImageEntry::QueryDirectory(XDirectoryInfo* out_info, size_t length,
return X_STATUS_SUCCESS;
}
MemoryMapping* DiscImageEntry::CreateMemoryMapping(Mode map_mode,
const size_t offset,
const size_t length) {
std::unique_ptr<MemoryMapping> DiscImageEntry::CreateMemoryMapping(
Mode map_mode, const size_t offset, const size_t length) {
if (map_mode != Mode::READ) {
// Only allow reads.
return NULL;
return nullptr;
}
size_t real_offset = gdfx_entry_->offset + offset;
size_t real_length =
length ? std::min(length, gdfx_entry_->size) : gdfx_entry_->size;
return new DiscImageMemoryMapping(mmap_->data() + real_offset, real_length,
mmap_);
return std::make_unique<DiscImageMemoryMapping>(mmap_->data() + real_offset,
real_length, mmap_);
}
X_STATUS DiscImageEntry::Open(KernelState* kernel_state, Mode mode,
bool async, XFile** out_file) {
X_STATUS DiscImageEntry::Open(KernelState* kernel_state, Mode mode, bool async,
XFile** out_file) {
*out_file = new DiscImageFile(kernel_state, mode, this);
return X_STATUS_SUCCESS;
}

View File

@@ -37,11 +37,11 @@ class DiscImageEntry : public Entry {
const char* file_name, bool restart);
virtual bool can_map() { return true; }
virtual MemoryMapping* CreateMemoryMapping(Mode map_mode, const size_t offset,
const size_t length);
virtual std::unique_ptr<MemoryMapping> CreateMemoryMapping(
Mode map_mode, const size_t offset, const size_t length);
virtual X_STATUS Open(KernelState* kernel_state, Mode mode,
bool async, XFile** out_file);
virtual X_STATUS Open(KernelState* kernel_state, Mode mode, bool async,
XFile** out_file);
private:
poly::MappedMemory* mmap_;

View File

@@ -22,7 +22,7 @@ HostPathDevice::HostPathDevice(const std::string& path,
HostPathDevice::~HostPathDevice() {}
Entry* HostPathDevice::ResolvePath(const char* path) {
std::unique_ptr<Entry> HostPathDevice::ResolvePath(const char* path) {
// The filesystem will have stripped our prefix off already, so the path will
// be in the form:
// some\PATH.foo
@@ -38,8 +38,7 @@ Entry* HostPathDevice::ResolvePath(const char* path) {
// TODO(benvanik): switch based on type
auto type = Entry::Type::FILE;
HostPathEntry* entry = new HostPathEntry(type, this, path, full_path);
return entry;
return std::make_unique<HostPathEntry>(type, this, path, full_path);
}
// TODO(gibbed): call into HostPathDevice?

View File

@@ -25,7 +25,7 @@ class HostPathDevice : public Device {
HostPathDevice(const std::string& path, const std::wstring& local_path);
~HostPathDevice() override;
Entry* ResolvePath(const char* path) override;
std::unique_ptr<Entry> ResolvePath(const char* path) override;
X_STATUS QueryVolume(XVolumeInfo* out_info, size_t length) override;
X_STATUS QueryFileSystemAttributes(XFileSystemAttributeInfo* out_info,

View File

@@ -121,9 +121,8 @@ X_STATUS HostPathEntry::QueryDirectory(XDirectoryInfo* out_info, size_t length,
return X_STATUS_SUCCESS;
}
MemoryMapping* HostPathEntry::CreateMemoryMapping(Mode map_mode,
const size_t offset,
const size_t length) {
std::unique_ptr<MemoryMapping> HostPathEntry::CreateMemoryMapping(
Mode map_mode, const size_t offset, const size_t length) {
auto mmap = poly::MappedMemory::Open(
local_path_,
map_mode == Mode::READ ? poly::MappedMemory::Mode::READ
@@ -133,13 +132,11 @@ MemoryMapping* HostPathEntry::CreateMemoryMapping(Mode map_mode,
return nullptr;
}
HostPathMemoryMapping* lfmm = new HostPathMemoryMapping(std::move(mmap));
return lfmm;
return std::make_unique<HostPathMemoryMapping>(std::move(mmap));
}
X_STATUS HostPathEntry::Open(KernelState* kernel_state, Mode mode,
bool async, XFile** out_file) {
X_STATUS HostPathEntry::Open(KernelState* kernel_state, Mode mode, bool async,
XFile** out_file) {
DWORD desired_access =
mode == Mode::READ ? GENERIC_READ : (GENERIC_READ | GENERIC_WRITE);
DWORD share_mode = FILE_SHARE_READ;

View File

@@ -31,8 +31,8 @@ class HostPathEntry : public Entry {
const char* file_name, bool restart);
virtual bool can_map() { return true; }
virtual MemoryMapping* CreateMemoryMapping(Mode map_mode, const size_t offset,
const size_t length);
virtual std::unique_ptr<MemoryMapping> CreateMemoryMapping(
Mode map_mode, const size_t offset, const size_t length);
virtual X_STATUS Open(KernelState* kernel_state, Mode mode, bool async,
XFile** out_file);

View File

@@ -42,7 +42,7 @@ int STFSContainerDevice::Init() {
return 0;
}
Entry* STFSContainerDevice::ResolvePath(const char* path) {
std::unique_ptr<Entry> STFSContainerDevice::ResolvePath(const char* path) {
// The filesystem will have stripped our prefix off already, so the path will
// be in the form:
// some\PATH.foo
@@ -64,7 +64,8 @@ Entry* STFSContainerDevice::ResolvePath(const char* path) {
Entry::Type type = stfs_entry->attributes & X_FILE_ATTRIBUTE_DIRECTORY
? Entry::Type::DIRECTORY
: Entry::Type::FILE;
return new STFSContainerEntry(type, this, path, mmap_.get(), stfs_entry);
return std::make_unique<STFSContainerEntry>(type, this, path, mmap_.get(),
stfs_entry);
}
X_STATUS STFSContainerDevice::QueryVolume(XVolumeInfo* out_info,

View File

@@ -31,7 +31,7 @@ class STFSContainerDevice : public Device {
int Init();
Entry* ResolvePath(const char* path) override;
std::unique_ptr<Entry> ResolvePath(const char* path) override;
X_STATUS QueryVolume(XVolumeInfo* out_info, size_t length) override;
X_STATUS QueryFileSystemAttributes(XFileSystemAttributeInfo* out_info,

View File

@@ -10,6 +10,7 @@
#ifndef XENIA_KERNEL_FS_ENTRY_H_
#define XENIA_KERNEL_FS_ENTRY_H_
#include <memory>
#include <string>
#include <xenia/core.h>
@@ -72,8 +73,8 @@ class Entry {
virtual bool can_map() { return false; }
virtual MemoryMapping* CreateMemoryMapping(Mode map_mode, const size_t offset,
const size_t length) {
virtual std::unique_ptr<MemoryMapping> CreateMemoryMapping(
Mode map_mode, const size_t offset, const size_t length) {
return NULL;
}

View File

@@ -142,7 +142,7 @@ int FileSystem::DeleteSymbolicLink(const std::string& path) {
return 0;
}
Entry* FileSystem::ResolvePath(const std::string& path) {
std::unique_ptr<Entry> FileSystem::ResolvePath(const std::string& path) {
// Strip off prefix and pass to device.
// e.g., d:\some\PATH.foo -> some\PATH.foo
// Support both symlinks and device specifiers, like:
@@ -173,7 +173,7 @@ Entry* FileSystem::ResolvePath(const std::string& path) {
}
XELOGE("ResolvePath(%s) failed - no root found", path.c_str());
return NULL;
return nullptr;
}
} // namespace fs

View File

@@ -10,6 +10,7 @@
#ifndef XENIA_KERNEL_FS_FILESYSTEM_H_
#define XENIA_KERNEL_FS_FILESYSTEM_H_
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
@@ -50,7 +51,7 @@ class FileSystem {
int CreateSymbolicLink(const std::string& path, const std::string& target);
int DeleteSymbolicLink(const std::string& path);
Entry* ResolvePath(const std::string& path);
std::unique_ptr<Entry> ResolvePath(const std::string& path);
private:
std::vector<Device*> devices_;

View File

@@ -33,41 +33,33 @@ const xe_xex2_header_t* XUserModule::xex_header() {
X_STATUS XUserModule::LoadFromFile(const char* path) {
X_STATUS result = X_STATUS_UNSUCCESSFUL;
XFile* file = NULL;
uint8_t* buffer = 0;
// Resolve the file to open.
// TODO(benvanik): make this code shared?
fs::Entry* fs_entry = kernel_state()->file_system()->ResolvePath(path);
auto fs_entry = kernel_state()->file_system()->ResolvePath(path);
if (!fs_entry) {
XELOGE("File not found: %s", path);
result = X_STATUS_NO_SUCH_FILE;
XEFAIL();
return X_STATUS_NO_SUCH_FILE;
}
if (fs_entry->type() != fs::Entry::Type::FILE) {
XELOGE("Invalid file type: %s", path);
result = X_STATUS_NO_SUCH_FILE;
XEFAIL();
return X_STATUS_NO_SUCH_FILE;
}
// If the FS supports mapping, map the file in and load from that.
if (fs_entry->can_map()) {
// Map.
fs::MemoryMapping* mmap =
fs_entry->CreateMemoryMapping(fs::Mode::READ, 0, 0);
auto mmap = fs_entry->CreateMemoryMapping(fs::Mode::READ, 0, 0);
XEEXPECTNOTNULL(mmap);
// Load the module.
result = LoadFromMemory(mmap->address(), mmap->length());
// Unmap memory and cleanup.
delete mmap;
} else {
XFileInfo file_info;
result = fs_entry->QueryInfo(&file_info);
XEEXPECTZERO(result);
size_t buffer_length = file_info.file_length;
buffer = (uint8_t*)malloc(buffer_length);
std::vector<uint8_t> buffer(file_info.file_length);
// Open file for reading.
result = fs_entry->Open(kernel_state(), fs::Mode::READ, false, &file);
@@ -76,21 +68,17 @@ X_STATUS XUserModule::LoadFromFile(const char* path) {
// Read entire file into memory.
// Ugh.
size_t bytes_read = 0;
result = file->Read(buffer, buffer_length, 0, &bytes_read);
result = file->Read(buffer.data(), buffer.size(), 0, &bytes_read);
XEEXPECTZERO(result);
// Load the module.
result = LoadFromMemory(buffer, bytes_read);
result = LoadFromMemory(buffer.data(), bytes_read);
}
XECLEANUP:
if (buffer) {
free(buffer);
}
if (file) {
file->Release();
}
delete fs_entry;
return result;
}

View File

@@ -81,7 +81,7 @@ SHIM_CALL NtCreateFile_shim(PPCContext* ppc_state, KernelState* state) {
uint32_t handle;
FileSystem* fs = state->file_system();
Entry* entry;
std::unique_ptr<Entry> entry;
XFile* root_file = NULL;
if (attrs.root_directory != 0xFFFFFFFD && // ObDosDevices
@@ -93,10 +93,10 @@ SHIM_CALL NtCreateFile_shim(PPCContext* ppc_state, KernelState* state) {
auto root_path = root_file->absolute_path();
auto target_path = root_path + object_name;
entry = fs->ResolvePath(target_path);
entry = std::move(fs->ResolvePath(target_path));
} else {
// Resolve the file using the virtual file system.
entry = fs->ResolvePath(object_name);
entry = std::move(fs->ResolvePath(object_name));
}
auto mode =
@@ -169,7 +169,7 @@ SHIM_CALL NtOpenFile_shim(PPCContext* ppc_state, KernelState* state) {
// Resolve the file using the virtual file system.
FileSystem* fs = state->file_system();
Entry* entry = fs->ResolvePath(object_name);
auto entry = fs->ResolvePath(object_name);
XFile* file = NULL;
if (entry && entry->type() == Entry::Type::FILE) {
// Open the file.
@@ -480,7 +480,7 @@ SHIM_CALL NtQueryFullAttributesFile_shim(PPCContext* ppc_state,
// Resolve the file using the virtual file system.
FileSystem* fs = state->file_system();
Entry* entry = fs->ResolvePath(object_name);
auto entry = fs->ResolvePath(object_name);
if (entry && entry->type() == Entry::Type::FILE) {
// Found.
XFileInfo file_info;