Skip to content

Commit af1ad91

Browse files
fix: arena closure checks and memory access
Signed-off-by: Akhilender Bongirwar <[email protected]>
1 parent 00930d1 commit af1ad91

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

kyo-offheap/native/src/main/java/lang/foreign/Arena.scala

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import scala.scalanative.unsafe.*
1111
final class Arena extends AutoCloseable:
1212
private val allocations = new ConcurrentLinkedQueue[MemorySegment]()
1313
@volatile private var _isClosed: Boolean = false
14+
def isClosed: Boolean = _isClosed
1415

1516
/** Allocates a block of off-heap memory of the given size (in bytes).
1617
*
@@ -36,8 +37,6 @@ final class Arena extends AutoCloseable:
3637
free(seg.ptr)
3738
seg = allocations.poll()
3839
end close
39-
40-
def isClosed: Boolean = _isClosed
4140
end Arena
4241

4342
object Arena:

kyo-offheap/native/src/main/java/lang/foreign/MemorySegment.scala

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import scala.scalanative.unsigned.*
1010
* This implementation wraps a pointer along with the allocated size in bytes. It also provides basic support for slicing and for
1111
* reading/writing primitive values, which are used by the Layout instances in the shared code.
1212
*/
13-
final class MemorySegment private (private[foreign] val ptr: Ptr[Byte], val byteSize: Long, private val arena: Arena):
14-
private def checkOpen(): Unit =
13+
final class MemorySegment private (private[foreign] val ptr: Ptr[Byte], val byteSize: Long, arena: Arena):
14+
private[MemorySegment] def checkOpen(): Unit =
1515
if arena.isClosed then
1616
throw new IllegalStateException("MemorySegment accessed after Arena was closed")
1717

@@ -154,6 +154,8 @@ object MemorySegment:
154154
* The number of bytes to copy.
155155
*/
156156
def copy(srcSegment: MemorySegment, srcOffset: Long, dstSegment: MemorySegment, dstOffset: Long, bytes: Long): Unit =
157+
srcSegment.checkOpen()
158+
dstSegment.checkOpen()
157159
require(srcOffset + bytes <= srcSegment.byteSize)
158160
require(dstOffset + bytes <= dstSegment.byteSize)
159161
val _ = memcpy(dstSegment.ptr + dstOffset, srcSegment.ptr + srcOffset, bytes.toCSize)

0 commit comments

Comments
 (0)