Skip to content

Commit 4fb4afa

Browse files
committed
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.
1 parent 2cb0ddf commit 4fb4afa

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

src/libutil/archive.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,14 @@ static void parseContents(CreateRegularFileSink & sink, Source & source)
142142
sink.preallocateContents(size);
143143

144144
uint64_t left = size;
145-
std::array<char, 65536> buf;
145+
auto buf = std::make_unique<std::array<char, 65536>>();
146146

147147
while (left) {
148148
checkInterrupt();
149-
auto n = buf.size();
149+
auto n = buf->size();
150150
if ((uint64_t)n > left) n = left;
151-
source(buf.data(), n);
152-
sink({buf.data(), n});
151+
source(buf->data(), n);
152+
sink({buf->data(), n});
153153
left -= n;
154154
}
155155

src/libutil/file-system.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,13 @@ void writeFile(const Path & path, Source & source, mode_t mode, bool sync)
299299
if (!fd)
300300
throw SysError("opening file '%1%'", path);
301301

302-
std::array<char, 64 * 1024> buf;
302+
auto buf = std::make_unique<std::array<char, 64 * 1024>>();
303303

304304
try {
305305
while (true) {
306306
try {
307-
auto n = source.read(buf.data(), buf.size());
308-
writeFull(fd.get(), {buf.data(), n});
307+
auto n = source.read(buf->data(), buf->size());
308+
writeFull(fd.get(), {buf->data(), n});
309309
} catch (EndOfFile &) { break; }
310310
}
311311
} catch (Error & e) {

src/libutil/posix-source-accessor.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ void PosixSourceAccessor::readFile(
6464

6565
off_t left = st.st_size;
6666

67-
std::array<unsigned char, 64 * 1024> buf;
67+
auto buf = std::make_unique<std::array<unsigned char, 64 * 1024>>();
6868
while (left) {
6969
checkInterrupt();
70-
ssize_t rd = read(fromDescriptorReadOnly(fd.get()), buf.data(), (size_t) std::min(left, (off_t) buf.size()));
70+
ssize_t rd = read(fromDescriptorReadOnly(fd.get()), buf->data(), (size_t) std::min(left, (off_t) buf->size()));
7171
if (rd == -1) {
7272
if (errno != EINTR)
7373
throw SysError("reading from file '%s'", showPath(path));
@@ -76,7 +76,7 @@ void PosixSourceAccessor::readFile(
7676
throw SysError("unexpected end-of-file reading '%s'", showPath(path));
7777
else {
7878
assert(rd <= left);
79-
sink({(char *) buf.data(), (size_t) rd});
79+
sink({(char *) buf->data(), (size_t) rd});
8080
left -= rd;
8181
}
8282
}

0 commit comments

Comments
 (0)