1 |
commit: b06656ffb20e9dc6da0af92388e0af94e2edac49 |
2 |
Author: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
3 |
AuthorDate: Wed Dec 28 22:48:44 2016 +0000 |
4 |
Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
5 |
CommitDate: Wed Dec 28 22:48:44 2016 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=b06656ff |
7 |
|
8 |
start some likely/unlikely helpers |
9 |
|
10 |
libq/xasprintf.c | 2 +- |
11 |
libq/xchdir.c | 2 +- |
12 |
libq/xgetcwd.c | 2 +- |
13 |
libq/xmalloc.c | 8 ++++---- |
14 |
libq/xmkdir.c | 2 +- |
15 |
libq/xreadlink.c | 2 +- |
16 |
libq/xregex.c | 4 ++-- |
17 |
libq/xstrdup.c | 2 +- |
18 |
libq/xsystem.c | 2 +- |
19 |
porting.h | 3 +++ |
20 |
10 files changed, 16 insertions(+), 13 deletions(-) |
21 |
|
22 |
diff --git a/libq/xasprintf.c b/libq/xasprintf.c |
23 |
index f2f1c58..160223e 100644 |
24 |
--- a/libq/xasprintf.c |
25 |
+++ b/libq/xasprintf.c |
26 |
@@ -25,7 +25,7 @@ |
27 |
#define xasprintf(strp, fmt, args...) \ |
28 |
({ \ |
29 |
int _ret = asprintf(strp, fmt , ## args); \ |
30 |
- if (_ret == -1) \ |
31 |
+ if (unlikely(_ret == -1)) \ |
32 |
err("Out of memory"); \ |
33 |
_ret; \ |
34 |
}) |
35 |
|
36 |
diff --git a/libq/xchdir.c b/libq/xchdir.c |
37 |
index b127e9c..242b04a 100644 |
38 |
--- a/libq/xchdir.c |
39 |
+++ b/libq/xchdir.c |
40 |
@@ -10,6 +10,6 @@ |
41 |
void xchdir(const char *path); |
42 |
void xchdir(const char *path) |
43 |
{ |
44 |
- if (chdir(path)) |
45 |
+ if (unlikely(chdir(path) != 0)) |
46 |
errp("chdir(%s) failed", path); |
47 |
} |
48 |
|
49 |
diff --git a/libq/xgetcwd.c b/libq/xgetcwd.c |
50 |
index 876e17c..31f531d 100644 |
51 |
--- a/libq/xgetcwd.c |
52 |
+++ b/libq/xgetcwd.c |
53 |
@@ -11,7 +11,7 @@ char *xgetcwd(char *buf, size_t size); |
54 |
char *xgetcwd(char *buf, size_t size) |
55 |
{ |
56 |
char *ret = getcwd(buf, size); |
57 |
- if (!ret) |
58 |
+ if (unlikely(ret == NULL)) |
59 |
errp("getcwd() failed"); |
60 |
return ret; |
61 |
} |
62 |
|
63 |
diff --git a/libq/xmalloc.c b/libq/xmalloc.c |
64 |
index 207a15a..9a9173d 100644 |
65 |
--- a/libq/xmalloc.c |
66 |
+++ b/libq/xmalloc.c |
67 |
@@ -30,7 +30,7 @@ |
68 |
static void *xmalloc(size_t size) |
69 |
{ |
70 |
void *ptr = malloc(size); |
71 |
- if (ptr == NULL) |
72 |
+ if (unlikely(ptr == NULL)) |
73 |
err("Out of memory"); |
74 |
return ptr; |
75 |
} |
76 |
@@ -38,7 +38,7 @@ static void *xmalloc(size_t size) |
77 |
static void *xcalloc(size_t nmemb, size_t size) |
78 |
{ |
79 |
void *ptr = calloc(nmemb, size); |
80 |
- if (ptr == NULL) |
81 |
+ if (unlikely(ptr == NULL)) |
82 |
err("Out of memory"); |
83 |
return ptr; |
84 |
} |
85 |
@@ -46,7 +46,7 @@ static void *xcalloc(size_t nmemb, size_t size) |
86 |
static void *xzalloc(size_t size) |
87 |
{ |
88 |
void *ptr = xmalloc(size); |
89 |
- if (ptr == NULL) |
90 |
+ if (unlikely(ptr == NULL)) |
91 |
err("Out of memory"); |
92 |
memset(ptr, 0x00, size); |
93 |
return ptr; |
94 |
@@ -55,7 +55,7 @@ static void *xzalloc(size_t size) |
95 |
static void *xrealloc(void *optr, size_t size) |
96 |
{ |
97 |
void *ptr = realloc(optr, size); |
98 |
- if (ptr == NULL) |
99 |
+ if (unlikely(ptr == NULL)) |
100 |
err("Out of memory"); |
101 |
return ptr; |
102 |
} |
103 |
|
104 |
diff --git a/libq/xmkdir.c b/libq/xmkdir.c |
105 |
index cc428aa..b47d44c 100644 |
106 |
--- a/libq/xmkdir.c |
107 |
+++ b/libq/xmkdir.c |
108 |
@@ -62,7 +62,7 @@ _q_static int rm_rf_at(int dfd, const char *path) |
109 |
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) |
110 |
continue; |
111 |
if (unlinkat(subdfd, de->d_name, 0) == -1) { |
112 |
- if (errno != EISDIR) |
113 |
+ if (unlikely(errno != EISDIR)) |
114 |
errp("could not unlink %s", de->d_name); |
115 |
rm_rf_at(subdfd, de->d_name); |
116 |
unlinkat(subdfd, de->d_name, AT_REMOVEDIR); |
117 |
|
118 |
diff --git a/libq/xreadlink.c b/libq/xreadlink.c |
119 |
index b580dbd..872264f 100644 |
120 |
--- a/libq/xreadlink.c |
121 |
+++ b/libq/xreadlink.c |
122 |
@@ -11,7 +11,7 @@ ssize_t xreadlink(const char *path, char *buf, size_t bufsiz); |
123 |
ssize_t xreadlink(const char *path, char *buf, size_t bufsiz) |
124 |
{ |
125 |
ssize_t ret = readlink(path, buf, bufsiz); |
126 |
- if (ret == -1) |
127 |
+ if (unlikely(ret == -1)) |
128 |
errp("readlink(%s) failed", path); |
129 |
return ret; |
130 |
} |
131 |
|
132 |
diff --git a/libq/xregex.c b/libq/xregex.c |
133 |
index 65f0d47..7df928a 100644 |
134 |
--- a/libq/xregex.c |
135 |
+++ b/libq/xregex.c |
136 |
@@ -1,7 +1,7 @@ |
137 |
static int wregcomp(regex_t *preg, const char *regex, int cflags) |
138 |
{ |
139 |
int ret = regcomp(preg, regex, cflags); |
140 |
- if (ret) { |
141 |
+ if (unlikely(ret)) { |
142 |
char errbuf[256]; |
143 |
regerror(ret, preg, errbuf, sizeof(errbuf)); |
144 |
warn("invalid regexp: %s -- %s\n", regex, errbuf); |
145 |
@@ -11,6 +11,6 @@ static int wregcomp(regex_t *preg, const char *regex, int cflags) |
146 |
|
147 |
static void xregcomp(regex_t *preg, const char *regex, int cflags) |
148 |
{ |
149 |
- if (wregcomp(preg, regex, cflags)) |
150 |
+ if (unlikely(wregcomp(preg, regex, cflags))) |
151 |
exit(EXIT_FAILURE); |
152 |
} |
153 |
|
154 |
diff --git a/libq/xstrdup.c b/libq/xstrdup.c |
155 |
index dd2c039..6924d21 100644 |
156 |
--- a/libq/xstrdup.c |
157 |
+++ b/libq/xstrdup.c |
158 |
@@ -35,7 +35,7 @@ static char *xstrdup(const char *s) |
159 |
return NULL; |
160 |
|
161 |
t = strdup(s); |
162 |
- if (t == NULL) |
163 |
+ if (unlikely(t == NULL)) |
164 |
err("Out of memory"); |
165 |
|
166 |
return t; |
167 |
|
168 |
diff --git a/libq/xsystem.c b/libq/xsystem.c |
169 |
index 71e8306..b4af651 100644 |
170 |
--- a/libq/xsystem.c |
171 |
+++ b/libq/xsystem.c |
172 |
@@ -10,7 +10,7 @@ |
173 |
|
174 |
static void xsystem(const char *command) |
175 |
{ |
176 |
- if (system(command)) |
177 |
+ if (unlikely(system(command))) |
178 |
errp("system(%s) failed", command); |
179 |
} |
180 |
|
181 |
|
182 |
diff --git a/porting.h b/porting.h |
183 |
index b166bc1..206c6e3 100644 |
184 |
--- a/porting.h |
185 |
+++ b/porting.h |
186 |
@@ -77,4 +77,7 @@ |
187 |
#define CONFIG_EPREFIX "/" |
188 |
#endif |
189 |
|
190 |
+#define likely(x) __builtin_expect((x), 1) |
191 |
+#define unlikely(x) __builtin_expect((x), 0) |
192 |
+ |
193 |
#endif |