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