2020-07-31 17:26:44 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-31 17:26:44 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-12-23 16:42:22 +03:00
|
|
|
#include <AK/Assertions.h>
|
2020-07-31 17:26:44 +03:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
2021-11-15 01:52:48 +03:00
|
|
|
#ifdef KERNEL
|
2023-02-24 20:54:30 +03:00
|
|
|
# include <Kernel/Memory/VirtualAddress.h>
|
2021-11-15 01:52:48 +03:00
|
|
|
#endif
|
|
|
|
|
2020-07-31 17:26:44 +03:00
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
2021-04-10 16:59:06 +03:00
|
|
|
concept PointerTypeName = IsPointer<T>;
|
2020-08-01 20:34:53 +03:00
|
|
|
|
2021-06-19 14:13:31 +03:00
|
|
|
template<PointerTypeName T>
|
2020-07-31 17:26:44 +03:00
|
|
|
class Userspace {
|
|
|
|
public:
|
2021-01-11 02:29:28 +03:00
|
|
|
Userspace() = default;
|
2020-07-31 17:26:44 +03:00
|
|
|
|
2020-08-22 17:45:52 +03:00
|
|
|
// Disable default implementations that would use surprising integer promotion.
|
2022-04-01 20:58:27 +03:00
|
|
|
bool operator==(Userspace const&) const = delete;
|
|
|
|
bool operator<=(Userspace const&) const = delete;
|
|
|
|
bool operator>=(Userspace const&) const = delete;
|
|
|
|
bool operator<(Userspace const&) const = delete;
|
|
|
|
bool operator>(Userspace const&) const = delete;
|
2020-08-22 17:45:52 +03:00
|
|
|
|
2020-08-01 08:18:24 +03:00
|
|
|
#ifdef KERNEL
|
2020-07-31 17:26:44 +03:00
|
|
|
Userspace(FlatPtr ptr)
|
|
|
|
: m_ptr(ptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-11-06 23:12:16 +03:00
|
|
|
explicit operator bool() const { return m_ptr != 0; }
|
|
|
|
|
2020-07-31 17:26:44 +03:00
|
|
|
FlatPtr ptr() const { return m_ptr; }
|
2021-11-15 01:52:48 +03:00
|
|
|
VirtualAddress vaddr() const { return VirtualAddress(m_ptr); }
|
|
|
|
T unsafe_userspace_ptr() const { return reinterpret_cast<T>(m_ptr); }
|
2020-07-31 17:26:44 +03:00
|
|
|
#else
|
|
|
|
Userspace(T ptr)
|
|
|
|
: m_ptr(ptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-11-06 23:12:16 +03:00
|
|
|
explicit operator bool() const { return m_ptr != nullptr; }
|
|
|
|
|
2020-07-31 17:26:44 +03:00
|
|
|
T ptr() const { return m_ptr; }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
2020-08-01 08:18:24 +03:00
|
|
|
#ifdef KERNEL
|
2020-07-31 17:26:44 +03:00
|
|
|
FlatPtr m_ptr { 0 };
|
|
|
|
#else
|
|
|
|
T m_ptr { nullptr };
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2020-08-07 10:38:36 +03:00
|
|
|
template<typename T, typename U>
|
2022-04-01 20:58:27 +03:00
|
|
|
inline Userspace<T> static_ptr_cast(Userspace<U> const& ptr)
|
2020-08-07 10:38:36 +03:00
|
|
|
{
|
|
|
|
#ifdef KERNEL
|
|
|
|
auto casted_ptr = static_cast<T>(ptr.unsafe_userspace_ptr());
|
|
|
|
#else
|
|
|
|
auto casted_ptr = static_cast<T>(ptr.ptr());
|
|
|
|
#endif
|
2023-03-07 17:28:21 +03:00
|
|
|
return Userspace<T>(reinterpret_cast<FlatPtr>(casted_ptr));
|
2020-08-07 10:38:36 +03:00
|
|
|
}
|
|
|
|
|
2020-07-31 17:26:44 +03:00
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2020-08-07 10:38:36 +03:00
|
|
|
using AK::static_ptr_cast;
|
2020-08-22 17:45:52 +03:00
|
|
|
using AK::Userspace;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|