Implement ctl::unique_ptr (#1216)

The way unique_ptr is supposed to work is as a purely compile-time check
that your raw pointers are getting deleted when they go out of scope. It
should ideally emit the same exact machine code as if you were using raw
pointers with manual deletes.

Part of what this means is that under normal circumstances, a unique_ptr
shouldn’t take up more space than a raw pointer - in other words, sizeof
unique_ptr<T> should == sizeof(T*).

The present PR doesn’t bother with the specialization for array types. I
also left a couple other parts of the STL API unimplemented. I’d love to
see someone else implement these, or I’ll get to them at some point.
This commit is contained in:
Steven Dee (Jōshin) 2024-06-15 19:54:52 -07:00 committed by GitHub
parent e38a6e7996
commit f9dd5683a4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 392 additions and 0 deletions

156
ctl/unique_ptr.h Normal file
View file

@ -0,0 +1,156 @@
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef COSMOPOLITAN_CTL_UNIQUE_PTR_H_
#define COSMOPOLITAN_CTL_UNIQUE_PTR_H_
#include <__utility/forward.h>
#include <__utility/move.h>
#include <__utility/swap.h>
namespace ctl {
template<typename T>
struct default_delete
{
constexpr void operator()(T* p) const noexcept
{
delete p;
}
};
template<typename T, typename D = default_delete<T>>
struct unique_ptr
{
using pointer = T*;
using element_type = T;
using deleter_type = D;
pointer p;
[[no_unique_address]] deleter_type d;
constexpr unique_ptr(nullptr_t = nullptr) noexcept : p(nullptr)
{
}
constexpr unique_ptr(pointer p) noexcept : p(p)
{
}
constexpr unique_ptr(pointer p, auto&& d) noexcept
: p(p), d(std::forward<decltype(d)>(d))
{
}
constexpr unique_ptr(unique_ptr&& u) noexcept : p(u.p), d(std::move(u.d))
{
u.p = nullptr;
}
// TODO(mrdomino):
// template <typename U, typename E>
// unique_ptr(unique_ptr<U, E>&& u) noexcept;
unique_ptr(const unique_ptr&) = delete;
inline ~unique_ptr() /* noexcept */
{
reset();
}
inline unique_ptr& operator=(unique_ptr r) noexcept
{
swap(r);
return *this;
}
inline pointer release() noexcept
{
pointer r = p;
p = nullptr;
return r;
}
inline void reset(nullptr_t = nullptr) noexcept
{
if (p)
d(p);
p = nullptr;
}
template<typename U>
// TODO(mrdomino):
/* requires is_convertible_v<U, T> */
inline void reset(U* p2)
{
if (p) {
d(p);
}
p = static_cast<pointer>(p2);
}
inline void swap(unique_ptr& r) noexcept
{
using std::swap;
swap(p, r.p);
swap(d, r.d);
}
inline pointer get() const noexcept
{
return p;
}
inline deleter_type& get_deleter() noexcept
{
return d;
}
inline const deleter_type& get_deleter() const noexcept
{
return d;
}
inline explicit operator bool() const noexcept
{
return p;
}
inline element_type& operator*() const
noexcept(noexcept(*std::declval<pointer>()))
{
if (!p)
__builtin_trap();
return *p;
}
inline pointer operator->() const noexcept
{
if (!p)
__builtin_trap();
return p;
}
};
template<typename T, typename... Args>
inline unique_ptr<T>
make_unique(Args&&... args)
{
return unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template<typename T>
inline unique_ptr<T>
make_unique_for_overwrite()
{
#if 0
// You'd think that it'd work like this, but std::unique_ptr does not.
return unique_ptr<T>(
static_cast<T*>(::operator new(sizeof(T), align_val_t(alignof(T)))));
#else
return unique_ptr<T>(new T);
#endif
}
// TODO(mrdomino): specializations for T[]
} // namespace ctl
#endif // COSMOPOLITAN_CTL_UNIQUE_PTR_H_