diff options
| -rw-r--r-- | src/main/scala/main/domain/MemoryOperations.scala | 46 |
1 files changed, 16 insertions, 30 deletions
diff --git a/src/main/scala/main/domain/MemoryOperations.scala b/src/main/scala/main/domain/MemoryOperations.scala index b58968f..8c2be88 100644 --- a/src/main/scala/main/domain/MemoryOperations.scala +++ b/src/main/scala/main/domain/MemoryOperations.scala @@ -6,24 +6,8 @@ import scala.util.hashing.MurmurHash3 import scala.collection.immutable.ListSet import scala.math._ import java.lang.foreign._ -import java.lang.foreign.MemorySegment -import java.lang.foreign.ValueLayout import scala.util.Using - - -/** - * Large memory object to allocate on the heap - * This is an implementation by me - * A method to do this bassically an Array with large bytes allocated to it - * */ - -// class MemoryObject { -// -// // TODO: create a large sized object -// -// } - /* * Using java.nio.ByteBuffer * Allocate memory then free memory @@ -35,21 +19,23 @@ import scala.util.Using * https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/foreign/package-summary.html * */ -object MemoryAllocater { +class MemoryAllocater { + + private val address: String = null + private val arena: Arena = Arena.global() + private val memorySegment = arena.allocate(1024 * 1024 * 1024) + + /** + * adding an offset so that the next piece of memory wont be overlapping with the one we just assigned + * */ + + def setValues(values: List[Int]): Unit = { - def run(): Unit = { + for i <- 0 until 10 do memorySegment.set(ValueLayout.JAVA_INT, offset * i, List[i]) + } - val arena: Arena = Arena.ofConfined() + def getValues: Unit = (0 until 10).map(i => memorySegment.get(ValueLayout.JAVA_INT, offset * i)) + def deallocateMemory: Unit = arena.close() - // I think this should be around one 1Gb of data - val memorySegment = arena.allocate((10 * 1024 * 1024) * 100) - // not memseg.setAtIndex like in the docs :( - /** - * adding an offset so that the next piece of memory wont be overlapping with the one we just assigned - * */ - val offset: Int = 4 - for i <- 0 until 10 do memorySegment.set(ValueLayout.JAVA_INT, offset * i, i) - for i <- 0 until 10 do println("Index : " + memorySegment.get(ValueLayout.JAVA_INT, offset * i)) - - } + def run(): Unit = println("address: " + memorySegment.address()) } |
