Skip to content

Commit 6205f75

Browse files
committed
new memory find algo
1 parent 48c45de commit 6205f75

File tree

4 files changed

+39
-36
lines changed

4 files changed

+39
-36
lines changed

main.kar

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
help printr
2-
str f1 "Hello, World!"
3-
call println
4-
xlit x1 0.2
5-
xlit x2 3.14
6-
xadd x1 x2
7-
cpy r5 f1
8-
call strings.cheekyfloat
1+
lit g1 1
2+
lit r4 0
3+
lit f1 10
4+
cpy f1 g4
5+
lit g5 2
6+
call arrays.create
7+
8+
lt r4 g4
9+
peek f1
10+
add g1 r4
11+
mul r4 g5
12+
cpy r4 f2
13+
call arrays.append
14+
jnz 8 i3
15+
16+
lit g1 3
17+
load g1 g2
18+
printr g2
Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
11
package helpers
22

3+
import MEMORY_LIMIT
4+
import data.memory.MemoryAddress
35
import errors
46
import internalMemory
7+
import kotlin.system.exitProcess
58

69

710
// should work with writeClosestString
811
fun findFreeMemory(size: Long): Long {
9-
val possibleStarts = emptyMap<Long?, Any?>().toMutableMap()
12+
var currentAddress = 0L
13+
var freeCount = 0L
1014

11-
internalMemory.memory.forEach {
12-
possibleStarts[it.key.address] = it.value.value
13-
}
14-
possibleStarts.filter { it.value == 0 }
15-
16-
// Find a SPOT for a starting address for the string
17-
var spot: Long? = null
18-
for (i in possibleStarts.keys) {
19-
var count = 0L
20-
for (j in 0L..size) {
21-
if (possibleStarts[i!! + j] == null) {
22-
count++
15+
while (currentAddress + size <= MEMORY_LIMIT) {
16+
if (internalMemory.memory[MemoryAddress(currentAddress)]?.value == null) {
17+
freeCount++
18+
if (freeCount == size + 1L) {
19+
return currentAddress - size
2320
}
21+
} else {
22+
freeCount = 0L
2423
}
25-
if (count == size + 1L) {
26-
spot = i
27-
break
28-
}
24+
currentAddress++
2925
}
3026

31-
if (spot == null) {
32-
errors.MemoryAllocationException("Could not allocate memory")
33-
}
34-
return spot!!
35-
}
27+
errors.MemoryAllocationException("Could not allocate memory")
28+
exitProcess(1)
29+
}

src/main/resources/lib/arrays/append.lib

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// f2 = value
33
lit s1 27
44
call arrays.size
5-
cpy f1 s1
6-
pop s2
7-
mov f2 s3
5+
cpy f1 s2
6+
pop s3
7+
mov f2 s4
88
syscall
99

src/main/resources/lib/arrays/get.lib

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
21
lit s1 28
3-
mov f1 s1
4-
mov f2 s2
2+
mov f1 s2 // array location
3+
mov f2 s3 // array index
54
syscall
65
push r2

0 commit comments

Comments
 (0)