From 813eaba6ea23544eea6bc6efcedada817b9c0b10 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 14 Jan 2022 13:09:21 -0700 Subject: [PATCH] [vector] Start adding destruction Now we need to remove manual destruction, ouch! --- src/hb-vector.hh | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/hb-vector.hh b/src/hb-vector.hh index e3fd51373..a474a7ba5 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -82,7 +82,7 @@ struct hb_vector_t void fini () { - // XXX Destruct + shrink_vector (0); hb_free (arrayZ); init (); } @@ -224,13 +224,34 @@ struct hb_vector_t new (std::addressof (new_array[i])) Type (); for (unsigned i = 0; i < (unsigned) length; i++) new_array[i] = std::move (arrayZ[i]); - for (unsigned i = 0; i < (unsigned) length; i++) - arrayZ[i].~Type (); + unsigned old_length = length; + shrink_vector (0); + length = old_length; hb_free (arrayZ); } return new_array; } + template ::value)> + void + shrink_vector (unsigned size) + { + length = size; + } + template ::value)> + void + shrink_vector (unsigned size) + { + while ((unsigned) length > size) + { + arrayZ[(unsigned) length - 1].~Type (); + length--; + } + } + + /* Allocate for size but don't adjust length. */ bool alloc (unsigned int size) { @@ -276,8 +297,7 @@ struct hb_vector_t // XXX construct objects. memset (arrayZ + length, 0, (size - length) * sizeof (*arrayZ)); else if (size < length) - // XXX destroy objects. - {} + shrink_vector (size); length = size; return true; @@ -307,9 +327,7 @@ struct hb_vector_t if (size >= length) return; - // XXX Destruct. - - length = size; + shrink_vector (size); } template