}
func initalize(for size: Int) {
- memory.initializeMemory(as: UInt8.self, repeating: 0, count: size)
+ memset(memory, 0, size)
}
/// Reallocates the buffer incase the object to be written doesnt fit in the current buffer
/// - Parameter size: Size of the current object
- @usableFromInline internal func reallocate(_ size: UInt32, writerSize: Int, alignment: Int) {
- let currentWritingIndex = capacity - writerSize
- while capacity <= writerSize + Int(size) {
+ @usableFromInline internal func reallocate(_ size: Int, writerSize: Int, alignment: Int) {
+ let currentWritingIndex = capacity &- writerSize
+ while capacity <= writerSize &+ size {
capacity = capacity << 1
}
capacity = capacity.convertToPowerofTwo
let newData = UnsafeMutableRawPointer.allocate(byteCount: capacity, alignment: alignment)
- memset(newData, 0, capacity - writerSize)
- memcpy(newData.advanced(by: capacity - writerSize), memory.advanced(by: currentWritingIndex), writerSize)
+ memset(newData, 0, capacity &- writerSize)
+ memcpy(newData.advanced(by: capacity &- writerSize), memory.advanced(by: currentWritingIndex), writerSize)
memory.deallocate()
memory = newData
}
/// Aliginment of the current memory being written to the buffer
internal var alignment = 1
/// Current Index which is being used to write to the buffer, it is written from the end to the start of the buffer
- internal var writerIndex: Int { return _storage.capacity - _writerSize }
+ internal var writerIndex: Int { return _storage.capacity &- _writerSize }
/// Reader is the position of the current Writer Index (capacity - size)
public var reader: Int { return writerIndex }
/// Fills the buffer with padding by adding to the writersize
/// - Parameter padding: Amount of padding between two to be serialized objects
- @usableFromInline mutating func fill(padding: UInt32) {
+ @usableFromInline mutating func fill(padding: Int) {
ensureSpace(size: padding)
- _writerSize += (MemoryLayout<UInt8>.size * Int(padding))
+ _writerSize = _writerSize &+ (MemoryLayout<UInt8>.size &* padding)
}
///Adds an array of type Scalar to the buffer memory
/// - Parameter elements: An array of Scalars
@usableFromInline mutating func push<T: Scalar>(elements: [T]) {
- let size = elements.count * MemoryLayout<T>.size
- ensureSpace(size: UInt32(size))
+ let size = elements.count &* MemoryLayout<T>.size
+ ensureSpace(size: size)
elements.lazy.reversed().forEach { (s) in
push(value: s, len: MemoryLayout.size(ofValue: s))
}
///Adds an array of type Bool to the buffer memory
/// - Parameter elements: An array of Bool
@usableFromInline mutating func push(elements: [Bool]) {
- let size = elements.count * MemoryLayout<Bool>.size
- ensureSpace(size: UInt32(size))
+ let size = elements.count &* MemoryLayout<Bool>.size
+ ensureSpace(size: size)
elements.lazy.reversed().forEach { (s) in
push(value: s ? 1 : 0, len: MemoryLayout.size(ofValue: s))
}
/// - value: Pointer to the object in memory
/// - size: Size of Value being written to the buffer
@usableFromInline mutating func push(struct value: UnsafeMutableRawPointer, size: Int) {
- ensureSpace(size: UInt32(size))
- memcpy(_storage.memory.advanced(by: writerIndex - size), value, size)
+ ensureSpace(size: size)
+ memcpy(_storage.memory.advanced(by: writerIndex &- size), value, size)
defer { value.deallocate() }
- _writerSize += size
+ _writerSize = _writerSize &+ size
}
/// Adds an object of type Scalar into the buffer
/// - value: Object that will be written to the buffer
/// - len: Offset to subtract from the WriterIndex
@usableFromInline mutating func push<T: Scalar>(value: T, len: Int) {
- ensureSpace(size: UInt32(len))
- var v = value.convertedEndian
- memcpy(_storage.memory.advanced(by: writerIndex - len), &v, len)
- _writerSize += len
+ ensureSpace(size: len)
+ var v = value
+ memcpy(_storage.memory.advanced(by: writerIndex &- len), &v, len)
+ _writerSize = _writerSize &+ len
}
/// Adds a string to the buffer using swift.utf8 object
/// - Parameter str: String that will be added to the buffer
/// - Parameter len: length of the string
@usableFromInline mutating func push(string str: String, len: Int) {
- ensureSpace(size: UInt32(len))
+ ensureSpace(size: len)
if str.utf8.withContiguousStorageIfAvailable({ self.push(bytes: $0, len: len) }) != nil {
} else {
let utf8View = str.utf8
/// - bytes: Pointer to the view
/// - len: Size of string
@usableFromInline mutating internal func push(bytes: UnsafeBufferPointer<String.UTF8View.Element>, len: Int) -> Bool {
- memcpy(_storage.memory.advanced(by: writerIndex - len), UnsafeRawPointer(bytes.baseAddress!), len)
- _writerSize += len
+ memcpy(_storage.memory.advanced(by: writerIndex &- len), UnsafeRawPointer(bytes.baseAddress!), len)
+ _writerSize = _writerSize &+ len
return true
}
func write<T>(value: T, index: Int, direct: Bool = false) {
var index = index
if !direct {
- index = _storage.capacity - index
+ index = _storage.capacity &- index
}
_storage.memory.storeBytes(of: value, toByteOffset: index, as: T.self)
}
/// Makes sure that buffer has enouch space for each of the objects that will be written into it
/// - Parameter size: size of object
@discardableResult
- @usableFromInline mutating func ensureSpace(size: UInt32) -> UInt32 {
- if Int(size) + _writerSize > _storage.capacity {
+ @usableFromInline mutating func ensureSpace(size: Int) -> Int {
+ if size &+ _writerSize > _storage.capacity {
_storage.reallocate(size, writerSize: _writerSize, alignment: alignment)
}
assert(size < FlatBufferMaxSize, "Buffer can't grow beyond 2 Gigabytes")
/// Resizes the buffer size
/// - Parameter size: new size for the buffer
@usableFromInline mutating internal func resize(_ size: Int) {
- assert((_writerSize - size) > 0)
+ assert((_writerSize &- size) > 0)
var zero: UInt8 = 0
- for i in 0..<(_writerSize - size) {
- memcpy(_storage.memory.advanced(by: writerIndex + i), &zero, MemoryLayout<UInt8>.size)
+ for i in 0..<(_writerSize &- size) {
+ memcpy(_storage.memory.advanced(by: writerIndex &+ i), &zero, MemoryLayout<UInt8>.size)
}
_writerSize = size
}
/// Creates a new Flatbuffer object that's duplicated from the current one
/// - Parameter removeBytes: the amount of bytes to remove from the current Size
public func duplicate(removing removeBytes: Int = 0) -> ByteBuffer {
- return ByteBuffer(memory: _storage.memory, count: _storage.capacity, removing: _writerSize - removeBytes)
+ return ByteBuffer(memory: _storage.memory, count: _storage.capacity, removing: _writerSize &- removeBytes)
}
}
public var data: Data {
assert(finished, "Data shouldn't be called before finish()")
return Data(bytes: _bb.memory.advanced(by: _bb.writerIndex),
- count: _bb.capacity - _bb.writerIndex)
+ count: _bb.capacity &- _bb.writerIndex)
}
/// Get's the fully sized buffer stored in memory
public var fullSizedByteArray: [UInt8] {
}
/// Returns the written size of the buffer
public var sizedByteArray: [UInt8] {
- let cp = _bb.capacity - _bb.writerIndex
+ let cp = _bb.capacity &- _bb.writerIndex
let start = _bb.memory.advanced(by: _bb.writerIndex)
.bindMemory(to: UInt8.self, capacity: cp)
/// - fields: Array of all the important fields to be serialized
mutating public func require(table: Offset<UOffset>, fields: [Int32]) {
for field in fields {
- let start = _bb.capacity - Int(table.o)
- let startTable = start - Int(_bb.read(def: Int32.self, position: start))
- let isOkay = _bb.read(def: VOffset.self, position: startTable + Int(field)) != 0
+ let start = _bb.capacity &- Int(table.o)
+ let startTable = start &- Int(_bb.read(def: Int32.self, position: start))
+ let isOkay = _bb.read(def: VOffset.self, position: startTable &+ Int(field)) != 0
assert(isOkay, "Flatbuffers requires the following field")
}
}
/// - prefix: if false it wont add the size of the buffer
mutating public func finish<T>(offset: Offset<T>, fileId: String, addPrefix prefix: Bool = false) {
let size = MemoryLayout<UOffset>.size
- preAlign(len: size + (prefix ? size : 0) + FileIdLength, alignment: _minAlignment)
+ preAlign(len: size &+ (prefix ? size : 0) &+ FileIdLength, alignment: _minAlignment)
assert(fileId.count == FileIdLength, "Flatbuffers requires file id to be 4")
_bb.push(string: fileId, len: 4)
finish(offset: offset, addPrefix: prefix)
mutating public func finish<T>(offset: Offset<T>, addPrefix prefix: Bool = false) {
notNested()
let size = MemoryLayout<UOffset>.size
- preAlign(len: size + (prefix ? size : 0), alignment: _minAlignment)
+ preAlign(len: size &+ (prefix ? size : 0), alignment: _minAlignment)
push(element: refer(to: offset.o))
if prefix { push(element: _bb.size) }
_vtableStorage.clear()
let sizeofVoffset = MemoryLayout<VOffset>.size
let vTableOffset = push(element: SOffset(0))
- let tableObjectSize = vTableOffset - startOffset
+ let tableObjectSize = vTableOffset &- startOffset
assert(tableObjectSize < 0x10000, "Buffer can't grow beyond 2 Gigabytes")
- let _max = UInt32(_vtableStorage.maxOffset) + UInt32(sizeofVoffset)
+ let _max = Int(_vtableStorage.maxOffset) &+ sizeofVoffset
_bb.fill(padding: _max)
- _bb.write(value: VOffset(tableObjectSize), index: _bb.writerIndex + sizeofVoffset, direct: true)
+ _bb.write(value: VOffset(tableObjectSize), index: _bb.writerIndex &+ sizeofVoffset, direct: true)
_bb.write(value: VOffset(_max), index: _bb.writerIndex, direct: true)
var itr = 0
while itr < _vtableStorage.writtenIndex {
let loaded = _vtableStorage.load(at: itr)
- itr += _vtableStorage.size
+ itr = itr &+ _vtableStorage.size
guard loaded.offset != 0 else { continue }
- let _index = (_bb.writerIndex + Int(loaded.position))
- _bb.write(value: VOffset(vTableOffset - loaded.offset), index: _index, direct: true)
+ let _index = (_bb.writerIndex &+ Int(loaded.position))
+ _bb.write(value: VOffset(vTableOffset &- loaded.offset), index: _index, direct: true)
}
_vtableStorage.clear()
let len2 = vt2.load(fromByteOffset: 0, as: Int16.self)
for table in _vtables {
- let position = _bb.capacity - Int(table)
+ let position = _bb.capacity &- Int(table)
let vt1 = _bb.memory.advanced(by: position)
let len1 = _bb.read(def: Int16.self, position: position)
if (len2 != len1 || 0 != memcmp(vt1, vt2, Int(len2))) { continue }
if let offset = isAlreadyAdded {
let vTableOff = Int(vTableOffset)
- let space = _bb.capacity - vTableOff
- _bb.write(value: Int32(offset - vTableOff), index: space, direct: true)
- _bb.resize(_bb.capacity - space)
+ let space = _bb.capacity &- vTableOff
+ _bb.write(value: Int32(offset &- vTableOff), index: space, direct: true)
+ _bb.resize(_bb.capacity &- space)
} else {
- _bb.write(value: Int32(vt_use) - Int32(vTableOffset), index: Int(vTableOffset))
+ _bb.write(value: Int32(vt_use &- vTableOffset), index: Int(vTableOffset))
_vtables.append(_bb.size)
}
isNested = false
/// - alignment: Alignment type
@usableFromInline mutating internal func preAlign(len: Int, alignment: Int) {
minAlignment(size: alignment)
- _bb.fill(padding: padding(bufSize: _bb.size + UOffset(len), elementSize: UOffset(alignment)))
+ _bb.fill(padding: Int(padding(bufSize: _bb.size &+ UOffset(len), elementSize: UOffset(alignment))))
}
/// Prealigns the buffer before writting a new object into the buffer
@usableFromInline mutating internal func refer(to off: UOffset) -> UOffset {
let size = MemoryLayout<UOffset>.size
preAlign(len: size, alignment: size)
- return _bb.size - off + UInt32(size)
+ return _bb.size &- off &+ UInt32(size)
}
/// Tracks the elements written into the buffer
mutating public func startVector(_ len: Int, elementSize: Int) {
notNested()
isNested = true
- preAlign(len: len * elementSize, type: UOffset.self)
- preAlign(len: len * elementSize, alignment: elementSize)
+ preAlign(len: len &* elementSize, type: UOffset.self)
+ preAlign(len: len &* elementSize, alignment: elementSize)
}
/// Ends the vector of at length
/// - returns: Offset of the vector
mutating public func createVector<T: Readable>(structs: [UnsafeMutableRawPointer],
type: T.Type) -> Offset<UOffset> {
- startVector(structs.count * T.size, elementSize: T.alignment)
+ startVector(structs.count &* T.size, elementSize: T.alignment)
for i in structs.lazy.reversed() {
create(struct: i, type: T.self)
}
mutating public func create(string str: String) -> Offset<String> {
let len = str.utf8.count
notNested()
- preAlign(len: len + 1, type: UOffset.self)
+ preAlign(len: len &+ 1, type: UOffset.self)
_bb.fill(padding: 1)
_bb.push(string: str, len: len)
push(element: UOffset(len))
/// - offset: Offset of another object to be written
/// - position: The predefined position of the object
mutating public func add<T>(offset: Offset<T>, at position: VOffset) {
- if offset.isEmpty {
- track(offset: 0, at: position)
- return
- }
+ if offset.isEmpty { return }
add(element: refer(to: offset.o), def: 0, at: position)
}
/// - def: Default value for that element
/// - position: The predefined position of the element
mutating public func add<T: Scalar>(element: T, def: T, at position: VOffset) {
- if (element == def && !serializeDefaults) {
- track(offset: 0, at: position)
- return
- }
- let off = push(element: element)
- track(offset: off, at: position)
+ if (element == def && !serializeDefaults) { return }
+ track(offset: push(element: element), at: position)
}
/// Adds Boolean values into the buffer
/// - returns: Postion of the Element
@discardableResult
mutating public func push<T: Scalar>(element: T) -> UOffset {
- preAlign(len: MemoryLayout<T>.size,
- alignment: MemoryLayout<T>.size)
- _bb.push(value: element, len: MemoryLayout<T>.size)
+ let size = MemoryLayout<T>.size
+ preAlign(len: size,
+ alignment: size)
+ _bb.push(value: element, len: size)
return _bb.size
}
/// Last written Index
var writtenIndex: Int = 0
/// the amount of added elements into the buffer
- var addedElements: Int { return capacity - (numOfFields * size) }
+ var addedElements: Int { return capacity - (numOfFields &* size) }
/// Creates the memory to store the buffer in
init() {
/// Builds a buffer with byte count of fieldloc.size * count of field numbers
/// - Parameter count: number of fields to be written
func start(count: Int) {
- let capacity = count * size
+ let capacity = count &* size
ensure(space: capacity)
}
/// - Parameter loc: Location of encoded element
func add(loc: FieldLoc) {
memory.baseAddress?.advanced(by: writtenIndex).storeBytes(of: loc, as: FieldLoc.self)
- writtenIndex += size
- numOfFields += 1
+ writtenIndex = writtenIndex &+ size
+ numOfFields = numOfFields &+ 1
maxOffset = max(loc.position, maxOffset)
}
/// Ensure that the buffer has enough space instead of recreating the buffer each time.
/// - Parameter space: space required for the new vtable
func ensure(space: Int) {
- guard space + writtenIndex > capacity else { return }
+ guard space &+ writtenIndex > capacity else { return }
memory.deallocate()
memory = UnsafeMutableRawBufferPointer.allocate(byteCount: space, alignment: size)
capacity = space