Skip to content

Commit 9791b77

Browse files
Trond Norbyedustin
Trond Norbye
authored andcommitted
Issue: memcached#83: Refactor: use htonll or ntohll if the os provides them
1 parent f9bc2b1 commit 9791b77

File tree

4 files changed

+63
-27
lines changed

4 files changed

+63
-27
lines changed

configure.ac

+22
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,28 @@ fi
283283

284284
AC_C_ENDIAN
285285

286+
AC_DEFUN([AC_C_HTONLL],
287+
[
288+
AC_MSG_CHECKING([for htonll])
289+
have_htoll="no"
290+
AC_RUN_IFELSE([
291+
AC_LANG_PROGRAM([
292+
#include <sys/types.h>
293+
#include <netinet/in.h>
294+
#include <inttypes.h>
295+
], [
296+
return htonll(0);
297+
])
298+
], [
299+
have_htoll="yes"
300+
AC_DEFINE([HAVE_HTONLL], [1], [Have ntohll])
301+
])
302+
303+
AC_MSG_RESULT([$have_htoll])
304+
])
305+
306+
AC_C_HTONLL
307+
286308
dnl Check whether the user's system supports pthread
287309
AC_SEARCH_LIBS(pthread_create, pthread)
288310
if test "x$ac_cv_search_pthread_create" == "xno"; then

memcached.c

+8-27
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ static void write_and_free(conn *c, char *buf, int bytes);
9393
static int ensure_iov_space(conn *c);
9494
static int add_iov(conn *c, const void *buf, int len);
9595
static int add_msghdr(conn *c);
96-
static uint64_t mc_swap64(uint64_t in);
9796

9897
/* time handling */
9998
static void set_current_time(void); /* update the global variable holding
@@ -891,7 +890,7 @@ static void add_bin_header(conn *c, uint16_t err, uint8_t hdr_len, uint16_t key_
891890

892891
header->response.bodylen = htonl(body_len);
893892
header->response.opaque = c->opaque;
894-
header->response.cas = mc_swap64(c->cas);
893+
header->response.cas = htonll(c->cas);
895894

896895
if (settings.verbose > 1) {
897896
int ii;
@@ -976,24 +975,6 @@ static void write_bin_response(conn *c, void *d, int hlen, int keylen, int dlen)
976975
}
977976
}
978977

979-
/* Byte swap a 64-bit number */
980-
static uint64_t mc_swap64(uint64_t in) {
981-
#ifdef ENDIAN_LITTLE
982-
/* Little endian, flip the bytes around until someone makes a faster/better
983-
* way to do this. */
984-
int64_t rv = 0;
985-
int i = 0;
986-
for(i = 0; i<8; i++) {
987-
rv = (rv << 8) | (in & 0xff);
988-
in >>= 8;
989-
}
990-
return rv;
991-
#else
992-
/* big-endian machines don't need byte swapping */
993-
return in;
994-
#endif
995-
}
996-
997978
static void complete_incr_bin(conn *c) {
998979
item *it;
999980
char *key;
@@ -1006,8 +987,8 @@ static void complete_incr_bin(conn *c) {
1006987
assert(c->wsize >= sizeof(*rsp));
1007988

1008989
/* fix byteorder in the request */
1009-
req->message.body.delta = mc_swap64(req->message.body.delta);
1010-
req->message.body.initial = mc_swap64(req->message.body.initial);
990+
req->message.body.delta = ntohll(req->message.body.delta);
991+
req->message.body.initial = ntohll(req->message.body.initial);
1011992
req->message.body.expiration = ntohl(req->message.body.expiration);
1012993
key = binary_get_key(c);
1013994
nkey = c->binary_header.request.keylen;
@@ -1047,7 +1028,7 @@ static void complete_incr_bin(conn *c) {
10471028
if (st != PROTOCOL_BINARY_RESPONSE_SUCCESS) {
10481029
write_bin_error(c, st, 0);
10491030
} else {
1050-
rsp->message.body.value = mc_swap64(strtoull(tmpbuf, NULL, 10));
1031+
rsp->message.body.value = htonll(strtoull(tmpbuf, NULL, 10));
10511032
c->cas = ITEM_get_cas(it);
10521033
write_bin_response(c, &rsp->message.body, 0, 0,
10531034
sizeof(rsp->message.body.value));
@@ -1056,7 +1037,7 @@ static void complete_incr_bin(conn *c) {
10561037
item_remove(it); /* release our reference */
10571038
} else if (!it && req->message.body.expiration != 0xffffffff) {
10581039
/* Save some room for the response */
1059-
rsp->message.body.value = mc_swap64(req->message.body.initial);
1040+
rsp->message.body.value = htonll(req->message.body.initial);
10601041
it = item_alloc(key, nkey, 0, realtime(req->message.body.expiration),
10611042
INCR_MAX_STORAGE_LEN);
10621043

@@ -1197,7 +1178,7 @@ static void process_bin_get(conn *c) {
11971178
keylen = nkey;
11981179
}
11991180
add_bin_header(c, 0, sizeof(rsp->message.body), keylen, bodylen);
1200-
rsp->message.header.response.cas = mc_swap64(ITEM_get_cas(it));
1181+
rsp->message.header.response.cas = htonll(ITEM_get_cas(it));
12011182

12021183
// add the flags
12031184
rsp->message.body.flags = htonl(strtoul(ITEM_suffix(it), NULL, 10));
@@ -1795,7 +1776,7 @@ static void process_bin_delete(conn *c) {
17951776

17961777
it = item_get(key, nkey);
17971778
if (it) {
1798-
uint64_t cas=mc_swap64(req->message.header.request.cas);
1779+
uint64_t cas = ntohll(req->message.header.request.cas);
17991780
if (cas == 0 || cas == ITEM_get_cas(it)) {
18001781
MEMCACHED_COMMAND_DELETE(c->sfd, ITEM_key(it), it->nkey);
18011782
item_unlink(it);
@@ -2922,7 +2903,7 @@ static int try_read_command(conn *c) {
29222903
c->binary_header = *req;
29232904
c->binary_header.request.keylen = ntohs(req->request.keylen);
29242905
c->binary_header.request.bodylen = ntohl(req->request.bodylen);
2925-
c->binary_header.request.cas = mc_swap64(req->request.cas);
2906+
c->binary_header.request.cas = ntohll(req->request.cas);
29262907

29272908
if (c->binary_header.request.magic != PROTOCOL_BINARY_REQ) {
29282909
if (settings.verbose) {

util.c

+28
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,31 @@ void vperror(const char *fmt, ...) {
103103

104104
perror(buf);
105105
}
106+
107+
#ifndef HAVE_HTONLL
108+
static uint64_t mc_swap64(uint64_t in) {
109+
#ifdef ENDIAN_LITTLE
110+
/* Little endian, flip the bytes around until someone makes a faster/better
111+
* way to do this. */
112+
int64_t rv = 0;
113+
int i = 0;
114+
for(i = 0; i<8; i++) {
115+
rv = (rv << 8) | (in & 0xff);
116+
in >>= 8;
117+
}
118+
return rv;
119+
#else
120+
/* big-endian machines don't need byte swapping */
121+
return in;
122+
#endif
123+
}
124+
125+
uint64_t ntohll(uint64_t val) {
126+
return mc_swap64(val);
127+
}
128+
129+
uint64_t htonll(uint64_t val) {
130+
return mc_swap64(val);
131+
}
132+
#endif
133+

util.h

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ bool safe_strtoll(const char *str, int64_t *out);
1212
bool safe_strtoul(const char *str, uint32_t *out);
1313
bool safe_strtol(const char *str, int32_t *out);
1414

15+
#ifndef HAVE_HTONLL
16+
extern uint64_t htonll(uint64_t);
17+
extern uint64_t ntohll(uint64_t);
18+
#endif
19+
1520
#ifdef __GCC
1621
# define __gcc_attribute__ __attribute__
1722
#else

0 commit comments

Comments
 (0)