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 {
|
|
|
|
|
2020-08-01 17:34:53 +00:00
|
|
|
// HACK: This is just here to make syntax highlighting work in Qt Creator.
|
|
|
|
// Once it supports C++20 concepts, we can remove this.
|
2020-08-16 22:11:05 +00:00
|
|
|
#if defined(__cpp_concepts) && !defined(__COVERITY__)
|
2020-07-31 14:26:44 +00:00
|
|
|
template<typename T>
|
2021-04-10 13:59:06 +00:00
|
|
|
concept PointerTypeName = IsPointer<T>;
|
2020-08-01 05:13:50 +00:00
|
|
|
template<PointerTypeName T>
|
2020-08-16 22:11:05 +00:00
|
|
|
#else
|
2021-04-10 13:59:06 +00:00
|
|
|
template<typename T, typename EnableIf<IsPointer<T>, int>::Type = 0>
|
2020-08-01 17:34:53 +00:00
|
|
|
#endif
|
|
|
|
|
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;
|