aboutsummaryrefslogtreecommitdiff
path: root/src/libutil
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2022-03-11 11:52:39 +0100
committerGitHub <noreply@github.com>2022-03-11 11:52:39 +0100
commit31a392dfe2c89a3cb5880eea9e4070ece4e14acb (patch)
tree7c6c6cd5deecea9997589e59f8cda225566e2eb7 /src/libutil
parent167766b65ca203238f733efd0b5df92d28ab66c4 (diff)
parent4d629c4f7abbbe58dfe6d9d2b37541cdf2331606 (diff)
Merge pull request #5865 from pennae/memory-friendliness
be more memory friendly
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/error.hh1
-rw-r--r--src/libutil/finally.hh7
-rw-r--r--src/libutil/util.cc15
3 files changed, 12 insertions, 11 deletions
diff --git a/src/libutil/error.hh b/src/libutil/error.hh
index 600e94888..93b789f0b 100644
--- a/src/libutil/error.hh
+++ b/src/libutil/error.hh
@@ -54,6 +54,7 @@ typedef enum {
lvlVomit
} Verbosity;
+/* adjust Pos::origin bit width when adding stuff here */
typedef enum {
foFile,
foStdin,
diff --git a/src/libutil/finally.hh b/src/libutil/finally.hh
index 7760cfe9a..dee2e8d2f 100644
--- a/src/libutil/finally.hh
+++ b/src/libutil/finally.hh
@@ -1,14 +1,13 @@
#pragma once
-#include <functional>
-
/* A trivial class to run a function at the end of a scope. */
+template<typename Fn>
class Finally
{
private:
- std::function<void()> fun;
+ Fn fun;
public:
- Finally(std::function<void()> fun) : fun(fun) { }
+ Finally(Fn fun) : fun(std::move(fun)) { }
~Finally() { fun(); }
};
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index b833038a9..9f13d5f02 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -682,7 +682,14 @@ std::string drainFD(int fd, bool block, const size_t reserveSize)
void drainFD(int fd, Sink & sink, bool block)
{
- int saved;
+ // silence GCC maybe-uninitialized warning in finally
+ int saved = 0;
+
+ if (!block) {
+ saved = fcntl(fd, F_GETFL);
+ if (fcntl(fd, F_SETFL, saved | O_NONBLOCK) == -1)
+ throw SysError("making file descriptor non-blocking");
+ }
Finally finally([&]() {
if (!block) {
@@ -691,12 +698,6 @@ void drainFD(int fd, Sink & sink, bool block)
}
});
- if (!block) {
- saved = fcntl(fd, F_GETFL);
- if (fcntl(fd, F_SETFL, saved | O_NONBLOCK) == -1)
- throw SysError("making file descriptor non-blocking");
- }
-
std::vector<unsigned char> buf(64 * 1024);
while (1) {
checkInterrupt();