Add A1 memory allocator

This commit is contained in:
josh committed 2023-01-01 22:02:48 -05:00
1 parent 103205bff2
commit 7b09c9beed
3 files changed
+58 -1

No files matched your search

+51
View File
@@ -0,0 +1,51 @@
/**
* A1 memory allocator.
*/
module hulk.hurl.a1;
import hulk.hurl;
import hulk.hippo;
/**
* The A1 memory allocator is a one-shot memory allocator for kernel memory
* that is allocated but never freed.
*/
struct A1
{
/**
* Number of bytes allocated in the A1 region so far.
*/
private static __gshared size_t allocated;
/**
* Allocate memory.
*
* @param size
* Size of memory to allocate.
*
* @return Address of allocated memory. This address will always be aligned
* to a multiple of 16 bytes.
*/
public static void * allocate(size_t size)
{
/* Round size up to a multiple of 16. */
size = (size + 0xFu) & ~0xFu;
ulong address = Hurl.A1_BASE + allocated;
ulong mapped_limit = (address + 0xFFFu) & ~0xFFFu;
allocated += size;
ulong desired_limit = (Hurl.A1_BASE + allocated + 0xFFFu) & ~0xFFFu;
while (desired_limit > mapped_limit)
{
void * page = Hippo.allocate_page();
Hurl.map(Hurl.A1_BASE + mapped_limit, page, PT_WRITABLE);
mapped_limit += PAGE_SIZE;
}
return cast(void *)address;
}
}