Skip to content

Commit

Permalink
fix: Rename a bunch of things
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jan 7, 2025
1 parent 4460500 commit 1ed7fdf
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class ArrayBufferHolder {
* Once the `ArrayBuffer` is no longer in use, the given `deleteFunc` will be called with the given `deleteFuncContext`
* as an argument. The caller is responsible for deleting `data` once this is called.
*/
static ArrayBufferHolder makeBuffer(uint8_t* _Nonnull data, size_t size, SwiftClosure destroy) {
static ArrayBufferHolder wrap(uint8_t* _Nonnull data, size_t size, SwiftClosure destroy) {
std::function<void()> deleteFunc = destroy.getFunction();
auto arrayBuffer = ArrayBuffer::makeBuffer(data, size, std::move(deleteFunc));
auto arrayBuffer = ArrayBuffer::wrap(data, size, std::move(deleteFunc));
return ArrayBufferHolder(arrayBuffer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import Foundation
*/
public typealias ArrayBufferHolder = margelo.nitro.ArrayBufferHolder

// pragma MARK: Wrap

public extension ArrayBufferHolder {
/**
* Create a new `ArrayBufferHolder` that wraps the given `data` of the given `size`
Expand All @@ -28,9 +30,27 @@ public extension ArrayBufferHolder {
// Convert escaping Swift closure to a `void*`
let swiftClosure = SwiftClosure(wrappingClosure: delete)
// Create ArrayBufferHolder with our wrapped Swift closure to make it callable as a C-function pointer
return ArrayBufferHolder.makeBuffer(data, size, swiftClosure)
return ArrayBufferHolder.wrap(data, size, swiftClosure)
}

/**
* Create a new `ArrayBufferHolder` that wraps the given `data` of the given `size`
* without performing a copy.
* When the `ArrayBuffer` is no longer used, `onDelete` will be called, in which
* you as a caller are responsible for deleting `data`.
*/
static func wrap(dataWithoutCopy data: UnsafeMutableRawPointer,
size: Int,
onDelete delete: @escaping () -> Void) -> ArrayBufferHolder {
return ArrayBufferHolder.wrap(dataWithoutCopy: data.assumingMemoryBound(to: UInt8.self),
size: size,
onDelete: delete)
}
}

// pragma MARK: Allocate

public extension ArrayBufferHolder {
/**
* Allocate a new buffer of the given `size`.
* If `initializeToZero` is `true`, all bytes are set to `0`, otherwise they are left untouched.
Expand All @@ -40,27 +60,48 @@ public extension ArrayBufferHolder {
if initializeToZero {
data.initialize(repeating: 0, count: size)
}

let deleteFunc = SwiftClosure {
data.deallocate()
}
return ArrayBufferHolder.makeBuffer(data, size, deleteFunc)
return ArrayBufferHolder.wrap(data, size, deleteFunc)
}
}

// pragma MARK: Copy

public extension ArrayBufferHolder {
/**
* Copy the given `UnsafeMutablePointer<UInt8>` into a new **owning** `ArrayBufferHolder`.
*/
static func copy(of other: UnsafeMutablePointer<UInt8>,
size: Int) -> ArrayBufferHolder {
// 1. Create new `UnsafeMutablePointer<UInt8>`
let copy = UnsafeMutablePointer<UInt8>.allocate(capacity: size)
// 2. Copy over data
copy.initialize(from: other, count: size)
// 3. Create memory safe destroyer
let deleteFunc = SwiftClosure {
copy.deallocate()
}
return ArrayBufferHolder.wrap(copy, size, deleteFunc)
}


/**
* Copy the given `UnsafeMutableRawPointer` into a new **owning** `ArrayBufferHolder`.
*/
static func copy(of other: UnsafeMutableRawPointer,
size: Int) -> ArrayBufferHolder {
return ArrayBufferHolder.copy(of: other.assumingMemoryBound(to: UInt8.self),
size: size)
}

/**
* Copy the given `ArrayBufferHolder` into a new **owning** `ArrayBufferHolder`.
*/
static func copy(of other: ArrayBufferHolder) -> ArrayBufferHolder {
let data = UnsafeMutablePointer<UInt8>.allocate(capacity: other.size)
let pointer = other.data.assumingMemoryBound(to: UInt8.self)
data.initialize(from: pointer, count: other.size)

let deleteFunc = SwiftClosure {
data.deallocate()
}
return ArrayBufferHolder.makeBuffer(data, other.size, deleteFunc)
return ArrayBufferHolder.copy(of: other.data, size: other.size)
}

/**
* Copy the given `Data` into a new **owning** `ArrayBufferHolder`.
*/
Expand Down

0 comments on commit 1ed7fdf

Please sign in to comment.