-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Properly initialize malloc free list (#700)
- Loading branch information
Showing
3 changed files
with
49 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* grainc-flags --compilation-mode=runtime */ | ||
|
||
import Malloc from "runtime/malloc" | ||
import WasmI32, { add as (+), sub as (-), mul as (*), eq as (==), ne as (!=) } from "runtime/unsafe/wasmi32" | ||
|
||
primitive assert : Bool -> Void = "@assert" | ||
primitive ignore : a -> Void = "@ignore" | ||
|
||
// Because we're in runtime mode, malloc has yet to be called | ||
// This provides us with a clean slate to test malloc | ||
|
||
// allow malloc to initialize | ||
ignore(Malloc.malloc(8n)) | ||
|
||
// The free list should be pointing at the base | ||
let base = Malloc.getFreePtr() | ||
assert base == Malloc._RESERVED_RUNTIME_SPACE | ||
|
||
// grab the next (and only) block in the free list | ||
let block = WasmI32.load(Malloc.getFreePtr(), 0n) | ||
assert WasmI32.load(block, 0n) == base | ||
|
||
// When we allocate, an extra 8 bytes is reserved for the block header | ||
let remainingMemory = WasmI32.load(block, 4n) - 8n | ||
|
||
let firstPtr = Malloc.malloc(remainingMemory) | ||
let secondPtr = Malloc.malloc(remainingMemory) | ||
|
||
// These two pointers should (obviously) be different | ||
assert firstPtr != secondPtr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters