Skip to content

Commit

Permalink
Adds API for copying memory within ByteBuffer, and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mustiikhalil committed Jan 11, 2025
1 parent 62f1d09 commit 8b96c52
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
16 changes: 16 additions & 0 deletions swift/Sources/FlatBuffers/ByteBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ public struct ByteBuffer {
_writerSize = capacity
}

/// Constructor that creates a Flatbuffer from unsafe memory region by copying
/// the underlying data to a new pointer
///
/// - Parameters:
/// - copyingMemoryBound: The unsafe memory region
/// - capacity: The size of the given memory region
@inline(__always)
public init(
copyingMemoryBound memory: UnsafeMutableRawPointer,
capacity: Int)
{
_storage = Storage(count: capacity, alignment: alignment)
_storage.copy(from: memory, count: capacity)
_writerSize = _storage.capacity
}

/// Creates a copy of the existing flatbuffer, by copying it to a different memory.
/// - Parameters:
/// - memory: Current memory of the buffer
Expand Down
2 changes: 1 addition & 1 deletion swift/Sources/FlatBuffers/FlatBufferBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public struct FlatBufferBuilder {
public var sizedBuffer: ByteBuffer {
assert(finished, "Data shouldn't be called before finish()")
return ByteBuffer(
assumingMemoryBound: _bb.memory.advanced(by: _bb.reader),
copyingMemoryBound: _bb.memory.advanced(by: _bb.reader),
capacity: Int(_bb.size))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2024 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import XCTest
@testable import FlatBuffers

final class ByteBufferTests: XCTestCase {
func testCopyingMemory() {
let count = 100
let ptr = UnsafeMutableRawPointer.allocate(byteCount: count, alignment: 1)
let byteBuffer = ByteBuffer(copyingMemoryBound: ptr, capacity: count)
XCTAssertNotEqual(byteBuffer.memory, ptr)
}

func testSamePointer() {
let count = 100
let ptr = UnsafeMutableRawPointer.allocate(byteCount: count, alignment: 1)
let byteBuffer = ByteBuffer(assumingMemoryBound: ptr, capacity: count)
XCTAssertEqual(byteBuffer.memory, ptr)
}

func testSameDataPtr() {
let count = 100
let ptr = Data(repeating: 0, count: count)
let byteBuffer = ByteBuffer(data: ptr)
ptr.withUnsafeBytes { ptr in
XCTAssertEqual(byteBuffer.memory, ptr.baseAddress)
}
}

func testSameArrayPtr() {
let count = 100
let ptr: [UInt8] = Array(repeating: 0, count: count)
let byteBuffer = ByteBuffer(bytes: ptr)
ptr.withUnsafeBytes { ptr in
XCTAssertEqual(byteBuffer.memory, ptr.baseAddress)
}
}
}

0 comments on commit 8b96c52

Please sign in to comment.