|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftShims |
| 14 | + |
| 15 | +public struct ClassMetadata { |
| 16 | + var superclassMetadata: UnsafeMutablePointer<ClassMetadata>? |
| 17 | + |
| 18 | + // There is no way to express the actual calling convention on the heap desroy |
| 19 | + // function (swiftcc with 'self') currently, so let's use UnsafeRawPointer |
| 20 | + // and a helper function in C (_swift_runtime_invoke_heap_object_destroy). |
| 21 | + var destroy: UnsafeRawPointer |
| 22 | +} |
| 23 | + |
| 24 | +public struct HeapObject { |
| 25 | + var metadata: UnsafeMutablePointer<ClassMetadata> |
| 26 | + var refcount: Int |
| 27 | +} |
| 28 | + |
| 29 | +@_silgen_name("posix_memalign") |
| 30 | +func posix_memalign(_: UnsafeMutablePointer<UnsafeMutableRawPointer?>, _: UInt, _: UInt) -> CInt |
| 31 | + |
| 32 | +func alignedAlloc(size: UInt, alignment: UInt) -> UnsafeMutableRawPointer? { |
| 33 | + let alignment = max(alignment, UInt(MemoryLayout<UnsafeRawPointer>.size)) |
| 34 | + var r: UnsafeMutableRawPointer? = nil |
| 35 | + _ = posix_memalign(&r, alignment, size) |
| 36 | + return r |
| 37 | +} |
| 38 | + |
| 39 | +@_cdecl("swift_slowAlloc") |
| 40 | +public func swift_slowAlloc(_ size: UInt, _ alignMask: UInt) -> UnsafeMutableRawPointer? { |
| 41 | + let alignment: UInt |
| 42 | + if alignMask == UInt.max { |
| 43 | + alignment = UInt(_swift_MinAllocationAlignment) |
| 44 | + } else { |
| 45 | + alignment = alignMask + 1 |
| 46 | + } |
| 47 | + return alignedAlloc(size: size, alignment: alignment) |
| 48 | +} |
| 49 | + |
| 50 | +@_cdecl("swift_slowDealloc") |
| 51 | +public func swift_slowDealloc(_ ptr: UnsafeMutableRawPointer?, _ size: UInt, _ alignMask: UInt) { |
| 52 | + free(ptr) |
| 53 | +} |
| 54 | + |
| 55 | +@_silgen_name("swift_allocObject") |
| 56 | +public func swift_allocObject(metadata: UnsafeMutablePointer<ClassMetadata>, requiredSize: UInt, requiredAlignmentMask: UInt) -> UnsafeMutablePointer<HeapObject> { |
| 57 | + let p = swift_slowAlloc(requiredSize, requiredAlignmentMask)! |
| 58 | + let object = p.assumingMemoryBound(to: HeapObject.self) |
| 59 | + object.pointee.metadata = metadata |
| 60 | + object.pointee.refcount = 1 |
| 61 | + return object |
| 62 | +} |
| 63 | + |
| 64 | +@_silgen_name("swift_deallocClassInstance") |
| 65 | +public func swift_deallocClassInstance(object: UnsafeMutablePointer<HeapObject>, allocatedSize: UInt, allocatedAlignMask: UInt) { |
| 66 | + free(object) |
| 67 | +} |
| 68 | + |
| 69 | +@_silgen_name("swift_initStackObject") |
| 70 | +public func swift_initStackObject(metadata: UnsafeMutablePointer<ClassMetadata>, object: UnsafeMutablePointer<HeapObject>) -> UnsafeMutablePointer<HeapObject> { |
| 71 | + object.pointee.metadata = metadata |
| 72 | + object.pointee.refcount = -1 |
| 73 | + return object |
| 74 | +} |
| 75 | + |
| 76 | +// TODO/FIXME: Refcounting and swift_once is not thread-safe, the following only works in single-threaded environments. |
| 77 | + |
| 78 | +@_silgen_name("swift_isUniquelyReferenced_nonNull_native") |
| 79 | +public func swift_isUniquelyReferenced_nonNull_native(object: UnsafeMutablePointer<HeapObject>) -> Bool { |
| 80 | + // TODO/FIXME: Refcounting is not thread-safe, the following only works in single-threaded environments. |
| 81 | + return object.pointee.refcount == 1 |
| 82 | +} |
| 83 | + |
| 84 | +@_silgen_name("swift_retain") |
| 85 | +public func swift_retain(object: Builtin.RawPointer) -> Builtin.RawPointer { |
| 86 | + if Int(Builtin.ptrtoint_Word(object)) == 0 { return object } |
| 87 | + let o = UnsafeMutablePointer<HeapObject>(object) |
| 88 | + // TODO/FIXME: Refcounting is not thread-safe, the following only works in single-threaded environments. |
| 89 | + if o.pointee.refcount == -1 { return o._rawValue } |
| 90 | + o.pointee.refcount += 1 |
| 91 | + return o._rawValue |
| 92 | +} |
| 93 | + |
| 94 | +@_silgen_name("swift_release") |
| 95 | +public func swift_release(object: Builtin.RawPointer) { |
| 96 | + if Int(Builtin.ptrtoint_Word(object)) == 0 { return } |
| 97 | + let o = UnsafeMutablePointer<HeapObject>(object) |
| 98 | + // TODO/FIXME: Refcounting is not thread-safe, the following only works in single-threaded environments. |
| 99 | + if o.pointee.refcount == -1 { return } |
| 100 | + o.pointee.refcount -= 1 |
| 101 | + if o.pointee.refcount == 0 { |
| 102 | + _swift_runtime_invoke_heap_object_destroy(o.pointee.metadata.pointee.destroy, o) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +@_silgen_name("swift_beginAccess") |
| 107 | +public func swift_beginAccess(pointer: UnsafeMutableRawPointer, buffer: UnsafeMutableRawPointer, flags: UInt, pc: UnsafeMutableRawPointer) { |
| 108 | +} |
| 109 | + |
| 110 | +@_silgen_name("swift_endAccess") |
| 111 | +public func swift_endAccess(buffer: UnsafeMutableRawPointer) { |
| 112 | +} |
| 113 | + |
| 114 | +@_silgen_name("swift_once") |
| 115 | +public func swift_once(predicate: UnsafeMutablePointer<Int>, fn: (@convention(c) (UnsafeMutableRawPointer)->()), context: UnsafeMutableRawPointer) { |
| 116 | + // TODO/FIXME: The following only works in single-threaded environments. |
| 117 | + if predicate.pointee == 0 { |
| 118 | + predicate.pointee = 1 |
| 119 | + fn(context) |
| 120 | + } |
| 121 | +} |
0 commit comments