diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2011-10-12 02:44:00 +0200 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2011-10-12 02:44:00 +0200 |
commit | 29b10266b3a799e4340535e524221796ba9c68dd (patch) | |
tree | 70a09b6439b6bf855eca2b33ade02fb83b18e757 | |
parent | a4640c8a04f2575f97c0ee92ebc5f39643c18040 (diff) | |
download | erlang-slang-fork-29b10266b3a799e4340535e524221796ba9c68dd.tar.gz |
replaced the put_ and get_ macros with slightly more sane and efficient versions
considering that erlang understands native endianess in binaries, the whole
translation is unnecessary
-rw-r--r-- | c_src/slang_drv.c | 31 |
1 files changed, 14 insertions, 17 deletions
diff --git a/c_src/slang_drv.c b/c_src/slang_drv.c index e46aa9d..d6fd6d3 100644 --- a/c_src/slang_drv.c +++ b/c_src/slang_drv.c @@ -1,8 +1,9 @@ - #include <stdio.h> #include <string.h> #include <signal.h> #include <unistd.h> +#include <arpa/inet.h> +#include <stdint.h> #include <slang.h> #include <erl_driver.h> @@ -15,27 +16,23 @@ /* Standard set of integer macros .. */ -#define get_int32(s) ((((unsigned char*) (s))[0] << 24) | \ - (((unsigned char*) (s))[1] << 16) | \ - (((unsigned char*) (s))[2] << 8) | \ - (((unsigned char*) (s))[3])) - -#define put_int32(i, s) {((char*)(s))[0] = (char)((i) >> 24) & 0xff; \ - ((char*)(s))[1] = (char)((i) >> 16) & 0xff; \ - ((char*)(s))[2] = (char)((i) >> 8) & 0xff; \ - ((char*)(s))[3] = (char)((i) & 0xff);} - -#define get_int16(s) ((((unsigned char*) (s))[0] << 8) | \ - (((unsigned char*) (s))[1])) +#define get_int32(s) ntohl(*(uint32_t *)(s)) +#define put_int32(i, s) do { \ + *(uint32_t *)(s) = htonl((uint32_t)(i)); \ +} while (0) -#define put_int16(i, s) {((unsigned char*)(s))[0] = ((i) >> 8) & 0xff; \ - ((unsigned char*)(s))[1] = (i) & 0xff;} +#define get_int16(s) ntohs(*(uint16_t *)(s)) -#define get_int8(s) ((((unsigned char*) (s))[0] )) +#define put_int16(i, s) do { \ + *(uint16_t *)(s) = htons((uint16_t)(i)); \ +} while (0) +#define get_int8(s) (*(uint8_t *)(s)) -#define put_int8(i, s) { ((unsigned char*)(s))[0] = (i) & 0xff;} +#define put_int8(i, s) do { \ + *(uint8_t *)(s) = (uint8_t)(i); \ +} while (0) |