In-progress work: refactoring PAL not to be instanced.

This removes a lot of useless passing around of the PAL object.
This commit is contained in:
Ben Vanik
2013-03-29 05:07:32 -07:00
parent c46093266e
commit 85bdbd24d1
45 changed files with 444 additions and 189 deletions

View File

@@ -23,8 +23,7 @@ typedef struct xe_file {
#endif // WIN32
xe_file_ref xe_file_open(xe_pal_ref pal, const xe_file_mode mode,
const xechar_t *path) {
xe_file_ref xe_file_open(const xe_file_mode mode, const xechar_t *path) {
xe_file_ref file = (xe_file_ref)xe_calloc(sizeof(xe_file_t));
xe_ref_init((xe_ref)file);
@@ -83,27 +82,3 @@ size_t xe_file_read(xe_file_ref file, const size_t offset,
return fread(buffer, 1, buffer_size, handle);
}
void xe_path_join(const xechar_t* left, const xechar_t* right,
xechar_t* out_path, size_t out_path_size) {
#if XE_WCHAR
xesnprintf(out_path, out_path_size, XT("%ls%c%ls"),
left, XE_PATH_SEPARATOR, right);
#else
xesnprintf(out_path, out_path_size, XT("%s%c%s"),
left, XE_PATH_SEPARATOR, right);
#endif // XE_WCHAR
}
void xe_path_get_absolute(const xechar_t* path, xechar_t* out_abs_path,
size_t abs_path_size) {
#if XE_PLATFORM(WIN32)
#if XE_WCHAR
_wfullpath(out_abs_path, path, abs_path_size);
#else
_fullpath(out_abs_path, path, abs_path_size);
#endif // XE_WCHAR
#else
realpath(path, out_abs_path);
#endif // WIN32
}

View File

@@ -11,7 +11,7 @@
#define XENIA_CORE_FILE_H_
#include <xenia/common.h>
#include <xenia/core/pal.h>
#include <xenia/core/path.h>
#include <xenia/core/ref.h>
@@ -25,18 +25,12 @@ typedef enum {
} xe_file_mode;
xe_file_ref xe_file_open(xe_pal_ref pal, const xe_file_mode mode,
const xechar_t *path);
xe_file_ref xe_file_open(const xe_file_mode mode, const xechar_t *path);
xe_file_ref xe_file_retain(xe_file_ref file);
void xe_file_release(xe_file_ref file);
size_t xe_file_get_length(xe_file_ref file);
size_t xe_file_read(xe_file_ref file, const size_t offset,
uint8_t *buffer, const size_t buffer_size);
void xe_path_join(const xechar_t* left, const xechar_t* right,
xechar_t* out_path, size_t out_path_size);
void xe_path_get_absolute(const xechar_t* path, xechar_t* out_abs_path,
size_t abs_path_size);
#endif // XENIA_CORE_FILE_H_

View File

@@ -53,7 +53,7 @@ struct xe_memory {
};
xe_memory_ref xe_memory_create(xe_pal_ref pal, xe_memory_options_t options) {
xe_memory_ref xe_memory_create(xe_memory_options_t options) {
uint32_t offset;
uint32_t mspace_size;

View File

@@ -11,7 +11,6 @@
#define XENIA_CORE_MEMORY_H_
#include <xenia/common.h>
#include <xenia/core/pal.h>
#include <xenia/core/ref.h>
@@ -24,7 +23,7 @@ struct xe_memory;
typedef struct xe_memory* xe_memory_ref;
xe_memory_ref xe_memory_create(xe_pal_ref pal, xe_memory_options_t options);
xe_memory_ref xe_memory_create(xe_memory_options_t options);
xe_memory_ref xe_memory_retain(xe_memory_ref memory);
void xe_memory_release(xe_memory_ref memory);

View File

@@ -12,7 +12,6 @@
#include <xenia/common.h>
#include <xenia/core/file.h>
#include <xenia/core/pal.h>
#include <xenia/core/ref.h>
@@ -20,8 +19,7 @@ struct xe_mmap;
typedef struct xe_mmap* xe_mmap_ref;
xe_mmap_ref xe_mmap_open(xe_pal_ref pal, const xe_file_mode mode,
const xechar_t *path,
xe_mmap_ref xe_mmap_open(const xe_file_mode mode, const xechar_t *path,
const size_t offset, const size_t length);
xe_mmap_ref xe_mmap_retain(xe_mmap_ref mmap);
void xe_mmap_release(xe_mmap_ref mmap);

View File

@@ -23,8 +23,7 @@ typedef struct xe_mmap {
} xe_mmap_t;
xe_mmap_ref xe_mmap_open(xe_pal_ref pal, const xe_file_mode mode,
const xechar_t *path,
xe_mmap_ref xe_mmap_open(const xe_file_mode mode, const xechar_t *path,
const size_t offset, const size_t length) {
xe_mmap_ref mmap = (xe_mmap_ref)xe_calloc(sizeof(xe_mmap_t));
xe_ref_init((xe_ref)mmap);

View File

@@ -21,8 +21,7 @@ typedef struct xe_mmap {
} xe_mmap_t;
xe_mmap_ref xe_mmap_open(xe_pal_ref pal, const xe_file_mode mode,
const xechar_t *path,
xe_mmap_ref xe_mmap_open(const xe_file_mode mode, const xechar_t *path,
const size_t offset, const size_t length) {
xe_mmap_ref mmap = (xe_mmap_ref)xe_calloc(sizeof(xe_mmap_t));
xe_ref_init((xe_ref)mmap);

View File

@@ -19,13 +19,19 @@ typedef struct {
} xe_pal_options_t;
struct xe_pal;
typedef struct xe_pal* xe_pal_ref;
int xe_pal_init(xe_pal_options_t options);
xe_pal_ref xe_pal_create(xe_pal_options_t options);
xe_pal_ref xe_pal_retain(xe_pal_ref pal);
void xe_pal_release(xe_pal_ref pal);
typedef struct {
struct {
uint32_t physical_count;
uint32_t logical_count;
} processors;
} xe_system_info;
int xe_pal_get_system_info(xe_system_info* out_info);
double xe_pal_now();
#endif // XENIA_CORE_PAL_H_

84
src/xenia/core/pal_mac.cc Normal file
View File

@@ -0,0 +1,84 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include <xenia/core/pal.h>
#include <CoreFoundation/CoreFoundation.h>
#include <asl.h>
#include <mach/mach_time.h>
#include <mach/kern_return.h>
#include <sys/types.h>
#include <sys/sysctl.h>
namespace {
typedef struct xe_pal_mac {
double time_to_sec;
} xe_pal_mac_t;
xe_pal_mac_t* pal;
}
void xe_pal_dealloc();
int xe_pal_init(xe_pal_options_t options) {
pal = (xe_pal_mac_t)xe_calloc(sizeof(pal));
mach_timebase_info_data_t info;
mach_timebase_info(&info);
pal->time_to_sec = (double)((info.numer / info.denom) / 1000000000.0);
atexit(xe_pal_dealloc);
return 0;
}
void xe_pal_dealloc() {
//
xe_free(pal);
}
int xe_pal_get_system_info(xe_system_info* out_info) {
xe_zero_struct(out_info, sizeof(xe_system_info));
out_info->processors.physical_count = 1;
out_info->processors.logical_count = 1;
// http://geekinfo.googlecode.com/svn-history/r74/trunk/src/macosxsystem.cpp
int count;
size_t size = sizeof(int);
if (!sysctlbyname("hw.physicalcpu", &count, &size, NULL, 0)) {
out_info->processors.physical_count = count;
} else if (!sysctlbyname("hw.packages", &count, &size, NULL, 0)) {
out_info->processors.physical_count = count;
} else {
out_info->processors.physical_count = 1;
}
if (!sysctlbyname("hw.logicalcpu", &count, &size, NULL, 0)) {
out_info->processors.logical_count = count;
} else if (!sysctlbyname("hw.ncpu", &count, &size, NULL, 0)) {
out_info->processors.logical_count = count;
} else {
out_info->processors.logical_count = 1;
}
return 0;
}
double xe_pal_now() {
// According to a bunch of random posts, CACurrentMediaTime is a better call:
// https://devforums.apple.com/message/118806#118806
// CACurrentMediaTime is based on mach_absolute_time(), which doesn't have a
// dependency on QuartzCore:
// http://developer.apple.com/library/mac/#qa/qa2004/qa1398.html
// return (double)CFAbsoluteTimeGetCurrent();
// return (double)CACurrentMediaTime();
return (double)(mach_absolute_time() * pal->time_to_sec);
}

View File

@@ -0,0 +1,62 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include <xenia/core/pal.h>
namespace {
typedef struct xe_pal_posix {
} xe_pal_posix_t;
xe_pal_posix_t* pal;
}
void xe_pal_dealloc();
int xe_pal_init(xe_pal_options_t options) {
pal = (xe_pal_posix_t)xe_calloc(sizeof(pal));
//
atexit(xe_pal_dealloc);
return 0;
}
void xe_pal_dealloc() {
//
xe_free(pal);
}
int xe_pal_get_system_info(xe_system_info* out_info) {
xe_zero_struct(out_info, sizeof(xe_system_info));
out_info->processors.physical_count = 1;
out_info->processors.logical_count = 1;
#if defined(_SC_NPROCESSORS_ONLN)
int nproc = sysconf(_SC_NPROCESSORS_ONLN);
if (nproc >= 1) {
// Only able to get logical count.
sysInfo->processors.logicalCount = nproc;
}
#else
#warning no calls to get processor counts
#endif // _SC_NPROCESSORS_ONLN
return 0;
}
double xe_pal_now() {
// http://www.kernel.org/doc/man-pages/online/pages/man2/clock_gettime.2.html
// http://juliusdavies.ca/posix_clocks/clock_realtime_linux_faq.html
struct timespec ts;
CPIGNORE(clock_gettime(CLOCK_MONOTONIC, &ts));
return (double)(ts.tv_sec + (ts.tv_nsec * 1000000000.0));
}

145
src/xenia/core/pal_win.cc Normal file
View File

@@ -0,0 +1,145 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include <xenia/core/pal.h>
namespace {
typedef struct xe_pal_win {
bool use_high_perf_timer;
double inv_ticks_per_sec;
} xe_pal_win_t;
xe_pal_win_t* pal;
}
void xe_pal_dealloc();
int xe_pal_init(xe_pal_options_t options) {
pal = (xe_pal_win_t)xe_calloc(sizeof(pal));
// Get QPC timing frequency... hopefully stable over the life of the app,
// but likely not.
// TODO(benvanik): requery periodically/etc.
LARGE_INTEGER ticks_per_sec;
if (QueryPerformanceFrequency(&ticks_per_sec)) {
pal->use_high_perf_timer = true;
pal->inv_ticks_per_sec = 1.0 / (double)ticks_per_sec.QuadPart;
} else {
pal->use_high_perf_timer = false;
XELOGW("Falling back from high performance timer");
}
// Setup COM on the main thread.
// NOTE: this may fail if COM has already been initialized - that's OK.
XEIGNORE(CoInitializeEx(NULL, COINIT_MULTITHREADED));
atexit(xe_pal_dealloc);
return 0;
}
void xe_pal_dealloc() {
//
xe_free(pal);
}
// http://msdn.microsoft.com/en-us/library/ms683194.aspx
namespace {
DWORD CountSetBits(ULONG_PTR bitMask) {
DWORD LSHIFT = sizeof(ULONG_PTR)*8 - 1;
DWORD bitSetCount = 0;
ULONG_PTR bitTest = (ULONG_PTR)1 << LSHIFT;
for (DWORD i = 0; i <= LSHIFT; ++i, bitTest /= 2) {
bitSetCount += ((bitMask & bitTest)?1:0);
}
return bitSetCount;
}
}
int xe_pal_get_system_info(xe_system_info* out_info) {
int result_code = 1;
xe_zero_struct(out_info, sizeof(xe_system_info));
out_info->processors.physical_count = 1;
out_info->processors.logical_count = 1;
typedef BOOL (WINAPI *LPFN_GLPI)(
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
HMODULE kernel32 = NULL;
LPFN_GLPI glpi = NULL;
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
kernel32 = GetModuleHandle(TEXT("kernel32"));
XEEXPECTNOTNULL(kernel32);
glpi = (LPFN_GLPI)GetProcAddress(kernel32, "GetLogicalProcessorInfomration");
XEEXPECTNOTNULL(glpi);
// Call GLPI once to get the buffer size, allocate it, then call again.
DWORD buffer_length = 0;
XEEXPECTFALSE(glpi(NULL, &buffer_length));
XEEXPECT(GetLastError() == ERROR_INSUFFICIENT_BUFFER);
buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)xe_malloc(buffer_length);
XEEXPECTNOTNULL(buffer);
XEEXPECTTRUE(glpi(buffer, &buffer_length));
XEEXPECTNOTZERO(buffer_length);
out_info->processors.physical_count = 0;
out_info->processors.logical_count = 0;
size_t info_count = buffer_length /
sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
for (size_t n = 0; n < info_count; n++) {
switch (buffer[n].Relationship) {
case RelationProcessorPackage:
out_info->processors.physical_count++;
break;
case RelationProcessorCore:
if (buffer[n].ProcessorCore.Flags == 1) {
// Hyper-threaded.
// The number of processors is set as bits in ProcessorMask.
out_info.processors.logical_count +=
CountSetBits(buffer[n].ProcessorMask);
} else {
// A real core - just count as one.
out_info->processors.logical_count++;
}
break;
}
}
out_info->processors.physical_count =
MAX(1, out_info->processors.physical_count);
out_info->processors.logical_count =
MAX(1, out_info->processors.logical_count);
result_code = 0;
XECLEANUP:
xe_free(buffer);
if (kernel32) {
FreeLibrary(kernel32);
}
return result_code;
}
double xe_pal_now() {
if (pal->use_high_perf_timer) {
LARGE_INTEGER counter;
QueryPerformanceCounter(&counter);
return counter.QuadPart * pal->inv_ticks_per_sec;
} else {
// Using GetSystemTimeAsFileTime instead of GetSystemTime() as it has a
// 100ns resolution.
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
ULARGE_INTEGER uli;
uli.LowPart = ft.dwLowDateTime;
uli.HighPart = ft.dwHighDateTime;
return uli.QuadPart / 10000000.0;
}
}

35
src/xenia/core/path.cc Normal file
View File

@@ -0,0 +1,35 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include <xenia/core/path.h>
void xe_path_join(const xechar_t* left, const xechar_t* right,
xechar_t* out_path, size_t out_path_size) {
#if XE_WCHAR
xesnprintf(out_path, out_path_size, XT("%ls%c%ls"),
left, XE_PATH_SEPARATOR, right);
#else
xesnprintf(out_path, out_path_size, XT("%s%c%s"),
left, XE_PATH_SEPARATOR, right);
#endif // XE_WCHAR
}
void xe_path_get_absolute(const xechar_t* path, xechar_t* out_abs_path,
size_t abs_path_size) {
#if XE_PLATFORM(WIN32)
#if XE_WCHAR
_wfullpath(out_abs_path, path, abs_path_size);
#else
_fullpath(out_abs_path, path, abs_path_size);
#endif // XE_WCHAR
#else
realpath(path, out_abs_path);
#endif // WIN32
}

View File

@@ -7,33 +7,19 @@
******************************************************************************
*/
#ifndef XENIA_CORE_PATH_H_
#define XENIA_CORE_PATH_H_
#include <xenia/common.h>
#include <xenia/core/pal.h>
#include <xenia/core/ref.h>
typedef struct xe_pal {
xe_ref_t ref;
void xe_path_join(const xechar_t* left, const xechar_t* right,
xechar_t* out_path, size_t out_path_size);
void xe_path_get_absolute(const xechar_t* path, xechar_t* out_abs_path,
size_t abs_path_size);
} xe_pal_t;
const xechar_t* xe_path_get_tmp(const xechar_t* prefix);
xe_pal_ref xe_pal_create(xe_pal_options_t options) {
xe_pal_ref pal = (xe_pal_ref)xe_calloc(sizeof(xe_pal_t));
xe_ref_init((xe_ref)pal);
//
return pal;
}
void xe_pal_dealloc(xe_pal_ref pal) {
//
}
xe_pal_ref xe_pal_retain(xe_pal_ref pal) {
xe_ref_retain((xe_ref)pal);
return pal;
}
void xe_pal_release(xe_pal_ref pal) {
xe_ref_release((xe_ref)pal, (xe_ref_dealloc_t)xe_pal_dealloc);
}
#endif // XENIA_CORE_PATH_H_

View File

@@ -0,0 +1,15 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include <xenia/core/path.h>
const xechar_t* xe_path_get_tmp(const xechar_t* prefix) {
//
}

View File

@@ -0,0 +1,15 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include <xenia/core/path.h>
const xechar_t* xe_path_get_tmp(const xechar_t* prefix) {
//
}

View File

@@ -7,8 +7,9 @@
'memory.h',
'mmap.h',
'mutex.h',
'pal.cc',
'pal.h',
'path.cc',
'path.h',
'ref.cc',
'ref.h',
'socket.h',
@@ -21,13 +22,26 @@
'sources': [
'mmap_posix.cc',
'mutex_posix.cc',
'path_posix.cc',
'socket_posix.cc',
],
}],
['OS == "linux"', {
'sources': [
'pal_posix.cc',
],
}],
['OS == "mac"', {
'sources': [
'pal_mac.cc',
],
}],
['OS == "win"', {
'sources': [
'mmap_win.cc',
'mutex_win.cc',
'pal_win.cc',
'path_win.cc',
'socket_win.cc',
],
}],

View File

@@ -24,8 +24,7 @@ typedef struct xe_thread {
xe_thread_ref xe_thread_create(
xe_pal_ref pal, const char* name, xe_thread_callback callback,
void* param) {
const char* name, xe_thread_callback callback, void* param) {
xe_thread_ref thread = (xe_thread_ref)xe_calloc(sizeof(xe_thread_t));
xe_ref_init((xe_ref)thread);

View File

@@ -23,7 +23,7 @@ typedef void (*xe_thread_callback)(void* param);
xe_thread_ref xe_thread_create(
xe_pal_ref pal, const char* name, xe_thread_callback callback, void* param);
const char* name, xe_thread_callback callback, void* param);
xe_thread_ref xe_thread_retain(xe_thread_ref thread);
void xe_thread_release(xe_thread_ref thread);