jc/memory.jai
2025-05-11 02:29:36 -06:00

32 lines
729 B
Text

Kilobyte :: 1024;
Megabyte :: 1024 * Kilobyte;
Gigabyte :: 1024 * Megabyte;
Default_Align :: #run 2 * align_of(rawptr);
align_of :: ($T: Type) -> uint #expand {
return #run -> uint {
info := type_info(struct{ p: u8; t: T; });
return info.members[1].offset_in_bytes.(uint);
};
}
bitcast :: ($T: Type, expr: Code) -> T #expand {
value := expr;
return (*value).(*T).*;
}
power_of_two :: (x: uint) -> bool {
if x == 0 return false;
return x & (x - 1) == 0;
}
align_forward :: (ptr: uint, align: uint = Default_Align) -> uint {
basic.assert(power_of_two(align), "alignment must be a power of two");
p := ptr;
mod := p & (align - 1);
if mod != 0 then p += align - mod;
return p;
}