Skip to content

Commit 148a7e2

Browse files
committed
[stdlib] make __SharedStringStorage able to own a pointer
1 parent 566fbf4 commit 148a7e2

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

stdlib/public/core/StringStorage.swift

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -681,6 +681,8 @@ final internal class __SharedStringStorage
681681

682682
internal var _breadcrumbs: _StringBreadcrumbs? = nil
683683

684+
internal var immortal = false
685+
684686
internal var count: Int { _countAndFlags.count }
685687

686688
internal init(
@@ -689,6 +691,7 @@ final internal class __SharedStringStorage
689691
) {
690692
self._owner = nil
691693
self.start = ptr
694+
self.immortal = true
692695
#if _pointerBitWidth(_64)
693696
self._countAndFlags = countAndFlags
694697
#elseif _pointerBitWidth(_32)
@@ -709,6 +712,32 @@ final internal class __SharedStringStorage
709712
return String(_StringGuts(self))
710713
}
711714
}
715+
716+
internal init(
717+
_mortal ptr: UnsafePointer<UInt8>,
718+
countAndFlags: _StringObject.CountAndFlags
719+
) {
720+
// ptr *must* be the start of an allocation
721+
self._owner = nil
722+
self.start = ptr
723+
self.immortal = false
724+
#if _pointerBitWidth(_64)
725+
self._countAndFlags = countAndFlags
726+
#elseif _pointerBitWidth(_32)
727+
self._count = countAndFlags.count
728+
self._countFlags = countAndFlags.flags
729+
#else
730+
#error("Unknown platform")
731+
#endif
732+
super.init()
733+
self._invariantCheck()
734+
}
735+
736+
deinit {
737+
if (_owner == nil) && !immortal {
738+
start.deallocate()
739+
}
740+
}
712741
}
713742

714743
extension __SharedStringStorage {

0 commit comments

Comments
 (0)