This is an automated email from the git hooks/post-receive script. guillem pushed a commit to branch main in repository dpkg.
View the commit online: https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=174aeca1de281178bcb973a8cac814a5ad41fb2b commit 174aeca1de281178bcb973a8cac814a5ad41fb2b Author: Guillem Jover <[email protected]> AuthorDate: Sat Nov 19 02:52:13 2022 +0100 libdpkg: Refactor meminfo gathering into a filename generic function This will make it possible to unit test the function. --- lib/dpkg/libdpkg.map | 1 + lib/dpkg/meminfo.c | 12 +++++++----- lib/dpkg/meminfo.h | 2 ++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/dpkg/libdpkg.map b/lib/dpkg/libdpkg.map index a031d8d11..74edd20b5 100644 --- a/lib/dpkg/libdpkg.map +++ b/lib/dpkg/libdpkg.map @@ -207,6 +207,7 @@ LIBDPKG_PRIVATE { setcloexec; # Memory information + meminfo_get_available_from_file; meminfo_get_available; # Compression support diff --git a/lib/dpkg/meminfo.c b/lib/dpkg/meminfo.c index fd6ccb4dd..d792db967 100644 --- a/lib/dpkg/meminfo.c +++ b/lib/dpkg/meminfo.c @@ -34,7 +34,6 @@ #include <dpkg/fdio.h> #include <dpkg/meminfo.h> -#ifdef __linux__ /* * An estimate of how much memory is available. Swap will not be used, the * page cache may be purged, not everything will be reclaimed that might be @@ -44,7 +43,7 @@ static const char str_MemAvailable[] = "MemAvailable"; static const size_t len_MemAvailable = sizeof(str_MemAvailable) - 1; int -meminfo_get_available(uint64_t *val) +meminfo_get_available_from_file(const char *filename, uint64_t *val) { char buf[4096]; char *str; @@ -53,7 +52,7 @@ meminfo_get_available(uint64_t *val) *val = 0; - fd = open("/proc/meminfo", O_RDONLY); + fd = open(filename, O_RDONLY); if (fd < 0) return -1; @@ -103,10 +102,13 @@ meminfo_get_available(uint64_t *val) } return -1; } -#else + int meminfo_get_available(uint64_t *val) { +#ifdef __linux__ + return meminfo_get_available_from_file("/proc/meminfo", val); +#else return -1; -} #endif +} diff --git a/lib/dpkg/meminfo.h b/lib/dpkg/meminfo.h index a80a47c2d..0f140b539 100644 --- a/lib/dpkg/meminfo.h +++ b/lib/dpkg/meminfo.h @@ -33,6 +33,8 @@ DPKG_BEGIN_DECLS * @{ */ +int +meminfo_get_available_from_file(const char *filename, uint64_t *val); int meminfo_get_available(uint64_t *val); -- Dpkg.Org's dpkg

