2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-05-17 03:40:15 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-01-19 13:18:27 +01:00
|
|
|
#include <AK/Traits.h>
|
2019-05-17 03:40:15 +02:00
|
|
|
#include <AK/Vector.h>
|
2020-10-31 17:12:23 -06:00
|
|
|
#include <Kernel/SpinLock.h>
|
2021-02-25 11:42:49 +01:00
|
|
|
#include <Kernel/VM/Range.h>
|
2019-05-17 03:40:15 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
2019-05-17 03:40:15 +02:00
|
|
|
class RangeAllocator {
|
|
|
|
|
public:
|
2020-01-17 23:05:37 +01:00
|
|
|
RangeAllocator();
|
2019-05-17 03:40:15 +02:00
|
|
|
~RangeAllocator();
|
|
|
|
|
|
2020-01-17 23:05:37 +01:00
|
|
|
void initialize_with_range(VirtualAddress, size_t);
|
|
|
|
|
void initialize_from_parent(const RangeAllocator&);
|
|
|
|
|
|
2021-01-27 21:01:45 +01:00
|
|
|
Optional<Range> allocate_anywhere(size_t, size_t alignment = PAGE_SIZE);
|
|
|
|
|
Optional<Range> allocate_specific(VirtualAddress, size_t);
|
2021-01-28 14:55:06 +01:00
|
|
|
Optional<Range> allocate_randomized(size_t, size_t alignment);
|
2021-01-27 21:01:45 +01:00
|
|
|
void deallocate(const Range&);
|
2019-05-17 03:40:15 +02:00
|
|
|
|
|
|
|
|
void dump() const;
|
|
|
|
|
|
2020-06-01 22:55:09 -06:00
|
|
|
bool contains(const Range& range) const
|
|
|
|
|
{
|
2020-10-31 17:12:23 -06:00
|
|
|
ScopedSpinLock lock(m_lock);
|
2020-06-01 22:55:09 -06:00
|
|
|
return m_total_range.contains(range);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 03:40:15 +02:00
|
|
|
private:
|
|
|
|
|
void carve_at_index(int, const Range&);
|
|
|
|
|
|
|
|
|
|
Vector<Range> m_available_ranges;
|
2020-01-19 13:18:27 +01:00
|
|
|
Range m_total_range;
|
2020-10-31 17:12:23 -06:00
|
|
|
mutable SpinLock<u8> m_lock;
|
2019-05-17 03:40:15 +02:00
|
|
|
};
|
2019-08-29 20:54:50 +02:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-19 13:18:27 +01:00
|
|
|
namespace AK {
|
|
|
|
|
template<>
|
2020-02-16 01:27:42 +01:00
|
|
|
struct Traits<Kernel::Range> : public GenericTraits<Kernel::Range> {
|
2020-01-19 13:18:27 +01:00
|
|
|
static constexpr bool is_trivial() { return true; }
|
|
|
|
|
};
|
|
|
|
|
}
|