AK: Fix ByteBuffer zero bytes allocations

This commit is contained in:
Tom 2020-08-30 09:00:57 -06:00 committed by Andreas Kling
parent 9ad5a261f7
commit f5bc7dbfda

View file

@ -256,6 +256,7 @@ private:
inline ByteBufferImpl::ByteBufferImpl(size_t size)
: m_size(size)
{
if (size != 0)
m_data = static_cast<u8*>(kmalloc(size));
m_owned = true;
}
@ -264,8 +265,10 @@ inline ByteBufferImpl::ByteBufferImpl(const void* data, size_t size, Constructio
: m_size(size)
{
ASSERT(mode == Copy);
if (size != 0) {
m_data = static_cast<u8*>(kmalloc(size));
__builtin_memcpy(m_data, data, size);
}
m_owned = true;
}
@ -284,11 +287,19 @@ inline void ByteBufferImpl::grow(size_t size)
{
ASSERT(size > m_size);
ASSERT(m_owned);
if (size == 0) {
if (m_data)
kfree(m_data);
m_data = nullptr;
m_size = 0;
return;
}
u8* new_data = static_cast<u8*>(kmalloc(size));
__builtin_memcpy(new_data, m_data, m_size);
u8* old_data = m_data;
m_data = new_data;
m_size = size;
if (old_data)
kfree(old_data);
}
@ -300,6 +311,7 @@ inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::create_uninitialized(size_t
inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::create_zeroed(size_t size)
{
auto buffer = ::adopt(*new ByteBufferImpl(size));
if (size != 0)
__builtin_memset(buffer->data(), 0, size);
return buffer;
}