ladybird/AK/Userspace.h
Ben Wiederhake c2a900b853 Everywhere: Remove unused includes of AK/StdLibExtras.h
These instances were detected by searching for files that include
AK/StdLibExtras.h, but don't match the regex:

\\b(abs|AK_REPLACED_STD_NAMESPACE|array_size|ceil_div|clamp|exchange|for
ward|is_constant_evaluated|is_power_of_two|max|min|mix|move|_RawPtr|RawP
tr|round_up_to_power_of_two|swap|to_underlying)\\b

(Without the linebreaks.)

This regex is pessimistic, so there might be more files that don't
actually use any "extra stdlib" functions.

In theory, one might use LibCPP to detect things like this
automatically, but let's do this one step after another.
2023-01-02 20:27:20 -05:00

80 lines
1.7 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Assertions.h>
#include <AK/Types.h>
#ifdef KERNEL
# include <Kernel/VirtualAddress.h>
#endif
namespace AK {
template<typename T>
concept PointerTypeName = IsPointer<T>;
template<PointerTypeName T>
class Userspace {
public:
Userspace() = default;
// Disable default implementations that would use surprising integer promotion.
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;
#ifdef KERNEL
Userspace(FlatPtr ptr)
: m_ptr(ptr)
{
}
explicit operator bool() const { return m_ptr != 0; }
FlatPtr ptr() const { return m_ptr; }
VirtualAddress vaddr() const { return VirtualAddress(m_ptr); }
T unsafe_userspace_ptr() const { return reinterpret_cast<T>(m_ptr); }
#else
Userspace(T ptr)
: m_ptr(ptr)
{
}
explicit operator bool() const { return m_ptr != nullptr; }
T ptr() const { return m_ptr; }
#endif
private:
#ifdef KERNEL
FlatPtr m_ptr { 0 };
#else
T m_ptr { nullptr };
#endif
};
template<typename T, typename U>
inline Userspace<T> static_ptr_cast(Userspace<U> const& 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);
}
}
#if USING_AK_GLOBALLY
using AK::static_ptr_cast;
using AK::Userspace;
#endif