diff --git a/src/alloy/arena.cc b/src/alloy/arena.cc index 65f953e33..d3a8992d8 100644 --- a/src/alloy/arena.cc +++ b/src/alloy/arena.cc @@ -12,7 +12,7 @@ namespace alloy { Arena::Arena(size_t chunk_size) - : chunk_size_(chunk_size), head_chunk_(NULL), active_chunk_(NULL) {} + : chunk_size_(chunk_size), head_chunk_(nullptr), active_chunk_(nullptr) {} Arena::~Arena() { Reset(); @@ -22,7 +22,7 @@ Arena::~Arena() { delete chunk; chunk = next; } - head_chunk_ = NULL; + head_chunk_ = nullptr; } void Arena::Reset() { @@ -86,7 +86,7 @@ void* Arena::CloneContents() { } Arena::Chunk::Chunk(size_t chunk_size) - : next(NULL), capacity(chunk_size), buffer(0), offset(0) { + : next(nullptr), capacity(chunk_size), buffer(0), offset(0) { buffer = (uint8_t*)xe_malloc(capacity); } diff --git a/src/alloy/arena.h b/src/alloy/arena.h index 862622deb..3dae80b8d 100644 --- a/src/alloy/arena.h +++ b/src/alloy/arena.h @@ -25,7 +25,7 @@ class Arena { void* Alloc(size_t size); template T* Alloc() { - return (T*)Alloc(sizeof(T)); + return reinterpret_cast(Alloc(sizeof(T))); } void* CloneContents(); diff --git a/src/alloy/backend/ivm/ivm_intcode.cc b/src/alloy/backend/ivm/ivm_intcode.cc index 839a729e4..cc9e81d73 100644 --- a/src/alloy/backend/ivm/ivm_intcode.cc +++ b/src/alloy/backend/ivm/ivm_intcode.cc @@ -2657,7 +2657,7 @@ int Translate_MUL(TranslationContext& ctx, Instr* i) { } } -#if !XE_COMPILER_MSVC +#if XE_COMPILER_MSVC uint64_t Mul128(uint64_t xi_low, uint64_t xi_high, uint64_t yi_low, uint64_t yi_high) { // 128bit multiply, simplified for two input 64bit integers. diff --git a/src/alloy/delegate.h b/src/alloy/delegate.h index 1d0344f29..53d4eae72 100644 --- a/src/alloy/delegate.h +++ b/src/alloy/delegate.h @@ -23,14 +23,14 @@ namespace alloy { template class Delegate { public: - typedef std::function listener_t; + typedef std::function Listener; - void AddListener(listener_t const& listener) { + void AddListener(Listener const& listener) { std::lock_guard guard(lock_); listeners_.push_back(listener); } - void RemoveListener(listener_t const& listener) { + void RemoveListener(Listener const& listener) { std::lock_guard guard(lock_); for (auto it = listeners_.begin(); it != listeners_.end(); ++it) { if (it == listener) { @@ -54,7 +54,7 @@ class Delegate { private: std::mutex lock_; - std::vector listeners_; + std::vector listeners_; }; } // namespace alloy diff --git a/src/alloy/hir/hir_builder.cc b/src/alloy/hir/hir_builder.cc index 2a2b011e1..439a09e51 100644 --- a/src/alloy/hir/hir_builder.cc +++ b/src/alloy/hir/hir_builder.cc @@ -146,7 +146,7 @@ void HIRBuilder::DumpOp(StringBuffer* str, OpcodeSignatureType sig_type, case OPCODE_SIG_TYPE_S: if (true) { auto target = op->symbol_info; - str->Append(target->name() ? target->name() : ""); + str->Append(!target->name().empty() ? target->name() : ""); } break; case OPCODE_SIG_TYPE_V: diff --git a/src/alloy/memory.cc b/src/alloy/memory.cc index 59568ee49..c334c31a4 100644 --- a/src/alloy/memory.cc +++ b/src/alloy/memory.cc @@ -9,30 +9,21 @@ #include -#if !XE_LIKE_WIN32 -#include -#endif +#include namespace alloy { -Memory::Memory() : membase_(0), reserve_address_(0) { -// TODO(benvanik): move to poly. -#if XE_LIKE_WIN32 - SYSTEM_INFO si; - GetSystemInfo(&si); - system_page_size_ = si.dwPageSize; -#else - system_page_size_ = getpagesize(); -#endif +Memory::Memory() : membase_(nullptr), reserve_address_(0) { + system_page_size_ = poly::page_size(); } -Memory::~Memory() {} +Memory::~Memory() = default; int Memory::Initialize() { return 0; } void Memory::Zero(uint64_t address, size_t size) { uint8_t* p = membase_ + address; - XEIGNORE(xe_zero_memory(p, size, 0, size)); + memset(p, 0, size); } void Memory::Fill(uint64_t address, size_t size, uint8_t value) { @@ -42,15 +33,15 @@ void Memory::Fill(uint64_t address, size_t size, uint8_t value) { void Memory::Copy(uint64_t dest, uint64_t src, size_t size) { uint8_t* pdest = membase_ + dest; - uint8_t* psrc = membase_ + src; - XEIGNORE(xe_copy_memory(pdest, size, psrc, size)); + const uint8_t* psrc = membase_ + src; + memcpy(pdest, psrc, size); } uint64_t Memory::SearchAligned(uint64_t start, uint64_t end, const uint32_t* values, size_t value_count) { assert_true(start <= end); - const uint32_t* p = (const uint32_t*)(membase_ + start); - const uint32_t* pe = (const uint32_t*)(membase_ + end); + const uint32_t* p = reinterpret_cast(membase_ + start); + const uint32_t* pe = reinterpret_cast(membase_ + end); while (p != pe) { if (*p == values[0]) { const uint32_t* pc = p + 1; @@ -62,7 +53,7 @@ uint64_t Memory::SearchAligned(uint64_t start, uint64_t end, matched++; } if (matched == value_count) { - return (uint64_t)((uint8_t*)p - membase_); + return uint64_t(reinterpret_cast(p) - membase_); } } p++; diff --git a/src/alloy/runtime/debug_info.cc b/src/alloy/runtime/debug_info.cc index 266e57946..64022868c 100644 --- a/src/alloy/runtime/debug_info.cc +++ b/src/alloy/runtime/debug_info.cc @@ -13,12 +13,12 @@ namespace alloy { namespace runtime { DebugInfo::DebugInfo() - : source_disasm_(0), - raw_hir_disasm_(0), - hir_disasm_(0), - machine_code_disasm_(0), + : source_disasm_(nullptr), + raw_hir_disasm_(nullptr), + hir_disasm_(nullptr), + machine_code_disasm_(nullptr), source_map_count_(0), - source_map_(NULL) {} + source_map_(nullptr) {} DebugInfo::~DebugInfo() { xe_free(source_map_); @@ -44,7 +44,7 @@ SourceMapEntry* DebugInfo::LookupSourceOffset(uint64_t offset) { return entry; } } - return NULL; + return nullptr; } SourceMapEntry* DebugInfo::LookupHIROffset(uint64_t offset) { @@ -55,7 +55,7 @@ SourceMapEntry* DebugInfo::LookupHIROffset(uint64_t offset) { return entry; } } - return NULL; + return nullptr; } SourceMapEntry* DebugInfo::LookupCodeOffset(uint64_t offset) { @@ -66,7 +66,7 @@ SourceMapEntry* DebugInfo::LookupCodeOffset(uint64_t offset) { return entry; } } - return NULL; + return nullptr; } } // namespace runtime diff --git a/src/alloy/runtime/debugger.cc b/src/alloy/runtime/debugger.cc index fee71e67e..a5c129fc8 100644 --- a/src/alloy/runtime/debugger.cc +++ b/src/alloy/runtime/debugger.cc @@ -19,19 +19,18 @@ namespace runtime { Breakpoint::Breakpoint(Type type, uint64_t address) : type_(type), address_(address) {} -Breakpoint::~Breakpoint() {} +Breakpoint::~Breakpoint() = default; Debugger::Debugger(Runtime* runtime) : runtime_(runtime) {} -Debugger::~Debugger() {} +Debugger::~Debugger() = default; int Debugger::SuspendAllThreads(uint32_t timeout_ms) { std::lock_guard guard(threads_lock_); int result = 0; - for (auto it = threads_.begin(); it != threads_.end(); ++it) { - ThreadState* thread_state = it->second; - if (thread_state->Suspend(timeout_ms)) { + for (auto thread_state : threads_) { + if (thread_state.second->Suspend(timeout_ms)) { result = 1; } } @@ -57,9 +56,8 @@ int Debugger::ResumeAllThreads(bool force) { std::lock_guard guard(threads_lock_); int result = 0; - for (auto it = threads_.begin(); it != threads_.end(); ++it) { - ThreadState* thread_state = it->second; - if (thread_state->Resume(force)) { + for (auto thread_state : threads_) { + if (thread_state.second->Resume(force)) { result = 1; } } @@ -69,9 +67,8 @@ int Debugger::ResumeAllThreads(bool force) { void Debugger::ForEachThread(std::function callback) { std::lock_guard guard(threads_lock_); - for (auto it = threads_.begin(); it != threads_.end(); ++it) { - ThreadState* thread_state = it->second; - callback(thread_state); + for (auto thread_state : threads_) { + callback(thread_state.second); } } @@ -87,8 +84,7 @@ int Debugger::AddBreakpoint(Breakpoint* breakpoint) { auto fns = runtime_->FindFunctionsWithAddress(breakpoint->address()); // Add. - for (auto it = fns.begin(); it != fns.end(); ++it) { - Function* fn = *it; + for (auto fn : fns) { if (fn->AddBreakpoint(breakpoint)) { return 1; } @@ -122,8 +118,7 @@ int Debugger::RemoveBreakpoint(Breakpoint* breakpoint) { auto fns = runtime_->FindFunctionsWithAddress(breakpoint->address()); // Remove. - for (auto it = fns.begin(); it != fns.end(); ++it) { - Function* fn = *it; + for (auto fn : fns) { fn->RemoveBreakpoint(breakpoint); } @@ -181,8 +176,8 @@ void Debugger::OnFunctionDefined(FunctionInfo* symbol_info, if (breakpoints.size()) { // Breakpoints to add! - for (auto it = breakpoints.begin(); it != breakpoints.end(); ++it) { - function->AddBreakpoint(*it); + for (auto breakpoint : breakpoints) { + function->AddBreakpoint(breakpoint); } } } diff --git a/src/alloy/runtime/debugger.h b/src/alloy/runtime/debugger.h index bacb58f3f..fcac741d8 100644 --- a/src/alloy/runtime/debugger.h +++ b/src/alloy/runtime/debugger.h @@ -41,7 +41,7 @@ class Breakpoint { uint64_t address() const { return address_; } const char* id() const { return id_.c_str(); } - void set_id(const char* id) { id_ = id; } + void set_id(const char* id) { id_ = std::string(id); } private: Type type_; @@ -53,7 +53,7 @@ class Breakpoint { class DebugEvent { public: DebugEvent(Debugger* debugger) : debugger_(debugger) {} - virtual ~DebugEvent() {} + virtual ~DebugEvent() = default; Debugger* debugger() const { return debugger_; } protected: @@ -67,7 +67,7 @@ class BreakpointHitEvent : public DebugEvent { : DebugEvent(debugger), thread_state_(thread_state), breakpoint_(breakpoint) {} - virtual ~BreakpointHitEvent() {} + ~BreakpointHitEvent() override = default; ThreadState* thread_state() const { return thread_state_; } Breakpoint* breakpoint() const { return breakpoint_; } diff --git a/src/alloy/runtime/entry_table.cc b/src/alloy/runtime/entry_table.cc index 0277be312..df1a6f89b 100644 --- a/src/alloy/runtime/entry_table.cc +++ b/src/alloy/runtime/entry_table.cc @@ -26,11 +26,11 @@ EntryTable::~EntryTable() { Entry* EntryTable::Get(uint64_t address) { std::lock_guard guard(lock_); EntryMap::const_iterator it = map_.find(address); - Entry* entry = it != map_.end() ? it->second : NULL; + Entry* entry = it != map_.end() ? it->second : nullptr; if (entry) { // TODO(benvanik): wait if needed? if (entry->status != Entry::STATUS_READY) { - entry = NULL; + entry = nullptr; } } return entry; @@ -39,7 +39,7 @@ Entry* EntryTable::Get(uint64_t address) { Entry::Status EntryTable::GetOrCreate(uint64_t address, Entry** out_entry) { lock_.lock(); EntryMap::const_iterator it = map_.find(address); - Entry* entry = it != map_.end() ? it->second : NULL; + Entry* entry = it != map_.end() ? it->second : nullptr; Entry::Status status; if (entry) { // If we aren't ready yet spin and wait. @@ -72,8 +72,8 @@ std::vector EntryTable::FindWithAddress(uint64_t address) { SCOPE_profile_cpu_f("alloy"); std::lock_guard guard(lock_); std::vector fns; - for (auto it = map_.begin(); it != map_.end(); ++it) { - Entry* entry = it->second; + for (auto& it : map_) { + Entry* entry = it.second; if (address >= entry->address && address <= entry->end_address) { if (entry->status == Entry::STATUS_READY) { fns.push_back(entry->function); diff --git a/src/alloy/runtime/function.cc b/src/alloy/runtime/function.cc index 974b50eaa..7002cc86b 100644 --- a/src/alloy/runtime/function.cc +++ b/src/alloy/runtime/function.cc @@ -26,9 +26,10 @@ Function::~Function() = default; int Function::AddBreakpoint(Breakpoint* breakpoint) { std::lock_guard guard(lock_); bool found = false; - for (auto it = breakpoints_.begin(); it != breakpoints_.end(); ++it) { - if (*it == breakpoint) { + for (auto other : breakpoints_) { + if (other == breakpoint) { found = true; + break; } } if (!found) { @@ -54,9 +55,8 @@ int Function::RemoveBreakpoint(Breakpoint* breakpoint) { Breakpoint* Function::FindBreakpoint(uint64_t address) { std::lock_guard guard(lock_); - Breakpoint* result = NULL; - for (auto it = breakpoints_.begin(); it != breakpoints_.end(); ++it) { - Breakpoint* breakpoint = *it; + Breakpoint* result = nullptr; + for (auto breakpoint : breakpoints_) { if (breakpoint->address() == address) { result = breakpoint; break; diff --git a/src/alloy/runtime/module.cc b/src/alloy/runtime/module.cc index 24e27f00c..ff640088b 100644 --- a/src/alloy/runtime/module.cc +++ b/src/alloy/runtime/module.cc @@ -34,7 +34,7 @@ bool Module::ContainsAddress(uint64_t address) { return true; } SymbolInfo* Module::LookupSymbol(uint64_t address, bool wait) { lock_.lock(); SymbolMap::const_iterator it = map_.find(address); - SymbolInfo* symbol_info = it != map_.end() ? it->second : NULL; + SymbolInfo* symbol_info = it != map_.end() ? it->second : nullptr; if (symbol_info) { if (symbol_info->status() == SymbolInfo::STATUS_DECLARING) { // Some other thread is declaring the symbol - wait. @@ -47,7 +47,7 @@ SymbolInfo* Module::LookupSymbol(uint64_t address, bool wait) { } while (symbol_info->status() == SymbolInfo::STATUS_DECLARING); } else { // Immediate request, just return. - symbol_info = NULL; + symbol_info = nullptr; } } } @@ -58,10 +58,10 @@ SymbolInfo* Module::LookupSymbol(uint64_t address, bool wait) { SymbolInfo::Status Module::DeclareSymbol(SymbolInfo::Type type, uint64_t address, SymbolInfo** out_symbol_info) { - *out_symbol_info = NULL; + *out_symbol_info = nullptr; lock_.lock(); SymbolMap::const_iterator it = map_.find(address); - SymbolInfo* symbol_info = it != map_.end() ? it->second : NULL; + SymbolInfo* symbol_info = it != map_.end() ? it->second : nullptr; SymbolInfo::Status status; if (symbol_info) { // If we exist but are the wrong type, die. @@ -235,8 +235,8 @@ int Module::ReadMap(const char* file_name) { continue; } // Don't overwrite names we've set elsewhere. - if (!fn_info->name()) { - // If it's an mangled C++ name (?name@...) just use the name. + if (fn_info->name().empty()) { + // If it's a mangled C++ name (?name@...) just use the name. // TODO(benvanik): better demangling, or leave it to clients. /*if (name[0] == '?') { size_t at = name.find('@'); diff --git a/src/alloy/runtime/module.h b/src/alloy/runtime/module.h index e55b18375..e74899a63 100644 --- a/src/alloy/runtime/module.h +++ b/src/alloy/runtime/module.h @@ -32,7 +32,7 @@ class Module { Memory* memory() const { return memory_; } - virtual const char* name() const = 0; + virtual const std::string& name() const = 0; virtual bool ContainsAddress(uint64_t address); diff --git a/src/alloy/runtime/raw_module.cc b/src/alloy/runtime/raw_module.cc index 43df9f67a..0d0a4c6d7 100644 --- a/src/alloy/runtime/raw_module.cc +++ b/src/alloy/runtime/raw_module.cc @@ -13,17 +13,12 @@ namespace alloy { namespace runtime { RawModule::RawModule(Runtime* runtime) - : Module(runtime), - name_(0), - base_address_(0), - low_address_(0), - high_address_(0) {} + : Module(runtime), base_address_(0), low_address_(0), high_address_(0) {} RawModule::~RawModule() { if (base_address_) { memory_->HeapFree(base_address_, high_address_ - low_address_); } - xe_free(name_); } int RawModule::LoadFile(uint64_t base_address, const char* path) { @@ -47,8 +42,7 @@ int RawModule::LoadFile(uint64_t base_address, const char* path) { fclose(file); // Setup debug info. - const char* name = xestrrchra(path, XE_PATH_SEPARATOR) + 1; - name_ = xestrdupa(name); + name_ = std::string(xestrrchra(path, XE_PATH_SEPARATOR) + 1); // TODO(benvanik): debug info low_address_ = base_address; diff --git a/src/alloy/runtime/raw_module.h b/src/alloy/runtime/raw_module.h index dfedfb60c..8eb2c645d 100644 --- a/src/alloy/runtime/raw_module.h +++ b/src/alloy/runtime/raw_module.h @@ -10,6 +10,8 @@ #ifndef ALLOY_RUNTIME_RAW_MODULE_H_ #define ALLOY_RUNTIME_RAW_MODULE_H_ +#include + #include namespace alloy { @@ -18,16 +20,16 @@ namespace runtime { class RawModule : public Module { public: RawModule(Runtime* runtime); - virtual ~RawModule(); + ~RawModule() override; int LoadFile(uint64_t base_address, const char* path); - virtual const char* name() const { return name_; } + const std::string& name() const override { return name_; } - virtual bool ContainsAddress(uint64_t address); + bool ContainsAddress(uint64_t address) override; private: - char* name_; + std::string name_; uint64_t base_address_; uint64_t low_address_; uint64_t high_address_; diff --git a/src/alloy/runtime/runtime.cc b/src/alloy/runtime/runtime.cc index 66e514ae8..86fbbc319 100644 --- a/src/alloy/runtime/runtime.cc +++ b/src/alloy/runtime/runtime.cc @@ -26,8 +26,7 @@ namespace runtime { using alloy::backend::Backend; using alloy::frontend::Frontend; -Runtime::Runtime(Memory* memory) - : memory_(memory), debugger_(0), frontend_(0), backend_(0) { +Runtime::Runtime(Memory* memory) : memory_(memory) { tracing::Initialize(); } @@ -41,19 +40,20 @@ Runtime::~Runtime() { } } - delete frontend_; - delete backend_; - delete debugger_; + debugger_.reset(); + frontend_.reset(); + backend_.reset(); tracing::Flush(); } -int Runtime::Initialize(Frontend* frontend, Backend* backend) { +int Runtime::Initialize(std::unique_ptr frontend, + std::unique_ptr backend) { // Must be initialized by subclass before calling into this. assert_not_null(memory_); // Create debugger first. Other types hook up to it. - debugger_ = new Debugger(this); + debugger_.reset(new Debugger(this)); if (frontend_ || backend_) { return 1; @@ -62,23 +62,23 @@ int Runtime::Initialize(Frontend* frontend, Backend* backend) { if (!backend) { #if defined(ALLOY_HAS_X64_BACKEND) && ALLOY_HAS_X64_BACKEND if (FLAGS_runtime_backend == "x64") { - backend = new alloy::backend::x64::X64Backend(this); + backend.reset(new alloy::backend::x64::X64Backend(this)); } #endif // ALLOY_HAS_X64_BACKEND #if defined(ALLOY_HAS_IVM_BACKEND) && ALLOY_HAS_IVM_BACKEND if (FLAGS_runtime_backend == "ivm") { - backend = new alloy::backend::ivm::IVMBackend(this); + backend.reset(new alloy::backend::ivm::IVMBackend(this)); } #endif // ALLOY_HAS_IVM_BACKEND if (FLAGS_runtime_backend == "any") { #if defined(ALLOY_HAS_X64_BACKEND) && ALLOY_HAS_X64_BACKEND if (!backend) { - backend = new alloy::backend::x64::X64Backend(this); + backend.reset(new alloy::backend::x64::X64Backend(this)); } #endif // ALLOY_HAS_X64_BACKEND #if defined(ALLOY_HAS_IVM_BACKEND) && ALLOY_HAS_IVM_BACKEND if (!backend) { - backend = new alloy::backend::ivm::IVMBackend(this); + backend.reset(new alloy::backend::ivm::IVMBackend(this)); } #endif // ALLOY_HAS_IVM_BACKEND } @@ -87,19 +87,20 @@ int Runtime::Initialize(Frontend* frontend, Backend* backend) { if (!backend) { return 1; } - backend_ = backend; - frontend_ = frontend; - int result = backend_->Initialize(); + int result = backend->Initialize(); if (result) { return result; } - result = frontend_->Initialize(); + result = frontend->Initialize(); if (result) { return result; } + backend_ = std::move(backend); + frontend_ = std::move(frontend); + return 0; } @@ -114,7 +115,7 @@ Module* Runtime::GetModule(const char* name) { Module* result = NULL; for (ModuleList::iterator it = modules_.begin(); it != modules_.end(); ++it) { Module* module = *it; - if (xestrcmpa(module->name(), name) == 0) { + if (module->name() == name) { result = module; break; } @@ -135,7 +136,7 @@ std::vector Runtime::FindFunctionsWithAddress(uint64_t address) { int Runtime::ResolveFunction(uint64_t address, Function** out_function) { SCOPE_profile_cpu_f("alloy"); - *out_function = NULL; + *out_function = nullptr; Entry* entry; Entry::Status status = entry_table_.GetOrCreate(address, &entry); if (status == Entry::STATUS_NEW) { @@ -170,12 +171,12 @@ int Runtime::LookupFunctionInfo(uint64_t address, FunctionInfo** out_symbol_info) { SCOPE_profile_cpu_f("alloy"); - *out_symbol_info = NULL; + *out_symbol_info = nullptr; // TODO(benvanik): fast reject invalid addresses/log errors. // Find the module that contains the address. - Module* code_module = NULL; + Module* code_module = nullptr; { std::lock_guard guard(modules_lock_); // TODO(benvanik): sort by code address (if contiguous) so can bsearch. @@ -203,7 +204,7 @@ int Runtime::LookupFunctionInfo(Module* module, uint64_t address, // Atomic create/lookup symbol in module. // If we get back the NEW flag we must declare it now. - FunctionInfo* symbol_info = NULL; + FunctionInfo* symbol_info = nullptr; SymbolInfo::Status symbol_status = module->DeclareFunction(address, &symbol_info); if (symbol_status == SymbolInfo::STATUS_NEW) { @@ -224,7 +225,7 @@ int Runtime::DemandFunction(FunctionInfo* symbol_info, Function** out_function) { SCOPE_profile_cpu_f("alloy"); - *out_function = NULL; + *out_function = nullptr; // Lock function for generation. If it's already being generated // by another thread this will block and return DECLARED. @@ -232,7 +233,7 @@ int Runtime::DemandFunction(FunctionInfo* symbol_info, SymbolInfo::Status symbol_status = module->DefineFunction(symbol_info); if (symbol_status == SymbolInfo::STATUS_NEW) { // Symbol is undefined, so define now. - Function* function = NULL; + Function* function = nullptr; int result = frontend_->DefineFunction(symbol_info, DEBUG_INFO_DEFAULT, &function); if (result) { diff --git a/src/alloy/runtime/runtime.h b/src/alloy/runtime/runtime.h index fc5b90347..97968a9b8 100644 --- a/src/alloy/runtime/runtime.h +++ b/src/alloy/runtime/runtime.h @@ -10,6 +10,7 @@ #ifndef ALLOY_RUNTIME_RUNTIME_H_ #define ALLOY_RUNTIME_RUNTIME_H_ +#include #include #include @@ -35,11 +36,12 @@ class Runtime { virtual ~Runtime(); Memory* memory() const { return memory_; } - Debugger* debugger() const { return debugger_; } - frontend::Frontend* frontend() const { return frontend_; } - backend::Backend* backend() const { return backend_; } + Debugger* debugger() const { return debugger_.get(); } + frontend::Frontend* frontend() const { return frontend_.get(); } + backend::Backend* backend() const { return backend_.get(); } - int Initialize(frontend::Frontend* frontend, backend::Backend* backend = 0); + int Initialize(std::unique_ptr frontend, + std::unique_ptr backend = 0); int AddModule(Module* module); Module* GetModule(const char* name); @@ -60,10 +62,10 @@ class Runtime { protected: Memory* memory_; - Debugger* debugger_; + std::unique_ptr debugger_; - frontend::Frontend* frontend_; - backend::Backend* backend_; + std::unique_ptr frontend_; + std::unique_ptr backend_; EntryTable entry_table_; std::mutex modules_lock_; diff --git a/src/alloy/runtime/symbol_info.cc b/src/alloy/runtime/symbol_info.cc index 2392cc452..b525bf841 100644 --- a/src/alloy/runtime/symbol_info.cc +++ b/src/alloy/runtime/symbol_info.cc @@ -17,30 +17,19 @@ SymbolInfo::SymbolInfo(Type type, Module* module, uint64_t address) module_(module), status_(STATUS_DEFINING), address_(address), - name_(0) {} + name_("") {} -SymbolInfo::~SymbolInfo() { - if (name_) { - xe_free(name_); - } -} - -void SymbolInfo::set_name(const char* name) { - if (name_) { - xe_free(name_); - } - name_ = xestrdupa(name); -} +SymbolInfo::~SymbolInfo() = default; FunctionInfo::FunctionInfo(Module* module, uint64_t address) : SymbolInfo(SymbolInfo::TYPE_FUNCTION, module, address), end_address_(0), behavior_(BEHAVIOR_DEFAULT), function_(0) { - xe_zero_struct(&extern_info_, sizeof(extern_info_)); + memset(&extern_info_, 0, sizeof(extern_info_)); } -FunctionInfo::~FunctionInfo() {} +FunctionInfo::~FunctionInfo() = default; void FunctionInfo::SetupExtern(ExternHandler handler, void* arg0, void* arg1) { behavior_ = BEHAVIOR_EXTERN; @@ -52,7 +41,7 @@ void FunctionInfo::SetupExtern(ExternHandler handler, void* arg0, void* arg1) { VariableInfo::VariableInfo(Module* module, uint64_t address) : SymbolInfo(SymbolInfo::TYPE_VARIABLE, module, address) {} -VariableInfo::~VariableInfo() {} +VariableInfo::~VariableInfo() = default; } // namespace runtime } // namespace alloy diff --git a/src/alloy/runtime/symbol_info.h b/src/alloy/runtime/symbol_info.h index e9a3b06f2..c8157105e 100644 --- a/src/alloy/runtime/symbol_info.h +++ b/src/alloy/runtime/symbol_info.h @@ -10,6 +10,8 @@ #ifndef ALLOY_RUNTIME_SYMBOL_INFO_H_ #define ALLOY_RUNTIME_SYMBOL_INFO_H_ +#include + #include namespace alloy { @@ -43,8 +45,8 @@ class SymbolInfo { void set_status(Status value) { status_ = value; } uint64_t address() const { return address_; } - 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& value) { name_ = value; } protected: Type type_; @@ -52,7 +54,7 @@ class SymbolInfo { Status status_; uint64_t address_; - char* name_; + std::string name_; }; class FunctionInfo : public SymbolInfo { diff --git a/src/alloy/runtime/thread_state.cc b/src/alloy/runtime/thread_state.cc index 549d7d2d3..c2469b3c5 100644 --- a/src/alloy/runtime/thread_state.cc +++ b/src/alloy/runtime/thread_state.cc @@ -14,13 +14,13 @@ namespace alloy { namespace runtime { -thread_local ThreadState* thread_state_ = NULL; +thread_local ThreadState* thread_state_ = nullptr; ThreadState::ThreadState(Runtime* runtime, uint32_t thread_id) : runtime_(runtime), memory_(runtime->memory()), thread_id_(thread_id), - name_(0), + name_(""), backend_data_(0), raw_context_(0) { if (thread_id_ == UINT_MAX) { @@ -37,21 +37,8 @@ ThreadState::~ThreadState() { runtime_->backend()->FreeThreadData(backend_data_); } if (thread_state_ == this) { - thread_state_ = NULL; + thread_state_ = nullptr; } - if (name_) { - xe_free(name_); - } -} - -void ThreadState::set_name(const char* value) { - if (value == name_) { - return; - } - if (name_) { - xe_free(name_); - } - name_ = xestrdupa(value); } void ThreadState::Bind(ThreadState* thread_state) { diff --git a/src/alloy/runtime/thread_state.h b/src/alloy/runtime/thread_state.h index 2d1e36b3f..be6655438 100644 --- a/src/alloy/runtime/thread_state.h +++ b/src/alloy/runtime/thread_state.h @@ -27,8 +27,8 @@ class ThreadState { Runtime* runtime() const { return runtime_; } Memory* memory() const { return memory_; } uint32_t thread_id() const { return thread_id_; } - const char* name() const { return name_; } - void set_name(const char* value); + const std::string& name() const { return name_; } + void set_name(const std::string& value) { name_ = value; } void* backend_data() const { return backend_data_; } void* raw_context() const { return raw_context_; } @@ -43,7 +43,7 @@ class ThreadState { Runtime* runtime_; Memory* memory_; uint32_t thread_id_; - char* name_; + std::string name_; void* backend_data_; void* raw_context_; }; diff --git a/src/alloy/string_buffer.cc b/src/alloy/string_buffer.cc index 887dbbf5f..ca667f997 100644 --- a/src/alloy/string_buffer.cc +++ b/src/alloy/string_buffer.cc @@ -11,20 +11,21 @@ namespace alloy { -StringBuffer::StringBuffer(size_t initial_capacity) - : buffer_(0), capacity_(0), offset_(0) { - capacity_ = MAX(initial_capacity, 1024); - buffer_ = (char*)xe_calloc(capacity_); - buffer_[0] = 0; +StringBuffer::StringBuffer(size_t initial_capacity) : offset_(0) { + buffer_.resize(MAX(initial_capacity, 1024)); } -StringBuffer::~StringBuffer() { xe_free(buffer_); } +StringBuffer::~StringBuffer() = default; void StringBuffer::Reset() { offset_ = 0; buffer_[0] = 0; } +void StringBuffer::Append(const std::string& value) { + Append(value.c_str()); +} + void StringBuffer::Append(const char* format, ...) { va_list args; va_start(args, format); @@ -34,12 +35,10 @@ void StringBuffer::Append(const char* format, ...) { void StringBuffer::AppendVarargs(const char* format, va_list args) { while (true) { - int len = - xevsnprintfa(buffer_ + offset_, capacity_ - offset_ - 1, format, args); + int len = vsnprintf(buffer_.data() + offset_, buffer_.size() - offset_ - 1, + format, args); if (len == -1) { - size_t old_capacity = capacity_; - capacity_ = capacity_ * 2; - buffer_ = (char*)xe_realloc(buffer_, old_capacity, capacity_); + buffer_.resize(buffer_.size() * 2); continue; } else { offset_ += len; @@ -50,18 +49,16 @@ void StringBuffer::AppendVarargs(const char* format, va_list args) { } void StringBuffer::AppendBytes(const uint8_t* buffer, size_t length) { - if (offset_ + length > capacity_) { - size_t old_capacity = capacity_; - capacity_ = MAX(capacity_ * 2, capacity_ + length); - buffer_ = (char*)xe_realloc(buffer_, old_capacity, capacity_); + if (offset_ + length > buffer_.size()) { + buffer_.resize(MAX(buffer_.size() * 2, buffer_.size() + length)); } - xe_copy_memory(buffer_ + offset_, capacity_ - offset_, buffer, length); + memcpy(buffer_.data() + offset_, buffer, length); offset_ += length; buffer_[offset_] = 0; } -const char* StringBuffer::GetString() const { return buffer_; } +const char* StringBuffer::GetString() const { return buffer_.data(); } -char* StringBuffer::ToString() { return xestrdupa(buffer_); } +char* StringBuffer::ToString() { return strdup(buffer_.data()); } } // namespace alloy diff --git a/src/alloy/string_buffer.h b/src/alloy/string_buffer.h index b14055d1d..ca553d343 100644 --- a/src/alloy/string_buffer.h +++ b/src/alloy/string_buffer.h @@ -10,6 +10,8 @@ #ifndef ALLOY_STRING_BUFFER_H_ #define ALLOY_STRING_BUFFER_H_ +#include + #include namespace alloy { @@ -23,6 +25,7 @@ class StringBuffer { void Reset(); + void Append(const std::string& value); void Append(const char* format, ...); void AppendVarargs(const char* format, va_list args); void AppendBytes(const uint8_t* buffer, size_t length); @@ -32,8 +35,7 @@ class StringBuffer { char* EncodeBase64(); private: - char* buffer_; - size_t capacity_; + std::vector buffer_; size_t offset_; }; diff --git a/src/alloy/tracing/channel.cc b/src/alloy/tracing/channel.cc index ea2616e52..46fca1f61 100644 --- a/src/alloy/tracing/channel.cc +++ b/src/alloy/tracing/channel.cc @@ -12,9 +12,9 @@ namespace alloy { namespace tracing { -Channel::Channel() {} +Channel::Channel() = default; -Channel::~Channel() {} +Channel::~Channel() = default; } // namespace tracing } // namespace alloy diff --git a/src/alloy/tracing/channels/file_channel.cc b/src/alloy/tracing/channels/file_channel.cc index 89767d428..99bc8ba5e 100644 --- a/src/alloy/tracing/channels/file_channel.cc +++ b/src/alloy/tracing/channels/file_channel.cc @@ -13,17 +13,16 @@ namespace alloy { namespace tracing { namespace channels { -FileChannel::FileChannel(const char* path) { - path_ = xestrdupa(path); - file_ = fopen(path, "wb"); +FileChannel::FileChannel(const std::string& path) : path_(path) { + file_ = fopen(path_.c_str(), "wb"); } FileChannel::~FileChannel() { std::lock_guard guard(lock_); - fclose(file_); - file_ = 0; - free(path_); - path_ = 0; + if (file_) { + fclose(file_); + file_ = nullptr; + } } void FileChannel::Write(size_t buffer_count, size_t buffer_lengths[], diff --git a/src/alloy/tracing/channels/file_channel.h b/src/alloy/tracing/channels/file_channel.h index ba290dc1a..0b7df668c 100644 --- a/src/alloy/tracing/channels/file_channel.h +++ b/src/alloy/tracing/channels/file_channel.h @@ -22,16 +22,16 @@ namespace channels { class FileChannel : public Channel { public: - FileChannel(const char* path); - virtual ~FileChannel(); + FileChannel(const std::string& path); + ~FileChannel() override; - virtual void Write(size_t buffer_count, size_t buffer_lengths[], - const uint8_t* buffers[]); + void Write(size_t buffer_count, size_t buffer_lengths[], + const uint8_t* buffers[]) override; - virtual void Flush(); + void Flush() override; private: - char* path_; + std::string path_; FILE* file_; std::mutex lock_; }; diff --git a/src/alloy/tracing/tracer.cc b/src/alloy/tracing/tracer.cc index 01ee501d3..3c4f3a157 100644 --- a/src/alloy/tracing/tracer.cc +++ b/src/alloy/tracing/tracer.cc @@ -22,7 +22,7 @@ Tracer::Tracer(Channel* channel) : channel_(channel) { thread_id_ = ++next_thread_id_; } -Tracer::~Tracer() {} +Tracer::~Tracer() = default; void Tracer::WriteEvent(uint32_t event_type, size_t size, const uint8_t* data) { uint32_t header[] = { diff --git a/src/alloy/tracing/tracer.h b/src/alloy/tracing/tracer.h index 3d322f495..ba7faeab2 100644 --- a/src/alloy/tracing/tracer.h +++ b/src/alloy/tracing/tracer.h @@ -26,7 +26,7 @@ class Tracer { void set_thread_id(int value) { thread_id_ = value; } void WriteEvent(uint32_t event_type, size_t size = 0, - const uint8_t* data = 0); + const uint8_t* data = nullptr); private: Channel* channel_; diff --git a/src/alloy/tracing/tracing.cc b/src/alloy/tracing/tracing.cc index ace0d54a9..7c7ffa2bd 100644 --- a/src/alloy/tracing/tracing.cc +++ b/src/alloy/tracing/tracing.cc @@ -24,13 +24,14 @@ DEFINE_string(trace_file, "", "Traces to the given file path."); namespace alloy { namespace tracing { -Channel* shared_channel = NULL; -thread_local Tracer* thread_tracer = NULL; +Channel* shared_channel = nullptr; +thread_local Tracer* thread_tracer = nullptr; void CleanupTracing() { if (shared_channel) { alloy::tracing::WriteEvent(EventType::TraceEOF({})); shared_channel->Flush(); + shared_channel = nullptr; } } @@ -67,7 +68,7 @@ void Flush() { Tracer* GetThreadTracer() { if (!shared_channel) { - return NULL; + return nullptr; } if (!thread_tracer) { thread_tracer = new Tracer(shared_channel); diff --git a/src/alloy/tracing/tracing.h b/src/alloy/tracing/tracing.h index 4ca602bc0..bcfe68a9d 100644 --- a/src/alloy/tracing/tracing.h +++ b/src/alloy/tracing/tracing.h @@ -20,13 +20,14 @@ namespace tracing { class Channel; class Tracer; -bool Initialize(Channel* channel = 0); +bool Initialize(Channel* channel = nullptr); void Shutdown(); void Flush(); Tracer* GetThreadTracer(); -void WriteEvent(uint32_t event_type, size_t size = 0, const void* data = 0); +void WriteEvent(uint32_t event_type, size_t size = 0, + const void* data = nullptr); template void WriteEvent(const T& ev) { diff --git a/src/poly/memory.cc b/src/poly/memory.cc new file mode 100644 index 000000000..448899769 --- /dev/null +++ b/src/poly/memory.cc @@ -0,0 +1,32 @@ +/** + ****************************************************************************** + * 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 + +#if !XE_LIKE_WIN32 +#include +#endif // !XE_LIKE_WIN32 + +namespace poly { + +size_t page_size() { + static size_t value = 0; + if (!value) { +#if XE_LIKE_WIN32 + SYSTEM_INFO si; + GetSystemInfo(&si); + value = si.dwPageSize; +#else + value = getpagesize(); +#endif // XE_LIKE_WIN32 + } + return value; +} + +} // namespace poly diff --git a/src/poly/memory.h b/src/poly/memory.h new file mode 100644 index 000000000..01dbc608e --- /dev/null +++ b/src/poly/memory.h @@ -0,0 +1,22 @@ +/** + ****************************************************************************** + * 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. * + ****************************************************************************** + */ + +#ifndef POLY_MEMORY_H_ +#define POLY_MEMORY_H_ + +#include +#include + +namespace poly { + +size_t page_size(); + +} // namespace poly + +#endif // POLY_MEMORY_H_ diff --git a/src/poly/platform.h b/src/poly/platform.h index f5705e11b..019d69060 100644 --- a/src/poly/platform.h +++ b/src/poly/platform.h @@ -142,6 +142,7 @@ XE_CPU: 32BIT | 64BIT | BIGENDIAN | LITTLEENDIAN #undef min #undef max #undef Yield +#define strdup _strdup #endif // WINCE || WIN32 #if XE_PLATFORM_XBOX360 diff --git a/src/poly/sources.gypi b/src/poly/sources.gypi index f9a0fe960..a58841268 100644 --- a/src/poly/sources.gypi +++ b/src/poly/sources.gypi @@ -7,6 +7,8 @@ 'cxx_compat.h', 'math.cc', 'math.h', + 'memory.cc', + 'memory.h', 'platform.h', 'poly-private.h', 'poly.cc', diff --git a/src/xenia/cpu/processor.cc b/src/xenia/cpu/processor.cc index b263e1aad..0432fa294 100644 --- a/src/xenia/cpu/processor.cc +++ b/src/xenia/cpu/processor.cc @@ -75,12 +75,10 @@ int Processor::Setup() { return 1; } - Backend* backend = 0; - // Backend* backend = new alloy::backend::ivm::IVMBackend( - // runtime); - // Backend* backend = new alloy::backend::x64::X64Backend( - // runtime); - int result = runtime_->Initialize(backend); + std::unique_ptr backend; + // backend.reset(new alloy::backend::ivm::IVMBackend(runtime)); + // backend.reset(new alloy::backend::x64::X64Backend(runtime)); + int result = runtime_->Initialize(std::move(backend)); if (result) { return result; } diff --git a/src/xenia/cpu/xenon_runtime.cc b/src/xenia/cpu/xenon_runtime.cc index 0477bd926..9caa0841e 100644 --- a/src/xenia/cpu/xenon_runtime.cc +++ b/src/xenia/cpu/xenon_runtime.cc @@ -23,8 +23,8 @@ using namespace xe::cpu; XenonRuntime::XenonRuntime( alloy::Memory* memory, ExportResolver* export_resolver) : - export_resolver_(export_resolver), - Runtime(memory) { + Runtime(memory), + export_resolver_(export_resolver) { } XenonRuntime::~XenonRuntime() { @@ -32,11 +32,11 @@ XenonRuntime::~XenonRuntime() { })); } -int XenonRuntime::Initialize(backend::Backend* backend) { - PPCFrontend* frontend = new PPCFrontend(this); +int XenonRuntime::Initialize(std::unique_ptr backend) { + std::unique_ptr frontend(new PPCFrontend(this)); // TODO(benvanik): set options/etc. - int result = Runtime::Initialize(frontend, backend); + int result = Runtime::Initialize(std::move(frontend), std::move(backend)); if (result) { return result; } diff --git a/src/xenia/cpu/xenon_runtime.h b/src/xenia/cpu/xenon_runtime.h index 067677720..3b6c219b7 100644 --- a/src/xenia/cpu/xenon_runtime.h +++ b/src/xenia/cpu/xenon_runtime.h @@ -31,7 +31,7 @@ public: ExportResolver* export_resolver() const { return export_resolver_; } - virtual int Initialize(alloy::backend::Backend* backend = 0); + virtual int Initialize(std::unique_ptr backend = 0); private: ExportResolver* export_resolver_; diff --git a/src/xenia/cpu/xex_module.cc b/src/xenia/cpu/xex_module.cc index 2ff90c16a..9c63f6292 100644 --- a/src/xenia/cpu/xex_module.cc +++ b/src/xenia/cpu/xex_module.cc @@ -29,18 +29,16 @@ void UndefinedImport(PPCContext* ppc_state, void* arg0, void* arg1) { XexModule::XexModule( XenonRuntime* runtime) : runtime_(runtime), - name_(0), path_(0), xex_(0), + xex_(0), base_address_(0), low_address_(0), high_address_(0), Module(runtime) { } XexModule::~XexModule() { xe_xex2_release(xex_); - xe_free(name_); - xe_free(path_); } -int XexModule::Load(const char* name, const char* path, xe_xex2_ref xex) { +int XexModule::Load(const std::string& name, const std::string& path, xe_xex2_ref xex) { int result; xex_ = xe_xex2_retain(xex); @@ -79,8 +77,8 @@ int XexModule::Load(const char* name, const char* path, xe_xex2_ref xex) { } // Setup debug info. - name_ = xestrdupa(name); - path_ = xestrdupa(path); + name_ = std::string(name); + path_ = std::string(path); // TODO(benvanik): debug info // Load a specified module map and diff. diff --git a/src/xenia/cpu/xex_module.h b/src/xenia/cpu/xex_module.h index 68b0b5676..6e973246b 100644 --- a/src/xenia/cpu/xex_module.h +++ b/src/xenia/cpu/xex_module.h @@ -10,6 +10,8 @@ #ifndef XENIA_CPU_XEX_MODULE_H_ #define XENIA_CPU_XEX_MODULE_H_ +#include + #include #include #include @@ -28,11 +30,11 @@ public: xe_xex2_ref xex() const { return xex_; } - int Load(const char* name, const char* path, xe_xex2_ref xex); + int Load(const std::string& name, const std::string& path, xe_xex2_ref xex); - virtual const char* name() const { return name_; } + const std::string& name() const override { return name_; } - virtual bool ContainsAddress(uint64_t address); + bool ContainsAddress(uint64_t address) override; private: int SetupImports(xe_xex2_ref xex); @@ -41,13 +43,13 @@ private: private: XenonRuntime* runtime_; - char* name_; - char* path_; - xe_xex2_ref xex_; + std::string name_; + std::string path_; + xe_xex2_ref xex_; - uint64_t base_address_; - uint64_t low_address_; - uint64_t high_address_; + uint64_t base_address_; + uint64_t low_address_; + uint64_t high_address_; }; diff --git a/tools/alloy-sandbox/alloy-sandbox.cc b/tools/alloy-sandbox/alloy-sandbox.cc index 324e8ea19..75ec323dc 100644 --- a/tools/alloy-sandbox/alloy-sandbox.cc +++ b/tools/alloy-sandbox/alloy-sandbox.cc @@ -8,6 +8,8 @@ */ #include +#include +#include #include #include #include @@ -31,12 +33,10 @@ int alloy_sandbox(int argc, xechar_t** argv) { ExportResolver* export_resolver = new ExportResolver(); XenonRuntime* runtime = new XenonRuntime(memory, export_resolver); - Backend* backend = 0; - // Backend* backend = new alloy::backend::interpreter::InterpreterBackend( - // runtime); - // Backend* backend = new alloy::backend::x64::X64Backend( - // runtime); - runtime->Initialize(backend); + std::unique_ptr backend; + // backend.reset(new alloy::backend::ivm::IVMBackend(runtime)); + // backend.reset(new alloy::backend::x64::X64Backend(runtime)); + runtime->Initialize(std::move(backend)); RawModule* module = new RawModule(runtime); module->LoadFile(0x82000000, "test\\codegen\\instr_add.bin");