[vector] Start adding destruction

Now we need to remove manual destruction, ouch!
This commit is contained in:
Behdad Esfahbod 2022-01-14 13:09:21 -07:00
parent 65a22836f8
commit 813eaba6ea
1 changed files with 26 additions and 8 deletions

View File

@ -82,7 +82,7 @@ struct hb_vector_t
void fini () void fini ()
{ {
// XXX Destruct shrink_vector (0);
hb_free (arrayZ); hb_free (arrayZ);
init (); init ();
} }
@ -224,13 +224,34 @@ struct hb_vector_t
new (std::addressof (new_array[i])) Type (); new (std::addressof (new_array[i])) Type ();
for (unsigned i = 0; i < (unsigned) length; i++) for (unsigned i = 0; i < (unsigned) length; i++)
new_array[i] = std::move (arrayZ[i]); new_array[i] = std::move (arrayZ[i]);
for (unsigned i = 0; i < (unsigned) length; i++) unsigned old_length = length;
arrayZ[i].~Type (); shrink_vector (0);
length = old_length;
hb_free (arrayZ); hb_free (arrayZ);
} }
return new_array; return new_array;
} }
template <typename T = Type,
hb_enable_if (std::is_trivially_copy_assignable<T>::value)>
void
shrink_vector (unsigned size)
{
length = size;
}
template <typename T = Type,
hb_enable_if (!std::is_trivially_copy_assignable<T>::value)>
void
shrink_vector (unsigned size)
{
while ((unsigned) length > size)
{
arrayZ[(unsigned) length - 1].~Type ();
length--;
}
}
/* Allocate for size but don't adjust length. */ /* Allocate for size but don't adjust length. */
bool alloc (unsigned int size) bool alloc (unsigned int size)
{ {
@ -276,8 +297,7 @@ struct hb_vector_t
// XXX construct objects. // XXX construct objects.
memset (arrayZ + length, 0, (size - length) * sizeof (*arrayZ)); memset (arrayZ + length, 0, (size - length) * sizeof (*arrayZ));
else if (size < length) else if (size < length)
// XXX destroy objects. shrink_vector (size);
{}
length = size; length = size;
return true; return true;
@ -307,9 +327,7 @@ struct hb_vector_t
if (size >= length) if (size >= length)
return; return;
// XXX Destruct. shrink_vector (size);
length = size;
} }
template <typename T> template <typename T>