mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:07:36 +00:00
AK: Add safe memset() wrapper to ByteBuffer
In the interest memory safety and of removing as many of foot guns as possible (like raw memset's which are notorious for typo's), a zeroing method seems like a useful utility to have on a buffer class.
This commit is contained in:
parent
86a3363ddf
commit
5e84320ecb
1 changed files with 12 additions and 0 deletions
|
@ -89,6 +89,8 @@ public:
|
||||||
|
|
||||||
void grow(size_t size);
|
void grow(size_t size);
|
||||||
|
|
||||||
|
void zero_fill();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit ByteBufferImpl(size_t);
|
explicit ByteBufferImpl(size_t);
|
||||||
ByteBufferImpl(const void*, size_t);
|
ByteBufferImpl(const void*, size_t);
|
||||||
|
@ -248,6 +250,11 @@ public:
|
||||||
__builtin_memcpy(this->data() + offset, data, data_size);
|
__builtin_memcpy(this->data() + offset, data, data_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void zero_fill()
|
||||||
|
{
|
||||||
|
m_impl->zero_fill();
|
||||||
|
}
|
||||||
|
|
||||||
operator Bytes() { return bytes(); }
|
operator Bytes() { return bytes(); }
|
||||||
operator ReadonlyBytes() const { return bytes(); }
|
operator ReadonlyBytes() const { return bytes(); }
|
||||||
|
|
||||||
|
@ -295,6 +302,11 @@ inline void ByteBufferImpl::grow(size_t size)
|
||||||
kfree(old_data);
|
kfree(old_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void ByteBufferImpl::zero_fill()
|
||||||
|
{
|
||||||
|
__builtin_memset(m_data, 0, m_size);
|
||||||
|
}
|
||||||
|
|
||||||
inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::create_uninitialized(size_t size)
|
inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::create_uninitialized(size_t size)
|
||||||
{
|
{
|
||||||
return ::adopt(*new ByteBufferImpl(size));
|
return ::adopt(*new ByteBufferImpl(size));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue