Kilobyte :: 1024; Megabyte :: 1024 * Kilobyte; Gigabyte :: 1024 * Megabyte; DefaultAlign :: #run 2 * align_of(*void); /// MemEqual checks the equality of two pieces of memory. /// /// Note: MemEqual will panic if size_in_bytes is negative. MemEqual :: (p1: *void, p2: *void, size_in_bytes: int) -> bool { if size_in_bytes < 0 { Panic("jc: size_in_bytes cannot be negative"); } return memcmp(p1, p2, size_in_bytes) == 0; // Provided by Preload } /// MemCopy copies the memory of src to dst. /// /// Note: MemCopy will panic if size_in_bytes is negative. MemCopy :: (dst: *void, src: *void, size_in_bytes: int) { if size_in_bytes < 0 { Panic("jc: size_in_bytes cannot be negative"); } memcpy(dst, src, size_in_bytes); // Provided by Preload } /// MemOverwrite overwites the memory of p with value. /// /// Note: MemOverwrite will panic if size_in_bytes is negative. MemOverwrite :: (p: *void, size_in_bytes: int, value: u8 = 0) { if size_in_bytes < 0 { Panic("jc: size_in_bytes cannot be negative"); } memset(p, value, size_in_bytes); // Provided by preload } /// MemZero zeroes the memory of p. /// /// Note: MemZero will panic if size_in_bytes is negative. MemZero :: (p: *void, size_in_bytes: int) { MemOverwrite(p, size_in_bytes, 0); } /// MemZero zeroes the memory of p. /// /// Note: MemZero will not call the initializer for aggregate types, /// so you may want MemReset instead. MemZero :: (p: *$T) { MemOverwrite(p, size_of(T), 0); } /// MemReset resets the memory of p, as if it was just instantiated. /// /// Note: MemReset will call the initializer for aggregate types, so you /// may want MemZero instead. MemReset :: (p: *$T) { initializer :: initializer_of(T); #if initializer { inline initializer(p); } else { inline MemZero(p); } } AlignUpwards :: (ptr: int, align: int = DefaultAlign) -> int { Assert(PowerOfTwo(align), "alignment must be a power of two"); p := ptr; mod := p & (align - 1); if mod != 0 then p += align - mod; return p; } PowerOfTwo :: (x: int) -> bool { if x == 0 return false; return x & (x - 1) == 0; } NextPowerOfTwo :: (x: int) -> int #no_aoc { Assert(PowerOfTwo(x), "value must be a power of two"); // Bit twiddling hacks next power of two x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x |= x >> 32; return x + 1; }