1/*
2 * Copyright (c) 2014 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24/*
25    CFWindowsUtilities.c
26    Copyright (c) 2008-2013, Apple Inc. All rights reserved.
27    Responsibility: Tony Parker
28*/
29
30#if DEPLOYMENT_TARGET_WINDOWS
31
32#include <CoreFoundation/CFArray.h>
33#include <CoreFoundation/CFString.h>
34#include "CFInternal.h"
35#include "CFPriv.h"
36
37#include <shlobj.h>
38
39#include <sys/stat.h>
40
41CF_EXPORT bool OSAtomicCompareAndSwapPtr(void *oldp, void *newp, void *volatile *dst)
42{
43    return oldp == InterlockedCompareExchangePointer(dst, newp, oldp);
44}
45
46CF_EXPORT bool OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst)
47{
48    return oldl == InterlockedCompareExchange(dst, newl, oldl);
49}
50
51CF_EXPORT bool OSAtomicCompareAndSwapPtrBarrier(void *oldp, void *newp, void *volatile *dst)
52{
53    return oldp == InterlockedCompareExchangePointer(dst, newp, oldp);
54}
55
56CF_EXPORT int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst)
57{
58    return InterlockedDecrement((volatile long *)dst);
59}
60
61CF_EXPORT int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst)
62{
63    return InterlockedIncrement((volatile long *)dst);
64}
65
66CF_EXPORT int32_t OSAtomicAdd32Barrier( int32_t theAmount, volatile int32_t *theValue ) {
67    return (InterlockedExchangeAdd((volatile LONG *)theValue, theAmount) + theAmount);
68}
69
70CF_EXPORT bool OSAtomicCompareAndSwap32Barrier(int32_t oldValue, int32_t newValue, volatile int32_t *theValue) {
71    return oldValue == InterlockedCompareExchange((long *)theValue, newValue, oldValue);
72}
73
74CF_EXPORT int32_t OSAtomicAdd32( int32_t theAmount, volatile int32_t *theValue ) {
75    return (InterlockedExchangeAdd((volatile LONG *)theValue, theAmount) + theAmount);
76}
77
78CF_EXPORT int32_t OSAtomicIncrement32(volatile int32_t *theValue) {
79    return InterlockedIncrement((volatile long *)theValue);
80}
81
82CF_EXPORT int32_t OSAtomicDecrement32(volatile int32_t *theValue) {
83    return InterlockedDecrement((volatile long *)theValue);
84}
85
86// These 64-bit versions of InterlockedCompareExchange are only available on client Vista and later, so we can't use them (yet).
87/*
88CF_EXPORT bool OSAtomicCompareAndSwap64( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue ) {
89    return __oldValue == InterlockedCompareExchange64((volatile LONGLONG *)__theValue, __newValue, __oldValue);
90}
91
92CF_EXPORT bool OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue ) {
93    return __oldValue == InterlockedCompareExchange64((volatile LONGLONG *)__theValue, __newValue, __oldValue);
94}
95
96CF_EXPORT int64_t OSAtomicAdd64( int64_t __theAmount, volatile int64_t *__theValue ) {
97    return (InterlockedExchangeAdd64((volatile LONGLONG *)__theValue, __theAmount) + __theAmount);
98}
99
100CF_EXPORT int64_t OSAtomicAdd64Barrier( int64_t __theAmount, volatile int64_t *__theValue ) {
101    retun (InterlockedExchangeAdd64((volatile LONGLONG *)__theValue, __theAmount) + __theAmount);
102}
103 */
104
105void OSMemoryBarrier() {
106    MemoryBarrier();
107}
108
109void _CFGetFrameworkPath(wchar_t *path, int maxLength) {
110#ifdef _DEBUG
111    // might be nice to get this from the project file at some point
112    wchar_t *DLLFileName = L"CoreFoundation_debug.dll";
113#else
114    wchar_t *DLLFileName = L"CoreFoundation.dll";
115#endif
116    path[0] = path[1] = 0;
117    DWORD wResult;
118    CFIndex idx;
119    HMODULE ourModule = GetModuleHandleW(DLLFileName);
120
121    CFAssert(ourModule, __kCFLogAssertion, "GetModuleHandle failed");
122
123    wResult = GetModuleFileNameW(ourModule, path, maxLength);
124    CFAssert1(wResult > 0, __kCFLogAssertion, "GetModuleFileName failed: %d", GetLastError());
125    CFAssert1(wResult < maxLength, __kCFLogAssertion, "GetModuleFileName result truncated: %s", path);
126
127    // strip off last component, the DLL name
128    for (idx = wResult - 1; idx; idx--) {
129        if ('\\' == path[idx]) {
130            path[idx] = '\0';
131            break;
132        }
133    }
134}
135
136
137#endif
138
139