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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
/*
This source file is part of the Swift System open source project
Copyright (c) 2020 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
*/
// Internal wrappers and typedefs which help reduce #if littering in System's
// code base.
// TODO: Should CSystem just include all the header files we need?
#if SYSTEM_PACKAGE_DARWIN
import Darwin
#elseif os(Windows)
import CSystem
import ucrt
#elseif canImport(Glibc)
@_implementationOnly import CSystem
import Glibc
#elseif canImport(Musl)
@_implementationOnly import CSystem
import Musl
#else
#error("Unsupported Platform")
#endif
internal typealias _COffT = off_t
// MARK: syscalls and variables
#if SYSTEM_PACKAGE_DARWIN
internal var system_errno: CInt {
get { Darwin.errno }
set { Darwin.errno = newValue }
}
#elseif os(Windows)
internal var system_errno: CInt {
get {
var value: CInt = 0
// TODO(compnerd) handle the error?
_ = ucrt._get_errno(&value)
return value
}
set {
_ = ucrt._set_errno(newValue)
}
}
#elseif canImport(Glibc)
internal var system_errno: CInt {
get { Glibc.errno }
set { Glibc.errno = newValue }
}
#elseif canImport(Musl)
internal var system_errno: CInt {
get { Musl.errno }
set { Musl.errno = newValue }
}
#endif
// MARK: C stdlib decls
// Convention: `system_foo` is system's wrapper for `foo`.
internal func system_strerror(_ __errnum: Int32) -> UnsafeMutablePointer<Int8>! {
strerror(__errnum)
}
internal func system_strlen(_ s: UnsafePointer<Int8>) -> Int {
strlen(s)
}
// Convention: `system_platform_foo` is a
// platform-representation-abstracted wrapper around `foo`-like functionality.
// Type and layout differences such as the `char` vs `wchar` are abstracted.
//
// strlen for the platform string
internal func system_platform_strlen(_ s: UnsafePointer<CInterop.PlatformChar>) -> Int {
#if os(Windows)
return wcslen(s)
#else
return strlen(s)
#endif
}
// Interop between String and platfrom string
extension String {
internal func _withPlatformString<Result>(
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
) rethrows -> Result {
// Need to #if because CChar may be signed
#if os(Windows)
return try withCString(encodedAs: CInterop.PlatformUnicodeEncoding.self, body)
#else
return try withCString(body)
#endif
}
internal init?(_platformString platformString: UnsafePointer<CInterop.PlatformChar>) {
// Need to #if because CChar may be signed
#if os(Windows)
guard let strRes = String.decodeCString(
platformString,
as: CInterop.PlatformUnicodeEncoding.self,
repairingInvalidCodeUnits: false
) else { return nil }
assert(strRes.repairsMade == false)
self = strRes.result
return
#else
self.init(validatingUTF8: platformString)
#endif
}
internal init(
_errorCorrectingPlatformString platformString: UnsafePointer<CInterop.PlatformChar>
) {
// Need to #if because CChar may be signed
#if os(Windows)
let strRes = String.decodeCString(
platformString,
as: CInterop.PlatformUnicodeEncoding.self,
repairingInvalidCodeUnits: true)
self = strRes!.result
return
#else
self.init(cString: platformString)
#endif
}
}
// TLS
#if os(Windows)
internal typealias _PlatformTLSKey = DWORD
#else
internal typealias _PlatformTLSKey = pthread_key_t
#endif
internal func makeTLSKey() -> _PlatformTLSKey {
#if os(Windows)
let raw: DWORD = FlsAlloc(nil)
if raw == FLS_OUT_OF_INDEXES {
fatalError("Unable to create key")
}
return raw
#else
var raw = pthread_key_t()
guard 0 == pthread_key_create(&raw, nil) else {
fatalError("Unable to create key")
}
return raw
#endif
}
internal func setTLS(_ key: _PlatformTLSKey, _ p: UnsafeMutableRawPointer?) {
#if os(Windows)
guard FlsSetValue(key, p) else {
fatalError("Unable to set TLS")
}
#else
guard 0 == pthread_setspecific(key, p) else {
fatalError("Unable to set TLS")
}
#endif
}
internal func getTLS(_ key: _PlatformTLSKey) -> UnsafeMutableRawPointer? {
#if os(Windows)
return FlsGetValue(key)
#else
return pthread_getspecific(key)
#endif
}
|