aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/serialise.cc
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2021-02-25 21:58:41 +0000
committerJohn Ericson <John.Ericson@Obsidian.Systems>2021-02-25 21:58:41 +0000
commit90d76fa399de4e207ea14ec4c0dd65434f60c152 (patch)
tree3c52e982cba5bcf7b91c99d1b63ba967b4ea8b92 /src/libutil/serialise.cc
parent4636cc9a1f6de70947abbfb17a0ad91981d1cad7 (diff)
parentca0994819d68aee26a2906c37a47ae609ac46c4c (diff)
Merge remote-tracking branch 'obsidian/path-info' into ca-drv-exotic
Diffstat (limited to 'src/libutil/serialise.cc')
-rw-r--r--src/libutil/serialise.cc135
1 files changed, 83 insertions, 52 deletions
diff --git a/src/libutil/serialise.cc b/src/libutil/serialise.cc
index 5c9f6f901..d1a16b6ba 100644
--- a/src/libutil/serialise.cc
+++ b/src/libutil/serialise.cc
@@ -11,23 +11,23 @@
namespace nix {
-void BufferedSink::operator () (const unsigned char * data, size_t len)
+void BufferedSink::operator () (std::string_view data)
{
- if (!buffer) buffer = decltype(buffer)(new unsigned char[bufSize]);
+ if (!buffer) buffer = decltype(buffer)(new char[bufSize]);
- while (len) {
+ while (!data.empty()) {
/* Optimisation: bypass the buffer if the data exceeds the
buffer size. */
- if (bufPos + len >= bufSize) {
+ if (bufPos + data.size() >= bufSize) {
flush();
- write(data, len);
+ write(data);
break;
}
/* Otherwise, copy the bytes to the buffer. Flush the buffer
when it's full. */
- size_t n = bufPos + len > bufSize ? bufSize - bufPos : len;
- memcpy(buffer.get() + bufPos, data, n);
- data += n; bufPos += n; len -= n;
+ size_t n = bufPos + data.size() > bufSize ? bufSize - bufPos : data.size();
+ memcpy(buffer.get() + bufPos, data.data(), n);
+ data.remove_prefix(n); bufPos += n;
if (bufPos == bufSize) flush();
}
}
@@ -38,7 +38,7 @@ void BufferedSink::flush()
if (bufPos == 0) return;
size_t n = bufPos;
bufPos = 0; // don't trigger the assert() in ~BufferedSink()
- write(buffer.get(), n);
+ write({buffer.get(), n});
}
@@ -52,16 +52,13 @@ size_t threshold = 256 * 1024 * 1024;
static void warnLargeDump()
{
- logWarning({
- .name = "Large path",
- .description = "dumping very large path (> 256 MiB); this may run out of memory"
- });
+ warn("dumping very large path (> 256 MiB); this may run out of memory");
}
-void FdSink::write(const unsigned char * data, size_t len)
+void FdSink::write(std::string_view data)
{
- written += len;
+ written += data.size();
static bool warned = false;
if (warn && !warned) {
if (written > threshold) {
@@ -70,7 +67,7 @@ void FdSink::write(const unsigned char * data, size_t len)
}
}
try {
- writeFull(fd, data, len);
+ writeFull(fd, data);
} catch (SysError & e) {
_good = false;
throw;
@@ -84,7 +81,7 @@ bool FdSink::good()
}
-void Source::operator () (unsigned char * data, size_t len)
+void Source::operator () (char * data, size_t len)
{
while (len) {
size_t n = read(data, len);
@@ -96,12 +93,12 @@ void Source::operator () (unsigned char * data, size_t len)
void Source::drainInto(Sink & sink)
{
std::string s;
- std::vector<unsigned char> buf(8192);
+ std::vector<char> buf(8192);
while (true) {
size_t n;
try {
n = read(buf.data(), buf.size());
- sink(buf.data(), n);
+ sink({buf.data(), n});
} catch (EndOfFile &) {
break;
}
@@ -117,9 +114,9 @@ std::string Source::drain()
}
-size_t BufferedSource::read(unsigned char * data, size_t len)
+size_t BufferedSource::read(char * data, size_t len)
{
- if (!buffer) buffer = decltype(buffer)(new unsigned char[bufSize]);
+ if (!buffer) buffer = decltype(buffer)(new char[bufSize]);
if (!bufPosIn) bufPosIn = readUnbuffered(buffer.get(), bufSize);
@@ -138,12 +135,12 @@ bool BufferedSource::hasData()
}
-size_t FdSource::readUnbuffered(unsigned char * data, size_t len)
+size_t FdSource::readUnbuffered(char * data, size_t len)
{
ssize_t n;
do {
checkInterrupt();
- n = ::read(fd, (char *) data, len);
+ n = ::read(fd, data, len);
} while (n == -1 && errno == EINTR);
if (n == -1) { _good = false; throw SysError("reading from file"); }
if (n == 0) { _good = false; throw EndOfFile("unexpected end-of-file"); }
@@ -158,10 +155,10 @@ bool FdSource::good()
}
-size_t StringSource::read(unsigned char * data, size_t len)
+size_t StringSource::read(char * data, size_t len)
{
if (pos == s.size()) throw EndOfFile("end of string reached");
- size_t n = s.copy((char *) data, len, pos);
+ size_t n = s.copy(data, len, pos);
pos += n;
return n;
}
@@ -171,6 +168,39 @@ size_t StringSource::read(unsigned char * data, size_t len)
#error Coroutines are broken in this version of Boost!
#endif
+/* A concrete datatype allow virtual dispatch of stack allocation methods. */
+struct VirtualStackAllocator {
+ StackAllocator *allocator = StackAllocator::defaultAllocator;
+
+ boost::context::stack_context allocate() {
+ return allocator->allocate();
+ }
+
+ void deallocate(boost::context::stack_context sctx) {
+ allocator->deallocate(sctx);
+ }
+};
+
+
+/* This class reifies the default boost coroutine stack allocation strategy with
+ a virtual interface. */
+class DefaultStackAllocator : public StackAllocator {
+ boost::coroutines2::default_stack stack;
+
+ boost::context::stack_context allocate() {
+ return stack.allocate();
+ }
+
+ void deallocate(boost::context::stack_context sctx) {
+ stack.deallocate(sctx);
+ }
+};
+
+static DefaultStackAllocator defaultAllocatorSingleton;
+
+StackAllocator *StackAllocator::defaultAllocator = &defaultAllocatorSingleton;
+
+
std::unique_ptr<Source> sinkToSource(
std::function<void(Sink &)> fun,
std::function<void()> eof)
@@ -192,13 +222,13 @@ std::unique_ptr<Source> sinkToSource(
std::string cur;
size_t pos = 0;
- size_t read(unsigned char * data, size_t len) override
+ size_t read(char * data, size_t len) override
{
if (!coro)
- coro = coro_t::pull_type([&](coro_t::push_type & yield) {
- LambdaSink sink([&](const unsigned char * data, size_t len) {
- if (len) yield(std::string((const char *) data, len));
- });
+ coro = coro_t::pull_type(VirtualStackAllocator{}, [&](coro_t::push_type & yield) {
+ LambdaSink sink([&](std::string_view data) {
+ if (!data.empty()) yield(std::string(data));
+ });
fun(sink);
});
@@ -211,7 +241,7 @@ std::unique_ptr<Source> sinkToSource(
}
auto n = std::min(cur.size() - pos, len);
- memcpy(data, (unsigned char *) cur.data() + pos, n);
+ memcpy(data, cur.data() + pos, n);
pos += n;
return n;
@@ -225,24 +255,24 @@ std::unique_ptr<Source> sinkToSource(
void writePadding(size_t len, Sink & sink)
{
if (len % 8) {
- unsigned char zero[8];
+ char zero[8];
memset(zero, 0, sizeof(zero));
- sink(zero, 8 - (len % 8));
+ sink({zero, 8 - (len % 8)});
}
}
-void writeString(const unsigned char * buf, size_t len, Sink & sink)
+void writeString(std::string_view data, Sink & sink)
{
- sink << len;
- sink(buf, len);
- writePadding(len, sink);
+ sink << data.size();
+ sink(data);
+ writePadding(data.size(), sink);
}
Sink & operator << (Sink & sink, const string & s)
{
- writeString((const unsigned char *) s.data(), s.size(), sink);
+ writeString(s, sink);
return sink;
}
@@ -273,8 +303,7 @@ Sink & operator << (Sink & sink, const Error & ex)
<< "Error"
<< info.level
<< info.name
- << info.description
- << (info.hint ? info.hint->str() : "")
+ << info.msg.str()
<< 0 // FIXME: info.errPos
<< info.traces.size();
for (auto & trace : info.traces) {
@@ -288,7 +317,7 @@ Sink & operator << (Sink & sink, const Error & ex)
void readPadding(size_t len, Source & source)
{
if (len % 8) {
- unsigned char zero[8];
+ char zero[8];
size_t n = 8 - (len % 8);
source(zero, n);
for (unsigned int i = 0; i < n; i++)
@@ -297,7 +326,7 @@ void readPadding(size_t len, Source & source)
}
-size_t readString(unsigned char * buf, size_t max, Source & source)
+size_t readString(char * buf, size_t max, Source & source)
{
auto len = readNum<size_t>(source);
if (len > max) throw SerialisationError("string is too long");
@@ -312,7 +341,7 @@ string readString(Source & source, size_t max)
auto len = readNum<size_t>(source);
if (len > max) throw SerialisationError("string is too long");
std::string res(len, 0);
- source((unsigned char*) res.data(), len);
+ source(res.data(), len);
readPadding(len, source);
return res;
}
@@ -341,12 +370,14 @@ Error readError(Source & source)
{
auto type = readString(source);
assert(type == "Error");
- ErrorInfo info;
- info.level = (Verbosity) readInt(source);
- info.name = readString(source);
- info.description = readString(source);
- auto hint = readString(source);
- if (hint != "") info.hint = hintformat(std::move(format("%s") % hint));
+ auto level = (Verbosity) readInt(source);
+ auto name = readString(source);
+ auto msg = readString(source);
+ ErrorInfo info {
+ .level = level,
+ .name = name,
+ .msg = hintformat(std::move(format("%s") % msg)),
+ };
auto havePos = readNum<size_t>(source);
assert(havePos == 0);
auto nrTraces = readNum<size_t>(source);
@@ -361,17 +392,17 @@ Error readError(Source & source)
}
-void StringSink::operator () (const unsigned char * data, size_t len)
+void StringSink::operator () (std::string_view data)
{
static bool warned = false;
if (!warned && s->size() > threshold) {
warnLargeDump();
warned = true;
}
- s->append((const char *) data, len);
+ s->append(data);
}
-size_t ChainSource::read(unsigned char * data, size_t len)
+size_t ChainSource::read(char * data, size_t len)
{
if (useSecond) {
return source2.read(data, len);