From 2c355b81f20fe0d40ac825375073d21b211732b9 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Fri, 4 Dec 2020 16:09:42 +0000 Subject: [PATCH] Add NetBSD support --- Source/Core/Common/MemoryUtil.cpp | 6 ++--- Source/Core/Common/StringUtil.cpp | 23 ++++++++++++++------ Source/Core/Common/Thread.cpp | 18 ++++++++++++++- Source/Core/Core/HW/EXI/EXI_DeviceEthernet.h | 2 +- Source/Core/Core/IOS/Network/Socket.h | 3 ++- Source/Core/Core/MemTools.cpp | 2 +- Source/Core/VideoCommon/DriverDetails.cpp | 2 ++ Source/Core/VideoCommon/DriverDetails.h | 1 + 8 files changed, 43 insertions(+), 14 deletions(-) diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index ca9fa64185..655838c4fa 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -19,7 +19,7 @@ #include #include #include -#if defined __APPLE__ || defined __FreeBSD__ || defined __OpenBSD__ +#if defined __APPLE__ || defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__ #include #elif defined __HAIKU__ #include @@ -156,7 +156,7 @@ size_t MemPhysical() memInfo.dwLength = sizeof(MEMORYSTATUSEX); GlobalMemoryStatusEx(&memInfo); return memInfo.ullTotalPhys; -#elif defined __APPLE__ || defined __FreeBSD__ || defined __OpenBSD__ +#elif defined __APPLE__ || defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__ int mib[2]; size_t physical_memory; mib[0] = CTL_HW; @@ -164,7 +164,7 @@ size_t MemPhysical() mib[1] = HW_MEMSIZE; #elif defined __FreeBSD__ mib[1] = HW_REALMEM; -#elif defined __OpenBSD__ +#elif defined __OpenBSD__ || defined __NetBSD__ mib[1] = HW_PHYSMEM; #endif size_t length = sizeof(size_t); diff --git a/Source/Core/Common/StringUtil.cpp b/Source/Core/Common/StringUtil.cpp index 8889301549..ffda64c79f 100644 --- a/Source/Core/Common/StringUtil.cpp +++ b/Source/Core/Common/StringUtil.cpp @@ -35,12 +35,16 @@ constexpr u32 CODEPAGE_SHIFT_JIS = 932; constexpr u32 CODEPAGE_WINDOWS_1252 = 1252; #else +#if defined(__NetBSD__) +#define LIBICONV_PLUG +#endif #include #include #include #endif -#if !defined(_WIN32) && !defined(ANDROID) && !defined(__HAIKU__) && !defined(__OpenBSD__) +#if !defined(_WIN32) && !defined(ANDROID) && !defined(__HAIKU__) && !defined(__OpenBSD__) && \ + !defined(__NetBSD__) static locale_t GetCLocale() { static locale_t c_locale = newlocale(LC_ALL_MASK, "C", nullptr); @@ -133,11 +137,11 @@ bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list ar c_locale = _create_locale(LC_ALL, "C"); writtenCount = _vsnprintf_l(out, outsize, format, c_locale, args); #else -#if !defined(ANDROID) && !defined(__HAIKU__) && !defined(__OpenBSD__) +#if !defined(ANDROID) && !defined(__HAIKU__) && !defined(__OpenBSD__) && !defined(__NetBSD__) locale_t previousLocale = uselocale(GetCLocale()); #endif writtenCount = vsnprintf(out, outsize, format, args); -#if !defined(ANDROID) && !defined(__HAIKU__) && !defined(__OpenBSD__) +#if !defined(ANDROID) && !defined(__HAIKU__) && !defined(__OpenBSD__) && !defined(__NetBSD__) uselocale(previousLocale); #endif #endif @@ -174,7 +178,7 @@ std::string StringFromFormatV(const char* format, va_list args) std::string temp = buf; delete[] buf; #else -#if !defined(ANDROID) && !defined(__HAIKU__) && !defined(__OpenBSD__) +#if !defined(ANDROID) && !defined(__HAIKU__) && !defined(__OpenBSD__) && !defined(__NetBSD__) locale_t previousLocale = uselocale(GetCLocale()); #endif if (vasprintf(&buf, format, args) < 0) @@ -183,7 +187,7 @@ std::string StringFromFormatV(const char* format, va_list args) buf = nullptr; } -#if !defined(ANDROID) && !defined(__HAIKU__) && !defined(__OpenBSD__) +#if !defined(ANDROID) && !defined(__HAIKU__) && !defined(__OpenBSD__) && !defined(__NetBSD__) uselocale(previousLocale); #endif @@ -524,8 +528,13 @@ std::string CodeTo(const char* tocode, const char* fromcode, std::basic_string_v while (src_bytes != 0) { size_t const iconv_result = - iconv(conv_desc, (char**)(&src_buffer), &src_bytes, &dst_buffer, &dst_bytes); - +#if defined(__OpenBSD__) || defined(__NetBSD__) + iconv(conv_desc, reinterpret_cast(&src_buffer), &src_bytes, &dst_buffer, + &dst_bytes); +#else + iconv(conv_desc, const_cast(reinterpret_cast(&src_buffer)), + &src_bytes, &dst_buffer, &dst_bytes); +#endif if ((size_t)-1 == iconv_result) { if (EILSEQ == errno || EINVAL == errno) diff --git a/Source/Core/Common/Thread.cpp b/Source/Core/Common/Thread.cpp index d9503c784e..10e5fa0552 100644 --- a/Source/Core/Common/Thread.cpp +++ b/Source/Core/Common/Thread.cpp @@ -18,6 +18,8 @@ #include #elif defined BSD4_4 || defined __FreeBSD__ || defined __OpenBSD__ #include +#elif defined __NetBSD__ +#include #elif defined __HAIKU__ #include #endif @@ -119,7 +121,9 @@ void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask) { #ifdef __APPLE__ thread_policy_set(pthread_mach_thread_np(thread), THREAD_AFFINITY_POLICY, (integer_t*)&mask, 1); -#elif (defined __linux__ || defined BSD4_4 || defined __FreeBSD__) && !(defined ANDROID) +#elif (defined __linux__ || defined BSD4_4 || defined __FreeBSD__ || defined __NetBSD__) && \ + !(defined ANDROID) +#ifndef __NetBSD__ #ifdef __FreeBSD__ cpuset_t cpu_set; #else @@ -132,6 +136,16 @@ void SetThreadAffinity(std::thread::native_handle_type thread, u32 mask) CPU_SET(i, &cpu_set); pthread_setaffinity_np(thread, sizeof(cpu_set), &cpu_set); +#else + cpuset_t* cpu_set = cpuset_create(); + + for (int i = 0; i != sizeof(mask) * 8; ++i) + if ((mask >> i) & 1) + cpuset_set(i, cpu_set); + + pthread_setaffinity_np(thread, cpuset_size(cpu_set), cpu_set); + cpuset_destroy(cpu_set); +#endif #endif } @@ -156,6 +170,8 @@ void SetCurrentThreadName(const char* name) pthread_setname_np(name); #elif defined __FreeBSD__ || defined __OpenBSD__ pthread_set_name_np(pthread_self(), name); +#elif defined(__NetBSD__) + pthread_setname_np(pthread_self(), "%s", const_cast(name)); #elif defined __HAIKU__ rename_thread(find_thread(nullptr), name); #else diff --git a/Source/Core/Core/HW/EXI/EXI_DeviceEthernet.h b/Source/Core/Core/HW/EXI/EXI_DeviceEthernet.h index ed79b9a9b1..1637b47633 100644 --- a/Source/Core/Core/HW/EXI/EXI_DeviceEthernet.h +++ b/Source/Core/Core/HW/EXI/EXI_DeviceEthernet.h @@ -383,7 +383,7 @@ private: bool m_bba_link_up = false; bool m_bba_failure_notified = false; #if defined(WIN32) || defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \ - defined(__OpenBSD__) + defined(__OpenBSD__) || defined(__NetBSD__) sf::UdpSocket m_sf_socket; sf::IpAddress m_sf_recipient_ip; char m_in_frame[9004]; diff --git a/Source/Core/Core/IOS/Network/Socket.h b/Source/Core/Core/IOS/Network/Socket.h index 4d637591b7..c72ffd6998 100644 --- a/Source/Core/Core/IOS/Network/Socket.h +++ b/Source/Core/Core/IOS/Network/Socket.h @@ -15,7 +15,8 @@ typedef pollfd pollfd_t; #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) #define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) -#elif defined(__linux__) or defined(__APPLE__) or defined(__FreeBSD__) or defined(__HAIKU__) +#elif defined(__linux__) or defined(__APPLE__) or defined(__FreeBSD__) or defined(__NetBSD__) or \ + defined(__HAIKU__) #include #include #include diff --git a/Source/Core/Core/MemTools.cpp b/Source/Core/Core/MemTools.cpp index 0de150eb0a..e0e0d6695b 100644 --- a/Source/Core/Core/MemTools.cpp +++ b/Source/Core/Core/MemTools.cpp @@ -17,7 +17,7 @@ #include "Core/MachineContext.h" #include "Core/PowerPC/JitInterface.h" -#ifdef __FreeBSD__ +#if defined(__FreeBSD__) || defined(__NetBSD__) #include #endif #ifndef _WIN32 diff --git a/Source/Core/VideoCommon/DriverDetails.cpp b/Source/Core/VideoCommon/DriverDetails.cpp index 88a482ecf8..9f5b06ccdd 100644 --- a/Source/Core/VideoCommon/DriverDetails.cpp +++ b/Source/Core/VideoCommon/DriverDetails.cpp @@ -35,6 +35,8 @@ constexpr u32 m_os = OS_ALL | OS_LINUX; constexpr u32 m_os = OS_ALL | OS_FREEBSD; #elif __OpenBSD__ constexpr u32 m_os = OS_ALL | OS_OPENBSD; +#elif __NetBSD__ +constexpr u32 m_os = OS_ALL | OS_NETBSD; #endif static API m_api = API_OPENGL; diff --git a/Source/Core/VideoCommon/DriverDetails.h b/Source/Core/VideoCommon/DriverDetails.h index 3e58a5fb58..2e25b2a79b 100644 --- a/Source/Core/VideoCommon/DriverDetails.h +++ b/Source/Core/VideoCommon/DriverDetails.h @@ -27,6 +27,7 @@ enum OS OS_ANDROID = (1 << 4), OS_FREEBSD = (1 << 5), OS_OPENBSD = (1 << 6), + OS_NETBSD = (1 << 7), }; // Enum of known vendors // Tegra and Nvidia are separated out due to such substantial differences