2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-02-09 18:08:43 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-08-06 21:04:12 +03:00
|
|
|
#include <AK/StdLibExtras.h>
|
2020-05-06 18:40:06 +03:00
|
|
|
#include <Kernel/KBufferBuilder.h>
|
2019-08-06 21:04:12 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-09-17 22:51:09 +03:00
|
|
|
inline bool KBufferBuilder::check_expand(size_t size)
|
2019-08-06 21:04:12 +03:00
|
|
|
{
|
2020-12-18 16:10:10 +03:00
|
|
|
if (!m_buffer)
|
|
|
|
return false;
|
2020-09-17 22:51:09 +03:00
|
|
|
if ((m_size + size) < m_buffer->capacity())
|
|
|
|
return true;
|
|
|
|
if (!m_can_expand)
|
|
|
|
return false;
|
|
|
|
if (Checked<size_t>::addition_would_overflow(m_size, size))
|
|
|
|
return false;
|
|
|
|
size_t new_buffer_size = m_size + size;
|
|
|
|
if (Checked<size_t>::addition_would_overflow(new_buffer_size, 1 * MiB))
|
|
|
|
return false;
|
2021-02-14 11:57:19 +03:00
|
|
|
new_buffer_size = page_round_up(new_buffer_size + 1 * MiB);
|
2020-09-17 22:51:09 +03:00
|
|
|
return m_buffer->expand(new_buffer_size);
|
2019-08-06 21:04:12 +03:00
|
|
|
}
|
|
|
|
|
2020-09-17 22:51:09 +03:00
|
|
|
bool KBufferBuilder::flush()
|
2019-08-06 21:04:12 +03:00
|
|
|
{
|
2020-09-06 00:52:14 +03:00
|
|
|
if (!m_buffer)
|
2020-09-17 22:51:09 +03:00
|
|
|
return false;
|
|
|
|
m_buffer->set_size(m_size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
OwnPtr<KBuffer> KBufferBuilder::build()
|
|
|
|
{
|
|
|
|
if (!flush())
|
2020-09-06 00:52:14 +03:00
|
|
|
return {};
|
2021-05-13 09:34:00 +03:00
|
|
|
|
|
|
|
return adopt_own_if_nonnull(new KBuffer(move(m_buffer)));
|
2019-08-06 21:04:12 +03:00
|
|
|
}
|
|
|
|
|
2020-09-17 22:51:09 +03:00
|
|
|
KBufferBuilder::KBufferBuilder(bool can_expand)
|
|
|
|
: m_buffer(KBufferImpl::try_create_with_size(4 * MiB, Region::Access::Read | Region::Access::Write))
|
|
|
|
, m_can_expand(can_expand)
|
2019-08-06 21:04:12 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-17 22:51:09 +03:00
|
|
|
KBufferBuilder::KBufferBuilder(RefPtr<KBufferImpl>& buffer, bool can_expand)
|
|
|
|
: m_buffer(buffer)
|
|
|
|
, m_can_expand(can_expand)
|
|
|
|
{
|
|
|
|
if (!m_buffer)
|
|
|
|
m_buffer = buffer = KBufferImpl::try_create_with_size(4 * MiB, Region::Access::Read | Region::Access::Write);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KBufferBuilder::append_bytes(ReadonlyBytes bytes)
|
|
|
|
{
|
|
|
|
if (!check_expand(bytes.size()))
|
|
|
|
return;
|
|
|
|
memcpy(insertion_ptr(), bytes.data(), bytes.size());
|
|
|
|
m_size += bytes.size();
|
|
|
|
}
|
|
|
|
|
2019-08-06 21:04:12 +03:00
|
|
|
void KBufferBuilder::append(const StringView& str)
|
|
|
|
{
|
|
|
|
if (str.is_empty())
|
|
|
|
return;
|
2020-09-17 22:51:09 +03:00
|
|
|
if (!check_expand(str.length()))
|
2019-08-06 21:04:12 +03:00
|
|
|
return;
|
|
|
|
memcpy(insertion_ptr(), str.characters_without_null_termination(), str.length());
|
|
|
|
m_size += str.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KBufferBuilder::append(const char* characters, int length)
|
|
|
|
{
|
|
|
|
if (!length)
|
|
|
|
return;
|
2020-09-17 22:51:09 +03:00
|
|
|
if (!check_expand(length))
|
2019-08-06 21:04:12 +03:00
|
|
|
return;
|
2020-07-02 11:14:48 +03:00
|
|
|
memcpy(insertion_ptr(), characters, length);
|
2019-08-06 21:04:12 +03:00
|
|
|
m_size += length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KBufferBuilder::append(char ch)
|
|
|
|
{
|
2020-09-17 22:51:09 +03:00
|
|
|
if (!check_expand(1))
|
2019-08-06 21:04:12 +03:00
|
|
|
return;
|
|
|
|
insertion_ptr()[0] = ch;
|
|
|
|
m_size += 1;
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:56:36 +03:00
|
|
|
void KBufferBuilder::append_escaped_for_json(const StringView& string)
|
|
|
|
{
|
|
|
|
for (auto ch : string) {
|
|
|
|
switch (ch) {
|
|
|
|
case '\e':
|
|
|
|
append("\\u001B");
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
append("\\b");
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
append("\\n");
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
append("\\t");
|
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
append("\\\"");
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
append("\\\\");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
append(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|