From 0bb0df5b2cbfe81e6f440363d123f16618403661 Mon Sep 17 00:00:00 2001 From: janwas Date: Mon, 1 Jan 2007 21:25:47 +0000 Subject: [PATCH] # new year's cleanup (reduce dependencies, clean up headers) - remove headers always included from PCH - nommgr.h is only included ifdef REDEFINED_NEW (allows leaving out the mmgr stuff) - in lib/*.cpp, moved the corresponding include file to right behind the PCH (catches headers that aren't compilable by themselves) - byte_order no longer depends on SDL - add debug_level (another means of filtering debug output; needed for thesis) - split posix stuff up into subdirs (lib/posix and sysdep/win/wposix). makes including only some of the modules (e.g. sockets, time) much easier. This was SVN commit r4728. --- source/graphics/Color.h | 1 - source/graphics/HFTracer.h | 2 - source/graphics/MapIO.h | 2 +- source/graphics/MapReader.cpp | 1 - source/graphics/MapWriter.cpp | 9 +- source/graphics/TerrainProperties.cpp | 2 +- source/gui/GUI.h | 1 - source/gui/MiniMap.cpp | 4 +- source/lib/allocators.cpp | 11 +- source/lib/allocators.h | 14 +- source/lib/app_hooks.cpp | 6 +- source/lib/byte_order.cpp | 57 +- source/lib/byte_order.h | 25 + source/lib/config.h | 4 +- source/lib/debug.cpp | 16 +- source/lib/debug.h | 11 + source/lib/debug_stl.cpp | 2 +- source/lib/input.cpp | 2 +- source/lib/lib.cpp | 5 +- source/lib/lib.h | 9 +- source/lib/lib_errors.cpp | 7 +- source/lib/lockfree.cpp | 4 +- source/lib/lockfree.h | 2 +- source/lib/mmgr.cpp | 5 +- source/lib/mmgr.h | 10 +- source/lib/ogl.cpp | 5 +- source/lib/path_util.cpp | 3 +- source/lib/path_util.h | 2 +- source/lib/{ => posix}/posix.h | 45 +- source/lib/posix/posix_aio.h | 8 + source/lib/posix/posix_dlfcn.h | 7 + source/lib/posix/posix_errno.h | 5 + source/lib/posix/posix_filesystem.h | 8 + source/lib/posix/posix_mman.h | 7 + source/lib/posix/posix_pthread.h | 8 + source/lib/posix/posix_sock.h | 11 + source/lib/posix/posix_terminal.h | 7 + source/lib/posix/posix_time.h | 7 + source/lib/{ => posix}/posix_types.h | 10 +- source/lib/posix/posix_utsname.h | 7 + source/lib/res/file/archive.cpp | 1 + source/lib/res/file/archive_builder.cpp | 2 + source/lib/res/file/compression.cpp | 2 +- source/lib/res/file/file.cpp | 13 +- source/lib/res/file/file.h | 2 +- source/lib/res/file/file_cache.cpp | 2 + source/lib/res/file/file_io.cpp | 5 +- source/lib/res/file/file_stats.cpp | 15 +- source/lib/res/file/file_stats.h | 4 +- source/lib/res/file/file_util.cpp | 3 +- source/lib/res/file/path.cpp | 4 +- source/lib/res/file/trace.cpp | 1 + source/lib/res/file/vfs.cpp | 2 +- source/lib/res/file/vfs.h | 2 +- source/lib/res/file/vfs_mount.cpp | 9 +- source/lib/res/file/vfs_mount.h | 1 + source/lib/res/file/vfs_optimizer.cpp | 1 + source/lib/res/file/vfs_redirector.cpp | 4 +- source/lib/res/file/vfs_redirector.h | 3 + source/lib/res/file/vfs_tree.cpp | 3 +- source/lib/res/file/zip.cpp | 3 +- source/lib/res/graphics/cursor.cpp | 2 +- source/lib/res/graphics/ogl_shader.cpp | 2 +- source/lib/res/graphics/ogl_shader.h | 1 - source/lib/res/graphics/ogl_tex.cpp | 5 +- source/lib/res/graphics/ogl_tex.h | 1 - source/lib/res/graphics/tex.cpp | 4 +- source/lib/res/graphics/tex_codec.cpp | 4 +- source/lib/res/graphics/unifont.cpp | 8 +- source/lib/res/h_mgr.cpp | 8 +- source/lib/res/h_mgr.h | 2 +- source/lib/res/handle.h | 8 +- source/lib/res/mem.cpp | 11 +- source/lib/res/res.cpp | 2 +- source/lib/res/sound/ogghack.cpp | 3 +- source/lib/res/sound/snd_mgr.cpp | 4 +- source/lib/self_test.cpp | 1 + source/lib/self_test.h | 2 +- source/lib/string_s.cpp | 8 +- source/lib/string_s.h | 6 +- source/lib/sysdep/cpu.cpp | 3 +- source/lib/sysdep/gfx.cpp | 3 +- source/lib/sysdep/ia32.cpp | 7 +- source/lib/sysdep/ia32.h | 2 - source/lib/sysdep/snd.cpp | 2 +- source/lib/sysdep/sysdep.cpp | 1 - source/lib/sysdep/tests/test_sysdep.h | 2 +- source/lib/sysdep/win/delay_load.cpp | 2 +- source/lib/sysdep/win/dll_ver.cpp | 4 +- source/lib/sysdep/win/wcpu.cpp | 6 +- source/lib/sysdep/win/wdbg.cpp | 11 +- source/lib/sysdep/win/wdbg_sym.cpp | 13 +- source/lib/sysdep/win/wdir_watch.cpp | 3 +- source/lib/sysdep/win/wgfx.cpp | 2 +- source/lib/sysdep/win/win_internal.h | 1 - source/lib/sysdep/win/wposix.cpp | 965 ------------------ source/lib/sysdep/win/wposix.h | 450 -------- source/lib/sysdep/win/{ => wposix}/waio.cpp | 106 +- source/lib/sysdep/win/{ => wposix}/waio.h | 52 +- source/lib/sysdep/win/wposix/wdlfcn.cpp | 62 ++ source/lib/sysdep/win/wposix/wdlfcn.h | 20 + source/lib/sysdep/win/wposix/werrno.h | 94 ++ source/lib/sysdep/win/wposix/wfilesystem.cpp | 353 +++++++ source/lib/sysdep/win/wposix/wfilesystem.h | 96 ++ source/lib/sysdep/win/wposix/wmman.cpp | 207 ++++ source/lib/sysdep/win/wposix/wmman.h | 37 + source/lib/sysdep/win/wposix/wposix.cpp | 114 +++ source/lib/sysdep/win/wposix/wposix.h | 40 + .../lib/sysdep/win/wposix/wposix_internal.h | 34 + .../sysdep/win/{ => wposix}/wposix_types.h | 27 + .../lib/sysdep/win/{ => wposix}/wpthread.cpp | 15 +- source/lib/sysdep/win/{ => wposix}/wpthread.h | 8 +- source/lib/sysdep/win/{ => wposix}/wsock.cpp | 6 +- source/lib/sysdep/win/{ => wposix}/wsock.h | 8 +- source/lib/sysdep/win/wposix/wterminal.cpp | 88 ++ source/lib/sysdep/win/wposix/wterminal.h | 66 ++ source/lib/sysdep/win/{ => wposix}/wtime.cpp | 45 +- source/lib/sysdep/win/{ => wposix}/wtime.h | 9 - source/lib/sysdep/win/wposix/wtime_internal.h | 19 + source/lib/sysdep/win/wposix/wutsname.cpp | 68 ++ source/lib/sysdep/win/wposix/wutsname.h | 22 + source/lib/sysdep/win/wsdl.cpp | 6 +- source/lib/sysdep/win/wsdl.h | 1 - source/lib/sysdep/win/wsnd.cpp | 10 +- source/lib/sysdep/win/wsysdep.cpp | 3 +- source/lib/tests/test_lockfree.h | 1 - source/lib/tests/test_string_s.h | 2 +- source/lib/timer.cpp | 5 +- source/lib/types.h | 2 +- source/network/AllNetMessages.h | 3 +- source/network/NetMessage.cpp | 1 - source/network/NetMessage.h | 1 - source/network/Network.cpp | 3 +- source/network/Network.h | 2 - source/network/Serialization.h | 1 - source/network/SocketBase.cpp | 2 - source/network/SocketBase.h | 3 +- source/network/StreamSocket.h | 1 - source/ps/CStr.cpp | 2 +- source/ps/DllLoader.cpp | 2 +- source/ps/Errors.cpp | 64 +- source/ps/Errors.h | 4 +- source/ps/FilePacker.h | 1 - source/ps/FileUnpacker.h | 1 - source/ps/Overlay.h | 2 - source/ps/StringConvert.cpp | 1 - source/ps/ThreadUtil.h | 2 +- source/ps/Util.cpp | 2 + source/ps/XML/XMLUtils.cpp | 2 +- source/ps/utf16string.h | 1 - source/simulation/AStarEngine.h | 1 - source/simulation/EntityHandles.h | 2 - source/simulation/Simulation.cpp | 2 +- source/simulation/Simulation.h | 1 - source/simulation/TurnManager.h | 2 - source/tools/atlas/GameInterface/Shareable.h | 2 +- 156 files changed, 1944 insertions(+), 1786 deletions(-) rename source/lib/{ => posix}/posix.h (79%) create mode 100644 source/lib/posix/posix_aio.h create mode 100644 source/lib/posix/posix_dlfcn.h create mode 100644 source/lib/posix/posix_errno.h create mode 100644 source/lib/posix/posix_filesystem.h create mode 100644 source/lib/posix/posix_mman.h create mode 100644 source/lib/posix/posix_pthread.h create mode 100644 source/lib/posix/posix_sock.h create mode 100644 source/lib/posix/posix_terminal.h create mode 100644 source/lib/posix/posix_time.h rename source/lib/{ => posix}/posix_types.h (92%) create mode 100644 source/lib/posix/posix_utsname.h delete mode 100644 source/lib/sysdep/win/wposix.cpp delete mode 100644 source/lib/sysdep/win/wposix.h rename source/lib/sysdep/win/{ => wposix}/waio.cpp (88%) rename source/lib/sysdep/win/{ => wposix}/waio.h (62%) create mode 100644 source/lib/sysdep/win/wposix/wdlfcn.cpp create mode 100644 source/lib/sysdep/win/wposix/wdlfcn.h create mode 100644 source/lib/sysdep/win/wposix/werrno.h create mode 100644 source/lib/sysdep/win/wposix/wfilesystem.cpp create mode 100644 source/lib/sysdep/win/wposix/wfilesystem.h create mode 100644 source/lib/sysdep/win/wposix/wmman.cpp create mode 100644 source/lib/sysdep/win/wposix/wmman.h create mode 100644 source/lib/sysdep/win/wposix/wposix.cpp create mode 100644 source/lib/sysdep/win/wposix/wposix.h create mode 100644 source/lib/sysdep/win/wposix/wposix_internal.h rename source/lib/sysdep/win/{ => wposix}/wposix_types.h (76%) rename source/lib/sysdep/win/{ => wposix}/wpthread.cpp (98%) rename source/lib/sysdep/win/{ => wposix}/wpthread.h (97%) rename source/lib/sysdep/win/{ => wposix}/wsock.cpp (97%) rename source/lib/sysdep/win/{ => wposix}/wsock.h (97%) create mode 100644 source/lib/sysdep/win/wposix/wterminal.cpp create mode 100644 source/lib/sysdep/win/wposix/wterminal.h rename source/lib/sysdep/win/{ => wposix}/wtime.cpp (95%) rename source/lib/sysdep/win/{ => wposix}/wtime.h (83%) create mode 100644 source/lib/sysdep/win/wposix/wtime_internal.h create mode 100644 source/lib/sysdep/win/wposix/wutsname.cpp create mode 100644 source/lib/sysdep/win/wposix/wutsname.h diff --git a/source/graphics/Color.h b/source/graphics/Color.h index 7eae8ea309..d4ef0cae3c 100644 --- a/source/graphics/Color.h +++ b/source/graphics/Color.h @@ -11,7 +11,6 @@ #include "maths/Vector3D.h" #include "maths/Vector4D.h" -#include "lib/types.h" // simple defines for 3 and 4 component floating point colors - just map to // corresponding vector types diff --git a/source/graphics/HFTracer.h b/source/graphics/HFTracer.h index 7bbb1b6fbd..de3d3169a2 100644 --- a/source/graphics/HFTracer.h +++ b/source/graphics/HFTracer.h @@ -12,8 +12,6 @@ class CVector3D; class CTerrain; -#include "lib/types.h" - /////////////////////////////////////////////////////////////////////////////// // CHFTracer: a class for determining ray intersections with a heightfield class CHFTracer diff --git a/source/graphics/MapIO.h b/source/graphics/MapIO.h index 601fe9cdc7..1415ca2211 100644 --- a/source/graphics/MapIO.h +++ b/source/graphics/MapIO.h @@ -14,7 +14,7 @@ public: struct STileDesc { // index into the texture array of first texture on tile u16 m_Tex1Index; - // index into the texture array of second texture; (0xffff) if none + // index into the texture array of second texture; (0xFFFF) if none u16 m_Tex2Index; // priority u32 m_Priority; diff --git a/source/graphics/MapReader.cpp b/source/graphics/MapReader.cpp index 9d4afbdbd3..329fc09e0b 100644 --- a/source/graphics/MapReader.cpp +++ b/source/graphics/MapReader.cpp @@ -13,7 +13,6 @@ #include "graphics/Unit.h" #include "graphics/UnitManager.h" #include "lib/timer.h" -#include "lib/types.h" #include "maths/MathUtil.h" #include "ps/CLogger.h" #include "ps/Game.h" diff --git a/source/graphics/MapWriter.cpp b/source/graphics/MapWriter.cpp index f255f476b0..1e3e6432f8 100644 --- a/source/graphics/MapWriter.cpp +++ b/source/graphics/MapWriter.cpp @@ -1,6 +1,5 @@ #include "precompiled.h" -#include "lib/types.h" #include "lib/res/file/vfs.h" #include "Camera.h" @@ -57,7 +56,7 @@ void CMapWriter::SaveMap(const char* filename, CTerrain* pTerrain, } /////////////////////////////////////////////////////////////////////////////////////////////////// -// GetHandleIndex: return the index of the given handle in the given list; or 0xffff if +// GetHandleIndex: return the index of the given handle in the given list; or 0xFFFF if // handle isn't in list static u16 GetHandleIndex(const Handle handle, const std::vector& handles) { @@ -68,7 +67,7 @@ static u16 GetHandleIndex(const Handle handle, const std::vector& handle } } - return 0xffff; + return 0xFFFF; } /////////////////////////////////////////////////////////////////////////////////////////////////// @@ -93,13 +92,13 @@ void CMapWriter::EnumTerrainTextures(CTerrain *pTerrain, for (u32 k=0;k<(u32)PATCH_SIZE;k++) { CMiniPatch& mp=pTerrain->GetPatch(i,j)->m_MiniPatches[m][k]; u16 index=u16(GetHandleIndex(mp.Tex1,handles)); - if (index==0xffff) { + if (index==0xFFFF) { index=(u16)handles.size(); handles.push_back(mp.Tex1); } tileptr->m_Tex1Index=index; - tileptr->m_Tex2Index=0xffff; + tileptr->m_Tex2Index=0xFFFF; tileptr->m_Priority=mp.Tex1Priority; tileptr++; } diff --git a/source/graphics/TerrainProperties.cpp b/source/graphics/TerrainProperties.cpp index 86362642a7..2e19bbeb8e 100644 --- a/source/graphics/TerrainProperties.cpp +++ b/source/graphics/TerrainProperties.cpp @@ -241,7 +241,7 @@ u32 CTerrainProperties::GetBaseColor() return m_pParent->GetBaseColor(); else // White, full opacity.. but this value shouldn't ever be used - return 0xffffffff; + return 0xFFFFFFFF; } const STerrainPassability &CTerrainProperties::GetPassability(HEntity entity) diff --git a/source/gui/GUI.h b/source/gui/GUI.h index fa8593db53..b053e6010e 100644 --- a/source/gui/GUI.h +++ b/source/gui/GUI.h @@ -27,7 +27,6 @@ gee@pyro.nu #include "ps/Pyrogenesis.h" #include "ps/CStr.h" -#include "lib/types.h" #include "lib/ogl.h" #include "GUIbase.h" diff --git a/source/gui/MiniMap.cpp b/source/gui/MiniMap.cpp index 7a29135854..f16ea9afba 100644 --- a/source/gui/MiniMap.cpp +++ b/source/gui/MiniMap.cpp @@ -512,11 +512,11 @@ void CMiniMap::RebuildTerrainTexture() if(mp) { CTextureEntry *tex = mp->Tex1 ? g_TexMan.FindTexture(mp->Tex1) : 0; - color = tex ? tex->GetBaseColor() : 0xffffffff; + color = tex ? tex->GetBaseColor() : 0xFFFFFFFF; } else { - color = 0xffffffff; + color = 0xFFFFFFFF; } *dataPtr++ = ScaleColor(color, float(val) / 255.0f); diff --git a/source/lib/allocators.cpp b/source/lib/allocators.cpp index 015bcf9379..a95d93be2b 100644 --- a/source/lib/allocators.cpp +++ b/source/lib/allocators.cpp @@ -9,7 +9,7 @@ */ /* - * Copyright (c) 2005 Jan Wassenberg + * Copyright (c) 2005-2006 Jan Wassenberg * * Redistribution and/or modification are also permitted under the * terms of the GNU General Public License as published by the @@ -21,12 +21,13 @@ */ #include "precompiled.h" +#include "allocators.h" -#include "lib/posix.h" // PROT_* constants for da_set_prot +#include "lib/posix/posix_mman.h" // PROT_* constants for da_set_prot +#include "lib/posix/posix.h" // sysconf #include "lib/sysdep/cpu.h" // CAS #include "byte_order.h" -#include "lib/res/file/file_io.h" // IO_EOF -#include "allocators.h" + //----------------------------------------------------------------------------- @@ -374,7 +375,7 @@ LibError da_read(DynArray* da, void* data, size_t size) { // make sure we have enough data to read if(da->pos+size > da->cur_size) - WARN_RETURN(ERR::IO_EOF); + WARN_RETURN(ERR::FAIL); memcpy2(data, da->base+da->pos, size); da->pos += size; diff --git a/source/lib/allocators.h b/source/lib/allocators.h index 1360c408ec..3ea81279ce 100644 --- a/source/lib/allocators.h +++ b/source/lib/allocators.h @@ -9,7 +9,7 @@ */ /* - * Copyright (c) 2005 Jan Wassenberg + * Copyright (c) 2005-2006 Jan Wassenberg * * Redistribution and/or modification are also permitted under the * terms of the GNU General Public License as published by the @@ -25,9 +25,9 @@ #include -#include "lib/types.h" +#include "lib/posix/posix_mman.h" // PROT_* #include "lib/sysdep/cpu.h" // CAS -#include "lib/posix.h" // PROT_* constants for da_set_prot + // // page aligned allocator @@ -487,6 +487,9 @@ doSomethingWith(yc); // read/write access your_class_wrapper.lock(); // disallow further access until next .get() .. **/ +#ifdef REDEFINED_NEW +# include "lib/nommgr.h" +#endif template class OverrunProtector { DynArray da; @@ -532,9 +535,7 @@ fail: if(da_set_size(&da, sizeof(T)) < 0) goto fail; -#include "nommgr.h" cached_ptr = new(da.base) T(); -#include "mmgr.h" lock(); } @@ -561,6 +562,9 @@ public: return cached_ptr; } }; +#ifdef REDEFINED_NEW +# include "lib/mmgr.h" +#endif // diff --git a/source/lib/app_hooks.cpp b/source/lib/app_hooks.cpp index 55991d12ae..a9b7d8e4c3 100644 --- a/source/lib/app_hooks.cpp +++ b/source/lib/app_hooks.cpp @@ -21,18 +21,16 @@ */ #include "precompiled.h" +#include "app_hooks.h" #include #include +#include "lib/path_util.h" #include "lib/sysdep/gfx.h" #include "lib/res/graphics/ogl_tex.h" #include "lib/res/file/file.h" #include "lib/res/file/vfs.h" -#include "lib/res/graphics/ogl_tex.h" -#include "lib/path_util.h" - -#include "app_hooks.h" //----------------------------------------------------------------------------- diff --git a/source/lib/byte_order.cpp b/source/lib/byte_order.cpp index 6e760e534c..d6212b9163 100644 --- a/source/lib/byte_order.cpp +++ b/source/lib/byte_order.cpp @@ -21,15 +21,44 @@ */ #include "precompiled.h" - #include "byte_order.h" -#include "sdl.h" +#ifndef swap16 + +u16 swap16(const u16 x) +{ + return (u16)(((x & 0xff) << 8) | (x >> 8)); +} + +u32 swap32(const u32 x) +{ + return (x << 24) | + (x >> 24) | + ((x << 8) & 0x00ff0000) | + ((x >> 8) & 0x0000ff00); +} + +u64 swap64(const u64 x) +{ + const u32 lo = (u32)(x & 0xFFFFFFFF); + const u32 hi = (u32)(x >> 32); + u64 ret = swap32(lo); + ret <<= 32; + // careful: must shift var of type u64, not u32 + ret |= swap32(hi); + return ret; +} + +#endif // #ifndef swap16 + + +//----------------------------------------------------------------------------- + u16 to_le16(u16 x) { -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - return SDL_Swap16(x); +#if BYTE_ORDER == BIG_ENDIAN + return swap16(x); #else return x; #endif @@ -37,8 +66,8 @@ u16 to_le16(u16 x) u32 to_le32(u32 x) { -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - return SDL_Swap32(x); +#if BYTE_ORDER == BIG_ENDIAN + return swap32(x); #else return x; #endif @@ -46,8 +75,8 @@ u32 to_le32(u32 x) u64 to_le64(u64 x) { -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - return SDL_Swap64(x); +#if BYTE_ORDER == BIG_ENDIAN + return swap64(x); #else return x; #endif @@ -56,28 +85,28 @@ u64 to_le64(u64 x) u16 to_be16(u16 x) { -#if SDL_BYTEORDER == SDL_BIG_ENDIAN +#if BYTE_ORDER == BIG_ENDIAN return x; #else - return SDL_Swap16(x); + return swap16(x); #endif } u32 to_be32(u32 x) { -#if SDL_BYTEORDER == SDL_BIG_ENDIAN +#if BYTE_ORDER == BIG_ENDIAN return x; #else - return SDL_Swap32(x); + return swap32(x); #endif } u64 to_be64(u64 x) { -#if SDL_BYTEORDER == SDL_BIG_ENDIAN +#if BYTE_ORDER == BIG_ENDIAN return x; #else - return SDL_Swap64(x); + return swap64(x); #endif } diff --git a/source/lib/byte_order.h b/source/lib/byte_order.h index 84b82f34ee..51700e17f0 100644 --- a/source/lib/byte_order.h +++ b/source/lib/byte_order.h @@ -20,6 +20,9 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ +#ifndef INCLUDED_BYTE_ORDER +#define INCLUDED_BYTE_ORDER + #include "config.h" @@ -83,3 +86,25 @@ extern void write_le64(void* p, u64 x); /// see write_le16 extern void write_be16(void* p, u16 x); extern void write_be32(void* p, u32 x); /// see write_be16 extern void write_be64(void* p, u64 x); /// see write_be16 + + +// Debug-mode ICC doesn't like the intrinsics, so only use them +// for MSVC and non-debug ICC. +#if MSC_VERSION && !( defined(__INTEL_COMPILER) && !defined(NDEBUG) ) +extern unsigned short _byteswap_ushort(unsigned short); +extern unsigned long _byteswap_ulong(unsigned long); +extern unsigned __int64 _byteswap_uint64(unsigned __int64); +#pragma intrinsic(_byteswap_ushort) +#pragma intrinsic(_byteswap_ulong) +#pragma intrinsic(_byteswap_uint64) +# define swap16 _byteswap_ushort +# define swap32 _byteswap_ulong +# define swap64 _byteswap_uint64 +#else +extern u16 swap16(const u16 x); +extern u32 swap32(const u32 x); +extern u64 swap64(const u64 x); +#endif // no swap intrinsics + + +#endif // #ifndef INCLUDED_BYTE_ORDER diff --git a/source/lib/config.h b/source/lib/config.h index 6fbe60519e..bf801452f3 100644 --- a/source/lib/config.h +++ b/source/lib/config.h @@ -368,9 +368,9 @@ // these are always available to users: if not provided by the CRT, we // implement them ourselves. this flag is only used to skip our impl. #if MSC_VERSION >= 1400 -# define HAVE_STRING_S 1 +# define HAVE_SECURE_CRT 1 #else -# define HAVE_STRING_S 0 +# define HAVE_SECURE_CRT 0 #endif diff --git a/source/lib/debug.cpp b/source/lib/debug.cpp index 33e03fa020..cf6122b158 100644 --- a/source/lib/debug.cpp +++ b/source/lib/debug.cpp @@ -21,21 +21,23 @@ */ #include "precompiled.h" +#include "debug.h" #include #include +#include "lib/posix/posix_pthread.h" #include "lib.h" -#include "posix.h" -#include "lib/res/file/file.h" // FILE_ACCESS +#include "app_hooks.h" +#include "path_util.h" +#include "debug_stl.h" #include "lib/sysdep/cpu.h" // CAS +#include "lib/res/file/file.h" // FILE_ACCESS // some functions here are called from within mmgr; disable its hooks // so that our allocations don't cause infinite recursion. -#include "nommgr.h" -#include "app_hooks.h" -#include "lib/path_util.h" -#include "debug_stl.h" -#include "debug.h" +#ifdef REDEFINED_NEW +# include "lib/nommgr.h" +#endif AT_STARTUP(\ diff --git a/source/lib/debug.h b/source/lib/debug.h index f08e908a5c..5cb46753bc 100644 --- a/source/lib/debug.h +++ b/source/lib/debug.h @@ -110,6 +110,17 @@ extern void debug_heap_enable(DebugHeapChecks what); // output //----------------------------------------------------------------------------- +enum DebugLevel +{ + DEBUG_LEVEL_NONE = 0, + DEBUG_LEVEL_BRIEF = 2, + DEBUG_LEVEL_DETAILED = 5, + DEBUG_LEVEL_FULL = 9, +}; +#define DEBUG_PRINTF_BRIEF if(debug_level >= DEBUG_LEVEL_BRIEF) debug_printf +#define DEBUG_PRINTF_DETAILED if(debug_level >= DEBUG_LEVEL_DETAILED) debug_printf +#define DEBUG_PRINTF_FULL if(debug_level >= DEBUG_LEVEL_FULL ) debug_printf + /** * write a formatted string to the debug channel, subject to filtering * (see below). implemented via debug_puts - see performance note there. diff --git a/source/lib/debug_stl.cpp b/source/lib/debug_stl.cpp index 8e701277e8..9355ae1795 100644 --- a/source/lib/debug_stl.cpp +++ b/source/lib/debug_stl.cpp @@ -21,13 +21,13 @@ */ #include "precompiled.h" +#include "debug_stl.h" #include #include #include #include -#include "debug_stl.h" #include "lib.h" // match_wildcard diff --git a/source/lib/input.cpp b/source/lib/input.cpp index 19e2feb9d6..b2abf162b4 100644 --- a/source/lib/input.cpp +++ b/source/lib/input.cpp @@ -22,12 +22,12 @@ */ #include "precompiled.h" +#include "input.h" #include #include #include "lib.h" -#include "input.h" #include "sdl.h" #include "lib/res/file/file.h" diff --git a/source/lib/lib.cpp b/source/lib/lib.cpp index da8a77df63..503f7da272 100644 --- a/source/lib/lib.cpp +++ b/source/lib/lib.cpp @@ -21,16 +21,13 @@ */ #include "precompiled.h" - +#include "lib.h" #include #include #include -#include "lib/types.h" -#include "lib.h" #include "lib/app_hooks.h" - #include "lib/sysdep/sysdep.h" //----------------------------------------------------------------------------- diff --git a/source/lib/lib.h b/source/lib/lib.h index 3bf8a5b33a..665c9765b0 100644 --- a/source/lib/lib.h +++ b/source/lib/lib.h @@ -60,7 +60,6 @@ scope #include // fabsf #include "config.h" -#include "lib/types.h" #if defined(__cplusplus) @@ -456,9 +455,13 @@ extern u16 subusw(u16 x, u16 y); * least-significant bit. only division, sqrt, sin/cos and other * trancendental operations introduce error. **/ -inline bool feq(float f1, float f2) +inline bool feq(double d1, double d2, double epsilon = 0.00001) +{ + return fabs(d1 - d2) < epsilon; +} + +inline bool feqf(float f1, float f2, float epsilon = 0.001f) { - const float epsilon = 0.00001f; return fabsf(f1 - f2) < epsilon; } diff --git a/source/lib/lib_errors.cpp b/source/lib/lib_errors.cpp index 8516fc74cb..769117745e 100644 --- a/source/lib/lib_errors.cpp +++ b/source/lib/lib_errors.cpp @@ -26,15 +26,16 @@ // object files of the same name but in different paths. #include "precompiled.h" - #include "lib_errors.h" -#include "lib/sysdep/sysdep.h" #include #include // abs -#include #include +#include "lib/posix/posix_errno.h" +#include "lib/sysdep/sysdep.h" + + struct LibErrorAssociation { const char* description_; diff --git a/source/lib/lockfree.cpp b/source/lib/lockfree.cpp index 9e204067df..526d41113d 100644 --- a/source/lib/lockfree.cpp +++ b/source/lib/lockfree.cpp @@ -21,14 +21,14 @@ */ #include "precompiled.h" +#include "lockfree.h" #include #include +#include "lib/posix/posix_pthread.h" #include "lib.h" -#include "posix.h" #include "lib/sysdep/cpu.h" -#include "lockfree.h" #include "timer.h" /* diff --git a/source/lib/lockfree.h b/source/lib/lockfree.h index ab3947e49d..fb364e18e7 100644 --- a/source/lib/lockfree.h +++ b/source/lib/lockfree.h @@ -23,7 +23,7 @@ #ifndef LOCKFREE_H__ #define LOCKFREE_H__ -#include "posix_types.h" // uintptr_t +#include "posix/posix_types.h" // uintptr_t /* diff --git a/source/lib/mmgr.cpp b/source/lib/mmgr.cpp index f035c30504..99abdd2a1b 100644 --- a/source/lib/mmgr.cpp +++ b/source/lib/mmgr.cpp @@ -34,16 +34,15 @@ AT_STARTUP(\ // but this is preferable to stubbing them out here ("least surprise"). #if CONFIG_USE_MMGR +#include "mmgr.h" + #include #include - #include #include #include - #include -#include "mmgr.h" #include "lib.h" #include "posix.h" #include "debug.h" diff --git a/source/lib/mmgr.h b/source/lib/mmgr.h index 173a958c2e..2b2f97f0f6 100644 --- a/source/lib/mmgr.h +++ b/source/lib/mmgr.h @@ -120,8 +120,6 @@ namespace ERR // optimizations, so duplicate this #if. #if CONFIG_USE_MMGR -#include "lib/types.h" - // // optional additional checks, enabled via mmgr_set_options. // these slow down the application; see 'digging deeper' in documentation. @@ -225,7 +223,13 @@ extern void operator delete[](void* p, const char* file, int line, const char* f #include "nommgr.h" -// MMGR version: +#if CONFIG_USE_MMGR || HAVE_VC_DEBUG_ALLOC +// notify user code that they must #include "nommgr.h" in places +// where our macro would cause breakage (e.g. placement new) +# define REDEFINED_NEW +#endif + +// mmgr version: // (to simplify code that may either use mmgr or the VC debug heap, // we support enabling/disabling both in this header) #if CONFIG_USE_MMGR diff --git a/source/lib/ogl.cpp b/source/lib/ogl.cpp index b7ecf72e2c..2d7300a9d9 100644 --- a/source/lib/ogl.cpp +++ b/source/lib/ogl.cpp @@ -21,7 +21,7 @@ */ #include "precompiled.h" - +#include "ogl.h" #include #include @@ -29,9 +29,8 @@ #include "lib.h" #include "sdl.h" -#include "ogl.h" -#include "lib/sysdep/gfx.h" #include "debug.h" +#include "lib/sysdep/gfx.h" #include "lib/res/h_mgr.h" #include "lib/res/graphics/tex.h" diff --git a/source/lib/path_util.cpp b/source/lib/path_util.cpp index 58c44c4327..af1a96bb56 100644 --- a/source/lib/path_util.cpp +++ b/source/lib/path_util.cpp @@ -21,12 +21,11 @@ */ #include "precompiled.h" +#include "path_util.h" #include #include "lib.h" -#include "posix.h" -#include "path_util.h" AT_STARTUP(\ diff --git a/source/lib/path_util.h b/source/lib/path_util.h index f07f7c9508..7f9fa34bb0 100644 --- a/source/lib/path_util.h +++ b/source/lib/path_util.h @@ -33,7 +33,7 @@ #ifndef PATH_UTIL_H__ #define PATH_UTIL_H__ -#include "posix.h" // PATH_MAX +#include "posix/posix_types.h" // PATH_MAX namespace ERR diff --git a/source/lib/posix.h b/source/lib/posix/posix.h similarity index 79% rename from source/lib/posix.h rename to source/lib/posix/posix.h index d477f149e0..312b49cb99 100644 --- a/source/lib/posix.h +++ b/source/lib/posix/posix.h @@ -53,36 +53,23 @@ need only be renamed (e.g. _open, _stat). */ +#ifndef INCLUDED_POSIX +#define INCLUDED_POSIX + #if OS_WIN - -#include "lib/sysdep/win/wposix.h" -#include "lib/sysdep/win/win.h" - -#else +# include "lib/sysdep/win/wposix/wposix.h" +# include "lib/sysdep/win/win.h" +#endif #include "posix_types.h" +#include "posix_aio.h" +#include "posix_dlfcn.h" +#include "posix_filesystem.h" +#include "posix_mman.h" +#include "posix_pthread.h" +#include "posix_sock.h" +#include "posix_terminal.h" +#include "posix_time.h" +#include "posix_utsname.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include - -#endif // #if OS_WIN +#endif // #ifndef INCLUDED_POSIX diff --git a/source/lib/posix/posix_aio.h b/source/lib/posix/posix_aio.h new file mode 100644 index 0000000000..4621ce3171 --- /dev/null +++ b/source/lib/posix/posix_aio.h @@ -0,0 +1,8 @@ +#if OS_WIN +# include "lib/sysdep/win/wposix/waio.h" +#else +# include +# include +#endif + +#include "posix_errno.h" // for user convenience diff --git a/source/lib/posix/posix_dlfcn.h b/source/lib/posix/posix_dlfcn.h new file mode 100644 index 0000000000..a38ecff049 --- /dev/null +++ b/source/lib/posix/posix_dlfcn.h @@ -0,0 +1,7 @@ +#if OS_WIN +# include "lib/sysdep/win/wposix/wdlfcn.h" +#else +# include +#endif + +#include "posix_errno.h" // for user convenience diff --git a/source/lib/posix/posix_errno.h b/source/lib/posix/posix_errno.h new file mode 100644 index 0000000000..6d69359ec8 --- /dev/null +++ b/source/lib/posix/posix_errno.h @@ -0,0 +1,5 @@ +#if OS_WIN +# include "lib/sysdep/win/wposix/werrno.h" +#else +# include +#endif diff --git a/source/lib/posix/posix_filesystem.h b/source/lib/posix/posix_filesystem.h new file mode 100644 index 0000000000..022ecb937b --- /dev/null +++ b/source/lib/posix/posix_filesystem.h @@ -0,0 +1,8 @@ +#if OS_WIN +# include "lib/sysdep/win/wposix/wfilesystem.h" +#else +# include +# include +#endif + +#include "posix_errno.h" // for user convenience diff --git a/source/lib/posix/posix_mman.h b/source/lib/posix/posix_mman.h new file mode 100644 index 0000000000..b2e24af1bc --- /dev/null +++ b/source/lib/posix/posix_mman.h @@ -0,0 +1,7 @@ +#if OS_WIN +# include "lib/sysdep/win/wposix/wmman.h" +#else +# include +#endif + +#include "posix_errno.h" // for user convenience diff --git a/source/lib/posix/posix_pthread.h b/source/lib/posix/posix_pthread.h new file mode 100644 index 0000000000..9e6ee6b739 --- /dev/null +++ b/source/lib/posix/posix_pthread.h @@ -0,0 +1,8 @@ +#if OS_WIN +# include "lib/sysdep/win/wposix/wpthread.h" +#else +# include +# include +#endif + +#include "posix_errno.h" // for user convenience diff --git a/source/lib/posix/posix_sock.h b/source/lib/posix/posix_sock.h new file mode 100644 index 0000000000..ccebe8d5d5 --- /dev/null +++ b/source/lib/posix/posix_sock.h @@ -0,0 +1,11 @@ +#if OS_WIN +# include "lib/sysdep/win/wposix/wsock.h" +#else +# include +# include +# include +# include +# include +#endif + +#include "posix_errno.h" // for user convenience diff --git a/source/lib/posix/posix_terminal.h b/source/lib/posix/posix_terminal.h new file mode 100644 index 0000000000..2bc6432c12 --- /dev/null +++ b/source/lib/posix/posix_terminal.h @@ -0,0 +1,7 @@ +#if OS_WIN +# include "lib/sysdep/win/wposix/wterminal.h" +#else +# include +#endif + +#include "posix_errno.h" // for user convenience diff --git a/source/lib/posix/posix_time.h b/source/lib/posix/posix_time.h new file mode 100644 index 0000000000..2e196c2715 --- /dev/null +++ b/source/lib/posix/posix_time.h @@ -0,0 +1,7 @@ +#if OS_WIN +# include "lib/sysdep/win/wposix/wtime.h" +#else +# include +#endif + +#include "posix_errno.h" // for user convenience diff --git a/source/lib/posix_types.h b/source/lib/posix/posix_types.h similarity index 92% rename from source/lib/posix_types.h rename to source/lib/posix/posix_types.h index 1448ae513b..67a3c45c99 100644 --- a/source/lib/posix_types.h +++ b/source/lib/posix/posix_types.h @@ -28,18 +28,24 @@ #include #include #include +#include #if OS_WIN -# include "lib/sysdep/win/wposix_types.h" +# include "lib/sysdep/win/wposix/wposix_types.h" #else + // unix/linux/glibc/gcc says that this macro has to be defined when including // stdint.h from C++ for stdint.h to define SIZE_MAX and friends # ifndef __STDC_LIMIT_MACROS # define __STDC_LIMIT_MACROS # endif # include + // but sometimes it still doesn't get defined, so define it ourselves # ifndef SIZE_MAX # define SIZE_MAX ((size_t)-1) # endif -#endif // #if OS_WIN + +#include + +#endif // #if !OS_WIN diff --git a/source/lib/posix/posix_utsname.h b/source/lib/posix/posix_utsname.h new file mode 100644 index 0000000000..1089e8ab4a --- /dev/null +++ b/source/lib/posix/posix_utsname.h @@ -0,0 +1,7 @@ +#if OS_WIN +# include "lib/sysdep/win/wposix/wutsname.h" +#else +# include +#endif + +#include "posix_errno.h" // for user convenience diff --git a/source/lib/res/file/archive.cpp b/source/lib/res/file/archive.cpp index 16fc3183c7..b91b65d91d 100644 --- a/source/lib/res/file/archive.cpp +++ b/source/lib/res/file/archive.cpp @@ -22,6 +22,7 @@ */ #include "precompiled.h" +#include "archive.h" #include "lib/timer.h" #include "lib/allocators.h" diff --git a/source/lib/res/file/archive_builder.cpp b/source/lib/res/file/archive_builder.cpp index ef12faffa8..cabd5fe317 100644 --- a/source/lib/res/file/archive_builder.cpp +++ b/source/lib/res/file/archive_builder.cpp @@ -21,6 +21,7 @@ */ #include "precompiled.h" +#include "archive_builder.h" #include "lib/lib.h" #include "lib/timer.h" @@ -30,6 +31,7 @@ #include "ps/Loader.h" #include + static inline bool file_type_is_uncompressible(const char* fn) { const char* ext = path_extension(fn); diff --git a/source/lib/res/file/compression.cpp b/source/lib/res/file/compression.cpp index 8a91692896..744acca86f 100644 --- a/source/lib/res/file/compression.cpp +++ b/source/lib/res/file/compression.cpp @@ -22,13 +22,13 @@ */ #include "precompiled.h" +#include "compression.h" #include #include "lib/res/mem.h" #include "lib/allocators.h" #include "lib/timer.h" -#include "compression.h" #include "file_io.h" // IO_EOF diff --git a/source/lib/res/file/file.cpp b/source/lib/res/file/file.cpp index 54ce53e3ba..7ca6add84a 100644 --- a/source/lib/res/file/file.cpp +++ b/source/lib/res/file/file.cpp @@ -22,7 +22,15 @@ */ #include "precompiled.h" +#include "file.h" +#include +#include +#include + +#include "lib/posix/posix_filesystem.h" +#include "lib/posix/posix_aio.h" +#include "lib/posix/posix_mman.h" #include "lib/lib.h" #include "lib/adts.h" #include "lib/sysdep/sysdep.h" @@ -30,11 +38,6 @@ #include "lib/allocators.h" #include "file_internal.h" -#include -#include - -#include - AT_STARTUP(\ error_setDescription(ERR::FILE_ACCESS, "Insufficient access rights to open file");\ diff --git a/source/lib/res/file/file.h b/source/lib/res/file/file.h index a8d5064aaa..ecb30fe75a 100644 --- a/source/lib/res/file/file.h +++ b/source/lib/res/file/file.h @@ -24,7 +24,7 @@ #ifndef FILE_H #define FILE_H -#include "lib/posix.h" // struct stat +#include "lib/posix/posix_filesystem.h" // struct stat namespace ERR diff --git a/source/lib/res/file/file_cache.cpp b/source/lib/res/file/file_cache.cpp index 81f3910456..57630ea054 100644 --- a/source/lib/res/file/file_cache.cpp +++ b/source/lib/res/file/file_cache.cpp @@ -22,9 +22,11 @@ */ #include "precompiled.h" +#include "file_cache.h" #include +#include "lib/posix/posix_mman.h" #include "lib/allocators.h" #include "lib/byte_order.h" #include "lib/cache_adt.h" diff --git a/source/lib/res/file/file_io.cpp b/source/lib/res/file/file_io.cpp index 65a5a4541a..586a35996b 100644 --- a/source/lib/res/file/file_io.cpp +++ b/source/lib/res/file/file_io.cpp @@ -21,11 +21,12 @@ */ #include "precompiled.h" +#include "file_io.h" #include +#include "lib/posix/posix_aio.h" #include "lib/lib.h" -#include "lib/posix.h" #include "lib/allocators.h" #include "lib/adts.h" #include "file_internal.h" @@ -156,7 +157,7 @@ LibError file_io_issue(File* f, off_t ofs, size_t size, void* p, FileIo* io) } const BlockId disk_pos = block_cache_make_id(f->atom_fn, ofs); - stats_io_check_seek(disk_pos); + stats_io_check_seek(disk_pos.atom_fn, disk_pos.block_num); return INFO::OK; } diff --git a/source/lib/res/file/file_stats.cpp b/source/lib/res/file/file_stats.cpp index 1b4bf89891..fe3ba626c1 100644 --- a/source/lib/res/file/file_stats.cpp +++ b/source/lib/res/file/file_stats.cpp @@ -21,6 +21,7 @@ */ #include "precompiled.h" +#include "file_stats.h" #include @@ -204,21 +205,23 @@ void stats_io_sync_finish(FileIOImplentation fi, FileOp fo, ssize_t user_size, d } -void stats_io_check_seek(BlockId disk_pos) +void stats_io_check_seek(const char* atom_fn, u32 block_num) { - static BlockId cur_disk_pos; + static const char* last_atom_fn; + static u32 last_block_num; // makes debugging ("why are there seeks") a bit nicer by suppressing // the first (bogus) seek. - if(!cur_disk_pos.atom_fn) + if(!last_atom_fn) goto dont_count_first_seek; - if(disk_pos.atom_fn != cur_disk_pos.atom_fn || // different file OR - disk_pos.block_num != cur_disk_pos.block_num+1) // nonsequential + if(atom_fn != last_atom_fn || // different file OR + block_num != last_block_num+1) // nonsequential io_seeks++; dont_count_first_seek: - cur_disk_pos = disk_pos; + last_atom_fn = atom_fn; + last_block_num = block_num; } diff --git a/source/lib/res/file/file_stats.h b/source/lib/res/file/file_stats.h index c25fefe543..d1c5af3e1d 100644 --- a/source/lib/res/file/file_stats.h +++ b/source/lib/res/file/file_stats.h @@ -53,7 +53,7 @@ extern void stats_buf_ref(); extern void stats_io_user_request(size_t user_size); extern void stats_io_sync_start(double* start_time_storage); extern void stats_io_sync_finish(FileIOImplentation fi, FileOp fo, ssize_t user_size, double* start_time_storage); -extern void stats_io_check_seek(BlockId disk_pos); +extern void stats_io_check_seek(const char* atom_fn, u32 block_num); extern void stats_cb_start(); extern void stats_cb_finish(); @@ -81,7 +81,7 @@ extern void stats_dump(); #define stats_io_user_request(user_size) #define stats_io_sync_start(disk_pos, start_time_storage) #define stats_io_sync_finish(fi, fo, user_size, start_time_storage) -#define stats_io_check_seek(disk_pos) +#define stats_io_check_seek(atom_fn, block_num) #define stats_cb_start() #define stats_cb_finish() #define stats_cache(cr, size, atom_fn) diff --git a/source/lib/res/file/file_util.cpp b/source/lib/res/file/file_util.cpp index 54da9c6676..2467d37da5 100644 --- a/source/lib/res/file/file_util.cpp +++ b/source/lib/res/file/file_util.cpp @@ -1,8 +1,9 @@ #include "precompiled.h" +#include + #include "file_internal.h" -#include static bool dirent_less(const DirEnt& d1, const DirEnt& d2) { diff --git a/source/lib/res/file/path.cpp b/source/lib/res/file/path.cpp index 277b20adcc..3a610caeef 100644 --- a/source/lib/res/file/path.cpp +++ b/source/lib/res/file/path.cpp @@ -21,13 +21,15 @@ */ #include "precompiled.h" +#include "path.h" #include +#include "lib/posix/posix_filesystem.h" #include "lib/lib.h" #include "lib/adts.h" -#include "file_internal.h" #include "lib/allocators.h" +#include "file_internal.h" AT_STARTUP(\ diff --git a/source/lib/res/file/trace.cpp b/source/lib/res/file/trace.cpp index 7b70a56763..616f83a61d 100644 --- a/source/lib/res/file/trace.cpp +++ b/source/lib/res/file/trace.cpp @@ -22,6 +22,7 @@ */ #include "precompiled.h" +#include "trace.h" #include "lib/allocators.h" #include "lib/timer.h" diff --git a/source/lib/res/file/vfs.cpp b/source/lib/res/file/vfs.cpp index e6f56d4c1d..84a2eb7867 100644 --- a/source/lib/res/file/vfs.cpp +++ b/source/lib/res/file/vfs.cpp @@ -21,11 +21,11 @@ */ #include "precompiled.h" +#include "vfs.h" #include #include #include - #include #include #include diff --git a/source/lib/res/file/vfs.h b/source/lib/res/file/vfs.h index 31067a76e6..a65b4fda36 100644 --- a/source/lib/res/file/vfs.h +++ b/source/lib/res/file/vfs.h @@ -197,7 +197,7 @@ Decompression is free because it is done in parallel with IOs. #define __VFS_H__ #include "../handle.h" // Handle def -#include "lib/posix.h" // struct stat +#include "lib/posix/posix_filesystem.h" // struct stat #include "file.h" // file open flags // upper bound on number of files; used as size of TNode pool and diff --git a/source/lib/res/file/vfs_mount.cpp b/source/lib/res/file/vfs_mount.cpp index a5fbecfa6d..38f2254c53 100644 --- a/source/lib/res/file/vfs_mount.cpp +++ b/source/lib/res/file/vfs_mount.cpp @@ -22,10 +22,7 @@ */ #include "precompiled.h" - -#include "lib/sysdep/dir_watch.h" -#include "lib/res/h_mgr.h" -#include "file_internal.h" +#include "vfs_mount.h" #include #include @@ -33,6 +30,10 @@ #include #include +#include "lib/sysdep/dir_watch.h" +#include "lib/res/h_mgr.h" +#include "file_internal.h" + // location of a file: either archive or a real directory. // not many instances => don't worry about efficiency. diff --git a/source/lib/res/file/vfs_mount.h b/source/lib/res/file/vfs_mount.h index 83045fbd08..1db5112af0 100644 --- a/source/lib/res/file/vfs_mount.h +++ b/source/lib/res/file/vfs_mount.h @@ -26,6 +26,7 @@ struct Mount; // must come before vfs_tree.h +#include "lib/res/handle.h" #include "file.h" #include "zip.h" #include "vfs_tree.h" diff --git a/source/lib/res/file/vfs_optimizer.cpp b/source/lib/res/file/vfs_optimizer.cpp index da99ecba05..7685ceca58 100644 --- a/source/lib/res/file/vfs_optimizer.cpp +++ b/source/lib/res/file/vfs_optimizer.cpp @@ -22,6 +22,7 @@ */ #include "precompiled.h" +#include "vfs_optimizer.h" #include #include diff --git a/source/lib/res/file/vfs_redirector.cpp b/source/lib/res/file/vfs_redirector.cpp index 88b30d36c4..616010d143 100644 --- a/source/lib/res/file/vfs_redirector.cpp +++ b/source/lib/res/file/vfs_redirector.cpp @@ -21,9 +21,11 @@ */ #include "precompiled.h" +#include "vfs_redirector.h" -#include "file_internal.h" #include "lib/byte_order.h" // FOURCC +#include "file_internal.h" + static const u32 vtbl_magic = FOURCC('F','P','V','T'); diff --git a/source/lib/res/file/vfs_redirector.h b/source/lib/res/file/vfs_redirector.h index cf8551c3c6..68877ba023 100644 --- a/source/lib/res/file/vfs_redirector.h +++ b/source/lib/res/file/vfs_redirector.h @@ -23,6 +23,9 @@ #ifndef VFS_REDIRECTOR_H__ #define VFS_REDIRECTOR_H__ +#include "file.h" +struct FileIo; + struct FileProvider_VTbl { // FOURCC that is checked on each access to ensure this is a valid vtbl. diff --git a/source/lib/res/file/vfs_tree.cpp b/source/lib/res/file/vfs_tree.cpp index da077fc097..5474619083 100644 --- a/source/lib/res/file/vfs_tree.cpp +++ b/source/lib/res/file/vfs_tree.cpp @@ -21,14 +21,15 @@ */ #include "precompiled.h" +#include "vfs_tree.h" #include #include - #include #include #include +#include "lib/posix/posix_pthread.h" #include "lib/allocators.h" #include "lib/adts.h" #include "file_internal.h" diff --git a/source/lib/res/file/zip.cpp b/source/lib/res/file/zip.cpp index 355632bc01..a9b9b3a76d 100644 --- a/source/lib/res/file/zip.cpp +++ b/source/lib/res/file/zip.cpp @@ -21,6 +21,7 @@ */ #include "precompiled.h" +#include "zip.h" #include #include @@ -29,8 +30,8 @@ #include "lib/byte_order.h" #include "lib/allocators.h" #include "lib/timer.h" -#include "file_internal.h" #include "lib/res/res.h" +#include "file_internal.h" // safe downcasters: cast from any integral type to u32 or u16; diff --git a/source/lib/res/graphics/cursor.cpp b/source/lib/res/graphics/cursor.cpp index 9a25ab9c88..0c52b9fab5 100644 --- a/source/lib/res/graphics/cursor.cpp +++ b/source/lib/res/graphics/cursor.cpp @@ -21,6 +21,7 @@ */ #include "precompiled.h" +#include "cursor.h" #include #include @@ -38,7 +39,6 @@ #include "lib/sysdep/sysdep.h" // sys_cursor_* #include "lib/res/res.h" #include "ogl_tex.h" -#include "cursor.h" /* This is used to create the sys cursor to use together with the OpenGL diff --git a/source/lib/res/graphics/ogl_shader.cpp b/source/lib/res/graphics/ogl_shader.cpp index 6fc3ef04b4..fcd8a7e8b6 100644 --- a/source/lib/res/graphics/ogl_shader.cpp +++ b/source/lib/res/graphics/ogl_shader.cpp @@ -9,6 +9,7 @@ */ #include "precompiled.h" +#include "ogl_shader.h" #include "lib/lib.h" #include "lib/res/res.h" @@ -17,7 +18,6 @@ #include "ps/CLogger.h" #include "ps/XML/Xeromyces.h" -#include "ogl_shader.h" #define LOG_CATEGORY "shaders" diff --git a/source/lib/res/graphics/ogl_shader.h b/source/lib/res/graphics/ogl_shader.h index 278de7b56f..f15799c8b6 100644 --- a/source/lib/res/graphics/ogl_shader.h +++ b/source/lib/res/graphics/ogl_shader.h @@ -13,7 +13,6 @@ #include "lib/res/handle.h" -#include "lib/types.h" #include "lib/ogl.h" diff --git a/source/lib/res/graphics/ogl_tex.cpp b/source/lib/res/graphics/ogl_tex.cpp index 9fc88e7436..79a7291aaa 100644 --- a/source/lib/res/graphics/ogl_tex.cpp +++ b/source/lib/res/graphics/ogl_tex.cpp @@ -22,14 +22,15 @@ */ #include "precompiled.h" +#include "ogl_tex.h" #include "lib/lib.h" #include "lib/app_hooks.h" +#include "lib/ogl.h" #include "lib/sysdep/gfx.h" #include "lib/res/res.h" -#include "lib/ogl.h" #include "tex.h" -#include "ogl_tex.h" + //---------------------------------------------------------------------------- diff --git a/source/lib/res/graphics/ogl_tex.h b/source/lib/res/graphics/ogl_tex.h index 51df8d12d9..30fc236e12 100644 --- a/source/lib/res/graphics/ogl_tex.h +++ b/source/lib/res/graphics/ogl_tex.h @@ -144,7 +144,6 @@ the next function to fail, but real apps should check and report errors. #define OGL_TEX_H__ #include "lib/res/handle.h" -#include "lib/types.h" #include "lib/ogl.h" #include "tex.h" diff --git a/source/lib/res/graphics/tex.cpp b/source/lib/res/graphics/tex.cpp index dfe49907e6..e4fd97ecc1 100644 --- a/source/lib/res/graphics/tex.cpp +++ b/source/lib/res/graphics/tex.cpp @@ -21,16 +21,16 @@ */ #include "precompiled.h" +#include "tex.h" #include #include - #include #include "lib/lib.h" #include "lib/timer.h" #include "lib/res/res.h" -#include "tex.h" + #include "tex_codec.h" diff --git a/source/lib/res/graphics/tex_codec.cpp b/source/lib/res/graphics/tex_codec.cpp index 03209ea200..474c0e652a 100644 --- a/source/lib/res/graphics/tex_codec.cpp +++ b/source/lib/res/graphics/tex_codec.cpp @@ -21,13 +21,13 @@ */ #include "precompiled.h" +#include "tex_codec.h" #include #include -#include "tex_codec.h" -#include "tex.h" #include "lib/path_util.h" +#include "tex.h" static const TexCodecVTbl* codecs; diff --git a/source/lib/res/graphics/unifont.cpp b/source/lib/res/graphics/unifont.cpp index a11d526e24..da8f249b96 100644 --- a/source/lib/res/graphics/unifont.cpp +++ b/source/lib/res/graphics/unifont.cpp @@ -9,16 +9,16 @@ */ #include "precompiled.h" +#include "unifont.h" +#include #include #include #include -#include - #include "lib/lib.h" -#include "lib/res/res.h" #include "lib/ogl.h" +#include "lib/res/res.h" #include "ogl_tex.h" // This isn't particularly efficient - it can be improved if we @@ -110,7 +110,7 @@ static LibError UniFont_reload(UniFont* f, const char* fn, Handle UNUSED(h)) int Codepoint, TextureX, TextureY, Width, Height, OffsetX, OffsetY, Advance; FNTStream >> Codepoint>>TextureX>>TextureY>>Width>>Height>>OffsetX>>OffsetY>>Advance; - if (Codepoint > 0xffff) + if (Codepoint > 0xFFFF) { debug_warn("Invalid codepoint"); continue; diff --git a/source/lib/res/h_mgr.cpp b/source/lib/res/h_mgr.cpp index a0cd020f3e..cb1924e64d 100644 --- a/source/lib/res/h_mgr.cpp +++ b/source/lib/res/h_mgr.cpp @@ -21,17 +21,17 @@ */ #include "precompiled.h" - -#include "lib/lib.h" #include "h_mgr.h" -#include "lib/allocators.h" #include // CHAR_BIT #include #include #include // std::bad_alloc + +#include "lib/lib.h" #include "lib/allocators.h" + static const uint MAX_EXTANT_HANDLES = 10000; // rationale @@ -66,7 +66,7 @@ static const uint MAX_EXTANT_HANDLES = 10000; // (field width determines maximum unambiguous resource allocs) #define TAG_BITS 32 const uint TAG_SHIFT = 0; -const u32 TAG_MASK = 0xffffffff; // safer than (1 << 32) - 1 +const u32 TAG_MASK = 0xFFFFFFFF; // safer than (1 << 32) - 1 // - index (0-based) of control block in our array. // (field width determines maximum currently open handles) diff --git a/source/lib/res/h_mgr.h b/source/lib/res/h_mgr.h index d9760179f4..4c2d1f17fb 100644 --- a/source/lib/res/h_mgr.h +++ b/source/lib/res/h_mgr.h @@ -246,7 +246,7 @@ we could switch H_DEREF to throwing an exception on error. #include "lib/lib.h" -#ifndef HANDLE_DEFINED +#ifndef INCLUDED_HANDLE #include "handle.h" #endif diff --git a/source/lib/res/handle.h b/source/lib/res/handle.h index 355a5ff850..03c2157727 100644 --- a/source/lib/res/handle.h +++ b/source/lib/res/handle.h @@ -1,9 +1,7 @@ // define "Handle". split out of h_mgr.h to reduce dependencies. -#ifndef HANDLE_DEFINED -#define HANDLE_DEFINED - -#include "lib/types.h" +#ifndef INCLUDED_HANDLE +#define INCLUDED_HANDLE // 0 = invalid handle value; < 0 is an error code. // 64 bits, because we want tags to remain unique: tag overflow may @@ -12,4 +10,4 @@ // not a lot. typedef i64 Handle; -#endif // #ifndef HANDLE_DEFINED +#endif // #ifndef INCLUDED_HANDLE diff --git a/source/lib/res/mem.cpp b/source/lib/res/mem.cpp index fbdf0863bc..7a80eaa026 100644 --- a/source/lib/res/mem.cpp +++ b/source/lib/res/mem.cpp @@ -22,17 +22,16 @@ */ #include "precompiled.h" - -#include "lib/lib.h" -#include "lib/posix.h" -#include "h_mgr.h" -#include "lib/allocators.h" // OverrunProtector #include "mem.h" #include - #include +#include "lib/posix/posix_pthread.h" +#include "lib/lib.h" +#include "lib/allocators.h" // OverrunProtector +#include "h_mgr.h" + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; struct ScopedLock diff --git a/source/lib/res/res.cpp b/source/lib/res/res.cpp index 5d8489928f..2cac8909b6 100644 --- a/source/lib/res/res.cpp +++ b/source/lib/res/res.cpp @@ -21,9 +21,9 @@ */ #include "precompiled.h" - #include "res.h" + AT_STARTUP(\ error_setDescription(ERR::RES_UNKNOWN_FORMAT, "Unknown file format");\ error_setDescription(ERR::RES_INCOMPLETE_HEADER, "File header not completely read");\ diff --git a/source/lib/res/sound/ogghack.cpp b/source/lib/res/sound/ogghack.cpp index 37b37c32ca..bb84b89f89 100644 --- a/source/lib/res/sound/ogghack.cpp +++ b/source/lib/res/sound/ogghack.cpp @@ -9,8 +9,7 @@ #include #include - -#include "ogghack.h" +#include "ogghack.h" // HACK: must be included after al.h (bad interface) #if MSC_VERSION # ifdef NDEBUG diff --git a/source/lib/res/sound/snd_mgr.cpp b/source/lib/res/sound/snd_mgr.cpp index eb00cdc711..c647d73c45 100644 --- a/source/lib/res/sound/snd_mgr.cpp +++ b/source/lib/res/sound/snd_mgr.cpp @@ -22,6 +22,7 @@ */ #include "precompiled.h" +#include "snd_mgr.h" #include // to extract snd_open's definition file contents #include @@ -46,14 +47,13 @@ #endif #include "lib/res/res.h" -#include "snd_mgr.h" #include "lib/timer.h" #include "lib/app_hooks.h" - #define OGG_HACK #include "ogghack.h" + #if MSC_VERSION # pragma comment(lib, "openal32.lib") #endif diff --git a/source/lib/self_test.cpp b/source/lib/self_test.cpp index ad88a92d6d..af499f3c9e 100644 --- a/source/lib/self_test.cpp +++ b/source/lib/self_test.cpp @@ -21,6 +21,7 @@ */ #include "precompiled.h" +#include "self_test.h" #include "timer.h" diff --git a/source/lib/self_test.h b/source/lib/self_test.h index b33ad6527e..d1953bf47b 100644 --- a/source/lib/self_test.h +++ b/source/lib/self_test.h @@ -179,7 +179,7 @@ extern bool self_test_active; // for convenience, to avoid having to include all of these manually #include "lib_errors.h" -#include "posix.h" +#include "posix/posix.h" #define CXXTEST_HAVE_EH #define CXXTEST_HAVE_STD diff --git a/source/lib/string_s.cpp b/source/lib/string_s.cpp index 5643e37cae..aa520a929a 100644 --- a/source/lib/string_s.cpp +++ b/source/lib/string_s.cpp @@ -21,12 +21,12 @@ */ #include "precompiled.h" +#include "string_s.h" #include #include "lib.h" -#include "posix.h" // SIZE_MAX - +#include "posix/posix_types.h" // SIZE_MAX // we were included from wstring_s.cpp; skip all stuff that // must only be done once. @@ -91,7 +91,7 @@ STMT( \ // skip our implementation if already available, but not the // self-test and the t* defines (needed for test). -#if !HAVE_STRING_S +#if !HAVE_SECURE_CRT // return length [in characters] of a string, not including the trailing @@ -213,4 +213,4 @@ int tcat_s(tchar* dst, size_t max_dst_chars, const tchar* src) return tncat_s(dst, max_dst_chars, src, SIZE_MAX); } -#endif // #if !HAVE_STRING_S +#endif // #if !HAVE_SECURE_CRT diff --git a/source/lib/string_s.h b/source/lib/string_s.h index d6b6576c76..9d2f07df40 100644 --- a/source/lib/string_s.h +++ b/source/lib/string_s.h @@ -23,7 +23,7 @@ #ifndef STRING_S_H__ #define STRING_S_H__ -#include "posix_types.h" // size_t +#include "posix/posix_types.h" // size_t #include "config.h" @@ -34,7 +34,7 @@ namespace ERR // only declare these functions if using our implementation // (otherwise, we risk incompatibilities) -#if !HAVE_STRING_S +#if !HAVE_SECURE_CRT // (conflicts with glibc definitions) #if !OS_UNIX @@ -82,6 +82,6 @@ extern int wcsncat_s(wchar_t* dst, size_t max_dst_chars, const wchar_t* src, siz extern int strcat_s(char* dst, size_t max_dst_chars, const char* src); extern int wcscat_s(wchar_t* dst, size_t max_dst_chars, const wchar_t* src); -#endif // #if !HAVE_STRING_S +#endif // #if !HAVE_SECURE_CRT #endif // #ifndef STRING_S_H__ diff --git a/source/lib/sysdep/cpu.cpp b/source/lib/sysdep/cpu.cpp index 91fe715911..9efe1b07e8 100644 --- a/source/lib/sysdep/cpu.cpp +++ b/source/lib/sysdep/cpu.cpp @@ -21,10 +21,9 @@ */ #include "precompiled.h" - -#include "lib/lib.h" #include "cpu.h" +#include "lib/lib.h" #if CPU_IA32 # include "lib/sysdep/ia32.h" #endif diff --git a/source/lib/sysdep/gfx.cpp b/source/lib/sysdep/gfx.cpp index 3512823950..2ab355bf0d 100644 --- a/source/lib/sysdep/gfx.cpp +++ b/source/lib/sysdep/gfx.cpp @@ -21,10 +21,11 @@ */ #include "precompiled.h" +#include "gfx.h" #include "lib/lib.h" #include "lib/sdl.h" -#include "gfx.h" + char gfx_card[GFX_CARD_LEN] = ""; char gfx_drv_ver[GFX_DRV_VER_LEN] = ""; diff --git a/source/lib/sysdep/ia32.cpp b/source/lib/sysdep/ia32.cpp index 2b6087693d..9622dabb83 100644 --- a/source/lib/sysdep/ia32.cpp +++ b/source/lib/sysdep/ia32.cpp @@ -21,22 +21,21 @@ */ #include "precompiled.h" +#include "ia32.h" #include #include - #include #include +#include "lib/posix/posix_pthread.h" #include "lib/lib.h" -#include "lib/posix.h" #include "lib/timer.h" -#include "ia32.h" #include "cpu.h" // HACK (see call to wtime_reset_impl) #if OS_WIN -#include "lib/sysdep/win/wtime.h" +#include "lib/sysdep/win/wposix/wtime_internal.h" #endif #if !HAVE_MS_ASM && !HAVE_GNU_ASM diff --git a/source/lib/sysdep/ia32.h b/source/lib/sysdep/ia32.h index ec8e080215..14c06f6524 100644 --- a/source/lib/sysdep/ia32.h +++ b/source/lib/sysdep/ia32.h @@ -27,8 +27,6 @@ #error "including ia32.h without CPU_IA32=1" #endif -#include "lib/types.h" - // some of these are implemented in asm, so make sure name mangling is // disabled. #ifdef __cplusplus diff --git a/source/lib/sysdep/snd.cpp b/source/lib/sysdep/snd.cpp index 8af8444677..820f8b756b 100644 --- a/source/lib/sysdep/snd.cpp +++ b/source/lib/sysdep/snd.cpp @@ -21,9 +21,9 @@ */ #include "precompiled.h" +#include "snd.h" #include "lib/lib.h" -#include "snd.h" char snd_card[SND_CARD_LEN]; char snd_drv_ver[SND_DRV_VER_LEN]; diff --git a/source/lib/sysdep/sysdep.cpp b/source/lib/sysdep/sysdep.cpp index 52609e43ee..6666fe26e3 100644 --- a/source/lib/sysdep/sysdep.cpp +++ b/source/lib/sysdep/sysdep.cpp @@ -21,7 +21,6 @@ */ #include "precompiled.h" - #include "sysdep.h" diff --git a/source/lib/sysdep/tests/test_sysdep.h b/source/lib/sysdep/tests/test_sysdep.h index 9cf2b20e28..24128c08a0 100644 --- a/source/lib/sysdep/tests/test_sysdep.h +++ b/source/lib/sysdep/tests/test_sysdep.h @@ -1,7 +1,7 @@ #include "lib/self_test.h" #include "lib/sysdep/sysdep.h" -#include "lib/posix.h" // fminf etc. +#include "lib/posix/posix.h" // fminf etc. class TestSysdep : public CxxTest::TestSuite { diff --git a/source/lib/sysdep/win/delay_load.cpp b/source/lib/sysdep/win/delay_load.cpp index 897c0a15bf..2377d83fa5 100644 --- a/source/lib/sysdep/win/delay_load.cpp +++ b/source/lib/sysdep/win/delay_load.cpp @@ -23,10 +23,10 @@ // modified from VC7 DelayHlp.cpp and DelayImp.h #include "precompiled.h" +#include "delay_load.h" #include "win_internal.h" -#include "delay_load.h" #define _DELAY_IMP_VER 2 diff --git a/source/lib/sysdep/win/dll_ver.cpp b/source/lib/sysdep/win/dll_ver.cpp index 39bb0946f5..d35a1afcd8 100644 --- a/source/lib/sysdep/win/dll_ver.cpp +++ b/source/lib/sysdep/win/dll_ver.cpp @@ -21,13 +21,13 @@ */ #include "precompiled.h" +#include "dll_ver.h" #include #include -#include "win_internal.h" #include "lib/path_util.h" -#include "dll_ver.h" +#include "win_internal.h" #if MSC_VERSION #pragma comment(lib, "version.lib") // DLL version diff --git a/source/lib/sysdep/win/wcpu.cpp b/source/lib/sysdep/win/wcpu.cpp index e0bf89086f..d7596a6523 100644 --- a/source/lib/sysdep/win/wcpu.cpp +++ b/source/lib/sysdep/win/wcpu.cpp @@ -21,13 +21,13 @@ */ #include "precompiled.h" +#include "lib/sysdep/cpu.h" #include "lib/lib.h" -#include "lib/posix.h" +#include "lib/posix/posix_pthread.h" +#include "lib/posix/posix_time.h" #include "win_internal.h" -#include "lib/sysdep/cpu.h" - // limit allows statically allocated per-CPU structures (for simplicity). // we're Windows-specific anyway; such systems won't foreseeably have more. // note: int instead of unsigned because is also signed (tri-state). diff --git a/source/lib/sysdep/win/wdbg.cpp b/source/lib/sysdep/win/wdbg.cpp index 4589678841..9840ea3117 100644 --- a/source/lib/sysdep/win/wdbg.cpp +++ b/source/lib/sysdep/win/wdbg.cpp @@ -21,19 +21,18 @@ */ #include "precompiled.h" +#include "wdbg.h" #include #include #include #include "lib/lib.h" -#include "win_internal.h" -#include "lib/posix.h" -#include "lib/sysdep/cpu.h" -#include "wdbg.h" +#include "lib/posix/posix_pthread.h" #include "lib/byte_order.h" // FOURCC #include "lib/app_hooks.h" - +#include "lib/sysdep/cpu.h" +#include "win_internal.h" #pragma data_seg(WIN_CALLBACK_PRE_MAIN(b)) WIN_REGISTER_FUNC(wdbg_init); @@ -130,7 +129,7 @@ void debug_heap_check() // if not called, the default is DEBUG_HEAP_NONE, i.e. do nothing. void debug_heap_enable(DebugHeapChecks what) { - // note: if MMGR is enabled, crtdbg.h will not have been included. + // note: if mmgr is enabled, crtdbg.h will not have been included. // in that case, we do nothing here - this interface is too basic to // control mmgr; use its API instead. #if !CONFIG_USE_MMGR && HAVE_VC_DEBUG_ALLOC diff --git a/source/lib/sysdep/win/wdbg_sym.cpp b/source/lib/sysdep/win/wdbg_sym.cpp index 89db1ac568..905213bd42 100644 --- a/source/lib/sysdep/win/wdbg_sym.cpp +++ b/source/lib/sysdep/win/wdbg_sym.cpp @@ -21,25 +21,26 @@ */ #include "precompiled.h" +#include "wdbg.h" #include #include #include #include "lib/lib.h" -#include "win_internal.h" -#define _NO_CVCONST_H // request SymTagEnum be defined -#include -#include // VARIANT -#include "lib/posix.h" #include "lib/sysdep/cpu.h" -#include "wdbg.h" + #include "lib/debug_stl.h" #include "lib/app_hooks.h" #include "lib/path_util.h" #if CPU_IA32 # include "lib/sysdep/ia32.h" #endif +#include "win_internal.h" + +#define _NO_CVCONST_H // request SymTagEnum be defined +#include // must come after win_internal +#include // VARIANT #if MSC_VERSION #pragma comment(lib, "dbghelp.lib") diff --git a/source/lib/sysdep/win/wdir_watch.cpp b/source/lib/sysdep/win/wdir_watch.cpp index 8c71fe208d..c390cdae4d 100644 --- a/source/lib/sysdep/win/wdir_watch.cpp +++ b/source/lib/sysdep/win/wdir_watch.cpp @@ -21,6 +21,7 @@ */ #include "precompiled.h" +#include "lib/sysdep/dir_watch.h" #include #include @@ -28,8 +29,8 @@ #include "lib/lib.h" #include "lib/path_util.h" -#include "win_internal.h" #include "lib/res/file/file.h" // path_is_subpath +#include "win_internal.h" #pragma data_seg(WIN_CALLBACK_POST_ATEXIT(x)) diff --git a/source/lib/sysdep/win/wgfx.cpp b/source/lib/sysdep/win/wgfx.cpp index 16a7e0e04e..ace345c6ae 100644 --- a/source/lib/sysdep/win/wgfx.cpp +++ b/source/lib/sysdep/win/wgfx.cpp @@ -21,8 +21,8 @@ */ #include "precompiled.h" - #include "lib/sysdep/gfx.h" + #include "dll_ver.h" // dll_list_* #include "win_internal.h" diff --git a/source/lib/sysdep/win/win_internal.h b/source/lib/sysdep/win/win_internal.h index 4d0b1e8d2a..77c165025b 100644 --- a/source/lib/sysdep/win/win_internal.h +++ b/source/lib/sysdep/win/win_internal.h @@ -28,7 +28,6 @@ #endif #include "lib/lib.h" // BIT -#include "lib/types.h" // intptr_t // Win32 socket decls aren't portable (e.g. problems with socklen_t) diff --git a/source/lib/sysdep/win/wposix.cpp b/source/lib/sysdep/win/wposix.cpp deleted file mode 100644 index 4ca46a14c8..0000000000 --- a/source/lib/sysdep/win/wposix.cpp +++ /dev/null @@ -1,965 +0,0 @@ -/** - * ========================================================================= - * File : wposix.cpp - * Project : 0 A.D. - * Description : emulate a subset of POSIX on Win32. - * - * @author Jan.Wassenberg@stud.uni-karlsruhe.de - * ========================================================================= - */ - -/* - * Copyright (c) 2004-2005 Jan Wassenberg - * - * Redistribution and/or modification are also permitted under the - * terms of the GNU General Public License as published by the - * Free Software Foundation (version 2 or later, at your option). - * - * 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. - */ - -#include "precompiled.h" - -#include -#include - -#include "lib/lib.h" -#include "win_internal.h" -#include "lib/allocators.h" -#include "lib/path_util.h" -#include "lib/posix.h" - - -// cast intptr_t to HANDLE; centralized for easier changing, e.g. avoiding -// warnings. i = -1 converts to INVALID_HANDLE_VALUE (same value). -static HANDLE HANDLE_from_intptr(intptr_t i) -{ - return (HANDLE)((char*)0 + i); -} - - -////////////////////////////////////////////////////////////////////////////// -// -// file -// -////////////////////////////////////////////////////////////////////////////// - - -int open(const char* fn, int oflag, ...) -{ - const bool is_com_port = strncmp(fn, "/dev/tty", 8) == 0; - // also used later, before aio_reopen - - // translate "/dev/tty%d" to "COM%d" - if(is_com_port) - { - char port[] = "COM1"; - const char digit = fn[8]+1; - // PCs only support COM1..COM4. - if(!('1' <= digit && digit <= '4')) - return -1; - port[3] = digit; - fn = port; - } - - mode_t mode = 0; - if(oflag & O_CREAT) - { - va_list args; - va_start(args, oflag); - mode = va_arg(args, mode_t); - va_end(args); - } - - WIN_SAVE_LAST_ERROR; // CreateFile - int fd = _open(fn, oflag, mode); - WIN_RESTORE_LAST_ERROR; - - // cases when we don't want to open a second AIO-capable handle: - // .. stdin/stdout/stderr - if(fd <= 2) - goto no_aio; - // .. COM port - we don't currently need AIO access for those, and - // aio_reopen's CreateFile would fail with "access denied". - if(is_com_port) - goto no_aio; - // .. caller is requesting we skip it (see file_open) - if(oflag & O_NO_AIO_NP) - goto no_aio; - - // none of the above apply; now re-open the file. - // note: this is possible because _open defaults to DENY_NONE sharing. - WARN_ERR(aio_reopen(fd, fn, oflag)); - -no_aio: - - // CRT doesn't like more than 255 files open. - // warn now, so that we notice why so many are open. -#ifndef NDEBUG - if(fd > 256) - WARN_ERR(ERR::LIMIT); -#endif - - return fd; -} - - -int close(int fd) -{ - debug_assert(3 <= fd && fd < 256); - - // note: there's no good way to notify us that wasn't opened for - // AIO, so we could skip aio_close. storing a bit in the fd is evil and - // a fd -> info map is redundant (waio already has one). - // therefore, we require aio_close to fail gracefully. - WARN_ERR(aio_close(fd)); - - return _close(fd); -} - - -// we don't want to #define read to _read, since that's a fairly common -// identifier. therefore, translate from MS CRT names via thunk functions. -// efficiency is less important, and the overhead could be optimized away. - -int read(int fd, void* buf, size_t nbytes) -{ - return _read(fd, buf, nbytes); -} - -int write(int fd, void* buf, size_t nbytes) -{ - return _write(fd, buf, nbytes); -} - - - -int ioctl(int fd, int op, int* data) -{ - const HANDLE h = HANDLE_from_intptr(_get_osfhandle(fd)); - - switch(op) - { - case TIOCMGET: - /* TIOCM_* mapped directly to MS_*_ON */ - GetCommModemStatus(h, (DWORD*)data); - break; - - case TIOCMBIS: - /* only RTS supported */ - if(*data & TIOCM_RTS) - EscapeCommFunction(h, SETRTS); - else - EscapeCommFunction(h, CLRRTS); - break; - - case TIOCMIWAIT: - static DWORD mask; - DWORD new_mask = 0; - if(*data & TIOCM_CD) - new_mask |= EV_RLSD; - if(*data & TIOCM_CTS) - new_mask |= EV_CTS; - if(new_mask != mask) - SetCommMask(h, mask = new_mask); - WaitCommEvent(h, &mask, 0); - break; - } - - return 0; -} - - - - -// -// determine file system type on the current drive - -// needed to work around incorrect FAT time translation. -// - -static enum Filesystem -{ - FS_INVALID, // detect_filesystem() not yet called - FS_FAT, // FAT12, FAT16, or FAT32 - FS_NTFS, // (most common) - FS_UNKNOWN // newer FS we don't know about -} -filesystem; - - -// rationale: the previous method of checking every path was way too slow -// (taking ~800ms total during init). instead, we only determine the FS once. -// this is quite a bit easier than intercepting chdir() calls and/or -// caching FS type per drive letter, but not foolproof. -// -// if some data files are on a different volume that is set up as FAT, -// the workaround below won't be triggered (=> timestamps may be off by -// 1 hour when DST is in effect). oh well, that is not a supported. -// -// the common case (everything is on a single NTFS volume) is more important -// and must run without penalty. - - -// called from the first filetime_to_time_t() call, not win.cpp init; -// this means we can rely on the current directory having been set to -// the app's directory (and therefore its appendant volume - see above). -static void detect_filesystem() -{ - char root_path[MAX_PATH] = "c:\\"; // default in case GCD fails - DWORD gcd_ret = GetCurrentDirectory(sizeof(root_path), root_path); - debug_assert(gcd_ret != 0); - // if this fails, no problem - we have the default from above. - root_path[3] = '\0'; // cut off after "c:\" - - char fs_name[32] = {0}; - BOOL ret = GetVolumeInformation(root_path, 0,0,0,0,0, fs_name, sizeof(fs_name)); - fs_name[ARRAY_SIZE(fs_name)-1] = '\0'; - debug_assert(ret != 0); - // if this fails, no problem - we really only care if fs is FAT, - // and will assume that's not the case (since fs_name != "FAT"). - - filesystem = FS_UNKNOWN; - - if(!strncmp(fs_name, "FAT", 3)) // e.g. FAT32 - filesystem = FS_FAT; - else if(!strcmp(fs_name, "NTFS")) - filesystem = FS_NTFS; -} - - -// from wtime -extern time_t time_t_from_local_filetime(FILETIME* ft); -extern time_t utc_filetime_to_time_t(FILETIME* ft); - -// convert Windows FILETIME to POSIX time_t (seconds-since-1970 UTC); -// used by stat and readdir_stat_np for st_mtime. -// -// works around a documented Windows bug in converting FAT file times -// (correct results are desired since VFS mount logic considers -// files 'equal' if their mtime and size are the same). -static time_t filetime_to_time_t(FILETIME* ft) -{ - ONCE(detect_filesystem()); - - // the FAT file system stores local file times, while - // NTFS records UTC. Windows does convert automatically, - // but uses the current DST settings. (boo!) - // we go back to local time, and convert properly. - if(filesystem == FS_FAT) - { - FILETIME local_ft; - FileTimeToLocalFileTime(ft, &local_ft); - return time_t_from_local_filetime(&local_ft); - } - - return utc_filetime_to_time_t(ft); -} - -/* -// currently only sets st_mode (file or dir) and st_size. -int stat(const char* fn, struct stat* s) -{ - memset(s, 0, sizeof(struct stat)); - - WIN32_FILE_ATTRIBUTE_DATA fad; - if(!GetFileAttributesEx(fn, GetFileExInfoStandard, &fad)) - return -1; - - s->st_mtime = filetime_to_time_t(fad.ftLastAccessTime) - - // dir - if(fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) - s->st_mode = S_IFDIR; - else - { - s->st_mode = S_IFREG; - s->st_size = (off_t)((((u64)fad.nFileSizeHigh) << 32) | fad.nFileSizeLow); - } - - return 0; -} -*/ - - -////////////////////////////////////////////////////////////////////////////// -// -// dir -// -////////////////////////////////////////////////////////////////////////////// - -#undef getcwd - -char* getcwd(char* buf, size_t buf_size) -{ - return _getcwd(buf, buf_size); -} - -char* realpath(const char* fn, char* path) -{ - if(!GetFullPathName(fn, PATH_MAX, path, 0)) - return 0; - return path; -} - - -int mkdir(const char* path, mode_t) -{ - return CreateDirectory(path, 0)? 0 : -1; -} - - -// opendir/readdir/closedir -// -// note: we avoid opening directories or returning entries that have -// hidden or system attributes set. this is to prevent returning something -// like "\System Volume Information", which raises an error upon opening. - -// 0-initialized by wdir_alloc for safety; this is required for -// num_entries_scanned. -struct WDIR -{ - HANDLE hFind; - - // the dirent returned by readdir. - // note: having only one global instance is not possible because - // multiple independent opendir/readdir sequences must be supported. - struct dirent ent; - - WIN32_FIND_DATA fd; - - // since opendir calls FindFirstFile, we need a means of telling the - // first call to readdir that we already have a file. - // that's the case iff this is == 0; we use a counter rather than a - // flag because that allows keeping statistics. - int num_entries_scanned; -}; - - -// suballocator - satisfies most requests with a reusable static instance, -// thus speeding up allocation and avoiding heap fragmentation. -// thread-safe. - -static WDIR global_wdir; -static uintptr_t global_wdir_is_in_use; - -// zero-initializes the WDIR (code below relies on this) -static inline WDIR* wdir_alloc() -{ - return (WDIR*)single_calloc(&global_wdir, &global_wdir_is_in_use, sizeof(WDIR)); -} - -static inline void wdir_free(WDIR* d) -{ - single_free(&global_wdir, &global_wdir_is_in_use, d); -} - - -static const DWORD hs = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM; - -// make sure path exists and is a normal (according to attributes) directory. -static bool is_normal_dir(const char* path) -{ - const DWORD fa = GetFileAttributes(path); - // .. path not found - if(fa == INVALID_FILE_ATTRIBUTES) - return false; - // .. not a directory - if((fa & FILE_ATTRIBUTE_DIRECTORY) == 0) - return false; - // .. hidden or system attribute(s) set - // this check is now disabled because wsnd's add_oal_dlls_in_dir - // needs to open the Windows system directory, which sometimes has - // these attributes set. - //if((fa & hs) != 0) - // return false; - return true; -} - - -DIR* opendir(const char* path) -{ - if(!is_normal_dir(path)) - { - errno = ENOENT; -fail: - debug_warn("opendir failed"); - return 0; - } - - WDIR* d = wdir_alloc(); - if(!d) - { - errno = ENOMEM; - goto fail; - } - - // build search path for FindFirstFile. note: "path\\dir" only returns - // information about that directory; trailing slashes aren't allowed. - // for dir entries to be returned, we have to append "\\*". - char search_path[PATH_MAX]; - snprintf(search_path, ARRAY_SIZE(search_path), "%s\\*", path); - - // note: we could store search_path and defer FindFirstFile until - // readdir. this way is a bit more complex but required for - // correctness (we must return a valid DIR iff is valid). - d->hFind = FindFirstFileA(search_path, &d->fd); - if(d->hFind == INVALID_HANDLE_VALUE) - { - // not an error - the directory is just empty. - if(GetLastError() == ERROR_NO_MORE_FILES) - goto success; - - // translate Win32 error to errno. - LibError err = LibError_from_win32(FALSE); - LibError_set_errno(err); - - // release the WDIR allocated above. - // unfortunately there's no way around this; we need to allocate - // d before FindFirstFile because it uses d->fd. copying from a - // temporary isn't nice either (this free doesn't happen often) - wdir_free(d); - goto fail; - } - -success: - return d; -} - - -struct dirent* readdir(DIR* d_) -{ - WDIR* const d = (WDIR*)d_; - - // avoid polluting the last error. - DWORD prev_err = GetLastError(); - - // first call - skip FindNextFile (see opendir). - if(d->num_entries_scanned == 0) - { - // this directory is empty. - if(d->hFind == INVALID_HANDLE_VALUE) - return 0; - goto already_have_file; - } - - // until end of directory or a valid entry was found: - for(;;) - { - if(!FindNextFileA(d->hFind, &d->fd)) - goto fail; -already_have_file: - - d->num_entries_scanned++; - - // not a hidden or system entry -> it's valid. - if((d->fd.dwFileAttributes & hs) == 0) - break; - } - - // this entry has passed all checks; return information about it. - // (note: d_name is a pointer; see struct dirent definition) - d->ent.d_name = d->fd.cFileName; - return &d->ent; - -fail: - // FindNextFile failed; determine why and bail. - // .. legit, end of dir reached. don't pollute last error code. - if(GetLastError() == ERROR_NO_MORE_FILES) - SetLastError(prev_err); - else - debug_warn("readdir: FindNextFile failed"); - return 0; -} - - -// return status for the dirent returned by the last successful -// readdir call from the given directory stream. -// currently sets st_size, st_mode, and st_mtime; the rest are zeroed. -// non-portable, but considerably faster than stat(). used by file_enum. -int readdir_stat_np(DIR* d_, struct stat* s) -{ - WDIR* d = (WDIR*)d_; - - memset(s, 0, sizeof(*s)); - s->st_size = (off_t)u64_from_u32(d->fd.nFileSizeHigh, d->fd.nFileSizeLow); - s->st_mode = (d->fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)? S_IFDIR : S_IFREG; - s->st_mtime = filetime_to_time_t(&d->fd.ftLastWriteTime); - return 0; -} - - -int closedir(DIR* d_) -{ - WDIR* const d = (WDIR*)d_; - - FindClose(d->hFind); - - wdir_free(d); - return 0; -} - - -////////////////////////////////////////////////////////////////////////////// -// -// terminal -// -////////////////////////////////////////////////////////////////////////////// - - -static HANDLE std_h[2] = { (HANDLE)((char*)0 + 3), (HANDLE)((char*)0 + 7) }; - - -void _get_console() -{ - AllocConsole(); -} - -void _hide_console() -{ - FreeConsole(); -} - - -int tcgetattr(int fd, struct termios* termios_p) -{ - if(fd >= 2) - return -1; - HANDLE h = std_h[fd]; - - DWORD mode; - GetConsoleMode(h, &mode); - termios_p->c_lflag = mode & (ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT); - - return 0; -} - - -int tcsetattr(int fd, int /* optional_actions */, const struct termios* termios_p) -{ - if(fd >= 2) - return -1; - HANDLE h = std_h[fd]; - SetConsoleMode(h, (DWORD)termios_p->c_lflag); - FlushConsoleInputBuffer(h); - - return 0; -} - - -int poll(struct pollfd /* fds */[], int /* nfds */, int /* timeout */) -{ - return -1; -} - - -////////////////////////////////////////////////////////////////////////////// -// -// memory mapping -// -////////////////////////////////////////////////////////////////////////////// - -// convert POSIX PROT_* flags to their Win32 PAGE_* enumeration equivalents. -// used by mprotect. -static DWORD win32_prot(int prot) -{ - // this covers all 8 combinations of read|write|exec - // (note that "none" means all flags are 0). - switch(prot & (PROT_READ|PROT_WRITE|PROT_EXEC)) - { - case PROT_NONE: - return PAGE_NOACCESS; - case PROT_READ: - return PAGE_READONLY; - case PROT_WRITE: - // not supported by Win32; POSIX allows us to also grant read access. - return PAGE_READWRITE; - case PROT_EXEC: - return PAGE_EXECUTE; - case PROT_READ|PROT_WRITE: - return PAGE_READWRITE; - case PROT_READ|PROT_EXEC: - return PAGE_EXECUTE_READ; - case PROT_WRITE|PROT_EXEC: - // not supported by Win32; POSIX allows us to also grant read access. - return PAGE_EXECUTE_READWRITE; - case PROT_READ|PROT_WRITE|PROT_EXEC: - return PAGE_EXECUTE_READWRITE; - NODEFAULT; - } -} - - -int mprotect(void* addr, size_t len, int prot) -{ - const DWORD flNewProtect = win32_prot(prot); - DWORD flOldProtect; // required by VirtualProtect - BOOL ok = VirtualProtect(addr, len, flNewProtect, &flOldProtect); - WARN_RETURN_IF_FALSE(ok); - return 0; -} - - -// called when flags & MAP_ANONYMOUS -static LibError mmap_mem(void* start, size_t len, int prot, int flags, int fd, void** pp) -{ - // sanity checks. we don't care about these but enforce them to - // ensure callers are compatible with mmap. - // .. MAP_ANONYMOUS is documented to require this. - debug_assert(fd == -1); - // .. if MAP_SHARED, writes are to change "the underlying [mapped] - // object", but there is none here (we're backed by the page file). - debug_assert(flags & MAP_PRIVATE); - - // see explanation at MAP_NORESERVE definition. - bool want_commit = (prot != PROT_NONE && !(flags & MAP_NORESERVE)); - - // decommit a given area (leaves its address space reserved) - if(!want_commit && start != 0 && flags & MAP_FIXED) - { - MEMORY_BASIC_INFORMATION mbi; - WARN_RETURN_IF_FALSE(VirtualQuery(start, &mbi, sizeof(mbi))); - if(mbi.State == MEM_COMMIT) - { - WARN_IF_FALSE(VirtualFree(start, len, MEM_DECOMMIT)); - *pp = 0; - // make sure *pp won't be misinterpreted as an error - cassert(MAP_FAILED != 0); - return INFO::OK; - } - } - - DWORD flAllocationType = want_commit? MEM_COMMIT : MEM_RESERVE; - DWORD flProtect = win32_prot(prot); - void* p = VirtualAlloc(start, len, flAllocationType, flProtect); - if(!p) - WARN_RETURN(ERR::NO_MEM); - *pp = p; - return INFO::OK; -} - - -// given mmap prot and flags, output protection/access values for use with -// CreateFileMapping / MapViewOfFile. they only support read-only, -// read/write and copy-on-write, so we dumb it down to that and later -// set the correct (and more restrictive) permission via mprotect. -static LibError mmap_file_access(int prot, int flags, DWORD& flProtect, DWORD& dwAccess) -{ - // assume read-only; other cases handled below. - flProtect = PAGE_READONLY; - dwAccess = FILE_MAP_READ; - - if(prot & PROT_WRITE) - { - // determine write behavior: (whether they change the underlying file) - switch(flags & (MAP_SHARED|MAP_PRIVATE)) - { - // .. changes are written to file. - case MAP_SHARED: - flProtect = PAGE_READWRITE; - dwAccess = FILE_MAP_WRITE; // read and write - break; - // .. copy-on-write mapping; writes do not affect the file. - case MAP_PRIVATE: - flProtect = PAGE_WRITECOPY; - dwAccess = FILE_MAP_COPY; - break; - // .. either none or both of the flags are set. the latter is - // definitely illegal according to POSIX and some man pages - // say exactly one must be set, so abort. - default: - WARN_RETURN(ERR::INVALID_PARAM); - } - } - - return INFO::OK; -} - - -static LibError mmap_file(void* start, size_t len, int prot, int flags, - int fd, off_t ofs, void** pp) -{ - debug_assert(fd != -1); // handled by mmap_mem - - WIN_SAVE_LAST_ERROR; - - HANDLE hFile = HANDLE_from_intptr(_get_osfhandle(fd)); - if(hFile == INVALID_HANDLE_VALUE) - WARN_RETURN(ERR::INVALID_PARAM); - - // MapViewOfFileEx will fail if the "suggested" base address is - // nonzero but cannot be honored, so wipe out unless MAP_FIXED. - if(!(flags & MAP_FIXED)) - start = 0; - - // choose protection and access rights for CreateFileMapping / - // MapViewOfFile. these are weaker than what PROT_* allows and - // are augmented below by subsequently mprotect-ing. - DWORD flProtect; DWORD dwAccess; - RETURN_ERR(mmap_file_access(prot, flags, flProtect, dwAccess)); - - // enough foreplay; now actually map. - const HANDLE hMap = CreateFileMapping(hFile, 0, flProtect, 0, 0, (LPCSTR)0); - // .. create failed; bail now to avoid overwriting the last error value. - if(!hMap) - WARN_RETURN(ERR::NO_MEM); - const DWORD ofs_hi = u64_hi(ofs), ofs_lo = u64_lo(ofs); - void* p = MapViewOfFileEx(hMap, dwAccess, ofs_hi, ofs_lo, (SIZE_T)len, start); - // .. make sure we got the requested address if MAP_FIXED was passed. - debug_assert(!(flags & MAP_FIXED) || (p == start)); - // .. free the mapping object now, so that we don't have to hold on to its - // handle until munmap(). it's not actually released yet due to the - // reference held by MapViewOfFileEx (if it succeeded). - CloseHandle(hMap); - // .. map failed; bail now to avoid "restoring" the last error value. - if(!p) - WARN_RETURN(ERR::NO_MEM); - - // slap on correct (more restrictive) permissions. - (void)mprotect(p, len, prot); - - WIN_RESTORE_LAST_ERROR; - *pp = p; - return INFO::OK; -} - - -void* mmap(void* start, size_t len, int prot, int flags, int fd, off_t ofs) -{ - void* p; - LibError err; - if(flags & MAP_ANONYMOUS) - err = mmap_mem(start, len, prot, flags, fd, &p); - else - err = mmap_file(start, len, prot, flags, fd, ofs, &p); - if(err < 0) - { - WARN_ERR(err); - LibError_set_errno(err); - return MAP_FAILED; - } - - return p; -} - - -int munmap(void* start, size_t UNUSED(len)) -{ - // UnmapViewOfFile checks if start was returned by MapViewOfFile*; - // if not, it will fail. - BOOL ok = UnmapViewOfFile(start); - if(!ok) - // VirtualFree requires dwSize to be 0 (entire region is released). - ok = VirtualFree(start, 0, MEM_RELEASE); - - WARN_RETURN_IF_FALSE(ok); // both failed - return 0; -} - - -//----------------------------------------------------------------------------- -// DLL -//----------------------------------------------------------------------------- - -static HMODULE HMODULE_from_void(void* handle) -{ - return (HMODULE)handle; -} - -static void* void_from_HMODULE(HMODULE hModule) -{ - return (void*)hModule; -} - - -int dlclose(void* handle) -{ - BOOL ok = FreeLibrary(HMODULE_from_void(handle)); - WARN_RETURN_IF_FALSE(ok); - return 0; -} - - -char* dlerror(void) -{ - return 0; -} - - -void* dlopen(const char* so_name, int flags) -{ - if(flags & RTLD_GLOBAL) - debug_warn("dlopen: unsupported flag(s)"); - - // if present, strip .so extension; add .dll extension - char dll_name[MAX_PATH]; - strcpy_s(dll_name, ARRAY_SIZE(dll_name)-5, so_name); - char* ext = (char*)path_extension(dll_name); - if(ext[0] == '\0') // no extension - strcat(dll_name, ".dll"); // safe - else // need to replace extension - SAFE_STRCPY(ext, "dll"); - - HMODULE hModule = LoadLibrary(dll_name); - if(!hModule) - debug_warn("dlopen failed"); - return void_from_HMODULE(hModule); -} - - -void* dlsym(void* handle, const char* sym_name) -{ - HMODULE hModule = HMODULE_from_void(handle); - void* sym = GetProcAddress(hModule, sym_name); - if(!sym) - debug_warn("dlsym failed"); - return sym; -} - - -//----------------------------------------------------------------------------- - - -int uname(struct utsname* un) -{ - static OSVERSIONINFO vi; - vi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); - GetVersionEx(&vi); - - // OS implementation name - const char* family = "??"; - int ver = (vi.dwMajorVersion << 8) | vi.dwMinorVersion; - if(vi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) - family = (ver == 0x045a)? "ME" : "9x"; - if(vi.dwPlatformId == VER_PLATFORM_WIN32_NT) - { - if(ver == 0x0500) - family = "2k"; - else if(ver == 0x0501) - family = "XP"; - else - family = "NT"; - } - sprintf(un->sysname, "Win%s", family); - - // release info - const char* vs = vi.szCSDVersion; - int sp; - if(sscanf(vs, "Service Pack %d", &sp) == 1) - sprintf(un->release, "SP %d", sp); - else - { - const char* release = ""; - if(vi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) - { - if(!strcmp(vs, " C")) - release = "OSR2"; - else if(!strcmp(vs, " A")) - release = "SE"; - } - strcpy(un->release, release); // safe - } - - // version - sprintf(un->version, "%lu.%02lu.%lu", vi.dwMajorVersion, vi.dwMinorVersion, vi.dwBuildNumber & 0xffff); - - // node name - DWORD buf_size = sizeof(un->nodename); - DWORD last_err = GetLastError(); - BOOL ok = GetComputerName(un->nodename, &buf_size); - // GetComputerName sets last error even on success - suppress. - if(ok) - SetLastError(last_err); - else - debug_warn("GetComputerName failed"); - - // hardware type - static SYSTEM_INFO si; - GetSystemInfo(&si); - if(si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) - strcpy(un->machine, "AMD64"); // safe - else - strcpy(un->machine, "IA-32"); // safe - - return 0; -} - - - - - - - -long sysconf(int name) -{ - // used by _SC_*_PAGES - static DWORD page_size; - static BOOL (WINAPI *pGlobalMemoryStatusEx)(MEMORYSTATUSEX*); - - ONCE( - { - // get page size - // (used by _SC_PAGESIZE and _SC_*_PAGES) - SYSTEM_INFO si; - GetSystemInfo(&si); // can't fail => page_size always > 0. - page_size = si.dwPageSize; - - // import GlobalMemoryStatusEx - it's not defined by the VC6 PSDK. - // used by _SC_*_PAGES if available (provides better results). - const HMODULE hKernel32Dll = LoadLibrary("kernel32.dll"); - *(void**)&pGlobalMemoryStatusEx = GetProcAddress(hKernel32Dll, "GlobalMemoryStatusEx"); - FreeLibrary(hKernel32Dll); - // make sure the reference is released so BoundsChecker - // doesn't complain. it won't actually be unloaded anyway - - // there is at least one other reference. - } - ); - - - switch(name) - { - case _SC_PAGESIZE: - // note: don't add _SC_PAGE_SIZE - they are different names but - // have the same value. - return page_size; - - case _SC_PHYS_PAGES: - case _SC_AVPHYS_PAGES: - { - u64 total_phys_mem; - u64 avail_phys_mem; - - // first try GlobalMemoryStatus - cannot fail. - // override its results if GlobalMemoryStatusEx is available. - MEMORYSTATUS ms; - GlobalMemoryStatus(&ms); - // can't fail. - total_phys_mem = ms.dwTotalPhys; - avail_phys_mem = ms.dwAvailPhys; - - // newer API is available: use it to report correct results - // (no overflow or wraparound) on systems with > 4 GB of memory. - MEMORYSTATUSEX mse = { sizeof(mse) }; - if(pGlobalMemoryStatusEx && pGlobalMemoryStatusEx(&mse)) - { - total_phys_mem = mse.ullTotalPhys; - avail_phys_mem = mse.ullAvailPhys; - } - // else: not an error, since this isn't available before Win2k / XP. - // we have results from GlobalMemoryStatus anyway. - - if(name == _SC_PHYS_PAGES) - return (long)(round_up((uintptr_t)total_phys_mem, 2*MiB) / page_size); - // Richter, "Programming Applications for Windows": - // reported value doesn't include non-paged pool reserved - // during boot; it's not considered available to kernel. - // it's 528 KiB on my 512 MiB machine (WinXP and Win2k). - else - return (long)(avail_phys_mem / page_size); - } - - default: - return -1; - } -} diff --git a/source/lib/sysdep/win/wposix.h b/source/lib/sysdep/win/wposix.h deleted file mode 100644 index f4570f6fa8..0000000000 --- a/source/lib/sysdep/win/wposix.h +++ /dev/null @@ -1,450 +0,0 @@ -/** - * ========================================================================= - * File : wposix.h - * Project : 0 A.D. - * Description : emulate a subset of POSIX on Win32. - * - * @author Jan.Wassenberg@stud.uni-karlsruhe.de - * ========================================================================= - */ - -/* - * Copyright (c) 2004-2005 Jan Wassenberg - * - * Redistribution and/or modification are also permitted under the - * terms of the GNU General Public License as published by the - * Free Software Foundation (version 2 or later, at your option). - * - * 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. - */ - -// note: try to avoid redefining CRT functions - if building against the -// DLL CRT, the declarations will be incompatible. adding _CRTIMP to the decl -// is a last resort (e.g. if the regular CRT headers would conflict). - - -#ifndef __WPOSIX_H__ -#define __WPOSIX_H__ - - -// split out of this module. -#include "wposix_types.h" -#include "waio.h" -#include "wsock.h" -#include "wtime.h" -#include "wpthread.h" - - -#ifdef __cplusplus -extern "C" { -#endif - - -// we define some CRT functions (e.g. access), because they're otherwise -// only brought in by win-specific headers (here, ). -// define correctly for static or DLL CRT in case the original header -// is included, to avoid conflict warnings. -#ifndef _CRTIMP -# ifdef _DLL -# define _CRTIMP __declspec(dllimport) -# else -# define _CRTIMP -# endif -#endif - - -// -// -// - -// Win32 MAX_PATH is 260; our number may be a bit more efficient. -#define PATH_MAX 256 - -#if OS_WIN -# ifndef SIZE_MAX // VC2005 already defines this in limits.h -# define SIZE_MAX 0xffffffff -# endif -#else -# define SIZE_MAX 0xffffffffffffffff -#endif - - -// -// sys/stat.h -// - -// already defined by MinGW -#if MSC_VERSION -typedef unsigned int mode_t; -#endif - -// VC libc includes stat, but it's quite slow. -// we implement our own, but use the CRT struct definition. -// rename the VC function definition to avoid conflict. -/* -#define stat vc_stat -// -// Extra hack for VC++ 2005, since it defines inline stat/fstat -// functions inside stat.h (which get confused by the -// macro-renaming of "stat") -# if MSC_VERSION >= 1400 -# define RC_INVOKED // stat.h only includes stat.inl if "!defined(RC_INVOKED) && !defined(__midl)" -# include -# undef RC_INVOKED -# else -# include -# endif -#undef stat -*/ -# include - -extern int mkdir(const char*, mode_t); - -// currently only sets st_mode (file or dir) and st_size. -//extern int stat(const char*, struct stat*); - -#define S_IRWXO 0xffff -#define S_IRWXU 0xffff -#define S_IRWXG 0xffff - // stat.h _S_* values are wrong! disassembly shows _S_IWRITE is 0x80, - // instead of 0x100. define christmas-tree value to be safe. - -#define S_ISDIR(m) (m & S_IFDIR) -#define S_ISREG(m) (m & S_IFREG) - - -// -// dirent.h -// - -typedef void DIR; - -struct dirent -{ - // note: SUSv3 describes this as a "char array" but of unspecified size. - // since that precludes using sizeof(), we may as well declare as a - // pointer to avoid copying in the implementation. - char* d_name; -}; - -extern DIR* opendir(const char* name); -extern struct dirent* readdir(DIR*); -extern int closedir(DIR*); - -// return status for the file returned by the last successful -// readdir call from the given directory stream. -// currently sets st_size, st_mode, and st_mtime; the rest are zeroed. -// non-portable, but considerably faster than stat(). used by file_enum. -extern int readdir_stat_np(DIR*, struct stat*); - - -// -// -// - -// mmap prot flags -#define PROT_NONE 0x00 -#define PROT_READ 0x01 -#define PROT_WRITE 0x02 -#define PROT_EXEC 0x04 - -// mmap flags -#define MAP_SHARED 0x01 // writes change the underlying file -#define MAP_PRIVATE 0x02 // writes do not affect the file (copy-on-write) -#define MAP_FIXED 0x04 -// .. non-portable -#define MAP_ANONYMOUS 0x10 -#define MAP_NORESERVE 0x20 - -// note: we need a means of only "reserving" virtual address ranges -// for the fixed-address expandable array mechanism. the non-portable -// MAP_NORESERVE flag says that no space in the page file need be reserved. -// the caller can still try to access the entire mapping, but might get -// SIGBUS if there isn't enough room to commit a page. Linux currently -// doesn't commit mmap-ed regions anyway, but we specify this flag to -// make sure of that in the future. - -#define MAP_FAILED ((void*)-1L) - -extern void* mmap(void* start, size_t len, int prot, int flags, int fd, off_t offset); -extern int munmap(void* start, size_t len); - -extern int mprotect(void* addr, size_t len, int prot); - -// -// -// - -// values from MS _open - do not change! -#define O_RDONLY 0x0000 // open for reading only -#define O_WRONLY 0x0001 // open for writing only -#define O_RDWR 0x0002 // open for reading and writing -#define O_APPEND 0x0008 // writes done at eof -#define O_CREAT 0x0100 // create and open file -#define O_TRUNC 0x0200 // open and truncate -#define O_EXCL 0x0400 // open only if file doesn't already exist - -// .. Win32-only (not specified by POSIX) -#define O_TEXT_NP 0x4000 // file mode is text (translated) -#define O_BINARY_NP 0x8000 // file mode is binary (untranslated) - -// .. wposix.cpp only (bit values not used by MS _open) -#define O_NO_AIO_NP 0x20000 - // wposix-specific: do not open a separate AIO-capable handle. - // this is used for small files where AIO overhead isn't worth it, - // thus speeding up loading and reducing resource usage. - -// .. not supported by Win32 (bit values not used by MS _open) -#define O_NONBLOCK 0x1000000 - - -// redefinition error here => io.h is getting included somewhere. -// we implement this function, so the io.h definition conflicts if -// compiling against the DLL CRT. either rename the io.h def -// (as with vc_stat), or don't include io.h. -extern int open(const char* fn, int mode, ...); - - -// -// -// - -// values from MS _access() implementation. do not change. -#define F_OK 0 -#define R_OK 4 -#define W_OK 2 -#define X_OK 0 - // MS implementation doesn't support this distinction. - // hence, the file is reported executable if it exists. - -extern int read (int fd, void* buf, size_t nbytes); // thunk -extern int write(int fd, void* buf, size_t nbytes); // thunk -extern _CRTIMP off_t lseek(int fd, off_t ofs, int whence); - - -// redefinition error here => io.h is getting included somewhere. -// we implement this function, so the io.h definition conflicts if -// compiling against the DLL CRT. either rename the io.h def -// (as with vc_stat), or don't include io.h. -extern int close(int); -extern _CRTIMP int access(const char*, int); - -extern int chdir(const char*); -#undef getcwd -extern char* getcwd(char*, size_t); - -extern _CRTIMP int rmdir(const char*); - -// user tests if available via #ifdef; can't use enum. -#define _SC_PAGESIZE 1 -#define _SC_PAGE_SIZE 1 -#define _SC_PHYS_PAGES 2 -#define _SC_AVPHYS_PAGES 3 - -extern long sysconf(int name); - - -// -// -// - -extern char* realpath(const char*, char*); - - - -// -// -// - -#define TCSANOW 0 - -struct termios -{ - long c_lflag; -}; - -#define ICANON 2 // do not change - correspond to ENABLE_LINE_INPUT / ENABLE_ECHO_INPUT -#define ECHO 4 - -extern int tcgetattr(int fd, struct termios* termios_p); -extern int tcsetattr(int fd, int optional_actions, const struct termios* termios_p); - - -// -// -// - -struct pollfd -{ - int fd; - short int events, revents; -}; - -#define POLLIN 1 - -extern int poll(struct pollfd[], int, int); - - -// -// -// - -// these have no meaning for the Windows GetProcAddress implementation, -// so they are ignored but provided for completeness. -#define RTLD_LAZY 0x01 -#define RTLD_NOW 0x02 -#define RTLD_GLOBAL 0x04 // semantics are unsupported, so complain if set. -#define RTLD_LOCAL 0x08 - -extern int dlclose(void* handle); -extern char* dlerror(void); -extern void* dlopen(const char* so_name, int flags); -extern void* dlsym(void* handle, const char* sym_name); - - -// -// -// - -struct utsname -{ - char sysname[9]; // Name of this implementation of the operating system. - char nodename[16]; // Name of this node within an implementation-defined - // communications network. - // Win9x requires this minimum buffer size. - char release[9]; // Current release level of this implementation. - char version[16]; // Current version level of this release. - char machine[9]; // Name of the hardware type on which the system is running. -}; - -extern int uname(struct utsname*); - - -// -// -// - -#include - -// this is an exhaustive list of SUSv3 error codes; -// see http://www.opengroup.org/onlinepubs/009695399/basedefs/errno.h.html . -// .. the following are already correctly defined by VC errno.h: -#if 0 -#define EPERM 1 // Operation not permitted -#define ENOENT 2 // No such file or directory -#define ESRCH 3 // No such process -#define EINTR 4 // Interrupted system call -#define EIO 5 // I/O error -#define ENXIO 6 // No such device or address -#define E2BIG 7 // Argument list too long -#define ENOEXEC 8 // Exec format error -#define EBADF 9 // Bad file number -#define ECHILD 10 // No child processes -#define EAGAIN 11 // Try again -#define ENOMEM 12 // Out of memory -#define EACCES 13 // Permission denied -#define EFAULT 14 // Bad address -#define EBUSY 16 // Device or resource busy -#define EEXIST 17 // File exists -#define ENODEV 19 // No such device -#define ENOTDIR 20 // Not a directory -#define EISDIR 21 // Is a directory -#define EINVAL 22 // Invalid argument -#define ENFILE 23 // File table overflow -#define EMFILE 24 // Too many open files -#define ENOTTY 25 // Not a typewriter -#define EFBIG 27 // File too large -#define ENOSPC 28 // No space left on device -#define ESPIPE 29 // Illegal seek -#define EMLINK 31 // Too many links -#define EPIPE 32 // Broken pipe -#define EDOM 33 // Math argument out of domain of func -#define ERANGE 34 // Math result not representable -#endif -// .. the following are unfortunately defined differently by VC errno.h; -// we have to stick with those interpretations because they are set -// by already-compiled CRT code. -#if 0 -#define EDEADLK 35 // Resource deadlock would occur -#define ENAMETOOLONG 36 // File name too long -#define ENOLCK 37 // No record locks available -#define ENOSYS 38 // Function not implemented -#define ENOTEMPTY 39 // Directory not empty -#define EILSEQ 84 // Illegal byte sequence -#endif -// .. the following aren't yet defined; we take on the Linux values for -// simplicity (why assign different values?) -#define ELOOP 40 // Too many symbolic links encountered -#define ENOMSG 42 // No message of desired type -#define EIDRM 43 // Identifier removed -#define EWOULDBLOCK EAGAIN // Operation would block -#define ENOLINK 67 // Reserved -#define EPROTO 71 // Protocol error -#define EMULTIHOP 72 // Reserved -#define EBADMSG 74 // Not a data message -#define EOVERFLOW 75 // Value too large for defined data type -#define ENOTSOCK 88 // Socket operation on non-socket -#define EDESTADDRREQ 89 // Destination address required -#define EMSGSIZE 90 // Message too long -#define EPROTOTYPE 91 // Protocol wrong type for socket -#define ENOPROTOOPT 92 // Protocol not available -#define EPROTONOSUPPORT 93 // Protocol not supported -#define EOPNOTSUPP 95 // Operation not supported on transport endpoint -#define EAFNOSUPPORT 97 // Address family not supported by protocol -#define EADDRINUSE 98 // Address already in use -#define EADDRNOTAVAIL 99 // Cannot assign requested address -#define ENETDOWN 100 // Network is down -#define ENETUNREACH 101 // Network is unreachable -#define ENETRESET 102 // Network dropped connection because of reset -#define ECONNABORTED 103 // Software caused connection abort -#define ECONNRESET 104 // Connection reset by peer -#define ENOBUFS 105 // No buffer space available -#define EISCONN 106 // Transport endpoint is already connected -#define ENOTCONN 107 // Transport endpoint is not connected -#define ETIMEDOUT 110 // Connection timed out -#define ECONNREFUSED 111 // Connection refused -#define EHOSTUNREACH 113 // No route to host -#define EALREADY 114 // Operation already in progress -#define EINPROGRESS 115 // Operation now in progress -#define ESTALE 116 // Reserved -#define EDQUOT 122 // Reserved -#define ECANCELED 125 // Operation Canceled - - -// -// serial port IOCTL -// - -// use with TIOCMBIS -#define TIOCM_RTS 1 - -// use with TIOCMGET or TIOCMIWAIT -#define TIOCM_CD 0x80 // MS_RLSD_ON -#define TIOCM_CTS 0x10 // MS_CTS_ON - -enum -{ - TIOCMBIS, // set control line - TIOCMGET, // get line state - TIOCMIWAIT // wait for status change -}; - -extern int ioctl(int fd, int op, int* data); - -#ifndef _WINSOCKAPI_ -#define FIONREAD 0 -#endif - - -extern void _get_console(void); -extern void _hide_console(void); - - -#ifdef __cplusplus -} -#endif - - -#endif // #ifndef __WPOSIX_H__ diff --git a/source/lib/sysdep/win/waio.cpp b/source/lib/sysdep/win/wposix/waio.cpp similarity index 88% rename from source/lib/sysdep/win/waio.cpp rename to source/lib/sysdep/win/wposix/waio.cpp index 4e6663e928..72b8ed9ae1 100644 --- a/source/lib/sysdep/win/waio.cpp +++ b/source/lib/sysdep/win/wposix/waio.cpp @@ -21,13 +21,15 @@ */ #include "precompiled.h" +#include "waio.h" #include #include // _aligned_malloc -#include "lib/lib.h" -#include "lib/posix.h" -#include "win_internal.h" +#include "wposix_internal.h" +#include "wfilesystem.h" // mode_t +#include "wtime.h" // timespec + #pragma data_seg(WIN_CALLBACK_PRE_LIBC(c)) WIN_REGISTER_FUNC(waio_init); @@ -333,6 +335,102 @@ fail: } + + +// do we want to open a second AIO-capable handle? +static bool isAioPossible(int fd, bool is_com_port, int oflag) +{ + // stdin/stdout/stderr + if(fd <= 2) + return false; + + // COM port - we don't currently need AIO access for those, and + // aio_reopen's CreateFile would fail with "access denied". + if(is_com_port) + return false; + + // caller is requesting we skip it (see file_open) + if(oflag & O_NO_AIO_NP) + return false; + + return true; +} + +int open(const char* fn, int oflag, ...) +{ + const bool is_com_port = strncmp(fn, "/dev/tty", 8) == 0; + // also used later, before aio_reopen + + // translate "/dev/tty%d" to "COM%d" + if(is_com_port) + { + char port[] = "COM1"; + const char digit = fn[8]+1; + // PCs only support COM1..COM4. + if(!('1' <= digit && digit <= '4')) + return -1; + port[3] = digit; + fn = port; + } + + mode_t mode = 0; + if(oflag & O_CREAT) + { + va_list args; + va_start(args, oflag); + mode = va_arg(args, mode_t); + va_end(args); + } + + WIN_SAVE_LAST_ERROR; // CreateFile + int fd = _open(fn, oflag, mode); + WIN_RESTORE_LAST_ERROR; + + // none of the above apply; now re-open the file. + // note: this is possible because _open defaults to DENY_NONE sharing. + if(isAioPossible(fd, is_com_port, oflag)) + WARN_ERR(aio_reopen(fd, fn, oflag)); + + // CRT doesn't like more than 255 files open. + // warn now, so that we notice why so many are open. +#ifndef NDEBUG + if(fd > 256) + WARN_ERR(ERR::LIMIT); +#endif + + return fd; +} + + +int close(int fd) +{ + debug_assert(3 <= fd && fd < 256); + + // note: there's no good way to notify us that wasn't opened for + // AIO, so we could skip aio_close. storing a bit in the fd is evil and + // a fd -> info map is redundant (waio already has one). + // therefore, we require aio_close to fail gracefully. + WARN_ERR(aio_close(fd)); + + return _close(fd); +} + + +// we don't want to #define read to _read, since that's a fairly common +// identifier. therefore, translate from MS CRT names via thunk functions. +// efficiency is less important, and the overhead could be optimized away. + +int read(int fd, void* buf, size_t nbytes) +{ + return _read(fd, buf, nbytes); +} + +int write(int fd, void* buf, size_t nbytes) +{ + return _write(fd, buf, nbytes); +} + + ////////////////////////////////////////////////////////////////////////////// // // Req @@ -581,7 +679,7 @@ static int aio_rw(struct aiocb* cb) r->ovl.Offset = u64_lo(actual_ofs); r->ovl.OffsetHigh = u64_hi(actual_ofs); - DWORD size32 = (DWORD)(actual_size & 0xffffffff); + DWORD size32 = (DWORD)(actual_size & 0xFFFFFFFF); BOOL ok; DWORD bytes_transferred; diff --git a/source/lib/sysdep/win/waio.h b/source/lib/sysdep/win/wposix/waio.h similarity index 62% rename from source/lib/sysdep/win/waio.h rename to source/lib/sysdep/win/wposix/waio.h index a75298dfac..8346a033bf 100644 --- a/source/lib/sysdep/win/waio.h +++ b/source/lib/sysdep/win/wposix/waio.h @@ -20,10 +20,9 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ -#ifndef WAIO_H__ -#define WAIO_H__ +#ifndef INCLUDED_WAIO +#define INCLUDED_WAIO -#include "lib/types.h" #include "wposix_types.h" @@ -50,6 +49,50 @@ struct sigevent }; +// +// +// + +// values from MS _open - do not change! +#define O_RDONLY 0x0000 // open for reading only +#define O_WRONLY 0x0001 // open for writing only +#define O_RDWR 0x0002 // open for reading and writing +#define O_APPEND 0x0008 // writes done at eof +#define O_CREAT 0x0100 // create and open file +#define O_TRUNC 0x0200 // open and truncate +#define O_EXCL 0x0400 // open only if file doesn't already exist + +// .. Win32-only (not specified by POSIX) +#define O_TEXT_NP 0x4000 // file mode is text (translated) +#define O_BINARY_NP 0x8000 // file mode is binary (untranslated) + +// .. wposix.cpp only (bit values not used by MS _open) +#define O_NO_AIO_NP 0x20000 +// wposix-specific: do not open a separate AIO-capable handle. +// this is used for small files where AIO overhead isn't worth it, +// thus speeding up loading and reducing resource usage. + +// .. not supported by Win32 (bit values not used by MS _open) +#define O_NONBLOCK 0x1000000 + + +// redefinition error here => io.h is getting included somewhere. +// we implement this function, so the io.h definition conflicts if +// compiling against the DLL CRT. either rename the io.h def +// (as with vc_stat), or don't include io.h. +extern int open(const char* fn, int mode, ...); +extern int close(int); + + +// +// +// + +extern int read (int fd, void* buf, size_t nbytes); // thunk +extern int write(int fd, void* buf, size_t nbytes); // thunk +extern _CRTIMP off_t lseek(int fd, off_t ofs, int whence); + + // // // @@ -87,6 +130,7 @@ extern int aio_error(const struct aiocb*); extern int aio_fsync(int, struct aiocb*); extern int aio_read(struct aiocb*); extern ssize_t aio_return(struct aiocb*); +struct timespec; extern int aio_suspend(const struct aiocb* const[], int, const struct timespec*); extern int aio_write(struct aiocb*); extern int lio_listio(int, struct aiocb* const[], int, struct sigevent*); @@ -97,4 +141,4 @@ extern int aio_reopen(int fd, const char* fn, int oflag, ...); // allocate and return a file descriptor extern int aio_assign_handle(uintptr_t handle); -#endif // #ifndef WAIO_H__ +#endif // #ifndef INCLUDED_WAIO diff --git a/source/lib/sysdep/win/wposix/wdlfcn.cpp b/source/lib/sysdep/win/wposix/wdlfcn.cpp new file mode 100644 index 0000000000..038db8d517 --- /dev/null +++ b/source/lib/sysdep/win/wposix/wdlfcn.cpp @@ -0,0 +1,62 @@ +#include "precompiled.h" +#include "wdlfcn.h" + +#include "lib/path_util.h" +#include "wposix_internal.h" + + +static HMODULE HMODULE_from_void(void* handle) +{ + return (HMODULE)handle; +} + +static void* void_from_HMODULE(HMODULE hModule) +{ + return (void*)hModule; +} + + +int dlclose(void* handle) +{ + BOOL ok = FreeLibrary(HMODULE_from_void(handle)); + WARN_RETURN_IF_FALSE(ok); + return 0; +} + + +char* dlerror(void) +{ + return 0; +} + + +void* dlopen(const char* so_name, int flags) +{ + if(flags & RTLD_GLOBAL) + debug_warn("dlopen: unsupported flag(s)"); + + // if present, strip .so extension; add .dll extension + char dll_name[MAX_PATH]; + strcpy_s(dll_name, ARRAY_SIZE(dll_name)-5, so_name); + char* ext = (char*)path_extension(dll_name); + if(ext[0] == '\0') // no extension + strcat(dll_name, ".dll"); // safe + else // need to replace extension + SAFE_STRCPY(ext, "dll"); + + HMODULE hModule = LoadLibrary(dll_name); + if(!hModule) + debug_warn("dlopen failed"); + return void_from_HMODULE(hModule); +} + + +void* dlsym(void* handle, const char* sym_name) +{ + HMODULE hModule = HMODULE_from_void(handle); + void* sym = GetProcAddress(hModule, sym_name); + if(!sym) + debug_warn("dlsym failed"); + return sym; +} + diff --git a/source/lib/sysdep/win/wposix/wdlfcn.h b/source/lib/sysdep/win/wposix/wdlfcn.h new file mode 100644 index 0000000000..39a96bf385 --- /dev/null +++ b/source/lib/sysdep/win/wposix/wdlfcn.h @@ -0,0 +1,20 @@ +#ifndef INCLUDED_WDLFCN +#define INCLUDED_WDLFCN + +// +// +// + +// these have no meaning for the Windows GetProcAddress implementation, +// so they are ignored but provided for completeness. +#define RTLD_LAZY 0x01 +#define RTLD_NOW 0x02 +#define RTLD_GLOBAL 0x04 // semantics are unsupported, so complain if set. +#define RTLD_LOCAL 0x08 + +extern int dlclose(void* handle); +extern char* dlerror(void); +extern void* dlopen(const char* so_name, int flags); +extern void* dlsym(void* handle, const char* sym_name); + +#endif // #ifndef INCLUDED_WDLFCN diff --git a/source/lib/sysdep/win/wposix/werrno.h b/source/lib/sysdep/win/wposix/werrno.h new file mode 100644 index 0000000000..733c2b95ea --- /dev/null +++ b/source/lib/sysdep/win/wposix/werrno.h @@ -0,0 +1,94 @@ +#ifndef INCLUDED_WERRNO +#define INCLUDED_WERRNO + +// +// +// + +#include + +// this is an exhaustive list of SUSv3 error codes; +// see http://www.opengroup.org/onlinepubs/009695399/basedefs/errno.h.html . +// .. the following are already correctly defined by VC errno.h: +#if 0 +#define EPERM 1 // Operation not permitted +#define ENOENT 2 // No such file or directory +#define ESRCH 3 // No such process +#define EINTR 4 // Interrupted system call +#define EIO 5 // I/O error +#define ENXIO 6 // No such device or address +#define E2BIG 7 // Argument list too long +#define ENOEXEC 8 // Exec format error +#define EBADF 9 // Bad file number +#define ECHILD 10 // No child processes +#define EAGAIN 11 // Try again +#define ENOMEM 12 // Out of memory +#define EACCES 13 // Permission denied +#define EFAULT 14 // Bad address +#define EBUSY 16 // Device or resource busy +#define EEXIST 17 // File exists +#define ENODEV 19 // No such device +#define ENOTDIR 20 // Not a directory +#define EISDIR 21 // Is a directory +#define EINVAL 22 // Invalid argument +#define ENFILE 23 // File table overflow +#define EMFILE 24 // Too many open files +#define ENOTTY 25 // Not a typewriter +#define EFBIG 27 // File too large +#define ENOSPC 28 // No space left on device +#define ESPIPE 29 // Illegal seek +#define EMLINK 31 // Too many links +#define EPIPE 32 // Broken pipe +#define EDOM 33 // Math argument out of domain of func +#define ERANGE 34 // Math result not representable +#endif +// .. the following are unfortunately defined differently by VC errno.h; +// we have to stick with those interpretations because they are set +// by already-compiled CRT code. +#if 0 +#define EDEADLK 35 // Resource deadlock would occur +#define ENAMETOOLONG 36 // File name too long +#define ENOLCK 37 // No record locks available +#define ENOSYS 38 // Function not implemented +#define ENOTEMPTY 39 // Directory not empty +#define EILSEQ 84 // Illegal byte sequence +#endif +// .. the following aren't yet defined; we take on the Linux values for +// simplicity (why assign different values?) +#define ELOOP 40 // Too many symbolic links encountered +#define ENOMSG 42 // No message of desired type +#define EIDRM 43 // Identifier removed +#define EWOULDBLOCK EAGAIN // Operation would block +#define ENOLINK 67 // Reserved +#define EPROTO 71 // Protocol error +#define EMULTIHOP 72 // Reserved +#define EBADMSG 74 // Not a data message +#define EOVERFLOW 75 // Value too large for defined data type +#define ENOTSOCK 88 // Socket operation on non-socket +#define EDESTADDRREQ 89 // Destination address required +#define EMSGSIZE 90 // Message too long +#define EPROTOTYPE 91 // Protocol wrong type for socket +#define ENOPROTOOPT 92 // Protocol not available +#define EPROTONOSUPPORT 93 // Protocol not supported +#define EOPNOTSUPP 95 // Operation not supported on transport endpoint +#define EAFNOSUPPORT 97 // Address family not supported by protocol +#define EADDRINUSE 98 // Address already in use +#define EADDRNOTAVAIL 99 // Cannot assign requested address +#define ENETDOWN 100 // Network is down +#define ENETUNREACH 101 // Network is unreachable +#define ENETRESET 102 // Network dropped connection because of reset +#define ECONNABORTED 103 // Software caused connection abort +#define ECONNRESET 104 // Connection reset by peer +#define ENOBUFS 105 // No buffer space available +#define EISCONN 106 // Transport endpoint is already connected +#define ENOTCONN 107 // Transport endpoint is not connected +#define ETIMEDOUT 110 // Connection timed out +#define ECONNREFUSED 111 // Connection refused +#define EHOSTUNREACH 113 // No route to host +#define EALREADY 114 // Operation already in progress +#define EINPROGRESS 115 // Operation now in progress +#define ESTALE 116 // Reserved +#define EDQUOT 122 // Reserved +#define ECANCELED 125 // Operation Canceled + +#endif // #ifndef INCLUDED_WERRNO diff --git a/source/lib/sysdep/win/wposix/wfilesystem.cpp b/source/lib/sysdep/win/wposix/wfilesystem.cpp new file mode 100644 index 0000000000..bea815c18b --- /dev/null +++ b/source/lib/sysdep/win/wposix/wfilesystem.cpp @@ -0,0 +1,353 @@ +#include "precompiled.h" +#include "wfilesystem.h" + +#include "lib/allocators.h" // single_calloc + +#include "wposix_internal.h" +#include "wtime_internal.h" // wtime_utc_filetime_to_time_t + + +// +// determine file system type on the current drive - +// needed to work around incorrect FAT time translation. +// + +static enum Filesystem +{ + FS_INVALID, // detect_filesystem() not yet called + FS_FAT, // FAT12, FAT16, or FAT32 + FS_NTFS, // (most common) + FS_UNKNOWN // newer FS we don't know about +} +filesystem; + + +// rationale: the previous method of checking every path was way too slow +// (taking ~800ms total during init). instead, we only determine the FS once. +// this is quite a bit easier than intercepting chdir() calls and/or +// caching FS type per drive letter, but not foolproof. +// +// if some data files are on a different volume that is set up as FAT, +// the workaround below won't be triggered (=> timestamps may be off by +// 1 hour when DST is in effect). oh well, that is not a supported. +// +// the common case (everything is on a single NTFS volume) is more important +// and must run without penalty. + + +// called from the first filetime_to_time_t() call, not win.cpp init; +// this means we can rely on the current directory having been set to +// the app's directory (and therefore its appendant volume - see above). +static void detect_filesystem() +{ + char root_path[MAX_PATH] = "c:\\"; // default in case GCD fails + DWORD gcd_ret = GetCurrentDirectory(sizeof(root_path), root_path); + debug_assert(gcd_ret != 0); + // if this fails, no problem - we have the default from above. + root_path[3] = '\0'; // cut off after "c:\" + + char fs_name[32] = {0}; + BOOL ret = GetVolumeInformation(root_path, 0,0,0,0,0, fs_name, sizeof(fs_name)); + fs_name[ARRAY_SIZE(fs_name)-1] = '\0'; + debug_assert(ret != 0); + // if this fails, no problem - we really only care if fs is FAT, + // and will assume that's not the case (since fs_name != "FAT"). + + filesystem = FS_UNKNOWN; + + if(!strncmp(fs_name, "FAT", 3)) // e.g. FAT32 + filesystem = FS_FAT; + else if(!strcmp(fs_name, "NTFS")) + filesystem = FS_NTFS; +} + + +// convert local FILETIME (includes timezone bias and possibly DST bias) +// to seconds-since-1970 UTC. +// +// note: splitting into month, year etc. is inefficient, +// but much easier than determining whether ft lies in DST, +// and ourselves adding the appropriate bias. +// +// called for FAT file times; see wposix filetime_to_time_t. +time_t time_t_from_local_filetime(FILETIME* ft) +{ + SYSTEMTIME st; + FileTimeToSystemTime(ft, &st); + + struct tm t; + t.tm_sec = st.wSecond; + t.tm_min = st.wMinute; + t.tm_hour = st.wHour; + t.tm_mday = st.wDay; + t.tm_mon = st.wMonth-1; + t.tm_year = st.wYear-1900; + t.tm_isdst = -1; + // let the CRT determine whether this local time + // falls under DST by the US rules. + return mktime(&t); +} + + +// convert Windows FILETIME to POSIX time_t (seconds-since-1970 UTC); +// used by stat and readdir_stat_np for st_mtime. +// +// works around a documented Windows bug in converting FAT file times +// (correct results are desired since VFS mount logic considers +// files 'equal' if their mtime and size are the same). +static time_t filetime_to_time_t(FILETIME* ft) +{ + ONCE(detect_filesystem()); + + // the FAT file system stores local file times, while + // NTFS records UTC. Windows does convert automatically, + // but uses the current DST settings. (boo!) + // we go back to local time, and convert properly. + if(filesystem == FS_FAT) + { + FILETIME local_ft; + FileTimeToLocalFileTime(ft, &local_ft); + return time_t_from_local_filetime(&local_ft); + } + + return wtime_utc_filetime_to_time_t(ft); +} + + +/* +// currently only sets st_mode (file or dir) and st_size. +int stat(const char* fn, struct stat* s) +{ + memset(s, 0, sizeof(struct stat)); + + WIN32_FILE_ATTRIBUTE_DATA fad; + if(!GetFileAttributesEx(fn, GetFileExInfoStandard, &fad)) + return -1; + + s->st_mtime = filetime_to_time_t(fad.ftLastAccessTime) + + // dir + if(fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + s->st_mode = S_IFDIR; + else + { + s->st_mode = S_IFREG; + s->st_size = (off_t)((((u64)fad.nFileSizeHigh) << 32) | fad.nFileSizeLow); + } + + return 0; +} +*/ + + +int mkdir(const char* path, mode_t) +{ + return CreateDirectory(path, 0)? 0 : -1; +} + + +//----------------------------------------------------------------------------- +// readdir +//----------------------------------------------------------------------------- + +// note: we avoid opening directories or returning entries that have +// hidden or system attributes set. this is to prevent returning something +// like "\System Volume Information", which raises an error upon opening. + +// 0-initialized by wdir_alloc for safety; this is required for +// num_entries_scanned. +struct WDIR +{ + HANDLE hFind; + + // the dirent returned by readdir. + // note: having only one global instance is not possible because + // multiple independent opendir/readdir sequences must be supported. + struct dirent ent; + + WIN32_FIND_DATA fd; + + // since opendir calls FindFirstFile, we need a means of telling the + // first call to readdir that we already have a file. + // that's the case iff this is == 0; we use a counter rather than a + // flag because that allows keeping statistics. + int num_entries_scanned; +}; + + +// suballocator - satisfies most requests with a reusable static instance, +// thus speeding up allocation and avoiding heap fragmentation. +// thread-safe. + +static WDIR global_wdir; +static uintptr_t global_wdir_is_in_use; + +// zero-initializes the WDIR (code below relies on this) +static inline WDIR* wdir_alloc() +{ + return (WDIR*)single_calloc(&global_wdir, &global_wdir_is_in_use, sizeof(WDIR)); +} + +static inline void wdir_free(WDIR* d) +{ + single_free(&global_wdir, &global_wdir_is_in_use, d); +} + + +static const DWORD hs = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM; + +// make sure path exists and is a normal (according to attributes) directory. +static bool is_normal_dir(const char* path) +{ + const DWORD fa = GetFileAttributes(path); + + // path not found + if(fa == INVALID_FILE_ATTRIBUTES) + return false; + + // not a directory + if((fa & FILE_ATTRIBUTE_DIRECTORY) == 0) + return false; + + // hidden or system attribute(s) set + // this check is now disabled because wsnd's add_oal_dlls_in_dir + // needs to open the Windows system directory, which sometimes has + // these attributes set. + //if((fa & hs) != 0) + // return false; + + return true; +} + + +DIR* opendir(const char* path) +{ + if(!is_normal_dir(path)) + { + errno = ENOENT; +fail: + debug_warn("opendir failed"); + return 0; + } + + WDIR* d = wdir_alloc(); + if(!d) + { + errno = ENOMEM; + goto fail; + } + + // build search path for FindFirstFile. note: "path\\dir" only returns + // information about that directory; trailing slashes aren't allowed. + // for dir entries to be returned, we have to append "\\*". + char search_path[PATH_MAX]; + snprintf(search_path, ARRAY_SIZE(search_path), "%s\\*", path); + + // note: we could store search_path and defer FindFirstFile until + // readdir. this way is a bit more complex but required for + // correctness (we must return a valid DIR iff is valid). + d->hFind = FindFirstFileA(search_path, &d->fd); + if(d->hFind == INVALID_HANDLE_VALUE) + { + // not an error - the directory is just empty. + if(GetLastError() == ERROR_NO_MORE_FILES) + goto success; + + // translate Win32 error to errno. + LibError err = LibError_from_win32(FALSE); + LibError_set_errno(err); + + // release the WDIR allocated above. + // unfortunately there's no way around this; we need to allocate + // d before FindFirstFile because it uses d->fd. copying from a + // temporary isn't nice either (this free doesn't happen often) + wdir_free(d); + goto fail; + } + +success: + return d; +} + + +struct dirent* readdir(DIR* d_) +{ + WDIR* const d = (WDIR*)d_; + + // avoid polluting the last error. + DWORD prev_err = GetLastError(); + + // first call - skip FindNextFile (see opendir). + if(d->num_entries_scanned == 0) + { + // this directory is empty. + if(d->hFind == INVALID_HANDLE_VALUE) + return 0; + goto already_have_file; + } + + // until end of directory or a valid entry was found: + for(;;) + { + if(!FindNextFileA(d->hFind, &d->fd)) + goto fail; +already_have_file: + + d->num_entries_scanned++; + + // not a hidden or system entry -> it's valid. + if((d->fd.dwFileAttributes & hs) == 0) + break; + } + + // this entry has passed all checks; return information about it. + // (note: d_name is a pointer; see struct dirent definition) + d->ent.d_name = d->fd.cFileName; + return &d->ent; + +fail: + // FindNextFile failed; determine why and bail. + // .. legit, end of dir reached. don't pollute last error code. + if(GetLastError() == ERROR_NO_MORE_FILES) + SetLastError(prev_err); + else + debug_warn("readdir: FindNextFile failed"); + return 0; +} + + +// return status for the dirent returned by the last successful +// readdir call from the given directory stream. +// currently sets st_size, st_mode, and st_mtime; the rest are zeroed. +// non-portable, but considerably faster than stat(). used by file_enum. +int readdir_stat_np(DIR* d_, struct stat* s) +{ + WDIR* d = (WDIR*)d_; + + memset(s, 0, sizeof(*s)); + s->st_size = (off_t)u64_from_u32(d->fd.nFileSizeHigh, d->fd.nFileSizeLow); + s->st_mode = (d->fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)? S_IFDIR : S_IFREG; + s->st_mtime = filetime_to_time_t(&d->fd.ftLastWriteTime); + return 0; +} + + +int closedir(DIR* d_) +{ + WDIR* const d = (WDIR*)d_; + + FindClose(d->hFind); + + wdir_free(d); + return 0; +} + + +//----------------------------------------------------------------------------- + +char* realpath(const char* fn, char* path) +{ + if(!GetFullPathName(fn, PATH_MAX, path, 0)) + return 0; + return path; +} diff --git a/source/lib/sysdep/win/wposix/wfilesystem.h b/source/lib/sysdep/win/wposix/wfilesystem.h new file mode 100644 index 0000000000..ce941305ed --- /dev/null +++ b/source/lib/sysdep/win/wposix/wfilesystem.h @@ -0,0 +1,96 @@ +#ifndef INCLUDED_WFILESYSTEM +#define INCLUDED_WFILESYSTEM + +// +// sys/stat.h +// + +// already defined by MinGW +#if MSC_VERSION +typedef unsigned int mode_t; +#endif + +// VC libc includes stat, but it's quite slow. +// we implement our own, but use the CRT struct definition. +// rename the VC function definition to avoid conflict. +/* +#define stat vc_stat +// +// Extra hack for VC++ 2005, since it defines inline stat/fstat +// functions inside stat.h (which get confused by the +// macro-renaming of "stat") +# if MSC_VERSION >= 1400 +# define RC_INVOKED // stat.h only includes stat.inl if "!defined(RC_INVOKED) && !defined(__midl)" +# include +# undef RC_INVOKED +# else +# include +# endif +#undef stat +*/ +#include + +extern int mkdir(const char*, mode_t); + +// currently only sets st_mode (file or dir) and st_size. +//extern int stat(const char*, struct stat*); + +#define S_IRWXO 0xFFFF +#define S_IRWXU 0xFFFF +#define S_IRWXG 0xFFFF +// stat.h _S_* values are wrong! disassembly shows _S_IWRITE is 0x80, +// instead of 0x100. define christmas-tree value to be safe. + +#define S_ISDIR(m) (m & S_IFDIR) +#define S_ISREG(m) (m & S_IFREG) + + +// +// dirent.h +// + +typedef void DIR; + +struct dirent +{ + // note: SUSv3 describes this as a "char array" but of unspecified size. + // since that precludes using sizeof(), we may as well declare as a + // pointer to avoid copying in the implementation. + char* d_name; +}; + +extern DIR* opendir(const char* name); +extern struct dirent* readdir(DIR*); +extern int closedir(DIR*); + +// return status for the file returned by the last successful +// readdir call from the given directory stream. +// currently sets st_size, st_mode, and st_mtime; the rest are zeroed. +// non-portable, but considerably faster than stat(). used by file_enum. +extern int readdir_stat_np(DIR*, struct stat*); + + +// +// +// + +extern char* realpath(const char*, char*); + + +// +// +// + +// values from MS _access() implementation. do not change. +#define F_OK 0 +#define R_OK 4 +#define W_OK 2 +// .. MS implementation doesn't support this distinction. +// hence, the file is reported executable if it exists. +#define X_OK 0 + +extern _CRTIMP int access(const char*, int); + +extern _CRTIMP int rmdir(const char*); + +#endif // #ifndef INCLUDED_WFILESYSTEM diff --git a/source/lib/sysdep/win/wposix/wmman.cpp b/source/lib/sysdep/win/wposix/wmman.cpp new file mode 100644 index 0000000000..1aa88bd423 --- /dev/null +++ b/source/lib/sysdep/win/wposix/wmman.cpp @@ -0,0 +1,207 @@ +#include "precompiled.h" +#include "wmman.h" + +#include "wposix_internal.h" + + +//----------------------------------------------------------------------------- +// memory mapping +//----------------------------------------------------------------------------- + +// convert POSIX PROT_* flags to their Win32 PAGE_* enumeration equivalents. +// used by mprotect. +static DWORD win32_prot(int prot) +{ + // this covers all 8 combinations of read|write|exec + // (note that "none" means all flags are 0). + switch(prot & (PROT_READ|PROT_WRITE|PROT_EXEC)) + { + case PROT_NONE: + return PAGE_NOACCESS; + case PROT_READ: + return PAGE_READONLY; + case PROT_WRITE: + // not supported by Win32; POSIX allows us to also grant read access. + return PAGE_READWRITE; + case PROT_EXEC: + return PAGE_EXECUTE; + case PROT_READ|PROT_WRITE: + return PAGE_READWRITE; + case PROT_READ|PROT_EXEC: + return PAGE_EXECUTE_READ; + case PROT_WRITE|PROT_EXEC: + // not supported by Win32; POSIX allows us to also grant read access. + return PAGE_EXECUTE_READWRITE; + case PROT_READ|PROT_WRITE|PROT_EXEC: + return PAGE_EXECUTE_READWRITE; + NODEFAULT; + } +} + + +int mprotect(void* addr, size_t len, int prot) +{ + const DWORD flNewProtect = win32_prot(prot); + DWORD flOldProtect; // required by VirtualProtect + BOOL ok = VirtualProtect(addr, len, flNewProtect, &flOldProtect); + WARN_RETURN_IF_FALSE(ok); + return 0; +} + + +// called when flags & MAP_ANONYMOUS +static LibError mmap_mem(void* start, size_t len, int prot, int flags, int fd, void** pp) +{ + // sanity checks. we don't care about these but enforce them to + // ensure callers are compatible with mmap. + // .. MAP_ANONYMOUS is documented to require this. + debug_assert(fd == -1); + // .. if MAP_SHARED, writes are to change "the underlying [mapped] + // object", but there is none here (we're backed by the page file). + debug_assert(flags & MAP_PRIVATE); + + // see explanation at MAP_NORESERVE definition. + bool want_commit = (prot != PROT_NONE && !(flags & MAP_NORESERVE)); + + // decommit a given area (leaves its address space reserved) + if(!want_commit && start != 0 && flags & MAP_FIXED) + { + MEMORY_BASIC_INFORMATION mbi; + WARN_RETURN_IF_FALSE(VirtualQuery(start, &mbi, sizeof(mbi))); + if(mbi.State == MEM_COMMIT) + { + WARN_IF_FALSE(VirtualFree(start, len, MEM_DECOMMIT)); + *pp = 0; + // make sure *pp won't be misinterpreted as an error + cassert(MAP_FAILED != 0); + return INFO::OK; + } + } + + DWORD flAllocationType = want_commit? MEM_COMMIT : MEM_RESERVE; + DWORD flProtect = win32_prot(prot); + void* p = VirtualAlloc(start, len, flAllocationType, flProtect); + if(!p) + WARN_RETURN(ERR::NO_MEM); + *pp = p; + return INFO::OK; +} + + +// given mmap prot and flags, output protection/access values for use with +// CreateFileMapping / MapViewOfFile. they only support read-only, +// read/write and copy-on-write, so we dumb it down to that and later +// set the correct (and more restrictive) permission via mprotect. +static LibError mmap_file_access(int prot, int flags, DWORD& flProtect, DWORD& dwAccess) +{ + // assume read-only; other cases handled below. + flProtect = PAGE_READONLY; + dwAccess = FILE_MAP_READ; + + if(prot & PROT_WRITE) + { + // determine write behavior: (whether they change the underlying file) + switch(flags & (MAP_SHARED|MAP_PRIVATE)) + { + // .. changes are written to file. + case MAP_SHARED: + flProtect = PAGE_READWRITE; + dwAccess = FILE_MAP_WRITE; // read and write + break; + // .. copy-on-write mapping; writes do not affect the file. + case MAP_PRIVATE: + flProtect = PAGE_WRITECOPY; + dwAccess = FILE_MAP_COPY; + break; + // .. either none or both of the flags are set. the latter is + // definitely illegal according to POSIX and some man pages + // say exactly one must be set, so abort. + default: + WARN_RETURN(ERR::INVALID_PARAM); + } + } + + return INFO::OK; +} + + +static LibError mmap_file(void* start, size_t len, int prot, int flags, + int fd, off_t ofs, void** pp) +{ + debug_assert(fd != -1); // handled by mmap_mem + + WIN_SAVE_LAST_ERROR; + + HANDLE hFile = HANDLE_from_intptr(_get_osfhandle(fd)); + if(hFile == INVALID_HANDLE_VALUE) + WARN_RETURN(ERR::INVALID_PARAM); + + // MapViewOfFileEx will fail if the "suggested" base address is + // nonzero but cannot be honored, so wipe out unless MAP_FIXED. + if(!(flags & MAP_FIXED)) + start = 0; + + // choose protection and access rights for CreateFileMapping / + // MapViewOfFile. these are weaker than what PROT_* allows and + // are augmented below by subsequently mprotect-ing. + DWORD flProtect; DWORD dwAccess; + RETURN_ERR(mmap_file_access(prot, flags, flProtect, dwAccess)); + + // enough foreplay; now actually map. + const HANDLE hMap = CreateFileMapping(hFile, 0, flProtect, 0, 0, (LPCSTR)0); + // .. create failed; bail now to avoid overwriting the last error value. + if(!hMap) + WARN_RETURN(ERR::NO_MEM); + const DWORD ofs_hi = u64_hi(ofs), ofs_lo = u64_lo(ofs); + void* p = MapViewOfFileEx(hMap, dwAccess, ofs_hi, ofs_lo, (SIZE_T)len, start); + // .. make sure we got the requested address if MAP_FIXED was passed. + debug_assert(!(flags & MAP_FIXED) || (p == start)); + // .. free the mapping object now, so that we don't have to hold on to its + // handle until munmap(). it's not actually released yet due to the + // reference held by MapViewOfFileEx (if it succeeded). + CloseHandle(hMap); + // .. map failed; bail now to avoid "restoring" the last error value. + if(!p) + WARN_RETURN(ERR::NO_MEM); + + // slap on correct (more restrictive) permissions. + (void)mprotect(p, len, prot); + + WIN_RESTORE_LAST_ERROR; + *pp = p; + return INFO::OK; +} + + +void* mmap(void* start, size_t len, int prot, int flags, int fd, off_t ofs) +{ + void* p; + LibError err; + if(flags & MAP_ANONYMOUS) + err = mmap_mem(start, len, prot, flags, fd, &p); + else + err = mmap_file(start, len, prot, flags, fd, ofs, &p); + if(err < 0) + { + WARN_ERR(err); + LibError_set_errno(err); + return MAP_FAILED; + } + + return p; +} + + +int munmap(void* start, size_t UNUSED(len)) +{ + // UnmapViewOfFile checks if start was returned by MapViewOfFile*; + // if not, it will fail. + BOOL ok = UnmapViewOfFile(start); + if(!ok) + // VirtualFree requires dwSize to be 0 (entire region is released). + ok = VirtualFree(start, 0, MEM_RELEASE); + + WARN_RETURN_IF_FALSE(ok); // both failed + return 0; +} + diff --git a/source/lib/sysdep/win/wposix/wmman.h b/source/lib/sysdep/win/wposix/wmman.h new file mode 100644 index 0000000000..344283111d --- /dev/null +++ b/source/lib/sysdep/win/wposix/wmman.h @@ -0,0 +1,37 @@ +#ifndef INCLUDED_WMMAN +#define INCLUDED_WMMAN + +// +// +// + +// mmap prot flags +#define PROT_NONE 0x00 +#define PROT_READ 0x01 +#define PROT_WRITE 0x02 +#define PROT_EXEC 0x04 + +// mmap flags +#define MAP_SHARED 0x01 // writes change the underlying file +#define MAP_PRIVATE 0x02 // writes do not affect the file (copy-on-write) +#define MAP_FIXED 0x04 +// .. non-portable +#define MAP_ANONYMOUS 0x10 +#define MAP_NORESERVE 0x20 + +// note: we need a means of only "reserving" virtual address ranges +// for the fixed-address expandable array mechanism. the non-portable +// MAP_NORESERVE flag says that no space in the page file need be reserved. +// the caller can still try to access the entire mapping, but might get +// SIGBUS if there isn't enough room to commit a page. Linux currently +// doesn't commit mmap-ed regions anyway, but we specify this flag to +// make sure of that in the future. + +#define MAP_FAILED ((void*)-1L) + +extern void* mmap(void* start, size_t len, int prot, int flags, int fd, off_t offset); +extern int munmap(void* start, size_t len); + +extern int mprotect(void* addr, size_t len, int prot); + +#endif // #ifndef INCLUDED_WMMAN diff --git a/source/lib/sysdep/win/wposix/wposix.cpp b/source/lib/sysdep/win/wposix/wposix.cpp new file mode 100644 index 0000000000..fb06e7d416 --- /dev/null +++ b/source/lib/sysdep/win/wposix/wposix.cpp @@ -0,0 +1,114 @@ +/** + * ========================================================================= + * File : wposix.cpp + * Project : 0 A.D. + * Description : emulate a subset of POSIX on Win32. + * + * @author Jan.Wassenberg@stud.uni-karlsruhe.de + * ========================================================================= + */ + +/* + * Copyright (c) 2004-2005 Jan Wassenberg + * + * Redistribution and/or modification are also permitted under the + * terms of the GNU General Public License as published by the + * Free Software Foundation (version 2 or later, at your option). + * + * 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. + */ + +#include "precompiled.h" +#include "wposix.h" + +#include "wposix_internal.h" + + +long sysconf(int name) +{ + // used by _SC_*_PAGES + static DWORD page_size; + static BOOL (WINAPI *pGlobalMemoryStatusEx)(MEMORYSTATUSEX*); + + ONCE( + { + // get page size + // (used by _SC_PAGESIZE and _SC_*_PAGES) + SYSTEM_INFO si; + GetSystemInfo(&si); // can't fail => page_size always > 0. + page_size = si.dwPageSize; + + // import GlobalMemoryStatusEx - it's not defined by the VC6 PSDK. + // used by _SC_*_PAGES if available (provides better results). + const HMODULE hKernel32Dll = LoadLibrary("kernel32.dll"); + *(void**)&pGlobalMemoryStatusEx = GetProcAddress(hKernel32Dll, "GlobalMemoryStatusEx"); + FreeLibrary(hKernel32Dll); + // make sure the reference is released so BoundsChecker + // doesn't complain. it won't actually be unloaded anyway - + // there is at least one other reference. + } + ); + + + switch(name) + { + case _SC_PAGESIZE: + // note: don't add _SC_PAGE_SIZE - they are different names but + // have the same value. + return page_size; + + case _SC_PHYS_PAGES: + case _SC_AVPHYS_PAGES: + { + u64 total_phys_mem; + u64 avail_phys_mem; + + // first try GlobalMemoryStatus - cannot fail. + // override its results if GlobalMemoryStatusEx is available. + MEMORYSTATUS ms; + GlobalMemoryStatus(&ms); + // can't fail. + total_phys_mem = ms.dwTotalPhys; + avail_phys_mem = ms.dwAvailPhys; + + // newer API is available: use it to report correct results + // (no overflow or wraparound) on systems with > 4 GB of memory. + MEMORYSTATUSEX mse = { sizeof(mse) }; + if(pGlobalMemoryStatusEx && pGlobalMemoryStatusEx(&mse)) + { + total_phys_mem = mse.ullTotalPhys; + avail_phys_mem = mse.ullAvailPhys; + } + // else: not an error, since this isn't available before Win2k / XP. + // we have results from GlobalMemoryStatus anyway. + + if(name == _SC_PHYS_PAGES) + return (long)(round_up((uintptr_t)total_phys_mem, 2*MiB) / page_size); + // Richter, "Programming Applications for Windows": + // reported value doesn't include non-paged pool reserved + // during boot; it's not considered available to kernel. + // it's 528 KiB on my 512 MiB machine (WinXP and Win2k). + else + return (long)(avail_phys_mem / page_size); + } + + default: + return -1; + } +} + + +//----------------------------------------------------------------------------- + +#ifdef REDEFINED_NEW +# include "lib/nommgr.h" +#endif +char* getcwd(char* buf, size_t buf_size) +{ + return _getcwd(buf, buf_size); +} +#ifdef REDEFINED_NEW +# include "lib/mmgr.h" +#endif diff --git a/source/lib/sysdep/win/wposix/wposix.h b/source/lib/sysdep/win/wposix/wposix.h new file mode 100644 index 0000000000..cc84caa084 --- /dev/null +++ b/source/lib/sysdep/win/wposix/wposix.h @@ -0,0 +1,40 @@ +/** + * ========================================================================= + * File : wposix.h + * Project : 0 A.D. + * Description : emulate a subset of POSIX on Win32. + * + * @author Jan.Wassenberg@stud.uni-karlsruhe.de + * ========================================================================= + */ + +/* + * Copyright (c) 2004-2005 Jan Wassenberg + * + * Redistribution and/or modification are also permitted under the + * terms of the GNU General Public License as published by the + * Free Software Foundation (version 2 or later, at your option). + * + * 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. + */ + +#ifndef INCLUDED_WPOSIX +#define INCLUDED_WPOSIX + +// misc routines + +extern int chdir(const char*); +#undef getcwd +extern char* getcwd(char*, size_t); + +// user tests if available via #ifdef; can't use enum. +#define _SC_PAGESIZE 1 +#define _SC_PAGE_SIZE 1 +#define _SC_PHYS_PAGES 2 +#define _SC_AVPHYS_PAGES 3 + +extern long sysconf(int name); + +#endif // #ifndef INCLUDED_WPOSIX diff --git a/source/lib/sysdep/win/wposix/wposix_internal.h b/source/lib/sysdep/win/wposix/wposix_internal.h new file mode 100644 index 0000000000..b30a5384d2 --- /dev/null +++ b/source/lib/sysdep/win/wposix/wposix_internal.h @@ -0,0 +1,34 @@ +#ifndef INCLUDED_WPOSIX_INTERNAL +#define INCLUDED_WPOSIX_INTERNAL + +#include "lib/lib.h" +#include "../win_internal.h" + +#include "werrno.h" + + +// we define some CRT functions (e.g. access), because they're otherwise +// only brought in by win-specific headers (here, ). +// define correctly for static or DLL CRT in case the original header +// is included, to avoid conflict warnings. +// +// note: try to avoid redefining CRT functions - if building against the +// DLL CRT, the declarations will be incompatible. adding _CRTIMP to the decl +// is a last resort (e.g. if the regular CRT headers would conflict). +#ifndef _CRTIMP +# ifdef _DLL +# define _CRTIMP extern "C" __declspec(dllimport) +# else +# define _CRTIMP +# endif +#endif + + +// cast intptr_t to HANDLE; centralized for easier changing, e.g. avoiding +// warnings. i = -1 converts to INVALID_HANDLE_VALUE (same value). +inline HANDLE HANDLE_from_intptr(intptr_t i) +{ + return (HANDLE)((char*)0 + i); +} + +#endif // #ifndef INCLUDED_WPOSIX_INTERNAL diff --git a/source/lib/sysdep/win/wposix_types.h b/source/lib/sysdep/win/wposix/wposix_types.h similarity index 76% rename from source/lib/sysdep/win/wposix_types.h rename to source/lib/sysdep/win/wposix/wposix_types.h index a899b6cb3c..c3a56a8971 100644 --- a/source/lib/sysdep/win/wposix_types.h +++ b/source/lib/sysdep/win/wposix/wposix_types.h @@ -20,6 +20,9 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ +#ifndef INCLUDED_WPOSIX_TYPES +#define INCLUDED_WPOSIX_TYPES + // // // @@ -51,9 +54,33 @@ typedef unsigned long long uint64_t; // however, it's not necessary with VC7 and later, and the compiler's // definition squelches 'pointer-to-int truncation' warnings, so don't. +#define INT16_MIN -32768 +#define INT16_MAX 32767 +#define UINT16_MIN 0u +#define UINT16_MAX 65536u + // // // typedef long ssize_t; + + +// +// +// + +// Win32 MAX_PATH is 260; our number may be a bit more efficient. +#define PATH_MAX 256 + +#if OS_WIN +# ifndef SIZE_MAX // VC2005 already defines this in limits.h +# define SIZE_MAX 0xFFFFFFFF +# endif +#else +# define SIZE_MAX 0xFFFFFFFFFFFFFFFF +#endif + + +#endif // #ifndef INCLUDED_WPOSIX_TYPES diff --git a/source/lib/sysdep/win/wpthread.cpp b/source/lib/sysdep/win/wposix/wpthread.cpp similarity index 98% rename from source/lib/sysdep/win/wpthread.cpp rename to source/lib/sysdep/win/wposix/wpthread.cpp index 8a67d6a598..48336ed331 100644 --- a/source/lib/sysdep/win/wpthread.cpp +++ b/source/lib/sysdep/win/wposix/wpthread.cpp @@ -21,15 +21,16 @@ */ #include "precompiled.h" +#include "wpthread.h" #include - #include -#include "lib/lib.h" -#include "lib/posix.h" -#include "win_internal.h" -#include "../cpu.h" // CAS +#include "lib/sysdep/cpu.h" // CAS + +#include "wposix_internal.h" +#include "wtime.h" // timespec + #pragma data_seg(WIN_CALLBACK_PRE_LIBC(b)) WIN_REGISTER_FUNC(wpthread_init); @@ -380,12 +381,12 @@ static DWORD calc_timeout_length_ms(const struct timespec* abs_timeout, // .. length > 49 days => result won't fit in 32 bits. most likely bogus. // note: we're careful to avoid returning exactly -1 since // that's the Win32 INFINITE value. - if(length_ms >= 0xffffffff) + if(length_ms >= 0xFFFFFFFF) { WARN_ERR(ERR::LIMIT); length_ms = 0xfffffffe; } - return (DWORD)(length_ms & 0xffffffff); + return (DWORD)(length_ms & 0xFFFFFFFF); } int sem_timedwait(sem_t* sem, const struct timespec* abs_timeout) diff --git a/source/lib/sysdep/win/wpthread.h b/source/lib/sysdep/win/wposix/wpthread.h similarity index 97% rename from source/lib/sysdep/win/wpthread.h rename to source/lib/sysdep/win/wposix/wpthread.h index cb83ffbcc0..98e76e4d7d 100644 --- a/source/lib/sysdep/win/wpthread.h +++ b/source/lib/sysdep/win/wposix/wpthread.h @@ -20,10 +20,8 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ -#ifndef WPTHREAD_H__ -#define WPTHREAD_H__ - -#include "wtime.h" // sem_timedwait +#ifndef INCLUDED_WPTHREAD +#define INCLUDED_WPTHREAD // @@ -117,4 +115,4 @@ extern int sem_destroy(sem_t*); // periodic timeout), or an error indication. extern int sem_msgwait_np(sem_t* sem); -#endif // #ifndef WPTHREAD_H__ +#endif // #ifndef INCLUDED_WPTHREAD diff --git a/source/lib/sysdep/win/wsock.cpp b/source/lib/sysdep/win/wposix/wsock.cpp similarity index 97% rename from source/lib/sysdep/win/wsock.cpp rename to source/lib/sysdep/win/wposix/wsock.cpp index 325a2f266b..27d5b5ff91 100644 --- a/source/lib/sysdep/win/wsock.cpp +++ b/source/lib/sysdep/win/wposix/wsock.cpp @@ -21,12 +21,10 @@ */ #include "precompiled.h" - -#include "win_internal.h" -#include "lib/lib.h" #include "wsock.h" -#include "delay_load.h" +#include "../delay_load.h" +#include "wposix_internal.h" #if MSC_VERSION diff --git a/source/lib/sysdep/win/wsock.h b/source/lib/sysdep/win/wposix/wsock.h similarity index 97% rename from source/lib/sysdep/win/wsock.h rename to source/lib/sysdep/win/wposix/wsock.h index 9b9651045a..6c869a6419 100644 --- a/source/lib/sysdep/win/wsock.h +++ b/source/lib/sysdep/win/wposix/wsock.h @@ -20,8 +20,8 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ -#ifndef WSOCK_H__ -#define WSOCK_H__ +#ifndef INCLUDED_WSOCK +#define INCLUDED_WSOCK #define IMP(ret, name, param) extern "C" __declspec(dllimport) ret __stdcall name param; @@ -48,7 +48,7 @@ typedef unsigned short sa_family_t; #define AF_INET6 23 #define PF_INET6 AF_INET6 -#define SOL_SOCKET 0xffff /* options for socket level */ +#define SOL_SOCKET 0xFFFF /* options for socket level */ #define TCP_NODELAY 0x0001 /* This is the slightly unreadable encoded form of the windows ioctl that sets @@ -227,4 +227,4 @@ IMP(ssize_t, recvfrom, (int, void*, size_t, int, struct sockaddr*, socklen_t*)) #undef IMP -#endif // #ifndef WSOCK_H__ +#endif // #ifndef INCLUDED_WSOCK diff --git a/source/lib/sysdep/win/wposix/wterminal.cpp b/source/lib/sysdep/win/wposix/wterminal.cpp new file mode 100644 index 0000000000..da1275dcde --- /dev/null +++ b/source/lib/sysdep/win/wposix/wterminal.cpp @@ -0,0 +1,88 @@ +#include "precompiled.h" +#include "wterminal.h" + +#include "wposix_internal.h" + + +int ioctl(int fd, int op, int* data) +{ + const HANDLE h = HANDLE_from_intptr(_get_osfhandle(fd)); + + switch(op) + { + case TIOCMGET: + /* TIOCM_* mapped directly to MS_*_ON */ + GetCommModemStatus(h, (DWORD*)data); + break; + + case TIOCMBIS: + /* only RTS supported */ + if(*data & TIOCM_RTS) + EscapeCommFunction(h, SETRTS); + else + EscapeCommFunction(h, CLRRTS); + break; + + case TIOCMIWAIT: + static DWORD mask; + DWORD new_mask = 0; + if(*data & TIOCM_CD) + new_mask |= EV_RLSD; + if(*data & TIOCM_CTS) + new_mask |= EV_CTS; + if(new_mask != mask) + SetCommMask(h, mask = new_mask); + WaitCommEvent(h, &mask, 0); + break; + } + + return 0; +} + + + +static HANDLE std_h[2] = { (HANDLE)((char*)0 + 3), (HANDLE)((char*)0 + 7) }; + + +void _get_console() +{ + AllocConsole(); +} + +void _hide_console() +{ + FreeConsole(); +} + + +int tcgetattr(int fd, struct termios* termios_p) +{ + if(fd >= 2) + return -1; + HANDLE h = std_h[fd]; + + DWORD mode; + GetConsoleMode(h, &mode); + termios_p->c_lflag = mode & (ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT); + + return 0; +} + + +int tcsetattr(int fd, int /* optional_actions */, const struct termios* termios_p) +{ + if(fd >= 2) + return -1; + HANDLE h = std_h[fd]; + SetConsoleMode(h, (DWORD)termios_p->c_lflag); + FlushConsoleInputBuffer(h); + + return 0; +} + + +int poll(struct pollfd /* fds */[], int /* nfds */, int /* timeout */) +{ + return -1; +} + diff --git a/source/lib/sysdep/win/wposix/wterminal.h b/source/lib/sysdep/win/wposix/wterminal.h new file mode 100644 index 0000000000..db13481aa4 --- /dev/null +++ b/source/lib/sysdep/win/wposix/wterminal.h @@ -0,0 +1,66 @@ +#ifndef INCLUDED_WTERMINAL +#define INCLUDED_WTERMINAL + +// +// serial port IOCTL +// + +// use with TIOCMBIS +#define TIOCM_RTS 1 + +// use with TIOCMGET or TIOCMIWAIT +#define TIOCM_CD 0x80 // MS_RLSD_ON +#define TIOCM_CTS 0x10 // MS_CTS_ON + +enum +{ + TIOCMBIS, // set control line + TIOCMGET, // get line state + TIOCMIWAIT // wait for status change +}; + +extern int ioctl(int fd, int op, int* data); + +#ifndef _WINSOCKAPI_ +#define FIONREAD 0 +#endif + + +extern void _get_console(void); +extern void _hide_console(void); + + +// +// +// + +struct pollfd +{ + int fd; + short int events, revents; +}; + +#define POLLIN 1 + +extern int poll(struct pollfd[], int, int); + + + +// +// +// + +#define TCSANOW 0 + +struct termios +{ + long c_lflag; +}; + +#define ICANON 2 // do not change - correspond to ENABLE_LINE_INPUT / ENABLE_ECHO_INPUT +#define ECHO 4 + +extern int tcgetattr(int fd, struct termios* termios_p); +extern int tcsetattr(int fd, int optional_actions, const struct termios* termios_p); + +#endif // #ifndef INCLUDED_WTERMINAL diff --git a/source/lib/sysdep/win/wtime.cpp b/source/lib/sysdep/win/wposix/wtime.cpp similarity index 95% rename from source/lib/sysdep/win/wtime.cpp rename to source/lib/sysdep/win/wposix/wtime.cpp index 66b5e54211..61577205a2 100644 --- a/source/lib/sysdep/win/wtime.cpp +++ b/source/lib/sysdep/win/wposix/wtime.cpp @@ -21,20 +21,18 @@ */ #include "precompiled.h" +#include "wtime.h" #include #include #include #include -#include "win_internal.h" -#include // _beginthreadex - -#include "lib/lib.h" -#include "lib/posix.h" #include "lib/adts.h" #include "lib/sysdep/ia32.h" #include "lib/sysdep/cpu.h" +#include "wposix_internal.h" +#include "wpthread.h" // define to disable time sources (useful for simulating other systems) @@ -671,45 +669,18 @@ static u64 u64_from_FILETIME(const FILETIME* ft) // convert UTC FILETIME to seconds-since-1970 UTC: // we just have to subtract POSIX epoch and scale down to units of seconds. // +// used by wfilesystem. +// // note: RtlTimeToSecondsSince1970 isn't officially documented, // so don't use that. -time_t utc_filetime_to_time_t(FILETIME* ft) +time_t wtime_utc_filetime_to_time_t(FILETIME* ft) { u64 hns = u64_from_FILETIME(ft); u64 s = (hns - posix_epoch_hns) / _1e7; - return (time_t)(s & 0xffffffff); + return (time_t)(s & 0xFFFFFFFF); } -// convert local FILETIME (includes timezone bias and possibly DST bias) -// to seconds-since-1970 UTC. -// -// note: splitting into month, year etc. is inefficient, -// but much easier than determining whether ft lies in DST, -// and ourselves adding the appropriate bias. -// -// called for FAT file times; see wposix filetime_to_time_t. -time_t time_t_from_local_filetime(FILETIME* ft) -{ - SYSTEMTIME st; - FileTimeToSystemTime(ft, &st); - - struct tm t; - t.tm_sec = st.wSecond; - t.tm_min = st.wMinute; - t.tm_hour = st.wHour; - t.tm_mday = st.wDay; - t.tm_mon = st.wMonth-1; - t.tm_year = st.wYear-1900; - t.tm_isdst = -1; - // let the CRT determine whether this local time - // falls under DST by the US rules. - return mktime(&t); -} - - - - // return nanoseconds since posix epoch as reported by system time // only 10 or 15 ms resolution! static i64 st_time_ns() @@ -789,7 +760,7 @@ int clock_gettime(clockid_t clock, struct timespec* t) debug_assert(clock == CLOCK_REALTIME); const i64 ns = time_ns(); - t->tv_sec = (time_t)((ns / _1e9) & 0xffffffff); + t->tv_sec = (time_t)((ns / _1e9) & 0xFFFFFFFF); t->tv_nsec = (long) (ns % _1e9); return 0; } diff --git a/source/lib/sysdep/win/wtime.h b/source/lib/sysdep/win/wposix/wtime.h similarity index 83% rename from source/lib/sysdep/win/wtime.h rename to source/lib/sysdep/win/wposix/wtime.h index 2f0cc62eb9..11b96376f1 100644 --- a/source/lib/sysdep/win/wtime.h +++ b/source/lib/sysdep/win/wposix/wtime.h @@ -77,13 +77,4 @@ extern int nanosleep(const struct timespec* rqtp, struct timespec* rmtp); extern int clock_gettime(clockid_t clock, struct timespec* ts); extern int clock_getres(clockid_t clock, struct timespec* res); - -// HACK: on Windows, the HRT makes its final implementation choice -// in the first calibrate call where cpu_freq is available. -// provide a routine that makes the choice when called, -// so app code isn't surprised by a timer change, although the HRT -// does try to keep the timer continuous. -extern void wtime_reset_impl(void); - - #endif // #ifndef WTIME_H__ diff --git a/source/lib/sysdep/win/wposix/wtime_internal.h b/source/lib/sysdep/win/wposix/wtime_internal.h new file mode 100644 index 0000000000..f091a4ef77 --- /dev/null +++ b/source/lib/sysdep/win/wposix/wtime_internal.h @@ -0,0 +1,19 @@ +#ifndef INCLUDED_WTIME_INTERNAL +#define INCLUDED_WTIME_INTERNAL + +// HACK: on Windows, the HRT makes its final implementation choice +// in the first calibrate call where cpu_freq is available. +// provide a routine that makes the choice when called, +// so app code isn't surprised by a timer change, although the HRT +// does try to keep the timer continuous. +extern void wtime_reset_impl(void); + + +// convert UTC FILETIME to seconds-since-1970 UTC. +// used by wfilesystem. +#ifndef _FILETIME_ // prevent ICE on VC7 +struct FILETIME; +#endif +extern time_t wtime_utc_filetime_to_time_t(FILETIME* ft); + +#endif // #ifndef INCLUDED_WTIME_INTERNAL diff --git a/source/lib/sysdep/win/wposix/wutsname.cpp b/source/lib/sysdep/win/wposix/wutsname.cpp new file mode 100644 index 0000000000..18a1422dab --- /dev/null +++ b/source/lib/sysdep/win/wposix/wutsname.cpp @@ -0,0 +1,68 @@ +#include "precompiled.h" +#include "wutsname.h" + +#include "wposix_internal.h" + +int uname(struct utsname* un) +{ + static OSVERSIONINFO vi; + vi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); + GetVersionEx(&vi); + + // OS implementation name + const char* family = "??"; + int ver = (vi.dwMajorVersion << 8) | vi.dwMinorVersion; + if(vi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) + family = (ver == 0x045a)? "ME" : "9x"; + if(vi.dwPlatformId == VER_PLATFORM_WIN32_NT) + { + if(ver == 0x0500) + family = "2k"; + else if(ver == 0x0501) + family = "XP"; + else + family = "NT"; + } + sprintf(un->sysname, "Win%s", family); + + // release info + const char* vs = vi.szCSDVersion; + int sp; + if(sscanf(vs, "Service Pack %d", &sp) == 1) + sprintf(un->release, "SP %d", sp); + else + { + const char* release = ""; + if(vi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) + { + if(!strcmp(vs, " C")) + release = "OSR2"; + else if(!strcmp(vs, " A")) + release = "SE"; + } + strcpy(un->release, release); // safe + } + + // version + sprintf(un->version, "%lu.%02lu.%lu", vi.dwMajorVersion, vi.dwMinorVersion, vi.dwBuildNumber & 0xFFFF); + + // node name + DWORD buf_size = sizeof(un->nodename); + DWORD last_err = GetLastError(); + BOOL ok = GetComputerName(un->nodename, &buf_size); + // GetComputerName sets last error even on success - suppress. + if(ok) + SetLastError(last_err); + else + debug_warn("GetComputerName failed"); + + // hardware type + static SYSTEM_INFO si; + GetSystemInfo(&si); + if(si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) + strcpy(un->machine, "AMD64"); // safe + else + strcpy(un->machine, "IA-32"); // safe + + return 0; +} diff --git a/source/lib/sysdep/win/wposix/wutsname.h b/source/lib/sysdep/win/wposix/wutsname.h new file mode 100644 index 0000000000..002d299c69 --- /dev/null +++ b/source/lib/sysdep/win/wposix/wutsname.h @@ -0,0 +1,22 @@ +#ifndef INCLUDED_WUTSNAME +#define INCLUDED_WUTSNAME + +// +// +// + +struct utsname +{ + char sysname[9]; // Name of this implementation of the operating system. + char nodename[16]; // Name of this node within an implementation-defined + // communications network. + // Win9x requires this minimum buffer size. + char release[9]; // Current release level of this implementation. + char version[16]; // Current version level of this release. + char machine[9]; // Name of the hardware type on which the system is running. +}; + +extern int uname(struct utsname*); + + +#endif // #ifndef INCLUDED_WUTSNAME diff --git a/source/lib/sysdep/win/wsdl.cpp b/source/lib/sysdep/win/wsdl.cpp index 72451a534e..bc80a659b2 100644 --- a/source/lib/sysdep/win/wsdl.cpp +++ b/source/lib/sysdep/win/wsdl.cpp @@ -21,6 +21,7 @@ */ #include "precompiled.h" +#include "lib/sdl.h" #include #include @@ -32,9 +33,8 @@ #include // _beginthreadex #include // message crackers -#include "lib/sdl.h" +#include "lib/posix/posix_pthread.h" #include "lib/lib.h" -#include "lib/posix.h" #include "lib/ogl.h" // needed to pull in the delay-loaded opengl32.dll @@ -1201,7 +1201,7 @@ u32 SDL_Swap32(const u32 x) #ifndef SDL_Swap64 u64 SDL_Swap64(const u64 x) { - const u32 lo = (u32)(x & 0xffffffff); + const u32 lo = (u32)(x & 0xFFFFFFFF); const u32 hi = (u32)(x >> 32); u64 ret = SDL_Swap32(lo); ret <<= 32; diff --git a/source/lib/sysdep/win/wsdl.h b/source/lib/sysdep/win/wsdl.h index 6993113355..26ae79e258 100644 --- a/source/lib/sysdep/win/wsdl.h +++ b/source/lib/sysdep/win/wsdl.h @@ -23,7 +23,6 @@ #ifndef WSDL_H__ #define WSDL_H__ -#include "lib/types.h" #include "SDL/SDL_keysym.h" typedef u8 Uint8; diff --git a/source/lib/sysdep/win/wsnd.cpp b/source/lib/sysdep/win/wsnd.cpp index 071ff0e974..ae9ab1c7ae 100644 --- a/source/lib/sysdep/win/wsnd.cpp +++ b/source/lib/sysdep/win/wsnd.cpp @@ -21,18 +21,18 @@ */ #include "precompiled.h" - -#include -#include +#include "lib/sysdep/snd.h" #include #include +#include +#include #include "lib/path_util.h" -#include "lib/sysdep/snd.h" +#include "lib/res/file/file.h" #include "dll_ver.h" // dll_list_* #include "win_internal.h" -#include "lib/res/file/file.h" + // DirectSound header // HACK: workaround for "subwtype.h not found" errors on VC6/7 hybrid. diff --git a/source/lib/sysdep/win/wsysdep.cpp b/source/lib/sysdep/win/wsysdep.cpp index ee512926d3..b66c3a3523 100644 --- a/source/lib/sysdep/win/wsysdep.cpp +++ b/source/lib/sysdep/win/wsysdep.cpp @@ -21,14 +21,13 @@ */ #include "precompiled.h" +#include "lib/sysdep/sysdep.h" #include "win_internal.h" // includes windows.h; must come before shlobj #include // pick_dir #include "lib/lib.h" -#include "lib/posix.h" #include "error_dialog.h" -#include "lib/res/graphics/tex.h" #if MSC_VERSION diff --git a/source/lib/tests/test_lockfree.h b/source/lib/tests/test_lockfree.h index 4c07a6abd2..6db9fcaee6 100644 --- a/source/lib/tests/test_lockfree.h +++ b/source/lib/tests/test_lockfree.h @@ -1,7 +1,6 @@ #include "lib/self_test.h" #include "lib/lib.h" -#include "lib/posix.h" #include "lib/lockfree.h" #include "lib/sysdep/cpu.h" // atomic_add #include "lib/timer.h" diff --git a/source/lib/tests/test_string_s.h b/source/lib/tests/test_string_s.h index 4399cdf985..3b6ca9de0b 100644 --- a/source/lib/tests/test_string_s.h +++ b/source/lib/tests/test_string_s.h @@ -107,7 +107,7 @@ public: // this is still preferable to completely disabling the self-test. void test_param_validation() { - #if !HAVE_STRING_S + #if !HAVE_SECURE_CRT debug_skip_next_err(ERR::INVALID_PARAM); TEST_CPY(0 ,0,0 , EINVAL,""); // all invalid debug_skip_next_err(ERR::INVALID_PARAM); diff --git a/source/lib/timer.cpp b/source/lib/timer.cpp index bd464f940e..ef4c0d3c83 100644 --- a/source/lib/timer.cpp +++ b/source/lib/timer.cpp @@ -22,16 +22,15 @@ */ #include "precompiled.h" +#include "timer.h" #include #include #include +#include "lib/posix/posix_time.h" #include "lib.h" -#include "posix.h" -#include "timer.h" #include "adts.h" - #include "lib/sysdep/cpu.h" // rationale for wrapping gettimeofday and clock_gettime, instead of emulating diff --git a/source/lib/types.h b/source/lib/types.h index f9191f5d1e..c398660fc3 100644 --- a/source/lib/types.h +++ b/source/lib/types.h @@ -23,7 +23,7 @@ #ifndef __TYPES_H__ #define __TYPES_H__ -#include "posix_types.h" +#include "posix/posix_types.h" // defines instead of typedefs so we can #undef conflicting decls diff --git a/source/network/AllNetMessages.h b/source/network/AllNetMessages.h index f682b3e71e..16ef994a72 100644 --- a/source/network/AllNetMessages.h +++ b/source/network/AllNetMessages.h @@ -1,7 +1,6 @@ #ifndef _AllNetMessages_H #define _AllNetMessages_H -#include "lib/types.h" #include "ps/CStr.h" #include "scripting/JSSerialization.h" @@ -112,7 +111,7 @@ enum PS_CHAT_RCP_FIRST_SPECIAL=0xfffd, PS_CHAT_RCP_ENEMIES=0xfffd, PS_CHAT_RCP_ALLIES=0xfffe, - PS_CHAT_RCP_ALL=0xffff, + PS_CHAT_RCP_ALL=0xFFFF, }; enum diff --git a/source/network/NetMessage.cpp b/source/network/NetMessage.cpp index de2808e7cd..908e8c540b 100644 --- a/source/network/NetMessage.cpp +++ b/source/network/NetMessage.cpp @@ -1,6 +1,5 @@ #include "precompiled.h" -#include "lib/posix.h" #include "lib/lib.h" #include #include diff --git a/source/network/NetMessage.h b/source/network/NetMessage.h index 1f1efec654..3f97148cf3 100644 --- a/source/network/NetMessage.h +++ b/source/network/NetMessage.h @@ -1,7 +1,6 @@ #ifndef _NetMessage_H #define _NetMessage_H -#include "lib/types.h" #include "Serialization.h" #include "SocketBase.h" diff --git a/source/network/Network.cpp b/source/network/Network.cpp index 0712fb7e84..ea980c716a 100644 --- a/source/network/Network.cpp +++ b/source/network/Network.cpp @@ -2,11 +2,10 @@ #include "Network.h" #include "Serialization.h" -#include - #include "ps/CLogger.h" #include "NetLog.h" + DEFINE_ERROR(CONFLICTING_OP_IN_PROGRESS, "A conflicting operation is already in progress"); #define ALIGN_UP(_n, _block) (_n+_block-(_n%_block)) diff --git a/source/network/Network.h b/source/network/Network.h index 29cc072fda..5a4647a4db 100644 --- a/source/network/Network.h +++ b/source/network/Network.h @@ -45,8 +45,6 @@ MORE INFO // Includes / Compiler directives //-------------------------------------------------------- -#include "lib/posix.h" -#include "lib/types.h" #include "ps/Pyrogenesis.h" #include "ps/ThreadUtil.h" #include "ps/Singleton.h" diff --git a/source/network/Serialization.h b/source/network/Serialization.h index bc76fdcb57..9c814be9a7 100644 --- a/source/network/Serialization.h +++ b/source/network/Serialization.h @@ -1,7 +1,6 @@ #ifndef _Serialization_H #define _Serialization_H -#include "lib/types.h" #include "lib/lib.h" #define Serialize_int_1(_pos, _val) \ diff --git a/source/network/SocketBase.cpp b/source/network/SocketBase.cpp index 9dabddeaed..3912697054 100644 --- a/source/network/SocketBase.cpp +++ b/source/network/SocketBase.cpp @@ -14,8 +14,6 @@ #include "ps/CLogger.h" #include "NetLog.h" -#include - // Record global transfer statistics (sent/recvd bytes). This will put a lock // /unlock pair in all read and write operations. #define RECORD_GLOBAL_STATS 1 diff --git a/source/network/SocketBase.h b/source/network/SocketBase.h index 4be950bf76..a80d12a1bf 100644 --- a/source/network/SocketBase.h +++ b/source/network/SocketBase.h @@ -5,8 +5,7 @@ // Includes / Compiler directives //-------------------------------------------------------- -#include "lib/posix.h" -#include "lib/types.h" +#include "lib/posix/posix_sock.h" #include "ps/Pyrogenesis.h" #include #include "ps/CStr.h" diff --git a/source/network/StreamSocket.h b/source/network/StreamSocket.h index 96a319e626..970162e903 100644 --- a/source/network/StreamSocket.h +++ b/source/network/StreamSocket.h @@ -1,7 +1,6 @@ #ifndef _StreamSocket_H #define _StreamSocket_H -#include "lib/types.h" #include "ps/Pyrogenesis.h" #include "Network.h" #include "SocketBase.h" diff --git a/source/ps/CStr.cpp b/source/ps/CStr.cpp index 965a90cbfd..cb63e9dd6b 100644 --- a/source/ps/CStr.cpp +++ b/source/ps/CStr.cpp @@ -3,7 +3,7 @@ #ifndef CStr_CPP_FIRST #define CStr_CPP_FIRST -#include "lib/posix.h" // for htons, ntohs +#include "lib/posix/posix_sock.h" // htons, ntohs #include "network/Serialization.h" #include diff --git a/source/ps/DllLoader.cpp b/source/ps/DllLoader.cpp index 0e0aa8cdd6..07b1ff196a 100644 --- a/source/ps/DllLoader.cpp +++ b/source/ps/DllLoader.cpp @@ -2,7 +2,7 @@ #include "DllLoader.h" -#include "lib/posix.h" +#include "lib/posix/posix_dlfcn.h" #include "lib/lib.h" void* const HANDLE_UNAVAILABLE = (void*)-1; diff --git a/source/ps/Errors.cpp b/source/ps/Errors.cpp index 63c8637034..b4b0b3d4a5 100644 --- a/source/ps/Errors.cpp +++ b/source/ps/Errors.cpp @@ -118,69 +118,69 @@ extern const PSRETURN CODE__PSRETURN_Scripting_DefineType = 0x09010000; extern const PSRETURN MASK__PSRETURN_Scripting_LoadFile = 0xffff0000; extern const PSRETURN CODE__PSRETURN_Scripting_LoadFile = 0x09020000; -extern const PSRETURN MASK__PSRETURN_CVFSFile_AlreadyLoaded = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_CVFSFile_AlreadyLoaded = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_CVFSFile_AlreadyLoaded = 0x01000001; -extern const PSRETURN MASK__PSRETURN_CVFSFile_InvalidBufferAccess = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_CVFSFile_InvalidBufferAccess = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_CVFSFile_InvalidBufferAccess = 0x01000002; -extern const PSRETURN MASK__PSRETURN_CVFSFile_LoadFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_CVFSFile_LoadFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_CVFSFile_LoadFailed = 0x01000003; -extern const PSRETURN MASK__PSRETURN_DllLoader_DllNotLoaded = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_DllLoader_DllNotLoaded = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_DllLoader_DllNotLoaded = 0x02000001; -extern const PSRETURN MASK__PSRETURN_DllLoader_SymbolNotFound = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_DllLoader_SymbolNotFound = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_DllLoader_SymbolNotFound = 0x02000002; -extern const PSRETURN MASK__PSRETURN_Error_InvalidError = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_Error_InvalidError = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_Error_InvalidError = 0x03000001; -extern const PSRETURN MASK__PSRETURN_File_InvalidType = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_File_InvalidType = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_File_InvalidType = 0x04000001; -extern const PSRETURN MASK__PSRETURN_File_InvalidVersion = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_File_InvalidVersion = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_File_InvalidVersion = 0x04000002; -extern const PSRETURN MASK__PSRETURN_File_OpenFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_File_OpenFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_File_OpenFailed = 0x04000003; -extern const PSRETURN MASK__PSRETURN_File_ReadFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_File_ReadFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_File_ReadFailed = 0x04000004; -extern const PSRETURN MASK__PSRETURN_File_UnexpectedEOF = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_File_UnexpectedEOF = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_File_UnexpectedEOF = 0x04000005; -extern const PSRETURN MASK__PSRETURN_File_WriteFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_File_WriteFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_File_WriteFailed = 0x04000006; -extern const PSRETURN MASK__PSRETURN_GUI_JSOpenFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_GUI_JSOpenFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_GUI_JSOpenFailed = 0x05000001; -extern const PSRETURN MASK__PSRETURN_Game_World_MapLoadFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_Game_World_MapLoadFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_Game_World_MapLoadFailed = 0x06040001; -extern const PSRETURN MASK__PSRETURN_I18n_Script_SetupFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_I18n_Script_SetupFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_I18n_Script_SetupFailed = 0x07030001; -extern const PSRETURN MASK__PSRETURN_Renderer_VBOFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_Renderer_VBOFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_Renderer_VBOFailed = 0x08000001; -extern const PSRETURN MASK__PSRETURN_Scripting_DefineType_AlreadyExists = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_Scripting_DefineType_AlreadyExists = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_Scripting_DefineType_AlreadyExists = 0x09010001; -extern const PSRETURN MASK__PSRETURN_Scripting_DefineType_CreationFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_Scripting_DefineType_CreationFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_Scripting_DefineType_CreationFailed = 0x09010002; -extern const PSRETURN MASK__PSRETURN_Scripting_LoadFile_EvalErrors = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_Scripting_LoadFile_EvalErrors = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_Scripting_LoadFile_EvalErrors = 0x09020001; -extern const PSRETURN MASK__PSRETURN_Scripting_LoadFile_OpenFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_Scripting_LoadFile_OpenFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_Scripting_LoadFile_OpenFailed = 0x09020002; -extern const PSRETURN MASK__PSRETURN_Scripting_CallFunctionFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_Scripting_CallFunctionFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_Scripting_CallFunctionFailed = 0x09000001; -extern const PSRETURN MASK__PSRETURN_Scripting_ConversionFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_Scripting_ConversionFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_Scripting_ConversionFailed = 0x09000002; -extern const PSRETURN MASK__PSRETURN_Scripting_CreateObjectFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_Scripting_CreateObjectFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_Scripting_CreateObjectFailed = 0x09000003; -extern const PSRETURN MASK__PSRETURN_Scripting_DefineConstantFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_Scripting_DefineConstantFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_Scripting_DefineConstantFailed = 0x09000004; -extern const PSRETURN MASK__PSRETURN_Scripting_RegisterFunctionFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_Scripting_RegisterFunctionFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_Scripting_RegisterFunctionFailed = 0x09000005; -extern const PSRETURN MASK__PSRETURN_Scripting_SetupFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_Scripting_SetupFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_Scripting_SetupFailed = 0x09000006; -extern const PSRETURN MASK__PSRETURN_Scripting_TypeDoesNotExist = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_Scripting_TypeDoesNotExist = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_Scripting_TypeDoesNotExist = 0x09000007; -extern const PSRETURN MASK__PSRETURN_System_RequiredExtensionsMissing = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_System_RequiredExtensionsMissing = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_System_RequiredExtensionsMissing = 0x0a000001; -extern const PSRETURN MASK__PSRETURN_System_SDLInitFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_System_SDLInitFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_System_SDLInitFailed = 0x0a000002; -extern const PSRETURN MASK__PSRETURN_System_VmodeFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_System_VmodeFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_System_VmodeFailed = 0x0a000003; -extern const PSRETURN MASK__PSRETURN_Xeromyces_XMLOpenFailed = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_Xeromyces_XMLOpenFailed = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_Xeromyces_XMLOpenFailed = 0x0b000001; -extern const PSRETURN MASK__PSRETURN_Xeromyces_XMLParseError = 0xffffffff; +extern const PSRETURN MASK__PSRETURN_Xeromyces_XMLParseError = 0xFFFFFFFF; extern const PSRETURN CODE__PSRETURN_Xeromyces_XMLParseError = 0x0b000002; PSRETURN PSERROR_CVFSFile_AlreadyLoaded::getCode() const { return 0x01000001; } diff --git a/source/ps/Errors.h b/source/ps/Errors.h index 29d551c5c7..564a458c31 100644 --- a/source/ps/Errors.h +++ b/source/ps/Errors.h @@ -3,8 +3,6 @@ #include -#include "lib/types.h" - typedef u32 PSRETURN; class PSERROR : public std::exception @@ -31,7 +29,7 @@ public: #define ERROR_IS(a, b) ( ((a) & MASK__PSRETURN_##b) == CODE__PSRETURN_##b ) const PSRETURN PSRETURN_OK = 0; -const PSRETURN MASK__PSRETURN_OK = 0xffffffff; +const PSRETURN MASK__PSRETURN_OK = 0xFFFFFFFF; const PSRETURN CODE__PSRETURN_OK = 0; const char* GetErrorString(PSRETURN code); diff --git a/source/ps/FilePacker.h b/source/ps/FilePacker.h index 9d787d2fd8..78583eeb2a 100644 --- a/source/ps/FilePacker.h +++ b/source/ps/FilePacker.h @@ -10,7 +10,6 @@ #define _FILEPACKER_H #include -#include "lib/types.h" // u32 #include "CStr.h" #include "ps/Errors.h" diff --git a/source/ps/FileUnpacker.h b/source/ps/FileUnpacker.h index 84dc20698e..3f05bbd544 100644 --- a/source/ps/FileUnpacker.h +++ b/source/ps/FileUnpacker.h @@ -10,7 +10,6 @@ #define _FILEUNPACKER_H #include -#include "lib/types.h" #include "lib/res/file/file.h" #include "CStr.h" diff --git a/source/ps/Overlay.h b/source/ps/Overlay.h index 33607cc972..3afcc861c6 100644 --- a/source/ps/Overlay.h +++ b/source/ps/Overlay.h @@ -12,8 +12,6 @@ by Rich Cross, rich@0ad.wildfiregames.com #ifndef COVERLAY_H #define COVERLAY_H -#include "lib/types.h" - class CStr8; struct CColor diff --git a/source/ps/StringConvert.cpp b/source/ps/StringConvert.cpp index 2b9f20ac26..6d0b1753d8 100644 --- a/source/ps/StringConvert.cpp +++ b/source/ps/StringConvert.cpp @@ -1,7 +1,6 @@ #include "precompiled.h" #include "StringConvert.h" -#include "lib/types.h" #include "lib/sdl.h" #include "scripting/SpiderMonkey.h" diff --git a/source/ps/ThreadUtil.h b/source/ps/ThreadUtil.h index 1cad93c5a0..fd655e5c2f 100644 --- a/source/ps/ThreadUtil.h +++ b/source/ps/ThreadUtil.h @@ -67,7 +67,7 @@ CLocker usage 2: //-------------------------------------------------------- #include "lib/lib.h" -#include "lib/posix.h" +#include "lib/posix/posix_pthread.h" #ifdef DEBUG_LOCKS diff --git a/source/ps/Util.cpp b/source/ps/Util.cpp index 7abca7bc6a..abeaaa5edc 100644 --- a/source/ps/Util.cpp +++ b/source/ps/Util.cpp @@ -1,5 +1,7 @@ #include "precompiled.h" +#include "lib/posix/posix_utsname.h" +#include "lib/posix/posix_sock.h" #include "lib/res/file/path.h" #include "lib/res/file/vfs.h" #include "lib/ogl.h" diff --git a/source/ps/XML/XMLUtils.cpp b/source/ps/XML/XMLUtils.cpp index f95d131f59..0d5ed8d003 100644 --- a/source/ps/XML/XMLUtils.cpp +++ b/source/ps/XML/XMLUtils.cpp @@ -3,7 +3,7 @@ #include "XML.h" #include "ps/CStr.h" #include "ps/CLogger.h" -#include "lib/posix.h" // ptrdiff_t +#include "lib/posix/posix_types.h" // ptrdiff_t #include "lib/res/file/vfs.h" #include "lib/res/mem.h" diff --git a/source/ps/utf16string.h b/source/ps/utf16string.h index b21925e50a..3cb3448a0b 100644 --- a/source/ps/utf16string.h +++ b/source/ps/utf16string.h @@ -9,7 +9,6 @@ // We now use this code on Windows as well, because wchar_t is a // native type and distinct from utf16_t. #include -#include "lib/types.h" typedef uint16_t utf16_t; typedef std::basic_string utf16string; diff --git a/source/simulation/AStarEngine.h b/source/simulation/AStarEngine.h index fe25411d00..8214945188 100644 --- a/source/simulation/AStarEngine.h +++ b/source/simulation/AStarEngine.h @@ -3,7 +3,6 @@ #include "ps/Vector2D.h" #include "ps/Player.h" -#include "lib/types.h" #include class AStarNode diff --git a/source/simulation/EntityHandles.h b/source/simulation/EntityHandles.h index 33da272c56..aa7aaeffe4 100644 --- a/source/simulation/EntityHandles.h +++ b/source/simulation/EntityHandles.h @@ -20,8 +20,6 @@ #ifndef ENTITY_HANDLE_INCLUDED #define ENTITY_HANDLE_INCLUDED -#include "lib/types.h" - #define INVALID_HANDLE 65535 // The maximum numerical value of an entity handle sent over the network #define MAX_HANDLE 0x7fff diff --git a/source/simulation/Simulation.cpp b/source/simulation/Simulation.cpp index ab50b22d31..79e15555cf 100644 --- a/source/simulation/Simulation.cpp +++ b/source/simulation/Simulation.cpp @@ -366,7 +366,7 @@ uint CSimulation::GetMessageMask(CNetMessage* UNUSED(pMsg), uint UNUSED(oldMask) // Pending a complete visibility/minimal-update implementation, we'll // simply select the first 32 connected clients ;-) - return 0xffffffff; + return 0xFFFFFFFF; } void CSimulation::QueueLocalCommand(CNetMessage *pMsg) diff --git a/source/simulation/Simulation.h b/source/simulation/Simulation.h index 011778e058..21462b7a36 100644 --- a/source/simulation/Simulation.h +++ b/source/simulation/Simulation.h @@ -1,7 +1,6 @@ #ifndef _Simulation_H #define _Simulation_H -#include "lib/types.h" #include class CGame; diff --git a/source/simulation/TurnManager.h b/source/simulation/TurnManager.h index 9531aa407f..4422526b64 100644 --- a/source/simulation/TurnManager.h +++ b/source/simulation/TurnManager.h @@ -5,8 +5,6 @@ //#include "Network/Network.h" #include -#include "lib/types.h" - class CGameRecord; class CNetMessage; class IMessagePipeEnd; diff --git a/source/tools/atlas/GameInterface/Shareable.h b/source/tools/atlas/GameInterface/Shareable.h index c6f1277f83..f9bdcf436e 100644 --- a/source/tools/atlas/GameInterface/Shareable.h +++ b/source/tools/atlas/GameInterface/Shareable.h @@ -52,7 +52,7 @@ after their definition. # ifdef __WXWINDOWS__ # undef new # else -# include "nommgr.h" +# include "lib/nommgr.h" # endif #endif