| 1 |
// PR tree-optimization/95638 |
| 2 |
// { dg-do run } |
| 3 |
// { dg-options "-O2 -std=c++14" } |
| 4 |
// { dg-skip-if "requires hosted libstdc++ for cassert" { ! hostedlib } } |
| 5 |
|
| 6 |
#include <cassert> |
| 7 |
#include <cstdio> |
| 8 |
#include <cstdint> |
| 9 |
#include <memory> |
| 10 |
#include <type_traits> |
| 11 |
#include <utility> |
| 12 |
|
| 13 |
template <typename Derived> |
| 14 |
class intrusive_ref_counter |
| 15 |
{ |
| 16 |
public: |
| 17 |
intrusive_ref_counter() = default; |
| 18 |
|
| 19 |
intrusive_ref_counter(intrusive_ref_counter const&) : |
| 20 |
_counter(0) |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
friend void intrusive_ptr_add_ref(const intrusive_ref_counter<Derived>* p) |
| 25 |
{ |
| 26 |
++p->_counter; |
| 27 |
} |
| 28 |
|
| 29 |
friend void intrusive_ptr_release(const intrusive_ref_counter<Derived>* p) |
| 30 |
{ |
| 31 |
if (--p->_counter == 0) |
| 32 |
{ |
| 33 |
delete static_cast<const Derived*>(p); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
private: |
| 38 |
mutable int _counter = 0; |
| 39 |
}; |
| 40 |
|
| 41 |
template <typename T> |
| 42 |
class intrusive_ptr |
| 43 |
{ |
| 44 |
public: |
| 45 |
intrusive_ptr() = default; |
| 46 |
|
| 47 |
intrusive_ptr(T* p): px(p) |
| 48 |
{ |
| 49 |
if (px != 0) intrusive_ptr_add_ref(px); |
| 50 |
} |
| 51 |
|
| 52 |
intrusive_ptr(intrusive_ptr const & rhs): px(rhs.px) |
| 53 |
{ |
| 54 |
if (px != 0) intrusive_ptr_add_ref(px); |
| 55 |
} |
| 56 |
|
| 57 |
~intrusive_ptr() |
| 58 |
{ |
| 59 |
if (px != 0) intrusive_ptr_release(px); |
| 60 |
} |
| 61 |
|
| 62 |
intrusive_ptr(intrusive_ptr && rhs) : px(rhs.px) |
| 63 |
{ |
| 64 |
rhs.px = 0; |
| 65 |
} |
| 66 |
|
| 67 |
explicit operator bool() const |
| 68 |
{ |
| 69 |
return px != 0; |
| 70 |
} |
| 71 |
|
| 72 |
private: |
| 73 |
T* px = nullptr; |
| 74 |
}; |
| 75 |
|
| 76 |
template <typename T, uint32_t MaxSize = 1> |
| 77 |
class Storage |
| 78 |
{ |
| 79 |
public: |
| 80 |
Storage() = default; |
| 81 |
|
| 82 |
Storage(Storage&& other) |
| 83 |
{ |
| 84 |
for (int i = 0; i < other._size; ++i) |
| 85 |
{ |
| 86 |
new (data() + i) T(std::move(other.data()[i])); |
| 87 |
++_size; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
~Storage() |
| 92 |
{ |
| 93 |
for (int i = 0; i < _size; ++i) |
| 94 |
{ |
| 95 |
data()[i].~T(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
void push_back(const T& value) |
| 100 |
{ |
| 101 |
assert(_size < MaxSize); |
| 102 |
|
| 103 |
new (data() + _size) T(value); |
| 104 |
++_size; |
| 105 |
} |
| 106 |
|
| 107 |
T& operator[](size_t idx) |
| 108 |
{ |
| 109 |
assert(idx < _size); |
| 110 |
return data()[idx]; |
| 111 |
} |
| 112 |
|
| 113 |
private: |
| 114 |
T* data() |
| 115 |
{ |
| 116 |
return reinterpret_cast<T*>(&_data); |
| 117 |
} |
| 118 |
|
| 119 |
private: |
| 120 |
uint32_t _size = 0; |
| 121 |
std::aligned_storage_t<sizeof(T[MaxSize])> _data; |
| 122 |
}; |
| 123 |
|
| 124 |
struct Item: intrusive_ref_counter<Item> |
| 125 |
{ |
| 126 |
}; |
| 127 |
|
| 128 |
using Collection = Storage<intrusive_ptr<Item>>; |
| 129 |
|
| 130 |
struct Holder |
| 131 |
{ |
| 132 |
__attribute__ ((noinline)) Holder(Collection collection) : collection(std::move(collection)) {} |
| 133 |
|
| 134 |
int64_t dummy = 0; // this is needed to reproduce the issue. |
| 135 |
Collection collection; |
| 136 |
}; |
| 137 |
|
| 138 |
int main() |
| 139 |
{ |
| 140 |
Collection collection; |
| 141 |
collection.push_back(intrusive_ptr<Item>(new Item())); |
| 142 |
|
| 143 |
Holder holder(std::move(collection)); |
| 144 |
|
| 145 |
auto item = holder.collection[0]; |
| 146 |
|
| 147 |
if (!item) |
| 148 |
__builtin_abort (); |
| 149 |
|
| 150 |
return 0; |
| 151 |
} |