From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 2/3] fallocate: avoid unnecessary computation
Date: Wed, 16 Jul 2014 21:54:57 +0100 [thread overview]
Message-ID: <1405544098-1740-2-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1405544098-1740-1-git-send-email-kerolasa@iki.fi>
Where POSIX_FADV_SEQUENTIAL and HAVE_POSIX_FADVISE are not available it
is waste of resources to have variables that are meaningful only for
posix_fadvise(). Also initialize the variables immediately to correct
values, and since cachesz is never changed mark it read only.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
sys-utils/fallocate.c | 21 ++++++++++-----------
1 file changed, 10 insertions(+), 11 deletions(-)
diff --git a/sys-utils/fallocate.c b/sys-utils/fallocate.c
index 93fd3b4..c619f64 100644
--- a/sys-utils/fallocate.c
+++ b/sys-utils/fallocate.c
@@ -174,17 +174,12 @@ static void dig_holes(int fd, off_t off, off_t len)
{
off_t end = len ? off + len : 0;
off_t hole_start = 0, hole_sz = 0;
- off_t cache_start = 0;
uintmax_t ct = 0;
- size_t bufsz, cachesz;
+ size_t bufsz;
char *buf;
struct stat st;
-
- if (fstat(fd, &st) != 0)
- err(EXIT_FAILURE, _("stat failed %s"), filename);
-
- bufsz = st.st_blksize;
-
+#if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
+ off_t cache_start = off;
/*
* We don't want to call POSIX_FADV_DONTNEED to discard cached
* data in PAGE_SIZE steps. IMHO it's overkill (too many syscalls).
@@ -193,15 +188,19 @@ static void dig_holes(int fd, off_t off, off_t len)
* a good compromise.
* -- kzak Feb-2014
*/
- cachesz = getpagesize() * 256;
+ const size_t cachesz = getpagesize() * 256;
+#endif
+
+ if (fstat(fd, &st) != 0)
+ err(EXIT_FAILURE, _("stat failed %s"), filename);
+
+ bufsz = st.st_blksize;
if (lseek(fd, off, SEEK_SET) < 0)
err(EXIT_FAILURE, _("seek on %s failed"), filename);
/* buffer + extra space for is_nul() sentinel */
buf = xmalloc(bufsz + sizeof(uintptr_t));
- cache_start = off;
-
#if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
posix_fadvise(fd, off, 0, POSIX_FADV_SEQUENTIAL);
#endif
--
2.0.1
next prev parent reply other threads:[~2014-07-16 20:55 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-16 20:54 [PATCH 1/3] lib/colors: remove memory leak Sami Kerola
2014-07-16 20:54 ` Sami Kerola [this message]
2014-07-16 20:54 ` [PATCH 3/3] fstrim: avoid TOCTOU race Sami Kerola
2014-07-17 12:02 ` [PATCH 1/3] lib/colors: remove memory leak Karel Zak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1405544098-1740-2-git-send-email-kerolasa@iki.fi \
--to=kerolasa@iki.fi \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox