From c71c9e63ec4c1cdfaeeaddc01ad31b15cfd2430e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 2 Aug 2018 11:54:20 -0400 Subject: [PATCH 1/3] kernel/thread: Make GetFreeThreadLocalSlot() internally linked This function isn't used outside of this translation unit, so we can make it internally linked. --- src/core/hle/kernel/thread.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 6f30948c7..6e84362f5 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -286,7 +286,7 @@ static void DebugThreadQueue() { * slot: The index of the first free slot in the indicated page. * alloc_needed: Whether there's a need to allocate a new TLS page (All pages are full). */ -std::tuple GetFreeThreadLocalSlot(std::vector>& tls_slots) { +static std::tuple GetFreeThreadLocalSlot(std::vector>& tls_slots) { // Iterate over all the allocated pages, and try to find one where not all slots are used. for (unsigned page = 0; page < tls_slots.size(); ++page) { const auto& page_tls_slots = tls_slots[page]; From 893d9c5ff3eb1f368539fc73cb2be94c4e101975 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 2 Aug 2018 11:55:10 -0400 Subject: [PATCH 2/3] kernel/thread: Make GetFreeThreadLocalSlot() reference parameter a const reference This function only reads the data being referenced, it doesn't modify it, so we can turn the reference into a const reference. --- src/core/hle/kernel/thread.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 6e84362f5..9b6598115 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -286,7 +286,8 @@ static void DebugThreadQueue() { * slot: The index of the first free slot in the indicated page. * alloc_needed: Whether there's a need to allocate a new TLS page (All pages are full). */ -static std::tuple GetFreeThreadLocalSlot(std::vector>& tls_slots) { +static std::tuple GetFreeThreadLocalSlot( + const std::vector>& tls_slots) { // Iterate over all the allocated pages, and try to find one where not all slots are used. for (unsigned page = 0; page < tls_slots.size(); ++page) { const auto& page_tls_slots = tls_slots[page]; From 596caf70b7181ac374a2017dc269ed0ae962e54f Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 2 Aug 2018 12:01:22 -0400 Subject: [PATCH 3/3] kernel/thread: Make GetFreeThreadLocalSlot()'s loop indices size_t Avoids using a u32 to compare against a range of size_t, which can be a source of warnings. While we're at it, compress a std::tie into a structured binding. --- src/core/hle/kernel/thread.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 9b6598115..accc853d3 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -286,14 +286,14 @@ static void DebugThreadQueue() { * slot: The index of the first free slot in the indicated page. * alloc_needed: Whether there's a need to allocate a new TLS page (All pages are full). */ -static std::tuple GetFreeThreadLocalSlot( +static std::tuple GetFreeThreadLocalSlot( const std::vector>& tls_slots) { // Iterate over all the allocated pages, and try to find one where not all slots are used. - for (unsigned page = 0; page < tls_slots.size(); ++page) { + for (std::size_t page = 0; page < tls_slots.size(); ++page) { const auto& page_tls_slots = tls_slots[page]; if (!page_tls_slots.all()) { // We found a page with at least one free slot, find which slot it is - for (unsigned slot = 0; slot < page_tls_slots.size(); ++slot) { + for (std::size_t slot = 0; slot < page_tls_slots.size(); ++slot) { if (!page_tls_slots.test(slot)) { return std::make_tuple(page, slot, false); } @@ -363,11 +363,8 @@ ResultVal> Thread::Create(std::string name, VAddr entry_point, // Find the next available TLS index, and mark it as used auto& tls_slots = owner_process->tls_slots; - bool needs_allocation = true; - u32 available_page; // Which allocated page has free space - u32 available_slot; // Which slot within the page is free - std::tie(available_page, available_slot, needs_allocation) = GetFreeThreadLocalSlot(tls_slots); + auto [available_page, available_slot, needs_allocation] = GetFreeThreadLocalSlot(tls_slots); if (needs_allocation) { // There are no already-allocated pages with free slots, lets allocate a new one. @@ -389,7 +386,7 @@ ResultVal> Thread::Create(std::string name, VAddr entry_point, owner_process->linear_heap_used += Memory::PAGE_SIZE; tls_slots.emplace_back(0); // The page is completely available at the start - available_page = static_cast(tls_slots.size() - 1); + available_page = tls_slots.size() - 1; available_slot = 0; // Use the first slot in the new page auto& vm_manager = owner_process->vm_manager;