diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2020-12-14 18:25:28 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2020-12-14 18:25:28 +0100 |
commit | fd3c512f88d43e6633bd3c3110cfa0bb321adaa8 (patch) | |
tree | 97211484388a0db0a85957f2e3f3724cb1c9159f | |
parent | db793480cb8ec3e5f878d1ec18b6ed5010c85e85 (diff) | |
download | busybox-w32-fd3c512f88d43e6633bd3c3110cfa0bb321adaa8.tar.gz busybox-w32-fd3c512f88d43e6633bd3c3110cfa0bb321adaa8.tar.bz2 busybox-w32-fd3c512f88d43e6633bd3c3110cfa0bb321adaa8.zip |
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.c | 2 | ||||
-rw-r--r-- | include/libbb.h | 3 | ||||
-rw-r--r-- | libbb/copyfd.c | 5 | ||||
-rw-r--r-- | libbb/xfuncs_printf.c | 21 | ||||
-rw-r--r-- | modutils/modutils.c | 2 | ||||
-rw-r--r-- | util-linux/uevent.c | 7 |
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) | |||
145 | /* remember size for munmap */ | 145 | /* remember size for munmap */ |
146 | G.mapsize = storepos; | 146 | G.mapsize = storepos; |
147 | /* some NOMMU systems prefer MAP_PRIVATE over MAP_SHARED */ | 147 | /* some NOMMU systems prefer MAP_PRIVATE over MAP_SHARED */ |
148 | G.map = mmap(0, storepos, PROT_READ, MAP_PRIVATE, fd, 0); | 148 | G.map = mmap_read(fd, storepos); |
149 | if (G.map == MAP_FAILED) | 149 | if (G.map == MAP_FAILED) |
150 | bb_perror_msg_and_die("mmap '%s'", filename); | 150 | bb_perror_msg_and_die("mmap '%s'", filename); |
151 | 151 | ||
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 | |||
387 | char *xstrdup(const char *s) FAST_FUNC RETURNS_MALLOC; | 387 | char *xstrdup(const char *s) FAST_FUNC RETURNS_MALLOC; |
388 | char *xstrndup(const char *s, int n) FAST_FUNC RETURNS_MALLOC; | 388 | char *xstrndup(const char *s, int n) FAST_FUNC RETURNS_MALLOC; |
389 | void *xmemdup(const void *s, int n) FAST_FUNC RETURNS_MALLOC; | 389 | void *xmemdup(const void *s, int n) FAST_FUNC RETURNS_MALLOC; |
390 | void *mmap_read(int fd, size_t size) FAST_FUNC; | ||
391 | void *mmap_anon(size_t size) FAST_FUNC; | ||
392 | void *xmmap_anon(size_t size) FAST_FUNC; | ||
390 | 393 | ||
391 | 394 | ||
392 | //TODO: supply a pointer to char[11] buffer (avoid statics)? | 395 | //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) | |||
75 | goto use_small_buf; | 75 | goto use_small_buf; |
76 | /* We want page-aligned buffer, just in case kernel is clever | 76 | /* We want page-aligned buffer, just in case kernel is clever |
77 | * and can do page-aligned io more efficiently */ | 77 | * and can do page-aligned io more efficiently */ |
78 | buffer = mmap(NULL, CONFIG_FEATURE_COPYBUF_KB * 1024, | 78 | buffer = mmap_anon(CONFIG_FEATURE_COPYBUF_KB * 1024); |
79 | PROT_READ | PROT_WRITE, | ||
80 | MAP_PRIVATE | MAP_ANON, | ||
81 | /* ignored: */ -1, 0); | ||
82 | buffer_size = CONFIG_FEATURE_COPYBUF_KB * 1024; | 79 | buffer_size = CONFIG_FEATURE_COPYBUF_KB * 1024; |
83 | if (buffer == MAP_FAILED) { | 80 | if (buffer == MAP_FAILED) { |
84 | use_small_buf: | 81 | 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) | |||
111 | return memcpy(xmalloc(n), s, n); | 111 | return memcpy(xmalloc(n), s, n); |
112 | } | 112 | } |
113 | 113 | ||
114 | void* FAST_FUNC mmap_read(int fd, size_t size) | ||
115 | { | ||
116 | return mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); | ||
117 | } | ||
118 | |||
119 | void* FAST_FUNC mmap_anon(size_t size) | ||
120 | { | ||
121 | return mmap(NULL, size, | ||
122 | PROT_READ | PROT_WRITE, | ||
123 | MAP_PRIVATE | MAP_ANONYMOUS, | ||
124 | /* ignored: */ -1, 0); | ||
125 | } | ||
126 | |||
127 | void* FAST_FUNC xmmap_anon(size_t size) | ||
128 | { | ||
129 | void *p = mmap_anon(size); | ||
130 | if (p == MAP_FAILED) | ||
131 | bb_die_memory_exhausted(); | ||
132 | return p; | ||
133 | } | ||
134 | |||
114 | // Die if we can't open a file and return a FILE* to it. | 135 | // Die if we can't open a file and return a FILE* to it. |
115 | // Notice we haven't got xfread(), This is for use with fscanf() and friends. | 136 | // Notice we haven't got xfread(), This is for use with fscanf() and friends. |
116 | FILE* FAST_FUNC xfopen(const char *path, const char *mode) | 137 | 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) | |||
169 | /* st.st_size is off_t, we can't just pass it to mmap */ | 169 | /* st.st_size is off_t, we can't just pass it to mmap */ |
170 | if (st.st_size <= *image_size_p) { | 170 | if (st.st_size <= *image_size_p) { |
171 | size_t image_size = st.st_size; | 171 | size_t image_size = st.st_size; |
172 | image = mmap(NULL, image_size, PROT_READ, MAP_PRIVATE, fd, 0); | 172 | image = mmap_read(fd, image_size); |
173 | if (image == MAP_FAILED) { | 173 | if (image == MAP_FAILED) { |
174 | image = NULL; | 174 | image = NULL; |
175 | } else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) { | 175 | } 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) | |||
74 | // for a new uevent notification to come in. | 74 | // for a new uevent notification to come in. |
75 | // We use a fresh mmap so that buffer is not allocated | 75 | // We use a fresh mmap so that buffer is not allocated |
76 | // until kernel actually starts filling it. | 76 | // until kernel actually starts filling it. |
77 | netbuf = mmap(NULL, USER_RCVBUF, | 77 | netbuf = xmmap_anon(USER_RCVBUF); |
78 | PROT_READ | PROT_WRITE, | ||
79 | MAP_PRIVATE | MAP_ANON, | ||
80 | /* ignored: */ -1, 0); | ||
81 | if (netbuf == MAP_FAILED) | ||
82 | bb_simple_perror_msg_and_die("mmap"); | ||
83 | 78 | ||
84 | // Here we block, possibly for a very long time | 79 | // Here we block, possibly for a very long time |
85 | len = safe_read(fd, netbuf, USER_RCVBUF - 1); | 80 | len = safe_read(fd, netbuf, USER_RCVBUF - 1); |