Add A1 memory allocator

This commit is contained in:
Josh Holtrop 2023-01-01 22:02:48 -05:00
parent 103205bff2
commit 7b09c9beed
3 changed files with 58 additions and 1 deletions

51
src/hulk/hurl/a1.d Normal file
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;
}
}

View File

@ -3,7 +3,7 @@
* *
* HURL provides virtual memory management for HULK. * HURL provides virtual memory management for HULK.
*/ */
module hulk.hurl; module hulk.hurl.hurl;
public import hulk.pagetable; public import hulk.pagetable;
import hulk.cpu; import hulk.cpu;
@ -25,6 +25,9 @@ struct Hurl
/** HULK framebuffer address. */ /** HULK framebuffer address. */
enum ulong HULK_FRAMEBUFFER = 0xFFFF_8001_0000_0000u; enum ulong HULK_FRAMEBUFFER = 0xFFFF_8001_0000_0000u;
/** A1 allocator range base address. */
enum ulong A1_BASE = 0xFFFF_8002_0000_0000u;
/** /**
* Pointer to the base page table. * Pointer to the base page table.
*/ */

3
src/hulk/hurl/package.d Normal file
View File

@ -0,0 +1,3 @@
module hulk.hurl;
public import hulk.hurl.hurl;