blob: cd7c614e7faed774105fdb6ba53036f5cd12cad2 [file] [log] [blame]
#include "alloc.h"
#include "dlog.h"
#include "spinlock.h"
static size_t alloc_base;
static size_t alloc_limit;
static struct spinlock alloc_lock = SPINLOCK_INIT;
/*
* Initializes the allocator.
*/
void halloc_init(size_t base, size_t size)
{
alloc_base = base;
alloc_limit = base + size;
}
/*
* Allocates the requested amount of memory. Return NULL when there isn't enough
* free memory.
*/
void *halloc(size_t size)
{
return halloc_aligned(size, 2 * sizeof(size_t));
}
/*
* Frees the provided memory.
*
* Currently unimplemented.
*/
void hfree(void *ptr)
{
dlog("Attempted to free pointer %p\n", ptr);
}
/*
* Allocates the requested amount of memory, with the requested alignment.
*
* Alignment must be a power of two. Returns NULL when there isn't enough free
* memory.
*/
void *halloc_aligned(size_t size, size_t align)
{
size_t begin;
size_t end;
sl_lock(&alloc_lock);
begin = (alloc_base + align - 1) & ~(align - 1);
end = begin + size;
/* Check for overflows, and that there is enough free mem. */
if (end > begin && begin >= alloc_base && end <= alloc_limit)
alloc_base = end;
else
begin = 0;
sl_unlock(&alloc_lock);
return (void *)begin;
}