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-08-01 08:13:50 +03:00
|
|
|
#include <AK/StdLibExtras.h>
|
2020-07-31 17:26:44 +03:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
operator FlatPtr() const { return (FlatPtr)m_ptr; }
|
|
|
|
|
2020-08-22 17:45:52 +03:00
|
|
|
// Disable default implementations that would use surprising integer promotion.
|
|
|
|
bool operator==(const Userspace&) const = delete;
|
|
|
|
bool operator<=(const Userspace&) const = delete;
|
|
|
|
bool operator>=(const Userspace&) const = delete;
|
|
|
|
bool operator<(const Userspace&) const = delete;
|
|
|
|
bool operator>(const Userspace&) const = delete;
|
|
|
|
|
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; }
|
2020-07-31 19:46:55 +03:00
|
|
|
T unsafe_userspace_ptr() const { return (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>
|
|
|
|
inline Userspace<T> static_ptr_cast(const Userspace<U>& ptr)
|
|
|
|
{
|
|
|
|
#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 17:26:44 +03:00
|
|
|
}
|
|
|
|
|
2020-08-07 10:38:36 +03:00
|
|
|
using AK::static_ptr_cast;
|
2020-08-22 17:45:52 +03:00
|
|
|
using AK::Userspace;
|