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>
This commit is contained in:
parent
db793480cb
commit
fd3c512f88
@ -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);
|
||||
|
||||
|
@ -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)?
|
||||
|
@ -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:
|
||||
|
@ -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)
|
||||
|
@ -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)) {
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user