~siborgium/prelockdpp

4f6dbb3c0f604303a625761d19e9ae3c1f6cca9c — Sergey Smirnykh 2 years ago 6cfa14c
Add DEFER macro, fix dir leak
1 files changed, 16 insertions(+), 7 deletions(-)

M main.cpp
M main.cpp => main.cpp +16 -7
@@ 20,6 20,18 @@
#include <signal.h>
#include <unistd.h>

template <typename F> struct finally {
    F f;
    finally(F&& f): f{ f } {}
    ~finally() {
        f();
    }
};
template <typename F> finally(F&&) -> finally<F>;

#define CONCAT(x, y) x##y
#define DEFER        finally CONCAT(_defer_, __COUNTER__) = [&]()

namespace fs = std::filesystem;

constexpr std::size_t MiB = 1024 * 1024;


@@ 153,7 165,7 @@ struct context {
        if (fd == -1) {
            throw errno_exception("open(/proc/uptime) failed", errno);
        }
        base_fd guard { fd };
        DEFER { close(fd); };

        // see /fs/proc/uptime.c
        // time is encoded as %lu.%2lu ...


@@ 187,12 199,9 @@ struct context {
                throw std::logic_error{ "Unexpected /proc/uptime fmt" };
            }

            unsigned long sec, nsec;
            from_chars(begin, end1, sec);
            from_chars(end1 + 1, end, nsec);
            timespec uptime;
            uptime.tv_sec = sec;
            uptime.tv_nsec = nsec;
            from_chars(begin, end1, uptime.tv_sec);
            from_chars(end1 + 1, begin, uptime.tv_nsec);
            return uptime;
        }
        throw std::logic_error{ "read(/proc/uptime) was empty" };


@@ 204,6 213,7 @@ struct context {
        if (!dir) {
            throw errno_exception("opendir(/proc) failed", errno);
        }
        DEFER{ closedir(dir); };
        errno = 0;
        auto* pid = &pids.emplace_back(self_pid);
        while (dirent* entry = readdir(dir)) {


@@ 219,7 229,6 @@ struct context {
        if (errno) {
            throw errno_exception("readdir(proc) failed", errno);
        }
        closedir(dir);
        return pids;
    }
    auto get_uniq_id(int pid) {