diff options
Diffstat (limited to 'src/main/scala')
| -rw-r--r-- | src/main/scala/main/domain/MemoryOperations.scala | 42 |
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()) |
