From 0a7c51ef7cc4cdf9ee43d322ea09e3e618a83789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Borgstr=C3=B6m?= Date: Wed, 29 Jan 2014 21:34:21 +0100 Subject: [PATCH] Improve endianness code portability --- attic/_hashindex.c | 30 ++++++++++++++++-------------- attic/hashindex.pyx | 22 +++++++++++----------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/attic/_hashindex.c b/attic/_hashindex.c index a4def48b8..ae6737e41 100644 --- a/attic/_hashindex.c +++ b/attic/_hashindex.c @@ -9,12 +9,14 @@ #include #include -#if defined(__linux__) -#include -#elif defined(__APPLE__) && defined(__MACH__) -#include +#if defined(BYTE_ORDER)&&(BYTE_ORDER == BIG_ENDIAN) +#define _le32toh(x) __builtin_bswap32(x) +#define _htole32(x) __builtin_bswap32(x) +#elif defined(BYTE_ORDER)&&(BYTE_ORDER == LITTLE_ENDIAN) +#define _le32toh(x) (x) +#define _htole32(x) (x) #else -#include +#error Unknown byte order #endif typedef struct { @@ -41,8 +43,8 @@ typedef struct { } HashIndex; #define MAGIC "ATTICIDX" -#define EMPTY htole32(0xffffffff) -#define DELETED htole32(0xfffffffe) +#define EMPTY _htole32(0xffffffff) +#define DELETED _htole32(0xfffffffe) #define MAX_BUCKET_SIZE 512 #define BUCKET_LOWER_LIMIT .25 #define BUCKET_UPPER_LIMIT .90 @@ -74,7 +76,7 @@ static void *hashindex_next_key(HashIndex *index, const void *key); static int hashindex_index(HashIndex *index, const void *key) { - return le32toh(*((uint32_t *)key)) % index->num_buckets; + return _le32toh(*((uint32_t *)key)) % index->num_buckets; } static int @@ -193,7 +195,7 @@ hashindex_open(const char *path, int readonly) EPRINTF_PATH(path, "Unknown file header"); return NULL; } - if(length != sizeof(HashHeader) + le32toh(header->num_buckets) * (header->key_size + header->value_size)) { + if(length != sizeof(HashHeader) + _le32toh(header->num_buckets) * (header->key_size + header->value_size)) { EPRINTF_PATH(path, "Incorrect file length"); return NULL; } @@ -204,8 +206,8 @@ hashindex_open(const char *path, int readonly) index->readonly = readonly; index->map_addr = addr; index->map_length = length; - index->num_entries = le32toh(header->num_entries); - index->num_buckets = le32toh(header->num_buckets); + index->num_entries = _le32toh(header->num_entries); + index->num_buckets = _le32toh(header->num_buckets); index->key_size = header->key_size; index->value_size = header->value_size; index->bucket_size = index->key_size + index->value_size; @@ -230,7 +232,7 @@ hashindex_create(const char *path, int capacity, int key_size, int value_size) .magic = MAGIC, .num_entries = 0, .key_size = key_size, .value_size = value_size }; capacity = MAX(MIN_BUCKETS, capacity); - header.num_buckets = htole32(capacity); + header.num_buckets = _htole32(capacity); if(!(fd = fopen(path, "w"))) { EPRINTF_PATH(path, "fopen failed"); @@ -282,8 +284,8 @@ hashindex_flush(HashIndex *index) if(index->readonly) { return 1; } - *((uint32_t *)(index->map_addr + 8)) = htole32(index->num_entries); - *((uint32_t *)(index->map_addr + 12)) = htole32(index->num_buckets); + *((uint32_t *)(index->map_addr + 8)) = _htole32(index->num_entries); + *((uint32_t *)(index->map_addr + 12)) = _htole32(index->num_buckets); if(msync(index->map_addr, index->map_length, MS_SYNC) < 0) { EPRINTF("msync failed"); return 0; diff --git a/attic/hashindex.pyx b/attic/hashindex.pyx index eaf7057fb..69c185ed4 100644 --- a/attic/hashindex.pyx +++ b/attic/hashindex.pyx @@ -16,8 +16,8 @@ cdef extern from "_hashindex.c": void *hashindex_next_key(HashIndex *index, void *key) int hashindex_delete(HashIndex *index, void *key) int hashindex_set(HashIndex *index, void *key, void *value) - int htole32(int v) - int le32toh(int v) + int _htole32(int v) + int _le32toh(int v) _NoDefault = object() @@ -90,13 +90,13 @@ cdef class NSIndex(IndexBase): data = hashindex_get(self.index, key) if not data: raise KeyError - return le32toh(data[0]), le32toh(data[1]) + return _le32toh(data[0]), _le32toh(data[1]) def __setitem__(self, key, value): assert len(key) == 32 cdef int[2] data - data[0] = htole32(value[0]) - data[1] = htole32(value[1]) + data[0] = _htole32(value[0]) + data[1] = _htole32(value[1]) if not hashindex_set(self.index, key, data): raise Exception('hashindex_set failed') @@ -126,7 +126,7 @@ cdef class NSKeyIterator: if not self.key: raise StopIteration cdef int *value = (self.key + 32) - return self.key[:32], (le32toh(value[0]), le32toh(value[1])) + return self.key[:32], (_le32toh(value[0]), _le32toh(value[1])) cdef class ChunkIndex(IndexBase): @@ -138,14 +138,14 @@ cdef class ChunkIndex(IndexBase): data = hashindex_get(self.index, key) if not data: raise KeyError - return le32toh(data[0]), le32toh(data[1]), le32toh(data[2]) + return _le32toh(data[0]), _le32toh(data[1]), _le32toh(data[2]) def __setitem__(self, key, value): assert len(key) == 32 cdef int[3] data - data[0] = htole32(value[0]) - data[1] = htole32(value[1]) - data[2] = htole32(value[2]) + data[0] = _htole32(value[0]) + data[1] = _htole32(value[1]) + data[2] = _htole32(value[2]) if not hashindex_set(self.index, key, data): raise Exception('hashindex_set failed') @@ -175,4 +175,4 @@ cdef class ChunkKeyIterator: if not self.key: raise StopIteration cdef int *value = (self.key + 32) - return self.key[:32], (le32toh(value[0]), le32toh(value[1]), le32toh(value[2])) + return self.key[:32], (_le32toh(value[0]), _le32toh(value[1]), _le32toh(value[2]))