2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-02-09 15:08:43 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-08-06 18:04:12 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2020-05-06 15:40:06 +00:00
|
|
|
#include <Kernel/KBufferBuilder.h>
|
2019-08-06 18:04:12 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-09-17 19:51:09 +00:00
|
|
|
inline bool KBufferBuilder::check_expand(size_t size)
|
2019-08-06 18:04:12 +00:00
|
|
|
{
|
2020-12-18 13:10:10 +00:00
|
|
|
if (!m_buffer)
|
|
|
|
return false;
|
2020-09-17 19:51:09 +00:00
|
|
|
if ((m_size + size) < m_buffer->capacity())
|
|
|
|
return true;
|
|
|
|
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-08-06 11:49:36 +00:00
|
|
|
new_buffer_size = Memory::page_round_up(new_buffer_size + 1 * MiB);
|
2020-09-17 19:51:09 +00:00
|
|
|
return m_buffer->expand(new_buffer_size);
|
2019-08-06 18:04:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 19:51:09 +00:00
|
|
|
bool KBufferBuilder::flush()
|
2019-08-06 18:04:12 +00:00
|
|
|
{
|
2020-09-05 21:52:14 +00:00
|
|
|
if (!m_buffer)
|
2020-09-17 19:51:09 +00:00
|
|
|
return false;
|
|
|
|
m_buffer->set_size(m_size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
OwnPtr<KBuffer> KBufferBuilder::build()
|
|
|
|
{
|
|
|
|
if (!flush())
|
2020-09-05 21:52:14 +00:00
|
|
|
return {};
|
2021-05-13 06:34:00 +00:00
|
|
|
|
2021-06-20 08:21:16 +00:00
|
|
|
return try_make<KBuffer>(move(m_buffer));
|
2019-08-06 18:04:12 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 13:16:25 +00:00
|
|
|
KBufferBuilder::KBufferBuilder()
|
2021-08-06 20:25:00 +00:00
|
|
|
: m_buffer(KBufferImpl::try_create_with_size(4 * MiB, Memory::Region::Access::ReadWrite))
|
2019-08-06 18:04:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-17 19:51:09 +00:00
|
|
|
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 18:04:12 +00:00
|
|
|
void KBufferBuilder::append(const StringView& str)
|
|
|
|
{
|
|
|
|
if (str.is_empty())
|
|
|
|
return;
|
2020-09-17 19:51:09 +00:00
|
|
|
if (!check_expand(str.length()))
|
2019-08-06 18:04:12 +00: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 19:51:09 +00:00
|
|
|
if (!check_expand(length))
|
2019-08-06 18:04:12 +00:00
|
|
|
return;
|
2020-07-02 08:14:48 +00:00
|
|
|
memcpy(insertion_ptr(), characters, length);
|
2019-08-06 18:04:12 +00:00
|
|
|
m_size += length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KBufferBuilder::append(char ch)
|
|
|
|
{
|
2020-09-17 19:51:09 +00:00
|
|
|
if (!check_expand(1))
|
2019-08-06 18:04:12 +00:00
|
|
|
return;
|
|
|
|
insertion_ptr()[0] = ch;
|
|
|
|
m_size += 1;
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:56:36 +00: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 00:27:42 +00:00
|
|
|
}
|