[Swift] Moves capacity outside of Storage (#8650)

- Cleans up capacity usage within the lib and moves it outside of the Storage

-  Use overflow operators
This commit is contained in:
mustiikhalil
2025-07-29 23:21:20 +02:00
committed by GitHub
parent f32a7dcbd2
commit 575d616e60
13 changed files with 121 additions and 110 deletions

View File

@@ -40,11 +40,11 @@ public struct ByteBuffer {
/// This storage doesn't own the memory, therefore, we won't deallocate on deinit.
private let isOwned: Bool
/// Capacity of UInt8 the buffer can hold
private let capacity: Int
/// Retained blob of data that requires the storage to retain a pointer to.
@usableFromInline
var retainedBlob: Blob
/// Capacity of UInt8 the buffer can hold
var capacity: Int
@usableFromInline
init(count: Int) {
@@ -178,12 +178,10 @@ public struct ByteBuffer {
/// The size of the elements written to the buffer + their paddings
private var _readerIndex: Int = 0
// /// Reader is the position of the current Writer Index (capacity - size)
// var reader: Int { _storage.capacity &- _readerIndex }
/// Current size of the buffer
public var count: Int { _readerIndex }
/// Current capacity for the buffer including unused space
public var capacity: Int { _storage.capacity }
public let capacity: Int
/// Constructor that creates a Flatbuffer object from an InternalByteBuffer
/// - Parameter
@@ -194,6 +192,7 @@ public struct ByteBuffer {
blob: .byteBuffer(byteBuffer),
capacity: byteBuffer.capacity)
_readerIndex = byteBuffer.writerIndex
capacity = byteBuffer.capacity
}
/// Constructor that creates a Flatbuffer from unsafe memory region by copying
@@ -209,7 +208,8 @@ public struct ByteBuffer {
{
_storage = Storage(count: capacity)
_storage.copy(from: memory, count: capacity)
_readerIndex = _storage.capacity
_readerIndex = capacity
self.capacity = capacity
}
/// Constructor that creates a Flatbuffer object from a UInt8
@@ -218,7 +218,8 @@ public struct ByteBuffer {
@inline(__always)
public init(bytes: [UInt8]) {
_storage = Storage(blob: .array(bytes), capacity: bytes.count)
_readerIndex = _storage.capacity
_readerIndex = bytes.count
capacity = bytes.count
}
#if !os(WASI)
@@ -228,7 +229,8 @@ public struct ByteBuffer {
@inline(__always)
public init(data: Data) {
_storage = Storage(blob: .data(data), capacity: data.count)
_readerIndex = _storage.capacity
_readerIndex = data.count
capacity = data.count
}
/// Constructor that creates a Flatbuffer object from a ContiguousBytes
@@ -241,7 +243,8 @@ public struct ByteBuffer {
count: Int)
{
_storage = Storage(blob: .bytes(contiguousBytes), capacity: count)
_readerIndex = _storage.capacity
_readerIndex = count
capacity = count
}
#endif
@@ -259,7 +262,8 @@ public struct ByteBuffer {
_storage = Storage(
blob: .pointer(memory),
capacity: capacity)
_readerIndex = _storage.capacity
_readerIndex = capacity
self.capacity = capacity
}
/// Creates a copy of the existing flatbuffer, by copying it to a different memory.
@@ -275,6 +279,7 @@ public struct ByteBuffer {
{
_storage = Storage(blob: blob, capacity: count)
_readerIndex = removeBytes
capacity = count
}
/// Write stores an object into the buffer directly or indirectly.
@@ -289,11 +294,11 @@ public struct ByteBuffer {
func write<T>(value: T, index: Int, direct: Bool = false) {
var index = index
if !direct {
index = _storage.capacity &- index
index = capacity &- index
}
assert(index < _storage.capacity, "Write index is out of writing bound")
assert(index < capacity, "Write index is out of writing bound")
assert(index >= 0, "Writer index should be above zero")
withUnsafePointer(to: value) { ptr in
_ = withUnsafePointer(to: value) { ptr in
_storage.withUnsafeRawPointer {
memcpy(
$0.advanced(by: index),
@@ -387,7 +392,7 @@ public struct ByteBuffer {
count: Int) -> [T]
{
assert(
index + count <= _storage.capacity,
index + count <= capacity,
"Reading out of bounds is illegal")
return _storage.readWithUnsafeRawPointer(position: index) {
@@ -405,7 +410,7 @@ public struct ByteBuffer {
type: String.Encoding) -> String?
{
assert(
index + count <= _storage.capacity,
index + count <= capacity,
"Reading out of bounds is illegal")
return _storage.readWithUnsafeRawPointer(position: index) {
let buf = UnsafeBufferPointer(
@@ -427,7 +432,7 @@ public struct ByteBuffer {
count: Int) -> String?
{
assert(
index + count <= _storage.capacity,
index + count <= capacity,
"Reading out of bounds is illegal")
return _storage.readWithUnsafeRawPointer(position: index) {
String(cString: $0.bindMemory(to: UInt8.self, capacity: count))
@@ -446,7 +451,7 @@ public struct ByteBuffer {
body: (UnsafeRawBufferPointer) throws -> T) rethrows -> T
{
assert(
index + count <= _storage.capacity,
index + count <= capacity,
"Reading out of bounds is illegal")
return try _storage.readWithUnsafeRawPointer(position: index) {
try body(UnsafeRawBufferPointer(start: $0, count: count))