Skip to content

Commit

Permalink
Add alignment helpers to core.memory
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTheFoxgirl committed Dec 26, 2024
1 parent 1d48b3c commit 408ed93
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions source/numem/core/memory/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,25 @@ T move(T)(scope ref return T from) {
*/
void copyTo(T)(ref T from, ref T to) {
__copy(from, to);
}

/**
Returns `bytes` aligned to a multiple of `alignment`.
*/
pragma(inline, true)
size_t alignSize(size_t bytes, size_t alignment) {
return bytes+(bytes%alignment);
}

/**
Aligns `bytes` to the closest memory page boundary.
If page-size is unknown, aligns to increments of `fallback`.
Returns:
New byte count aligned to page size.
*/
size_t alignToPage(size_t bytes, size_t fallback=4096) {
import numem.core.system : sysGetPageSize;
size_t pageSize = sysGetPageSize();
return alignSize(bytes, pageSize == 1 ? fallback : pageSize);
}

0 comments on commit 408ed93

Please sign in to comment.