From a4dfc23abcc1d463caf8b94d3571745917225b39 Mon Sep 17 00:00:00 2001 From: Ben Vanik Date: Sat, 16 Aug 2014 02:30:23 -0700 Subject: [PATCH] Working on switching to std::string. --- src/alloy/frontend/ppc/ppc_context.cc | 4 +- src/poly/sources.gypi | 1 + src/poly/string.cc | 16 ++++++ src/poly/string.h | 10 +++- src/poly/threading.h | 3 ++ src/poly/threading_mac.cc | 8 +-- src/poly/threading_posix.cc | 42 +++++++++++++++ src/poly/threading_win.cc | 4 ++ src/xenia/export_resolver.cc | 4 +- src/xenia/gpu/d3d11/d3d11_shader_resource.cc | 4 +- src/xenia/gpu/xenos/ucode_disassembler.cc | 2 +- src/xenia/kernel/fs/device.cc | 4 +- src/xenia/kernel/fs/device.h | 2 +- .../kernel/fs/devices/disc_image_file.cc | 6 +-- src/xenia/kernel/fs/devices/disc_image_file.h | 34 ++++++------ src/xenia/kernel/fs/devices/host_path_file.cc | 6 +-- src/xenia/kernel/fs/devices/host_path_file.h | 38 +++++++------ .../kernel/fs/devices/stfs_container_file.cc | 6 +-- .../kernel/fs/devices/stfs_container_file.h | 34 ++++++------ src/xenia/kernel/fs/entry.cc | 27 +++------- src/xenia/kernel/fs/entry.h | 16 +++--- src/xenia/kernel/fs/filesystem.cc | 54 ++++++++----------- src/xenia/kernel/fs/filesystem.h | 6 +-- src/xenia/kernel/fs/gdfx.cc | 2 +- src/xenia/kernel/fs/stfs.cc | 2 +- src/xenia/kernel/kernel_state.cc | 6 +-- src/xenia/kernel/objects/xfile.h | 13 ++--- src/xenia/kernel/objects/xthread.cc | 38 ++----------- src/xenia/kernel/objects/xthread.h | 7 +-- src/xenia/kernel/objects/xuser_module.cc | 2 +- src/xenia/kernel/util/xex2.cc | 2 +- src/xenia/sources.gypi | 1 - src/xenia/string.cc | 33 ------------ src/xenia/string.h | 24 +-------- 34 files changed, 211 insertions(+), 250 deletions(-) create mode 100644 src/poly/threading_posix.cc delete mode 100644 src/xenia/string.cc diff --git a/src/alloy/frontend/ppc/ppc_context.cc b/src/alloy/frontend/ppc/ppc_context.cc index b6eeb336c..b6b743621 100644 --- a/src/alloy/frontend/ppc/ppc_context.cc +++ b/src/alloy/frontend/ppc/ppc_context.cc @@ -13,7 +13,9 @@ namespace alloy { namespace frontend { namespace ppc { -uint64_t ParseInt64(const char* value) { return xestrtoulla(value, NULL, 0); } +uint64_t ParseInt64(const char* value) { + return std::strtoull(value, nullptr, 0); +} void PPCContext::SetRegFromString(const char* name, const char* value) { int n; diff --git a/src/poly/sources.gypi b/src/poly/sources.gypi index 19e08905b..547d9e838 100644 --- a/src/poly/sources.gypi +++ b/src/poly/sources.gypi @@ -27,6 +27,7 @@ }], ['OS == "linux"', { 'sources': [ + 'threading_posix.cc', ], }], ['OS == "mac"', { diff --git a/src/poly/string.cc b/src/poly/string.cc index 1d8bb53e2..4716c0a32 100644 --- a/src/poly/string.cc +++ b/src/poly/string.cc @@ -23,4 +23,20 @@ std::wstring to_wstring(const std::string& source) { return converter.from_bytes(source); } +std::string::size_type find_first_of_case(const std::string& target, + const std::string& search) { + const char* str = target.c_str(); + while (*str) { + if (!strncasecmp(str, search.c_str(), search.size())) { + break; + } + str++; + } + if (*str) { + return str - target.c_str(); + } else { + return std::string::npos; + } +} + } // namespace poly diff --git a/src/poly/string.h b/src/poly/string.h index 20b684e6d..7d360abd2 100644 --- a/src/poly/string.h +++ b/src/poly/string.h @@ -12,13 +12,21 @@ #include -#include +#include + +#if XE_LIKE_WIN32 +#define strcasecmp _stricmp +#define strncasecmp _strnicmp +#endif // XE_LIKE_WIN32 namespace poly { std::string to_string(const std::wstring& source); std::wstring to_wstring(const std::string& source); +std::string::size_type find_first_of_case(const std::string& target, + const std::string& search); + } // namespace poly #endif // POLY_STRING_H_ diff --git a/src/poly/threading.h b/src/poly/threading.h index 61e74a300..4c8284bf5 100644 --- a/src/poly/threading.h +++ b/src/poly/threading.h @@ -13,6 +13,7 @@ #include #include #include +#include #include @@ -28,6 +29,8 @@ uint32_t current_thread_id(); // Sets the current thread name. void set_name(const std::string& name); +// Sets the target thread name. +void set_name(std::thread::native_handle_type handle, const std::string& name); // Yields the current thread to the scheduler. Maybe. void Yield(); diff --git a/src/poly/threading_mac.cc b/src/poly/threading_mac.cc index b0ec61406..6d448315c 100644 --- a/src/poly/threading_mac.cc +++ b/src/poly/threading_mac.cc @@ -25,11 +25,11 @@ uint32_t current_thread_id() { } void set_name(const std::string& name) { -#if XE_LIKE_OSX pthread_setname_np(name.c_str()); -#else - pthread_setname_np(pthread_self(), name.c_str()); -#endif // XE_LIKE_OSX +} + +void set_name(std::thread::native_handle_type handle, const std::string& name) { + // ? } void Yield() { pthread_yield_np(); } diff --git a/src/poly/threading_posix.cc b/src/poly/threading_posix.cc new file mode 100644 index 000000000..005a708d6 --- /dev/null +++ b/src/poly/threading_posix.cc @@ -0,0 +1,42 @@ +/** + ****************************************************************************** + * Xenia : Xbox 360 Emulator Research Project * + ****************************************************************************** + * Copyright 2014 Ben Vanik. All rights reserved. * + * Released under the BSD license - see LICENSE in the root for more details. * + ****************************************************************************** + */ + +#include + +#include +#include + +namespace poly { +namespace threading { + +//uint64_t ticks() { return mach_absolute_time(); } + +// uint32_t current_thread_id() { +// mach_port_t tid = pthread_mach_thread_np(pthread_self()); +// return static_cast(tid); +// } + +void set_name(const std::string& name) { + pthread_setname_np(pthread_self(), name.c_str()); +} + +void set_name(std::thread::native_handle_type handle, const std::string& name) { + pthread_setname_np(pthread_self(), name.c_str()); +} + +void Yield() { pthread_yield_np(); } + +void Sleep(std::chrono::microseconds duration) { + timespec rqtp = {duration.count() / 1000000, duration.count() % 1000}; + nanosleep(&rqtp, nullptr); + // TODO(benvanik): spin while rmtp >0? +} + +} // namespace threading +} // namespace poly diff --git a/src/poly/threading_win.cc b/src/poly/threading_win.cc index d5a1a2b3f..a75c73fac 100644 --- a/src/poly/threading_win.cc +++ b/src/poly/threading_win.cc @@ -57,6 +57,10 @@ void set_name(const std::string& name) { set_name(static_cast(-1), name); } +void set_name(std::thread::native_handle_type handle, const std::string& name) { + set_name(GetThreadId(handle), name); +} + void Yield() { SwitchToThread(); } void Sleep(std::chrono::microseconds duration) { diff --git a/src/xenia/export_resolver.cc b/src/xenia/export_resolver.cc index c311819b8..19a29cce1 100644 --- a/src/xenia/export_resolver.cc +++ b/src/xenia/export_resolver.cc @@ -38,7 +38,7 @@ void ExportResolver::RegisterTable( uint16_t ExportResolver::GetLibraryOrdinal(const char* library_name) { uint16_t n = 0; for (auto it = tables_.begin(); it != tables_.end(); ++it, n++) { - if (!xestrcmpa(library_name, it->name)) { + if (!strcmp(library_name, it->name)) { return n; } } @@ -61,7 +61,7 @@ KernelExport* ExportResolver::GetExportByOrdinal(const char* library_name, const uint32_t ordinal) { for (std::vector::iterator it = tables_.begin(); it != tables_.end(); ++it) { - if (!xestrcmpa(library_name, it->name)) { + if (!strcmp(library_name, it->name)) { // TODO(benvanik): binary search? for (size_t n = 0; n < it->count; n++) { if (it->exports[n].ordinal == ordinal) { diff --git a/src/xenia/gpu/d3d11/d3d11_shader_resource.cc b/src/xenia/gpu/d3d11/d3d11_shader_resource.cc index c9c3e033f..7fc40905f 100644 --- a/src/xenia/gpu/d3d11/d3d11_shader_resource.cc +++ b/src/xenia/gpu/d3d11/d3d11_shader_resource.cc @@ -128,7 +128,7 @@ int D3D11VertexShaderResource::Prepare( XELOGE("D3D11: failed to translate vertex shader"); return ret; } - translated_src_ = xestrdupa(translator.translated_src()); + translated_src_ = strdup(translator.translated_src()); ID3D10Blob* shader_blob = D3D11ShaderCompile( XE_GPU_SHADER_TYPE_VERTEX, translated_src_, disasm_src()); @@ -351,7 +351,7 @@ int D3D11PixelShaderResource::Prepare(const xe_gpu_program_cntl_t& program_cntl, XELOGE("D3D11: failed to translate pixel shader"); return ret; } - translated_src_ = xestrdupa(translator.translated_src()); + translated_src_ = strdup(translator.translated_src()); ID3D10Blob* shader_blob = D3D11ShaderCompile( XE_GPU_SHADER_TYPE_PIXEL, translated_src_, disasm_src()); diff --git a/src/xenia/gpu/xenos/ucode_disassembler.cc b/src/xenia/gpu/xenos/ucode_disassembler.cc index db3ad44eb..0e0ac9875 100644 --- a/src/xenia/gpu/xenos/ucode_disassembler.cc +++ b/src/xenia/gpu/xenos/ucode_disassembler.cc @@ -754,7 +754,7 @@ char* xenos::DisassembleShader( } } - char* result = xestrdupa(output->buffer); + char* result = strdup(output->buffer); delete output; return result; } diff --git a/src/xenia/kernel/fs/device.cc b/src/xenia/kernel/fs/device.cc index c9c37664a..9bc191b27 100644 --- a/src/xenia/kernel/fs/device.cc +++ b/src/xenia/kernel/fs/device.cc @@ -15,6 +15,4 @@ using namespace xe::kernel::fs; Device::Device(const std::string& path) : path_(path) {} -Device::~Device() {} - -const char* Device::path() const { return path_.c_str(); } +Device::~Device() = default; diff --git a/src/xenia/kernel/fs/device.h b/src/xenia/kernel/fs/device.h index 01215e47a..81d409761 100644 --- a/src/xenia/kernel/fs/device.h +++ b/src/xenia/kernel/fs/device.h @@ -26,7 +26,7 @@ class Device { Device(const std::string& path); virtual ~Device(); - const char* path() const; + const std::string& path() const { return path_; } virtual Entry* ResolvePath(const char* path) = 0; diff --git a/src/xenia/kernel/fs/devices/disc_image_file.cc b/src/xenia/kernel/fs/devices/disc_image_file.cc index c094dbef7..3f78efd5b 100644 --- a/src/xenia/kernel/fs/devices/disc_image_file.cc +++ b/src/xenia/kernel/fs/devices/disc_image_file.cc @@ -29,15 +29,15 @@ DiscImageFile::DiscImageFile( DiscImageFile::~DiscImageFile() { } -const char* DiscImageFile::path(void) const { +const std::string& DiscImageFile::path() const { return entry_->path(); } -const char* DiscImageFile::absolute_path(void) const { +const std::string& DiscImageFile::absolute_path() const { return entry_->absolute_path(); } -const char* DiscImageFile::name(void) const { +const std::string& DiscImageFile::name() const { return entry_->name(); } diff --git a/src/xenia/kernel/fs/devices/disc_image_file.h b/src/xenia/kernel/fs/devices/disc_image_file.h index b43f89863..e138705de 100644 --- a/src/xenia/kernel/fs/devices/disc_image_file.h +++ b/src/xenia/kernel/fs/devices/disc_image_file.h @@ -15,43 +15,39 @@ #include - namespace xe { namespace kernel { namespace fs { class DiscImageEntry; - class DiscImageFile : public XFile { -public: + public: DiscImageFile(KernelState* kernel_state, uint32_t desired_access, DiscImageEntry* entry); - virtual ~DiscImageFile(); + ~DiscImageFile() override; - virtual const char* path(void) const; - virtual const char* absolute_path(void) const; - virtual const char* name(void) const; + const std::string& path() const override; + const std::string& absolute_path() const override; + const std::string& name() const override; - virtual X_STATUS QueryInfo(XFileInfo* out_info); - virtual X_STATUS QueryDirectory(XDirectoryInfo* out_info, - size_t length, const char* file_name, bool restart); - virtual X_STATUS QueryVolume(XVolumeInfo* out_info, size_t length); - virtual X_STATUS QueryFileSystemAttributes(XFileSystemAttributeInfo* out_info, size_t length); + X_STATUS QueryInfo(XFileInfo* out_info) override; + X_STATUS QueryDirectory(XDirectoryInfo* out_info, size_t length, + const char* file_name, bool restart) override; + X_STATUS QueryVolume(XVolumeInfo* out_info, size_t length) override; + X_STATUS QueryFileSystemAttributes(XFileSystemAttributeInfo* out_info, + size_t length) override; -protected: - virtual X_STATUS ReadSync( - void* buffer, size_t buffer_length, size_t byte_offset, - size_t* out_bytes_read); + protected: + X_STATUS ReadSync(void* buffer, size_t buffer_length, size_t byte_offset, + size_t* out_bytes_read) override; -private: + private: DiscImageEntry* entry_; }; - } // namespace fs } // namespace kernel } // namespace xe - #endif // XENIA_KERNEL_FS_DEVICES_DISC_IMAGE_FILE_H_ diff --git a/src/xenia/kernel/fs/devices/host_path_file.cc b/src/xenia/kernel/fs/devices/host_path_file.cc index b36f9f890..166a57bc9 100644 --- a/src/xenia/kernel/fs/devices/host_path_file.cc +++ b/src/xenia/kernel/fs/devices/host_path_file.cc @@ -29,15 +29,15 @@ HostPathFile::~HostPathFile() { CloseHandle(file_handle_); } -const char* HostPathFile::path(void) const { +const std::string& HostPathFile::path() const { return entry_->path(); } -const char* HostPathFile::absolute_path(void) const { +const std::string& HostPathFile::absolute_path() const { return entry_->absolute_path(); } -const char* HostPathFile::name(void) const { +const std::string& HostPathFile::name() const { return entry_->name(); } diff --git a/src/xenia/kernel/fs/devices/host_path_file.h b/src/xenia/kernel/fs/devices/host_path_file.h index e759ba763..9ef850c4c 100644 --- a/src/xenia/kernel/fs/devices/host_path_file.h +++ b/src/xenia/kernel/fs/devices/host_path_file.h @@ -10,49 +10,47 @@ #ifndef XENIA_KERNEL_FS_DEVICES_HOST_PATH_FILE_H_ #define XENIA_KERNEL_FS_DEVICES_HOST_PATH_FILE_H_ +#include + #include #include #include - namespace xe { namespace kernel { namespace fs { class HostPathEntry; - class HostPathFile : public XFile { -public: + public: HostPathFile(KernelState* kernel_state, uint32_t desired_access, HostPathEntry* entry, HANDLE file_handle); - virtual ~HostPathFile(); + ~HostPathFile() override; - virtual const char* path(void) const; - virtual const char* absolute_path(void) const; - virtual const char* name(void) const; + const std::string& path() const override; + const std::string& absolute_path() const override; + const std::string& name() const override; - virtual X_STATUS QueryInfo(XFileInfo* out_info); - virtual X_STATUS QueryDirectory(XDirectoryInfo* out_info, - size_t length, const char* file_name, bool restart); - virtual X_STATUS QueryVolume(XVolumeInfo* out_info, size_t length); - virtual X_STATUS QueryFileSystemAttributes(XFileSystemAttributeInfo* out_info, size_t length); + X_STATUS QueryInfo(XFileInfo* out_info) override; + X_STATUS QueryDirectory(XDirectoryInfo* out_info, size_t length, + const char* file_name, bool restart) override; + X_STATUS QueryVolume(XVolumeInfo* out_info, size_t length) override; + X_STATUS QueryFileSystemAttributes(XFileSystemAttributeInfo* out_info, + size_t length) override; -protected: - virtual X_STATUS ReadSync( - void* buffer, size_t buffer_length, size_t byte_offset, - size_t* out_bytes_read); + protected: + X_STATUS ReadSync(void* buffer, size_t buffer_length, size_t byte_offset, + size_t* out_bytes_read) override; -private: + private: HostPathEntry* entry_; - HANDLE file_handle_; + HANDLE file_handle_; }; - } // namespace fs } // namespace kernel } // namespace xe - #endif // XENIA_KERNEL_FS_DEVICES_HOST_PATH_FILE_H_ diff --git a/src/xenia/kernel/fs/devices/stfs_container_file.cc b/src/xenia/kernel/fs/devices/stfs_container_file.cc index 4f9f25a53..fbba72a99 100644 --- a/src/xenia/kernel/fs/devices/stfs_container_file.cc +++ b/src/xenia/kernel/fs/devices/stfs_container_file.cc @@ -29,15 +29,15 @@ STFSContainerFile::STFSContainerFile( STFSContainerFile::~STFSContainerFile() { } -const char* STFSContainerFile::path(void) const { +const std::string& STFSContainerFile::path() const { return entry_->path(); } -const char* STFSContainerFile::absolute_path(void) const { +const std::string& STFSContainerFile::absolute_path() const { return entry_->absolute_path(); } -const char* STFSContainerFile::name(void) const { +const std::string& STFSContainerFile::name() const { return entry_->name(); } diff --git a/src/xenia/kernel/fs/devices/stfs_container_file.h b/src/xenia/kernel/fs/devices/stfs_container_file.h index 778404eaf..eed3e232d 100644 --- a/src/xenia/kernel/fs/devices/stfs_container_file.h +++ b/src/xenia/kernel/fs/devices/stfs_container_file.h @@ -15,43 +15,39 @@ #include - namespace xe { namespace kernel { namespace fs { class STFSContainerEntry; - class STFSContainerFile : public XFile { -public: + public: STFSContainerFile(KernelState* kernel_state, uint32_t desired_access, STFSContainerEntry* entry); - virtual ~STFSContainerFile(); + ~STFSContainerFile() override; - virtual const char* path(void) const; - virtual const char* absolute_path(void) const; - virtual const char* name(void) const; + const std::string& path() const override; + const std::string& absolute_path() const override; + const std::string& name() const override; - virtual X_STATUS QueryInfo(XFileInfo* out_info); - virtual X_STATUS QueryDirectory(XDirectoryInfo* out_info, - size_t length, const char* file_name, bool restart); - virtual X_STATUS QueryVolume(XVolumeInfo* out_info, size_t length); - virtual X_STATUS QueryFileSystemAttributes(XFileSystemAttributeInfo* out_info, size_t length); + X_STATUS QueryInfo(XFileInfo* out_info) override; + X_STATUS QueryDirectory(XDirectoryInfo* out_info, size_t length, + const char* file_name, bool restart) override; + X_STATUS QueryVolume(XVolumeInfo* out_info, size_t length) override; + X_STATUS QueryFileSystemAttributes(XFileSystemAttributeInfo* out_info, + size_t length) override; -protected: - virtual X_STATUS ReadSync( - void* buffer, size_t buffer_length, size_t byte_offset, - size_t* out_bytes_read); + protected: + X_STATUS ReadSync(void* buffer, size_t buffer_length, size_t byte_offset, + size_t* out_bytes_read) override; -private: + private: STFSContainerEntry* entry_; }; - } // namespace fs } // namespace kernel } // namespace xe - #endif // XENIA_KERNEL_FS_DEVICES_STFS_CONTAINER_FILE_H_ diff --git a/src/xenia/kernel/fs/entry.cc b/src/xenia/kernel/fs/entry.cc index 0658d0f7b..30b18d74b 100644 --- a/src/xenia/kernel/fs/entry.cc +++ b/src/xenia/kernel/fs/entry.cc @@ -14,28 +14,17 @@ using namespace xe; using namespace xe::kernel; using namespace xe::kernel::fs; +MemoryMapping::MemoryMapping(uint8_t* address, size_t length) + : address_(address), length_(length) {} -MemoryMapping::MemoryMapping(uint8_t* address, size_t length) : - address_(address), length_(length) { -} +MemoryMapping::~MemoryMapping() {} -MemoryMapping::~MemoryMapping() { -} - - -Entry::Entry(Type type, Device* device, const char* path) : - type_(type), - device_(device) { +Entry::Entry(Type type, Device* device, const std::string& path) + : type_(type), device_(device), path_(path) { assert_not_null(device); - path_ = xestrdupa(path); - // TODO(benvanik): *shudder* - absolute_path_ = xestrdupa((std::string(device->path()) + std::string(path)).c_str()); + absolute_path_ = device->path() + path; // TODO(benvanik): last index of \, unless \ at end, then before that - name_ = NULL; + name_ = ""; } -Entry::~Entry() { - xe_free(name_); - xe_free(path_); - xe_free(absolute_path_); -} +Entry::~Entry() = default; diff --git a/src/xenia/kernel/fs/entry.h b/src/xenia/kernel/fs/entry.h index c21b57a16..1f5b09d1a 100644 --- a/src/xenia/kernel/fs/entry.h +++ b/src/xenia/kernel/fs/entry.h @@ -10,6 +10,8 @@ #ifndef XENIA_KERNEL_FS_ENTRY_H_ #define XENIA_KERNEL_FS_ENTRY_H_ +#include + #include #include @@ -52,14 +54,14 @@ public: kTypeDirectory, }; - Entry(Type type, Device* device, const char* path); + Entry(Type type, Device* device, const std::string& path); virtual ~Entry(); Type type() const { return type_; } Device* device() const { return device_; } - const char* path() const { return path_; } - const char* absolute_path() const { return absolute_path_; } - const char* name() const { return name_; } + const std::string& path() const { return path_; } + const std::string& absolute_path() const { return absolute_path_; } + const std::string& name() const { return name_; } virtual X_STATUS QueryInfo(XFileInfo* out_info) = 0; virtual X_STATUS QueryDirectory(XDirectoryInfo* out_info, @@ -79,9 +81,9 @@ public: private: Type type_; Device* device_; - char* path_; - char* absolute_path_; - char* name_; + std::string path_; + std::string absolute_path_; + std::string name_; }; diff --git a/src/xenia/kernel/fs/filesystem.cc b/src/xenia/kernel/fs/filesystem.cc index 538959329..26cc4b35d 100644 --- a/src/xenia/kernel/fs/filesystem.cc +++ b/src/xenia/kernel/fs/filesystem.cc @@ -9,6 +9,7 @@ #include +#include #include #include #include @@ -129,24 +130,22 @@ int FileSystem::RegisterSTFSContainerDevice( return RegisterDevice(path, device); } -int FileSystem::CreateSymbolicLink(const char* path, const char* target) { - symlinks_.insert(std::pair( - xestrdupa(path), - xestrdupa(target))); +int FileSystem::CreateSymbolicLink(const std::string& path, + const std::string& target) { + symlinks_.insert({path, target}); return 0; } -int FileSystem::DeleteSymbolicLink(const char* path) { - std::unordered_map::iterator it = - symlinks_.find(std::string(path)); - if (it != symlinks_.end()) { - symlinks_.erase(it); - return 0; +int FileSystem::DeleteSymbolicLink(const std::string& path) { + auto& it = symlinks_.find(path); + if (it == symlinks_.end()) { + return 1; } - return 1; + symlinks_.erase(it); + return 0; } -Entry* FileSystem::ResolvePath(const char* path) { +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: @@ -158,33 +157,24 @@ Entry* FileSystem::ResolvePath(const char* path) { // Resolve symlinks. // TODO(benvanik): more robust symlink handling - right now we assume simple // drive path -> device mappings with nothing nested. - char full_path[poly::max_path]; - XEIGNORE(xestrcpya(full_path, XECOUNT(full_path), path)); - for (std::unordered_map::iterator it = - symlinks_.begin(); it != symlinks_.end(); ++it) { - if (xestrcasestra(path, it->first.c_str()) == path) { - // Found symlink, fixup. - const char* after_path = path + it->first.size(); - XEIGNORE(xesnprintfa(full_path, XECOUNT(full_path), "%s%s", - it->second.c_str(), after_path)); + std::string full_path = path; + for (const auto& it : symlinks_) { + if (poly::find_first_of_case(path, it.first) == 0) { + // Found symlink, fixup by replacing the prefix. + full_path = it.second + full_path.substr(it.first.size()); break; } } // Scan all devices. - for (std::vector::iterator it = devices_.begin(); - it != devices_.end(); ++it) { - Device* device = *it; - if (xestrcasestra(full_path, device->path()) == full_path) { - // Found! - // Trim the device prefix off and pass down. - char device_path[poly::max_path]; - XEIGNORE(xestrcpya(device_path, XECOUNT(device_path), - full_path + xestrlena(device->path()))); - return device->ResolvePath(device_path); + for (auto& device : devices_) { + if (poly::find_first_of_case(full_path, device->path()) == 0) { + // Found! Trim the device prefix off and pass down. + auto device_path = full_path.substr(device->path().size()); + return device->ResolvePath(device_path.c_str()); } } - XELOGE("ResolvePath(%s) failed - no root found", path); + XELOGE("ResolvePath(%s) failed - no root found", path.c_str()); return NULL; } diff --git a/src/xenia/kernel/fs/filesystem.h b/src/xenia/kernel/fs/filesystem.h index 76d6ab549..de0107fac 100644 --- a/src/xenia/kernel/fs/filesystem.h +++ b/src/xenia/kernel/fs/filesystem.h @@ -47,10 +47,10 @@ class FileSystem { int RegisterSTFSContainerDevice(const std::string& path, const std::wstring& local_path); - int CreateSymbolicLink(const char* path, const char* target); - int DeleteSymbolicLink(const char* path); + int CreateSymbolicLink(const std::string& path, const std::string& target); + int DeleteSymbolicLink(const std::string& path); - Entry* ResolvePath(const char* path); + Entry* ResolvePath(const std::string& path); private: std::vector devices_; diff --git a/src/xenia/kernel/fs/gdfx.cc b/src/xenia/kernel/fs/gdfx.cc index 4e2f52abd..27a948e7a 100644 --- a/src/xenia/kernel/fs/gdfx.cc +++ b/src/xenia/kernel/fs/gdfx.cc @@ -40,7 +40,7 @@ GDFXEntry* GDFXEntry::GetChild(const char* name) { for (std::vector::iterator it = children.begin(); it != children.end(); ++it) { GDFXEntry* entry = *it; - if (xestrcasecmpa(entry->name.c_str(), name) == 0) { + if (strcasecmp(entry->name.c_str(), name) == 0) { return entry; } } diff --git a/src/xenia/kernel/fs/stfs.cc b/src/xenia/kernel/fs/stfs.cc index b7662f35e..b3e4a8788 100644 --- a/src/xenia/kernel/fs/stfs.cc +++ b/src/xenia/kernel/fs/stfs.cc @@ -108,7 +108,7 @@ STFSEntry* STFSEntry::GetChild(const char* name) { // TODO(benvanik): a faster search for (auto it = children.begin(); it != children.end(); ++it) { STFSEntry* entry = *it; - if (xestrcasecmpa(entry->name.c_str(), name) == 0) { + if (strcasecmp(entry->name.c_str(), name) == 0) { return entry; } } diff --git a/src/xenia/kernel/kernel_state.cc b/src/xenia/kernel/kernel_state.cc index ed3537af2..ab789a4aa 100644 --- a/src/xenia/kernel/kernel_state.cc +++ b/src/xenia/kernel/kernel_state.cc @@ -80,15 +80,15 @@ XModule* KernelState::GetModule(const char* name) { // NULL name = self. // TODO(benvanik): lookup module from caller address. return GetExecutableModule(); - } else if (xestrcasecmpa(name, "xam.xex") == 0) { + } else if (strcasecmp(name, "xam.xex") == 0) { auto module = emulator_->xam(); module->Retain(); return module; - } else if (xestrcasecmpa(name, "xboxkrnl.exe") == 0) { + } else if (strcasecmp(name, "xboxkrnl.exe") == 0) { auto module = emulator_->xboxkrnl(); module->Retain(); return module; - } else if (xestrcasecmpa(name, "kernel32.dll") == 0) { + } else if (strcasecmp(name, "kernel32.dll") == 0) { // Some games request this, for some reason. wtf. return NULL; } else { diff --git a/src/xenia/kernel/objects/xfile.h b/src/xenia/kernel/objects/xfile.h index ecb944413..49bec2e16 100644 --- a/src/xenia/kernel/objects/xfile.h +++ b/src/xenia/kernel/objects/xfile.h @@ -127,18 +127,19 @@ class XFile : public XObject { public: virtual ~XFile(); - virtual const char* path(void) const = 0; - virtual const char* absolute_path(void) const = 0; - virtual const char* name(void) const = 0; + virtual const std::string& path() const = 0; + virtual const std::string& absolute_path() const = 0; + virtual const std::string& name() const = 0; size_t position() const { return position_; } void set_position(size_t value) { position_ = value; } virtual X_STATUS QueryInfo(XFileInfo* out_info) = 0; - virtual X_STATUS QueryDirectory(XDirectoryInfo* out_info, - size_t length, const char* file_name, bool restart) = 0; + virtual X_STATUS QueryDirectory(XDirectoryInfo* out_info, size_t length, + const char* file_name, bool restart) = 0; virtual X_STATUS QueryVolume(XVolumeInfo* out_info, size_t length) = 0; - virtual X_STATUS QueryFileSystemAttributes(XFileSystemAttributeInfo* out_info, size_t length) = 0; + virtual X_STATUS QueryFileSystemAttributes(XFileSystemAttributeInfo* out_info, + size_t length) = 0; X_STATUS Read(void* buffer, size_t buffer_length, size_t byte_offset, size_t* out_bytes_read); diff --git a/src/xenia/kernel/objects/xthread.cc b/src/xenia/kernel/objects/xthread.cc index 70918dd8c..68f11c7f4 100644 --- a/src/xenia/kernel/objects/xthread.cc +++ b/src/xenia/kernel/objects/xthread.cc @@ -42,7 +42,6 @@ XThread::XThread(KernelState* kernel_state, thread_state_address_(0), thread_state_(0), event_(NULL), - name_(0), irql_(0) { creation_params_.stack_size = stack_size; creation_params_.xapi_thread_startup = xapi_thread_startup; @@ -95,9 +94,6 @@ XThread::~XThread() { if (thread_state_address_) { kernel_state()->memory()->HeapFree(thread_state_address_, 0); } - if (name_) { - xe_free(name_); - } if (thread_handle_) { // TODO(benvanik): platform kill @@ -149,35 +145,9 @@ void XThread::set_last_error(uint32_t error_code) { poly::store_and_swap(p + 0x160, error_code); } -void XThread::set_name(const char* name) { - if (name == name_) { - return; - } - if (name_) { - xe_free(name_); - } - name_ = xestrdupa(name); - -#if XE_PLATFORM_WIN32 - // Do the nasty set for us. - #pragma pack(push, 8) - typedef struct tagTHREADNAME_INFO { - DWORD dwType; // must be 0x1000 - LPCSTR szName; // pointer to name (in user addr space) - DWORD dwThreadID; // thread ID (-1=caller thread) - DWORD dwFlags; // reserved for future use, must be zero - } THREADNAME_INFO; - #pragma pack(pop) - THREADNAME_INFO info; - info.dwType = 0x1000; - info.szName = name_; - info.dwThreadID = ::GetThreadId(thread_handle_); - info.dwFlags = 0; - __try { - RaiseException(0x406D1388, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info); - } __except(EXCEPTION_CONTINUE_EXECUTION) { - } -#endif // WIN32 +void XThread::set_name(const std::string& name) { + name_ = name; + poly::threading::set_name(thread_handle_, name); } X_STATUS XThread::Create() { @@ -287,7 +257,7 @@ X_STATUS XThread::Exit(int exit_code) { static uint32_t __stdcall XThreadStartCallbackWin32(void* param) { XThread* thread = reinterpret_cast(param); - xe::Profiler::ThreadEnter(thread->name()); + xe::Profiler::ThreadEnter(thread->name().c_str()); current_thread_tls = thread; thread->Execute(); current_thread_tls = nullptr; diff --git a/src/xenia/kernel/objects/xthread.h b/src/xenia/kernel/objects/xthread.h index 06c93fc73..591a41b19 100644 --- a/src/xenia/kernel/objects/xthread.h +++ b/src/xenia/kernel/objects/xthread.h @@ -12,6 +12,7 @@ #include #include +#include #include @@ -44,8 +45,8 @@ public: uint32_t thread_id(); uint32_t last_error(); void set_last_error(uint32_t error_code); - const char* name() const { return name_; } - void set_name(const char* name); + const std::string& name() const { return name_; } + void set_name(const std::string& name); X_STATUS Create(); X_STATUS Exit(int exit_code); @@ -96,7 +97,7 @@ private: uint32_t thread_state_address_; cpu::XenonThreadState* thread_state_; - char* name_; + std::string name_; std::atomic irql_; std::mutex apc_lock_; diff --git a/src/xenia/kernel/objects/xuser_module.cc b/src/xenia/kernel/objects/xuser_module.cc index 7d566ce7c..c8f5c86e0 100644 --- a/src/xenia/kernel/objects/xuser_module.cc +++ b/src/xenia/kernel/objects/xuser_module.cc @@ -139,7 +139,7 @@ X_STATUS XUserModule::GetSection( auto header = xe_xex2_get_header(xex_); for (size_t n = 0; n < header->resource_info_count; n++) { auto& res = header->resource_infos[n]; - if (xestrcmpa(name, res.name) == 0) { + if (strcmp(name, res.name) == 0) { // Found! *out_section_data = res.address; *out_section_size = res.size; diff --git a/src/xenia/kernel/util/xex2.cc b/src/xenia/kernel/util/xex2.cc index 1f2f7b6ed..93ec53ca2 100644 --- a/src/xenia/kernel/util/xex2.cc +++ b/src/xenia/kernel/util/xex2.cc @@ -902,7 +902,7 @@ int xe_xex2_load_pe(xe_xex2_ref xex) { const PESection* xe_xex2_get_pe_section(xe_xex2_ref xex, const char* name) { for (std::vector::iterator it = xex->sections->begin(); it != xex->sections->end(); ++it) { - if (!xestrcmpa((*it)->name, name)) { + if (!strcmp((*it)->name, name)) { return *it; } } diff --git a/src/xenia/sources.gypi b/src/xenia/sources.gypi index db918c4e9..88a621e58 100644 --- a/src/xenia/sources.gypi +++ b/src/xenia/sources.gypi @@ -18,7 +18,6 @@ 'platform.h', 'profiling.cc', 'profiling.h', - 'string.cc', 'string.h', 'types.h', 'xbox.h', diff --git a/src/xenia/string.cc b/src/xenia/string.cc deleted file mode 100644 index 9d90f2952..000000000 --- a/src/xenia/string.cc +++ /dev/null @@ -1,33 +0,0 @@ -/** - ****************************************************************************** - * Xenia : Xbox 360 Emulator Research Project * - ****************************************************************************** - * Copyright 2013 Ben Vanik. All rights reserved. * - * Released under the BSD license - see LICENSE in the root for more details. * - ****************************************************************************** - */ - -#include - -#include - - -#if XE_PLATFORM_WIN32 - -char* xestrcasestra(const char* str, const char* substr) { - const size_t len = xestrlena(substr); - while (*str) { - if (!_strnicmp(str, substr, len)) { - break; - } - str++; - } - - if (*str) { - return (char*)str; - } else { - return NULL; - } -} - -#endif // WIN32 diff --git a/src/xenia/string.h b/src/xenia/string.h index 4f4c44642..f2f9b51cc 100644 --- a/src/xenia/string.h +++ b/src/xenia/string.h @@ -11,46 +11,32 @@ #define XENIA_STRING_H_ #include - +#include // NOTE: these differing implementations should behave pretty much the same. // If they don't, then they will have to be abstracted out. -#define XEInvalidSize ((size_t)(-1)) - #if !XE_LIKE_WIN32 int strncpy_s(char* dest, size_t destLength, const char* source, size_t count); #define strcpy_s(dest, destLength, source) !(strcpy(dest, source) == dest + (destLength*0)) #define strcat_s(dest, destLength, source) !(strcat(dest, source) == dest + (destLength*0)) #define _snprintf_s(dest, destLength, x, format, ...) snprintf(dest, destLength, format, ##__VA_ARGS__) #define xestrdupa strdup -#define xestrtoulla strtoull -#define xestrcasestra strcasestr #else -#define strcasecmp _stricmp #define xestrdupa _strdup -#define xestrtoullw _wcstoui64 -#define xestrtoulla _strtoui64 -char* xestrcasestra(const char* str, const char* substr); #endif // !WIN32 #define xestrlenw wcslen -#define xestrcmpw wcscmp -#define xestrcasecmpw _wcsicmp #define xestrdupw _wcsdup #define xestrchrw wcschr #define xestrrchrw wcsrchr -#define xestrcasestrw ?? #define xestrcpyw(dest, destLength, source) (wcscpy_s(dest, destLength, source) == 0) #define xestrncpyw(dest, destLength, source, count) (wcsncpy_s(dest, destLength, source, count) == 0) #define xestrcatw(dest, destLength, source) (wcscat_s(dest, destLength, source) == 0) #define xesnprintfw(buffer, bufferCount, format, ...) _snwprintf_s(buffer, bufferCount, (bufferCount) ? (bufferCount - 1) : 0, format, ##__VA_ARGS__) #define xevsnprintfw(buffer, bufferCount, format, args) _vsnwprintf_s(buffer, bufferCount, (bufferCount) ? (bufferCount - 1) : 0, format, args) -#define xevscprintfw(format, args) _vscwprintf(format, args) #define xestrlena strlen -#define xestrcmpa strcmp -#define xestrcasecmpa strcasecmp #define xestrchra strchr #define xestrrchra strrchr #define xestrcpya(dest, destLength, source) (strcpy_s(dest, destLength, source) == 0) @@ -65,13 +51,9 @@ typedef wchar_t xechar_t; #define XE_WCHAR 1 #define xestrlen xestrlenw -#define xestrcmp xestrcmpw -#define xestrcasecmp xestrcasecmpw #define xestrdup xestrdupw #define xestrchr xestrchrw #define xestrrchr xestrrchrw -#define xestrcasestr xestrcasestrw -#define xestrtoull xestrtoullw #define xestrcpy xestrcpyw #define xestrncpy xestrncpyw #define xestrcat xestrcatw @@ -86,13 +68,9 @@ typedef char xechar_t; #define XE_CHAR 1 #define xestrlen xestrlena -#define xestrcmp xestrcmpa -#define xestrcasecmp xestrcasecmpa #define xestrdup xestrdupa #define xestrchr xestrchra #define xestrrchr xestrrchra -#define xestrcasestr xestrcasestra -#define xestrtoull xestrtoulla #define xestrcpy xestrcpya #define xestrncpy xestrncpya #define xestrcat xestrcata