summaryrefslogtreecommitdiff
path: root/tools/source
diff options
context:
space:
mode:
authorNoel Grandin <noelgrandin@gmail.com>2024-03-02 16:47:10 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2024-03-02 20:11:07 +0100
commit06daf1719ef6dfb5b01d7ff09ce6df4c5c332cd5 (patch)
tree27ddc66d717d90ed30749daa535aa11601b1cfa3 /tools/source
parentf4fc510a28d11d63f6c91af6727292a53ca0517c (diff)
avoid a memcpy when constructing output of tools::JsonWriter
we can store the data in an rtl::OString object and then return that directly Change-Id: I65af6820bfd3135b38d437cf9736fffff8924e88 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164291 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'tools/source')
-rw-r--r--tools/source/misc/json_writer.cxx21
1 files changed, 12 insertions, 9 deletions
diff --git a/tools/source/misc/json_writer.cxx b/tools/source/misc/json_writer.cxx
index 86021dfcc5ca..0ffc5cb903f9 100644
--- a/tools/source/misc/json_writer.cxx
+++ b/tools/source/misc/json_writer.cxx
@@ -20,8 +20,8 @@ namespace tools
constexpr int DEFAULT_BUFFER_SIZE = 2048;
JsonWriter::JsonWriter()
- : mpBuffer(static_cast<char*>(malloc(DEFAULT_BUFFER_SIZE)))
- , mPos(mpBuffer)
+ : mpBuffer(rtl_string_alloc(DEFAULT_BUFFER_SIZE))
+ , mPos(mpBuffer->buffer)
, mSpaceAllocated(DEFAULT_BUFFER_SIZE)
, mStartNodeCount(0)
, mbFirstFieldInNode(true)
@@ -38,7 +38,7 @@ JsonWriter::JsonWriter()
JsonWriter::~JsonWriter()
{
assert(mbClosed && "forgot to extract data?");
- free(mpBuffer);
+ rtl_string_release(mpBuffer);
}
JsonWriter::ScopedJsonWriterNode<'}'> JsonWriter::startNode(std::string_view pNodeName)
@@ -291,12 +291,15 @@ void JsonWriter::addCommaBeforeField()
void JsonWriter::ensureSpace(int noMoreBytesRequired)
{
assert(!mbClosed && "already extracted data");
- int currentUsed = mPos - mpBuffer;
+ int currentUsed = mPos - mpBuffer->buffer;
if (currentUsed + noMoreBytesRequired >= mSpaceAllocated)
{
auto newSize = (currentUsed + noMoreBytesRequired) * 2;
- mpBuffer = static_cast<char*>(realloc(mpBuffer, newSize));
- mPos = mpBuffer + currentUsed;
+ rtl_String* pNewBuffer = rtl_string_alloc(newSize);
+ memcpy(pNewBuffer->buffer, mpBuffer->buffer, currentUsed);
+ rtl_string_release(mpBuffer);
+ mpBuffer = pNewBuffer;
+ mPos = mpBuffer->buffer + currentUsed;
mSpaceAllocated = newSize;
addValidationMark();
@@ -339,13 +342,13 @@ OString JsonWriter::finishAndGetAsOString()
*mPos = 0;
mbClosed = true;
- OString ret(mpBuffer, mPos - mpBuffer);
- return ret;
+ mpBuffer->length = mPos - mpBuffer->buffer;
+ return mpBuffer;
}
bool JsonWriter::isDataEquals(std::string_view s) const
{
- return std::string_view(mpBuffer, static_cast<size_t>(mPos - mpBuffer)) == s;
+ return std::string_view(mpBuffer->buffer, static_cast<size_t>(mPos - mpBuffer->buffer)) == s;
}
} // namespace tools