Skip to content

Commit b23dc8e

Browse files
authored
Merge pull request #1796 from compnerd/simple-windows
Simple portions of the Windows Port
2 parents f51bd9f + b145058 commit b23dc8e

28 files changed

+298
-154
lines changed

CoreFoundation/AppServices.subproj/CFUserNotification.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
#include "CFRuntime_Internal.h"
1818
#include <CoreFoundation/CFMachPort.h>
1919
#include <stdlib.h>
20-
#include <unistd.h>
2120
#include <stdio.h>
2221
#include <mach/mach.h>
2322
#include <mach/error.h>
2423
#include <limits.h>
2524
#include <errno.h>
25+
26+
#if __has_include(<unistd.h>)
27+
#include <unistd.h>
28+
#endif
29+
#if _POSIX_THREADS
2630
#include <pthread.h>
31+
#endif
2732

2833
#define CFUserNotificationLog(alertHeader, alertMessage) CFLog(3, CFSTR("%@: %@"), alertHeader, alertMessage);
2934

CoreFoundation/Base.subproj/CFAsmMacros.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
/* Use .note.GNU-stack to explicitly indicate a non-exec stack, b/c of bad */
1818
/* default behaviour when translating handwritten assembly files (needed on */
19-
/* GNU/* platforms, Android and FreeBSD, as tests have shown). */
19+
/* GNU platforms, Android and FreeBSD, as tests have shown). */
2020
/* Platform tests are documented at http://git.savannah.gnu.org/gitweb/?p=libffcall.git;a=blob;f=porting-tools/execstack/README */
2121
#if (defined(__GNUC__) || defined (__llvm__) || defined (__clang__)) && defined(__ELF__) && (defined (__linux__) || defined (__linux) || defined (__gnu_linux__) || defined(__FreeBSD__) || defined (__FreeBSD_kernel__))
2222
#define NO_EXEC_STACK_DIRECTIVE .section .note.GNU-stack,"",%progbits

CoreFoundation/Base.subproj/CFBase.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
#include <CoreFoundation/CFBase.h>
1212
#include "CFInternal.h"
1313
#include "CFRuntime_Internal.h"
14-
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
14+
#if __has_include(<unistd.h>)
15+
#include <unistd.h>
16+
#endif
17+
#if _POSIX_THREADS
1518
#include <pthread.h>
1619
#endif
1720
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
@@ -102,9 +105,6 @@ CF_INLINE CFAllocatorPreferredSizeCallBack __CFAllocatorGetPreferredSizeFunction
102105
}
103106

104107
static const void * const __MallocDefaultZoneInfoPlaceholder = NULL;
105-
#if !TARGET_OS_MAC
106-
#define malloc_default_zone() (NULL)
107-
#endif
108108

109109
#if TARGET_OS_MAC
110110

CoreFoundation/Base.subproj/CFFileUtilities.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,7 @@ CF_PRIVATE CFIndex _CFLengthAfterDeletingPathExtension(UniChar *unichars, CFInde
10151015
#define DT_DIR 4
10161016
#define DT_REG 8
10171017
#define DT_LNK 10
1018+
#define DT_UNKNOWN 0
10181019
#endif
10191020

10201021
// NOTE: on Windows the filename is UTF16-encoded, the fileNameLen is result of wcslen. This function automatically skips '.' and '..', and '._' files

CoreFoundation/Base.subproj/CFInternal.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,17 @@ CF_EXTERN_C_BEGIN
101101
#include <xlocale.h>
102102
#endif // !TARGET_OS_CYGWIN && !defined(__linux__)
103103

104-
#include <unistd.h>
105104
#include <sys/time.h>
106105
#include <signal.h>
107106
#include <stdio.h>
108107
#endif // TARGET_OS_MAC || TARGET_OS_LINUX || TARGET_OS_BSD
109108

109+
#if __has_include(<unistd.h>)
110+
#include <unistd.h>
111+
#endif
112+
#if _POSIX_THREADS
110113
#include <pthread.h>
114+
#endif
111115

112116
#if !DEPLOYMENT_RUNTIME_SWIFT && __has_include(<os/log.h>)
113117
#include <os/log.h>
@@ -406,6 +410,7 @@ CF_PRIVATE Boolean __CFProcessIsRestricted(void);
406410
#if DEPLOYMENT_TARGET_WINDOWS
407411
#define SAFE_STACK_BUFFER_DECL(Type, Name, Count, Max) Type *Name; BOOL __ ## Name ## WasMallocd = NO; if (sizeof(Type) * Count > Max) { Name = (Type *)malloc((Count) * sizeof(Type)); __ ## Name ## WasMallocd = YES; } else Name = (Count > 0) ? _alloca((Count) * sizeof(Type)) : NULL
408412
#define SAFE_STACK_BUFFER_USE(Type, Name, Count, Max) if (sizeof(Type) * Count > Max) { Name = (Type *)malloc((Count) * sizeof(Type)); __ ## Name ## WasMallocd = YES; } else Name = (Count > 0) ? _alloca((Count) * sizeof(Type)) : NULL
413+
#define SAFE_STACK_BUFFER_CLEANUP(Name) if (__ ## Name ## WasMallocd) free(Name)
409414
#else
410415
// Declare and allocate a stack buffer. Max is the max size (in bytes) before falling over to malloc.
411416
#define SAFE_STACK_BUFFER_DECL(Type, Name, Count, Max) Type *Name; BOOL __ ## Name ## WasMallocd = NO; if (sizeof(Type) * Count > Max) { Name = (Type *)malloc((Count) * sizeof(Type)); __ ## Name ## WasMallocd = YES; } else Name = (Count > 0) ? alloca((Count) * sizeof(Type)) : NULL
@@ -574,7 +579,7 @@ typedef CFLock_t OSSpinLock;
574579

575580
#if __has_include(<os/lock.h>)
576581
#include <os/lock.h>
577-
#else
582+
#elif _POSIX_THREADS
578583
#define OS_UNFAIR_LOCK_INIT PTHREAD_MUTEX_INITIALIZER
579584
typedef pthread_mutex_t os_unfair_lock;
580585
typedef pthread_mutex_t * os_unfair_lock_t;
@@ -682,6 +687,7 @@ CF_PRIVATE CFArrayRef _CFCreateCFArrayByTokenizingString(const char *values, cha
682687
#define DT_DIR 4
683688
#define DT_REG 8
684689
#define DT_LNK 10
690+
#define DT_UNKNOWN 0
685691
#endif
686692

687693
/*
@@ -880,12 +886,6 @@ CF_EXPORT void _NS_pthread_setname_np(const char *name);
880886
#define pthread_main_np _CFIsMainThread
881887
#endif
882888

883-
#if DEPLOYMENT_TARGET_WINDOWS
884-
// replacement for DISPATCH_QUEUE_OVERCOMMIT until we get a bug fix in dispatch on Windows
885-
// <rdar://problem/7923891> dispatch on Windows: Need queue_private.h
886-
#define DISPATCH_QUEUE_OVERCOMMIT 2
887-
#endif
888-
889889
#if DEPLOYMENT_TARGET_WINDOWS
890890
CF_PRIVATE const wchar_t *_CFDLLPath(void);
891891
#endif
@@ -937,7 +937,7 @@ enum {
937937
};
938938
#endif
939939

940-
#if DEPLOYMENT_TARGET_LINUX
940+
#if DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_WINDOWS
941941
#define QOS_CLASS_USER_INITIATED DISPATCH_QUEUE_PRIORITY_HIGH
942942
#define QOS_CLASS_DEFAULT DISPATCH_QUEUE_PRIORITY_DEFAULT
943943
#define QOS_CLASS_UTILITY DISPATCH_QUEUE_PRIORITY_LOW

CoreFoundation/Base.subproj/CFKnownLocations.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ CFURLRef _Nullable _CFKnownLocationCreatePreferencesURLForUser(CFKnownLocationUs
7272
}
7373
}
7474

75+
#elif DEPLOYMENT_TARGET_WINDOWS
76+
77+
switch (user) {
78+
case _kCFKnownLocationUserAny:
79+
location = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, CFSTR("\\Users\\All Users\\AppData\\Local"), kCFURLWindowsPathStyle, true);
80+
break;
81+
case _kCFKnownLocationUserCurrent:
82+
username = CFGetUserName();
83+
// fallthrough
84+
case _kCFKnownLocationUserByName:
85+
const char *user = CFStringGetCStringPtr(username, kCFStringEncodingUTF8);
86+
CFURLRef userdir = CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault, (const unsigned char *)user, strlen(user), true);
87+
CFURLRef homedir = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorSystemDefault, CFSTR("\\Users"), kCFURLWindowsPathStyle, true, userdir);
88+
location = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorSystemDefault, CFSTR("\\AppData\\Local"), kCFURLWindowsPathStyle, true, homedir);
89+
CFRelease(homedir);
90+
CFRelease(userdir);
91+
break;
92+
}
93+
7594
#else
7695

7796
#error For this platform, you need to define a preferences path for both 'any user' (i.e. installation-wide preferences) or the current user.

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
#include <shellapi.h>
2727
#include <shlobj.h>
2828
#include <WinIoCtl.h>
29+
#include <direct.h>
30+
#define SECURITY_WIN32
31+
#include <Security.h>
2932

3033
#define getcwd _NS_getcwd
3134

@@ -302,6 +305,14 @@ CF_CROSS_PLATFORM_EXPORT CFStringRef CFCopyFullUserName(void) {
302305
if (upwd && upwd->pw_gecos) {
303306
result = CFStringCreateWithCString(kCFAllocatorSystemDefault, upwd->pw_gecos, kCFPlatformInterfaceStringEncoding);
304307
}
308+
#elif TARGET_OS_WIN32
309+
ULONG ulLength = 0;
310+
GetUserNameExW(NameDisplay, NULL, &ulLength);
311+
312+
WCHAR *wszBuffer[ulLength + 1];
313+
GetUserNameExW(NameDisplay, (LPWSTR)wszBuffer, &ulLength);
314+
315+
result = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, (UniChar *)wszBuffer, ulLength);
305316
#else
306317
#error "Please add an implementation for CFCopyFullUserName() that copies the full (display) user name"
307318
#endif
@@ -589,12 +600,12 @@ static DWORD __CFTSDIndexKey = 0xFFFFFFFF;
589600

590601
// Called from CFRuntime's startup code, on Windows only
591602
CF_PRIVATE void __CFTSDWindowsInitialize() {
592-
__CFTSDIndexKey = TlsAlloc();
603+
__CFTSDIndexKey = FlsAlloc(__CFTSDFinalize);
593604
}
594605

595606
// Called from CFRuntime's cleanup code, on Windows only
596607
CF_PRIVATE void __CFTSDWindowsCleanup() {
597-
TlsFree(__CFTSDIndexKey);
608+
FlsFree(__CFTSDIndexKey);
598609
}
599610

600611
// Called for each thread as it exits, on Windows only
@@ -624,7 +635,7 @@ static void __CFTSDSetSpecific(void *arg) {
624635
#elif DEPLOYMENT_TARGET_LINUX
625636
pthread_setspecific(__CFTSDIndexKey, arg);
626637
#elif DEPLOYMENT_TARGET_WINDOWS
627-
TlsSetValue(__CFTSDIndexKey, arg);
638+
FlsSetValue(__CFTSDIndexKey, arg);
628639
#endif
629640
}
630641

@@ -634,7 +645,7 @@ static void *__CFTSDGetSpecific() {
634645
#elif DEPLOYMENT_TARGET_LINUX
635646
return pthread_getspecific(__CFTSDIndexKey);
636647
#elif DEPLOYMENT_TARGET_WINDOWS
637-
return TlsGetValue(__CFTSDIndexKey);
648+
return FlsGetValue(__CFTSDIndexKey);
638649
#endif
639650
}
640651

@@ -1325,21 +1336,36 @@ static void _CFThreadSpecificDestructor(void *ctx) {
13251336

13261337
_CFThreadSpecificKey _CFThreadSpecificKeyCreate() {
13271338
_CFThreadSpecificKey key;
1339+
#if DEPLOYMENT_TARGET_WINDOWS
1340+
key = FlsAlloc(_CFThreadSpecificDestructor);
1341+
#else
13281342
pthread_key_create(&key, &_CFThreadSpecificDestructor);
1343+
#endif
13291344
return key;
13301345
}
13311346

13321347
CFTypeRef _Nullable _CFThreadSpecificGet(_CFThreadSpecificKey key) {
1348+
#if DEPLOYMENT_TARGET_WINDOWS
1349+
return (CFTypeRef)FlsGetValue(key);
1350+
#else
13331351
return (CFTypeRef)pthread_getspecific(key);
1352+
#endif
13341353
}
13351354

13361355
void _CFThreadSpecificSet(_CFThreadSpecificKey key, CFTypeRef _Nullable value) {
1356+
#if DEPLOYMENT_TARGET_WINDOWS
1357+
if (value != NULL) {
1358+
swift_retain((void *)value);
1359+
}
1360+
FlsSetValue(key, value);
1361+
#else
13371362
if (value != NULL) {
13381363
swift_retain((void *)value);
13391364
pthread_setspecific(key, value);
13401365
} else {
13411366
pthread_setspecific(key, NULL);
13421367
}
1368+
#endif
13431369
}
13441370

13451371
_CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (* _Nonnull startfn)(void *_Nullable), void *_CF_RESTRICT _Nullable context) {

CoreFoundation/Base.subproj/CFPriv.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -567,14 +567,11 @@ CF_EXPORT CFMessagePortRef _CFMessagePortCreateLocalEx(CFAllocatorRef allocator,
567567

568568
#endif
569569

570-
#if TARGET_OS_MAC || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE
571-
#include <pthread.h>
572-
#elif !TARGET_OS_LINUX
573-
// Avoid including the pthread header
574-
#ifndef HAVE_STRUCT_TIMESPEC
575-
#define HAVE_STRUCT_TIMESPEC 1
576-
struct timespec { long tv_sec; long tv_nsec; };
570+
#if __has_include(<unistd.h>)
571+
#include <unistd.h>
577572
#endif
573+
#if _POSIX_THREADS
574+
#include <pthread.h>
578575
#endif
579576

580577
CF_INLINE CFAbsoluteTime _CFAbsoluteTimeFromFileTimeSpec(struct timespec ts) {

CoreFoundation/Base.subproj/CFUtilities.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <string.h>
2929
#include <stdio.h>
3030
#include <stdlib.h>
31-
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS
31+
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
3232
#include <asl.h>
3333
#else
3434
#define ASL_LEVEL_EMERG 0
@@ -40,7 +40,6 @@
4040
#include <unistd.h>
4141
#include <sys/uio.h>
4242
#include <mach/mach.h>
43-
#include <pthread.h>
4443
#include <mach-o/loader.h>
4544
#include <mach-o/dyld.h>
4645
#include <crt_externs.h>
@@ -61,12 +60,15 @@
6160

6261
#if DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
6362
#include <string.h>
64-
#include <pthread.h>
6563
#include <sys/mman.h>
66-
#include <unistd.h>
6764
#endif
6865

69-
66+
#if __has_include(<unistd.h>)
67+
#include <unistd.h>
68+
#endif
69+
#if _POSIX_THREADS
70+
#include <pthread.h>
71+
#endif
7072

7173

7274

@@ -759,6 +761,16 @@ static bool also_do_stderr(const _cf_logging_style style) {
759761
return result;
760762
}
761763

764+
#if DEPLOYMENT_TARGET_WINDOWS
765+
static struct tm *localtime_r(time_t *tv, struct tm *result) {
766+
struct tm *tm = localtime(tv);
767+
if (tm) {
768+
*result = *tm;
769+
}
770+
return tm;
771+
}
772+
#endif
773+
762774
static void _populateBanner(char **banner, char **time, char **thread, int *bannerLen) {
763775
double dummy;
764776
CFAbsoluteTime at = CFAbsoluteTimeGetCurrent();
@@ -778,8 +790,8 @@ static void _populateBanner(char **banner, char **time, char **thread, int *bann
778790
asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d %s[%d:%llu] ", year, month, day, hour, minute, second, ms, *_CFGetProgname(), getpid(), tid);
779791
asprintf(thread, "%x", pthread_mach_thread_np(pthread_self()));
780792
#elif DEPLOYMENT_TARGET_WINDOWS
781-
bannerLen = asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d %s[%d:%x] ", year, month, day, hour, minute, second, ms, *_CFGetProgname(), getpid(), GetCurrentThreadId());
782-
asprintf(thread, "%x", GetCurrentThreadId());
793+
bannerLen = asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d %s[%d:%lx] ", year, month, day, hour, minute, second, ms, *_CFGetProgname(), getpid(), GetCurrentThreadId());
794+
asprintf(thread, "%lx", GetCurrentThreadId());
783795
#else
784796
bannerLen = asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d %s[%d:%x] ", year, month, day, hour, minute, second, ms, *_CFGetProgname(), getpid(), (unsigned int)pthread_self());
785797
asprintf(thread, "%lx", pthread_self());
@@ -802,6 +814,7 @@ static void _logToStderr(char *banner, const char *message, size_t length) {
802814
writev(STDERR_FILENO, v[0].iov_base ? v : v + 1, nv);
803815
__CFUnlock(&lock);
804816
#elif DEPLOYMENT_TARGET_WINDOWS
817+
size_t bannerLen = strlen(banner);
805818
size_t bufLen = bannerLen + length + 1;
806819
char *buf = (char *)malloc(sizeof(char) * bufLen);
807820
if (banner) {
@@ -869,7 +882,7 @@ static void __CFLogCStringLegacy(int32_t lev, const char *message, size_t length
869882
_populateBanner(&banner, &time, &thread, &bannerLen);
870883
}
871884

872-
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS
885+
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
873886
#pragma GCC diagnostic push
874887
#pragma GCC diagnostic ignored "-Wdeprecated"
875888
uid_t euid;
@@ -1202,7 +1215,7 @@ CF_PRIVATE Boolean _CFReadMappedFromFile(CFStringRef path, Boolean map, Boolean
12021215
}
12031216
#if __LP64__
12041217
#else
1205-
if (statBuf.st_size > (1LL << 31)) { // refuse to do more than 2GB
1218+
if (statBuf.st_size > (1ull << 31)) { // refuse to do more than 2GB
12061219
close(fd);
12071220
if (errorPtr) *errorPtr = _CFErrorWithFilePathCodeDomain(kCFErrorDomainPOSIX, EFBIG, path);
12081221
return false;
@@ -1524,6 +1537,8 @@ CFDictionaryRef __CFGetEnvironment() {
15241537
#define environ __environ
15251538
#endif
15261539
char **envp = environ;
1540+
#elif TARGET_OS_WIN32
1541+
char **envp = _environ;
15271542
#endif
15281543
envDict = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
15291544
for (; *envp; ++envp) {

0 commit comments

Comments
 (0)