From fd3c512f88d43e6633bd3c3110cfa0bb321adaa8 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 14 Dec 2020 18:25:28 +0100 Subject: libbb: create and use mmap() helpers function old new delta mmap_anon - 22 +22 mmap_read - 21 +21 xmmap_anon - 16 +16 rpm_gettags 465 447 -18 bb_full_fd_action 498 480 -18 uevent_main 337 310 -27 ------------------------------------------------------------------------------ (add/remove: 3/0 grow/shrink: 0/3 up/down: 59/-63) Total: -4 bytes Signed-off-by: Denys Vlasenko --- libbb/copyfd.c | 5 +---- libbb/xfuncs_printf.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) (limited to 'libbb') diff --git a/libbb/copyfd.c b/libbb/copyfd.c index d41fd10f0..7f9d92ea9 100644 --- a/libbb/copyfd.c +++ b/libbb/copyfd.c @@ -75,10 +75,7 @@ static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size) goto use_small_buf; /* We want page-aligned buffer, just in case kernel is clever * and can do page-aligned io more efficiently */ - buffer = mmap(NULL, CONFIG_FEATURE_COPYBUF_KB * 1024, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANON, - /* ignored: */ -1, 0); + buffer = mmap_anon(CONFIG_FEATURE_COPYBUF_KB * 1024); buffer_size = CONFIG_FEATURE_COPYBUF_KB * 1024; if (buffer == MAP_FAILED) { use_small_buf: diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c index fcc798662..db40e996b 100644 --- a/libbb/xfuncs_printf.c +++ b/libbb/xfuncs_printf.c @@ -111,6 +111,27 @@ void* FAST_FUNC xmemdup(const void *s, int n) return memcpy(xmalloc(n), s, n); } +void* FAST_FUNC mmap_read(int fd, size_t size) +{ + return mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); +} + +void* FAST_FUNC mmap_anon(size_t size) +{ + return mmap(NULL, size, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, + /* ignored: */ -1, 0); +} + +void* FAST_FUNC xmmap_anon(size_t size) +{ + void *p = mmap_anon(size); + if (p == MAP_FAILED) + bb_die_memory_exhausted(); + return p; +} + // Die if we can't open a file and return a FILE* to it. // Notice we haven't got xfread(), This is for use with fscanf() and friends. FILE* FAST_FUNC xfopen(const char *path, const char *mode) -- cgit v1.2.3