RawBuffer.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. This source file is part of the Swift System open source project
  3. Copyright (c) 2021 Apple Inc. and the Swift System project authors
  4. Licensed under Apache License v2.0 with Runtime Library Exception
  5. See https://swift.org/LICENSE.txt for license information
  6. */
  7. // A copy-on-write fixed-size buffer of raw memory.
  8. internal struct _RawBuffer {
  9. internal var _storage: Storage?
  10. internal init() {
  11. self._storage = nil
  12. }
  13. internal init(minimumCapacity: Int) {
  14. if minimumCapacity > 0 {
  15. self._storage = Storage.create(minimumCapacity: minimumCapacity)
  16. } else {
  17. self._storage = nil
  18. }
  19. }
  20. }
  21. extension _RawBuffer {
  22. internal var capacity: Int {
  23. _storage?.header ?? 0 // Note: not capacity!
  24. }
  25. internal mutating func ensureUnique() {
  26. guard _storage != nil else { return }
  27. let unique = isKnownUniquelyReferenced(&_storage)
  28. if !unique {
  29. _storage = _copy(capacity: capacity)
  30. }
  31. }
  32. internal func _grow(desired: Int) -> Int {
  33. let next = Int(1.75 * Double(self.capacity))
  34. return Swift.max(next, desired)
  35. }
  36. internal mutating func ensureUnique(capacity: Int) {
  37. let unique = isKnownUniquelyReferenced(&_storage)
  38. if !unique || self.capacity < capacity {
  39. _storage = _copy(capacity: _grow(desired: capacity))
  40. }
  41. }
  42. internal func withUnsafeBytes<R>(
  43. _ body: (UnsafeRawBufferPointer) throws -> R
  44. ) rethrows -> R {
  45. guard let storage = _storage else {
  46. return try body(UnsafeRawBufferPointer(start: nil, count: 0))
  47. }
  48. return try storage.withUnsafeMutablePointers { count, bytes in
  49. let buffer = UnsafeRawBufferPointer(start: bytes, count: count.pointee)
  50. return try body(buffer)
  51. }
  52. }
  53. internal mutating func withUnsafeMutableBytes<R>(
  54. _ body: (UnsafeMutableRawBufferPointer) throws -> R
  55. ) rethrows -> R {
  56. guard _storage != nil else {
  57. return try body(UnsafeMutableRawBufferPointer(start: nil, count: 0))
  58. }
  59. ensureUnique()
  60. return try _storage!.withUnsafeMutablePointers { count, bytes in
  61. let buffer = UnsafeMutableRawBufferPointer(start: bytes, count: count.pointee)
  62. return try body(buffer)
  63. }
  64. }
  65. }
  66. extension _RawBuffer {
  67. internal class Storage: ManagedBuffer<Int, UInt8> {
  68. internal static func create(minimumCapacity: Int) -> Storage {
  69. Storage.create(
  70. minimumCapacity: minimumCapacity,
  71. makingHeaderWith: {
  72. #if os(OpenBSD)
  73. minimumCapacity
  74. #else
  75. $0.capacity
  76. #endif
  77. }
  78. ) as! Storage
  79. }
  80. }
  81. internal func _copy(capacity: Int) -> Storage {
  82. let copy = Storage.create(minimumCapacity: capacity)
  83. copy.withUnsafeMutablePointers { dstlen, dst in
  84. self.withUnsafeBytes { src in
  85. guard src.count > 0 else { return }
  86. assert(src.count <= dstlen.pointee)
  87. UnsafeMutableRawPointer(dst)
  88. .copyMemory(
  89. from: src.baseAddress!,
  90. byteCount: Swift.min(src.count, dstlen.pointee))
  91. }
  92. }
  93. return copy
  94. }
  95. }