implemented realloc as well as a bunch of fixes and some small changes like changing the order of arguments for !, making alloc drop etc.
This commit is contained in:
@@ -3,7 +3,7 @@ import ../stdlib/io.sl
|
||||
import ../stdlib/mem.sl
|
||||
|
||||
word test-mem-alloc
|
||||
4096 alloc dup 1337 swap ! # allocate 4096 bytes, store 1337 at start
|
||||
4096 alloc dup 1337 ! # allocate 4096 bytes, store 1337 at start
|
||||
dup @ puti cr # print value at start
|
||||
4096 free # free the memory
|
||||
end
|
||||
@@ -16,9 +16,9 @@ end
|
||||
word main
|
||||
32 alloc # allocate 32 bytes (enough for a Point struct)
|
||||
dup
|
||||
dup 111 swap Point.x!
|
||||
dup 222 swap Point.y!
|
||||
dup Point.x@ puti cr
|
||||
Point.y@ puti cr
|
||||
dup 111 Point.x! # store 111 at offset 0 (Point.x)
|
||||
dup 222 Point.y! # store 222 at offset 8 (Point.y)
|
||||
dup Point.x@ puti cr # print x
|
||||
dup Point.y@ puti cr # print y
|
||||
32 free # free the memory
|
||||
end
|
||||
|
||||
@@ -66,7 +66,7 @@ end
|
||||
|
||||
word test-store
|
||||
mem-slot dup
|
||||
123 swap !
|
||||
123 !
|
||||
@ puti cr
|
||||
end
|
||||
|
||||
@@ -80,7 +80,7 @@ word test-mmap
|
||||
0 # offset
|
||||
mmap
|
||||
dup
|
||||
1337 swap !
|
||||
1337 !
|
||||
dup
|
||||
@ puti cr
|
||||
4096 munmap drop
|
||||
@@ -132,10 +132,10 @@ end
|
||||
|
||||
word test-struct
|
||||
mem-slot
|
||||
dup 111 swap Point.x!
|
||||
dup 222 swap Point.y!
|
||||
dup Point.x@ puti cr
|
||||
Point.y@ puti cr
|
||||
dup 111 ! # Point.x = 111
|
||||
dup 8 + 222 ! # Point.y = 222
|
||||
dup @ puti cr # print Point.x
|
||||
dup 8 + @ puti cr # print Point.y
|
||||
Point.size puti cr
|
||||
end
|
||||
|
||||
|
||||
@@ -1,2 +1,9 @@
|
||||
5
|
||||
6
|
||||
6
|
||||
aaaaaaaaaa
|
||||
111
|
||||
222
|
||||
111
|
||||
222
|
||||
333
|
||||
444
|
||||
|
||||
36
tests/mem.sl
36
tests/mem.sl
@@ -1,9 +1,39 @@
|
||||
import ../stdlib/stdlib.sl
|
||||
import ../stdlib/io.sl
|
||||
|
||||
word main
|
||||
mem 5 swap !
|
||||
mem 8 + 6 swap !
|
||||
mem 5 !
|
||||
mem 8 + 6 !
|
||||
mem @ puti cr
|
||||
mem 8 + @ puti cr
|
||||
|
||||
10 alloc
|
||||
dup
|
||||
10 97 memset
|
||||
10 2dup puts
|
||||
free
|
||||
|
||||
|
||||
# Test realloc: allocate 16 bytes, fill with data, then grow to 32 bytes
|
||||
16 alloc
|
||||
dup 111 ! # Store 111 at offset 0
|
||||
dup 8 + 222 ! # Store 222 at offset 8
|
||||
|
||||
# Realloc to 32 bytes
|
||||
16 32 realloc # ( addr old_len=16 new_len=32 ) -> ( new_addr )
|
||||
|
||||
# Verify old data is preserved
|
||||
dup @ puti cr # Should print 111
|
||||
dup 8 + @ puti cr # Should print 222
|
||||
|
||||
# Write new data to the expanded region
|
||||
dup 16 + 333 ! # Store 333 at offset 16
|
||||
dup 24 + 444 ! # Store 444 at offset 24
|
||||
|
||||
# Print all values to verify
|
||||
dup @ puti cr # 111
|
||||
dup 8 + @ puti cr # 222
|
||||
dup 16 + @ puti cr # 333
|
||||
dup 24 + @ puti cr # 444
|
||||
|
||||
32 free
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user