@@ -14,13 +14,13 @@ class SSOVector {
1414 // if grow internal has been called, it means that we need to grow out of situ
1515 HARD_ASSERT (new_capacity > m_capacity, " New capacity should be bigger than existing capacity" )
1616 if (m_capacity == SSO) {
17- m_storage = allocate_uninitialized<T>(new_capacity);
17+ m_storage = allocate_uninitialized<T>(new_capacity);
1818 m_capacity = new_capacity;
1919 if constexpr (SSO != 0 ) UninitializedMoveConstruct (m_storage, addressof (m_situ_storage[0 ]), m_size);
2020 } else {
2121 T* new_storage = allocate_uninitialized<T>(new_capacity);
2222 UninitializedMoveConstruct (new_storage, m_storage, m_size);
23- deallocate<T, DeallocType::Multiple>(m_storage );
23+ destroy_if_not_sso ( );
2424 m_storage = new_storage;
2525 m_capacity = new_capacity;
2626 }
@@ -30,6 +30,12 @@ class SSOVector {
3030 if (m_size == m_capacity) { grow_to_capacity (m_capacity + 1 ); }
3131 new (&m_storage[m_size++]) T{ move (item) };
3232 }
33+ void destroy_if_not_sso () {
34+ if (m_capacity > SSO && m_storage != nullptr ) {
35+ for (size_t i = 0 ; i < m_size; ++i) { m_storage[i].~T (); }
36+ deallocate<T, DeallocType::Multiple>(m_storage);
37+ }
38+ }
3339
3440 public:
3541 constexpr SSOVector () = default;
@@ -46,7 +52,7 @@ class SSOVector {
4652 requires (OTHER_SSO != SSO)
4753 : m_size(other.size()) {
4854 if (other.size () > SSO) {
49- m_storage = allocate_uninitialized<T>(other.capacity ());
55+ m_storage = allocate_uninitialized<T>(other.capacity ());
5056 m_capacity = other.capacity ();
5157 }
5258 UninitializedCopyConstruct (m_storage, other.storage (), other.size ());
@@ -87,7 +93,7 @@ class SSOVector {
8793 {
8894 m_size = other.size ();
8995 if (m_size > m_capacity) {
90- if (m_capacity != SSO) deallocate<T, DeallocType::Multiple>(m_storage );
96+ destroy_if_not_sso ( );
9197 m_storage = allocate_initialized<T>(other.capacity ());
9298 m_capacity = other.capacity ();
9399 }
@@ -117,11 +123,11 @@ class SSOVector {
117123 if (m_capacity > SSO && other.m_size > m_capacity) {
118124 // if we're already not in situ and we can't fit the other vector, let's resize
119125 // if we're already not in situ *but* we can fit the other vector, we don't do anything
120- deallocate<T, DeallocType::Multiple>(m_storage );
126+ destroy_if_not_sso ( );
121127 allocate_initialized<T>(other.m_size );
122128 } else if (other.m_capacity == SSO && m_capacity > SSO) {
123129 // if the other one is in situ but we're not, then we delete our storage, since we don't really need it
124- deallocate<T, DeallocType::Multiple>(m_storage );
130+ destroy_if_not_sso ( );
125131 m_storage = SSO != 0 ? addressof (m_situ_storage[0 ]) : m_situ_storage;
126132 }
127133 m_size = other.m_size ;
@@ -131,7 +137,7 @@ class SSOVector {
131137 }
132138 SSOVector& operator =(SSOVector&& other) noexcept {
133139 if (this == &other) return *this ;
134- if (m_capacity > SSO) { deallocate<T, DeallocType::Multiple>(m_storage); }
140+ destroy_if_not_sso ();
135141 m_size = other.m_size ;
136142 m_capacity = other.m_capacity ;
137143 if (other.m_capacity == SSO) {
@@ -178,7 +184,7 @@ class SSOVector {
178184 }
179185 void release_strong () {
180186 if (m_capacity != SSO) {
181- deallocate<T, DeallocType::Multiple>(m_storage );
187+ destroy_if_not_sso ( );
182188 m_storage = SSO != 0 ? addressof (m_situ_storage[0 ]) : m_situ_storage;
183189 m_capacity = SSO;
184190 }
@@ -208,6 +214,7 @@ class SSOVector {
208214 m_capacity = basic_growth (m_capacity + 1 );
209215 m_storage = allocate_uninitialized<T>(m_capacity);
210216 UninitializedCopyConstruct (m_storage + sizeof (T), storage, m_size);
217+ for (size_t i = 0 ; i < m_size; i++) { storage[i].~T (); }
211218 deallocate<T, DeallocType::Multiple>(storage);
212219 }
213220 } else {
@@ -228,9 +235,7 @@ class SSOVector {
228235 {
229236 if (new_size < m_size) return ;
230237 if (new_size > m_capacity) { grow_to_capacity (new_size); }
231- for (size_t i = m_size; i < new_size; i++) {
232- new (&m_storage[i]) T{};
233- }
238+ for (size_t i = m_size; i < new_size; i++) { new (&m_storage[i]) T{}; }
234239 m_size = new_size;
235240 }
236241 void reserve (size_t new_capacity) {
@@ -243,9 +248,7 @@ class SSOVector {
243248 }
244249 const T& last () const { return m_storage[m_size - 1 ]; }
245250 T& last () { return m_storage[m_size - 1 ]; }
246- ~SSOVector () {
247- if (m_capacity > SSO) deallocate<T, DeallocType::Multiple>(m_storage);
248- }
251+ ~SSOVector () { destroy_if_not_sso (); }
249252};
250253template <Printable T, size_t S>
251254struct PrintInfo <SSOVector<T, S>> {
0 commit comments