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