Skip to content

Commit a742dbe

Browse files
committed
misc: add restrict qualifier to get more optimization opportunities
Signed-off-by: He Xian <hexian000@outlook.com>
1 parent 3a661db commit a742dbe

File tree

6 files changed

+57
-46
lines changed

6 files changed

+57
-46
lines changed

contrib/csnippets/utils/debug.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929

3030
#define INDENT " "
3131

32-
void slog_extra_txt(FILE *restrict f, void *data)
32+
void slog_extra_txt(FILE *restrict f, void *restrict data)
3333
{
34-
const struct slog_extra_txt *restrict extra = data;
34+
const struct slog_extra_txt *extra = data;
3535
size_t n = extra->len;
36-
const char *s = extra->data;
36+
const char *restrict s = extra->data;
3737
struct {
3838
BUFFER_HDR;
3939
unsigned char data[256];
@@ -118,9 +118,9 @@ void slog_extra_txt(FILE *restrict f, void *data)
118118
}
119119
}
120120

121-
void slog_extra_bin(FILE *restrict f, void *data)
121+
void slog_extra_bin(FILE *restrict f, void *restrict data)
122122
{
123-
const struct slog_extra_bin *restrict extra = data;
123+
const struct slog_extra_bin *extra = data;
124124
size_t n = extra->len;
125125
struct {
126126
BUFFER_HDR;

contrib/csnippets/utils/serialize.h

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,97 +11,97 @@
1111

1212
#include <stdint.h>
1313

14-
static inline uint_least8_t read_uint8(const void *p)
14+
static inline uint_least8_t read_uint8(const void *restrict p)
1515
{
16-
const unsigned char *restrict b = p;
16+
const unsigned char *b = p;
1717
return b[0];
1818
}
1919

20-
static inline void write_uint8(void *p, uint_least8_t v)
20+
static inline void write_uint8(void *restrict p, uint_least8_t v)
2121
{
22-
unsigned char *restrict b = p;
22+
unsigned char *b = p;
2323
b[0] = v;
2424
}
2525

26-
static inline uint_least16_t read_uint16(const void *p)
26+
static inline uint_least16_t read_uint16(const void *restrict p)
2727
{
28-
const unsigned char *restrict b = p;
28+
const unsigned char *b = p;
2929
return (((uint_fast16_t)b[0]) << 8U) | ((uint_fast16_t)b[1]);
3030
}
3131

32-
static inline uint_least16_t read_uint16_le(const void *p)
32+
static inline uint_least16_t read_uint16_le(const void *restrict p)
3333
{
34-
const unsigned char *restrict b = p;
34+
const unsigned char *b = p;
3535
return (((uint_fast16_t)b[0])) | ((uint_fast16_t)b[1] << 8U);
3636
}
3737

38-
static inline void write_uint16(void *p, uint_least16_t v)
38+
static inline void write_uint16(void *restrict p, uint_least16_t v)
3939
{
40-
unsigned char *restrict b = p;
40+
unsigned char *b = p;
4141
b[0] = (unsigned char)(v >> 8U);
4242
b[1] = (unsigned char)v;
4343
}
4444

45-
static inline void write_uint16_le(void *p, uint_least16_t v)
45+
static inline void write_uint16_le(void *restrict p, uint_least16_t v)
4646
{
47-
unsigned char *restrict b = p;
47+
unsigned char *b = p;
4848
b[0] = (unsigned char)v;
4949
b[1] = (unsigned char)(v >> 8U);
5050
}
5151

52-
static inline uint_least32_t read_uint32(const void *p)
52+
static inline uint_least32_t read_uint32(const void *restrict p)
5353
{
54-
const unsigned char *restrict b = p;
54+
const unsigned char *b = p;
5555
return (uint_fast32_t)(b[0]) << 24U | (uint_fast32_t)(b[1]) << 16U |
5656
(uint_fast32_t)(b[2]) << 8U | (uint_fast32_t)(b[3]);
5757
}
5858

59-
static inline uint_least32_t read_uint32_le(const void *p)
59+
static inline uint_least32_t read_uint32_le(const void *restrict p)
6060
{
61-
const unsigned char *restrict b = p;
61+
const unsigned char *b = p;
6262
return (uint_fast32_t)(b[0]) | (uint_fast32_t)(b[1]) << 8U |
6363
(uint_fast32_t)(b[2]) << 16U | (uint_fast32_t)(b[3]) << 24U;
6464
}
6565

66-
static inline void write_uint32(void *p, uint_least32_t v)
66+
static inline void write_uint32(void *restrict p, uint_least32_t v)
6767
{
68-
unsigned char *restrict b = p;
68+
unsigned char *b = p;
6969
b[0] = (unsigned char)(v >> 24U);
7070
b[1] = (unsigned char)(v >> 16U);
7171
b[2] = (unsigned char)(v >> 8U);
7272
b[3] = (unsigned char)v;
7373
}
7474

75-
static inline void write_uint32_le(void *p, uint_least32_t v)
75+
static inline void write_uint32_le(void *restrict p, uint_least32_t v)
7676
{
77-
unsigned char *restrict b = p;
77+
unsigned char *b = p;
7878
b[0] = (unsigned char)v;
7979
b[1] = (unsigned char)(v >> 8U);
8080
b[2] = (unsigned char)(v >> 16U);
8181
b[3] = (unsigned char)(v >> 24U);
8282
}
8383

84-
static inline uint_least64_t read_uint64(const void *p)
84+
static inline uint_least64_t read_uint64(const void *restrict p)
8585
{
86-
const unsigned char *restrict b = p;
86+
const unsigned char *b = p;
8787
return (uint_fast64_t)(b[0]) << 56U | (uint_fast64_t)(b[1]) << 48U |
8888
(uint_fast64_t)(b[2]) << 40U | (uint_fast64_t)(b[3]) << 32U |
8989
(uint_fast64_t)(b[4]) << 24U | (uint_fast64_t)(b[5]) << 16U |
9090
(uint_fast64_t)(b[6]) << 8U | (uint_fast64_t)(b[7]);
9191
}
9292

93-
static inline uint_least64_t read_uint64_le(const void *p)
93+
static inline uint_least64_t read_uint64_le(const void *restrict p)
9494
{
95-
const unsigned char *restrict b = p;
95+
const unsigned char *b = p;
9696
return (uint_fast64_t)(b[0]) | (uint_fast64_t)(b[1]) << 8U |
9797
(uint_fast64_t)(b[2]) << 16U | (uint_fast64_t)(b[3]) << 24U |
9898
(uint_fast64_t)(b[4]) << 32U | (uint_fast64_t)(b[5]) << 40U |
9999
(uint_fast64_t)(b[6]) << 48U | (uint_fast64_t)(b[7]) << 56U;
100100
}
101101

102-
static inline void write_uint64(void *p, uint_least64_t v)
102+
static inline void write_uint64(void *restrict p, uint_least64_t v)
103103
{
104-
unsigned char *restrict b = p;
104+
unsigned char *b = p;
105105
b[0] = (unsigned char)(v >> 56U);
106106
b[1] = (unsigned char)(v >> 48U);
107107
b[2] = (unsigned char)(v >> 40U);
@@ -112,9 +112,9 @@ static inline void write_uint64(void *p, uint_least64_t v)
112112
b[7] = (unsigned char)v;
113113
}
114114

115-
static inline void write_uint64_le(void *p, uint_least64_t v)
115+
static inline void write_uint64_le(void *restrict p, uint_least64_t v)
116116
{
117-
unsigned char *restrict b = p;
117+
unsigned char *b = p;
118118
b[0] = (unsigned char)v;
119119
b[1] = (unsigned char)(v >> 8U);
120120
b[2] = (unsigned char)(v >> 16U);

src/obfs.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,7 @@ void obfs_ctx_auth(struct obfs_ctx *restrict ctx, const bool ok)
15691569

15701570
static void obfs_accept_one(
15711571
struct obfs *restrict obfs, struct ev_loop *loop, const int fd,
1572-
struct sockaddr *sa, socklen_t len)
1572+
struct sockaddr *restrict sa, socklen_t len)
15731573
{
15741574
struct obfs_ctx *restrict ctx = obfs_ctx_new(obfs);
15751575
if (ctx == NULL) {
@@ -1614,7 +1614,8 @@ static bool is_startup_limited(struct obfs *restrict obfs)
16141614
return false;
16151615
}
16161616

1617-
void obfs_accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
1617+
void obfs_accept_cb(
1618+
struct ev_loop *loop, struct ev_io *restrict watcher, int revents)
16181619
{
16191620
CHECK_REVENTS(revents, EV_READ);
16201621

src/session.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ struct session {
104104

105105
#define SESSION_MAKEKEY(key, sa, conv) \
106106
do { \
107-
unsigned char *p = (key); \
107+
unsigned char *restrict p = (key); \
108108
size_t size = SESSION_KEY_SIZE; \
109109
const size_t n = getsocklen(sa); \
110110
write_uint32(p, conv); \

src/sockutil.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ static bool sa_matches_inet6(
177177
return true;
178178
}
179179

180-
bool sa_matches(const struct sockaddr *bind, const struct sockaddr *dest)
180+
bool sa_matches(
181+
const struct sockaddr *restrict bind,
182+
const struct sockaddr *restrict dest)
181183
{
182184
const int domain = bind->sa_family;
183185
if (domain != dest->sa_family) {
@@ -198,8 +200,9 @@ bool sa_matches(const struct sockaddr *bind, const struct sockaddr *dest)
198200
FAIL();
199201
}
200202

201-
static int
202-
format_sa_inet(char *s, const size_t maxlen, const struct sockaddr_in *sa)
203+
static int format_sa_inet(
204+
char *restrict s, const size_t maxlen,
205+
const struct sockaddr_in *restrict sa)
203206
{
204207
char buf[INET_ADDRSTRLEN];
205208
if (inet_ntop(AF_INET, &(sa->sin_addr), buf, sizeof(buf)) == NULL) {
@@ -209,8 +212,9 @@ format_sa_inet(char *s, const size_t maxlen, const struct sockaddr_in *sa)
209212
return snprintf(s, maxlen, "%s:%" PRIu16, buf, port);
210213
}
211214

212-
static int
213-
format_sa_inet6(char *s, const size_t maxlen, const struct sockaddr_in6 *sa)
215+
static int format_sa_inet6(
216+
char *restrict s, const size_t maxlen,
217+
const struct sockaddr_in6 *restrict sa)
214218
{
215219
char buf[INET6_ADDRSTRLEN];
216220
if (inet_ntop(AF_INET6, &(sa->sin6_addr), buf, sizeof(buf)) == NULL) {
@@ -225,7 +229,9 @@ format_sa_inet6(char *s, const size_t maxlen, const struct sockaddr_in6 *sa)
225229
s, maxlen, "[%s%%%" PRIu32 "]:%" PRIu16, buf, scope, port);
226230
}
227231

228-
int format_sa(char *s, const size_t maxlen, const struct sockaddr *sa)
232+
int format_sa(
233+
char *restrict s, const size_t maxlen,
234+
const struct sockaddr *restrict sa)
229235
{
230236
switch (sa->sa_family) {
231237
case AF_INET:
@@ -238,7 +244,8 @@ int format_sa(char *s, const size_t maxlen, const struct sockaddr *sa)
238244
return snprintf(s, maxlen, "<af:%jd>", (intmax_t)sa->sa_family);
239245
}
240246

241-
static bool find_addrinfo(union sockaddr_max *sa, const struct addrinfo *it)
247+
static bool find_addrinfo(
248+
union sockaddr_max *restrict sa, const struct addrinfo *restrict it)
242249
{
243250
for (; it != NULL; it = it->ai_next) {
244251
switch (it->ai_family) {
@@ -261,7 +268,9 @@ static bool find_addrinfo(union sockaddr_max *sa, const struct addrinfo *it)
261268
/* RFC 1035: Section 2.3.4 */
262269
#define FQDN_MAX_LENGTH ((size_t)(255))
263270

264-
bool resolve_addr(union sockaddr_max *sa, const char *s, const int flags)
271+
bool resolve_addr(
272+
union sockaddr_max *restrict sa, const char *restrict s,
273+
const int flags)
265274
{
266275
const size_t addrlen = strlen(s);
267276
ASSERT(addrlen <= FQDN_MAX_LENGTH + 1 + 5);

src/util.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void genpsk(const char *method)
182182
}
183183
#endif
184184

185-
bool parse_user(struct user_ident *ident, const char *s)
185+
bool parse_user(struct user_ident *restrict ident, const char *restrict s)
186186
{
187187
const size_t len = strlen(s);
188188
if (len >= 1024) {
@@ -294,7 +294,8 @@ void drop_privileges(const struct user_ident *restrict ident)
294294
}
295295

296296
void daemonize(
297-
const struct user_ident *ident, const bool nochdir, const bool noclose)
297+
const struct user_ident *restrict ident, const bool nochdir,
298+
const bool noclose)
298299
{
299300
/* Create an anonymous pipe for communicating with daemon process. */
300301
int fd[2];

0 commit comments

Comments
 (0)