formatting

This commit is contained in:
jacob 2025-06-17 22:54:40 -05:00
parent 743738ac30
commit 0055626fe6

View File

@ -17,37 +17,34 @@ struct arena *arena_alloc(u64 reserve)
reserve += ARENA_BLOCK_SIZE - block_remainder; reserve += ARENA_BLOCK_SIZE - block_remainder;
} }
u8 *reserve_base = sys_memory_reserve(reserve + ARENA_HEADER_SIZE); u8 *base = sys_memory_reserve(reserve);
if (!reserve_base) { if (!base) {
/* Hard fail on memory reserve failure for now */ /* Hard fail on memory reserve failure for now */
sys_panic(LIT("Failed to reserve memory")); sys_panic(LIT("Failed to reserve memory"));
} }
u64 reserved = reserve; u64 reserved = reserve;
gstat_add(GSTAT_MEMORY_RESERVED, reserve + ARENA_HEADER_SIZE); gstat_add(GSTAT_MEMORY_RESERVED, reserve);
/* Commit initial block */ /* Commit initial block */
u8 *base = sys_memory_commit(reserve_base, ARENA_BLOCK_SIZE); base = sys_memory_commit(base, ARENA_BLOCK_SIZE);
if (!base) { if (!base) {
/* Hard fail on commit failure */ /* Hard fail on commit failure */
sys_panic(LIT("Failed to commit initial memory block: System may be out of memory")); sys_panic(LIT("Failed to commit initial memory block: System may be out of memory"));
} }
ASAN_POISON(base + sizeof(struct arena), ARENA_BLOCK_SIZE - sizeof(struct arena));
ASSERT(((u64)base & 0xFFF) == 0); /* Base should be 4k aligned */ ASSERT(((u64)base & 0xFFF) == 0); /* Base should be 4k aligned */
CT_ASSERT(ARENA_HEADER_SIZE <= ARENA_BLOCK_SIZE); /* Header must fit in first block */ CT_ASSERT(ARENA_HEADER_SIZE <= ARENA_BLOCK_SIZE); /* Header must fit in first block */
CT_ASSERT(sizeof(struct arena) <= ARENA_HEADER_SIZE); /* Arena struct must fit in header */ CT_ASSERT(sizeof(struct arena) <= ARENA_HEADER_SIZE); /* Arena struct must fit in header */
gstat_add(GSTAT_MEMORY_COMMITTED, ARENA_BLOCK_SIZE);
__profalloc(reserve_base, ARENA_BLOCK_SIZE);
/* Create arena struct at end of header block */ __profalloc(base, ARENA_BLOCK_SIZE);
ASAN_POISON(base + sizeof(struct arena), ARENA_BLOCK_SIZE - sizeof(struct arena));
gstat_add(GSTAT_MEMORY_COMMITTED, ARENA_BLOCK_SIZE);
gstat_add(GSTAT_NUM_ARENAS, 1);
/* Create & return arena header at beginning of block */
struct arena *arena = (struct arena *)base; struct arena *arena = (struct arena *)base;
MEMZERO_STRUCT(arena); MEMZERO_STRUCT(arena);
arena->committed = ARENA_BLOCK_SIZE - ARENA_HEADER_SIZE;
base += ARENA_HEADER_SIZE;
u64 committed = ARENA_BLOCK_SIZE - ARENA_HEADER_SIZE;
gstat_add(GSTAT_NUM_ARENAS, 1);
arena->committed = committed;
arena->reserved = reserved; arena->reserved = reserved;
return arena; return arena;
} }
@ -69,9 +66,10 @@ void *arena_push_bytes_no_zero(struct arena *arena, u64 size, u64 align)
ASSERT(align > 0); ASSERT(align > 0);
ASSERT(!arena->readonly); ASSERT(!arena->readonly);
void *start = NULL; void *ptr = NULL;
u8 *base = arena_base(arena);
/* TODO: Remove this check? (Only here to avoid aligning when size = 0) */ /* Check to avoid aligning when size = 0 */
if (size > 0) { if (size > 0) {
u64 aligned_start_pos = (arena->pos + (align - 1)); u64 aligned_start_pos = (arena->pos + (align - 1));
aligned_start_pos -= aligned_start_pos % align; aligned_start_pos -= aligned_start_pos % align;
@ -87,7 +85,7 @@ void *arena_push_bytes_no_zero(struct arena *arena, u64 size, u64 align)
/* Hard fail if we overflow reserved memory for now */ /* Hard fail if we overflow reserved memory for now */
sys_panic(LIT("Failed to commit new memory block: Overflow of reserved memory")); sys_panic(LIT("Failed to commit new memory block: Overflow of reserved memory"));
} }
void *commit_address = arena_base(arena) + arena->committed; void *commit_address = base + arena->committed;
if (!sys_memory_commit(commit_address, commit_bytes)) { if (!sys_memory_commit(commit_address, commit_bytes)) {
/* Hard fail on memory allocation failure for now */ /* Hard fail on memory allocation failure for now */
sys_panic(LIT("Failed to commit new memory block: System may be out of memory")); sys_panic(LIT("Failed to commit new memory block: System may be out of memory"));
@ -98,14 +96,15 @@ void *arena_push_bytes_no_zero(struct arena *arena, u64 size, u64 align)
__profalloc(arena->reserve_base, arena->committed + commit_bytes + ARENA_HEADER_SIZE); __profalloc(arena->reserve_base, arena->committed + commit_bytes + ARENA_HEADER_SIZE);
ASAN_POISON(commit_address, commit_bytes); ASAN_POISON(commit_address, commit_bytes);
} }
start = arena_base(arena) + aligned_start_pos;
ptr = base + aligned_start_pos;
ASAN_UNPOISON(ptr, new_pos - aligned_start_pos);
arena->pos = new_pos; arena->pos = new_pos;
ASAN_UNPOISON(start, (arena_base(arena) + arena->pos) - (u8 *)start);
} else { } else {
start = arena_base(arena) + arena->pos; ptr = base + arena->pos;
} }
return start; return ptr;
} }
/* Copies the memory from the source arena into the destination arena, /* Copies the memory from the source arena into the destination arena,
@ -121,21 +120,9 @@ void arena_copy_replace(struct arena *dst, struct arena *src)
void arena_decommit_unused_blocks(struct arena *arena) void arena_decommit_unused_blocks(struct arena *arena)
{ {
#if 0 /* Not implemented */
ASSERT(!arena->readonly); ASSERT(false);
u64 next_block_pos = ARENA_BLOCK_SIZE * ((arena->pos + (ARENA_BLOCK_SIZE - 1)) / ARENA_BLOCK_SIZE);
if (arena->committed > next_block_pos) {
u8 *decommit_start = arena_base(arena) + next_block_pos;
u64 decommit_size = (arena_base(arena) + arena->committed) - decommit_start;
sys_memory_decommit(decommit_start, decommit_size);
arena->committed = next_block_pos;
gstat_sub(GSTAT_MEMORY_COMMITTED, decommit_size);
}
#else
/* TODO */
ASSERT(false); /* Not implemented */
(UNUSED)arena; (UNUSED)arena;
#endif
} }
void arena_set_readonly(struct arena *arena) void arena_set_readonly(struct arena *arena)