diff options
author | Lucas De Marchi <lucas.demarchi@profusion.mobi> | 2012-05-21 20:43:39 -0300 |
---|---|---|
committer | Lucas De Marchi <lucas.demarchi@profusion.mobi> | 2012-05-21 20:43:39 -0300 |
commit | af9080d9f92272a8331b28b875b4f745bd0043c1 (patch) | |
tree | fedae46d3ecb4fb6ce1f72c44101bd6a3f3250dc | |
parent | a6b67f90c3fd4a2d2880649bea7e95df0f35bb08 (diff) | |
download | kmod-af9080d9f92272a8331b28b875b4f745bd0043c1.tar.gz |
libkmod-hash: use generic function for unaligned access
-rw-r--r-- | libkmod/libkmod-hash.c | 18 | ||||
-rw-r--r-- | libkmod/libkmod-util.h | 2 |
2 files changed, 6 insertions, 14 deletions
diff --git a/libkmod/libkmod-hash.c b/libkmod/libkmod-hash.c index 3a6c8bf..1d02da5 100644 --- a/libkmod/libkmod-hash.c +++ b/libkmod/libkmod-hash.c @@ -21,6 +21,7 @@ #include "libkmod.h" #include "libkmod-hash.h" +#include "libkmod-util.h" #include <stdlib.h> #include <string.h> #include <errno.h> @@ -83,15 +84,6 @@ void hash_free(struct hash *hash) free(hash); } -struct unaligned_short { - unsigned short v; -} __attribute__((packed)); - -static inline unsigned short get16bits(const char *ptr) -{ - return ((struct unaligned_short *)ptr)->v; -} - static inline unsigned int hash_superfast(const char *key, unsigned int len) { /* Paul Hsieh (http://www.azillionmonkeys.com/qed/hash.html) @@ -104,8 +96,8 @@ static inline unsigned int hash_superfast(const char *key, unsigned int len) /* Main loop */ for (; len > 0; len--) { - hash += get16bits(key); - tmp = (get16bits(key + 2) << 11) ^ hash; + hash += get_unaligned((uint16_t *) key); + tmp = (get_unaligned((uint16_t *)(key + 2)) << 11) ^ hash; hash = (hash << 16) ^ tmp; key += 4; hash += hash >> 11; @@ -114,14 +106,14 @@ static inline unsigned int hash_superfast(const char *key, unsigned int len) /* Handle end cases */ switch (rem) { case 3: - hash += get16bits(key); + hash += get_unaligned((uint16_t *) key); hash ^= hash << 16; hash ^= key[2] << 18; hash += hash >> 11; break; case 2: - hash += get16bits(key); + hash += get_unaligned((uint16_t *) key); hash ^= hash << 11; hash += hash >> 17; break; diff --git a/libkmod/libkmod-util.h b/libkmod/libkmod-util.h index ffe7c43..dae613a 100644 --- a/libkmod/libkmod-util.h +++ b/libkmod/libkmod-util.h @@ -33,7 +33,7 @@ unsigned long long stat_mstamp(const struct stat *st); __p->__v; \ }) -#define bt_put_unaligned(val, ptr) \ +#define put_unaligned(val, ptr) \ do { \ struct __attribute__((packed)) { \ typeof(*(ptr)) __v; \ |