mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 08:44:58 +00:00
AK: Turn ByteBuffer into a value type
Previously ByteBuffer would internally hold a RefPtr to the byte buffer and would behave like a reference type, i.e. copying a ByteBuffer would not create a duplicate byte buffer, but rather two objects which refer to the same internal buffer. This also changes ByteBuffer so that it has some internal capacity much like the Vector<T> type. Unlike Vector<T> however a byte buffer's data may be uninitialized. With this commit ByteBuffer makes use of the kmalloc_good_size() API to pick an optimal allocation size for its internal buffer.
This commit is contained in:
parent
f0fa51773a
commit
fcaf98361f
9 changed files with 176 additions and 236 deletions
|
@ -21,23 +21,12 @@ inline void StringBuilder::will_append(size_t size)
|
|||
Checked<size_t> needed_capacity = m_length;
|
||||
needed_capacity += size;
|
||||
VERIFY(!needed_capacity.has_overflow());
|
||||
if (needed_capacity < inline_capacity)
|
||||
return;
|
||||
Checked<size_t> expanded_capacity = needed_capacity;
|
||||
expanded_capacity *= 2;
|
||||
VERIFY(!expanded_capacity.has_overflow());
|
||||
if (m_buffer.is_null()) {
|
||||
m_buffer.grow(expanded_capacity.value());
|
||||
memcpy(m_buffer.data(), m_inline_buffer, m_length);
|
||||
} else if (needed_capacity.value() > m_buffer.size()) {
|
||||
m_buffer.grow(expanded_capacity.value());
|
||||
}
|
||||
m_buffer.grow(needed_capacity.value());
|
||||
}
|
||||
|
||||
StringBuilder::StringBuilder(size_t initial_capacity)
|
||||
: m_buffer(decltype(m_buffer)::create_uninitialized(initial_capacity))
|
||||
{
|
||||
if (initial_capacity > inline_capacity)
|
||||
m_buffer.grow(initial_capacity);
|
||||
}
|
||||
|
||||
void StringBuilder::append(const StringView& str)
|
||||
|
@ -94,7 +83,6 @@ StringView StringBuilder::string_view() const
|
|||
void StringBuilder::clear()
|
||||
{
|
||||
m_buffer.clear();
|
||||
m_inline_buffer[0] = '\0';
|
||||
m_length = 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue