From fd564cd44acf5efe80b6ecaee178e733bb77605e Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sat, 15 Jul 2023 08:18:05 +0100 Subject: Rename several Windows files to use the same naming scheme. [skip alpine ci] [skip irctest ci] [skip macos ci] [skip ubuntu ci] --- include/compat.h | 2 +- win/CMakeLists.txt | 6 +- win/inspircd_memory_functions.cpp | 73 ------------------- win/inspircd_win32wrapper.cpp | 48 ------------- win/inspircd_win32wrapper.h | 145 -------------------------------------- win/modules/CMakeLists.txt | 4 +- win/win32memory.cpp | 73 +++++++++++++++++++ win/win32wrapper.cpp | 48 +++++++++++++ win/win32wrapper.h | 145 ++++++++++++++++++++++++++++++++++++++ 9 files changed, 272 insertions(+), 272 deletions(-) delete mode 100644 win/inspircd_memory_functions.cpp delete mode 100644 win/inspircd_win32wrapper.cpp delete mode 100644 win/inspircd_win32wrapper.h create mode 100644 win/win32memory.cpp create mode 100644 win/win32wrapper.cpp create mode 100644 win/win32wrapper.h diff --git a/include/compat.h b/include/compat.h index d2cb18b86..7cee76142 100644 --- a/include/compat.h +++ b/include/compat.h @@ -37,7 +37,7 @@ * order to build on Windows correctly. */ #if defined _WIN32 -# include "inspircd_win32wrapper.h" +# include "win32wrapper.h" #else # define DllExport __attribute__ ((visibility ("default"))) # define CoreExport __attribute__ ((visibility ("default"))) diff --git a/win/CMakeLists.txt b/win/CMakeLists.txt index 676c313b0..6ec76d5e8 100644 --- a/win/CMakeLists.txt +++ b/win/CMakeLists.txt @@ -45,7 +45,7 @@ string(REGEX REPLACE ".*InspIRCd-([^\"]+).*" "\\1" VERSION_FULL "${VERSIONSH}") file(GLOB INSPIRCD_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${INSPIRCD_BASE}/src/*.cpp" "${INSPIRCD_BASE}/src/socketengines/select.cpp" - "${INSPIRCD_BASE}/win/inspircd_win32wrapper.cpp" + "${INSPIRCD_BASE}/win/win32wrapper.cpp" "${INSPIRCD_BASE}/win/win32service.cpp" ) list(SORT INSPIRCD_SOURCES) @@ -54,14 +54,14 @@ include_directories("${INSPIRCD_BASE}/win" "${INSPIRCD_BASE}/include") include_directories(SYSTEM "${INSPIRCD_BASE}/vendor") if(MSVC) - add_library(win32_memory STATIC "${INSPIRCD_BASE}/win/inspircd_memory_functions.cpp") + add_library(win32memory STATIC "${INSPIRCD_BASE}/win/win32memory.cpp") endif() configure_file("${INSPIRCD_BASE}/win/inspircd.rc.cmake" "${INSPIRCD_BASE}/win/inspircd.rc") configure_file("${INSPIRCD_BASE}/make/template/config.h" "${INSPIRCD_BASE}/include/config.h") add_executable(inspircd ${INSPIRCD_SOURCES} "${INSPIRCD_BASE}/win/inspircd.rc") -target_link_libraries(inspircd win32_memory) +target_link_libraries(inspircd win32memory) set_target_properties(inspircd PROPERTIES COMPILE_DEFINITIONS "FMT_LIB_EXPORT" ENABLE_EXPORTS ON) install(TARGETS inspircd RUNTIME DESTINATION .) diff --git a/win/inspircd_memory_functions.cpp b/win/inspircd_memory_functions.cpp deleted file mode 100644 index c2535091f..000000000 --- a/win/inspircd_memory_functions.cpp +++ /dev/null @@ -1,73 +0,0 @@ -/* - * InspIRCd -- Internet Relay Chat Daemon - * - * Copyright (C) 2013 Sadie Powell - * Copyright (C) 2013 Adam - * Copyright (C) 2012 Robby - * Copyright (C) 2012 ChrisTX - * Copyright (C) 2009 John Brooks - * Copyright (C) 2007-2008 Craig Edwards - * Copyright (C) 2007 Robin Burchell - * Copyright (C) 2007 Dennis Friis - * - * This file is part of InspIRCd. InspIRCd is free software: you can - * redistribute it and/or modify it under the terms of the GNU General Public - * License as published by the Free Software Foundation, version 2. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -#include -#include - -#include - -/** On windows, all dll files and executables have their own private heap, - * whereas on POSIX systems, shared objects loaded into an executable share - * the executable's heap. This means that if we pass an arbitrary pointer to - * a windows DLL which is not allocated in that dll, without some form of - * marshalling, we get a page fault. To fix this, these overridden operators - * new and delete use the windows HeapAlloc and HeapFree functions to claim - * memory from the windows global heap. This makes windows 'act like' POSIX - * when it comes to memory usage between dlls and exes. - */ - -void * ::operator new(size_t iSize) -{ - void* ptr = HeapAlloc(GetProcessHeap(), 0, iSize); - /* This is the correct behaviour according to C++ standards for out of memory, - * not returning null -- Brain - */ - if (!ptr) - throw std::bad_alloc(); - else - return ptr; -} - -void ::operator delete(void * ptr) -{ - if (ptr) - HeapFree(GetProcessHeap(), 0, ptr); -} - -void * operator new[] (size_t iSize) -{ - void* ptr = HeapAlloc(GetProcessHeap(), 0, iSize); - if (!ptr) - throw std::bad_alloc(); - else - return ptr; -} - -void operator delete[] (void* ptr) -{ - if (ptr) - HeapFree(GetProcessHeap(), 0, ptr); -} diff --git a/win/inspircd_win32wrapper.cpp b/win/inspircd_win32wrapper.cpp deleted file mode 100644 index 7fa0f8513..000000000 --- a/win/inspircd_win32wrapper.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * InspIRCd -- Internet Relay Chat Daemon - * - * Copyright (C) 2013, 2019, 2021-2022 Sadie Powell - * Copyright (C) 2013 ChrisTX - * Copyright (C) 2012 Robby - * Copyright (C) 2007-2008 Craig Edwards - * Copyright (C) 2007 Dennis Friis - * - * This file is part of InspIRCd. InspIRCd is free software: you can - * redistribute it and/or modify it under the terms of the GNU General Public - * License as published by the Free Software Foundation, version 2. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -#include "inspircd.h" - -CWin32Exception::CWin32Exception() -{ - dwErrorCode = GetLastError(); - szErrorString = GetErrorMessage(dwErrorCode); - - for (size_t pos = 0; ((pos = szErrorString.find_first_of("\r\n", pos)) != std::string::npos); ) - szErrorString[pos] = ' '; -} - -CWin32Exception::CWin32Exception(const CWin32Exception& other) - : szErrorString(other.szErrorString) -{ -} - -const char* CWin32Exception::what() const noexcept -{ - return szErrorString.c_str(); -} - -DWORD CWin32Exception::GetErrorCode() -{ - return dwErrorCode; -} diff --git a/win/inspircd_win32wrapper.h b/win/inspircd_win32wrapper.h deleted file mode 100644 index 72f97caf1..000000000 --- a/win/inspircd_win32wrapper.h +++ /dev/null @@ -1,145 +0,0 @@ -/* - * InspIRCd -- Internet Relay Chat Daemon - * - * Copyright (C) 2020 Matt Schatz - * Copyright (C) 2013-2015 Attila Molnar - * Copyright (C) 2013, 2015, 2018-2019, 2021-2022 Sadie Powell - * Copyright (C) 2012-2013 ChrisTX - * Copyright (C) 2012 Robby - * Copyright (C) 2011, 2014, 2019 Adam - * Copyright (C) 2009 Uli Schlachter - * Copyright (C) 2007-2009 Robin Burchell - * Copyright (C) 2007-2008 Craig Edwards - * Copyright (C) 2007, 2009 Dennis Friis - * Copyright (C) 2007 burlex - * - * This file is part of InspIRCd. InspIRCd is free software: you can - * redistribute it and/or modify it under the terms of the GNU General Public - * License as published by the Free Software Foundation, version 2. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -#pragma once - -#include "win32service.h" - -/* Macros for exporting symbols - dependent on what is being compiled */ - -#ifdef DLL_BUILD -#define CoreExport __declspec(dllimport) -#define DllExport __declspec(dllexport) -#else -#define CoreExport __declspec(dllexport) -#define DllExport __declspec(dllimport) -#endif - - -/* Disable the deprecation warnings.. it spams :P */ -#define _CRT_SECURE_NO_DEPRECATE -#define _WINSOCK_DEPRECATED_NO_WARNINGS - -/* Normal windows (platform-specific) includes */ -#include -#pragma comment(lib, "Ws2_32.lib") -#include -#include -#include - -/* strcasecmp is not defined on windows by default */ -#define strcasecmp _stricmp -#define strncasecmp _strnicmp - -typedef SSIZE_T ssize_t; - -// warning: 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2' -// Normally, this is a huge problem, but due to our new/delete remap, we can ignore it. -#pragma warning(disable:4251) - -// warning: DLL-interface classkey 'identifier' used as base for DLL-interface classkey 'identifier' -#pragma warning(disable:4275) - -// warning: unreferenced formal parameter -// Unimportant for now, but for the next version, we should take a look at these again. -#pragma warning(disable:4100) - -// warning: 'class' : assignment operator could not be generated -#pragma warning(disable:4512) - -// warning C4127: conditional expression is constant -// This will be triggered like crazy because FOREACH_MOD and similar macros are wrapped in do { ... } while(0) constructs -#pragma warning(disable:4127) - -// warning C4996: The POSIX name for this item is deprecated. -#pragma warning(disable:4996) - -// warning C4244: conversion from 'x' to 'y', possible loss of data -#pragma warning(disable:4244) - -// warning C4267: 'var' : conversion from 'size_t' to 'type', possible loss of data -#pragma warning(disable:4267) - -// warning C4706: assignment within conditional expression -#pragma warning(disable:4706) - -// warning C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning) -#pragma warning(disable:4800) - -/* Shared memory allocation functions */ -void * ::operator new(size_t iSize); -void ::operator delete(void * ptr); - -#include - -class CWin32Exception final - : public std::exception -{ -public: - CWin32Exception(); - CWin32Exception(const CWin32Exception& other); - virtual const char* what() const noexcept; - DWORD GetErrorCode(); - -private: - std::string szErrorString; - DWORD dwErrorCode; -}; - -// POSIX iovec -struct iovec final -{ - void* iov_base; // Starting address - size_t iov_len; // Number of bytes to transfer -}; - -// Windows WSABUF with POSIX field names -struct WindowsIOVec final -{ - // POSIX iovec has iov_base then iov_len, WSABUF in Windows has the fields in reverse order - u_long iov_len; // Number of bytes to transfer - char FAR* iov_base; // Starting address -}; - -inline ssize_t writev(int fd, const WindowsIOVec* iov, int count) -{ - DWORD sent; - int ret = WSASend(fd, reinterpret_cast(const_cast(iov)), count, &sent, 0, nullptr, nullptr); - if (ret == 0) - return sent; - return -1; -} - -inline std::string GetErrorMessage(DWORD dwErrorCode) -{ - char szErrorString[1024]; - if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)szErrorString, _countof(szErrorString), nullptr) == 0) - sprintf_s(szErrorString, _countof(szErrorString), "Error code: %u", dwErrorCode); - return szErrorString; -} diff --git a/win/modules/CMakeLists.txt b/win/modules/CMakeLists.txt index 751f4ae3e..36219cfb0 100644 --- a/win/modules/CMakeLists.txt +++ b/win/modules/CMakeLists.txt @@ -64,8 +64,8 @@ foreach(MODULE_NAME ${INSPIRCD_MODULES}) target_link_libraries(${BASE_NAME} inspircd) add_dependencies(${BASE_NAME} inspircd) if(MSVC) - target_link_libraries(${BASE_NAME} win32_memory) - add_dependencies(${BASE_NAME} win32_memory) + target_link_libraries(${BASE_NAME} win32memory) + add_dependencies(${BASE_NAME} win32memory) endif() if(CONAN_CXX_FLAGS) diff --git a/win/win32memory.cpp b/win/win32memory.cpp new file mode 100644 index 000000000..c2535091f --- /dev/null +++ b/win/win32memory.cpp @@ -0,0 +1,73 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2013 Sadie Powell + * Copyright (C) 2013 Adam + * Copyright (C) 2012 Robby + * Copyright (C) 2012 ChrisTX + * Copyright (C) 2009 John Brooks + * Copyright (C) 2007-2008 Craig Edwards + * Copyright (C) 2007 Robin Burchell + * Copyright (C) 2007 Dennis Friis + * + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include +#include + +#include + +/** On windows, all dll files and executables have their own private heap, + * whereas on POSIX systems, shared objects loaded into an executable share + * the executable's heap. This means that if we pass an arbitrary pointer to + * a windows DLL which is not allocated in that dll, without some form of + * marshalling, we get a page fault. To fix this, these overridden operators + * new and delete use the windows HeapAlloc and HeapFree functions to claim + * memory from the windows global heap. This makes windows 'act like' POSIX + * when it comes to memory usage between dlls and exes. + */ + +void * ::operator new(size_t iSize) +{ + void* ptr = HeapAlloc(GetProcessHeap(), 0, iSize); + /* This is the correct behaviour according to C++ standards for out of memory, + * not returning null -- Brain + */ + if (!ptr) + throw std::bad_alloc(); + else + return ptr; +} + +void ::operator delete(void * ptr) +{ + if (ptr) + HeapFree(GetProcessHeap(), 0, ptr); +} + +void * operator new[] (size_t iSize) +{ + void* ptr = HeapAlloc(GetProcessHeap(), 0, iSize); + if (!ptr) + throw std::bad_alloc(); + else + return ptr; +} + +void operator delete[] (void* ptr) +{ + if (ptr) + HeapFree(GetProcessHeap(), 0, ptr); +} diff --git a/win/win32wrapper.cpp b/win/win32wrapper.cpp new file mode 100644 index 000000000..7fa0f8513 --- /dev/null +++ b/win/win32wrapper.cpp @@ -0,0 +1,48 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2013, 2019, 2021-2022 Sadie Powell + * Copyright (C) 2013 ChrisTX + * Copyright (C) 2012 Robby + * Copyright (C) 2007-2008 Craig Edwards + * Copyright (C) 2007 Dennis Friis + * + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "inspircd.h" + +CWin32Exception::CWin32Exception() +{ + dwErrorCode = GetLastError(); + szErrorString = GetErrorMessage(dwErrorCode); + + for (size_t pos = 0; ((pos = szErrorString.find_first_of("\r\n", pos)) != std::string::npos); ) + szErrorString[pos] = ' '; +} + +CWin32Exception::CWin32Exception(const CWin32Exception& other) + : szErrorString(other.szErrorString) +{ +} + +const char* CWin32Exception::what() const noexcept +{ + return szErrorString.c_str(); +} + +DWORD CWin32Exception::GetErrorCode() +{ + return dwErrorCode; +} diff --git a/win/win32wrapper.h b/win/win32wrapper.h new file mode 100644 index 000000000..72f97caf1 --- /dev/null +++ b/win/win32wrapper.h @@ -0,0 +1,145 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2020 Matt Schatz + * Copyright (C) 2013-2015 Attila Molnar + * Copyright (C) 2013, 2015, 2018-2019, 2021-2022 Sadie Powell + * Copyright (C) 2012-2013 ChrisTX + * Copyright (C) 2012 Robby + * Copyright (C) 2011, 2014, 2019 Adam + * Copyright (C) 2009 Uli Schlachter + * Copyright (C) 2007-2009 Robin Burchell + * Copyright (C) 2007-2008 Craig Edwards + * Copyright (C) 2007, 2009 Dennis Friis + * Copyright (C) 2007 burlex + * + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#pragma once + +#include "win32service.h" + +/* Macros for exporting symbols - dependent on what is being compiled */ + +#ifdef DLL_BUILD +#define CoreExport __declspec(dllimport) +#define DllExport __declspec(dllexport) +#else +#define CoreExport __declspec(dllexport) +#define DllExport __declspec(dllimport) +#endif + + +/* Disable the deprecation warnings.. it spams :P */ +#define _CRT_SECURE_NO_DEPRECATE +#define _WINSOCK_DEPRECATED_NO_WARNINGS + +/* Normal windows (platform-specific) includes */ +#include +#pragma comment(lib, "Ws2_32.lib") +#include +#include +#include + +/* strcasecmp is not defined on windows by default */ +#define strcasecmp _stricmp +#define strncasecmp _strnicmp + +typedef SSIZE_T ssize_t; + +// warning: 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2' +// Normally, this is a huge problem, but due to our new/delete remap, we can ignore it. +#pragma warning(disable:4251) + +// warning: DLL-interface classkey 'identifier' used as base for DLL-interface classkey 'identifier' +#pragma warning(disable:4275) + +// warning: unreferenced formal parameter +// Unimportant for now, but for the next version, we should take a look at these again. +#pragma warning(disable:4100) + +// warning: 'class' : assignment operator could not be generated +#pragma warning(disable:4512) + +// warning C4127: conditional expression is constant +// This will be triggered like crazy because FOREACH_MOD and similar macros are wrapped in do { ... } while(0) constructs +#pragma warning(disable:4127) + +// warning C4996: The POSIX name for this item is deprecated. +#pragma warning(disable:4996) + +// warning C4244: conversion from 'x' to 'y', possible loss of data +#pragma warning(disable:4244) + +// warning C4267: 'var' : conversion from 'size_t' to 'type', possible loss of data +#pragma warning(disable:4267) + +// warning C4706: assignment within conditional expression +#pragma warning(disable:4706) + +// warning C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning) +#pragma warning(disable:4800) + +/* Shared memory allocation functions */ +void * ::operator new(size_t iSize); +void ::operator delete(void * ptr); + +#include + +class CWin32Exception final + : public std::exception +{ +public: + CWin32Exception(); + CWin32Exception(const CWin32Exception& other); + virtual const char* what() const noexcept; + DWORD GetErrorCode(); + +private: + std::string szErrorString; + DWORD dwErrorCode; +}; + +// POSIX iovec +struct iovec final +{ + void* iov_base; // Starting address + size_t iov_len; // Number of bytes to transfer +}; + +// Windows WSABUF with POSIX field names +struct WindowsIOVec final +{ + // POSIX iovec has iov_base then iov_len, WSABUF in Windows has the fields in reverse order + u_long iov_len; // Number of bytes to transfer + char FAR* iov_base; // Starting address +}; + +inline ssize_t writev(int fd, const WindowsIOVec* iov, int count) +{ + DWORD sent; + int ret = WSASend(fd, reinterpret_cast(const_cast(iov)), count, &sent, 0, nullptr, nullptr); + if (ret == 0) + return sent; + return -1; +} + +inline std::string GetErrorMessage(DWORD dwErrorCode) +{ + char szErrorString[1024]; + if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)szErrorString, _countof(szErrorString), nullptr) == 0) + sprintf_s(szErrorString, _countof(szErrorString), "Error code: %u", dwErrorCode); + return szErrorString; +} -- cgit v1.3.1-10-gc9f91