Add hulk.util and round_up_power_2()

This commit is contained in:
Josh Holtrop 2023-02-24 21:15:33 -05:00
parent 2ab388f2e9
commit ea6f485bd9
3 changed files with 23 additions and 4 deletions

View File

@ -5,6 +5,8 @@ module hulk.hurl.a1;
import hulk.hurl; import hulk.hurl;
import hulk.hippo; import hulk.hippo;
import hulk.util;
import hulk.pagetable;
/** /**
* The A1 memory allocator is a one-shot memory allocator for kernel memory * The A1 memory allocator is a one-shot memory allocator for kernel memory
@ -29,15 +31,15 @@ struct A1
public static void * allocate(size_t size) public static void * allocate(size_t size)
{ {
/* Round size up to a multiple of 16. */ /* Round size up to a multiple of 16. */
size = (size + 0xFu) & ~0xFu; size = round_up_power_2(size, 16u);
ulong address = Hurl.A1_BASE + allocated; ulong address = Hurl.A1_BASE + allocated;
ulong mapped_limit = (address + 0xFFFu) & ~0xFFFu; ulong mapped_limit = round_up_power_2(address, PAGE_SIZE);
allocated += size; allocated += size;
ulong desired_limit = (Hurl.A1_BASE + allocated + 0xFFFu) & ~0xFFFu; ulong desired_limit = round_up_power_2(Hurl.A1_BASE + allocated, PAGE_SIZE);
while (desired_limit > mapped_limit) while (desired_limit > mapped_limit)
{ {

View File

@ -155,7 +155,7 @@ struct Hurl
*/ */
size_t usable_memory; size_t usable_memory;
size_t physical_address_limit; size_t physical_address_limit;
const(size_t) fb_size = (header.bootinfo.fb.height * header.bootinfo.fb.stride * 4u + PAGE_SIZE - 1u) & ~(PAGE_SIZE - 1u); const(size_t) fb_size = round_up_power_2(header.bootinfo.fb.height * header.bootinfo.fb.stride * 4u, PAGE_SIZE);
ulong[2][2] reserved = [ ulong[2][2] reserved = [
[header.bootinfo.hulk_phys, LinkerAddresses.hulk_binary_size], [header.bootinfo.hulk_phys, LinkerAddresses.hulk_binary_size],
[cast(ulong)header.bootinfo.fb.buffer, fb_size], [cast(ulong)header.bootinfo.fb.buffer, fb_size],

17
src/hulk/util.d Normal file
View File

@ -0,0 +1,17 @@
/**
* HULK utility functions.
*/
module hulk.util;
/**
* Round a value up to the next power of 2.
*
* @param v Value to round up.
* @param p Power of 2 to round up to.
*
* @return Rounded up value.
*/
T round_up_power_2(T)(T v, T p)
{
return (v + p - 1u) & ~(p - 1u);
}