Implement ctl::unique_ptr

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 STL approach of having a deleter
as a second template parameter makes this slightly tricky but doable. It
works like this: normally each member of a struct needs to take at least
1 byte, but this doesn’t apply to inheriting from a base struct that has
no members. So practically speaking, what we do is, when it will save us
space, we inherit from our deleter instead of including it as a member.

The above is what our slightly complicated ctl::compressed_pair class is
doing. Honestly we could just elide compressed_pair and inherit from the
pair_elem for our deleter since a T* always takes up a word, but I wrote
the whole thing anyway - maybe it’ll come in handy somewhere else.

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:
Jōshin 2024-06-15 12:08:09 -07:00 committed by Steven Dee (Jōshin)
parent 3a599bfbe1
commit 89b9a76b01
No known key found for this signature in database
5 changed files with 482 additions and 0 deletions

19
ctl/compressed_pair.cc Normal file
View file

@ -0,0 +1,19 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 Justine Alexandra Roberts Tunney
//
// Permission to use, copy, modify, and/or distribute this software for
// any purpose with or without fee is hereby granted, provided that the
// above copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include "compressed_pair.h"

100
ctl/compressed_pair.h Normal file
View file

@ -0,0 +1,100 @@
// -*-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_COMPRESSED_PAIR_H_
#define COSMOPOLITAN_CTL_COMPRESSED_PAIR_H_
#include <__type_traits/is_empty.h>
#include <__utility/forward.h>
#include <__utility/move.h>
#include <__utility/swap.h>
namespace ctl {
namespace __ {
template<typename T>
concept Empty = std::is_empty_v<T>;
template<typename T, int I>
struct pair_elem
{
using value_type = T;
constexpr pair_elem(auto&& u) : t(std::forward<decltype(u)>(u))
{
}
constexpr T& get() noexcept
{
return t;
}
constexpr const T& get() const noexcept
{
return t;
}
private:
T t;
};
template<Empty T, int I>
struct pair_elem<T, I> : private T
{
using value_type = T;
constexpr pair_elem(auto&& u) : T(std::forward<decltype(u)>(u))
{
}
constexpr T& get() noexcept
{
return static_cast<T&>(*this);
}
constexpr const T& get() const noexcept
{
return static_cast<const T&>(*this);
}
protected:
};
} // namespace __
template<typename A, typename B>
struct compressed_pair
: private __::pair_elem<A, 0>
, private __::pair_elem<B, 1>
{
using first_type = __::pair_elem<A, 0>;
using second_type = __::pair_elem<B, 1>;
constexpr compressed_pair(auto&& a, auto&& b)
: first_type(std::forward<decltype(a)>(a))
, second_type(std::forward<decltype(b)>(b))
{
}
constexpr typename first_type::value_type& first() noexcept
{
return static_cast<first_type&>(*this).get();
}
constexpr const typename first_type::value_type& first() const noexcept
{
return static_cast<const first_type&>(*this).get();
}
constexpr typename second_type::value_type& second() noexcept
{
return static_cast<second_type&>(*this).get();
}
constexpr const typename second_type::value_type& second() const noexcept
{
return static_cast<const second_type&>(*this).get();
}
};
} // namespace ctl
#endif // COSMOPOLITAN_CTL_COMPRESSED_PAIR_H_

19
ctl/unique_ptr.cc Normal file
View file

@ -0,0 +1,19 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 Justine Alexandra Roberts Tunney
//
// Permission to use, copy, modify, and/or distribute this software for
// any purpose with or without fee is hereby granted, provided that the
// above copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include "unique_ptr.h"

153
ctl/unique_ptr.h Normal file
View file

@ -0,0 +1,153 @@
// -*-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 "compressed_pair.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;
compressed_pair<pointer, D> d;
constexpr unique_ptr(nullptr_t = nullptr) noexcept : d(nullptr, D{})
{
}
constexpr unique_ptr(pointer p) noexcept : d(p, D{})
{
}
constexpr unique_ptr(pointer p, auto&& d) noexcept
: d(p, std::forward<decltype(d)>(d))
{
}
constexpr unique_ptr(unique_ptr&& u) noexcept
: d(u.d.first(), std::move(u.d.second()))
{
u.d.first() = 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 = d.first();
d.first() = nullptr;
return r;
}
inline void reset(nullptr_t = nullptr) noexcept
{
if (d.first())
d.second()(d.first());
d.first() = nullptr;
}
template<typename U>
// TODO(mrdomino):
/* requires is_convertible_v<U, T> */
inline void reset(U* p2)
{
if (d.first()) {
d.second()(d.first());
}
d.first() = static_cast<pointer>(p2);
}
inline void swap(unique_ptr& r) noexcept
{
using std::swap;
swap(d, r.d);
}
inline pointer get() const noexcept
{
return d.first();
}
inline deleter_type& get_deleter() noexcept
{
return d.second();
}
inline const deleter_type& get_deleter() const noexcept
{
return d.second();
}
inline explicit operator bool() const noexcept
{
return d.first();
}
inline element_type& operator*() const
noexcept(noexcept(*std::declval<pointer>()))
{
if (!*this)
__builtin_trap();
return *d.first();
}
inline pointer operator->() const noexcept
{
if (!*this)
__builtin_trap();
return d.first();
}
};
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_

191
test/ctl/unique_ptr_test.cc Normal file
View file

@ -0,0 +1,191 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 Justine Alexandra Roberts Tunney
//
// Permission to use, copy, modify, and/or distribute this software for
// any purpose with or without fee is hereby granted, provided that the
// above copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include "ctl/unique_ptr.h"
#include <type_traits>
#include "libc/runtime/runtime.h"
// #include <memory>
// #define ctl std
template<typename T, typename D = ctl::default_delete<T>>
using Ptr = ctl::unique_ptr<T, D>;
template<typename T, typename... Args>
Ptr<T>
Mk(Args&&... args)
{
return ctl::make_unique<T, Args...>(std::forward<Args>(args)...);
}
template<typename T>
Ptr<T>
MkRaw()
{
return ctl::make_unique_for_overwrite<T>();
}
#undef ctl
static int g = 0;
struct SetsGDeleter
{
void operator()(auto*) const noexcept
{
++g;
}
};
static_assert(sizeof(Ptr<int, SetsGDeleter>) == sizeof(int*));
struct SetsGCtor
{
SetsGCtor()
{
++g;
}
};
struct SetsGDtor
{
~SetsGDtor()
{
++g;
}
};
int
main()
{
{
Ptr<int> x(new int(5));
}
{
Ptr<int, SetsGDeleter> x(new int());
x.reset();
if (g != 1)
return 1;
}
{
g = 0;
Ptr<int, SetsGDeleter> x(new int());
delete x.release();
x.reset();
if (g)
return 17;
}
{
Ptr<int> x(new int(5)), y(new int(6));
x.swap(y);
if (*x != 6 || *y != 5)
return 2;
}
{
Ptr<int> x;
if (x)
return 3;
x.reset(new int(5));
if (!x)
return 4;
}
{
g = 0;
Ptr<SetsGCtor> x;
if (g)
return 5;
x = Mk<SetsGCtor>();
if (g != 1)
return 6;
}
{
g = 0;
auto x = Mk<SetsGDtor>();
if (g)
return 7;
x.reset();
if (g != 1)
return 8;
if (x)
return 9;
}
{
g = 0;
Ptr<SetsGDtor> x, y;
x = Mk<SetsGDtor>();
y = Mk<SetsGDtor>();
#if 0
// shouldn't compile
x = y;
#endif
x = std::move(y);
if (g != 1)
return 10;
if (y)
return 11;
}
{
g = 0;
{
auto x = Mk<SetsGDtor>();
}
if (g != 1)
return 12;
}
{
g = 0;
{
auto x = Mk<SetsGDtor>();
x.release();
}
if (g)
return 13;
}
#if 0
// I could not figure out how to test make_unique_for_overwrite. The only
// side effects it has are illegal to detect?
{
g = 0;
auto x = MkRaw<DefaultInitialized>();
if (g)
return 14;
x.reset();
if (g)
return 15;
x = Mk<DefaultInitialized>();
if (g != 1)
return 16;
}
#endif
// next is 18
return 0;
}