Scoop API

scoop:scoop.platform:0.1.0

src/memory/native_byte_borrow.scoop

Selected production source · src/memory/native_byte_borrow.scoop

1package scoop.platform.memory
3/**
4 * An explicit native-buffer borrow retaining its owner until completion. It must be completed
5 * exactly once after all native access ends.
6 */
7public 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: Boolean
15) {
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 is
21 * invalid.
22 */
23 @Unsafe
24 public fun pointer(): Ptr<Byte>? {
25 if (!active.load()) throw IllegalStateException(Some("buffer borrow has ended"))
26 return address
27 }
29 /**
30 * Ends the borrow and advances the appropriate owner cursor by a valid consumed/produced
31 * count. Repeated completion is rejected.
32 */
33 @Unsafe
34 public fun complete(count: Long) {
35 if (!active.compareAndSet(true, false)) throw IllegalStateException(Some("buffer borrow has ended"))
36 val buffer = owner!!
37 owner = None
38 buffer.finishBorrow(count, length, writable)
39 }