aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2020-12-14 18:25:28 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2020-12-14 18:25:28 +0100
commitfd3c512f88d43e6633bd3c3110cfa0bb321adaa8 (patch)
tree97211484388a0db0a85957f2e3f3724cb1c9159f
parentdb793480cb8ec3e5f878d1ec18b6ed5010c85e85 (diff)
downloadbusybox-fd3c512f88d43e6633bd3c3110cfa0bb321adaa8.tar.gz
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 <vda.linux@googlemail.com>
-rw-r--r--archival/rpm.c2
-rw-r--r--include/libbb.h3
-rw-r--r--libbb/copyfd.c5
-rw-r--r--libbb/xfuncs_printf.c21
-rw-r--r--modutils/modutils.c2
-rw-r--r--util-linux/uevent.c7
6 files changed, 28 insertions, 12 deletions
diff --git a/archival/rpm.c b/archival/rpm.c
index 68afba914..a4d850b46 100644
--- a/archival/rpm.c
+++ b/archival/rpm.c
@@ -145,7 +145,7 @@ static int rpm_gettags(const char *filename)
/* remember size for munmap */
G.mapsize = storepos;
/* some NOMMU systems prefer MAP_PRIVATE over MAP_SHARED */
- G.map = mmap(0, storepos, PROT_READ, MAP_PRIVATE, fd, 0);
+ G.map = mmap_read(fd, storepos);
if (G.map == MAP_FAILED)
bb_perror_msg_and_die("mmap '%s'", filename);
diff --git a/include/libbb.h b/include/libbb.h
index 8f1ee7eec..a74b3119f 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -387,6 +387,9 @@ void* xrealloc_vector_helper(void *vector, unsigned sizeof_and_shift, int idx) F
char *xstrdup(const char *s) FAST_FUNC RETURNS_MALLOC;
char *xstrndup(const char *s, int n) FAST_FUNC RETURNS_MALLOC;
void *xmemdup(const void *s, int n) FAST_FUNC RETURNS_MALLOC;
+void *mmap_read(int fd, size_t size) FAST_FUNC;
+void *mmap_anon(size_t size) FAST_FUNC;
+void *xmmap_anon(size_t size) FAST_FUNC;
//TODO: supply a pointer to char[11] buffer (avoid statics)?
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)
diff --git a/modutils/modutils.c b/modutils/modutils.c
index 6f7cd9721..f7ad5e805 100644
--- a/modutils/modutils.c
+++ b/modutils/modutils.c
@@ -169,7 +169,7 @@ void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p)
/* st.st_size is off_t, we can't just pass it to mmap */
if (st.st_size <= *image_size_p) {
size_t image_size = st.st_size;
- image = mmap(NULL, image_size, PROT_READ, MAP_PRIVATE, fd, 0);
+ image = mmap_read(fd, image_size);
if (image == MAP_FAILED) {
image = NULL;
} else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) {
diff --git a/util-linux/uevent.c b/util-linux/uevent.c
index 045b35432..015f1ee78 100644
--- a/util-linux/uevent.c
+++ b/util-linux/uevent.c
@@ -74,12 +74,7 @@ int uevent_main(int argc UNUSED_PARAM, char **argv)
// for a new uevent notification to come in.
// We use a fresh mmap so that buffer is not allocated
// until kernel actually starts filling it.
- netbuf = mmap(NULL, USER_RCVBUF,
- PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANON,
- /* ignored: */ -1, 0);
- if (netbuf == MAP_FAILED)
- bb_simple_perror_msg_and_die("mmap");
+ netbuf = xmmap_anon(USER_RCVBUF);
// Here we block, possibly for a very long time
len = safe_read(fd, netbuf, USER_RCVBUF - 1);