2024-06-16 02:54:52 +00:00
|
|
|
// -*- 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.
|
|
|
|
|
2024-06-29 02:07:35 +00:00
|
|
|
#include "ctl/is_same.h"
|
2024-06-16 02:54:52 +00:00
|
|
|
#include "ctl/unique_ptr.h"
|
Introduce more CTL content
This change introduces accumulate, addressof, advance, all_of, distance,
array, enable_if, allocator_traits, back_inserter, bad_alloc, is_signed,
any_of, copy, exception, fill, fill_n, is_same, is_same_v, out_of_range,
lexicographical_compare, is_integral, uninitialized_fill_n, is_unsigned,
numeric_limits, uninitialized_fill, iterator_traits, move_backward, min,
max, iterator_tag, move_iterator, reverse_iterator, uninitialized_move_n
This change experiments with rewriting the ctl::vector class to make the
CTL design more similar to the STL. So far it has not slowed things down
to have 42 #include lines rather than 2, since it's still almost nothing
compared to LLVM's code. In fact the closer we can flirt with being just
like libcxx, the better chance we might have of discovering exactly what
makes it so slow to compile. It would be an enormous discovery if we can
find one simple trick to solving the issue there instead.
This also fixes a bug in `ctl::string(const string &s)` when `s` is big.
2024-06-28 05:18:55 +00:00
|
|
|
#include "libc/mem/leaks.h"
|
2024-06-16 02:54:52 +00:00
|
|
|
|
|
|
|
// #include <memory>
|
Introduce more CTL content
This change introduces accumulate, addressof, advance, all_of, distance,
array, enable_if, allocator_traits, back_inserter, bad_alloc, is_signed,
any_of, copy, exception, fill, fill_n, is_same, is_same_v, out_of_range,
lexicographical_compare, is_integral, uninitialized_fill_n, is_unsigned,
numeric_limits, uninitialized_fill, iterator_traits, move_backward, min,
max, iterator_tag, move_iterator, reverse_iterator, uninitialized_move_n
This change experiments with rewriting the ctl::vector class to make the
CTL design more similar to the STL. So far it has not slowed things down
to have 42 #include lines rather than 2, since it's still almost nothing
compared to LLVM's code. In fact the closer we can flirt with being just
like libcxx, the better chance we might have of discovering exactly what
makes it so slow to compile. It would be an enormous discovery if we can
find one simple trick to solving the issue there instead.
This also fixes a bug in `ctl::string(const string &s)` when `s` is big.
2024-06-28 05:18:55 +00:00
|
|
|
// #include <type_traits>
|
2024-06-16 02:54:52 +00:00
|
|
|
// #define ctl std
|
|
|
|
|
2024-09-01 20:47:30 +00:00
|
|
|
using ctl::unique_ptr;
|
|
|
|
using ctl::make_unique;
|
|
|
|
using ctl::make_unique_for_overwrite;
|
2024-06-16 02:54:52 +00:00
|
|
|
|
2024-09-01 20:47:30 +00:00
|
|
|
#undef ctl
|
2024-06-16 02:54:52 +00:00
|
|
|
|
2024-09-01 20:47:30 +00:00
|
|
|
// The following few definitions are used to get observability into aspects of
|
|
|
|
// an object's lifecycle, to make sure that e.g. constructing a unique_ptr of a
|
|
|
|
// type does not construct an object, and that make_unique does construct an
|
|
|
|
// object.
|
2024-06-16 02:54:52 +00:00
|
|
|
static int g = 0;
|
|
|
|
|
2024-09-01 20:47:30 +00:00
|
|
|
struct ConstructG
|
2024-06-16 02:54:52 +00:00
|
|
|
{
|
2024-09-01 20:47:30 +00:00
|
|
|
ConstructG()
|
2024-06-16 02:54:52 +00:00
|
|
|
{
|
|
|
|
++g;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-01 20:47:30 +00:00
|
|
|
struct DestructG
|
2024-06-16 02:54:52 +00:00
|
|
|
{
|
2024-09-01 20:47:30 +00:00
|
|
|
~DestructG()
|
2024-06-16 02:54:52 +00:00
|
|
|
{
|
2024-09-01 20:47:30 +00:00
|
|
|
++g;
|
2024-06-16 02:54:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-01 20:47:30 +00:00
|
|
|
struct CallG
|
2024-06-16 02:54:52 +00:00
|
|
|
{
|
2024-06-29 02:07:35 +00:00
|
|
|
void operator()(auto* x) const noexcept
|
2024-06-16 02:54:52 +00:00
|
|
|
{
|
2024-09-01 20:47:30 +00:00
|
|
|
++g;
|
2024-06-16 02:54:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-01 20:47:30 +00:00
|
|
|
// A unique_ptr with an empty deleter should be the same size as a raw pointer.
|
|
|
|
static_assert(sizeof(unique_ptr<int, decltype([] {})>) == sizeof(int*));
|
2024-06-16 02:54:52 +00:00
|
|
|
|
2024-09-01 20:47:30 +00:00
|
|
|
struct FinalDeleter final
|
2024-06-16 02:54:52 +00:00
|
|
|
{
|
2024-09-01 20:47:30 +00:00
|
|
|
void operator()(auto* x) const noexcept
|
2024-06-16 02:54:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-01 20:47:30 +00:00
|
|
|
// ctl::unique_ptr does not need to inherit from its deleter for this property;
|
|
|
|
// the STL often does, though, so we don't hold them to the following.
|
|
|
|
static_assert(!ctl::is_same_v<unique_ptr<int>, ctl::unique_ptr<int>> ||
|
|
|
|
sizeof(unique_ptr<int, FinalDeleter>) == sizeof(int*));
|
2024-06-16 02:54:52 +00:00
|
|
|
|
2024-06-20 22:44:31 +00:00
|
|
|
struct Base
|
|
|
|
{};
|
|
|
|
|
|
|
|
struct Derived : Base
|
|
|
|
{};
|
|
|
|
|
2024-06-16 02:54:52 +00:00
|
|
|
int
|
|
|
|
main()
|
|
|
|
{
|
2024-09-01 20:47:30 +00:00
|
|
|
int a;
|
2024-06-29 02:07:35 +00:00
|
|
|
|
2024-06-16 02:54:52 +00:00
|
|
|
{
|
2024-09-01 20:47:30 +00:00
|
|
|
// Shouldn't cause any memory leaks.
|
|
|
|
unique_ptr<int> x(new int(5));
|
2024-06-16 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-09-01 20:47:30 +00:00
|
|
|
// Deleter is called if the pointer is non-null when reset.
|
|
|
|
unique_ptr<int, CallG> x(&a);
|
2024-06-16 02:54:52 +00:00
|
|
|
x.reset();
|
|
|
|
if (g != 1)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
g = 0;
|
2024-09-01 20:47:30 +00:00
|
|
|
// Deleter is not called if the pointer is null when reset.
|
|
|
|
unique_ptr<int, CallG> x(&a);
|
|
|
|
x.release();
|
2024-06-16 02:54:52 +00:00
|
|
|
x.reset();
|
|
|
|
if (g)
|
|
|
|
return 17;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-09-01 20:47:30 +00:00
|
|
|
g = 0;
|
|
|
|
// Deleter is called when the pointer goes out of scope.
|
|
|
|
{
|
|
|
|
unique_ptr<int, CallG> x(&a);
|
|
|
|
}
|
|
|
|
if (!g)
|
|
|
|
return 18;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
g = 0;
|
|
|
|
// Deleter is called if scope ends exceptionally.
|
|
|
|
try {
|
|
|
|
unique_ptr<int, CallG> x(&a);
|
|
|
|
throw 'a';
|
|
|
|
} catch (char) {
|
|
|
|
}
|
|
|
|
if (!g)
|
|
|
|
return 19;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
unique_ptr<int> x(new int(5)), y(new int(6));
|
2024-06-16 02:54:52 +00:00
|
|
|
x.swap(y);
|
|
|
|
if (*x != 6 || *y != 5)
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-09-01 20:47:30 +00:00
|
|
|
unique_ptr<int> x;
|
2024-06-16 02:54:52 +00:00
|
|
|
if (x)
|
|
|
|
return 3;
|
|
|
|
x.reset(new int(5));
|
|
|
|
if (!x)
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
g = 0;
|
2024-09-01 20:47:30 +00:00
|
|
|
unique_ptr<ConstructG> x;
|
2024-06-16 02:54:52 +00:00
|
|
|
if (g)
|
|
|
|
return 5;
|
2024-09-01 20:47:30 +00:00
|
|
|
x = make_unique<ConstructG>();
|
2024-06-16 02:54:52 +00:00
|
|
|
if (g != 1)
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
g = 0;
|
2024-09-01 20:47:30 +00:00
|
|
|
auto x = make_unique<DestructG>();
|
2024-06-16 02:54:52 +00:00
|
|
|
if (g)
|
|
|
|
return 7;
|
|
|
|
x.reset();
|
|
|
|
if (g != 1)
|
|
|
|
return 8;
|
|
|
|
if (x)
|
|
|
|
return 9;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
g = 0;
|
2024-09-01 20:47:30 +00:00
|
|
|
unique_ptr<DestructG> x, y;
|
|
|
|
x = make_unique<DestructG>();
|
|
|
|
y = make_unique<DestructG>();
|
2024-06-16 02:54:52 +00:00
|
|
|
#if 0
|
|
|
|
// shouldn't compile
|
|
|
|
x = y;
|
|
|
|
#endif
|
2024-06-19 05:00:59 +00:00
|
|
|
x = ctl::move(y);
|
2024-06-16 02:54:52 +00:00
|
|
|
if (g != 1)
|
|
|
|
return 10;
|
|
|
|
if (y)
|
|
|
|
return 11;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
g = 0;
|
|
|
|
{
|
2024-09-01 20:47:30 +00:00
|
|
|
auto x = make_unique<DestructG>();
|
2024-06-16 02:54:52 +00:00
|
|
|
}
|
|
|
|
if (g != 1)
|
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
g = 0;
|
|
|
|
{
|
2024-09-01 20:47:30 +00:00
|
|
|
auto x = make_unique<DestructG>();
|
2024-06-29 02:07:35 +00:00
|
|
|
delete x.release();
|
2024-06-16 02:54:52 +00:00
|
|
|
}
|
2024-06-29 02:07:35 +00:00
|
|
|
if (g != 1)
|
2024-06-16 02:54:52 +00:00
|
|
|
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;
|
2024-09-01 20:47:30 +00:00
|
|
|
auto x = make_unique_for_overwrite<DefaultInitialized>();
|
2024-06-16 02:54:52 +00:00
|
|
|
if (g)
|
|
|
|
return 14;
|
|
|
|
x.reset();
|
|
|
|
if (g)
|
|
|
|
return 15;
|
2024-09-01 20:47:30 +00:00
|
|
|
x = make_unique<DefaultInitialized>();
|
2024-06-16 02:54:52 +00:00
|
|
|
if (g != 1)
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{
|
|
|
|
int a;
|
|
|
|
// Should compile.
|
2024-09-01 20:47:30 +00:00
|
|
|
unique_ptr<int, FinalDeleter> x(&a);
|
2024-06-16 02:54:52 +00:00
|
|
|
}
|
|
|
|
|
2024-06-20 22:44:31 +00:00
|
|
|
{
|
2024-09-01 20:47:30 +00:00
|
|
|
unique_ptr<Base> x(new Base);
|
2024-06-20 22:44:31 +00:00
|
|
|
x.reset(new Derived);
|
|
|
|
|
2024-09-01 20:47:30 +00:00
|
|
|
unique_ptr<Derived> y(new Derived);
|
|
|
|
unique_ptr<Base> z(ctl::move(y));
|
2024-06-20 22:44:31 +00:00
|
|
|
}
|
|
|
|
|
2024-06-29 02:07:35 +00:00
|
|
|
CheckForMemoryLeaks();
|
2024-06-16 02:54:52 +00:00
|
|
|
}
|