Dead simple frame allocator

Looks for the first free page frame in the bitmap and returns it
This commit is contained in:
Eryn Wells 2016-04-14 01:38:36 -04:00
parent 1e02f3779d
commit e92718cad0
2 changed files with 37 additions and 0 deletions

View file

@ -47,6 +47,25 @@ FrameAllocator::initialize(const StartupInformation& startupInformation)
reserveRange(startupInformation.kernelStart, startupInformation.kernelSize() + mBitmapSize);
}
void*
FrameAllocator::allocate()
{
// Find the first bitmap with a free slot, and the first free slot, and return it.
const u32 pagesPerBitmap = Bitmap::length;
for (usize i = 0; i < mNumberOfPages; i += pagesPerBitmap) {
auto bitmap = mBitmap[i];
if (!bitmap.isFull()) {
for (usize j = 0; j < pagesPerBitmap; j++) {
if (!bitmap.isSet(j)) {
bitmap.set(j);
return addressOfPage(i * pagesPerBitmap + j);
}
}
}
}
return nullptr;
}
void
FrameAllocator::reserveRange(u32 start,
u32 length)
@ -88,4 +107,11 @@ FrameAllocator::reserveRange(u32 start,
}
}
inline void*
FrameAllocator::addressOfPage(usize page)
const
{
return reinterpret_cast<void*>(page * memory::pageSize);
}
} /* namespace kernel */

View file

@ -21,6 +21,14 @@ struct FrameAllocator
void initialize(const StartupInformation& startupInformation);
/**
* Allocate a page. Find a free page, mark it in use, and return its
* address.
*
* @return The base address of a free page, or 0 if no page could be allocated.
*/
void* allocate();
private:
typedef kstd::Bitmap<u8> Bitmap;
@ -33,6 +41,9 @@ private:
/** Reserve a range of memory. */
void reserveRange(u32 start, u32 length);
/** Return the physical memory address of `page`. */
void* addressOfPage(usize page) const;
};
} /* namespace */