scoop:scoop.platform:0.1.0
src/memory/native_byte_borrow.scoop
Selected production source · src/memory/native_byte_borrow.scoop
1
package scoop.platform.memory3
/**4
* An explicit native-buffer borrow retaining its owner until completion. It must be completed5
* exactly once after all native access ends.6
*/7
public class NativeByteBorrow internal constructor(8
owner: NativeByteBuffer,9
private val address: Ptr<Byte>?,10
/**11
* The maximum number of bytes covered by this borrow.12
*/13
public val length: Long,14
private val writable: Boolean15
) {16
private var owner: NativeByteBuffer? = Some(owner)17
private val active: AtomicBoolean = AtomicBoolean(true)19
/**20
* Returns the borrowed native address, or None for an empty region. Access after completion is21
* invalid.22
*/23
@Unsafe24
public fun pointer(): Ptr<Byte>? {25
if (!active.load()) throw IllegalStateException(Some("buffer borrow has ended"))26
return address27
}29
/**30
* Ends the borrow and advances the appropriate owner cursor by a valid consumed/produced31
* count. Repeated completion is rejected.32
*/33
@Unsafe34
public fun complete(count: Long) {35
if (!active.compareAndSet(true, false)) throw IllegalStateException(Some("buffer borrow has ended"))36
val buffer = owner!!37
owner = None38
buffer.finishBorrow(count, length, writable)39
}40
}