Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for writing arbitrary long strings in LineWriter #15

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/interpret/heaptrack_interpret.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ struct AccumulatedTraceData
if (internedString) {
*internedString = it->first.data();
}
out.write("s %s\n", str.c_str());
out.write("s ");
out.write(str);
out.write("\n");
return id;
}

Expand Down
23 changes: 23 additions & 0 deletions src/util/linewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,29 @@ class LineWriter
return write("%s", line);
}

/**
* write an arbitrary string literal to the buffer
*/
bool write(const std::string& line)
{
const auto length = line.length();
if (availableSpace() < length) {
if (!flush()) {
return false;
}
if (availableSpace() < length) {
int ret = 0;
do {
ret = ::write(fd, line.c_str(), length);
} while (ret < 0 && errno == EINTR);
return ret >= 0;
}
}
memcpy(out(), line.c_str(), length);
bufferSize += length;
return true;
}

/**
* write one of the common heaptrack output lines to the buffer
*
Expand Down
10 changes: 10 additions & 0 deletions tests/auto/tst_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ TEST_CASE ("buffered write", "[write]") {
for (unsigned i = 0; i < 10000; ++i) {
REQUIRE(writer.write("%d %x\n", 42, 42));
expectedContents += "42 2a\n";
if (i % 1000 == 0) {
const string longString(LineWriter::BUFFER_CAPACITY * 2, '*');
REQUIRE(writer.write(longString));
expectedContents += longString;
}
}
for (unsigned i = 0; i < LineWriter::BUFFER_CAPACITY * 2; ++i) {
const string longString(i, '*');
REQUIRE(writer.write(longString));
expectedContents += longString;
}
REQUIRE(expectedContents.size() > LineWriter::BUFFER_CAPACITY);
REQUIRE(writer.flush());
Expand Down