/* Copyright (C) 2025 Aryadev Chavali
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License Version 2
* for details.
* You may distribute and modify this code under the terms of the
* GNU General Public License Version 2, which you should have received a copy
of along with this
* program. If not, please go to .
* Created: 2025-05-12
* Description:
*/
#include
#include
// Allocates against stable memory i.e. we can have pointers of this lying
// around without any fear of them being thrown away.
void *context_alloc(context_t *context, u64 size)
{
return arena_alloc(&context->memory, size);
}
// Allocate against a "scratch space", separate from main memory, for internal
// use.
void *context_salloc(context_t *context, u64 size)
{
return arena_alloc(&context->scratch, size);
}
void context_reset_read(context_t *context)
{
arena_reset(&context->read);
}
void context_reset_scratch(context_t *context)
{
arena_reset(&context->scratch);
}
void context_reset(context_t *context)
{
arena_reset(&context->memory);
arena_reset(&context->read);
arena_reset(&context->scratch);
}
void context_cleanup(context_t *context)
{
if (!context)
return;
arena_cleanup(&context->memory);
arena_cleanup(&context->read);
arena_cleanup(&context->scratch);
memset(context, 0, sizeof(*context));
}
void context_report(context_t *context)
{
#if DEBUG
// Figure this out at runtime
u64 mem_used = 0, mem_cap = 0;
for (page_t *page = context->memory.start; page; page = page->next)
{
mem_used += page->size;
mem_cap += page->capacity;
}
u64 read_used = 0, read_cap = 0;
for (page_t *page = context->read.start; page; page = page->next)
{
read_used += page->size;
read_cap += page->capacity;
}
u64 scr_used = 0, scr_cap = 0;
for (page_t *page = context->scratch.start; page; page = page->next)
{
scr_used += page->size;
scr_cap += page->capacity;
}
info(": %luB/%luB main memory used\n", mem_used, mem_cap);
info(": %luB/%luB read space used\n", read_used, read_cap);
info(": %luB/%luB scratch space used\n", scr_used, scr_cap);
#endif
}