git » nmdb » commit e97b3e9

Move htonll() and ntohll() to a common, shared file

author Alberto Bertogli
2008-06-22 23:54:28 UTC
committer Alberto Bertogli
2008-06-22 23:57:39 UTC
parent 6fa4ef45e1f4917ca7d01aa1bba2c7fec83267a8

Move htonll() and ntohll() to a common, shared file

As we expect to use these functions in different places in the future, it
makes no sense anymore to have this code duplicated inside their users. So
move them to a common file, and use symlinks because it's not worth the
trouble of having a common directory for just to files (yet).

Signed-off-by: Alberto Bertogli <albertito@gmail.com>

libnmdb/Makefile +1 -1
libnmdb/libnmdb.c +1 -28
libnmdb/netutils.c +1 -0
libnmdb/netutils.h +1 -0
nmdb/Makefile +1 -1
nmdb/netutils.c +53 -0
nmdb/netutils.h +11 -0
nmdb/parse.c +1 -51

diff --git a/libnmdb/Makefile b/libnmdb/Makefile
index 4e197f2..831cc3f 100644
--- a/libnmdb/Makefile
+++ b/libnmdb/Makefile
@@ -24,7 +24,7 @@ endif
 PREFIX=/usr/local
 
 
-OBJS = libnmdb.o tcp.o tipc.o udp.o sctp.o
+OBJS = libnmdb.o netutils.o tcp.o tipc.o udp.o sctp.o
 
 
 ifneq ($(V), 1)
diff --git a/libnmdb/libnmdb.c b/libnmdb/libnmdb.c
index 4306e16..fd52306 100644
--- a/libnmdb/libnmdb.c
+++ b/libnmdb/libnmdb.c
@@ -14,6 +14,7 @@
 #include "udp.h"
 #include "sctp.h"
 #include "internal.h"
+#include "netutils.h"
 
 
 /* Compare two servers by their connection identifiers. It is used internally
@@ -571,34 +572,6 @@ int nmdb_cache_cas(nmdb_t *db, const unsigned char *key, size_t ksize,
 }
 
 
-/* htonll() is not standard, so we define it using an UGLY trick because there
- * is no standard way to check for endianness at runtime! (this is the same as
- * the one in nmdb/parse.c, the infraestructure to keep these common is not
- * worth it)*/
-static uint64_t htonll(uint64_t x)
-{
-	static int endianness = 0;
-
-	/* determine the endianness by checking how htonl() behaves; use -1
-	 * for little endian and 1 for big endian */
-	if (endianness == 0) {
-		if (htonl(1) == 1)
-			endianness = 1;
-		else
-			endianness = -1;
-	}
-
-	if (endianness == 1) {
-		/* big endian */
-		return x;
-	}
-
-	/* little endian */
-	return ( htonl( (x >> 32) & 0xFFFFFFFF ) | \
-			( (uint64_t) htonl(x & 0xFFFFFFFF) ) << 32 );
-}
-
-
 static int do_incr(nmdb_t *db, const unsigned char *key, size_t ksize,
 		int64_t increment, unsigned short flags)
 {
diff --git a/libnmdb/netutils.c b/libnmdb/netutils.c
new file mode 120000
index 0000000..73cb507
--- /dev/null
+++ b/libnmdb/netutils.c
@@ -0,0 +1 @@
+../nmdb/netutils.c
\ No newline at end of file
diff --git a/libnmdb/netutils.h b/libnmdb/netutils.h
new file mode 120000
index 0000000..24df4a2
--- /dev/null
+++ b/libnmdb/netutils.h
@@ -0,0 +1 @@
+../nmdb/netutils.h
\ No newline at end of file
diff --git a/nmdb/Makefile b/nmdb/Makefile
index f20ecfe..f9d3265 100644
--- a/nmdb/Makefile
+++ b/nmdb/Makefile
@@ -28,7 +28,7 @@ endif
 PREFIX=/usr/local
 
 
-OBJS = cache.o dbloop.o queue.o log.o net.o parse.o stats.o main.o
+OBJS = cache.o dbloop.o queue.o log.o net.o netutils.o parse.o stats.o main.o
 LIBS = -levent -lpthread -lrt
 
 
diff --git a/nmdb/netutils.c b/nmdb/netutils.c
new file mode 100644
index 0000000..663b3c0
--- /dev/null
+++ b/nmdb/netutils.c
@@ -0,0 +1,53 @@
+
+#include <arpa/inet.h>		/* htonl() and friends */
+#include "netutils.h"
+
+
+/* ntohll() and htonll() are not standard, so we define it using an UGLY trick
+ * because there is no standard way to check for endianness at runtime! */
+uint64_t ntohll(uint64_t x)
+{
+	static int endianness = 0;
+
+	/* determine the endianness by checking how htonl() behaves; use -1
+	 * for little endian and 1 for big endian */
+	if (endianness == 0) {
+		if (htonl(1) == 1)
+			endianness = 1;
+		else
+			endianness = -1;
+	}
+
+	if (endianness == 1) {
+		/* big endian */
+		return x;
+	}
+
+	/* little endian */
+	return ( ntohl( (x >> 32) & 0xFFFFFFFF ) | \
+			( (uint64_t) ntohl(x & 0xFFFFFFFF) ) << 32 );
+}
+
+uint64_t htonll(uint64_t x)
+{
+	static int endianness = 0;
+
+	/* determine the endianness by checking how htonl() behaves; use -1
+	 * for little endian and 1 for big endian */
+	if (endianness == 0) {
+		if (htonl(1) == 1)
+			endianness = 1;
+		else
+			endianness = -1;
+	}
+
+	if (endianness == 1) {
+		/* big endian */
+		return x;
+	}
+
+	/* little endian */
+	return ( htonl( (x >> 32) & 0xFFFFFFFF ) | \
+			( (uint64_t) htonl(x & 0xFFFFFFFF) ) << 32 );
+}
+
diff --git a/nmdb/netutils.h b/nmdb/netutils.h
new file mode 100644
index 0000000..301617c
--- /dev/null
+++ b/nmdb/netutils.h
@@ -0,0 +1,11 @@
+
+#ifndef _NETUTILS_H
+#define _NETUTILS_H
+
+#include <stdint.h>
+
+uint64_t ntohll(uint64_t x);
+uint64_t htonll(uint64_t x);
+
+#endif
+
diff --git a/nmdb/parse.c b/nmdb/parse.c
index e7c9b72..57a2ba8 100644
--- a/nmdb/parse.c
+++ b/nmdb/parse.c
@@ -4,12 +4,12 @@
 #include <string.h>		/* memcpy() */
 #include <arpa/inet.h>		/* htonl() and friends */
 
-
 #include "parse.h"
 #include "req.h"
 #include "queue.h"
 #include "net-const.h"
 #include "common.h"
+#include "netutils.h"
 
 
 static void parse_get(struct req_info *req);
@@ -450,56 +450,6 @@ static void parse_cas(struct req_info *req)
 	return;
 }
 
-
-/* ntohll() and htonll() are not standard, so we define it using an UGLY trick
- * because there is no standard way to check for endianness at runtime! */
-static uint64_t ntohll(uint64_t x)
-{
-	static int endianness = 0;
-
-	/* determine the endianness by checking how htonl() behaves; use -1
-	 * for little endian and 1 for big endian */
-	if (endianness == 0) {
-		if (htonl(1) == 1)
-			endianness = 1;
-		else
-			endianness = -1;
-	}
-
-	if (endianness == 1) {
-		/* big endian */
-		return x;
-	}
-
-	/* little endian */
-	return ( ntohl( (x >> 32) & 0xFFFFFFFF ) | \
-			( (uint64_t) ntohl(x & 0xFFFFFFFF) ) << 32 );
-}
-
-static uint64_t htonll(uint64_t x)
-{
-	static int endianness = 0;
-
-	/* determine the endianness by checking how htonl() behaves; use -1
-	 * for little endian and 1 for big endian */
-	if (endianness == 0) {
-		if (htonl(1) == 1)
-			endianness = 1;
-		else
-			endianness = -1;
-	}
-
-	if (endianness == 1) {
-		/* big endian */
-		return x;
-	}
-
-	/* little endian */
-	return ( htonl( (x >> 32) & 0xFFFFFFFF ) | \
-			( (uint64_t) htonl(x & 0xFFFFFFFF) ) << 32 );
-}
-
-
 static void parse_incr(struct req_info *req)
 {
 	int cres, cache_only;