From 08b0226a1681adef38778b8278cd73ea53cdaa78 Mon Sep 17 00:00:00 2001 From: Ben Vanik Date: Thu, 21 Aug 2014 07:54:19 -0700 Subject: [PATCH] Finishing unique_ptr'ing and fixing file memory management (for now). --- src/alloy/backend/x64/x64_code_cache_win.cc | 7 +- src/alloy/runtime/debugger.h | 3 + src/xenia/apu/apu.cc | 2 +- src/xenia/core/run_loop_win.cc | 56 ++++++------- src/xenia/core/socket_win.cc | 78 +++++++++---------- src/xenia/emulator.cc | 6 +- src/xenia/emulator.h | 1 + src/xenia/gpu/d3d11/d3d11_buffer_resource.cc | 4 +- src/xenia/gpu/d3d11/d3d11_geometry_shader.cc | 7 +- src/xenia/gpu/d3d11/d3d11_graphics_driver.cc | 36 ++++----- src/xenia/gpu/d3d11/d3d11_graphics_system.h | 2 +- src/xenia/gpu/d3d11/d3d11_profiler_display.cc | 12 +-- .../gpu/d3d11/d3d11_sampler_state_resource.cc | 3 +- src/xenia/gpu/d3d11/d3d11_shader_resource.cc | 33 ++++---- src/xenia/gpu/d3d11/d3d11_texture_resource.cc | 9 +-- src/xenia/gpu/d3d11/d3d11_window.cc | 3 +- src/xenia/hid/hid.cc | 1 + src/xenia/hid/input_system.cc | 23 ++---- src/xenia/hid/input_system.h | 5 +- .../kernel/fs/devices/disc_image_file.cc | 2 +- src/xenia/kernel/fs/devices/host_path_file.cc | 5 +- .../kernel/fs/devices/stfs_container_file.cc | 2 +- src/xenia/kernel/fs/filesystem.cc | 10 +++ src/xenia/kernel/fs/filesystem.h | 2 + src/xenia/kernel/objects/xuser_module.cc | 3 +- src/xenia/kernel/xboxkrnl_io.cc | 12 +-- 26 files changed, 152 insertions(+), 175 deletions(-) diff --git a/src/alloy/backend/x64/x64_code_cache_win.cc b/src/alloy/backend/x64/x64_code_cache_win.cc index 1084b0508..50cac069f 100644 --- a/src/alloy/backend/x64/x64_code_cache_win.cc +++ b/src/alloy/backend/x64/x64_code_cache_win.cc @@ -94,7 +94,7 @@ void* X64CodeCache::PlaceCode(void* machine_code, size_t code_size, lock_.unlock(); // Copy code. - xe_copy_struct(final_address, machine_code, code_size); + memcpy(final_address, machine_code, code_size); // This isn't needed on x64 (probably), but is convention. FlushInstructionCache(GetCurrentProcess(), final_address, alloc_size); @@ -109,7 +109,7 @@ X64CodeChunk::X64CodeChunk(size_t chunk_size) fn_table_capacity = static_cast(poly::round_up(capacity / ESTIMATED_FN_SIZE, 16)); size_t table_size = fn_table_capacity * sizeof(RUNTIME_FUNCTION); - fn_table = (RUNTIME_FUNCTION*)xe_malloc(table_size); + fn_table = (RUNTIME_FUNCTION*)malloc(table_size); fn_table_count = 0; fn_table_handle = 0; RtlAddGrowableFunctionTable(&fn_table_handle, fn_table, fn_table_count, @@ -198,8 +198,7 @@ void X64CodeChunk::AddTableEntry(uint8_t* code, size_t code_size, RtlDeleteGrowableFunctionTable(fn_table_handle); size_t old_size = fn_table_capacity * sizeof(RUNTIME_FUNCTION); size_t new_size = old_size * 2; - auto new_table = - (RUNTIME_FUNCTION*)xe_realloc(fn_table, old_size, new_size); + auto new_table = (RUNTIME_FUNCTION*)realloc(fn_table, new_size); assert_not_null(new_table); if (!new_table) { return; diff --git a/src/alloy/runtime/debugger.h b/src/alloy/runtime/debugger.h index 971443605..789e894a8 100644 --- a/src/alloy/runtime/debugger.h +++ b/src/alloy/runtime/debugger.h @@ -95,6 +95,9 @@ class Debugger { void FindBreakpoints(uint64_t address, std::vector& out_breakpoints); + // TODO(benvanik): utility functions for modification (make function ignored, + // etc). + void OnThreadCreated(ThreadState* thread_state); void OnThreadDestroyed(ThreadState* thread_state); void OnFunctionDefined(FunctionInfo* symbol_info, Function* function); diff --git a/src/xenia/apu/apu.cc b/src/xenia/apu/apu.cc index 4bac1f732..e7ed8c292 100644 --- a/src/xenia/apu/apu.cc +++ b/src/xenia/apu/apu.cc @@ -22,7 +22,7 @@ std::unique_ptr xe::apu::CreateNop(Emulator* emulator) { #if XE_PLATFORM_WIN32 #include -AudioSystem* xe::apu::CreateXAudio2(Emulator* emulator) { +std::unique_ptr xe::apu::CreateXAudio2(Emulator* emulator) { return xe::apu::xaudio2::Create(emulator); } #endif // WIN32 diff --git a/src/xenia/core/run_loop_win.cc b/src/xenia/core/run_loop_win.cc index 164932f1b..43fdaaa8d 100644 --- a/src/xenia/core/run_loop_win.cc +++ b/src/xenia/core/run_loop_win.cc @@ -9,29 +9,23 @@ #include +typedef struct xe_run_loop { xe_ref_t ref; } xe_run_loop_t; -typedef struct xe_run_loop { - xe_ref_t ref; -} xe_run_loop_t; - - -#define WM_XE_RUN_LOOP_QUIT (WM_APP + 0x100) -#define WM_XE_RUN_LOOP_CALL (WM_APP + 0x101) +#define WM_XE_RUN_LOOP_QUIT (WM_APP + 0x100) +#define WM_XE_RUN_LOOP_CALL (WM_APP + 0x101) typedef struct xe_run_loop_call { - xe_run_loop_callback callback; - void* data; + xe_run_loop_callback callback; + void* data; } xe_run_loop_call_t; - xe_run_loop_ref xe_run_loop_create() { - xe_run_loop_ref run_loop = (xe_run_loop_ref)xe_calloc(sizeof(xe_run_loop_t)); + xe_run_loop_ref run_loop = (xe_run_loop_ref)calloc(1, sizeof(xe_run_loop_t)); xe_ref_init((xe_ref)run_loop); return run_loop; } -void xe_run_loop_dealloc(xe_run_loop_ref run_loop) { -} +void xe_run_loop_dealloc(xe_run_loop_ref run_loop) {} xe_run_loop_ref xe_run_loop_retain(xe_run_loop_ref run_loop) { xe_ref_retain((xe_ref)run_loop); @@ -48,19 +42,19 @@ int xe_run_loop_pump(xe_run_loop_ref run_loop) { TranslateMessage(&msg); DispatchMessage(&msg); switch (msg.message) { - case WM_XE_RUN_LOOP_CALL: - if (msg.wParam == (WPARAM)run_loop) { - xe_run_loop_call_t* call = (xe_run_loop_call_t*)msg.lParam; - call->callback(call->data); - xe_free(call); - } - break; - case WM_XE_RUN_LOOP_QUIT: - if (msg.wParam == (WPARAM)run_loop) { - // Done! - return 1; - } - break; + case WM_XE_RUN_LOOP_CALL: + if (msg.wParam == (WPARAM)run_loop) { + xe_run_loop_call_t* call = (xe_run_loop_call_t*)msg.lParam; + call->callback(call->data); + free(call); + } + break; + case WM_XE_RUN_LOOP_QUIT: + if (msg.wParam == (WPARAM)run_loop) { + // Done! + return 1; + } + break; } } return 0; @@ -70,11 +64,11 @@ void xe_run_loop_quit(xe_run_loop_ref run_loop) { PostMessage(NULL, WM_XE_RUN_LOOP_QUIT, (WPARAM)run_loop, 0); } -void xe_run_loop_call(xe_run_loop_ref run_loop, - xe_run_loop_callback callback, void* data) { +void xe_run_loop_call(xe_run_loop_ref run_loop, xe_run_loop_callback callback, + void* data) { xe_run_loop_call_t* call = - (xe_run_loop_call_t*)xe_calloc(sizeof(xe_run_loop_call_t)); - call->callback = callback; - call->data = data; + (xe_run_loop_call_t*)calloc(1, sizeof(xe_run_loop_call_t)); + call->callback = callback; + call->data = data; PostMessage(NULL, WM_XE_RUN_LOOP_CALL, (WPARAM)run_loop, (LPARAM)call); } diff --git a/src/xenia/core/socket_win.cc b/src/xenia/core/socket_win.cc index c95b231d8..7579ff5ad 100644 --- a/src/xenia/core/socket_win.cc +++ b/src/xenia/core/socket_win.cc @@ -16,10 +16,8 @@ #include #include - // TODO(benvanik): win32 calls - void xe_socket_init() { WSADATA wsa_data; int result = WSAStartup(MAKEWORD(2, 2), &wsa_data); @@ -41,12 +39,8 @@ void xe_socket_close(socket_t socket) { struct linger so_linger; so_linger.l_onoff = TRUE; so_linger.l_linger = 30; - setsockopt( - socket, - SOL_SOCKET, - SO_LINGER, - (const char*)&so_linger, - sizeof so_linger); + setsockopt(socket, SOL_SOCKET, SO_LINGER, (const char*)&so_linger, + sizeof so_linger); shutdown(socket, SD_SEND); closesocket(socket); } @@ -57,32 +51,32 @@ void xe_socket_set_keepalive(socket_t socket, bool value) { alive.keepalivetime = 7200000; alive.keepaliveinterval = 6000; DWORD bytes_returned; - WSAIoctl(socket, SIO_KEEPALIVE_VALS, &alive, sizeof(alive), - NULL, 0, &bytes_returned, NULL, NULL); + WSAIoctl(socket, SIO_KEEPALIVE_VALS, &alive, sizeof(alive), NULL, 0, + &bytes_returned, NULL, NULL); } void xe_socket_set_reuseaddr(socket_t socket, bool value) { int opt_value = value ? 1 : 0; - setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, - (const char*)&opt_value, sizeof(opt_value)); + setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt_value, + sizeof(opt_value)); } void xe_socket_set_nodelay(socket_t socket, bool value) { int opt_value = value ? 1 : 0; - setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, - (const char*)&opt_value, sizeof(opt_value)); + setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (const char*)&opt_value, + sizeof(opt_value)); } void xe_socket_set_nonblock(socket_t socket, bool value) { u_long mode = value ? 1 : 0; - ioctlsocket(socket, FIONBIO, &mode); + ioctlsocket(socket, FIONBIO, &mode); } int xe_socket_bind(socket_t socket, uint32_t port) { struct sockaddr_in socket_addr; - socket_addr.sin_family = AF_INET; + socket_addr.sin_family = AF_INET; socket_addr.sin_addr.s_addr = htonl(INADDR_ANY); - socket_addr.sin_port = htons(port); + socket_addr.sin_port = htons(port); int r = bind(socket, (struct sockaddr*)&socket_addr, sizeof(socket_addr)); if (r == SOCKET_ERROR) { return 1; @@ -92,9 +86,9 @@ int xe_socket_bind(socket_t socket, uint32_t port) { int xe_socket_bind_loopback(socket_t socket) { struct sockaddr_in socket_addr; - socket_addr.sin_family = AF_INET; + socket_addr.sin_family = AF_INET; socket_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); - socket_addr.sin_port = htons(0); + socket_addr.sin_port = htons(0); int r = bind(socket, (struct sockaddr*)&socket_addr, sizeof(socket_addr)); if (r == SOCKET_ERROR) { return 1; @@ -113,8 +107,8 @@ int xe_socket_listen(socket_t socket) { int xe_socket_accept(socket_t socket, xe_socket_connection_t* out_client_info) { struct sockaddr_in client_addr; int client_count = sizeof(client_addr); - socket_t client_socket_id = accept( - socket, (struct sockaddr*)&client_addr, &client_count); + socket_t client_socket_id = + accept(socket, (struct sockaddr*)&client_addr, &client_count); if (client_socket_id == INVALID_SOCKET) { return 1; } @@ -122,8 +116,8 @@ int xe_socket_accept(socket_t socket, xe_socket_connection_t* out_client_info) { out_client_info->socket = client_socket_id; int client_ip = client_addr.sin_addr.s_addr; - inet_ntop(AF_INET, &client_ip, - out_client_info->addr, poly::countof(out_client_info->addr)); + inet_ntop(AF_INET, &client_ip, out_client_info->addr, + poly::countof(out_client_info->addr)); return 0; } @@ -153,16 +147,16 @@ int64_t xe_socket_recv(socket_t socket, uint8_t* data, size_t length, int flags, } struct xe_socket_loop { - socket_t socket; + socket_t socket; - socket_t notify_rd_id; - socket_t notify_wr_id; + socket_t notify_rd_id; + socket_t notify_wr_id; WSAPOLLFD events[2]; - bool pending_queued_write; - bool pending_recv; - bool pending_send; + bool pending_queued_write; + bool pending_recv; + bool pending_send; }; namespace { @@ -188,8 +182,8 @@ int Win32SocketPair(socket_t sockets[2]) { sockaddr client_name; int client_name_len = sizeof(client_name); r = getsockname(client, &client_name, &client_name_len); - const char *pc = (const char*)&client_name; - const char *pn = (const char*)&listener_name; + const char* pc = (const char*)&client_name; + const char* pn = (const char*)&listener_name; for (size_t n = 0; n < sizeof(client_name); n++) { if (pc[n] != pn[n]) { closesocket(listener); @@ -208,28 +202,28 @@ int Win32SocketPair(socket_t sockets[2]) { } xe_socket_loop_t* xe_socket_loop_create(socket_t socket) { - xe_socket_loop_t* loop = (xe_socket_loop_t*)xe_calloc( - sizeof(xe_socket_loop_t)); + xe_socket_loop_t* loop = + (xe_socket_loop_t*)calloc(1, sizeof(xe_socket_loop_t)); loop->socket = socket; - socket_t notify_ids[2] = { 0, 0 }; + socket_t notify_ids[2] = {0, 0}; for (int retry = 0; retry < 5; retry++) { if (!Win32SocketPair(notify_ids)) { break; } } if (!notify_ids[0]) { - xe_free(loop); + free(loop); return NULL; } loop->notify_rd_id = notify_ids[0]; loop->notify_wr_id = notify_ids[1]; - loop->events[0].fd = socket; - loop->events[0].events = POLLIN; - loop->events[1].fd = loop->notify_rd_id; - loop->events[1].events = POLLIN; + loop->events[0].fd = socket; + loop->events[0].events = POLLIN; + loop->events[1].fd = loop->notify_rd_id; + loop->events[1].events = POLLIN; return loop; } @@ -237,11 +231,11 @@ xe_socket_loop_t* xe_socket_loop_create(socket_t socket) { void xe_socket_loop_destroy(xe_socket_loop_t* loop) { closesocket(loop->notify_rd_id); closesocket(loop->notify_wr_id); - xe_free(loop); + free(loop); } -int xe_socket_loop_poll(xe_socket_loop_t* loop, - bool check_read, bool check_write) { +int xe_socket_loop_poll(xe_socket_loop_t* loop, bool check_read, + bool check_write) { // Prep events object. loop->events[0].events = 0; if (check_read) { diff --git a/src/xenia/emulator.cc b/src/xenia/emulator.cc index 484571271..1ba106486 100644 --- a/src/xenia/emulator.cc +++ b/src/xenia/emulator.cc @@ -33,7 +33,7 @@ using namespace xe::kernel::fs; using namespace xe::ui; Emulator::Emulator(const std::wstring& command_line) - : command_line_(command_line) {} + : command_line_(command_line), main_window_(nullptr) {} Emulator::~Emulator() { // Note that we delete things in the reverse order they were initialized. @@ -134,8 +134,8 @@ X_STATUS Emulator::Setup() { kernel_state_ = std::make_unique(this); // HLE kernel modules. - xboxkrnl_ = std::make_unique(this, kernel_state_); - xam_ = std::make_unique(this, kernel_state_); + xboxkrnl_ = std::make_unique(this, kernel_state_.get()); + xam_ = std::make_unique(this, kernel_state_.get()); return result; } diff --git a/src/xenia/emulator.h b/src/xenia/emulator.h index dd07a2be3..8b82e09fa 100644 --- a/src/xenia/emulator.h +++ b/src/xenia/emulator.h @@ -24,6 +24,7 @@ class AudioSystem; } // namespace apu namespace cpu { class Processor; +class XenonThreadState; } // namespace cpu namespace gpu { class GraphicsSystem; diff --git a/src/xenia/gpu/d3d11/d3d11_buffer_resource.cc b/src/xenia/gpu/d3d11/d3d11_buffer_resource.cc index 9f05c7ec9..72282923f 100644 --- a/src/xenia/gpu/d3d11/d3d11_buffer_resource.cc +++ b/src/xenia/gpu/d3d11/d3d11_buffer_resource.cc @@ -34,7 +34,7 @@ D3D11IndexBufferResource::~D3D11IndexBufferResource() { int D3D11IndexBufferResource::CreateHandle() { D3D11_BUFFER_DESC buffer_desc; - xe_zero_struct(&buffer_desc, sizeof(buffer_desc)); + memset(&buffer_desc, 0, sizeof(buffer_desc)); buffer_desc.ByteWidth = static_cast(memory_range_.length); buffer_desc.Usage = D3D11_USAGE_DYNAMIC; buffer_desc.BindFlags = D3D11_BIND_INDEX_BUFFER; @@ -100,7 +100,7 @@ D3D11VertexBufferResource::~D3D11VertexBufferResource() { int D3D11VertexBufferResource::CreateHandle() { D3D11_BUFFER_DESC buffer_desc; - xe_zero_struct(&buffer_desc, sizeof(buffer_desc)); + memset(&buffer_desc, 0, sizeof(buffer_desc)); buffer_desc.ByteWidth = static_cast(memory_range_.length); buffer_desc.Usage = D3D11_USAGE_DYNAMIC; buffer_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; diff --git a/src/xenia/gpu/d3d11/d3d11_geometry_shader.cc b/src/xenia/gpu/d3d11/d3d11_geometry_shader.cc index 64214e31a..161b716f1 100644 --- a/src/xenia/gpu/d3d11/d3d11_geometry_shader.cc +++ b/src/xenia/gpu/d3d11/d3d11_geometry_shader.cc @@ -59,9 +59,8 @@ int D3D11GeometryShader::Prepare(D3D11VertexShaderResource* vertex_shader) { return 1; } byte_code_length = shader_blob->GetBufferSize(); - byte_code = xe_malloc(byte_code_length); - xe_copy_struct( - byte_code, shader_blob->GetBufferPointer(), byte_code_length); + byte_code = malloc(byte_code_length); + memcpy(byte_code, shader_blob->GetBufferPointer(), byte_code_length); SafeRelease(shader_blob); // Create shader. @@ -71,7 +70,7 @@ int D3D11GeometryShader::Prepare(D3D11VertexShaderResource* vertex_shader) { &handle_); if (FAILED(hr)) { XELOGE("D3D11: failed to create geometry shader"); - xe_free(byte_code); + free(byte_code); return 1; } diff --git a/src/xenia/gpu/d3d11/d3d11_graphics_driver.cc b/src/xenia/gpu/d3d11/d3d11_graphics_driver.cc index 1bdccf3ec..4a5bf6385 100644 --- a/src/xenia/gpu/d3d11/d3d11_graphics_driver.cc +++ b/src/xenia/gpu/d3d11/d3d11_graphics_driver.cc @@ -39,13 +39,12 @@ D3D11GraphicsDriver::D3D11GraphicsDriver( resource_cache_ = new D3D11ResourceCache(memory, device_, context_); - xe_zero_struct(&state_, sizeof(state_)); + memset(&state_, 0, sizeof(state_)); - xe_zero_struct(&render_targets_, sizeof(render_targets_)); + memset(&render_targets_, 0, sizeof(render_targets_)); HRESULT hr; - D3D11_BUFFER_DESC buffer_desc; - xe_zero_struct(&buffer_desc, sizeof(buffer_desc)); + D3D11_BUFFER_DESC buffer_desc = {0}; buffer_desc.Usage = D3D11_USAGE_DYNAMIC; buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; @@ -100,8 +99,7 @@ int D3D11GraphicsDriver::Initialize() { void D3D11GraphicsDriver::InitializeInvalidTexture() { // TODO(benvanik): pattern? - D3D11_TEXTURE2D_DESC texture_desc; - xe_zero_struct(&texture_desc, sizeof(texture_desc)); + D3D11_TEXTURE2D_DESC texture_desc = {0}; texture_desc.Width = 4; texture_desc.Height = 4; texture_desc.MipLevels = 1; @@ -131,8 +129,7 @@ void D3D11GraphicsDriver::InitializeInvalidTexture() { return; } - D3D11_SHADER_RESOURCE_VIEW_DESC texture_view_desc; - xe_zero_struct(&texture_view_desc, sizeof(texture_view_desc)); + D3D11_SHADER_RESOURCE_VIEW_DESC texture_view_desc = {}; texture_view_desc.Format = texture_desc.Format; texture_view_desc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D; texture_view_desc.Texture2D.MipLevels = 1; @@ -141,8 +138,7 @@ void D3D11GraphicsDriver::InitializeInvalidTexture() { texture, &texture_view_desc, &invalid_texture_view_); SafeRelease(texture); - D3D11_SAMPLER_DESC sampler_desc; - xe_zero_struct(&sampler_desc, sizeof(sampler_desc)); + D3D11_SAMPLER_DESC sampler_desc = {}; sampler_desc.Filter; sampler_desc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; sampler_desc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; @@ -396,8 +392,7 @@ int D3D11GraphicsDriver::SetupRasterizerState(const DrawCommand& command) { ID3D11RasterizerState* rasterizer_state = nullptr; auto it = rasterizer_state_cache_.find(key); if (it == rasterizer_state_cache_.end()) { - D3D11_RASTERIZER_DESC rasterizer_desc; - xe_zero_struct(&rasterizer_desc, sizeof(rasterizer_desc)); + D3D11_RASTERIZER_DESC rasterizer_desc = {}; rasterizer_desc.FillMode = D3D11_FILL_SOLID; // D3D11_FILL_WIREFRAME; switch (mode_control & 0x3) { case 0: @@ -480,8 +475,7 @@ int D3D11GraphicsDriver::SetupBlendState(const DrawCommand& command) { ID3D11BlendState* blend_state = nullptr; auto it = blend_state_cache_.find(key); if (it == blend_state_cache_.end()) { - D3D11_BLEND_DESC blend_desc; - xe_zero_struct(&blend_desc, sizeof(blend_desc)); + D3D11_BLEND_DESC blend_desc = {0}; //blend_desc.AlphaToCoverageEnable = false; // ? blend_desc.IndependentBlendEnable = true; @@ -557,8 +551,7 @@ int D3D11GraphicsDriver::SetupDepthStencilState(const DrawCommand& command) { ID3D11DepthStencilState* depth_stencil_state = nullptr; auto it = depth_stencil_state_cache_.find(key); if (it == depth_stencil_state_cache_.end()) { - D3D11_DEPTH_STENCIL_DESC depth_stencil_desc; - xe_zero_struct(&depth_stencil_desc, sizeof(depth_stencil_desc)); + D3D11_DEPTH_STENCIL_DESC depth_stencil_desc = {0}; // A2XX_RB_DEPTHCONTROL_BACKFACE_ENABLE // ? // A2XX_RB_DEPTHCONTROL_Z_ENABLE @@ -846,8 +839,7 @@ int D3D11GraphicsDriver::RebuildRenderTargets(uint32_t width, 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)); + D3D11_TEXTURE2D_DESC color_buffer_desc = {}; color_buffer_desc.Width = width; color_buffer_desc.Height = height; color_buffer_desc.MipLevels = 1; @@ -865,7 +857,7 @@ int D3D11GraphicsDriver::RebuildRenderTargets(uint32_t width, &color_buffer_desc, NULL, &cb.buffer); D3D11_RENDER_TARGET_VIEW_DESC render_target_view_desc; - xe_zero_struct(&render_target_view_desc, sizeof(render_target_view_desc)); + memset(&render_target_view_desc, 0, sizeof(render_target_view_desc)); render_target_view_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; render_target_view_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; // render_target_view_desc.Buffer ? @@ -875,8 +867,7 @@ int D3D11GraphicsDriver::RebuildRenderTargets(uint32_t width, &cb.color_view_8888); } - D3D11_TEXTURE2D_DESC depth_stencil_desc; - xe_zero_struct(&depth_stencil_desc, sizeof(depth_stencil_desc)); + D3D11_TEXTURE2D_DESC depth_stencil_desc = {}; depth_stencil_desc.Width = width; depth_stencil_desc.Height = height; depth_stencil_desc.MipLevels = 1; @@ -891,8 +882,7 @@ int D3D11GraphicsDriver::RebuildRenderTargets(uint32_t width, device_->CreateTexture2D( &depth_stencil_desc, NULL, &render_targets_.depth_buffer); - D3D11_DEPTH_STENCIL_VIEW_DESC depth_stencil_view_desc; - xe_zero_struct(&depth_stencil_view_desc, sizeof(depth_stencil_view_desc)); + D3D11_DEPTH_STENCIL_VIEW_DESC depth_stencil_view_desc = {}; depth_stencil_view_desc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; depth_stencil_view_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; depth_stencil_view_desc.Flags = 0; diff --git a/src/xenia/gpu/d3d11/d3d11_graphics_system.h b/src/xenia/gpu/d3d11/d3d11_graphics_system.h index bdefc2c21..02d83328d 100644 --- a/src/xenia/gpu/d3d11/d3d11_graphics_system.h +++ b/src/xenia/gpu/d3d11/d3d11_graphics_system.h @@ -23,7 +23,7 @@ namespace d3d11 { class D3D11Window; -GraphicsSystem* Create(Emulator* emulator); +std::unique_ptr Create(Emulator* emulator); class D3D11GraphicsSystem : public GraphicsSystem { diff --git a/src/xenia/gpu/d3d11/d3d11_profiler_display.cc b/src/xenia/gpu/d3d11/d3d11_profiler_display.cc index 5b23ea3ec..6bc31ea15 100644 --- a/src/xenia/gpu/d3d11/d3d11_profiler_display.cc +++ b/src/xenia/gpu/d3d11/d3d11_profiler_display.cc @@ -175,8 +175,7 @@ bool D3D11ProfilerDisplay::SetupState() { HRESULT hr; auto device = window_->device(); - D3D11_BLEND_DESC blend_desc; - xe_zero_struct(&blend_desc, sizeof(blend_desc)); + D3D11_BLEND_DESC blend_desc = {0}; blend_desc.RenderTarget[0].BlendEnable = true; blend_desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; blend_desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; @@ -188,8 +187,7 @@ bool D3D11ProfilerDisplay::SetupState() { hr = device->CreateBlendState(&blend_desc, &blend_state_); assert_true(SUCCEEDED(hr)); - D3D11_DEPTH_STENCIL_DESC depth_stencil_desc; - xe_zero_struct(&depth_stencil_desc, sizeof(depth_stencil_desc)); + D3D11_DEPTH_STENCIL_DESC depth_stencil_desc = {0}; depth_stencil_desc.DepthEnable = false; depth_stencil_desc.StencilEnable = false; depth_stencil_desc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO; @@ -371,8 +369,7 @@ bool D3D11ProfilerDisplay::SetupFont() { return false; } - D3D11_SHADER_RESOURCE_VIEW_DESC texture_view_desc; - xe_zero_struct(&texture_view_desc, sizeof(texture_view_desc)); + D3D11_SHADER_RESOURCE_VIEW_DESC texture_view_desc = {}; texture_view_desc.Format = texture_desc.Format; texture_view_desc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D; texture_view_desc.Texture2D.MipLevels = 1; @@ -385,8 +382,7 @@ bool D3D11ProfilerDisplay::SetupFont() { return false; } - D3D11_SAMPLER_DESC sampler_desc; - xe_zero_struct(&sampler_desc, sizeof(sampler_desc)); + D3D11_SAMPLER_DESC sampler_desc = {}; sampler_desc.Filter = D3D11_ENCODE_BASIC_FILTER( D3D11_FILTER_TYPE_POINT, D3D11_FILTER_TYPE_POINT, D3D11_FILTER_TYPE_POINT, false); diff --git a/src/xenia/gpu/d3d11/d3d11_sampler_state_resource.cc b/src/xenia/gpu/d3d11/d3d11_sampler_state_resource.cc index 1f9971ae4..3572f5718 100644 --- a/src/xenia/gpu/d3d11/d3d11_sampler_state_resource.cc +++ b/src/xenia/gpu/d3d11/d3d11_sampler_state_resource.cc @@ -35,8 +35,7 @@ int D3D11SamplerStateResource::Prepare() { return 0; } - D3D11_SAMPLER_DESC sampler_desc; - xe_zero_struct(&sampler_desc, sizeof(sampler_desc)); + D3D11_SAMPLER_DESC sampler_desc = {}; // MIN, MAG, MIP static const D3D11_FILTER filter_matrix[2][2][3] = { { diff --git a/src/xenia/gpu/d3d11/d3d11_shader_resource.cc b/src/xenia/gpu/d3d11/d3d11_shader_resource.cc index 8e749ce47..e352936a7 100644 --- a/src/xenia/gpu/d3d11/d3d11_shader_resource.cc +++ b/src/xenia/gpu/d3d11/d3d11_shader_resource.cc @@ -97,7 +97,7 @@ D3D11VertexShaderResource::D3D11VertexShaderResource( handle_(nullptr), input_layout_(nullptr), translated_src_(nullptr) { - xe_zero_struct(geometry_shaders_, sizeof(geometry_shaders_)); + memset(geometry_shaders_, 0, sizeof(geometry_shaders_)); } D3D11VertexShaderResource::~D3D11VertexShaderResource() { @@ -106,7 +106,7 @@ D3D11VertexShaderResource::~D3D11VertexShaderResource() { for (int i = 0; i < poly::countof(geometry_shaders_); ++i) { delete geometry_shaders_[i]; } - xe_free(translated_src_); + free(translated_src_); } int D3D11VertexShaderResource::Prepare( @@ -135,25 +135,22 @@ int D3D11VertexShaderResource::Prepare( return 1; } byte_code_length = shader_blob->GetBufferSize(); - byte_code = xe_malloc(byte_code_length); - xe_copy_struct( - byte_code, shader_blob->GetBufferPointer(), byte_code_length); + byte_code = malloc(byte_code_length); + memcpy(byte_code, shader_blob->GetBufferPointer(), byte_code_length); SafeRelease(shader_blob); // Create shader. HRESULT hr = resource_cache_->device()->CreateVertexShader( - byte_code, byte_code_length, - nullptr, - &handle_); + byte_code, byte_code_length, nullptr, &handle_); if (FAILED(hr)) { XELOGE("D3D11: failed to create vertex shader"); - xe_free(byte_code); + free(byte_code); return 1; } // Create input layout. ret = CreateInputLayout(byte_code, byte_code_length); - xe_free(byte_code); + free(byte_code); if (ret) { return 1; } @@ -174,9 +171,8 @@ int D3D11VertexShaderResource::CreateInputLayout(const void* byte_code, return 0; } - D3D11_INPUT_ELEMENT_DESC* element_descs = - (D3D11_INPUT_ELEMENT_DESC*)xe_alloca( - sizeof(D3D11_INPUT_ELEMENT_DESC) * element_count); + D3D11_INPUT_ELEMENT_DESC* element_descs = (D3D11_INPUT_ELEMENT_DESC*)alloca( + sizeof(D3D11_INPUT_ELEMENT_DESC) * element_count); uint32_t el_index = 0; for (uint32_t n = 0; n < inputs.count; n++) { const auto& input = inputs.descs[n]; @@ -327,7 +323,7 @@ D3D11PixelShaderResource::D3D11PixelShaderResource( D3D11PixelShaderResource::~D3D11PixelShaderResource() { SafeRelease(handle_); - xe_free(translated_src_); + free(translated_src_); } int D3D11PixelShaderResource::Prepare(const xe_gpu_program_cntl_t& program_cntl, @@ -358,9 +354,8 @@ int D3D11PixelShaderResource::Prepare(const xe_gpu_program_cntl_t& program_cntl, return 1; } byte_code_length = shader_blob->GetBufferSize(); - byte_code = xe_malloc(byte_code_length); - xe_copy_struct( - byte_code, shader_blob->GetBufferPointer(), byte_code_length); + byte_code = malloc(byte_code_length); + memcpy(byte_code, shader_blob->GetBufferPointer(), byte_code_length); SafeRelease(shader_blob); // Create shader. @@ -370,11 +365,11 @@ int D3D11PixelShaderResource::Prepare(const xe_gpu_program_cntl_t& program_cntl, &handle_); if (FAILED(hr)) { XELOGE("D3D11: failed to create pixel shader"); - xe_free(byte_code); + free(byte_code); return 1; } - xe_free(byte_code); + free(byte_code); is_prepared_ = true; return 0; } diff --git a/src/xenia/gpu/d3d11/d3d11_texture_resource.cc b/src/xenia/gpu/d3d11/d3d11_texture_resource.cc index 201d86f56..ebe57fdb1 100644 --- a/src/xenia/gpu/d3d11/d3d11_texture_resource.cc +++ b/src/xenia/gpu/d3d11/d3d11_texture_resource.cc @@ -37,8 +37,7 @@ D3D11TextureResource::~D3D11TextureResource() { int D3D11TextureResource::CreateHandle() { SCOPE_profile_cpu_f("gpu"); - D3D11_SHADER_RESOURCE_VIEW_DESC srv_desc; - xe_zero_struct(&srv_desc, sizeof(srv_desc)); + D3D11_SHADER_RESOURCE_VIEW_DESC srv_desc = {}; // TODO(benvanik): this may need to be typed on the fetch instruction (float/int/etc?) srv_desc.Format = info_.format; @@ -94,8 +93,7 @@ int D3D11TextureResource::CreateHandle() { int D3D11TextureResource::CreateHandle1D() { uint32_t width = 1 + info_.size_1d.width; - D3D11_TEXTURE1D_DESC texture_desc; - xe_zero_struct(&texture_desc, sizeof(texture_desc)); + D3D11_TEXTURE1D_DESC texture_desc = {}; texture_desc.Width = width; texture_desc.MipLevels = 1; texture_desc.ArraySize = 1; @@ -113,8 +111,7 @@ int D3D11TextureResource::CreateHandle1D() { } int D3D11TextureResource::CreateHandle2D() { - D3D11_TEXTURE2D_DESC texture_desc; - xe_zero_struct(&texture_desc, sizeof(texture_desc)); + D3D11_TEXTURE2D_DESC texture_desc = {}; texture_desc.Width = info_.size_2d.output_width; texture_desc.Height = info_.size_2d.output_height; texture_desc.MipLevels = 1; diff --git a/src/xenia/gpu/d3d11/d3d11_window.cc b/src/xenia/gpu/d3d11/d3d11_window.cc index 192e87225..eb1e50d5b 100644 --- a/src/xenia/gpu/d3d11/d3d11_window.cc +++ b/src/xenia/gpu/d3d11/d3d11_window.cc @@ -57,8 +57,7 @@ int D3D11Window::Initialize(const std::wstring& title, uint32_t width, } // Setup swap chain. - DXGI_SWAP_CHAIN_DESC desc; - xe_zero_struct(&desc, sizeof(desc)); + DXGI_SWAP_CHAIN_DESC desc = {}; desc.OutputWindow = handle(); desc.Windowed = TRUE; desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; diff --git a/src/xenia/hid/hid.cc b/src/xenia/hid/hid.cc index 50fed497c..a31e40fd8 100644 --- a/src/xenia/hid/hid.cc +++ b/src/xenia/hid/hid.cc @@ -9,6 +9,7 @@ #include #include +#include using namespace xe; using namespace xe::hid; diff --git a/src/xenia/hid/input_system.cc b/src/xenia/hid/input_system.cc index 1813e181e..0c0431118 100644 --- a/src/xenia/hid/input_system.cc +++ b/src/xenia/hid/input_system.cc @@ -19,12 +19,7 @@ namespace hid { InputSystem::InputSystem(Emulator* emulator) : emulator_(emulator), memory_(emulator->memory()) {} -InputSystem::~InputSystem() { - for (auto it = drivers_.begin(); it != drivers_.end(); ++it) { - InputDriver* driver = *it; - delete driver; - } -} +InputSystem::~InputSystem() = default; X_STATUS InputSystem::Setup() { processor_ = emulator_->processor(); @@ -32,14 +27,15 @@ X_STATUS InputSystem::Setup() { return X_STATUS_SUCCESS; } -void InputSystem::AddDriver(InputDriver* driver) { drivers_.push_back(driver); } +void InputSystem::AddDriver(std::unique_ptr driver) { + drivers_.push_back(std::move(driver)); +} X_RESULT InputSystem::GetCapabilities(uint32_t user_index, uint32_t flags, X_INPUT_CAPABILITIES* out_caps) { SCOPE_profile_cpu_f("hid"); - for (auto it = drivers_.begin(); it != drivers_.end(); ++it) { - InputDriver* driver = *it; + for (auto& driver : drivers_) { if (XSUCCEEDED(driver->GetCapabilities(user_index, flags, out_caps))) { return X_ERROR_SUCCESS; } @@ -50,8 +46,7 @@ X_RESULT InputSystem::GetCapabilities(uint32_t user_index, uint32_t flags, X_RESULT InputSystem::GetState(uint32_t user_index, X_INPUT_STATE* out_state) { SCOPE_profile_cpu_f("hid"); - for (auto it = drivers_.begin(); it != drivers_.end(); ++it) { - InputDriver* driver = *it; + for (auto& driver : drivers_) { if (driver->GetState(user_index, out_state) == X_ERROR_SUCCESS) { return X_ERROR_SUCCESS; } @@ -63,8 +58,7 @@ X_RESULT InputSystem::SetState(uint32_t user_index, X_INPUT_VIBRATION* vibration) { SCOPE_profile_cpu_f("hid"); - for (auto it = drivers_.begin(); it != drivers_.end(); ++it) { - InputDriver* driver = *it; + for (auto& driver : drivers_) { if (XSUCCEEDED(driver->SetState(user_index, vibration))) { return X_ERROR_SUCCESS; } @@ -76,8 +70,7 @@ X_RESULT InputSystem::GetKeystroke(uint32_t user_index, uint32_t flags, X_INPUT_KEYSTROKE* out_keystroke) { SCOPE_profile_cpu_f("hid"); - for (auto it = drivers_.begin(); it != drivers_.end(); ++it) { - InputDriver* driver = *it; + for (auto& driver : drivers_) { if (XSUCCEEDED(driver->GetKeystroke(user_index, flags, out_keystroke))) { return X_ERROR_SUCCESS; } diff --git a/src/xenia/hid/input_system.h b/src/xenia/hid/input_system.h index 309b1da02..98ff0e23a 100644 --- a/src/xenia/hid/input_system.h +++ b/src/xenia/hid/input_system.h @@ -10,6 +10,7 @@ #ifndef XENIA_HID_INPUT_SYSTEM_H_ #define XENIA_HID_INPUT_SYSTEM_H_ +#include #include #include @@ -32,7 +33,7 @@ class InputSystem { X_STATUS Setup(); - void AddDriver(InputDriver* driver); + void AddDriver(std::unique_ptr driver); X_RESULT GetCapabilities(uint32_t user_index, uint32_t flags, X_INPUT_CAPABILITIES* out_caps); @@ -46,7 +47,7 @@ class InputSystem { Memory* memory_; cpu::Processor* processor_; - std::vector drivers_; + std::vector> drivers_; }; } // namespace hid diff --git a/src/xenia/kernel/fs/devices/disc_image_file.cc b/src/xenia/kernel/fs/devices/disc_image_file.cc index 82afb41f1..1efb68b3f 100644 --- a/src/xenia/kernel/fs/devices/disc_image_file.cc +++ b/src/xenia/kernel/fs/devices/disc_image_file.cc @@ -23,7 +23,7 @@ DiscImageFile::DiscImageFile(KernelState* kernel_state, Mode mode, DiscImageEntry* entry) : XFile(kernel_state, mode), entry_(entry) {} -DiscImageFile::~DiscImageFile() {} +DiscImageFile::~DiscImageFile() { delete entry_; } const std::string& DiscImageFile::path() const { return entry_->path(); } diff --git a/src/xenia/kernel/fs/devices/host_path_file.cc b/src/xenia/kernel/fs/devices/host_path_file.cc index 0a81b8fed..eafd2cd1f 100644 --- a/src/xenia/kernel/fs/devices/host_path_file.cc +++ b/src/xenia/kernel/fs/devices/host_path_file.cc @@ -22,7 +22,10 @@ HostPathFile::HostPathFile(KernelState* kernel_state, Mode mode, file_handle_(file_handle), XFile(kernel_state, mode) {} -HostPathFile::~HostPathFile() { CloseHandle(file_handle_); } +HostPathFile::~HostPathFile() { + CloseHandle(file_handle_); + delete entry_; +} const std::string& HostPathFile::path() const { return entry_->path(); } diff --git a/src/xenia/kernel/fs/devices/stfs_container_file.cc b/src/xenia/kernel/fs/devices/stfs_container_file.cc index fa7750c57..b52c6e7ce 100644 --- a/src/xenia/kernel/fs/devices/stfs_container_file.cc +++ b/src/xenia/kernel/fs/devices/stfs_container_file.cc @@ -23,7 +23,7 @@ STFSContainerFile::STFSContainerFile(KernelState* kernel_state, Mode mode, STFSContainerEntry* entry) : entry_(entry), XFile(kernel_state, mode) {} -STFSContainerFile::~STFSContainerFile() {} +STFSContainerFile::~STFSContainerFile() { delete entry_; } const std::string& STFSContainerFile::path() const { return entry_->path(); } diff --git a/src/xenia/kernel/fs/filesystem.cc b/src/xenia/kernel/fs/filesystem.cc index 8499c3185..e3b37bb89 100644 --- a/src/xenia/kernel/fs/filesystem.cc +++ b/src/xenia/kernel/fs/filesystem.cc @@ -176,6 +176,16 @@ std::unique_ptr FileSystem::ResolvePath(const std::string& path) { return nullptr; } +X_STATUS FileSystem::Open(std::unique_ptr entry, + KernelState* kernel_state, Mode mode, bool async, + XFile** out_file) { + auto result = entry->Open(kernel_state, mode, async, out_file); + if (XSUCCEEDED(result)) { + entry.release(); + } + return result; +} + } // namespace fs } // namespace kernel } // namespace xe diff --git a/src/xenia/kernel/fs/filesystem.h b/src/xenia/kernel/fs/filesystem.h index c785966be..708f89343 100644 --- a/src/xenia/kernel/fs/filesystem.h +++ b/src/xenia/kernel/fs/filesystem.h @@ -52,6 +52,8 @@ class FileSystem { int DeleteSymbolicLink(const std::string& path); std::unique_ptr ResolvePath(const std::string& path); + X_STATUS Open(std::unique_ptr entry, KernelState* kernel_state, + Mode mode, bool async, XFile** out_file); private: std::vector devices_; diff --git a/src/xenia/kernel/objects/xuser_module.cc b/src/xenia/kernel/objects/xuser_module.cc index 164f407ad..4f59c205d 100644 --- a/src/xenia/kernel/objects/xuser_module.cc +++ b/src/xenia/kernel/objects/xuser_module.cc @@ -62,7 +62,8 @@ X_STATUS XUserModule::LoadFromFile(const char* path) { std::vector buffer(file_info.file_length); // Open file for reading. - result = fs_entry->Open(kernel_state(), fs::Mode::READ, false, &file); + result = kernel_state()->file_system()->Open( + std::move(fs_entry), kernel_state(), fs::Mode::READ, false, &file); XEEXPECTZERO(result); // Read entire file into memory. diff --git a/src/xenia/kernel/xboxkrnl_io.cc b/src/xenia/kernel/xboxkrnl_io.cc index 7b8dfcab3..a74a77549 100644 --- a/src/xenia/kernel/xboxkrnl_io.cc +++ b/src/xenia/kernel/xboxkrnl_io.cc @@ -105,9 +105,9 @@ SHIM_CALL NtCreateFile_shim(PPCContext* ppc_state, KernelState* state) { XFile* file = NULL; if (entry && entry->type() == Entry::Type::FILE) { // Open the file. - result = entry->Open(state, mode, - false, // TODO(benvanik): pick async mode, if needed. - &file); + result = fs->Open(std::move(entry), state, mode, + false, // TODO(benvanik): pick async mode, if needed. + &file); } else { result = X_STATUS_NO_SUCH_FILE; info = X_FILE_DOES_NOT_EXIST; @@ -173,9 +173,9 @@ SHIM_CALL NtOpenFile_shim(PPCContext* ppc_state, KernelState* state) { XFile* file = NULL; if (entry && entry->type() == Entry::Type::FILE) { // Open the file. - result = entry->Open(state, mode, - false, // TODO(benvanik): pick async mode, if needed. - &file); + result = fs->Open(std::move(entry), state, mode, + false, // TODO(benvanik): pick async mode, if needed. + &file); } else { result = X_STATUS_NO_SUCH_FILE; info = X_FILE_DOES_NOT_EXIST;