Yes, that's not completely-from-scratch. But let's be honest, am I going to do everything from scratch? Probably not. I'm not making my own bootloader. And making a proper smaller-than-4-KB allocator is not something I want to do. Plus, liballoc works perfectly in this rewrite, seeing as the MM code actually works, instead of leaking all your poor memory And liballoc_{lock, unlock} can be actually defined, since we have spinlocks here!
30 lines
679 B
C++
30 lines
679 B
C++
#include "memory/MemoryManager.h"
|
|
#include "thread/Spinlock.h"
|
|
|
|
#include <stddef.h>
|
|
|
|
Spinlock alloc_lock;
|
|
|
|
extern "C" int liballoc_lock()
|
|
{
|
|
alloc_lock.acquire();
|
|
return !alloc_lock.locked(); // Sanity check: the spinlock should be locked (return 0)
|
|
}
|
|
|
|
extern "C" int liballoc_unlock()
|
|
{
|
|
alloc_lock.release();
|
|
return 0; // No sanity check this time because another thread could have acquired the lock the instant we let go of
|
|
// it.
|
|
}
|
|
|
|
extern "C" void* liballoc_alloc(size_t count)
|
|
{
|
|
return MemoryManager::get_pages(count);
|
|
}
|
|
|
|
extern "C" int liballoc_free(void* addr, size_t count)
|
|
{
|
|
MemoryManager::release_pages(addr, count);
|
|
return 0;
|
|
} |