forceAlloc impl

This commit is contained in:
minjaesong
2025-04-07 21:10:19 +09:00
parent f30022bf66
commit 9ce7e3dfd2
3 changed files with 17 additions and 2 deletions

View File

@@ -16,7 +16,7 @@ sys.free(metaArea)
if (addrToLoad == 0)
addrToLoad = sys.malloc(imageSize + 4)
else
forceAlloc(addrToLoad, imageSize + 4)
sys.forceAlloc(addrToLoad, imageSize + 4)
// writes IMAGE_SIZE and the BINARY_IMAGE directly to the memory
infile.pread(addrToLoad, imageSize + 4, 8)

View File

@@ -45,6 +45,7 @@ class VM(
val memsize = minOf(USER_SPACE_SIZE, _memsize.toLong())
val MALLOC_UNIT = 64
val MALLOC_RESERVED_BLOCKS = 4 // 4*64=256 bytes are always reserved and won't be allocated to a pointer
private val mallocBlockSize = (memsize / MALLOC_UNIT).toInt()
internal val usermem = UnsafeHelper.allocate(memsize, this)
@@ -218,7 +219,7 @@ class VM(
}
private fun findEmptySpace(blockSize: Int): Int? {
var cursorHead = 0
var cursorHead = MALLOC_RESERVED_BLOCKS
var cursorTail: Int
val cursorHeadMaxInclusive = mallocBlockSize - blockSize
while (cursorHead <= cursorHeadMaxInclusive) {
@@ -261,6 +262,19 @@ class VM(
allocatedBlockCount -= count
}
internal fun forceAlloc(ptr: Int, size: Int) {
val allocBlocks = ceil(size.toDouble() / MALLOC_UNIT).toInt()
val blockStart = ptr / MALLOC_UNIT
var previouslyUnallocated = 0
for (i in blockStart until blockStart + allocBlocks) {
if (mallocMap.get(i) == false) previouslyUnallocated++
mallocMap.set(i, true)
}
allocatedBlockCount += previouslyUnallocated
mallocSizes[blockStart] = allocBlocks
}
internal data class VMNativePtr(val address: Int, val size: Int)
}

View File

@@ -82,6 +82,7 @@ class VMJSR223Delegate(private val vm: VM) {
fun nanoTime() = System.nanoTime()
fun malloc(size: Int) = vm.malloc(size)
fun free(ptr: Int) = vm.free(ptr)
fun forceAlloc(ptr: Int, size: Int) = vm.forceAlloc(ptr, size)
fun memcpy(from: Int, to: Int, len: Int) {
val from = from.toLong()
val to = to.toLong()