From 4fb4afa5b0268f9fd6fe40c780433449f1a7b1d5 Mon Sep 17 00:00:00 2001 From: Brian McKenna Date: Fri, 15 Nov 2024 11:00:00 +1100 Subject: [PATCH] Move arrays to heap, to reduce stack usage I keep hitting stack issues on Windows. Let's just start by reducing the stack by quite a bit. --- src/libutil/archive.cc | 8 ++++---- src/libutil/file-system.cc | 6 +++--- src/libutil/posix-source-accessor.cc | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/libutil/archive.cc b/src/libutil/archive.cc index 20d8a1e09be..144460ff576 100644 --- a/src/libutil/archive.cc +++ b/src/libutil/archive.cc @@ -142,14 +142,14 @@ static void parseContents(CreateRegularFileSink & sink, Source & source) sink.preallocateContents(size); uint64_t left = size; - std::array buf; + auto buf = std::make_unique>(); while (left) { checkInterrupt(); - auto n = buf.size(); + auto n = buf->size(); if ((uint64_t)n > left) n = left; - source(buf.data(), n); - sink({buf.data(), n}); + source(buf->data(), n); + sink({buf->data(), n}); left -= n; } diff --git a/src/libutil/file-system.cc b/src/libutil/file-system.cc index 923220fd0b2..7d9c0eac524 100644 --- a/src/libutil/file-system.cc +++ b/src/libutil/file-system.cc @@ -299,13 +299,13 @@ void writeFile(const Path & path, Source & source, mode_t mode, bool sync) if (!fd) throw SysError("opening file '%1%'", path); - std::array buf; + auto buf = std::make_unique>(); try { while (true) { try { - auto n = source.read(buf.data(), buf.size()); - writeFull(fd.get(), {buf.data(), n}); + auto n = source.read(buf->data(), buf->size()); + writeFull(fd.get(), {buf->data(), n}); } catch (EndOfFile &) { break; } } } catch (Error & e) { diff --git a/src/libutil/posix-source-accessor.cc b/src/libutil/posix-source-accessor.cc index 8ee986d3fe9..dc53be58257 100644 --- a/src/libutil/posix-source-accessor.cc +++ b/src/libutil/posix-source-accessor.cc @@ -64,10 +64,10 @@ void PosixSourceAccessor::readFile( off_t left = st.st_size; - std::array buf; + auto buf = std::make_unique>(); while (left) { checkInterrupt(); - ssize_t rd = read(fromDescriptorReadOnly(fd.get()), buf.data(), (size_t) std::min(left, (off_t) buf.size())); + ssize_t rd = read(fromDescriptorReadOnly(fd.get()), buf->data(), (size_t) std::min(left, (off_t) buf->size())); if (rd == -1) { if (errno != EINTR) throw SysError("reading from file '%s'", showPath(path)); @@ -76,7 +76,7 @@ void PosixSourceAccessor::readFile( throw SysError("unexpected end-of-file reading '%s'", showPath(path)); else { assert(rd <= left); - sink({(char *) buf.data(), (size_t) rd}); + sink({(char *) buf->data(), (size_t) rd}); left -= rd; } }