mirror of
https://github.com/borgbackup/borg.git
synced 2024-12-25 01:06:50 +00:00
Improve endianness code portability
This commit is contained in:
parent
5bde1c8987
commit
0a7c51ef7c
2 changed files with 27 additions and 25 deletions
|
@ -9,12 +9,14 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
|
||||||
#if defined(__linux__)
|
#if defined(BYTE_ORDER)&&(BYTE_ORDER == BIG_ENDIAN)
|
||||||
#include <endian.h>
|
#define _le32toh(x) __builtin_bswap32(x)
|
||||||
#elif defined(__APPLE__) && defined(__MACH__)
|
#define _htole32(x) __builtin_bswap32(x)
|
||||||
#include <machine/endian.h>
|
#elif defined(BYTE_ORDER)&&(BYTE_ORDER == LITTLE_ENDIAN)
|
||||||
|
#define _le32toh(x) (x)
|
||||||
|
#define _htole32(x) (x)
|
||||||
#else
|
#else
|
||||||
#include <sys/endian.h>
|
#error Unknown byte order
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -41,8 +43,8 @@ typedef struct {
|
||||||
} HashIndex;
|
} HashIndex;
|
||||||
|
|
||||||
#define MAGIC "ATTICIDX"
|
#define MAGIC "ATTICIDX"
|
||||||
#define EMPTY htole32(0xffffffff)
|
#define EMPTY _htole32(0xffffffff)
|
||||||
#define DELETED htole32(0xfffffffe)
|
#define DELETED _htole32(0xfffffffe)
|
||||||
#define MAX_BUCKET_SIZE 512
|
#define MAX_BUCKET_SIZE 512
|
||||||
#define BUCKET_LOWER_LIMIT .25
|
#define BUCKET_LOWER_LIMIT .25
|
||||||
#define BUCKET_UPPER_LIMIT .90
|
#define BUCKET_UPPER_LIMIT .90
|
||||||
|
@ -74,7 +76,7 @@ static void *hashindex_next_key(HashIndex *index, const void *key);
|
||||||
static int
|
static int
|
||||||
hashindex_index(HashIndex *index, const void *key)
|
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
|
static int
|
||||||
|
@ -193,7 +195,7 @@ hashindex_open(const char *path, int readonly)
|
||||||
EPRINTF_PATH(path, "Unknown file header");
|
EPRINTF_PATH(path, "Unknown file header");
|
||||||
return NULL;
|
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");
|
EPRINTF_PATH(path, "Incorrect file length");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -204,8 +206,8 @@ hashindex_open(const char *path, int readonly)
|
||||||
index->readonly = readonly;
|
index->readonly = readonly;
|
||||||
index->map_addr = addr;
|
index->map_addr = addr;
|
||||||
index->map_length = length;
|
index->map_length = length;
|
||||||
index->num_entries = le32toh(header->num_entries);
|
index->num_entries = _le32toh(header->num_entries);
|
||||||
index->num_buckets = le32toh(header->num_buckets);
|
index->num_buckets = _le32toh(header->num_buckets);
|
||||||
index->key_size = header->key_size;
|
index->key_size = header->key_size;
|
||||||
index->value_size = header->value_size;
|
index->value_size = header->value_size;
|
||||||
index->bucket_size = index->key_size + index->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
|
.magic = MAGIC, .num_entries = 0, .key_size = key_size, .value_size = value_size
|
||||||
};
|
};
|
||||||
capacity = MAX(MIN_BUCKETS, capacity);
|
capacity = MAX(MIN_BUCKETS, capacity);
|
||||||
header.num_buckets = htole32(capacity);
|
header.num_buckets = _htole32(capacity);
|
||||||
|
|
||||||
if(!(fd = fopen(path, "w"))) {
|
if(!(fd = fopen(path, "w"))) {
|
||||||
EPRINTF_PATH(path, "fopen failed");
|
EPRINTF_PATH(path, "fopen failed");
|
||||||
|
@ -282,8 +284,8 @@ hashindex_flush(HashIndex *index)
|
||||||
if(index->readonly) {
|
if(index->readonly) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
*((uint32_t *)(index->map_addr + 8)) = htole32(index->num_entries);
|
*((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 + 12)) = _htole32(index->num_buckets);
|
||||||
if(msync(index->map_addr, index->map_length, MS_SYNC) < 0) {
|
if(msync(index->map_addr, index->map_length, MS_SYNC) < 0) {
|
||||||
EPRINTF("msync failed");
|
EPRINTF("msync failed");
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -16,8 +16,8 @@ cdef extern from "_hashindex.c":
|
||||||
void *hashindex_next_key(HashIndex *index, void *key)
|
void *hashindex_next_key(HashIndex *index, void *key)
|
||||||
int hashindex_delete(HashIndex *index, void *key)
|
int hashindex_delete(HashIndex *index, void *key)
|
||||||
int hashindex_set(HashIndex *index, void *key, void *value)
|
int hashindex_set(HashIndex *index, void *key, void *value)
|
||||||
int htole32(int v)
|
int _htole32(int v)
|
||||||
int le32toh(int v)
|
int _le32toh(int v)
|
||||||
|
|
||||||
|
|
||||||
_NoDefault = object()
|
_NoDefault = object()
|
||||||
|
@ -90,13 +90,13 @@ cdef class NSIndex(IndexBase):
|
||||||
data = <int *>hashindex_get(self.index, <char *>key)
|
data = <int *>hashindex_get(self.index, <char *>key)
|
||||||
if not data:
|
if not data:
|
||||||
raise KeyError
|
raise KeyError
|
||||||
return le32toh(data[0]), le32toh(data[1])
|
return _le32toh(data[0]), _le32toh(data[1])
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
assert len(key) == 32
|
assert len(key) == 32
|
||||||
cdef int[2] data
|
cdef int[2] data
|
||||||
data[0] = htole32(value[0])
|
data[0] = _htole32(value[0])
|
||||||
data[1] = htole32(value[1])
|
data[1] = _htole32(value[1])
|
||||||
if not hashindex_set(self.index, <char *>key, data):
|
if not hashindex_set(self.index, <char *>key, data):
|
||||||
raise Exception('hashindex_set failed')
|
raise Exception('hashindex_set failed')
|
||||||
|
|
||||||
|
@ -126,7 +126,7 @@ cdef class NSKeyIterator:
|
||||||
if not self.key:
|
if not self.key:
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
cdef int *value = <int *>(self.key + 32)
|
cdef int *value = <int *>(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):
|
cdef class ChunkIndex(IndexBase):
|
||||||
|
@ -138,14 +138,14 @@ cdef class ChunkIndex(IndexBase):
|
||||||
data = <int *>hashindex_get(self.index, <char *>key)
|
data = <int *>hashindex_get(self.index, <char *>key)
|
||||||
if not data:
|
if not data:
|
||||||
raise KeyError
|
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):
|
def __setitem__(self, key, value):
|
||||||
assert len(key) == 32
|
assert len(key) == 32
|
||||||
cdef int[3] data
|
cdef int[3] data
|
||||||
data[0] = htole32(value[0])
|
data[0] = _htole32(value[0])
|
||||||
data[1] = htole32(value[1])
|
data[1] = _htole32(value[1])
|
||||||
data[2] = htole32(value[2])
|
data[2] = _htole32(value[2])
|
||||||
if not hashindex_set(self.index, <char *>key, data):
|
if not hashindex_set(self.index, <char *>key, data):
|
||||||
raise Exception('hashindex_set failed')
|
raise Exception('hashindex_set failed')
|
||||||
|
|
||||||
|
@ -175,4 +175,4 @@ cdef class ChunkKeyIterator:
|
||||||
if not self.key:
|
if not self.key:
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
cdef int *value = <int *>(self.key + 32)
|
cdef int *value = <int *>(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]))
|
||||||
|
|
Loading…
Reference in a new issue