libluna/Vector: Use bytes instead of count in resize()

This fixes a bug that was causing the heap linked list to become quite corrupted.
This commit is contained in:
apio 2023-03-10 21:07:08 +01:00
parent e8a401efc2
commit 41203df472
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -150,6 +150,11 @@ template <typename T> class Vector
return m_capacity;
}
usize byte_capacity() const
{
return m_capacity * sizeof(T);
}
usize size() const
{
return m_size;
@ -162,10 +167,14 @@ template <typename T> class Vector
Result<void> resize(usize new_capacity)
{
void* ptr = TRY(realloc_impl(m_data, new_capacity));
if (new_capacity < m_capacity) memcpy(ptr, m_data, new_capacity);
const usize new_byte_capacity = new_capacity * sizeof(T);
void* const ptr = TRY(realloc_impl(m_data, new_byte_capacity));
if (new_capacity < m_capacity) memcpy(ptr, m_data, new_byte_capacity);
else
memcpy(ptr, m_data, m_capacity);
memcpy(ptr, m_data, byte_capacity());
m_capacity = new_capacity;
m_data = (T*)ptr;
return {};