diff options
author | Brian Gianforcaro <b.gianfo@gmail.com> | 2021-02-21 02:07:14 -0800 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-02-21 11:52:47 +0100 |
commit | 5e84320ecb87a96d00b768b4d4b9277fb9e5f7d0 (patch) | |
tree | ad2490bc1deafe6915f94c58ac12affe1e5088e1 /AK | |
parent | 86a3363ddfa4ad8c6b4b71a07383563a9669f424 (diff) | |
download | serenity-5e84320ecb87a96d00b768b4d4b9277fb9e5f7d0.zip |
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.
Diffstat (limited to 'AK')
-rw-r--r-- | AK/ByteBuffer.h | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/AK/ByteBuffer.h b/AK/ByteBuffer.h index b4b05e87fc..feabaeaa4c 100644 --- a/AK/ByteBuffer.h +++ b/AK/ByteBuffer.h @@ -89,6 +89,8 @@ public: void grow(size_t size); + void zero_fill(); + private: explicit ByteBufferImpl(size_t); ByteBufferImpl(const void*, size_t); @@ -248,6 +250,11 @@ public: __builtin_memcpy(this->data() + offset, data, data_size); } + void zero_fill() + { + m_impl->zero_fill(); + } + operator Bytes() { return bytes(); } operator ReadonlyBytes() const { return bytes(); } @@ -295,6 +302,11 @@ inline void ByteBufferImpl::grow(size_t size) kfree(old_data); } +inline void ByteBufferImpl::zero_fill() +{ + __builtin_memset(m_data, 0, m_size); +} + inline NonnullRefPtr<ByteBufferImpl> ByteBufferImpl::create_uninitialized(size_t size) { return ::adopt(*new ByteBufferImpl(size)); |