mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 18:37:34 +00:00
Kernel: Support best fit allocation policy in kmalloc()
Add find_best_fit() which implements best fit allocation algorithm. Kmalloc now uses a best fit allocation policy for large allocations.
This commit is contained in:
parent
0d08ed2693
commit
c0a4cf5e8d
2 changed files with 39 additions and 1 deletions
29
AK/Bitmap.h
29
AK/Bitmap.h
|
@ -285,6 +285,35 @@ public:
|
|||
return {};
|
||||
}
|
||||
|
||||
Optional<size_t> find_best_fit(size_t minimum_length) const
|
||||
{
|
||||
size_t start = 0;
|
||||
size_t best_region_start = 0;
|
||||
size_t best_region_size = max_size;
|
||||
bool found = false;
|
||||
|
||||
while (true) {
|
||||
// Look for the next block which is bigger than requested length.
|
||||
auto length_of_found_range = find_next_range_of_unset_bits(start, minimum_length, best_region_size);
|
||||
if (length_of_found_range.has_value()) {
|
||||
if (best_region_size > length_of_found_range.value() || !found) {
|
||||
best_region_start = start;
|
||||
best_region_size = length_of_found_range.value();
|
||||
found = true;
|
||||
}
|
||||
start += length_of_found_range.value();
|
||||
} else {
|
||||
// There are no ranges which can fit requested length.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found) {
|
||||
return best_region_start;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Bitmap()
|
||||
: m_size(0)
|
||||
, m_owned(true)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue