mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 09:04:59 +00:00
Kernel: Move {Virtual,Physical}Address classes to the Memory directory
This commit is contained in:
parent
64af4953c2
commit
aaa1de7878
54 changed files with 61 additions and 59 deletions
|
@ -9,8 +9,8 @@
|
|||
#include <Kernel/Memory/AllocationStrategy.h>
|
||||
#include <Kernel/Memory/MemoryManager.h>
|
||||
#include <Kernel/Memory/PageFaultResponse.h>
|
||||
#include <Kernel/Memory/PhysicalAddress.h>
|
||||
#include <Kernel/Memory/VMObject.h>
|
||||
#include <Kernel/PhysicalAddress.h>
|
||||
|
||||
namespace Kernel::Memory {
|
||||
|
||||
|
|
|
@ -7,8 +7,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <Kernel/Memory/PhysicalAddress.h>
|
||||
#include <Kernel/Memory/Region.h>
|
||||
#include <Kernel/PhysicalAddress.h>
|
||||
|
||||
namespace Kernel::Memory {
|
||||
|
||||
|
|
68
Kernel/Memory/PhysicalAddress.h
Normal file
68
Kernel/Memory/PhysicalAddress.h
Normal file
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Checked.h>
|
||||
#include <AK/Format.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
typedef u64 PhysicalPtr;
|
||||
typedef u64 PhysicalSize;
|
||||
|
||||
class PhysicalAddress {
|
||||
public:
|
||||
ALWAYS_INLINE static PhysicalPtr physical_page_base(PhysicalPtr page_address) { return page_address & ~(PhysicalPtr)0xfff; }
|
||||
ALWAYS_INLINE static size_t physical_page_index(PhysicalPtr page_address)
|
||||
{
|
||||
auto page_index = page_address >> 12;
|
||||
if constexpr (sizeof(size_t) < sizeof(PhysicalPtr))
|
||||
VERIFY(!(page_index & ~(PhysicalPtr)((size_t)-1)));
|
||||
return (size_t)(page_index);
|
||||
}
|
||||
|
||||
PhysicalAddress() = default;
|
||||
explicit PhysicalAddress(PhysicalPtr address)
|
||||
: m_address(address)
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] PhysicalAddress offset(PhysicalPtr o) const { return PhysicalAddress(m_address + o); }
|
||||
[[nodiscard]] bool offset_addition_would_overflow(PhysicalPtr o) const { return Checked<PhysicalPtr>::addition_would_overflow(m_address, o); }
|
||||
[[nodiscard]] PhysicalPtr get() const { return m_address; }
|
||||
void set(PhysicalPtr address) { m_address = address; }
|
||||
void mask(PhysicalPtr m) { m_address &= m; }
|
||||
|
||||
[[nodiscard]] bool is_null() const { return m_address == 0; }
|
||||
|
||||
// NOLINTNEXTLINE(readability-make-member-function-const) const PhysicalAddress shouldn't be allowed to modify the underlying memory
|
||||
[[nodiscard]] u8* as_ptr() { return reinterpret_cast<u8*>(m_address); }
|
||||
[[nodiscard]] u8 const* as_ptr() const { return reinterpret_cast<u8 const*>(m_address); }
|
||||
|
||||
[[nodiscard]] PhysicalAddress page_base() const { return PhysicalAddress(physical_page_base(m_address)); }
|
||||
[[nodiscard]] PhysicalPtr offset_in_page() const { return PhysicalAddress(m_address & 0xfff).get(); }
|
||||
|
||||
bool operator==(PhysicalAddress const& other) const { return m_address == other.m_address; }
|
||||
bool operator!=(PhysicalAddress const& other) const { return m_address != other.m_address; }
|
||||
bool operator>(PhysicalAddress const& other) const { return m_address > other.m_address; }
|
||||
bool operator>=(PhysicalAddress const& other) const { return m_address >= other.m_address; }
|
||||
bool operator<(PhysicalAddress const& other) const { return m_address < other.m_address; }
|
||||
bool operator<=(PhysicalAddress const& other) const { return m_address <= other.m_address; }
|
||||
|
||||
private:
|
||||
PhysicalPtr m_address { 0 };
|
||||
};
|
||||
|
||||
template<>
|
||||
struct AK::Formatter<PhysicalAddress> : AK::Formatter<FormatString> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, PhysicalAddress value)
|
||||
{
|
||||
if constexpr (sizeof(PhysicalPtr) == sizeof(u64))
|
||||
return AK::Formatter<FormatString>::format(builder, "P{:016x}"sv, value.get());
|
||||
else
|
||||
return AK::Formatter<FormatString>::format(builder, "P{}"sv, value.as_ptr());
|
||||
}
|
||||
};
|
|
@ -7,7 +7,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <Kernel/PhysicalAddress.h>
|
||||
#include <Kernel/Memory/PhysicalAddress.h>
|
||||
|
||||
namespace Kernel::Memory {
|
||||
|
||||
|
|
|
@ -10,8 +10,8 @@
|
|||
#include <AK/IntrusiveRedBlackTree.h>
|
||||
#include <Kernel/Locking/Spinlock.h>
|
||||
#include <Kernel/Memory/Region.h>
|
||||
#include <Kernel/Memory/VirtualAddress.h>
|
||||
#include <Kernel/Memory/VirtualRange.h>
|
||||
#include <Kernel/VirtualAddress.h>
|
||||
|
||||
namespace Kernel::Memory {
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <Kernel/PhysicalAddress.h>
|
||||
#include <Kernel/Memory/PhysicalAddress.h>
|
||||
#include <Kernel/UserOrKernelBuffer.h>
|
||||
|
||||
namespace Kernel::Memory {
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
#include <Kernel/Devices/BlockDevice.h>
|
||||
#include <Kernel/Memory/AnonymousVMObject.h>
|
||||
#include <Kernel/Memory/MemoryManager.h>
|
||||
#include <Kernel/PhysicalAddress.h>
|
||||
#include <Kernel/Memory/PhysicalAddress.h>
|
||||
|
||||
namespace Kernel::Memory {
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
#include <Kernel/Memory/AnonymousVMObject.h>
|
||||
#include <Kernel/Memory/MemoryManager.h>
|
||||
#include <Kernel/Memory/PageFaultResponse.h>
|
||||
#include <Kernel/PhysicalAddress.h>
|
||||
#include <Kernel/Memory/PhysicalAddress.h>
|
||||
|
||||
namespace Kernel::Memory {
|
||||
|
||||
|
|
61
Kernel/Memory/VirtualAddress.h
Normal file
61
Kernel/Memory/VirtualAddress.h
Normal file
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Format.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
class VirtualAddress {
|
||||
public:
|
||||
VirtualAddress() = default;
|
||||
constexpr explicit VirtualAddress(FlatPtr address)
|
||||
: m_address(address)
|
||||
{
|
||||
}
|
||||
|
||||
explicit VirtualAddress(void const* address)
|
||||
: m_address((FlatPtr)address)
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool is_null() const { return m_address == 0; }
|
||||
[[nodiscard]] constexpr bool is_page_aligned() const { return (m_address & 0xfff) == 0; }
|
||||
|
||||
[[nodiscard]] constexpr VirtualAddress offset(FlatPtr o) const { return VirtualAddress(m_address + o); }
|
||||
[[nodiscard]] constexpr FlatPtr get() const { return m_address; }
|
||||
void set(FlatPtr address) { m_address = address; }
|
||||
void mask(FlatPtr m) { m_address &= m; }
|
||||
|
||||
bool operator<=(VirtualAddress const& other) const { return m_address <= other.m_address; }
|
||||
bool operator>=(VirtualAddress const& other) const { return m_address >= other.m_address; }
|
||||
bool operator>(VirtualAddress const& other) const { return m_address > other.m_address; }
|
||||
bool operator<(VirtualAddress const& other) const { return m_address < other.m_address; }
|
||||
bool operator==(VirtualAddress const& other) const { return m_address == other.m_address; }
|
||||
bool operator!=(VirtualAddress const& other) const { return m_address != other.m_address; }
|
||||
|
||||
// NOLINTNEXTLINE(readability-make-member-function-const) const VirtualAddress shouldn't be allowed to modify the underlying memory
|
||||
[[nodiscard]] u8* as_ptr() { return reinterpret_cast<u8*>(m_address); }
|
||||
[[nodiscard]] u8 const* as_ptr() const { return reinterpret_cast<u8 const*>(m_address); }
|
||||
|
||||
[[nodiscard]] VirtualAddress page_base() const { return VirtualAddress(m_address & ~(FlatPtr)0xfffu); }
|
||||
|
||||
private:
|
||||
FlatPtr m_address { 0 };
|
||||
};
|
||||
|
||||
inline VirtualAddress operator-(VirtualAddress const& a, VirtualAddress const& b)
|
||||
{
|
||||
return VirtualAddress(a.get() - b.get());
|
||||
}
|
||||
|
||||
template<>
|
||||
struct AK::Formatter<VirtualAddress> : AK::Formatter<FormatString> {
|
||||
ErrorOr<void> format(FormatBuilder& builder, VirtualAddress const& value)
|
||||
{
|
||||
return AK::Formatter<FormatString>::format(builder, "V{}"sv, value.as_ptr());
|
||||
}
|
||||
};
|
|
@ -8,7 +8,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Error.h>
|
||||
#include <Kernel/VirtualAddress.h>
|
||||
#include <Kernel/Memory/VirtualAddress.h>
|
||||
|
||||
namespace Kernel::Memory {
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue