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-08-01 05:13:50 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2020-07-31 14:26:44 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
operator bool() const { return m_ptr; }
|
|
|
|
operator FlatPtr() const { return (FlatPtr)m_ptr; }
|
|
|
|
|
2020-08-22 14:45:52 +00: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 05:18:24 +00:00
|
|
|
#ifdef KERNEL
|
2020-07-31 14:26:44 +00:00
|
|
|
Userspace(FlatPtr ptr)
|
|
|
|
: m_ptr(ptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FlatPtr ptr() const { return m_ptr; }
|
2020-07-31 16:46:55 +00:00
|
|
|
T unsafe_userspace_ptr() const { return (T)m_ptr; }
|
2020-07-31 14:26:44 +00:00
|
|
|
#else
|
|
|
|
Userspace(T ptr)
|
|
|
|
: m_ptr(ptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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>
|
|
|
|
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 14:26:44 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 07:38:36 +00:00
|
|
|
using AK::static_ptr_cast;
|
2020-08-22 14:45:52 +00:00
|
|
|
using AK::Userspace;
|