From 2d24c4e3705baece64c38fdc94403721b614207c Mon Sep 17 00:00:00 2001 From: Chris Pikul Date: Tue, 9 Jan 2024 15:22:49 -0800 Subject: [PATCH] added platform for MSVC Windows DLL --- msvc/.gitignore | 4 + msvc/pg_uuidv7/pg_uuidv7.sln | 31 ++++ msvc/pg_uuidv7/pg_uuidv7.vcxproj | 179 +++++++++++++++++++++++ msvc/pg_uuidv7/pg_uuidv7.vcxproj.filters | 22 +++ pg_uuidv7.c | 84 +++++++++++ 5 files changed, 320 insertions(+) create mode 100644 msvc/.gitignore create mode 100644 msvc/pg_uuidv7/pg_uuidv7.sln create mode 100644 msvc/pg_uuidv7/pg_uuidv7.vcxproj create mode 100644 msvc/pg_uuidv7/pg_uuidv7.vcxproj.filters diff --git a/msvc/.gitignore b/msvc/.gitignore new file mode 100644 index 0000000..ceefef0 --- /dev/null +++ b/msvc/.gitignore @@ -0,0 +1,4 @@ +# Ignore the MSVC stuff we don't need like build artifacts +pg_uuidv7/x64 +*.user +.vs diff --git a/msvc/pg_uuidv7/pg_uuidv7.sln b/msvc/pg_uuidv7/pg_uuidv7.sln new file mode 100644 index 0000000..1e863f6 --- /dev/null +++ b/msvc/pg_uuidv7/pg_uuidv7.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34031.279 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pg_uuidv7", "pg_uuidv7.vcxproj", "{AA8B1FD0-55F8-4E62-B9B0-7E9C53977578}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AA8B1FD0-55F8-4E62-B9B0-7E9C53977578}.Debug|x64.ActiveCfg = Debug|x64 + {AA8B1FD0-55F8-4E62-B9B0-7E9C53977578}.Debug|x64.Build.0 = Debug|x64 + {AA8B1FD0-55F8-4E62-B9B0-7E9C53977578}.Debug|x86.ActiveCfg = Debug|Win32 + {AA8B1FD0-55F8-4E62-B9B0-7E9C53977578}.Debug|x86.Build.0 = Debug|Win32 + {AA8B1FD0-55F8-4E62-B9B0-7E9C53977578}.Release|x64.ActiveCfg = Release|x64 + {AA8B1FD0-55F8-4E62-B9B0-7E9C53977578}.Release|x64.Build.0 = Release|x64 + {AA8B1FD0-55F8-4E62-B9B0-7E9C53977578}.Release|x86.ActiveCfg = Release|Win32 + {AA8B1FD0-55F8-4E62-B9B0-7E9C53977578}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A7BEE67A-8DE9-4D39-A75B-D0F3733A3EAC} + EndGlobalSection +EndGlobal diff --git a/msvc/pg_uuidv7/pg_uuidv7.vcxproj b/msvc/pg_uuidv7/pg_uuidv7.vcxproj new file mode 100644 index 0000000..993ae9f --- /dev/null +++ b/msvc/pg_uuidv7/pg_uuidv7.vcxproj @@ -0,0 +1,179 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {aa8b1fd0-55f8-4e62-b9b0-7e9c53977578} + pguuidv7 + 10.0 + + + + DynamicLibrary + true + v143 + Unicode + + + DynamicLibrary + false + v143 + true + Unicode + + + DynamicLibrary + true + v143 + Unicode + + + DynamicLibrary + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + false + + + false + + + false + + + false + + + + Level3 + true + WIN32;%(PreprocessorDefinitions) + true + false + CompileAsC + C:\Program Files\PostgreSQL\16\include\server\port\win32_msvc;C:\Program Files\PostgreSQL\16\include\server\port\win32;C:\Program Files\PostgreSQL\16\include\server;C:\Program Files\PostgreSQL\16\include;%(AdditionalIncludeDirectories) + + + Console + true + C:\Program Files\PostgreSQL\16\lib;%(AdditionalLibraryDirectories) + postgres.lib;%(AdditionalDependencies) + + + false + + + + + Level3 + true + true + true + WIN32;%(PreprocessorDefinitions) + true + false + CompileAsC + C:\Program Files\PostgreSQL\16\include\server\port\win32_msvc;C:\Program Files\PostgreSQL\16\include\server\port\win32;C:\Program Files\PostgreSQL\16\include\server;C:\Program Files\PostgreSQL\16\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + C:\Program Files\PostgreSQL\16\lib;%(AdditionalLibraryDirectories) + postgres.lib;%(AdditionalDependencies) + + + false + + + + + Level3 + true + WIN32;%(PreprocessorDefinitions) + true + false + CompileAsC + C:\Program Files\PostgreSQL\16\include\server\port\win32_msvc;C:\Program Files\PostgreSQL\16\include\server\port\win32;C:\Program Files\PostgreSQL\16\include\server;C:\Program Files\PostgreSQL\16\include;%(AdditionalIncludeDirectories) + + + Console + true + C:\Program Files\PostgreSQL\16\lib;%(AdditionalLibraryDirectories) + postgres.lib;%(AdditionalDependencies) + + + false + + + + + Level3 + true + true + true + WIN32;%(PreprocessorDefinitions) + true + false + CompileAsC + C:\Program Files\PostgreSQL\16\include\server\port\win32_msvc;C:\Program Files\PostgreSQL\16\include\server\port\win32;C:\Program Files\PostgreSQL\16\include\server;C:\Program Files\PostgreSQL\16\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + C:\Program Files\PostgreSQL\16\lib;%(AdditionalLibraryDirectories) + postgres.lib;%(AdditionalDependencies) + + + false + + + + + + + + + \ No newline at end of file diff --git a/msvc/pg_uuidv7/pg_uuidv7.vcxproj.filters b/msvc/pg_uuidv7/pg_uuidv7.vcxproj.filters new file mode 100644 index 0000000..1b977d3 --- /dev/null +++ b/msvc/pg_uuidv7/pg_uuidv7.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/pg_uuidv7.c b/pg_uuidv7.c index 0fd93e7..8dbd4a9 100644 --- a/pg_uuidv7.c +++ b/pg_uuidv7.c @@ -12,8 +12,82 @@ */ #define EPOCH_DIFF_USECS ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * USECS_PER_DAY) +/* + * Monkey-patch the clock_gettime function for windows + * + * Thanks to https://stackoverflow.com/questions/5404277/porting-clock-gettime-to-windows + */ +#ifdef WIN32 +LARGE_INTEGER +getFILETIMEoffset() +{ + SYSTEMTIME s; + FILETIME f; + LARGE_INTEGER t; + + s.wYear = 1970; + s.wMonth = 1; + s.wDay = 1; + s.wHour = 0; + s.wMinute = 0; + s.wSecond = 0; + s.wMilliseconds = 0; + SystemTimeToFileTime(&s, &f); + t.QuadPart = f.dwHighDateTime; + t.QuadPart <<= 32; + t.QuadPart |= f.dwLowDateTime; + return (t); +} + +int +clock_gettime(int X, struct timeval* tv) +{ + LARGE_INTEGER t; + FILETIME f; + double microseconds; + static LARGE_INTEGER offset; + static double frequencyToMicroseconds; + static int initialized = 0; + static BOOL usePerformanceCounter = 0; + + if (!initialized) { + LARGE_INTEGER performanceFrequency; + initialized = 1; + usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency); + if (usePerformanceCounter) { + QueryPerformanceCounter(&offset); + frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.; + } + else { + offset = getFILETIMEoffset(); + frequencyToMicroseconds = 10.; + } + } + if (usePerformanceCounter) QueryPerformanceCounter(&t); + else { + GetSystemTimeAsFileTime(&f); + t.QuadPart = f.dwHighDateTime; + t.QuadPart <<= 32; + t.QuadPart |= f.dwLowDateTime; + } + + t.QuadPart -= offset.QuadPart; + microseconds = (double)t.QuadPart / frequencyToMicroseconds; + t.QuadPart = microseconds; + tv->tv_sec = t.QuadPart / 1000000; + tv->tv_usec = t.QuadPart % 1000000; + return (0); +} +#define CLOCK_REALTIME 0 +#endif + PG_MODULE_MAGIC; +/* DLL Export if building with MSVC */ +#ifdef WIN32 +PGDLLEXPORT Datum uuid_generate_v7(PG_FUNCTION_ARGS); +#endif + PG_FUNCTION_INFO_V1(uuid_generate_v7); Datum uuid_generate_v7(PG_FUNCTION_ARGS) @@ -49,6 +123,11 @@ Datum uuid_generate_v7(PG_FUNCTION_ARGS) PG_RETURN_UUID_P(uuid); } +/* DLL Export if building with MSVC */ +#ifdef WIN32 +PGDLLEXPORT Datum uuid_v7_to_timestamptz(PG_FUNCTION_ARGS); +#endif + PG_FUNCTION_INFO_V1(uuid_v7_to_timestamptz); Datum uuid_v7_to_timestamptz(PG_FUNCTION_ARGS) @@ -63,6 +142,11 @@ Datum uuid_v7_to_timestamptz(PG_FUNCTION_ARGS) PG_RETURN_TIMESTAMPTZ(ts); } +/* DLL Export if building with MSVC */ +#ifdef WIN32 +PGDLLEXPORT Datum uuid_timestamptz_to_v7(PG_FUNCTION_ARGS); +#endif + PG_FUNCTION_INFO_V1(uuid_timestamptz_to_v7); Datum uuid_timestamptz_to_v7(PG_FUNCTION_ARGS)