memory free

main
Ashelyn Dawn 11 months ago
parent 299190d76f
commit d4f5abbf38

@ -3,7 +3,7 @@
<body>
<script type="text/javascript">
WebAssembly.instantiateStreaming(fetch("./output.wasm"), {
wasi_unstable: {
wasi_snapshot_preview1: {
proc_exit: (i) => {throw new Error('err ' + i)},
fd_write: (a, b, c, d) => console.log('writing something')
}

@ -7,6 +7,7 @@
call $memory_init
;; allocate string location
i32.const 13
call $memory_alloc
local.set $stringLocation
@ -22,6 +23,10 @@
i32.const 13 ;; length
call $print
;; free string
local.get $stringLocation
call $memory_free
call $proc_exit
unreachable
)

@ -1,5 +1,4 @@
;; Currently using a hard-coded struct at location 0
(import "wasi_unstable" "fd_write" (func $fd_write (param $file_descriptor i32) (param $io_vectors i32) (param $io_vecnum i32) (param $num_written_ptr i32) (result i32)))
(import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param $file_descriptor i32) (param $io_vectors i32) (param $io_vecnum i32) (param $num_written_ptr i32) (result i32)))
(func $print (param $location i32) (param $length i32) (result i32)
(local $structLoc i32)
@ -35,10 +34,19 @@
call $fd_write
local.tee $resultCode
;; if 0 code, make sure all bytes were written
local.get $structLoc ;; free calling struct
call $memory_free
local.get $resultLoc ;; get bytes written on stack
i32.load
local.get $resultLoc ;; free bytes written location
call $memory_free
local.set $resultLoc ;; put in result variable
;; if code was 0, make sure all bytes were written
i32.eqz
if
local.get $resultLoc
local.get $resultLoc ;; should be value, not pointer any more
local.get $length
i32.lt_u
if

@ -16,6 +16,8 @@
call $memory_markBlockSize
)
;; this runs into issue when allocating the final block in memory
;; our end spot is not aligned with our block size rounding
(func $memory_alloc (export "alloc") (param $requestedBytes i32) (result i32)
(local $blockAddr i32)
local.get $requestedBytes
@ -45,6 +47,87 @@
return
)
(func $memory_free (export "free_block") (param $pointer i32)
(local $startPointer i32)
(local $blockSize i32)
(local $temp i32)
;; start with the direct block
local.get $pointer
i32.const 4
i32.sub
local.tee $startPointer
call $memory_getUsableSizeOfBlock
i32.const 8
i32.add
local.set $blockSize
;; check block before
local.get $startPointer
i32.const 4
i32.sub
local.tee $temp
call $memory_isBlockUsed
i32.eqz
local.get $temp
i32.const 4 ;; start of allocateable memory
i32.ge_u
i32.and
if
;; update start of block
local.get $temp
local.get $temp
call $memory_getUsableSizeOfBlock
i32.const 4
i32.add
i32.sub
local.tee $startPointer
;; update size
call $memory_getUsableSizeOfBlock
local.get $blockSize
i32.const 8
i32.add
i32.add
local.set $blockSize
end
;; check block after
local.get $startPointer
local.get $blockSize
i32.add
local.tee $temp
memory.size ;; first make sure it's inside memory
i32.const 16
i32.shl
i32.lt_u
if
;; check if not used
local.get $temp
call $memory_isBlockUsed
i32.eqz
if
;; update size
local.get $temp
call $memory_getUsableSizeOfBlock
local.get $blockSize
i32.const 8
i32.add
i32.add
local.set $blockSize
end
end
;; re-init block
local.get $startPointer
local.get $blockSize
call $memory_markBlockSize
)
;; returns address of boundary word for first free block
(func $memory_findFreeBlock (export "findFree") (param $minimumBytes i32) (result i32)
(local $memorySize i32)

Loading…
Cancel
Save