From: "Mike Frysinger" <vapier@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/portage-utils:master commit in: /, libq/
Date: Thu, 29 Dec 2016 02:25:51 +0000 (UTC) [thread overview]
Message-ID: <1482965324.b06656ffb20e9dc6da0af92388e0af94e2edac49.vapier@gentoo> (raw)
commit: b06656ffb20e9dc6da0af92388e0af94e2edac49
Author: Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Wed Dec 28 22:48:44 2016 +0000
Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Wed Dec 28 22:48:44 2016 +0000
URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=b06656ff
start some likely/unlikely helpers
libq/xasprintf.c | 2 +-
libq/xchdir.c | 2 +-
libq/xgetcwd.c | 2 +-
libq/xmalloc.c | 8 ++++----
libq/xmkdir.c | 2 +-
libq/xreadlink.c | 2 +-
libq/xregex.c | 4 ++--
libq/xstrdup.c | 2 +-
libq/xsystem.c | 2 +-
porting.h | 3 +++
10 files changed, 16 insertions(+), 13 deletions(-)
diff --git a/libq/xasprintf.c b/libq/xasprintf.c
index f2f1c58..160223e 100644
--- a/libq/xasprintf.c
+++ b/libq/xasprintf.c
@@ -25,7 +25,7 @@
#define xasprintf(strp, fmt, args...) \
({ \
int _ret = asprintf(strp, fmt , ## args); \
- if (_ret == -1) \
+ if (unlikely(_ret == -1)) \
err("Out of memory"); \
_ret; \
})
diff --git a/libq/xchdir.c b/libq/xchdir.c
index b127e9c..242b04a 100644
--- a/libq/xchdir.c
+++ b/libq/xchdir.c
@@ -10,6 +10,6 @@
void xchdir(const char *path);
void xchdir(const char *path)
{
- if (chdir(path))
+ if (unlikely(chdir(path) != 0))
errp("chdir(%s) failed", path);
}
diff --git a/libq/xgetcwd.c b/libq/xgetcwd.c
index 876e17c..31f531d 100644
--- a/libq/xgetcwd.c
+++ b/libq/xgetcwd.c
@@ -11,7 +11,7 @@ char *xgetcwd(char *buf, size_t size);
char *xgetcwd(char *buf, size_t size)
{
char *ret = getcwd(buf, size);
- if (!ret)
+ if (unlikely(ret == NULL))
errp("getcwd() failed");
return ret;
}
diff --git a/libq/xmalloc.c b/libq/xmalloc.c
index 207a15a..9a9173d 100644
--- a/libq/xmalloc.c
+++ b/libq/xmalloc.c
@@ -30,7 +30,7 @@
static void *xmalloc(size_t size)
{
void *ptr = malloc(size);
- if (ptr == NULL)
+ if (unlikely(ptr == NULL))
err("Out of memory");
return ptr;
}
@@ -38,7 +38,7 @@ static void *xmalloc(size_t size)
static void *xcalloc(size_t nmemb, size_t size)
{
void *ptr = calloc(nmemb, size);
- if (ptr == NULL)
+ if (unlikely(ptr == NULL))
err("Out of memory");
return ptr;
}
@@ -46,7 +46,7 @@ static void *xcalloc(size_t nmemb, size_t size)
static void *xzalloc(size_t size)
{
void *ptr = xmalloc(size);
- if (ptr == NULL)
+ if (unlikely(ptr == NULL))
err("Out of memory");
memset(ptr, 0x00, size);
return ptr;
@@ -55,7 +55,7 @@ static void *xzalloc(size_t size)
static void *xrealloc(void *optr, size_t size)
{
void *ptr = realloc(optr, size);
- if (ptr == NULL)
+ if (unlikely(ptr == NULL))
err("Out of memory");
return ptr;
}
diff --git a/libq/xmkdir.c b/libq/xmkdir.c
index cc428aa..b47d44c 100644
--- a/libq/xmkdir.c
+++ b/libq/xmkdir.c
@@ -62,7 +62,7 @@ _q_static int rm_rf_at(int dfd, const char *path)
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."))
continue;
if (unlinkat(subdfd, de->d_name, 0) == -1) {
- if (errno != EISDIR)
+ if (unlikely(errno != EISDIR))
errp("could not unlink %s", de->d_name);
rm_rf_at(subdfd, de->d_name);
unlinkat(subdfd, de->d_name, AT_REMOVEDIR);
diff --git a/libq/xreadlink.c b/libq/xreadlink.c
index b580dbd..872264f 100644
--- a/libq/xreadlink.c
+++ b/libq/xreadlink.c
@@ -11,7 +11,7 @@ ssize_t xreadlink(const char *path, char *buf, size_t bufsiz);
ssize_t xreadlink(const char *path, char *buf, size_t bufsiz)
{
ssize_t ret = readlink(path, buf, bufsiz);
- if (ret == -1)
+ if (unlikely(ret == -1))
errp("readlink(%s) failed", path);
return ret;
}
diff --git a/libq/xregex.c b/libq/xregex.c
index 65f0d47..7df928a 100644
--- a/libq/xregex.c
+++ b/libq/xregex.c
@@ -1,7 +1,7 @@
static int wregcomp(regex_t *preg, const char *regex, int cflags)
{
int ret = regcomp(preg, regex, cflags);
- if (ret) {
+ if (unlikely(ret)) {
char errbuf[256];
regerror(ret, preg, errbuf, sizeof(errbuf));
warn("invalid regexp: %s -- %s\n", regex, errbuf);
@@ -11,6 +11,6 @@ static int wregcomp(regex_t *preg, const char *regex, int cflags)
static void xregcomp(regex_t *preg, const char *regex, int cflags)
{
- if (wregcomp(preg, regex, cflags))
+ if (unlikely(wregcomp(preg, regex, cflags)))
exit(EXIT_FAILURE);
}
diff --git a/libq/xstrdup.c b/libq/xstrdup.c
index dd2c039..6924d21 100644
--- a/libq/xstrdup.c
+++ b/libq/xstrdup.c
@@ -35,7 +35,7 @@ static char *xstrdup(const char *s)
return NULL;
t = strdup(s);
- if (t == NULL)
+ if (unlikely(t == NULL))
err("Out of memory");
return t;
diff --git a/libq/xsystem.c b/libq/xsystem.c
index 71e8306..b4af651 100644
--- a/libq/xsystem.c
+++ b/libq/xsystem.c
@@ -10,7 +10,7 @@
static void xsystem(const char *command)
{
- if (system(command))
+ if (unlikely(system(command)))
errp("system(%s) failed", command);
}
diff --git a/porting.h b/porting.h
index b166bc1..206c6e3 100644
--- a/porting.h
+++ b/porting.h
@@ -77,4 +77,7 @@
#define CONFIG_EPREFIX "/"
#endif
+#define likely(x) __builtin_expect((x), 1)
+#define unlikely(x) __builtin_expect((x), 0)
+
#endif
next reply other threads:[~2016-12-29 2:25 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-12-29 2:25 Mike Frysinger [this message]
-- strict thread matches above, loose matches on Subject: below --
2025-08-25 10:49 [gentoo-commits] proj/portage-utils:master commit in: /, libq/ Fabian Groffen
2025-08-19 14:11 Fabian Groffen
2025-04-20 8:43 Fabian Groffen
2024-01-02 7:57 Fabian Groffen
2020-05-16 18:27 Fabian Groffen
2020-01-05 16:08 Fabian Groffen
2020-01-04 19:48 Fabian Groffen
2020-01-01 17:54 Fabian Groffen
2019-11-15 13:52 Fabian Groffen
2019-07-14 13:09 Fabian Groffen
2019-05-07 6:19 Fabian Groffen
2019-05-06 17:33 Fabian Groffen
2019-05-02 15:48 Fabian Groffen
2018-05-18 16:58 Fabian Groffen
2016-12-29 2:25 Mike Frysinger
2016-02-14 1:26 Mike Frysinger
2016-02-14 1:26 Mike Frysinger
2015-11-28 2:44 Mike Frysinger
2015-11-28 2:44 Mike Frysinger
2015-02-24 1:26 Mike Frysinger
2014-03-21 5:32 Mike Frysinger
2014-03-10 8:45 Mike Frysinger
2014-03-10 6:00 Mike Frysinger
2014-03-08 5:51 Mike Frysinger
2014-03-08 5:51 Mike Frysinger
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=1482965324.b06656ffb20e9dc6da0af92388e0af94e2edac49.vapier@gentoo \
--to=vapier@gentoo.org \
--cc=gentoo-commits@lists.gentoo.org \
--cc=gentoo-dev@lists.gentoo.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