|
| 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 | + |
| 27 | + // TODO: This is just an initial support for strong refcounting only. We need |
| 28 | + // to think about supporting (or banning) weak and/or unowned references. |
| 29 | + var refcount: Int |
| 30 | + |
| 31 | + static let immortalRefCount = -1 |
| 32 | +} |
| 33 | + |
| 34 | +func alignedAlloc(size: Int, alignment: Int) -> UnsafeMutableRawPointer? { |
| 35 | + let alignment = max(alignment, MemoryLayout<UnsafeRawPointer>.size) |
| 36 | + var r: UnsafeMutableRawPointer? = nil |
| 37 | + _ = posix_memalign(&r, alignment, size) |
| 38 | + return r |
| 39 | +} |
| 40 | + |
| 41 | +@_cdecl("swift_slowAlloc") |
| 42 | +public func swift_slowAlloc(_ size: Int, _ alignMask: Int) -> UnsafeMutableRawPointer? { |
| 43 | + let alignment: Int |
| 44 | + if alignMask == -1 { |
| 45 | + alignment = _swift_MinAllocationAlignment |
| 46 | + } else { |
| 47 | + alignment = alignMask + 1 |
| 48 | + } |
| 49 | + return alignedAlloc(size: size, alignment: alignment) |
| 50 | +} |
| 51 | + |
| 52 | +@_cdecl("swift_slowDealloc") |
| 53 | +public func swift_slowDealloc(_ ptr: UnsafeMutableRawPointer?, _ size: Int, _ alignMask: Int) { |
| 54 | + free(ptr) |
| 55 | +} |
| 56 | + |
| 57 | +@_silgen_name("swift_allocObject") |
| 58 | +public func swift_allocObject(metadata: UnsafeMutablePointer<ClassMetadata>, requiredSize: Int, requiredAlignmentMask: Int) -> UnsafeMutablePointer<HeapObject> { |
| 59 | + let p = swift_slowAlloc(requiredSize, requiredAlignmentMask)! |
| 60 | + let object = p.assumingMemoryBound(to: HeapObject.self) |
| 61 | + object.pointee.metadata = metadata |
| 62 | + object.pointee.refcount = 1 |
| 63 | + return object |
| 64 | +} |
| 65 | + |
| 66 | +@_silgen_name("swift_deallocClassInstance") |
| 67 | +public func swift_deallocClassInstance(object: UnsafeMutablePointer<HeapObject>, allocatedSize: Int, allocatedAlignMask: Int) { |
| 68 | + free(object) |
| 69 | +} |
| 70 | + |
| 71 | +@_silgen_name("swift_initStackObject") |
| 72 | +public func swift_initStackObject(metadata: UnsafeMutablePointer<ClassMetadata>, object: UnsafeMutablePointer<HeapObject>) -> UnsafeMutablePointer<HeapObject> { |
| 73 | + object.pointee.metadata = metadata |
| 74 | + |
| 75 | + // TODO/FIXME: Making all stack promoted objects immortal is not correct. |
| 76 | + object.pointee.refcount = HeapObject.immortalRefCount |
| 77 | + return object |
| 78 | +} |
| 79 | + |
| 80 | +// TODO/FIXME: Refcounting and swift_once is not thread-safe, the following only works in single-threaded environments. |
| 81 | + |
| 82 | +@_silgen_name("swift_isUniquelyReferenced_nonNull_native") |
| 83 | +public func swift_isUniquelyReferenced_nonNull_native(object: UnsafeMutablePointer<HeapObject>) -> Bool { |
| 84 | + // TODO/FIXME: Refcounting is not thread-safe, the following only works in single-threaded environments. |
| 85 | + return object.pointee.refcount == 1 |
| 86 | +} |
| 87 | + |
| 88 | +@_silgen_name("swift_retain") |
| 89 | +public func swift_retain(object: Builtin.RawPointer) -> Builtin.RawPointer { |
| 90 | + if Int(Builtin.ptrtoint_Word(object)) == 0 { return object } |
| 91 | + let o = UnsafeMutablePointer<HeapObject>(object) |
| 92 | + // TODO/FIXME: Refcounting is not thread-safe, the following only works in single-threaded environments. |
| 93 | + if o.pointee.refcount == HeapObject.immortalRefCount { return o._rawValue } |
| 94 | + o.pointee.refcount += 1 |
| 95 | + return o._rawValue |
| 96 | +} |
| 97 | + |
| 98 | +@_silgen_name("swift_release") |
| 99 | +public func swift_release(object: Builtin.RawPointer) { |
| 100 | + if Int(Builtin.ptrtoint_Word(object)) == 0 { return } |
| 101 | + let o = UnsafeMutablePointer<HeapObject>(object) |
| 102 | + // TODO/FIXME: Refcounting is not thread-safe, the following only works in single-threaded environments. |
| 103 | + if o.pointee.refcount == HeapObject.immortalRefCount { return } |
| 104 | + o.pointee.refcount -= 1 |
| 105 | + if o.pointee.refcount == 0 { |
| 106 | + _swift_runtime_invoke_heap_object_destroy(o.pointee.metadata.pointee.destroy, o) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +@_silgen_name("swift_beginAccess") |
| 111 | +public func swift_beginAccess(pointer: UnsafeMutableRawPointer, buffer: UnsafeMutableRawPointer, flags: UInt, pc: UnsafeMutableRawPointer) { |
| 112 | + // TODO: Add actual exclusivity checking. |
| 113 | +} |
| 114 | + |
| 115 | +@_silgen_name("swift_endAccess") |
| 116 | +public func swift_endAccess(buffer: UnsafeMutableRawPointer) { |
| 117 | + // TODO: Add actual exclusivity checking. |
| 118 | +} |
| 119 | + |
| 120 | +@_silgen_name("swift_once") |
| 121 | +public func swift_once(predicate: UnsafeMutablePointer<Int>, fn: (@convention(c) (UnsafeMutableRawPointer)->()), context: UnsafeMutableRawPointer) { |
| 122 | + // TODO/FIXME: The following only works in single-threaded environments. |
| 123 | + if predicate.pointee == 0 { |
| 124 | + predicate.pointee = 1 |
| 125 | + fn(context) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +@_silgen_name("swift_deletedMethodError") |
| 130 | +public func swift_deletedMethodError() -> Never { |
| 131 | + Builtin.int_trap() |
| 132 | +} |
0 commit comments