diff options
| author | Abdellah El Morabit <nsrddyn@gmail.com> | 2025-11-29 23:45:57 +0100 |
|---|---|---|
| committer | Abdellah El Morabit <nsrddyn@gmail.com> | 2025-11-29 23:45:57 +0100 |
| commit | b0534fe076f14981428b48769964b4053e3a3a77 (patch) | |
| tree | 499b845cfab34d8fd8fb5cb417b9b5ff63b94ba7 /src/main/scala | |
| parent | 0c5e2d23133fcff99eab74662091af1a69f6084b (diff) | |
checkpoint: attempt at implementing more functional programming in the MemoryAllocater class
*a checkpoint is a save of the code when it still has bugs or issues
Diffstat (limited to 'src/main/scala')
| -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()) } |
