AK: Fix FixedArray zero bytes allocations

This commit is contained in:
Tom 2020-08-30 09:01:20 -06:00 committed by Andreas Kling
parent f5bc7dbfda
commit 80560d1be3
Notes: sideshowbarker 2024-07-19 02:59:56 +09:00

View file

@ -38,10 +38,12 @@ public:
explicit FixedArray(size_t size)
: m_size(size)
{
if (m_size != 0) {
m_elements = (T*)kmalloc(sizeof(T) * m_size);
for (size_t i = 0; i < m_size; ++i)
new (&m_elements[i]) T();
}
}
~FixedArray()
{
clear();
@ -50,10 +52,12 @@ public:
FixedArray(const FixedArray& other)
: m_size(other.m_size)
{
if (m_size != 0) {
m_elements = (T*)kmalloc(sizeof(T) * m_size);
for (size_t i = 0; i < m_size; ++i)
new (&m_elements[i]) T(other[i]);
}
}
FixedArray& operator=(const FixedArray& other)
{
@ -106,6 +110,10 @@ public:
{
if (new_size == m_size)
return;
if (new_size == 0) {
clear();
return;
}
auto* new_elements = (T*)kmalloc(new_size * sizeof(T));
for (size_t i = 0; i < min(new_size, m_size); ++i)
new (&new_elements[i]) T(move(m_elements[i]));