Skip to content

Instantly share code, notes, and snippets.

@Varriount
Created May 4, 2014 22:17
Show Gist options
  • Save Varriount/335018e9f5d9ed177743 to your computer and use it in GitHub Desktop.
Save Varriount/335018e9f5d9ed177743 to your computer and use it in GitHub Desktop.
Aligned Allocation
proc allocAligned(size, alignment: int): pointer =
## Allocates a block of memory whose address is aligned to `alignment`
## This is needed for the buffer accepted by ReadDirectoryChanges, which
## must be allocated on an int32 boundary.
var address = alloc0(size)
result = address
while (cast[int](address) mod alignment) != 0:
address = cast[pointer](cast[int](address)+1)
# Move the memory to the correct address if needed, and then resize it.
if address != result:
moveMem(result, address, size + (alignment*2))
result = address
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment