summaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/common/common.h13
-rw-r--r--src/common/common_paths.h37
-rw-r--r--src/common/file_util.cpp10
-rw-r--r--src/common/file_util.h5
-rw-r--r--src/common/log.h2
-rw-r--r--src/common/logging/backend.cpp1
-rw-r--r--src/common/logging/log.h24
-rw-r--r--src/common/mem_arena.cpp84
-rw-r--r--src/common/mem_arena.h8
-rw-r--r--src/common/platform.h1
-rw-r--r--src/common/swap.h4
-rw-r--r--src/common/thread_queue_list.h218
12 files changed, 106 insertions, 301 deletions
diff --git a/src/common/common.h b/src/common/common.h
index ba33373ae..3246c7797 100644
--- a/src/common/common.h
+++ b/src/common/common.h
@@ -11,13 +11,6 @@
#include <cstdio>
#include <cstring>
-// Force enable logging in the right modes. For some reason, something had changed
-// so that debugfast no longer logged.
-#if defined(_DEBUG) || defined(DEBUGFAST)
-#undef LOGGING
-#define LOGGING 1
-#endif
-
#define STACKALIGN
// An inheritable class to disallow the copy constructor and operator= functions
@@ -154,16 +147,10 @@ enum EMUSTATE_CHANGE
#ifdef _MSC_VER
-#ifndef _XBOX
inline unsigned long long bswap64(unsigned long long x) { return _byteswap_uint64(x); }
inline unsigned int bswap32(unsigned int x) { return _byteswap_ulong(x); }
inline unsigned short bswap16(unsigned short x) { return _byteswap_ushort(x); }
#else
-inline unsigned long long bswap64(unsigned long long x) { return __loaddoublewordbytereverse(0, &x); }
-inline unsigned int bswap32(unsigned int x) { return __loadwordbytereverse(0, &x); }
-inline unsigned short bswap16(unsigned short x) { return __loadshortbytereverse(0, &x); }
-#endif
-#else
// TODO: speedup
inline unsigned short bswap16(unsigned short x) { return (x << 8) | (x >> 8); }
inline unsigned int bswap32(unsigned int x) { return (x >> 24) | ((x & 0xFF0000) >> 8) | ((x & 0xFF00) << 8) | (x << 24);}
diff --git a/src/common/common_paths.h b/src/common/common_paths.h
index e692e5492..0ecf2d9de 100644
--- a/src/common/common_paths.h
+++ b/src/common/common_paths.h
@@ -35,26 +35,23 @@
#define JAP_DIR "JAP"
// Subdirs in the User dir returned by GetUserPath(D_USER_IDX)
-#define CONFIG_DIR "config"
-#define GAMECONFIG_DIR "game_config"
-#define MAPS_DIR "maps"
-#define CACHE_DIR "cache"
-#define SDMC_DIR "sdmc"
-#define EXTSAVEDATA_DIR "extsavedata"
-#define SAVEDATA_DIR "savedata"
-#define SAVEDATACHECK_DIR "savedatacheck"
-#define SYSDATA_DIR "sysdata"
-#define SYSSAVEDATA_DIR "syssavedata"
-#define SHADERCACHE_DIR "shader_cache"
-#define STATESAVES_DIR "state_saves"
-#define SCREENSHOTS_DIR "screenShots"
-#define DUMP_DIR "dump"
-#define DUMP_TEXTURES_DIR "textures"
-#define DUMP_FRAMES_DIR "frames"
-#define DUMP_AUDIO_DIR "audio"
-#define LOGS_DIR "logs"
-#define SHADERS_DIR "shaders"
-#define SYSCONF_DIR "sysconf"
+#define CONFIG_DIR "config"
+#define GAMECONFIG_DIR "game_config"
+#define MAPS_DIR "maps"
+#define CACHE_DIR "cache"
+#define SDMC_DIR "sdmc"
+#define NAND_DIR "nand"
+#define SYSDATA_DIR "sysdata"
+#define SHADERCACHE_DIR "shader_cache"
+#define STATESAVES_DIR "state_saves"
+#define SCREENSHOTS_DIR "screenShots"
+#define DUMP_DIR "dump"
+#define DUMP_TEXTURES_DIR "textures"
+#define DUMP_FRAMES_DIR "frames"
+#define DUMP_AUDIO_DIR "audio"
+#define LOGS_DIR "logs"
+#define SHADERS_DIR "shaders"
+#define SYSCONF_DIR "sysconf"
// Filenames
// Files in the directory returned by GetUserPath(D_CONFIG_IDX)
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 0a6cd80c8..706e7c842 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -676,11 +676,8 @@ const std::string& GetUserPath(const unsigned int DirIDX, const std::string &new
paths[D_MAPS_IDX] = paths[D_USER_IDX] + MAPS_DIR DIR_SEP;
paths[D_CACHE_IDX] = paths[D_USER_IDX] + CACHE_DIR DIR_SEP;
paths[D_SDMC_IDX] = paths[D_USER_IDX] + SDMC_DIR DIR_SEP;
- paths[D_EXTSAVEDATA] = paths[D_USER_IDX] + EXTSAVEDATA_DIR DIR_SEP;
- paths[D_SAVEDATA_IDX] = paths[D_USER_IDX] + SAVEDATA_DIR DIR_SEP;
- paths[D_SAVEDATACHECK_IDX] = paths[D_USER_IDX] + SAVEDATACHECK_DIR DIR_SEP;
+ paths[D_NAND_IDX] = paths[D_USER_IDX] + NAND_DIR DIR_SEP;
paths[D_SYSDATA_IDX] = paths[D_USER_IDX] + SYSDATA_DIR DIR_SEP;
- paths[D_SYSSAVEDATA_IDX] = paths[D_USER_IDX] + SYSSAVEDATA_DIR DIR_SEP;
paths[D_SHADERCACHE_IDX] = paths[D_USER_IDX] + SHADERCACHE_DIR DIR_SEP;
paths[D_SHADERS_IDX] = paths[D_USER_IDX] + SHADERS_DIR DIR_SEP;
paths[D_STATESAVES_IDX] = paths[D_USER_IDX] + STATESAVES_DIR DIR_SEP;
@@ -722,10 +719,7 @@ const std::string& GetUserPath(const unsigned int DirIDX, const std::string &new
paths[D_MAPS_IDX] = paths[D_USER_IDX] + MAPS_DIR DIR_SEP;
paths[D_CACHE_IDX] = paths[D_USER_IDX] + CACHE_DIR DIR_SEP;
paths[D_SDMC_IDX] = paths[D_USER_IDX] + SDMC_DIR DIR_SEP;
- paths[D_EXTSAVEDATA] = paths[D_USER_IDX] + EXTSAVEDATA_DIR DIR_SEP;
- paths[D_SAVEDATA_IDX] = paths[D_USER_IDX] + SAVEDATA_DIR DIR_SEP;
- paths[D_SAVEDATACHECK_IDX] = paths[D_USER_IDX] + SAVEDATACHECK_DIR DIR_SEP;
- paths[D_SYSSAVEDATA_IDX] = paths[D_USER_IDX] + SYSSAVEDATA_DIR DIR_SEP;
+ paths[D_NAND_IDX] = paths[D_USER_IDX] + NAND_DIR DIR_SEP;
paths[D_SHADERCACHE_IDX] = paths[D_USER_IDX] + SHADERCACHE_DIR DIR_SEP;
paths[D_SHADERS_IDX] = paths[D_USER_IDX] + SHADERS_DIR DIR_SEP;
paths[D_STATESAVES_IDX] = paths[D_USER_IDX] + STATESAVES_DIR DIR_SEP;
diff --git a/src/common/file_util.h b/src/common/file_util.h
index c83ecd87d..86aab2e3d 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -27,11 +27,8 @@ enum {
D_STATESAVES_IDX,
D_SCREENSHOTS_IDX,
D_SDMC_IDX,
- D_EXTSAVEDATA,
- D_SAVEDATA_IDX,
- D_SAVEDATACHECK_IDX,
+ D_NAND_IDX,
D_SYSDATA_IDX,
- D_SYSSAVEDATA_IDX,
D_HIRESTEXTURES_IDX,
D_DUMP_IDX,
D_DUMPFRAMES_IDX,
diff --git a/src/common/log.h b/src/common/log.h
index 667f2fbb9..b397cf14d 100644
--- a/src/common/log.h
+++ b/src/common/log.h
@@ -14,7 +14,7 @@
#endif
#endif
-#if _DEBUG
+#ifdef _DEBUG
#define _dbg_assert_(_t_, _a_) \
if (!(_a_)) {\
LOG_CRITICAL(_t_, "Error...\n\n Line: %d\n File: %s\n Time: %s\n\nIgnore and continue?", \
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp
index 7ac30ad50..83ebb42d9 100644
--- a/src/common/logging/backend.cpp
+++ b/src/common/logging/backend.cpp
@@ -22,6 +22,7 @@ static std::shared_ptr<Logger> global_logger;
SUB(Common, Memory) \
CLS(Core) \
SUB(Core, ARM11) \
+ SUB(Core, Timing) \
CLS(Config) \
CLS(Debug) \
SUB(Debug, Emulated) \
diff --git a/src/common/logging/log.h b/src/common/logging/log.h
index 06b99b07a..3d94bf0d9 100644
--- a/src/common/logging/log.h
+++ b/src/common/logging/log.h
@@ -41,6 +41,7 @@ enum class Class : ClassType {
Common_Memory, ///< Memory mapping and management functions
Core, ///< LLE emulation core
Core_ARM11, ///< ARM11 CPU core
+ Core_Timing, ///< CoreTiming functions
Config, ///< Emulator configuration (including commandline)
Debug, ///< Debugging tools
Debug_Emulated, ///< Debug messages from the emulated programs
@@ -73,17 +74,6 @@ enum class Class : ClassType {
};
/**
- * Level below which messages are simply discarded without buffering regardless of the display
- * settings.
- */
-const Level MINIMUM_LEVEL =
-#ifdef _DEBUG
- Level::Trace;
-#else
- Level::Debug;
-#endif
-
-/**
* Logs a message to the global logger. This proxy exists to avoid exposing the details of the
* Logger class, including the ConcurrentRingBuffer template, to all files that desire to log
* messages, reducing unecessary recompilations.
@@ -102,13 +92,15 @@ void LogMessage(Class log_class, Level log_level,
} // namespace Log
#define LOG_GENERIC(log_class, log_level, ...) \
- do { \
- if (::Log::Level::log_level >= ::Log::MINIMUM_LEVEL) \
- ::Log::LogMessage(::Log::Class::log_class, ::Log::Level::log_level, \
- __FILE__, __LINE__, __func__, __VA_ARGS__); \
- } while (0)
+ ::Log::LogMessage(::Log::Class::log_class, ::Log::Level::log_level, \
+ __FILE__, __LINE__, __func__, __VA_ARGS__)
+#ifdef _DEBUG
#define LOG_TRACE( log_class, ...) LOG_GENERIC(log_class, Trace, __VA_ARGS__)
+#else
+#define LOG_TRACE( log_class, ...) (void(0))
+#endif
+
#define LOG_DEBUG( log_class, ...) LOG_GENERIC(log_class, Debug, __VA_ARGS__)
#define LOG_INFO( log_class, ...) LOG_GENERIC(log_class, Info, __VA_ARGS__)
#define LOG_WARNING( log_class, ...) LOG_GENERIC(log_class, Warning, __VA_ARGS__)
diff --git a/src/common/mem_arena.cpp b/src/common/mem_arena.cpp
index 9904d2472..a20361d6f 100644
--- a/src/common/mem_arena.cpp
+++ b/src/common/mem_arena.cpp
@@ -29,10 +29,6 @@
#endif
#endif
-#ifdef IOS
-void* globalbase = nullptr;
-#endif
-
#ifdef ANDROID
// Hopefully this ABI will never change...
@@ -95,7 +91,7 @@ int ashmem_unpin_region(int fd, size_t offset, size_t len)
#endif // Android
-#if defined(_WIN32) && !defined(_XBOX)
+#if defined(_WIN32)
SYSTEM_INFO sysInfo;
#endif
@@ -103,11 +99,7 @@ SYSTEM_INFO sysInfo;
// Windows mappings need to be on 64K boundaries, due to Alpha legacy.
#ifdef _WIN32
size_t roundup(size_t x) {
-#ifndef _XBOX
int gran = sysInfo.dwAllocationGranularity ? sysInfo.dwAllocationGranularity : 0x10000;
-#else
- int gran = 0x10000; // 64k in 360
-#endif
return (x + gran - 1) & ~(gran - 1);
}
#else
@@ -120,10 +112,8 @@ size_t roundup(size_t x) {
void MemArena::GrabLowMemSpace(size_t size)
{
#ifdef _WIN32
-#ifndef _XBOX
hMemoryMapping = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, (DWORD)(size), nullptr);
GetSystemInfo(&sysInfo);
-#endif
#elif defined(ANDROID)
// Use ashmem so we don't have to allocate a file on disk!
fd = ashmem_create_region("PPSSPP_RAM", size);
@@ -163,9 +153,6 @@ void MemArena::ReleaseSpace()
#ifdef _WIN32
CloseHandle(hMemoryMapping);
hMemoryMapping = 0;
-#elif defined(__SYMBIAN32__)
- memmap->Close();
- delete memmap;
#else
close(fd);
#endif
@@ -175,22 +162,13 @@ void MemArena::ReleaseSpace()
void *MemArena::CreateView(s64 offset, size_t size, void *base)
{
#ifdef _WIN32
-#ifdef _XBOX
- size = roundup(size);
- // use 64kb pages
- void * ptr = VirtualAlloc(nullptr, size, MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
- return ptr;
-#else
size = roundup(size);
void *ptr = MapViewOfFileEx(hMemoryMapping, FILE_MAP_ALL_ACCESS, 0, (DWORD)((u64)offset), size, base);
return ptr;
-#endif
#else
void *retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED |
// Do not sync memory to underlying file. Linux has this by default.
-#ifdef BLACKBERRY
- MAP_NOSYNCFILE |
-#elif defined(__FreeBSD__)
+#ifdef __FreeBSD__
MAP_NOSYNC |
#endif
((base == nullptr) ? 0 : MAP_FIXED), fd, offset);
@@ -208,17 +186,12 @@ void *MemArena::CreateView(s64 offset, size_t size, void *base)
void MemArena::ReleaseView(void* view, size_t size)
{
#ifdef _WIN32
-#ifndef _XBOX
UnmapViewOfFile(view);
-#endif
-#elif defined(__SYMBIAN32__)
- memmap->Decommit(((int)view - (int)memmap->Base()) & 0x3FFFFFFF, size);
#else
munmap(view, size);
#endif
}
-#ifndef __SYMBIAN32__
u8* MemArena::Find4GBBase()
{
#ifdef _M_X64
@@ -242,20 +215,6 @@ u8* MemArena::Find4GBBase()
}
return base;
#else
-#ifdef IOS
- void* base = nullptr;
- if (globalbase == nullptr){
- base = mmap(0, 0x08000000, PROT_READ | PROT_WRITE,
- MAP_ANON | MAP_SHARED, -1, 0);
- if (base == MAP_FAILED) {
- PanicAlert("Failed to map 128 MB of memory space: %s", strerror(errno));
- return 0;
- }
- munmap(base, 0x08000000);
- globalbase = base;
- }
- else{ base = globalbase; }
-#else
void* base = mmap(0, 0x10000000, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_SHARED, -1, 0);
if (base == MAP_FAILED) {
@@ -263,12 +222,10 @@ u8* MemArena::Find4GBBase()
return 0;
}
munmap(base, 0x10000000);
-#endif
return static_cast<u8*>(base);
#endif
#endif
}
-#endif
// yeah, this could also be done in like two bitwise ops...
@@ -284,10 +241,6 @@ static bool Memory_TryBase(u8 *base, const MemoryView *views, int num_views, u32
size_t position = 0;
size_t last_position = 0;
-#if defined(_XBOX)
- void *ptr;
-#endif
-
// Zero all the pointers to be sure.
for (int i = 0; i < num_views; i++)
{
@@ -308,18 +261,6 @@ static bool Memory_TryBase(u8 *base, const MemoryView *views, int num_views, u32
position = last_position;
}
else {
-#ifdef __SYMBIAN32__
- *(view.out_ptr_low) = (u8*)((int)arena->memmap->Base() + view.virtual_address);
- arena->memmap->Commit(view.virtual_address & 0x3FFFFFFF, view.size);
- }
- *(view.out_ptr) = (u8*)((int)arena->memmap->Base() + view.virtual_address & 0x3FFFFFFF);
-#elif defined(_XBOX)
- *(view.out_ptr_low) = (u8*)(base + view.virtual_address);
- //arena->memmap->Commit(view.virtual_address & 0x3FFFFFFF, view.size);
- ptr = VirtualAlloc(base + (view.virtual_address & 0x3FFFFFFF), view.size, MEM_COMMIT, PAGE_READWRITE);
- }
- *(view.out_ptr) = (u8*)base + (view.virtual_address & 0x3FFFFFFF);
-#else
*(view.out_ptr_low) = (u8*)arena->CreateView(position, view.size);
if (!*view.out_ptr_low)
goto bail;
@@ -340,7 +281,6 @@ static bool Memory_TryBase(u8 *base, const MemoryView *views, int num_views, u32
}
#endif
-#endif
last_position = position;
position += roundup(view.size);
}
@@ -389,9 +329,7 @@ u8 *MemoryMap_Setup(const MemoryView *views, int num_views, u32 flags, MemArena
total_mem += roundup(views[i].size);
}
// Grab some pagefile backed memory out of the void ...
-#ifndef __SYMBIAN32__
arena->GrabLowMemSpace(total_mem);
-#endif
// Now, create views in high memory where there's plenty of space.
#ifdef _M_X64
@@ -403,15 +341,6 @@ u8 *MemoryMap_Setup(const MemoryView *views, int num_views, u32 flags, MemArena
PanicAlert("MemoryMap_Setup: Failed finding a memory base.");
return 0;
}
-#elif defined(_XBOX)
- // Reserve 256MB
- u8 *base = (u8*)VirtualAlloc(0, 0x10000000, MEM_RESERVE | MEM_LARGE_PAGES, PAGE_READWRITE);
- if (!Memory_TryBase(base, views, num_views, flags, arena))
- {
- PanicAlert("MemoryMap_Setup: Failed finding a memory base.");
- exit(0);
- return 0;
- }
#elif defined(_WIN32)
// Try a whole range of possible bases. Return once we got a valid one.
u32 max_base_addr = 0x7FFF0000 - 0x10000000;
@@ -428,15 +357,6 @@ u8 *MemoryMap_Setup(const MemoryView *views, int num_views, u32 flags, MemArena
break;
}
}
-#elif defined(__SYMBIAN32__)
- arena->memmap = new RChunk();
- arena->memmap->CreateDisconnectedLocal(0, 0, 0x10000000);
- if (!Memory_TryBase(arena->memmap->Base(), views, num_views, flags, arena))
- {
- PanicAlert("MemoryMap_Setup: Failed finding a memory base.");
- return 0;
- }
- u8* base = arena->memmap->Base();
#else
// Linux32 is fine with the x64 method, although limited to 32-bit with no automirrors.
u8 *base = MemArena::Find4GBBase();
diff --git a/src/common/mem_arena.h b/src/common/mem_arena.h
index b5f0aa890..3379d2529 100644
--- a/src/common/mem_arena.h
+++ b/src/common/mem_arena.h
@@ -21,10 +21,6 @@
#include <windows.h>
#endif
-#ifdef __SYMBIAN32__
-#include <e32std.h>
-#endif
-
#include "common/common.h"
// This class lets you create a block of anonymous RAM, and then arbitrarily map views into it.
@@ -39,12 +35,8 @@ public:
void *CreateView(s64 offset, size_t size, void *base = 0);
void ReleaseView(void *view, size_t size);
-#ifdef __SYMBIAN32__
- RChunk* memmap;
-#else
// This only finds 1 GB in 32-bit
static u8 *Find4GBBase();
-#endif
private:
#ifdef _WIN32
diff --git a/src/common/platform.h b/src/common/platform.h
index ce9cfd4a2..ba1109c9f 100644
--- a/src/common/platform.h
+++ b/src/common/platform.h
@@ -35,7 +35,6 @@
#define PLATFORM_MACOSX 2
#define PLATFORM_LINUX 3
#define PLATFORM_ANDROID 4
-#define PLATFORM_IOS 5
////////////////////////////////////////////////////////////////////////////////////////////////////
// Platform detection
diff --git a/src/common/swap.h b/src/common/swap.h
index 4f8f39efb..e2d918362 100644
--- a/src/common/swap.h
+++ b/src/common/swap.h
@@ -48,11 +48,7 @@
// MSVC
#elif defined(_MSC_VER) && !defined(COMMON_BIG_ENDIAN) && !defined(COMMON_LITTLE_ENDIAN)
-#ifdef _XBOX
-#define COMMON_BIG_ENDIAN 1
-#else
#define COMMON_LITTLE_ENDIAN 1
-#endif
#endif
diff --git a/src/common/thread_queue_list.h b/src/common/thread_queue_list.h
index 4e1c0a215..444abf115 100644
--- a/src/common/thread_queue_list.h
+++ b/src/common/thread_queue_list.h
@@ -4,213 +4,143 @@
#pragma once
+#include <array>
+#include <deque>
+
+#include <boost/range/algorithm_ext/erase.hpp>
+
#include "common/common.h"
namespace Common {
-template<class IdType>
+template<class T, unsigned int N>
struct ThreadQueueList {
- // Number of queues (number of priority levels starting at 0.)
- static const int NUM_QUEUES = 128;
+ // TODO(yuriks): If performance proves to be a problem, the std::deques can be replaced with
+ // (dynamically resizable) circular buffers to remove their overhead when
+ // inserting and popping.
- // Initial number of threads a single queue can handle.
- static const int INITIAL_CAPACITY = 32;
+ typedef unsigned int Priority;
- struct Queue {
- // Next ever-been-used queue (worse priority.)
- Queue *next;
- // First valid item in data.
- int first;
- // One after last valid item in data.
- int end;
- // A too-large array with room on the front and end.
- IdType *data;
- // Size of data array.
- int capacity;
- };
+ // Number of priority levels. (Valid levels are [0..NUM_QUEUES).)
+ static const Priority NUM_QUEUES = N;
ThreadQueueList() {
- memset(queues, 0, sizeof(queues));
- first = invalid();
- }
-
- ~ThreadQueueList() {
- for (int i = 0; i < NUM_QUEUES; ++i)
- {
- if (queues[i].data != nullptr)
- free(queues[i].data);
- }
+ first = nullptr;
}
// Only for debugging, returns priority level.
- int contains(const IdType uid) {
- for (int i = 0; i < NUM_QUEUES; ++i)
- {
- if (queues[i].data == nullptr)
- continue;
-
- Queue *cur = &queues[i];
- for (int j = cur->first; j < cur->end; ++j)
- {
- if (cur->data[j] == uid)
- return i;
+ Priority contains(const T& uid) {
+ for (Priority i = 0; i < NUM_QUEUES; ++i) {
+ Queue& cur = queues[i];
+ if (std::find(cur.data.cbegin(), cur.data.cend(), uid) != cur.data.cend()) {
+ return i;
}
}
return -1;
}
- inline IdType pop_first() {
+ T pop_first() {
Queue *cur = first;
- while (cur != invalid())
- {
- if (cur->end - cur->first > 0)
- return cur->data[cur->first++];
- cur = cur->next;
+ while (cur != nullptr) {
+ if (!cur->data.empty()) {
+ auto tmp = std::move(cur->data.front());
+ cur->data.pop_front();
+ return tmp;
+ }
+ cur = cur->next_nonempty;
}
- //_dbg_assert_msg_(SCEKERNEL, false, "ThreadQueueList should not be empty.");
- return 0;
+ return T();
}
- inline IdType pop_first_better(u32 priority) {
+ T pop_first_better(Priority priority) {
Queue *cur = first;
Queue *stop = &queues[priority];
- while (cur < stop)
- {
- if (cur->end - cur->first > 0)
- return cur->data[cur->first++];
- cur = cur->next;
+ while (cur < stop) {
+ if (!cur->data.empty()) {
+ auto tmp = std::move(cur->data.front());
+ cur->data.pop_front();
+ return tmp;
+ }
+ cur = cur->next_nonempty;
}
- return 0;
+ return T();
}
- inline void push_front(u32 priority, const IdType threadID) {
+ void push_front(Priority priority, const T& thread_id) {
Queue *cur = &queues[priority];
- cur->data[--cur->first] = threadID;
- if (cur->first == 0)
- rebalance(priority);
+ cur->data.push_front(thread_id);
}
- inline void push_back(u32 priority, const IdType threadID) {
+ void push_back(Priority priority, const T& thread_id) {
Queue *cur = &queues[priority];
- cur->data[cur->end++] = threadID;
- if (cur->end == cur->capacity)
- rebalance(priority);
+ cur->data.push_back(thread_id);
}
- inline void remove(u32 priority, const IdType threadID) {
+ void remove(Priority priority, const T& thread_id) {
Queue *cur = &queues[priority];
- //_dbg_assert_msg_(SCEKERNEL, cur->next != NULL, "ThreadQueueList::Queue should already be linked up.");
-
- for (int i = cur->first; i < cur->end; ++i)
- {
- if (cur->data[i] == threadID)
- {
- int remaining = --cur->end - i;
- if (remaining > 0)
- memmove(&cur->data[i], &cur->data[i + 1], remaining * sizeof(IdType));
- return;
- }
- }
-
- // Wasn't there.
+ boost::remove_erase(cur->data, thread_id);
}
- inline void rotate(u32 priority) {
+ void rotate(Priority priority) {
Queue *cur = &queues[priority];
- //_dbg_assert_msg_(SCEKERNEL, cur->next != NULL, "ThreadQueueList::Queue should already be linked up.");
- if (cur->end - cur->first > 1)
- {
- cur->data[cur->end++] = cur->data[cur->first++];
- if (cur->end == cur->capacity)
- rebalance(priority);
+ if (cur->data.size() > 1) {
+ cur->data.push_back(std::move(cur->data.front()));
+ cur->data.pop_front();
}
}
- inline void clear() {
- for (int i = 0; i < NUM_QUEUES; ++i)
- {
- if (queues[i].data != nullptr)
- free(queues[i].data);
- }
- memset(queues, 0, sizeof(queues));
- first = invalid();
+ void clear() {
+ queues.fill(Queue());
+ first = nullptr;
}
- inline bool empty(u32 priority) const {
+ bool empty(Priority priority) const {
const Queue *cur = &queues[priority];
- return cur->first == cur->end;
+ return cur->data.empty();
}
- inline void prepare(u32 priority) {
- Queue *cur = &queues[priority];
- if (cur->next == nullptr)
- link(priority, INITIAL_CAPACITY);
+ void prepare(Priority priority) {
+ Queue* cur = &queues[priority];
+ if (cur->next_nonempty == UnlinkedTag())
+ link(priority);
}
private:
- Queue *invalid() const {
- return (Queue *) -1;
+ struct Queue {
+ // Points to the next active priority, skipping over ones that have never been used.
+ Queue* next_nonempty = UnlinkedTag();
+ // Double-ended queue of threads in this priority level
+ std::deque<T> data;
+ };
+
+ /// Special tag used to mark priority levels that have never been used.
+ static Queue* UnlinkedTag() {
+ return reinterpret_cast<Queue*>(1);
}
- void link(u32 priority, int size) {
- //_dbg_assert_msg_(SCEKERNEL, queues[priority].data == NULL, "ThreadQueueList::Queue should only be initialized once.");
-
- if (size <= INITIAL_CAPACITY)
- size = INITIAL_CAPACITY;
- else
- {
- int goal = size;
- size = INITIAL_CAPACITY;
- while (size < goal)
- size *= 2;
- }
+ void link(Priority priority) {
Queue *cur = &queues[priority];
- cur->data = (IdType *) malloc(sizeof(IdType) * size);
- cur->capacity = size;
- cur->first = size / 2;
- cur->end = size / 2;
-
- for (int i = (int) priority - 1; i >= 0; --i)
- {
- if (queues[i].next != nullptr)
- {
- cur->next = queues[i].next;
- queues[i].next = cur;
+
+ for (int i = priority - 1; i >= 0; --i) {
+ if (queues[i].next_nonempty != UnlinkedTag()) {
+ cur->next_nonempty = queues[i].next_nonempty;
+ queues[i].next_nonempty = cur;
return;
}
}
- cur->next = first;
+ cur->next_nonempty = first;
first = cur;
}
- void rebalance(u32 priority) {
- Queue *cur = &queues[priority];
- int size = cur->end - cur->first;
- if (size >= cur->capacity - 2) {
- IdType *new_data = (IdType *)realloc(cur->data, cur->capacity * 2 * sizeof(IdType));
- if (new_data != nullptr) {
- cur->capacity *= 2;
- cur->data = new_data;
- }
- }
-
- int newFirst = (cur->capacity - size) / 2;
- if (newFirst != cur->first) {
- memmove(&cur->data[newFirst], &cur->data[cur->first], size * sizeof(IdType));
- cur->first = newFirst;
- cur->end = newFirst + size;
- }
- }
-
// The first queue that's ever been used.
- Queue *first;
+ Queue* first;
// The priority level queues of thread ids.
- Queue queues[NUM_QUEUES];
+ std::array<Queue, NUM_QUEUES> queues;
};
} // namespace