Skip to content

Instantly share code, notes, and snippets.

@rbehrends
Created May 4, 2014 22:37
Show Gist options
  • Save rbehrends/ab31743265488801cf02 to your computer and use it in GitHub Desktop.
Save rbehrends/ab31743265488801cf02 to your computer and use it in GitHub Desktop.
proc allocAligned(size, alignment: int): tuple[base, start: pointer] =
assert((alignment and (alignment - 1)) == 0) # Power of 2?
var address = alloc0(size+alignment)
if (cast[int](address) and (alignment - 1)) == 0:
(address, address)
else:
let offset = alignment - (cast[int](address) and (alignment - 1))
(address, cast[pointer](cast[int](address) + offset))
when isMainModule:
echo repr(allocAligned(128, 4))
echo repr(allocAligned(1024, 256))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment