From e046e98358467bc0b8a3e6bdb7685311aea462e4 Mon Sep 17 00:00:00 2001 From: Robert Spalek Date: Sat, 15 Oct 2005 18:06:38 +0000 Subject: [PATCH] partmap_map() inlined and added an even faster version of it --- lib/lib.h | 26 +++++++++++++++++++++++++- lib/partmap.c | 46 +++++++++++++++++----------------------------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/lib/lib.h b/lib/lib.h index 47606169..81728f10 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -203,11 +203,35 @@ void munmap_file(void *start, unsigned len); /* partmap.c */ +struct partmap { + int fd; + sh_off_t file_size; + sh_off_t start_off, end_off; + byte *start_map; + int writeable; +}; + struct partmap; struct partmap *partmap_open(byte *name, int writeable); -void *partmap_map(struct partmap *p, sh_off_t offset, uns size); void partmap_close(struct partmap *p); sh_off_t partmap_size(struct partmap *p); +void partmap_load(struct partmap *p, sh_off_t start, uns size); + +static inline void * +partmap_map(struct partmap *p, sh_off_t start, uns size) +{ + if (unlikely(!p->start_map || start < p->start_off || (sh_off_t) (start+size) > p->end_off)) + partmap_load(p, start, size); + return p->start_map + (start - p->start_off); +} + +static inline void * +partmap_map_forward(struct partmap *p, sh_off_t start, uns size) +{ + if (unlikely((sh_off_t) (start+size) > p->end_off)) + partmap_load(p, start, size); + return p->start_map + (start - p->start_off); +} /* proctitle.c */ diff --git a/lib/partmap.c b/lib/partmap.c index 8b6cdc94..e0832fcd 100644 --- a/lib/partmap.c +++ b/lib/partmap.c @@ -2,7 +2,7 @@ * UCW Library -- Mapping of File Parts * * (c) 2003 Martin Mares - * (c) 2003 Robert Spalek + * (c) 2003--2005 Robert Spalek * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -19,14 +19,6 @@ #include #include -struct partmap { - int fd; - sh_off_t file_size; - sh_off_t start_off, end_off; - byte *start_map; - int writeable; -}; - #ifdef TEST #define PARTMAP_WINDOW 4096 #else @@ -62,27 +54,23 @@ partmap_close(struct partmap *p) xfree(p); } -void * -partmap_map(struct partmap *p, sh_off_t start, uns size) +void +partmap_load(struct partmap *p, sh_off_t start, uns size) { - if (unlikely(!p->start_map || start < p->start_off || (sh_off_t) (start+size) > p->end_off)) - { - if (p->start_map) - munmap(p->start_map, p->end_off - p->start_off); - sh_off_t end = start + size; - sh_off_t win_start = start/PAGE_SIZE * PAGE_SIZE; - uns win_len = PARTMAP_WINDOW; - if ((sh_off_t) (win_start+win_len) > p->file_size) - win_len = ALIGN(p->file_size - win_start, PAGE_SIZE); - if ((sh_off_t) (win_start+win_len) < end) - die("partmap_map: Window is too small for mapping %d bytes", size); - p->start_map = sh_mmap(NULL, win_len, p->writeable ? (PROT_READ | PROT_WRITE) : PROT_READ, MAP_SHARED, p->fd, win_start); - if (p->start_map == MAP_FAILED) - die("mmap failed at position %Ld: %m", (long long)win_start); - p->start_off = win_start; - p->end_off = win_start+win_len; - } - return p->start_map + (start - p->start_off); + if (p->start_map) + munmap(p->start_map, p->end_off - p->start_off); + sh_off_t end = start + size; + sh_off_t win_start = start/PAGE_SIZE * PAGE_SIZE; + uns win_len = PARTMAP_WINDOW; + if ((sh_off_t) (win_start+win_len) > p->file_size) + win_len = ALIGN(p->file_size - win_start, PAGE_SIZE); + if ((sh_off_t) (win_start+win_len) < end) + die("partmap_map: Window is too small for mapping %d bytes", size); + p->start_map = sh_mmap(NULL, win_len, p->writeable ? (PROT_READ | PROT_WRITE) : PROT_READ, MAP_SHARED, p->fd, win_start); + if (p->start_map == MAP_FAILED) + die("mmap failed at position %Ld: %m", (long long)win_start); + p->start_off = win_start; + p->end_off = win_start+win_len; } #ifdef TEST -- 2.39.5