Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame^] | 1 | import Foundation |
| 2 | |
| 3 | public struct ByteBuffer { |
| 4 | |
| 5 | /// Storage is a container that would hold the memory pointer to solve the issue of |
| 6 | /// deallocating the memory that was held by (memory: UnsafeMutableRawPointer) |
| 7 | @usableFromInline final class Storage { |
| 8 | // This storage doesn't own the memory, therefore, we won't deallocate on deinit. |
| 9 | private let unowned: Bool |
| 10 | /// pointer to the start of the buffer object in memory |
| 11 | var memory: UnsafeMutableRawPointer |
| 12 | /// Capacity of UInt8 the buffer can hold |
| 13 | var capacity: Int |
| 14 | |
| 15 | init(count: Int, alignment: Int) { |
| 16 | memory = UnsafeMutableRawPointer.allocate(byteCount: count, alignment: alignment) |
| 17 | capacity = count |
| 18 | unowned = false |
| 19 | } |
| 20 | |
| 21 | init(memory: UnsafeMutableRawPointer, capacity: Int, unowned: Bool) { |
| 22 | self.memory = memory |
| 23 | self.capacity = capacity |
| 24 | self.unowned = unowned |
| 25 | } |
| 26 | |
| 27 | deinit { |
| 28 | if !unowned { |
| 29 | memory.deallocate() |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func copy(from ptr: UnsafeRawPointer, count: Int) { |
| 34 | assert(!unowned, "copy should NOT be called on a buffer that is built by assumingMemoryBound") |
| 35 | memory.copyMemory(from: ptr, byteCount: count) |
| 36 | } |
| 37 | |
| 38 | func initialize(for size: Int) { |
| 39 | assert(!unowned, "initalize should NOT be called on a buffer that is built by assumingMemoryBound") |
| 40 | memset(memory, 0, size) |
| 41 | } |
| 42 | |
| 43 | /// Reallocates the buffer incase the object to be written doesnt fit in the current buffer |
| 44 | /// - Parameter size: Size of the current object |
| 45 | @usableFromInline internal func reallocate(_ size: Int, writerSize: Int, alignment: Int) { |
| 46 | let currentWritingIndex = capacity &- writerSize |
| 47 | while capacity <= writerSize &+ size { |
| 48 | capacity = capacity << 1 |
| 49 | } |
| 50 | |
| 51 | /// solution take from Apple-NIO |
| 52 | capacity = capacity.convertToPowerofTwo |
| 53 | |
| 54 | let newData = UnsafeMutableRawPointer.allocate(byteCount: capacity, alignment: alignment) |
| 55 | memset(newData, 0, capacity &- writerSize) |
| 56 | memcpy(newData.advanced(by: capacity &- writerSize), memory.advanced(by: currentWritingIndex), writerSize) |
| 57 | memory.deallocate() |
| 58 | memory = newData |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | @usableFromInline var _storage: Storage |
| 63 | |
| 64 | /// The size of the elements written to the buffer + their paddings |
| 65 | private var _writerSize: Int = 0 |
| 66 | /// Aliginment of the current memory being written to the buffer |
| 67 | internal var alignment = 1 |
| 68 | /// Current Index which is being used to write to the buffer, it is written from the end to the start of the buffer |
| 69 | internal var writerIndex: Int { return _storage.capacity &- _writerSize } |
| 70 | |
| 71 | /// Reader is the position of the current Writer Index (capacity - size) |
| 72 | public var reader: Int { return writerIndex } |
| 73 | /// Current size of the buffer |
| 74 | public var size: UOffset { return UOffset(_writerSize) } |
| 75 | /// Public Pointer to the buffer object in memory. This should NOT be modified for any reason |
| 76 | public var memory: UnsafeMutableRawPointer { return _storage.memory } |
| 77 | /// Current capacity for the buffer |
| 78 | public var capacity: Int { return _storage.capacity } |
| 79 | |
| 80 | /// Constructor that creates a Flatbuffer object from a UInt8 |
| 81 | /// - Parameter bytes: Array of UInt8 |
| 82 | public init(bytes: [UInt8]) { |
| 83 | var b = bytes |
| 84 | _storage = Storage(count: bytes.count, alignment: alignment) |
| 85 | _writerSize = _storage.capacity |
| 86 | b.withUnsafeMutableBytes { bufferPointer in |
| 87 | self._storage.copy(from: bufferPointer.baseAddress!, count: bytes.count) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// Constructor that creates a Flatbuffer from the Swift Data type object |
| 92 | /// - Parameter data: Swift data Object |
| 93 | public init(data: Data) { |
| 94 | var b = data |
| 95 | _storage = Storage(count: data.count, alignment: alignment) |
| 96 | _writerSize = _storage.capacity |
| 97 | b.withUnsafeMutableBytes { bufferPointer in |
| 98 | self._storage.copy(from: bufferPointer.baseAddress!, count: data.count) |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /// Constructor that creates a Flatbuffer instance with a size |
| 103 | /// - Parameter size: Length of the buffer |
| 104 | init(initialSize size: Int) { |
| 105 | let size = size.convertToPowerofTwo |
| 106 | _storage = Storage(count: size, alignment: alignment) |
| 107 | _storage.initialize(for: size) |
| 108 | } |
| 109 | |
| 110 | #if swift(>=5.0) |
| 111 | /// Constructor that creates a Flatbuffer object from a ContiguousBytes |
| 112 | /// - Parameters: |
| 113 | /// - contiguousBytes: Binary stripe to use as the buffer |
| 114 | /// - count: amount of readable bytes |
| 115 | public init<Bytes: ContiguousBytes>( |
| 116 | contiguousBytes: Bytes, |
| 117 | count: Int |
| 118 | ) { |
| 119 | _storage = Storage(count: count, alignment: alignment) |
| 120 | _writerSize = _storage.capacity |
| 121 | contiguousBytes.withUnsafeBytes { buf in |
| 122 | _storage.copy(from: buf.baseAddress!, count: buf.count) |
| 123 | } |
| 124 | } |
| 125 | #endif |
| 126 | |
| 127 | /// Constructor that creates a Flatbuffer from unsafe memory region without copying |
| 128 | /// - Parameter assumingMemoryBound: The unsafe memory region |
| 129 | /// - Parameter capacity: The size of the given memory region |
| 130 | public init(assumingMemoryBound memory: UnsafeMutableRawPointer, capacity: Int) { |
| 131 | _storage = Storage(memory: memory, capacity: capacity, unowned: true) |
| 132 | _writerSize = capacity |
| 133 | } |
| 134 | |
| 135 | /// Creates a copy of the buffer that's being built by calling sizedBuffer |
| 136 | /// - Parameters: |
| 137 | /// - memory: Current memory of the buffer |
| 138 | /// - count: count of bytes |
| 139 | internal init(memory: UnsafeMutableRawPointer, count: Int) { |
| 140 | _storage = Storage(count: count, alignment: alignment) |
| 141 | _storage.copy(from: memory, count: count) |
| 142 | _writerSize = _storage.capacity |
| 143 | } |
| 144 | |
| 145 | /// Creates a copy of the existing flatbuffer, by copying it to a different memory. |
| 146 | /// - Parameters: |
| 147 | /// - memory: Current memory of the buffer |
| 148 | /// - count: count of bytes |
| 149 | /// - removeBytes: Removes a number of bytes from the current size |
| 150 | internal init(memory: UnsafeMutableRawPointer, count: Int, removing removeBytes: Int) { |
| 151 | _storage = Storage(count: count, alignment: alignment) |
| 152 | _storage.copy(from: memory, count: count) |
| 153 | _writerSize = removeBytes |
| 154 | } |
| 155 | |
| 156 | /// Fills the buffer with padding by adding to the writersize |
| 157 | /// - Parameter padding: Amount of padding between two to be serialized objects |
| 158 | @usableFromInline mutating func fill(padding: Int) { |
| 159 | assert(padding >= 0, "Fill should be larger than or equal to zero") |
| 160 | ensureSpace(size: padding) |
| 161 | _writerSize = _writerSize &+ (MemoryLayout<UInt8>.size &* padding) |
| 162 | } |
| 163 | |
| 164 | ///Adds an array of type Scalar to the buffer memory |
| 165 | /// - Parameter elements: An array of Scalars |
| 166 | @usableFromInline mutating func push<T: Scalar>(elements: [T]) { |
| 167 | let size = elements.count &* MemoryLayout<T>.size |
| 168 | ensureSpace(size: size) |
| 169 | elements.reversed().forEach { (s) in |
| 170 | push(value: s, len: MemoryLayout.size(ofValue: s)) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /// A custom type of structs that are padded according to the flatbuffer padding, |
| 175 | /// - Parameters: |
| 176 | /// - value: Pointer to the object in memory |
| 177 | /// - size: Size of Value being written to the buffer |
| 178 | @available(*, deprecated, message: "0.9.0 will be removing the following method. Regenerate the code") |
| 179 | @usableFromInline mutating func push(struct value: UnsafeMutableRawPointer, size: Int) { |
| 180 | ensureSpace(size: size) |
| 181 | memcpy(_storage.memory.advanced(by: writerIndex &- size), value, size) |
| 182 | defer { value.deallocate() } |
| 183 | _writerSize = _writerSize &+ size |
| 184 | } |
| 185 | |
| 186 | /// Prepares the buffer to receive a struct of certian size. |
| 187 | /// The alignment of the memory is already handled since we already called preAlign |
| 188 | /// - Parameter size: size of the struct |
| 189 | @usableFromInline mutating func prepareBufferToReceiveStruct(of size: Int) { |
| 190 | ensureSpace(size: size) |
| 191 | _writerSize = _writerSize &+ size |
| 192 | } |
| 193 | |
| 194 | /// Reverse the input direction to the buffer, since `FlatBuffers` uses a back to front, following method will take current `writerIndex` |
| 195 | /// and writes front to back into the buffer, respecting the padding & the alignment |
| 196 | /// - Parameters: |
| 197 | /// - value: value of type Scalar |
| 198 | /// - position: position relative to the `writerIndex` |
| 199 | /// - len: length of the value in terms of bytes |
| 200 | @usableFromInline mutating func reversePush<T: Scalar>(value: T, position: Int, len: Int) { |
| 201 | var v = value |
| 202 | memcpy(_storage.memory.advanced(by: writerIndex &+ position), &v, len) |
| 203 | } |
| 204 | |
| 205 | /// Adds an object of type Scalar into the buffer |
| 206 | /// - Parameters: |
| 207 | /// - value: Object that will be written to the buffer |
| 208 | /// - len: Offset to subtract from the WriterIndex |
| 209 | @usableFromInline mutating func push<T: Scalar>(value: T, len: Int) { |
| 210 | ensureSpace(size: len) |
| 211 | var v = value |
| 212 | memcpy(_storage.memory.advanced(by: writerIndex &- len), &v, len) |
| 213 | _writerSize = _writerSize &+ len |
| 214 | } |
| 215 | |
| 216 | /// Adds a string to the buffer using swift.utf8 object |
| 217 | /// - Parameter str: String that will be added to the buffer |
| 218 | /// - Parameter len: length of the string |
| 219 | @usableFromInline mutating func push(string str: String, len: Int) { |
| 220 | ensureSpace(size: len) |
| 221 | if str.utf8.withContiguousStorageIfAvailable({ self.push(bytes: $0, len: len) }) != nil { |
| 222 | } else { |
| 223 | let utf8View = str.utf8 |
| 224 | for c in utf8View.reversed() { |
| 225 | push(value: c, len: 1) |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /// Writes a string to Bytebuffer using UTF8View |
| 231 | /// - Parameters: |
| 232 | /// - bytes: Pointer to the view |
| 233 | /// - len: Size of string |
| 234 | @usableFromInline mutating internal func push(bytes: UnsafeBufferPointer<String.UTF8View.Element>, len: Int) -> Bool { |
| 235 | memcpy(_storage.memory.advanced(by: writerIndex &- len), UnsafeRawPointer(bytes.baseAddress!), len) |
| 236 | _writerSize = _writerSize &+ len |
| 237 | return true |
| 238 | } |
| 239 | |
| 240 | /// Write stores an object into the buffer directly or indirectly. |
| 241 | /// |
| 242 | /// Direct: ignores the capacity of buffer which would mean we are referring to the direct point in memory |
| 243 | /// indirect: takes into respect the current capacity of the buffer (capacity - index), writing to the buffer from the end |
| 244 | /// - Parameters: |
| 245 | /// - value: Value that needs to be written to the buffer |
| 246 | /// - index: index to write to |
| 247 | /// - direct: Should take into consideration the capacity of the buffer |
| 248 | func write<T>(value: T, index: Int, direct: Bool = false) { |
| 249 | var index = index |
| 250 | if !direct { |
| 251 | index = _storage.capacity &- index |
| 252 | } |
| 253 | assert(index < _storage.capacity, "Write index is out of writing bound") |
| 254 | assert(index >= 0, "Writer index should be above zero") |
| 255 | _storage.memory.storeBytes(of: value, toByteOffset: index, as: T.self) |
| 256 | } |
| 257 | |
| 258 | /// Makes sure that buffer has enouch space for each of the objects that will be written into it |
| 259 | /// - Parameter size: size of object |
| 260 | @discardableResult |
| 261 | @usableFromInline mutating func ensureSpace(size: Int) -> Int { |
| 262 | if size &+ _writerSize > _storage.capacity { |
| 263 | _storage.reallocate(size, writerSize: _writerSize, alignment: alignment) |
| 264 | } |
| 265 | assert(size < FlatBufferMaxSize, "Buffer can't grow beyond 2 Gigabytes") |
| 266 | return size |
| 267 | } |
| 268 | |
| 269 | /// pops the written VTable if it's already written into the buffer |
| 270 | /// - Parameter size: size of the `VTable` |
| 271 | @usableFromInline mutating internal func pop(_ size: Int) { |
| 272 | assert((_writerSize &- size) > 0, "New size should NOT be a negative number") |
| 273 | memset(_storage.memory.advanced(by: writerIndex), 0, _writerSize &- size) |
| 274 | _writerSize = size |
| 275 | } |
| 276 | |
| 277 | /// Clears the current size of the buffer |
| 278 | mutating public func clearSize() { |
| 279 | _writerSize = 0 |
| 280 | } |
| 281 | |
| 282 | /// Clears the current instance of the buffer, replacing it with new memory |
| 283 | mutating public func clear() { |
| 284 | _writerSize = 0 |
| 285 | alignment = 1 |
| 286 | _storage.initialize(for: _storage.capacity) |
| 287 | } |
| 288 | |
| 289 | /// Reads an object from the buffer |
| 290 | /// - Parameters: |
| 291 | /// - def: Type of the object |
| 292 | /// - position: the index of the object in the buffer |
| 293 | public func read<T>(def: T.Type, position: Int) -> T { |
| 294 | assert(position + MemoryLayout<T>.size <= _storage.capacity, "Reading out of bounds is illegal") |
| 295 | return _storage.memory.advanced(by: position).load(as: T.self) |
| 296 | } |
| 297 | |
| 298 | /// Reads a slice from the memory assuming a type of T |
| 299 | /// - Parameters: |
| 300 | /// - index: index of the object to be read from the buffer |
| 301 | /// - count: count of bytes in memory |
| 302 | public func readSlice<T>(index: Int32, |
| 303 | count: Int32) -> [T] { |
| 304 | let _index = Int(index) |
| 305 | let _count = Int(count) |
| 306 | assert(_index + _count <= _storage.capacity, "Reading out of bounds is illegal") |
| 307 | let start = _storage.memory.advanced(by: _index).assumingMemoryBound(to: T.self) |
| 308 | let array = UnsafeBufferPointer(start: start, count: _count) |
| 309 | return Array(array) |
| 310 | } |
| 311 | |
| 312 | /// Reads a string from the buffer and encodes it to a swift string |
| 313 | /// - Parameters: |
| 314 | /// - index: index of the string in the buffer |
| 315 | /// - count: length of the string |
| 316 | /// - type: Encoding of the string |
| 317 | public func readString(at index: Int32, |
| 318 | count: Int32, |
| 319 | type: String.Encoding = .utf8) -> String? { |
| 320 | let _index = Int(index) |
| 321 | let _count = Int(count) |
| 322 | assert(_index + _count <= _storage.capacity, "Reading out of bounds is illegal") |
| 323 | let start = _storage.memory.advanced(by: _index).assumingMemoryBound(to: UInt8.self) |
| 324 | let bufprt = UnsafeBufferPointer(start: start, count: _count) |
| 325 | return String(bytes: Array(bufprt), encoding: type) |
| 326 | } |
| 327 | |
| 328 | /// Creates a new Flatbuffer object that's duplicated from the current one |
| 329 | /// - Parameter removeBytes: the amount of bytes to remove from the current Size |
| 330 | public func duplicate(removing removeBytes: Int = 0) -> ByteBuffer { |
| 331 | assert(removeBytes > 0, "Can NOT remove negative bytes") |
| 332 | assert(removeBytes < _storage.capacity, "Can NOT remove more bytes than the ones allocated") |
| 333 | return ByteBuffer(memory: _storage.memory, count: _storage.capacity, removing: _writerSize &- removeBytes) |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | extension ByteBuffer: CustomDebugStringConvertible { |
| 338 | |
| 339 | public var debugDescription: String { |
| 340 | """ |
| 341 | buffer located at: \(_storage.memory), with capacity of \(_storage.capacity) |
| 342 | { writerSize: \(_writerSize), readerSize: \(reader), writerIndex: \(writerIndex) } |
| 343 | """ |
| 344 | } |
| 345 | } |