Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 1 | /* |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 2 | * Copyright 2021 Google Inc. All rights reserved. |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 17 | #if !os(WASI) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 18 | import Foundation |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 19 | #else |
| 20 | import SwiftOverlayShims |
| 21 | #endif |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 22 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 23 | /// `ByteBuffer` is the interface that stores the data for a `Flatbuffers` object |
| 24 | /// it allows users to write and read data directly from memory thus the use of its |
| 25 | /// functions should be used |
| 26 | @frozen |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 27 | public struct ByteBuffer { |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 28 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 29 | /// Storage is a container that would hold the memory pointer to solve the issue of |
| 30 | /// deallocating the memory that was held by (memory: UnsafeMutableRawPointer) |
| 31 | @usableFromInline |
| 32 | final class Storage { |
| 33 | // This storage doesn't own the memory, therefore, we won't deallocate on deinit. |
| 34 | private let unowned: Bool |
| 35 | /// pointer to the start of the buffer object in memory |
| 36 | var memory: UnsafeMutableRawPointer |
| 37 | /// Capacity of UInt8 the buffer can hold |
| 38 | var capacity: Int |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 39 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 40 | @usableFromInline |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 41 | init(count: Int, alignment: Int) { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 42 | memory = UnsafeMutableRawPointer.allocate( |
| 43 | byteCount: count, |
| 44 | alignment: alignment) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 45 | capacity = count |
| 46 | unowned = false |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 47 | } |
| 48 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 49 | @usableFromInline |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 50 | init(memory: UnsafeMutableRawPointer, capacity: Int, unowned: Bool) { |
| 51 | self.memory = memory |
| 52 | self.capacity = capacity |
| 53 | self.unowned = unowned |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 56 | deinit { |
| 57 | if !unowned { |
| 58 | memory.deallocate() |
| 59 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 60 | } |
| 61 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 62 | @usableFromInline |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 63 | func copy(from ptr: UnsafeRawPointer, count: Int) { |
| 64 | assert( |
| 65 | !unowned, |
| 66 | "copy should NOT be called on a buffer that is built by assumingMemoryBound") |
| 67 | memory.copyMemory(from: ptr, byteCount: count) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 68 | } |
| 69 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 70 | @usableFromInline |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 71 | func initialize(for size: Int) { |
| 72 | assert( |
| 73 | !unowned, |
| 74 | "initalize should NOT be called on a buffer that is built by assumingMemoryBound") |
| 75 | memset(memory, 0, size) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 78 | /// Reallocates the buffer incase the object to be written doesnt fit in the current buffer |
| 79 | /// - Parameter size: Size of the current object |
| 80 | @usableFromInline |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 81 | func reallocate(_ size: Int, writerSize: Int, alignment: Int) { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 82 | let currentWritingIndex = capacity &- writerSize |
| 83 | while capacity <= writerSize &+ size { |
| 84 | capacity = capacity << 1 |
| 85 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 86 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 87 | /// solution take from Apple-NIO |
| 88 | capacity = capacity.convertToPowerofTwo |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 89 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 90 | let newData = UnsafeMutableRawPointer.allocate( |
| 91 | byteCount: capacity, |
| 92 | alignment: alignment) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 93 | memset(newData, 0, capacity &- writerSize) |
| 94 | memcpy( |
| 95 | newData.advanced(by: capacity &- writerSize), |
| 96 | memory.advanced(by: currentWritingIndex), |
| 97 | writerSize) |
| 98 | memory.deallocate() |
| 99 | memory = newData |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 100 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 101 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 102 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 103 | @usableFromInline var _storage: Storage |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 104 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 105 | /// The size of the elements written to the buffer + their paddings |
| 106 | private var _writerSize: Int = 0 |
| 107 | /// Aliginment of the current memory being written to the buffer |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 108 | var alignment = 1 |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 109 | /// Current Index which is being used to write to the buffer, it is written from the end to the start of the buffer |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 110 | var writerIndex: Int { _storage.capacity &- _writerSize } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 111 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 112 | /// Reader is the position of the current Writer Index (capacity - size) |
| 113 | public var reader: Int { writerIndex } |
| 114 | /// Current size of the buffer |
| 115 | public var size: UOffset { UOffset(_writerSize) } |
| 116 | /// Public Pointer to the buffer object in memory. This should NOT be modified for any reason |
| 117 | public var memory: UnsafeMutableRawPointer { _storage.memory } |
| 118 | /// Current capacity for the buffer |
| 119 | public var capacity: Int { _storage.capacity } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 120 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 121 | /// Constructor that creates a Flatbuffer object from a UInt8 |
| 122 | /// - Parameter bytes: Array of UInt8 |
| 123 | public init(bytes: [UInt8]) { |
| 124 | var b = bytes |
| 125 | _storage = Storage(count: bytes.count, alignment: alignment) |
| 126 | _writerSize = _storage.capacity |
| 127 | b.withUnsafeMutableBytes { bufferPointer in |
| 128 | self._storage.copy(from: bufferPointer.baseAddress!, count: bytes.count) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 129 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 130 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 131 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 132 | #if !os(WASI) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 133 | /// Constructor that creates a Flatbuffer from the Swift Data type object |
| 134 | /// - Parameter data: Swift data Object |
| 135 | public init(data: Data) { |
| 136 | var b = data |
| 137 | _storage = Storage(count: data.count, alignment: alignment) |
| 138 | _writerSize = _storage.capacity |
| 139 | b.withUnsafeMutableBytes { bufferPointer in |
| 140 | self._storage.copy(from: bufferPointer.baseAddress!, count: data.count) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 141 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 142 | } |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 143 | #endif |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 144 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 145 | /// Constructor that creates a Flatbuffer instance with a size |
| 146 | /// - Parameter size: Length of the buffer |
| 147 | init(initialSize size: Int) { |
| 148 | let size = size.convertToPowerofTwo |
| 149 | _storage = Storage(count: size, alignment: alignment) |
| 150 | _storage.initialize(for: size) |
| 151 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 152 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 153 | #if swift(>=5.0) && !os(WASI) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 154 | /// Constructor that creates a Flatbuffer object from a ContiguousBytes |
| 155 | /// - Parameters: |
| 156 | /// - contiguousBytes: Binary stripe to use as the buffer |
| 157 | /// - count: amount of readable bytes |
| 158 | public init<Bytes: ContiguousBytes>( |
| 159 | contiguousBytes: Bytes, |
| 160 | count: Int) |
| 161 | { |
| 162 | _storage = Storage(count: count, alignment: alignment) |
| 163 | _writerSize = _storage.capacity |
| 164 | contiguousBytes.withUnsafeBytes { buf in |
| 165 | _storage.copy(from: buf.baseAddress!, count: buf.count) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 166 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 167 | } |
| 168 | #endif |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 169 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 170 | /// Constructor that creates a Flatbuffer from unsafe memory region without copying |
| 171 | /// - Parameter assumingMemoryBound: The unsafe memory region |
| 172 | /// - Parameter capacity: The size of the given memory region |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 173 | public init( |
| 174 | assumingMemoryBound memory: UnsafeMutableRawPointer, |
| 175 | capacity: Int) |
| 176 | { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 177 | _storage = Storage(memory: memory, capacity: capacity, unowned: true) |
| 178 | _writerSize = capacity |
| 179 | } |
| 180 | |
| 181 | /// Creates a copy of the buffer that's being built by calling sizedBuffer |
| 182 | /// - Parameters: |
| 183 | /// - memory: Current memory of the buffer |
| 184 | /// - count: count of bytes |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 185 | init(memory: UnsafeMutableRawPointer, count: Int) { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 186 | _storage = Storage(count: count, alignment: alignment) |
| 187 | _storage.copy(from: memory, count: count) |
| 188 | _writerSize = _storage.capacity |
| 189 | } |
| 190 | |
| 191 | /// Creates a copy of the existing flatbuffer, by copying it to a different memory. |
| 192 | /// - Parameters: |
| 193 | /// - memory: Current memory of the buffer |
| 194 | /// - count: count of bytes |
| 195 | /// - removeBytes: Removes a number of bytes from the current size |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 196 | init( |
| 197 | memory: UnsafeMutableRawPointer, |
| 198 | count: Int, |
| 199 | removing removeBytes: Int) |
| 200 | { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 201 | _storage = Storage(count: count, alignment: alignment) |
| 202 | _storage.copy(from: memory, count: count) |
| 203 | _writerSize = removeBytes |
| 204 | } |
| 205 | |
| 206 | /// Fills the buffer with padding by adding to the writersize |
| 207 | /// - Parameter padding: Amount of padding between two to be serialized objects |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 208 | @inline(__always) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 209 | @usableFromInline |
| 210 | mutating func fill(padding: Int) { |
| 211 | assert(padding >= 0, "Fill should be larger than or equal to zero") |
| 212 | ensureSpace(size: padding) |
| 213 | _writerSize = _writerSize &+ (MemoryLayout<UInt8>.size &* padding) |
| 214 | } |
| 215 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 216 | /// Adds an array of type Scalar to the buffer memory |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 217 | /// - Parameter elements: An array of Scalars |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 218 | @inline(__always) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 219 | @usableFromInline |
| 220 | mutating func push<T: Scalar>(elements: [T]) { |
| 221 | let size = elements.count &* MemoryLayout<T>.size |
| 222 | ensureSpace(size: size) |
| 223 | elements.reversed().forEach { s in |
| 224 | push(value: s, len: MemoryLayout.size(ofValue: s)) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 225 | } |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 226 | } |
| 227 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 228 | /// Adds an object of type NativeStruct into the buffer |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 229 | /// - Parameters: |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 230 | /// - value: Object that will be written to the buffer |
| 231 | /// - size: size to subtract from the WriterIndex |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 232 | @usableFromInline |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 233 | @inline(__always) |
| 234 | mutating func push<T: NativeStruct>(struct value: T, size: Int) { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 235 | ensureSpace(size: size) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 236 | var v = value |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 237 | memcpy(_storage.memory.advanced(by: writerIndex &- size), &v, size) |
| 238 | _writerSize = _writerSize &+ size |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /// Adds an object of type Scalar into the buffer |
| 242 | /// - Parameters: |
| 243 | /// - value: Object that will be written to the buffer |
| 244 | /// - len: Offset to subtract from the WriterIndex |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 245 | @inline(__always) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 246 | @usableFromInline |
| 247 | mutating func push<T: Scalar>(value: T, len: Int) { |
| 248 | ensureSpace(size: len) |
| 249 | var v = value |
| 250 | memcpy(_storage.memory.advanced(by: writerIndex &- len), &v, len) |
| 251 | _writerSize = _writerSize &+ len |
| 252 | } |
| 253 | |
| 254 | /// Adds a string to the buffer using swift.utf8 object |
| 255 | /// - Parameter str: String that will be added to the buffer |
| 256 | /// - Parameter len: length of the string |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 257 | @inline(__always) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 258 | @usableFromInline |
| 259 | mutating func push(string str: String, len: Int) { |
| 260 | ensureSpace(size: len) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 261 | if str.utf8 |
| 262 | .withContiguousStorageIfAvailable({ self.push(bytes: $0, len: len) }) != |
| 263 | nil |
| 264 | { |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 265 | } else { |
| 266 | let utf8View = str.utf8 |
| 267 | for c in utf8View.reversed() { |
| 268 | push(value: c, len: 1) |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /// Writes a string to Bytebuffer using UTF8View |
| 274 | /// - Parameters: |
| 275 | /// - bytes: Pointer to the view |
| 276 | /// - len: Size of string |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 277 | @usableFromInline |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 278 | @inline(__always) |
| 279 | mutating func push( |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 280 | bytes: UnsafeBufferPointer<String.UTF8View.Element>, |
| 281 | len: Int) -> Bool |
| 282 | { |
| 283 | memcpy( |
| 284 | _storage.memory.advanced(by: writerIndex &- len), |
| 285 | UnsafeRawPointer(bytes.baseAddress!), |
| 286 | len) |
| 287 | _writerSize = _writerSize &+ len |
| 288 | return true |
| 289 | } |
| 290 | |
| 291 | /// Write stores an object into the buffer directly or indirectly. |
| 292 | /// |
| 293 | /// Direct: ignores the capacity of buffer which would mean we are referring to the direct point in memory |
| 294 | /// indirect: takes into respect the current capacity of the buffer (capacity - index), writing to the buffer from the end |
| 295 | /// - Parameters: |
| 296 | /// - value: Value that needs to be written to the buffer |
| 297 | /// - index: index to write to |
| 298 | /// - direct: Should take into consideration the capacity of the buffer |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 299 | @inline(__always) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 300 | func write<T>(value: T, index: Int, direct: Bool = false) { |
| 301 | var index = index |
| 302 | if !direct { |
| 303 | index = _storage.capacity &- index |
| 304 | } |
| 305 | assert(index < _storage.capacity, "Write index is out of writing bound") |
| 306 | assert(index >= 0, "Writer index should be above zero") |
| 307 | _storage.memory.storeBytes(of: value, toByteOffset: index, as: T.self) |
| 308 | } |
| 309 | |
| 310 | /// Makes sure that buffer has enouch space for each of the objects that will be written into it |
| 311 | /// - Parameter size: size of object |
| 312 | @discardableResult |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 313 | @usableFromInline |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 314 | @inline(__always) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 315 | mutating func ensureSpace(size: Int) -> Int { |
| 316 | if size &+ _writerSize > _storage.capacity { |
| 317 | _storage.reallocate(size, writerSize: _writerSize, alignment: alignment) |
| 318 | } |
| 319 | assert(size < FlatBufferMaxSize, "Buffer can't grow beyond 2 Gigabytes") |
| 320 | return size |
| 321 | } |
| 322 | |
| 323 | /// pops the written VTable if it's already written into the buffer |
| 324 | /// - Parameter size: size of the `VTable` |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 325 | @usableFromInline |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 326 | @inline(__always) |
| 327 | mutating func pop(_ size: Int) { |
| 328 | assert( |
| 329 | (_writerSize &- size) > 0, |
| 330 | "New size should NOT be a negative number") |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 331 | memset(_storage.memory.advanced(by: writerIndex), 0, _writerSize &- size) |
| 332 | _writerSize = size |
| 333 | } |
| 334 | |
| 335 | /// Clears the current size of the buffer |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 336 | @inline(__always) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 337 | mutating public func clearSize() { |
| 338 | _writerSize = 0 |
| 339 | } |
| 340 | |
| 341 | /// Clears the current instance of the buffer, replacing it with new memory |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 342 | @inline(__always) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 343 | mutating public func clear() { |
| 344 | _writerSize = 0 |
| 345 | alignment = 1 |
| 346 | _storage.initialize(for: _storage.capacity) |
| 347 | } |
| 348 | |
| 349 | /// Reads an object from the buffer |
| 350 | /// - Parameters: |
| 351 | /// - def: Type of the object |
| 352 | /// - position: the index of the object in the buffer |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 353 | @inline(__always) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 354 | public func read<T>(def: T.Type, position: Int) -> T { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 355 | _storage.memory.advanced(by: position).load(as: T.self) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /// Reads a slice from the memory assuming a type of T |
| 359 | /// - Parameters: |
| 360 | /// - index: index of the object to be read from the buffer |
| 361 | /// - count: count of bytes in memory |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 362 | @inline(__always) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 363 | public func readSlice<T>( |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 364 | index: Int, |
| 365 | count: Int) -> [T] |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 366 | { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 367 | assert( |
| 368 | index + count <= _storage.capacity, |
| 369 | "Reading out of bounds is illegal") |
| 370 | let start = _storage.memory.advanced(by: index) |
| 371 | .assumingMemoryBound(to: T.self) |
| 372 | let array = UnsafeBufferPointer(start: start, count: count) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 373 | return Array(array) |
| 374 | } |
| 375 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 376 | #if !os(WASI) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 377 | /// Reads a string from the buffer and encodes it to a swift string |
| 378 | /// - Parameters: |
| 379 | /// - index: index of the string in the buffer |
| 380 | /// - count: length of the string |
| 381 | /// - type: Encoding of the string |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 382 | @inline(__always) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 383 | public func readString( |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 384 | at index: Int, |
| 385 | count: Int, |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 386 | type: String.Encoding = .utf8) -> String? |
| 387 | { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 388 | assert( |
| 389 | index + count <= _storage.capacity, |
| 390 | "Reading out of bounds is illegal") |
| 391 | let start = _storage.memory.advanced(by: index) |
| 392 | .assumingMemoryBound(to: UInt8.self) |
| 393 | let bufprt = UnsafeBufferPointer(start: start, count: count) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 394 | return String(bytes: Array(bufprt), encoding: type) |
| 395 | } |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 396 | #else |
| 397 | /// Reads a string from the buffer and encodes it to a swift string |
| 398 | /// - Parameters: |
| 399 | /// - index: index of the string in the buffer |
| 400 | /// - count: length of the string |
| 401 | /// - type: Encoding of the string |
| 402 | @inline(__always) |
| 403 | public func readString( |
| 404 | at index: Int, |
| 405 | count: Int) -> String? |
| 406 | { |
| 407 | assert( |
| 408 | index + count <= _storage.capacity, |
| 409 | "Reading out of bounds is illegal") |
| 410 | let start = _storage.memory.advanced(by: index) |
| 411 | .assumingMemoryBound(to: UInt8.self) |
| 412 | let bufprt = UnsafeBufferPointer(start: start, count: count) |
| 413 | return String(cString: bufprt.baseAddress!) |
| 414 | } |
| 415 | #endif |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 416 | |
| 417 | /// Creates a new Flatbuffer object that's duplicated from the current one |
| 418 | /// - Parameter removeBytes: the amount of bytes to remove from the current Size |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 419 | @inline(__always) |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 420 | public func duplicate(removing removeBytes: Int = 0) -> ByteBuffer { |
| 421 | assert(removeBytes > 0, "Can NOT remove negative bytes") |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 422 | assert( |
| 423 | removeBytes < _storage.capacity, |
| 424 | "Can NOT remove more bytes than the ones allocated") |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 425 | return ByteBuffer( |
| 426 | memory: _storage.memory, |
| 427 | count: _storage.capacity, |
| 428 | removing: _writerSize &- removeBytes) |
| 429 | } |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 430 | |
| 431 | /// Returns the written bytes into the ``ByteBuffer`` |
| 432 | public var underlyingBytes: [UInt8] { |
| 433 | let cp = capacity &- writerIndex |
| 434 | let start = memory.advanced(by: writerIndex) |
| 435 | .bindMemory(to: UInt8.self, capacity: cp) |
| 436 | |
| 437 | let ptr = UnsafeBufferPointer<UInt8>(start: start, count: cp) |
| 438 | return Array(ptr) |
| 439 | } |
| 440 | |
| 441 | /// SkipPrefix Skips the first 4 bytes in case one of the following |
| 442 | /// functions are called `getPrefixedSizeCheckedRoot` & `getPrefixedSizeRoot` |
| 443 | /// which allows us to skip the first 4 bytes instead of recreating the buffer |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 444 | @discardableResult |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 445 | @usableFromInline |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 446 | @inline(__always) |
| 447 | mutating func skipPrefix() -> Int32 { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 448 | _writerSize = _writerSize &- MemoryLayout<Int32>.size |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 449 | return read(def: Int32.self, position: 0) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 450 | } |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame^] | 451 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | extension ByteBuffer: CustomDebugStringConvertible { |
| 455 | |
Austin Schuh | 58b9b47 | 2020-11-25 19:12:44 -0800 | [diff] [blame] | 456 | public var debugDescription: String { |
| 457 | """ |
| 458 | buffer located at: \(_storage.memory), with capacity of \(_storage.capacity) |
| 459 | { writerSize: \(_writerSize), readerSize: \(reader), writerIndex: \(writerIndex) } |
| 460 | """ |
| 461 | } |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 462 | } |