mirror of
https://github.com/apple/swift-foundation.git
synced 2025-05-23 14:00:14 +08:00
62 lines
1.8 KiB
Swift
62 lines
1.8 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2022 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if canImport(Darwin)
|
|
import Darwin
|
|
|
|
fileprivate var _pageSize: Int {
|
|
Int(vm_page_size)
|
|
}
|
|
#elseif canImport(WinSDK)
|
|
import WinSDK
|
|
fileprivate var _pageSize: Int {
|
|
var sysInfo: SYSTEM_INFO = SYSTEM_INFO()
|
|
GetSystemInfo(&sysInfo)
|
|
return Int(sysInfo.dwPageSize)
|
|
}
|
|
#elseif os(WASI)
|
|
// WebAssembly defines a fixed page size
|
|
fileprivate let _pageSize: Int = 65_536
|
|
#elseif canImport(Glibc)
|
|
import Glibc
|
|
fileprivate let _pageSize: Int = Int(getpagesize())
|
|
#elseif canImport(C)
|
|
fileprivate let _pageSize: Int = Int(getpagesize())
|
|
#endif // canImport(Darwin)
|
|
|
|
internal struct Platform {
|
|
static var pageSize: Int {
|
|
_pageSize
|
|
}
|
|
|
|
static func roundDownToMultipleOfPageSize(_ size: Int) -> Int {
|
|
return size & ~(self.pageSize - 1)
|
|
}
|
|
|
|
static func roundUpToMultipleOfPageSize(_ size: Int) -> Int {
|
|
return (self.pageSize + size - 1) & ~(self.pageSize - 1)
|
|
}
|
|
|
|
static func copyMemoryPages(_ source: UnsafeRawPointer, _ dest: UnsafeMutableRawPointer, _ length: Int) {
|
|
#if canImport(Darwin)
|
|
if vm_copy(
|
|
mach_task_self_,
|
|
vm_address_t(UInt(bitPattern: source)),
|
|
vm_size_t(length),
|
|
vm_address_t(UInt(bitPattern: dest))) != KERN_SUCCESS {
|
|
memmove(dest, source, length)
|
|
}
|
|
#else
|
|
memmove(dest, source, length)
|
|
#endif // canImport(Darwin)
|
|
}
|
|
}
|