Skip to content

Instantly share code, notes, and snippets.

@ebb
Created February 21, 2018 16:57
Show Gist options
  • Save ebb/ffc40965027bed3cf3464f1e2b45e206 to your computer and use it in GitHub Desktop.
Save ebb/ffc40965027bed3cf3464f1e2b45e206 to your computer and use it in GitHub Desktop.
Define (heap_alloc align size)
Begin {
Let start
Let start (Fetch uint32 heap_top)
In
Cond {
| [align > 4]
Let c [[align / 4] - 1]
In
(and [start + c] (complement c))
| True
start
}
In
Begin {
Let limit (Fetch uint32 heap_limit)
In
When Or [start > limit] [size > [4 * [limit - start]]] {
(die "Failed to allocate memory.")
}
}
(Store uint32 heap_top [start + [[size + 3] / 4]])
start
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment