Dead simple frame allocator
Looks for the first free page frame in the bitmap and returns it
This commit is contained in:
parent
1e02f3779d
commit
e92718cad0
2 changed files with 37 additions and 0 deletions
|
@ -47,6 +47,25 @@ FrameAllocator::initialize(const StartupInformation& startupInformation)
|
||||||
reserveRange(startupInformation.kernelStart, startupInformation.kernelSize() + mBitmapSize);
|
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
|
void
|
||||||
FrameAllocator::reserveRange(u32 start,
|
FrameAllocator::reserveRange(u32 start,
|
||||||
u32 length)
|
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 */
|
} /* namespace kernel */
|
||||||
|
|
|
@ -21,6 +21,14 @@ struct FrameAllocator
|
||||||
|
|
||||||
void initialize(const StartupInformation& startupInformation);
|
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:
|
private:
|
||||||
typedef kstd::Bitmap<u8> Bitmap;
|
typedef kstd::Bitmap<u8> Bitmap;
|
||||||
|
|
||||||
|
@ -33,6 +41,9 @@ private:
|
||||||
|
|
||||||
/** Reserve a range of memory. */
|
/** Reserve a range of memory. */
|
||||||
void reserveRange(u32 start, u32 length);
|
void reserveRange(u32 start, u32 length);
|
||||||
|
|
||||||
|
/** Return the physical memory address of `page`. */
|
||||||
|
void* addressOfPage(usize page) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
} /* namespace */
|
} /* namespace */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue