From ea6f485bd905a155ee801044cb8e0e6a129d40c0 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 24 Feb 2023 21:15:33 -0500 Subject: [PATCH] Add hulk.util and round_up_power_2() --- src/hulk/hurl/a1.d | 8 +++++--- src/hulk/hurl/hurl.d | 2 +- src/hulk/util.d | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 src/hulk/util.d diff --git a/src/hulk/hurl/a1.d b/src/hulk/hurl/a1.d index bb9861c..df1d786 100644 --- a/src/hulk/hurl/a1.d +++ b/src/hulk/hurl/a1.d @@ -5,6 +5,8 @@ module hulk.hurl.a1; import hulk.hurl; import hulk.hippo; +import hulk.util; +import hulk.pagetable; /** * 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) { /* 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 mapped_limit = (address + 0xFFFu) & ~0xFFFu; + ulong mapped_limit = round_up_power_2(address, PAGE_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) { diff --git a/src/hulk/hurl/hurl.d b/src/hulk/hurl/hurl.d index 83ffae7..f78c51d 100644 --- a/src/hulk/hurl/hurl.d +++ b/src/hulk/hurl/hurl.d @@ -155,7 +155,7 @@ struct Hurl */ size_t usable_memory; 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 = [ [header.bootinfo.hulk_phys, LinkerAddresses.hulk_binary_size], [cast(ulong)header.bootinfo.fb.buffer, fb_size], diff --git a/src/hulk/util.d b/src/hulk/util.d new file mode 100644 index 0000000..f575912 --- /dev/null +++ b/src/hulk/util.d @@ -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); +}