summaryrefslogtreecommitdiff
path: root/src/main/scala
diff options
context:
space:
mode:
authornasr <nsrddyn@gmail.com>2025-12-01 21:37:29 +0100
committernasr <nsrddyn@gmail.com>2025-12-01 21:37:29 +0100
commit140b498027915ebef1e4934ee5d5ce54ae5bd228 (patch)
tree218be18b68003beb0ef738499b44315dd14c5415 /src/main/scala
parent2a49df00757b5c62332d79ee99e8db250a825aba (diff)
feature: arena error handling
Diffstat (limited to 'src/main/scala')
-rw-r--r--src/main/scala/main/domain/MemoryOperations.scala42
1 files changed, 39 insertions, 3 deletions
diff --git a/src/main/scala/main/domain/MemoryOperations.scala b/src/main/scala/main/domain/MemoryOperations.scala
index c0b1cbf..81feff9 100644
--- a/src/main/scala/main/domain/MemoryOperations.scala
+++ b/src/main/scala/main/domain/MemoryOperations.scala
@@ -19,7 +19,8 @@ import scala.util.Using
* https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/foreign/package-summary.html
* */
-case class Memory (arena: Arena, memorySegment: MemorySegment)
+
+
class MemoryAllocater {
@@ -32,9 +33,41 @@ class MemoryAllocater {
* adding an offset so that the next piece of memory wont be overlapping with the one we just assigned
* */
- def createMemorySegment: Memory = Memory ( arena = Arena.ofConfined(), memorySegment = arena.allocate(1024 * 1024 * 1024))
+ /**
+ * Creation of arena and memory segment within the arena
+ * if an arena already exists the method closes it and creates a new one
+ * */
+
+ def createMemorySegment: Unit = {
+ if arena == null || memorySegment == null then {
+
+ /**
+ * close the current arena to assign a new one
+ * trying to do this allot to stress the ram
+ *
+ * */
+
+ if arena != null then arena.close()
+ /**
+ *
+ * Assigning a global arena so multiple threads can access it
+ * for example when a thread tries to close the previous arena
+ *
+ * */
+ arena = Arena.global()
+ memorySegment = arena.allocate(1024L * 1024 * 1024) // 1 GiB
+
+ }
+
+ }
+
+ /**
+ * Checks if arena is created if it isn't call the createMemorySegment method
+ * Close the previous arena if there is one and initlizate values on a memory segment
+ * */
def setValues(values: List[Int], baseOffset: Int): Unit = {
+ if this.arena == null then createMemorySegment
val stride = Integer.BYTES.toLong
val start = baseOffset.toLong
@@ -46,6 +79,10 @@ class MemoryAllocater {
}
+ /**
+ * Retrieves the set values that were initialized on
+ * the off heap in the setValues method
+ * */
def getValues(offset: Int, len: Int): List[Int] = {
val stride = Integer.BYTES.toLong
@@ -56,7 +93,6 @@ class MemoryAllocater {
}.toList
}
- def deallocateMemory: Unit = if arena != null then arena.close()
def run(): Unit = println("address: " + memorySegment.address())