From f2a9fa3bf9e68ad0bd3e95d1afa553eeeb9cc094 Mon Sep 17 00:00:00 2001 From: Ben Vanik Date: Sat, 16 Aug 2014 17:58:33 -0700 Subject: [PATCH] XECOUNT to countof. --- src/alloy/backend/x64/x64_sequences.cc | 4 +- .../passes/register_allocation_pass.cc | 8 +-- src/alloy/frontend/ppc/ppc_disasm.cc | 6 +-- src/alloy/frontend/ppc/ppc_hir_builder.cc | 3 +- src/alloy/frontend/ppc/ppc_instr.cc | 4 +- src/alloy/frontend/ppc/ppc_instr_tables.h | 27 +++++----- src/poly/math.h | 5 ++ src/xenia/apu/audio_system.cc | 48 ++++++++--------- src/xenia/core/socket_win.cc | 7 ++- src/xenia/cpu/xenon_memory.cc | 6 +-- src/xenia/cpu/xex_module.cc | 35 +++++++------ src/xenia/export_resolver.cc | 3 +- src/xenia/gpu/d3d11/d3d11_geometry_shader.cc | 3 +- src/xenia/gpu/d3d11/d3d11_graphics_driver.cc | 33 ++++++------ src/xenia/gpu/d3d11/d3d11_graphics_system.cc | 3 +- src/xenia/gpu/d3d11/d3d11_profiler_display.cc | 52 ++++++++++++------- src/xenia/gpu/d3d11/d3d11_shader_resource.cc | 5 +- src/xenia/gpu/graphics_system.cc | 4 +- src/xenia/gpu/shader_resource.cc | 7 +-- .../kernel/fs/devices/disc_image_device.cc | 7 +-- .../fs/devices/stfs_container_device.cc | 7 +-- src/xenia/kernel/fs/gdfx.cc | 3 +- src/xenia/kernel/objects/xthread.cc | 31 +++++------ src/xenia/kernel/util/export_table_pre.inc | 2 +- src/xenia/kernel/util/xex2.cc | 7 +-- src/xenia/kernel/xam_module.cc | 3 +- src/xenia/kernel/xboxkrnl_module.cc | 8 +-- src/xenia/logging.cc | 5 +- src/xenia/types.h | 9 ---- tools/xenia-run/xenia-run.cc | 25 ++++----- 30 files changed, 195 insertions(+), 175 deletions(-) diff --git a/src/alloy/backend/x64/x64_sequences.cc b/src/alloy/backend/x64/x64_sequences.cc index f5709d0d8..fed77e3a4 100644 --- a/src/alloy/backend/x64/x64_sequences.cc +++ b/src/alloy/backend/x64/x64_sequences.cc @@ -1017,7 +1017,7 @@ EMITTER(LOAD_VECTOR_SHL_I8, MATCH(I, I8<>>)) { static void Emit(X64Emitter& e, const EmitArgType& i) { if (i.src1.is_constant) { auto sh = i.src1.constant(); - assert_true(sh < XECOUNT(lvsl_table)); + assert_true(sh < poly::countof(lvsl_table)); e.mov(e.rax, (uintptr_t)&lvsl_table[sh]); e.vmovaps(i.dest, e.ptr[e.rax]); } else { @@ -1069,7 +1069,7 @@ EMITTER(LOAD_VECTOR_SHR_I8, MATCH(I, I8<>>)) { static void Emit(X64Emitter& e, const EmitArgType& i) { if (i.src1.is_constant) { auto sh = i.src1.constant(); - assert_true(sh < XECOUNT(lvsr_table)); + assert_true(sh < poly::countof(lvsr_table)); e.mov(e.rax, (uintptr_t)&lvsr_table[sh]); e.vmovaps(i.dest, e.ptr[e.rax]); } else { diff --git a/src/alloy/compiler/passes/register_allocation_pass.cc b/src/alloy/compiler/passes/register_allocation_pass.cc index 2175765c2..cdfe910ad 100644 --- a/src/alloy/compiler/passes/register_allocation_pass.cc +++ b/src/alloy/compiler/passes/register_allocation_pass.cc @@ -55,7 +55,7 @@ RegisterAllocationPass::RegisterAllocationPass(const MachineInfo* machine_info) } RegisterAllocationPass::~RegisterAllocationPass() { - for (size_t n = 0; n < XECOUNT(usage_sets_.all_sets); n++) { + for (size_t n = 0; n < poly::countof(usage_sets_.all_sets); n++) { if (!usage_sets_.all_sets[n]) { break; } @@ -171,7 +171,7 @@ int RegisterAllocationPass::Run(HIRBuilder* builder) { void RegisterAllocationPass::DumpUsage(const char* name) { #if 0 fprintf(stdout, "\n%s:\n", name); - for (size_t i = 0; i < XECOUNT(usage_sets_.all_sets); ++i) { + for (size_t i = 0; i < poly::countof(usage_sets_.all_sets); ++i) { auto usage_set = usage_sets_.all_sets[i]; if (usage_set) { fprintf(stdout, "set %s:\n", usage_set->set->name); @@ -190,7 +190,7 @@ void RegisterAllocationPass::DumpUsage(const char* name) { } void RegisterAllocationPass::PrepareBlockState() { - for (size_t i = 0; i < XECOUNT(usage_sets_.all_sets); ++i) { + for (size_t i = 0; i < poly::countof(usage_sets_.all_sets); ++i) { auto usage_set = usage_sets_.all_sets[i]; if (usage_set) { usage_set->availability.set(); @@ -201,7 +201,7 @@ void RegisterAllocationPass::PrepareBlockState() { } void RegisterAllocationPass::AdvanceUses(Instr* instr) { - for (size_t i = 0; i < XECOUNT(usage_sets_.all_sets); ++i) { + for (size_t i = 0; i < poly::countof(usage_sets_.all_sets); ++i) { auto usage_set = usage_sets_.all_sets[i]; if (!usage_set) { break; diff --git a/src/alloy/frontend/ppc/ppc_disasm.cc b/src/alloy/frontend/ppc/ppc_disasm.cc index 94fd1325e..726e5486e 100644 --- a/src/alloy/frontend/ppc/ppc_disasm.cc +++ b/src/alloy/frontend/ppc/ppc_disasm.cc @@ -294,7 +294,7 @@ void Disasm_bcx(InstrData& i, StringBuffer* str) { } char s2[8] = {0}; if (!select_bits(i.B.BO, 4, 4)) { - xesnprintfa(s2, XECOUNT(s2), "cr%d, ", i.B.BI >> 2); + xesnprintfa(s2, poly::countof(s2), "cr%d, ", i.B.BI >> 2); } uint32_t nia; if (i.B.AA) { @@ -310,7 +310,7 @@ void Disasm_bcctrx(InstrData& i, StringBuffer* str) { const char* s0 = i.XL.LK ? "lr, " : ""; char s2[8] = {0}; if (!select_bits(i.XL.BO, 4, 4)) { - xesnprintfa(s2, XECOUNT(s2), "cr%d, ", i.XL.BI >> 2); + xesnprintfa(s2, poly::countof(s2), "cr%d, ", i.XL.BI >> 2); } str->Append("%-8s %s%sctr", i.type->name, s0, s2); // TODO(benvanik): resolve target name? @@ -328,7 +328,7 @@ void Disasm_bclrx(InstrData& i, StringBuffer* str) { } char s2[8] = {0}; if (!select_bits(i.XL.BO, 4, 4)) { - xesnprintfa(s2, XECOUNT(s2), "cr%d, ", i.XL.BI >> 2); + xesnprintfa(s2, poly::countof(s2), "cr%d, ", i.XL.BI >> 2); } str->Append("%-8s %s%s", name, s1, s2); } diff --git a/src/alloy/frontend/ppc/ppc_hir_builder.cc b/src/alloy/frontend/ppc/ppc_hir_builder.cc index 660ac1107..a426dd257 100644 --- a/src/alloy/frontend/ppc/ppc_hir_builder.cc +++ b/src/alloy/frontend/ppc/ppc_hir_builder.cc @@ -168,7 +168,8 @@ int PPCHIRBuilder::Emit(FunctionInfo* symbol_info, uint32_t flags) { void PPCHIRBuilder::AnnotateLabel(uint64_t address, Label* label) { char name_buffer[13]; - xesnprintfa(name_buffer, XECOUNT(name_buffer), "loc_%.8X", (uint32_t)address); + xesnprintfa(name_buffer, poly::countof(name_buffer), "loc_%.8X", + (uint32_t)address); label->name = (char*)arena_->Alloc(sizeof(name_buffer)); xe_copy_struct(label->name, name_buffer, sizeof(name_buffer)); } diff --git a/src/alloy/frontend/ppc/ppc_instr.cc b/src/alloy/frontend/ppc/ppc_instr.cc index 43b52d759..0410a1502 100644 --- a/src/alloy/frontend/ppc/ppc_instr.cc +++ b/src/alloy/frontend/ppc/ppc_instr.cc @@ -25,7 +25,7 @@ void InstrOperand::Dump(std::string& out_str) { } char buffer[32]; - const size_t max_count = XECOUNT(buffer); + const size_t max_count = poly::countof(buffer); switch (type) { case InstrOperand::kRegister: switch (reg.set) { @@ -373,7 +373,7 @@ InstrType* GetInstrType(uint32_t code) { // Slow lookup via linear scan. // This is primarily due to laziness. It could be made fast like the others. for (size_t n = 0; - n < XECOUNT(alloy::frontend::ppc::tables::instr_table_scan); n++) { + n < poly::countof(alloy::frontend::ppc::tables::instr_table_scan); n++) { slot = &(alloy::frontend::ppc::tables::instr_table_scan[n]); if (slot->opcode == (code & slot->opcode_mask)) { return slot; diff --git a/src/alloy/frontend/ppc/ppc_instr_tables.h b/src/alloy/frontend/ppc/ppc_instr_tables.h index 65f1ddc49..ccab23c49 100644 --- a/src/alloy/frontend/ppc/ppc_instr_tables.h +++ b/src/alloy/frontend/ppc/ppc_instr_tables.h @@ -13,6 +13,7 @@ #include #include +#include namespace alloy { namespace frontend { @@ -91,8 +92,8 @@ void Disasm_vspltisw(InstrData& i, StringBuffer* str); namespace tables { -static InstrType** instr_table_prep(InstrType* unprep, int unprep_count, int a, - int b) { +static InstrType** instr_table_prep(InstrType* unprep, size_t unprep_count, + int a, int b) { int prep_count = (int)pow(2.0, b - a + 1); InstrType** prep = (InstrType**)xe_calloc(prep_count * sizeof(void*)); for (int n = 0; n < unprep_count; n++) { @@ -102,7 +103,7 @@ static InstrType** instr_table_prep(InstrType* unprep, int unprep_count, int a, return prep; } -static InstrType** instr_table_prep_63(InstrType* unprep, int unprep_count, +static InstrType** instr_table_prep_63(InstrType* unprep, size_t unprep_count, int a, int b) { // Special handling for A format instructions. int prep_count = (int)pow(2.0, b - a + 1); @@ -362,7 +363,7 @@ static InstrType instr_table_4_unprep[] = { "Vector Logical XOR"), }; static InstrType** instr_table_4 = instr_table_prep( - instr_table_4_unprep, XECOUNT(instr_table_4_unprep), 0, 11); + instr_table_4_unprep, poly::countof(instr_table_4_unprep), 0, 11); // Opcode = 19, index = bits 10-1 (10) static InstrType instr_table_19_unprep[] = { @@ -382,7 +383,7 @@ static InstrType instr_table_19_unprep[] = { "Branch Conditional to Count Register"), }; static InstrType** instr_table_19 = instr_table_prep( - instr_table_19_unprep, XECOUNT(instr_table_19_unprep), 1, 10); + instr_table_19_unprep, poly::countof(instr_table_19_unprep), 1, 10); // Opcode = 30, index = bits 4-1 (4) static InstrType instr_table_30_unprep[] = { @@ -398,7 +399,7 @@ static InstrType instr_table_30_unprep[] = { // INSTRUCTION(rldcrx, 0x78000012, MDS, General , 0), }; static InstrType** instr_table_30 = instr_table_prep( - instr_table_30_unprep, XECOUNT(instr_table_30_unprep), 0, 0); + instr_table_30_unprep, poly::countof(instr_table_30_unprep), 0, 0); // Opcode = 31, index = bits 10-1 (10) static InstrType instr_table_31_unprep[] = { @@ -645,7 +646,7 @@ static InstrType instr_table_31_unprep[] = { "Store Vector Right Indexed LRU"), }; static InstrType** instr_table_31 = instr_table_prep( - instr_table_31_unprep, XECOUNT(instr_table_31_unprep), 1, 10); + instr_table_31_unprep, poly::countof(instr_table_31_unprep), 1, 10); // Opcode = 58, index = bits 1-0 (2) static InstrType instr_table_58_unprep[] = { @@ -656,7 +657,7 @@ static InstrType instr_table_58_unprep[] = { "Load Word Algebraic"), }; static InstrType** instr_table_58 = instr_table_prep( - instr_table_58_unprep, XECOUNT(instr_table_58_unprep), 0, 1); + instr_table_58_unprep, poly::countof(instr_table_58_unprep), 0, 1); // Opcode = 59, index = bits 5-1 (5) static InstrType instr_table_59_unprep[] = { @@ -682,7 +683,7 @@ static InstrType instr_table_59_unprep[] = { "Floating Negative Multiply-Add [Single]"), }; static InstrType** instr_table_59 = instr_table_prep( - instr_table_59_unprep, XECOUNT(instr_table_59_unprep), 1, 5); + instr_table_59_unprep, poly::countof(instr_table_59_unprep), 1, 5); // Opcode = 62, index = bits 1-0 (2) static InstrType instr_table_62_unprep[] = { @@ -691,7 +692,7 @@ static InstrType instr_table_62_unprep[] = { "Store Doubleword with Update"), }; static InstrType** instr_table_62 = instr_table_prep( - instr_table_62_unprep, XECOUNT(instr_table_62_unprep), 0, 1); + instr_table_62_unprep, poly::countof(instr_table_62_unprep), 0, 1); // Opcode = 63, index = bits 10-1 (10) // NOTE: the A format instructions need some special handling because @@ -751,7 +752,7 @@ static InstrType instr_table_63_unprep[] = { "Floating Convert From Integer Doubleword"), }; static InstrType** instr_table_63 = instr_table_prep_63( - instr_table_63_unprep, XECOUNT(instr_table_63_unprep), 1, 10); + instr_table_63_unprep, poly::countof(instr_table_63_unprep), 1, 10); // Main table, index = bits 31-26 (6) : (code >> 26) static InstrType instr_table_unprep[64] = { @@ -830,8 +831,8 @@ static InstrType instr_table_unprep[64] = { INSTRUCTION(stfdu, 0xDC000000, D, General, D_FRT_RA_I, "Store Floating-Point Double with Update"), }; -static InstrType** instr_table = - instr_table_prep(instr_table_unprep, XECOUNT(instr_table_unprep), 26, 31); +static InstrType** instr_table = instr_table_prep( + instr_table_unprep, poly::countof(instr_table_unprep), 26, 31); // Altivec instructions. // TODO(benvanik): build a table like the other instructions. diff --git a/src/poly/math.h b/src/poly/math.h index 69a8597d3..1b059c53d 100644 --- a/src/poly/math.h +++ b/src/poly/math.h @@ -20,6 +20,11 @@ namespace poly { +template +size_t countof(T (&arr)[N]) { + return std::extent::value; +} + // Rounds up the given value to the given alignment. template T align(T value, T alignment) { diff --git a/src/xenia/apu/audio_system.cc b/src/xenia/apu/audio_system.cc index c61e1fe84..bf73c5d90 100644 --- a/src/xenia/apu/audio_system.cc +++ b/src/xenia/apu/audio_system.cc @@ -10,20 +10,17 @@ #include #include -#include +#include #include #include #include - using namespace xe; using namespace xe::apu; using namespace xe::cpu; - -AudioSystem::AudioSystem(Emulator* emulator) : - emulator_(emulator), memory_(emulator->memory()), - running_(false) { +AudioSystem::AudioSystem(Emulator* emulator) + : emulator_(emulator), memory_(emulator->memory()), running_(false) { memset(clients_, 0, sizeof(clients_)); for (size_t i = 0; i < maximum_client_count_; ++i) { client_wait_handles_[i] = CreateEvent(NULL, TRUE, FALSE, NULL); @@ -42,19 +39,16 @@ X_STATUS AudioSystem::Setup() { // Let the processor know we want register access callbacks. emulator_->memory()->AddMappedRange( - 0x7FEA0000, - 0xFFFF0000, - 0x0000FFFF, - this, + 0x7FEA0000, 0xFFFF0000, 0x0000FFFF, this, reinterpret_cast(MMIOReadRegisterThunk), reinterpret_cast(MMIOWriteRegisterThunk)); // Setup worker thread state. This lets us make calls into guest code. - thread_state_ = new XenonThreadState( - emulator_->processor()->runtime(), 0, 16 * 1024, 0); + thread_state_ = + new XenonThreadState(emulator_->processor()->runtime(), 0, 16 * 1024, 0); thread_state_->set_name("Audio Worker"); - thread_block_ = (uint32_t)memory_->HeapAlloc( - 0, 2048, alloy::MEMORY_FLAG_ZERO); + thread_block_ = + (uint32_t)memory_->HeapAlloc(0, 2048, alloy::MEMORY_FLAG_ZERO); thread_state_->context()->r[13] = thread_block_; // Create worker thread. @@ -78,7 +72,8 @@ void AudioSystem::ThreadStart() { // Main run loop. while (running_) { - auto result = WaitForMultipleObjectsEx(maximum_client_count_, client_wait_handles_, FALSE, INFINITE, FALSE); + auto result = WaitForMultipleObjectsEx( + maximum_client_count_, client_wait_handles_, FALSE, INFINITE, FALSE); if (result == WAIT_FAILED) { DWORD err = GetLastError(); assert_always(); @@ -86,7 +81,8 @@ void AudioSystem::ThreadStart() { } size_t pumped = 0; - if (result >= WAIT_OBJECT_0 && result <= WAIT_OBJECT_0 + (maximum_client_count_ - 1)) { + if (result >= WAIT_OBJECT_0 && + result <= WAIT_OBJECT_0 + (maximum_client_count_ - 1)) { size_t index = result - WAIT_OBJECT_0; do { lock_.lock(); @@ -94,12 +90,15 @@ void AudioSystem::ThreadStart() { uint32_t client_callback_arg = clients_[index].wrapped_callback_arg; lock_.unlock(); if (client_callback) { - uint64_t args[] = { client_callback_arg }; - processor->Execute(thread_state_, client_callback, args, XECOUNT(args)); + uint64_t args[] = {client_callback_arg}; + processor->Execute(thread_state_, client_callback, args, + poly::countof(args)); } pumped++; index++; - } while (index < maximum_client_count_ && WaitForSingleObject(client_wait_handles_[index], 0) == WAIT_OBJECT_0); + } while (index < maximum_client_count_ && + WaitForSingleObject(client_wait_handles_[index], 0) == + WAIT_OBJECT_0); } if (!running_) { @@ -118,8 +117,7 @@ void AudioSystem::ThreadStart() { xe::Profiler::ThreadExit(); } -void AudioSystem::Initialize() { -} +void AudioSystem::Initialize() {} void AudioSystem::Shutdown() { running_ = false; @@ -129,8 +127,8 @@ void AudioSystem::Shutdown() { memory()->HeapFree(thread_block_, 0); } -X_STATUS AudioSystem::RegisterClient( - uint32_t callback, uint32_t callback_arg, size_t* out_index) { +X_STATUS AudioSystem::RegisterClient(uint32_t callback, uint32_t callback_arg, + size_t* out_index) { assert_true(unused_clients_.size()); std::lock_guard lock(lock_); @@ -151,7 +149,7 @@ X_STATUS AudioSystem::RegisterClient( auto mem = memory()->membase(); poly::store_and_swap(mem + ptr, callback_arg); - clients_[index] = { driver, callback, callback_arg, ptr }; + clients_[index] = {driver, callback, callback_arg, ptr}; if (out_index) { *out_index = index; @@ -176,7 +174,7 @@ void AudioSystem::UnregisterClient(size_t index) { std::lock_guard lock(lock_); assert_true(index < maximum_client_count_); DestroyDriver(clients_[index].driver); - clients_[index] = { 0 }; + clients_[index] = {0}; unused_clients_.push(index); } diff --git a/src/xenia/core/socket_win.cc b/src/xenia/core/socket_win.cc index 0adc79f9e..c95b231d8 100644 --- a/src/xenia/core/socket_win.cc +++ b/src/xenia/core/socket_win.cc @@ -9,6 +9,8 @@ #include +#include + #include #include #include @@ -121,7 +123,7 @@ int xe_socket_accept(socket_t socket, xe_socket_connection_t* out_client_info) { int client_ip = client_addr.sin_addr.s_addr; inet_ntop(AF_INET, &client_ip, - out_client_info->addr, XECOUNT(out_client_info->addr)); + out_client_info->addr, poly::countof(out_client_info->addr)); return 0; } @@ -250,7 +252,8 @@ int xe_socket_loop_poll(xe_socket_loop_t* loop, } // Poll. - int r = WSAPoll(loop->events, XECOUNT(loop->events), -1); + int r = WSAPoll(loop->events, static_cast(poly::countof(loop->events)), + -1); if (r == -1) { return 1; } diff --git a/src/xenia/cpu/xenon_memory.cc b/src/xenia/cpu/xenon_memory.cc index c8f73064c..9c95609b5 100644 --- a/src/xenia/cpu/xenon_memory.cc +++ b/src/xenia/cpu/xenon_memory.cc @@ -243,8 +243,8 @@ const static struct { 0xE0000000, 0xFFFFFFFF, 0x00000000, // - physical 4k pages }; int XenonMemory::MapViews(uint8_t* mapping_base) { - assert_true(XECOUNT(map_info) == XECOUNT(views_.all_views)); - for (size_t n = 0; n < XECOUNT(map_info); n++) { + assert_true(poly::countof(map_info) == poly::countof(views_.all_views)); + for (size_t n = 0; n < poly::countof(map_info); n++) { #if XE_PLATFORM_WIN32 views_.all_views[n] = reinterpret_cast(MapViewOfFileEx( mapping_, @@ -268,7 +268,7 @@ XECLEANUP: } void XenonMemory::UnmapViews() { - for (size_t n = 0; n < XECOUNT(views_.all_views); n++) { + for (size_t n = 0; n < poly::countof(views_.all_views); n++) { if (views_.all_views[n]) { #if XE_PLATFORM_WIN32 UnmapViewOfFile(views_.all_views[n]); diff --git a/src/xenia/cpu/xex_module.cc b/src/xenia/cpu/xex_module.cc index d2c0b0957..05bc443ef 100644 --- a/src/xenia/cpu/xex_module.cc +++ b/src/xenia/cpu/xex_module.cc @@ -11,6 +11,7 @@ #include +#include #include #include #include @@ -113,12 +114,12 @@ int XexModule::SetupLibraryImports(const xe_xex2_import_library_t* library) { if (kernel_export) { if (info->thunk_address) { - xesnprintfa(name, XECOUNT(name), "__imp_%s", kernel_export->name); + xesnprintfa(name, poly::countof(name), "__imp_%s", kernel_export->name); } else { - xesnprintfa(name, XECOUNT(name), "%s", kernel_export->name); + xesnprintfa(name, poly::countof(name), "%s", kernel_export->name); } } else { - xesnprintfa(name, XECOUNT(name), "__imp_%s_%.3X", library->name, + xesnprintfa(name, poly::countof(name), "__imp_%s_%.3X", library->name, info->ordinal); } @@ -158,10 +159,10 @@ int XexModule::SetupLibraryImports(const xe_xex2_import_library_t* library) { if (info->thunk_address) { if (kernel_export) { - xesnprintfa(name, XECOUNT(name), "%s", kernel_export->name); + xesnprintfa(name, poly::countof(name), "%s", kernel_export->name); } else { - xesnprintfa(name, XECOUNT(name), "__kernel_%s_%.3X", library->name, - info->ordinal); + xesnprintfa(name, poly::countof(name), "__kernel_%s_%.3X", + library->name, info->ordinal); } // On load we have something like this in memory: @@ -398,17 +399,17 @@ int XexModule::FindSaveRest() { if (!gplr_start) { gplr_start = memory_->SearchAligned( start_address, end_address, - gprlr_code_values, XECOUNT(gprlr_code_values)); + gprlr_code_values, poly::countof(gprlr_code_values)); } if (!fpr_start) { fpr_start = memory_->SearchAligned( start_address, end_address, - fpr_code_values, XECOUNT(fpr_code_values)); + fpr_code_values, poly::countof(fpr_code_values)); } if (!vmx_start) { vmx_start = memory_->SearchAligned( start_address, end_address, - vmx_code_values, XECOUNT(vmx_code_values)); + vmx_code_values, poly::countof(vmx_code_values)); } if (gplr_start && fpr_start && vmx_start) { break; @@ -422,7 +423,7 @@ int XexModule::FindSaveRest() { if (gplr_start) { uint64_t address = gplr_start; for (int n = 14; n <= 31; n++) { - xesnprintfa(name, XECOUNT(name), "__savegprlr_%d", n); + xesnprintfa(name, poly::countof(name), "__savegprlr_%d", n); FunctionInfo* symbol_info; DeclareFunction(address, &symbol_info); symbol_info->set_end_address(address + (31 - n) * 4 + 2 * 4); @@ -435,7 +436,7 @@ int XexModule::FindSaveRest() { } address = gplr_start + 20 * 4; for (int n = 14; n <= 31; n++) { - xesnprintfa(name, XECOUNT(name), "__restgprlr_%d", n); + xesnprintfa(name, poly::countof(name), "__restgprlr_%d", n); FunctionInfo* symbol_info; DeclareFunction(address, &symbol_info); symbol_info->set_end_address(address + (31 - n) * 4 + 3 * 4); @@ -450,7 +451,7 @@ int XexModule::FindSaveRest() { if (fpr_start) { uint64_t address = fpr_start; for (int n = 14; n <= 31; n++) { - xesnprintfa(name, XECOUNT(name), "__savefpr_%d", n); + xesnprintfa(name, poly::countof(name), "__savefpr_%d", n); FunctionInfo* symbol_info; DeclareFunction(address, &symbol_info); symbol_info->set_end_address(address + (31 - n) * 4 + 1 * 4); @@ -463,7 +464,7 @@ int XexModule::FindSaveRest() { } address = fpr_start + (18 * 4) + (1 * 4); for (int n = 14; n <= 31; n++) { - xesnprintfa(name, XECOUNT(name), "__restfpr_%d", n); + xesnprintfa(name, poly::countof(name), "__restfpr_%d", n); FunctionInfo* symbol_info; DeclareFunction(address, &symbol_info); symbol_info->set_end_address(address + (31 - n) * 4 + 1 * 4); @@ -483,7 +484,7 @@ int XexModule::FindSaveRest() { // 64-127 rest uint64_t address = vmx_start; for (int n = 14; n <= 31; n++) { - xesnprintfa(name, XECOUNT(name), "__savevmx_%d", n); + xesnprintfa(name, poly::countof(name), "__savevmx_%d", n); FunctionInfo* symbol_info; DeclareFunction(address, &symbol_info); symbol_info->set_name(name); @@ -495,7 +496,7 @@ int XexModule::FindSaveRest() { } address += 4; for (int n = 64; n <= 127; n++) { - xesnprintfa(name, XECOUNT(name), "__savevmx_%d", n); + xesnprintfa(name, poly::countof(name), "__savevmx_%d", n); FunctionInfo* symbol_info; DeclareFunction(address, &symbol_info); symbol_info->set_name(name); @@ -507,7 +508,7 @@ int XexModule::FindSaveRest() { } address = vmx_start + (18 * 2 * 4) + (1 * 4) + (64 * 2 * 4) + (1 * 4); for (int n = 14; n <= 31; n++) { - xesnprintfa(name, XECOUNT(name), "__restvmx_%d", n); + xesnprintfa(name, poly::countof(name), "__restvmx_%d", n); FunctionInfo* symbol_info; DeclareFunction(address, &symbol_info); symbol_info->set_name(name); @@ -519,7 +520,7 @@ int XexModule::FindSaveRest() { } address += 4; for (int n = 64; n <= 127; n++) { - xesnprintfa(name, XECOUNT(name), "__restvmx_%d", n); + xesnprintfa(name, poly::countof(name), "__restvmx_%d", n); FunctionInfo* symbol_info; DeclareFunction(address, &symbol_info); symbol_info->set_name(name); diff --git a/src/xenia/export_resolver.cc b/src/xenia/export_resolver.cc index ae7204f80..7f56d89a1 100644 --- a/src/xenia/export_resolver.cc +++ b/src/xenia/export_resolver.cc @@ -9,6 +9,7 @@ #include +#include using namespace xe; @@ -22,7 +23,7 @@ ExportResolver::~ExportResolver() { void ExportResolver::RegisterTable( const char* library_name, KernelExport* exports, const size_t count) { ExportTable table; - xestrcpya(table.name, XECOUNT(table.name), library_name); + xestrcpya(table.name, poly::countof(table.name), library_name); table.exports = exports; table.count = count; tables_.push_back(table); diff --git a/src/xenia/gpu/d3d11/d3d11_geometry_shader.cc b/src/xenia/gpu/d3d11/d3d11_geometry_shader.cc index e59f9823b..511d8dd93 100644 --- a/src/xenia/gpu/d3d11/d3d11_geometry_shader.cc +++ b/src/xenia/gpu/d3d11/d3d11_geometry_shader.cc @@ -9,6 +9,7 @@ #include +#include #include #include #include @@ -98,7 +99,7 @@ ID3D10Blob* D3D11GeometryShader::Compile(const char* shader_source) { } uint64_t hash = hash64(shader_source, strlen(shader_source)); // ? char file_name[poly::max_path]; - xesnprintfa(file_name, XECOUNT(file_name), + xesnprintfa(file_name, poly::countof(file_name), "%s/gen_%.16llX.gs", base_path, hash); diff --git a/src/xenia/gpu/d3d11/d3d11_graphics_driver.cc b/src/xenia/gpu/d3d11/d3d11_graphics_driver.cc index 31c38880a..1bdccf3ec 100644 --- a/src/xenia/gpu/d3d11/d3d11_graphics_driver.cc +++ b/src/xenia/gpu/d3d11/d3d11_graphics_driver.cc @@ -9,6 +9,7 @@ #include +#include #include #include #include @@ -249,7 +250,7 @@ int D3D11GraphicsDriver::UpdateState(const DrawCommand& command) { register_file_[XE_GPU_REG_RB_COLOR3_INFO].u32, }; ID3D11RenderTargetView* render_target_views[4] = { 0 }; - for (int n = 0; n < XECOUNT(color_info); n++) { + for (int n = 0; n < poly::countof(color_info); n++) { auto cb = render_targets_.color_buffers[n]; uint32_t color_format = (color_info[n] >> 16) & 0xF; switch (color_format) { @@ -484,7 +485,7 @@ int D3D11GraphicsDriver::SetupBlendState(const DrawCommand& command) { //blend_desc.AlphaToCoverageEnable = false; // ? blend_desc.IndependentBlendEnable = true; - for (int n = 0; n < XECOUNT(blend_control); n++) { + for (int n = 0; n < poly::countof(blend_control); n++) { // A2XX_RB_BLEND_CONTROL_COLOR_SRCBLEND blend_desc.RenderTarget[n].SrcBlend = blend_map[(blend_control[n] & 0x0000001F) >> 0]; // A2XX_RB_BLEND_CONTROL_COLOR_DESTBLEND @@ -642,13 +643,14 @@ int D3D11GraphicsDriver::SetupShaders(const DrawCommand& command) { // Set constant buffers. ID3D11Buffer* vs_constant_buffers[] = { - state_.constant_buffers.float_constants, - state_.constant_buffers.bool_constants, - state_.constant_buffers.loop_constants, - state_.constant_buffers.vs_consts, + state_.constant_buffers.float_constants, + state_.constant_buffers.bool_constants, + state_.constant_buffers.loop_constants, + state_.constant_buffers.vs_consts, }; - context_->VSSetConstantBuffers(0, XECOUNT(vs_constant_buffers), - vs_constant_buffers); + context_->VSSetConstantBuffers( + 0, static_cast(poly::countof(vs_constant_buffers)), + vs_constant_buffers); // Setup input layout (as encoded in vertex shader). auto vs = static_cast(command.vertex_shader); @@ -666,12 +668,13 @@ int D3D11GraphicsDriver::SetupShaders(const DrawCommand& command) { // Set constant buffers. ID3D11Buffer* vs_constant_buffers[] = { - state_.constant_buffers.float_constants, - state_.constant_buffers.bool_constants, - state_.constant_buffers.loop_constants, + state_.constant_buffers.float_constants, + state_.constant_buffers.bool_constants, + state_.constant_buffers.loop_constants, }; - context_->PSSetConstantBuffers(0, XECOUNT(vs_constant_buffers), - vs_constant_buffers); + context_->PSSetConstantBuffers( + 0, static_cast(poly::countof(vs_constant_buffers)), + vs_constant_buffers); } else { context_->PSSetShader(nullptr, nullptr, 0); return 1; @@ -824,7 +827,7 @@ int D3D11GraphicsDriver::RebuildRenderTargets(uint32_t width, SCOPE_profile_cpu_f("gpu"); // Remove old versions. - for (int n = 0; n < XECOUNT(render_targets_.color_buffers); n++) { + for (int n = 0; n < poly::countof(render_targets_.color_buffers); n++) { auto& cb = render_targets_.color_buffers[n]; SafeRelease(cb.buffer); SafeRelease(cb.color_view_8888); @@ -841,7 +844,7 @@ int D3D11GraphicsDriver::RebuildRenderTargets(uint32_t width, return 0; } - for (int n = 0; n < XECOUNT(render_targets_.color_buffers); n++) { + for (int n = 0; n < poly::countof(render_targets_.color_buffers); n++) { auto& cb = render_targets_.color_buffers[n]; D3D11_TEXTURE2D_DESC color_buffer_desc; xe_zero_struct(&color_buffer_desc, sizeof(color_buffer_desc)); diff --git a/src/xenia/gpu/d3d11/d3d11_graphics_system.cc b/src/xenia/gpu/d3d11/d3d11_graphics_system.cc index 9942ce0b6..f88977bac 100644 --- a/src/xenia/gpu/d3d11/d3d11_graphics_system.cc +++ b/src/xenia/gpu/d3d11/d3d11_graphics_system.cc @@ -9,6 +9,7 @@ #include +#include #include #include #include @@ -89,7 +90,7 @@ void D3D11GraphicsSystem::Initialize() { 0, // software driver HMODULE flags, feature_levels, - XECOUNT(feature_levels), + static_cast(poly::countof(feature_levels)), D3D11_SDK_VERSION, &device_, &actual_feature_level, diff --git a/src/xenia/gpu/d3d11/d3d11_profiler_display.cc b/src/xenia/gpu/d3d11/d3d11_profiler_display.cc index 9d0bcde7f..5b23ea3ec 100644 --- a/src/xenia/gpu/d3d11/d3d11_profiler_display.cc +++ b/src/xenia/gpu/d3d11/d3d11_profiler_display.cc @@ -11,6 +11,7 @@ #include +#include #include #include @@ -267,14 +268,23 @@ bool D3D11ProfilerDisplay::SetupShaders() { } D3D11_INPUT_ELEMENT_DESC element_descs[] = { - { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0, }, - { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0, }, - { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0, }, + { + "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, + D3D11_INPUT_PER_VERTEX_DATA, 0, + }, + { + "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, + D3D11_INPUT_PER_VERTEX_DATA, 0, + }, + { + "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, D3D11_APPEND_ALIGNED_ELEMENT, + D3D11_INPUT_PER_VERTEX_DATA, 0, + }, }; - hr = device->CreateInputLayout(element_descs, (UINT)XECOUNT(element_descs), - vs_code_blob->GetBufferPointer(), - vs_code_blob->GetBufferSize(), - &shader_layout_); + hr = device->CreateInputLayout( + element_descs, (UINT)poly::countof(element_descs), + vs_code_blob->GetBufferPointer(), vs_code_blob->GetBufferSize(), + &shader_layout_); if (FAILED(hr)) { XELOGE("Failed to create profiler input layout"); return false; @@ -298,7 +308,7 @@ bool D3D11ProfilerDisplay::SetupFont() { auto device = window_->device(); // Setup font lookup table. - for (uint32_t i = 0; i < XECOUNT(font_description_.char_offsets); ++i) { + for (uint32_t i = 0; i < poly::countof(font_description_.char_offsets); ++i) { font_description_.char_offsets[i] = 206; } for (uint32_t i = 'A'; i <= 'Z'; ++i) { @@ -468,12 +478,14 @@ void D3D11ProfilerDisplay::Begin() { font_sampler_state_, nullptr, }; - context->PSSetSamplers(0, XECOUNT(ps_samplers), ps_samplers); - ID3D11ShaderResourceView* ps_resources[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT] = { - font_texture_view_, - nullptr, - }; - context->PSSetShaderResources(0, XECOUNT(ps_resources), ps_resources); + context->PSSetSamplers(0, static_cast(poly::countof(ps_samplers)), + ps_samplers); + ID3D11ShaderResourceView* + ps_resources[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT] = { + font_texture_view_, nullptr, + }; + context->PSSetShaderResources( + 0, static_cast(poly::countof(ps_resources)), ps_resources); context->IASetInputLayout(shader_layout_); } @@ -483,19 +495,23 @@ void D3D11ProfilerDisplay::End() { D3D11ProfilerDisplay::Vertex* D3D11ProfilerDisplay::AllocateVertices( D3D_PRIMITIVE_TOPOLOGY primitive, size_t count) { - if (draw_state_.vertex_index + count > XECOUNT(draw_state_.vertex_buffer)) { + if (draw_state_.vertex_index + count > + poly::countof(draw_state_.vertex_buffer)) { Flush(); } - assert_true(draw_state_.vertex_index + count <= XECOUNT(draw_state_.vertex_buffer)); + assert_true(draw_state_.vertex_index + count <= + poly::countof(draw_state_.vertex_buffer)); size_t head = draw_state_.vertex_index; draw_state_.vertex_index += count; if (draw_state_.command_index && - draw_state_.commands[draw_state_.command_index - 1].primitive == primitive) { + draw_state_.commands[draw_state_.command_index - 1].primitive == + primitive) { draw_state_.commands[draw_state_.command_index - 1].vertex_count += count; } else { - assert_true(draw_state_.command_index < XECOUNT(draw_state_.commands)); + assert_true(draw_state_.command_index < + poly::countof(draw_state_.commands)); draw_state_.commands[draw_state_.command_index].primitive = primitive; draw_state_.commands[draw_state_.command_index].vertex_count = count; ++draw_state_.command_index; diff --git a/src/xenia/gpu/d3d11/d3d11_shader_resource.cc b/src/xenia/gpu/d3d11/d3d11_shader_resource.cc index ed95ee39b..e8bb26956 100644 --- a/src/xenia/gpu/d3d11/d3d11_shader_resource.cc +++ b/src/xenia/gpu/d3d11/d3d11_shader_resource.cc @@ -9,6 +9,7 @@ #include +#include #include #include #include @@ -49,7 +50,7 @@ ID3D10Blob* D3D11ShaderCompile(XE_GPU_SHADER_TYPE type, } size_t hash = hash64(disasm_source, strlen(disasm_source)); // ? char file_name[poly::max_path]; - xesnprintfa(file_name, XECOUNT(file_name), + xesnprintfa(file_name, poly::countof(file_name), "%s/gen_%.16llX.%s", base_path, hash, @@ -105,7 +106,7 @@ D3D11VertexShaderResource::D3D11VertexShaderResource( D3D11VertexShaderResource::~D3D11VertexShaderResource() { SafeRelease(handle_); SafeRelease(input_layout_); - for (int i = 0; i < XECOUNT(geometry_shaders_); ++i) { + for (int i = 0; i < poly::countof(geometry_shaders_); ++i) { delete geometry_shaders_[i]; } xe_free(translated_src_); diff --git a/src/xenia/gpu/graphics_system.cc b/src/xenia/gpu/graphics_system.cc index 6ab0d14ff..93b69984e 100644 --- a/src/xenia/gpu/graphics_system.cc +++ b/src/xenia/gpu/graphics_system.cc @@ -9,7 +9,7 @@ #include -#include +#include #include #include #include @@ -199,5 +199,5 @@ void GraphicsSystem::DispatchInterruptCallback( } uint64_t args[] = { source, interrupt_callback_data_ }; processor_->ExecuteInterrupt( - cpu, interrupt_callback_, args, XECOUNT(args)); + cpu, interrupt_callback_, args, poly::countof(args)); } diff --git a/src/xenia/gpu/shader_resource.cc b/src/xenia/gpu/shader_resource.cc index 8f6458e41..42986bf74 100644 --- a/src/xenia/gpu/shader_resource.cc +++ b/src/xenia/gpu/shader_resource.cc @@ -9,6 +9,7 @@ #include +#include #include @@ -188,7 +189,7 @@ void ShaderResource::GatherVertexFetch(const instr_fetch_vtx_t* vtx) { auto& desc = inputs.descs[n]; auto& info = desc.info; if (desc.fetch_slot == fetch_slot) { - assert_true(info.element_count <= XECOUNT(info.elements)); + assert_true(info.element_count <= poly::countof(info.elements)); // It may not hold that all strides are equal, but I hope it does. assert_true(!vtx->stride || info.stride_words == vtx->stride); el = &info.elements[info.element_count++]; @@ -197,7 +198,7 @@ void ShaderResource::GatherVertexFetch(const instr_fetch_vtx_t* vtx) { } if (!el) { assert_not_zero(vtx->stride); - assert_true(inputs.count + 1 < XECOUNT(inputs.descs)); + assert_true(inputs.count + 1 < poly::countof(inputs.descs)); auto& desc = inputs.descs[inputs.count++]; desc.input_index = inputs.count - 1; desc.fetch_slot = fetch_slot; @@ -251,7 +252,7 @@ void ShaderResource::GatherVertexFetch(const instr_fetch_vtx_t* vtx) { void ShaderResource::GatherTextureFetch(const xenos::instr_fetch_tex_t* tex) { // TODO(benvanik): check dest_swiz to see if we are writing anything. - assert_true(sampler_inputs_.count + 1 < XECOUNT(sampler_inputs_.descs)); + assert_true(sampler_inputs_.count + 1 < poly::countof(sampler_inputs_.descs)); auto& input = sampler_inputs_.descs[sampler_inputs_.count++]; input.input_index = sampler_inputs_.count - 1; input.fetch_slot = tex->const_idx & 0xF; // ? diff --git a/src/xenia/kernel/fs/devices/disc_image_device.cc b/src/xenia/kernel/fs/devices/disc_image_device.cc index e7bfda041..0e282af49 100644 --- a/src/xenia/kernel/fs/devices/disc_image_device.cc +++ b/src/xenia/kernel/fs/devices/disc_image_device.cc @@ -9,6 +9,7 @@ #include +#include #include #include @@ -57,12 +58,12 @@ Entry* DiscImageDevice::ResolvePath(const char* path) { // Walk the path, one separator at a time. // We copy it into the buffer and shift it left over and over. char remaining[poly::max_path]; - xestrcpya(remaining, XECOUNT(remaining), path); + xestrcpya(remaining, poly::countof(remaining), path); while (remaining[0]) { char* next_slash = strchr(remaining, '\\'); if (next_slash == remaining) { // Leading slash - shift - xestrcpya(remaining, XECOUNT(remaining), remaining + 1); + xestrcpya(remaining, poly::countof(remaining), remaining + 1); continue; } @@ -82,7 +83,7 @@ Entry* DiscImageDevice::ResolvePath(const char* path) { if (!next_slash) { break; } - xestrcpya(remaining, XECOUNT(remaining), next_slash + 1); + xestrcpya(remaining, poly::countof(remaining), next_slash + 1); } Entry::Type type = gdfx_entry->attributes & X_FILE_ATTRIBUTE_DIRECTORY ? diff --git a/src/xenia/kernel/fs/devices/stfs_container_device.cc b/src/xenia/kernel/fs/devices/stfs_container_device.cc index a0f176df2..219d8bdee 100644 --- a/src/xenia/kernel/fs/devices/stfs_container_device.cc +++ b/src/xenia/kernel/fs/devices/stfs_container_device.cc @@ -9,6 +9,7 @@ #include +#include #include #include @@ -57,12 +58,12 @@ Entry* STFSContainerDevice::ResolvePath(const char* path) { // Walk the path, one separator at a time. // We copy it into the buffer and shift it left over and over. char remaining[poly::max_path]; - xestrcpya(remaining, XECOUNT(remaining), path); + xestrcpya(remaining, poly::countof(remaining), path); while (remaining[0]) { char* next_slash = strchr(remaining, '\\'); if (next_slash == remaining) { // Leading slash - shift - xestrcpya(remaining, XECOUNT(remaining), remaining + 1); + xestrcpya(remaining, poly::countof(remaining), remaining + 1); continue; } @@ -82,7 +83,7 @@ Entry* STFSContainerDevice::ResolvePath(const char* path) { if (!next_slash) { break; } - xestrcpya(remaining, XECOUNT(remaining), next_slash + 1); + xestrcpya(remaining, poly::countof(remaining), next_slash + 1); } Entry::Type type = stfs_entry->attributes & X_FILE_ATTRIBUTE_DIRECTORY ? diff --git a/src/xenia/kernel/fs/gdfx.cc b/src/xenia/kernel/fs/gdfx.cc index 27a948e7a..9f275ff33 100644 --- a/src/xenia/kernel/fs/gdfx.cc +++ b/src/xenia/kernel/fs/gdfx.cc @@ -11,6 +11,7 @@ #include +#include using namespace xe; using namespace xe::kernel; @@ -105,7 +106,7 @@ GDFX::Error GDFX::Verify(ParseState& state) { 0x00000000, 0x0000FB20, 0x00020600, 0x0FD90000, }; bool magic_found = false; - for (size_t n = 0; n < XECOUNT(likely_offsets); n++) { + for (size_t n = 0; n < poly::countof(likely_offsets); n++) { state.game_offset = likely_offsets[n]; if (VerifyMagic(state, state.game_offset + (32 * kXESectorSize))) { magic_found = true; diff --git a/src/xenia/kernel/objects/xthread.cc b/src/xenia/kernel/objects/xthread.cc index 68f11c7f4..646178c8a 100644 --- a/src/xenia/kernel/objects/xthread.cc +++ b/src/xenia/kernel/objects/xthread.cc @@ -219,7 +219,7 @@ X_STATUS XThread::Create() { } char thread_name[32]; - xesnprintfa(thread_name, XECOUNT(thread_name), "XThread%04X", handle()); + xesnprintfa(thread_name, poly::countof(thread_name), "XThread%04X", handle()); set_name(thread_name); uint32_t proc_mask = creation_params_.creation_flags >> 24; @@ -366,22 +366,19 @@ void XThread::Execute() { // If a XapiThreadStartup value is present, we use that as a trampoline. // Otherwise, we are a raw thread. if (creation_params_.xapi_thread_startup) { - uint64_t args[] = { - creation_params_.start_address, - creation_params_.start_context - }; - kernel_state()->processor()->Execute( - thread_state_, - creation_params_.xapi_thread_startup, args, XECOUNT(args)); + uint64_t args[] = {creation_params_.start_address, + creation_params_.start_context}; + kernel_state()->processor()->Execute(thread_state_, + creation_params_.xapi_thread_startup, + args, poly::countof(args)); } else { // Run user code. - uint64_t args[] = { - creation_params_.start_context - }; + uint64_t args[] = {creation_params_.start_context}; int exit_code = (int)kernel_state()->processor()->Execute( - thread_state_, - creation_params_.start_address, args, XECOUNT(args)); - // If we got here it means the execute completed without an exit being called. + thread_state_, creation_params_.start_address, args, + poly::countof(args)); + // If we got here it means the execute completed without an exit being + // called. // Treat the return code as an implicit exit code. Exit(exit_code); } @@ -459,7 +456,7 @@ void XThread::DeliverAPCs(void* data) { thread->scratch_address_ + 12, }; processor->ExecuteInterrupt( - 0, kernel_routine, kernel_args, XECOUNT(kernel_args)); + 0, kernel_routine, kernel_args, poly::countof(kernel_args)); normal_routine = poly::load_and_swap(scratch_ptr + 0); normal_context = poly::load_and_swap(scratch_ptr + 4); system_arg1 = poly::load_and_swap(scratch_ptr + 8); @@ -472,7 +469,7 @@ void XThread::DeliverAPCs(void* data) { // normal_routine(normal_context, system_arg1, system_arg2) uint64_t normal_args[] = { normal_context, system_arg1, system_arg2 }; processor->ExecuteInterrupt( - 0, normal_routine, normal_args, XECOUNT(normal_args)); + 0, normal_routine, normal_args, poly::countof(normal_args)); thread->LockApc(); } } @@ -498,7 +495,7 @@ void XThread::RundownAPCs() { // rundown_routine(apc) uint64_t args[] = { apc_address }; kernel_state()->processor()->ExecuteInterrupt( - 0, rundown_routine, args, XECOUNT(args)); + 0, rundown_routine, args, poly::countof(args)); } } UnlockApc(); diff --git a/src/xenia/kernel/util/export_table_pre.inc b/src/xenia/kernel/util/export_table_pre.inc index d7c14a751..a03a01d4c 100644 --- a/src/xenia/kernel/util/export_table_pre.inc +++ b/src/xenia/kernel/util/export_table_pre.inc @@ -19,7 +19,7 @@ * #include * export_resolver_->RegisterTable( * "my_module.xex", - * my_module_export_table, XECOUNT(my_module_export_table)); + * my_module_export_table, poly::countof(my_module_export_table)); */ diff --git a/src/xenia/kernel/util/xex2.cc b/src/xenia/kernel/util/xex2.cc index 9ce9ddb86..23308e96b 100644 --- a/src/xenia/kernel/util/xex2.cc +++ b/src/xenia/kernel/util/xex2.cc @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -237,7 +238,7 @@ int xe_xex2_read_header(const uint8_t *addr, const size_t length, break; case XEX_HEADER_IMPORT_LIBRARIES: { - const size_t max_count = XECOUNT(header->import_libraries); + const size_t max_count = poly::countof(header->import_libraries); size_t count = poly::load_and_swap(pp + 0x08); assert_true(count <= max_count); if (count > max_count) { @@ -263,7 +264,7 @@ int xe_xex2_read_header(const uint8_t *addr, const size_t length, for (size_t i = 0, j = 0; i < string_table_size;) { assert_true(j <= 0xFF); if (j == name_index) { - xestrcpya(library->name, XECOUNT(library->name), + xestrcpya(library->name, poly::countof(library->name), string_table + i); break; } @@ -292,7 +293,7 @@ int xe_xex2_read_header(const uint8_t *addr, const size_t length, break; case XEX_HEADER_STATIC_LIBRARIES: { - const size_t max_count = XECOUNT(header->static_libraries); + const size_t max_count = poly::countof(header->static_libraries); size_t count = (opt_header->length - 4) / 16; assert_true(count <= max_count); if (count > max_count) { diff --git a/src/xenia/kernel/xam_module.cc b/src/xenia/kernel/xam_module.cc index b67bc2da7..51649628e 100644 --- a/src/xenia/kernel/xam_module.cc +++ b/src/xenia/kernel/xam_module.cc @@ -9,6 +9,7 @@ #include +#include #include #include #include @@ -27,7 +28,7 @@ XamModule::XamModule(Emulator* emulator, KernelState* kernel_state) : }; #include export_resolver_->RegisterTable( - "xam.xex", xam_export_table, XECOUNT(xam_export_table)); + "xam.xex", xam_export_table, poly::countof(xam_export_table)); // Register all exported functions. xam::RegisterContentExports(export_resolver_, kernel_state); diff --git a/src/xenia/kernel/xboxkrnl_module.cc b/src/xenia/kernel/xboxkrnl_module.cc index f9daefb54..f98e7a16d 100644 --- a/src/xenia/kernel/xboxkrnl_module.cc +++ b/src/xenia/kernel/xboxkrnl_module.cc @@ -10,7 +10,7 @@ #include #include - +#include #include #include #include @@ -34,8 +34,8 @@ XboxkrnlModule::XboxkrnlModule(Emulator* emulator, KernelState* kernel_state) : #include }; #include - export_resolver_->RegisterTable( - "xboxkrnl.exe", xboxkrnl_export_table, XECOUNT(xboxkrnl_export_table)); + export_resolver_->RegisterTable("xboxkrnl.exe", xboxkrnl_export_table, + poly::countof(xboxkrnl_export_table)); // Register all exported functions. xboxkrnl::RegisterAudioExports(export_resolver_, kernel_state); @@ -113,7 +113,7 @@ XboxkrnlModule::XboxkrnlModule(Emulator* emulator, KernelState* kernel_state) : pExLoadedCommandLine); char command_line[] = "\"default.xex\""; xe_copy_memory(mem + pExLoadedCommandLine, 1024, - command_line, XECOUNT(command_line) + 1); + command_line, poly::countof(command_line) + 1); // XboxKrnlVersion (8b) // Kernel version, looks like 2b.2b.2b.2b. diff --git a/src/xenia/logging.cc b/src/xenia/logging.cc index 0d181e26e..f585bddf1 100644 --- a/src/xenia/logging.cc +++ b/src/xenia/logging.cc @@ -13,6 +13,7 @@ #include #include +#include #include DEFINE_bool(fast_stdout, false, @@ -63,7 +64,7 @@ void xe_log_line(const char* file_path, const uint32_t line_number, char buffer[2048]; va_list args; va_start(args, fmt); - xe_format_log_line(buffer, XECOUNT(buffer), + xe_format_log_line(buffer, poly::countof(buffer), file_path, line_number, function_name, level_char, fmt, args); va_end(args); @@ -88,7 +89,7 @@ void xe_handle_fatal( char buffer[2048]; va_list args; va_start(args, fmt); - xe_format_log_line(buffer, XECOUNT(buffer), + xe_format_log_line(buffer, poly::countof(buffer), file_path, line_number, function_name, 'X', fmt, args); va_end(args); diff --git a/src/xenia/types.h b/src/xenia/types.h index 3d4209a24..41aea7c3b 100644 --- a/src/xenia/types.h +++ b/src/xenia/types.h @@ -82,15 +82,6 @@ #endif // MSVC typedef XECACHEALIGN volatile void xe_aligned_void_t; -#if XE_COMPILER_MSVC -// http://msdn.microsoft.com/en-us/library/ms175773.aspx -#define XECOUNT(array) _countof(array) -#elif XE_COMPILER_GNUC -#define XECOUNT(array) (sizeof(array) / sizeof(__typeof__(array[0]))) -#else -#define XECOUNT(array) (sizeof(array) / sizeof(array[0])) -#endif // MSVC - #define XEFAIL() goto XECLEANUP #define XEEXPECT(expr) if (!(expr) ) { goto XECLEANUP; } #define XEEXPECTTRUE(expr) if (!(expr) ) { goto XECLEANUP; } diff --git a/tools/xenia-run/xenia-run.cc b/tools/xenia-run/xenia-run.cc index 51a6dcb3f..6c2c38802 100644 --- a/tools/xenia-run/xenia-run.cc +++ b/tools/xenia-run/xenia-run.cc @@ -17,13 +17,9 @@ using namespace xe; DEFINE_string(target, "", "Specifies the target .xex or .iso to execute."); int xenia_run(std::vector& args) { - int result_code = 1; - Profiler::Initialize(); Profiler::ThreadEnter("main"); - Emulator* emulator = NULL; - // Grab path from the flag or unnamed argument. if (!FLAGS_target.size() && args.size() < 2) { google::ShowUsageWithFlags("xenia-run"); @@ -45,15 +41,17 @@ int xenia_run(std::vector& args) { // Create platform abstraction layer. xe_pal_options_t pal_options; xe_zero_struct(&pal_options, sizeof(pal_options)); - XEEXPECTZERO(xe_pal_init(pal_options)); + if (xe_pal_init(pal_options)) { + XELOGE("Failed to initialize PAL"); + return 1; + } // Create the emulator. - emulator = new Emulator(L""); - XEEXPECTNOTNULL(emulator); + auto emulator = std::make_unique(L""); X_STATUS result = emulator->Setup(); if (XFAILED(result)) { XELOGE("Failed to setup emulator: %.8X", result); - XEFAIL(); + return 1; } // Launch based on file type. @@ -72,18 +70,13 @@ int xenia_run(std::vector& args) { } if (XFAILED(result)) { XELOGE("Failed to launch target: %.8X", result); - XEFAIL(); + return 1; } - result_code = 0; -XECLEANUP: - delete emulator; - if (result_code) { - XEFATAL("Failed to launch emulator: %d", result_code); - } + emulator.reset(); Profiler::Dump(); Profiler::Shutdown(); - return result_code; + return 0; } DEFINE_ENTRY_POINT(L"xenia-run", L"xenia-run some.xex", xenia_run);