forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCFWindowsUtilities.c
120 lines (93 loc) · 4.02 KB
/
CFWindowsUtilities.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
CFWindowsUtilities.c
Copyright (c) 2008-2018, Apple Inc. and the Swift project authors
Portions Copyright (c) 2014-2018, Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
Responsibility: Tony Parker
*/
#if TARGET_OS_WIN32
#include <CoreFoundation/CFArray.h>
#include <CoreFoundation/CFString.h>
#include "CFInternal.h"
#include "CFPriv.h"
#include <shlobj.h>
#include <sys/stat.h>
CF_EXPORT bool OSAtomicCompareAndSwapPtr(void *oldp, void *newp, void *volatile *dst)
{
return oldp == InterlockedCompareExchangePointer(dst, newp, oldp);
}
CF_EXPORT bool OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst)
{
return oldl == InterlockedCompareExchange(dst, newl, oldl);
}
CF_EXPORT bool OSAtomicCompareAndSwapPtrBarrier(void *oldp, void *newp, void *volatile *dst)
{
return oldp == InterlockedCompareExchangePointer(dst, newp, oldp);
}
CF_EXPORT int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst)
{
return InterlockedDecrement((volatile long *)dst);
}
CF_EXPORT int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst)
{
return InterlockedIncrement((volatile long *)dst);
}
CF_EXPORT int32_t OSAtomicAdd32Barrier( int32_t theAmount, volatile int32_t *theValue ) {
return (InterlockedExchangeAdd((volatile LONG *)theValue, theAmount) + theAmount);
}
CF_EXPORT bool OSAtomicCompareAndSwap32Barrier(int32_t oldValue, int32_t newValue, volatile int32_t *theValue) {
return oldValue == InterlockedCompareExchange((long *)theValue, newValue, oldValue);
}
CF_EXPORT int32_t OSAtomicAdd32( int32_t theAmount, volatile int32_t *theValue ) {
return (InterlockedExchangeAdd((volatile LONG *)theValue, theAmount) + theAmount);
}
CF_EXPORT int32_t OSAtomicIncrement32(volatile int32_t *theValue) {
return InterlockedIncrement((volatile long *)theValue);
}
CF_EXPORT int32_t OSAtomicDecrement32(volatile int32_t *theValue) {
return InterlockedDecrement((volatile long *)theValue);
}
// These 64-bit versions of InterlockedCompareExchange are only available on client Vista and later, so we can't use them (yet).
/*
CF_EXPORT bool OSAtomicCompareAndSwap64( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue ) {
return __oldValue == InterlockedCompareExchange64((volatile LONGLONG *)__theValue, __newValue, __oldValue);
}
CF_EXPORT bool OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue ) {
return __oldValue == InterlockedCompareExchange64((volatile LONGLONG *)__theValue, __newValue, __oldValue);
}
CF_EXPORT int64_t OSAtomicAdd64( int64_t __theAmount, volatile int64_t *__theValue ) {
return (InterlockedExchangeAdd64((volatile LONGLONG *)__theValue, __theAmount) + __theAmount);
}
CF_EXPORT int64_t OSAtomicAdd64Barrier( int64_t __theAmount, volatile int64_t *__theValue ) {
retun (InterlockedExchangeAdd64((volatile LONGLONG *)__theValue, __theAmount) + __theAmount);
}
*/
void OSMemoryBarrier() {
MemoryBarrier();
}
void _CFGetFrameworkPath(wchar_t *path, int maxLength) {
#ifdef _DEBUG
// might be nice to get this from the project file at some point
wchar_t *DLLFileName = L"CoreFoundation_debug.dll";
#else
wchar_t *DLLFileName = L"CoreFoundation.dll";
#endif
path[0] = path[1] = 0;
DWORD wResult;
CFIndex idx;
HMODULE ourModule = GetModuleHandleW(DLLFileName);
CFAssert(ourModule, __kCFLogAssertion, "GetModuleHandle failed");
wResult = GetModuleFileNameW(ourModule, path, maxLength);
CFAssert1(wResult > 0, __kCFLogAssertion, "GetModuleFileName failed: %d", GetLastError());
CFAssert1(wResult < maxLength, __kCFLogAssertion, "GetModuleFileName result truncated: %s", path);
// strip off last component, the DLL name
for (idx = wResult - 1; idx; idx--) {
if ('\\' == path[idx]) {
path[idx] = '\0';
break;
}
}
}
#endif