Skip to content

Commit

Permalink
Improving portability toward Windows #46 (comment).
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Sep 5, 2016
1 parent be27db4 commit 100397d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
13 changes: 13 additions & 0 deletions include/roaring/portability.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,17 @@ static inline int hamming(uint64_t x) {
#define UINT32_C(c) (c##UL)
#endif


// portable version of posix_memalign
static inline void * aligned_malloc(size_t alignment, size_t size) {
void *p;
#ifdef _MSC_VER
p = _aligned_malloc(size, alignment);
#else
if (posix_memalign(&p, alignment, size) != 0)
return NULL;
#endif
return p;
}

#endif /* INCLUDE_PORTABILITY_H_ */
16 changes: 8 additions & 8 deletions src/containers/bitset.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ bitset_container_t *bitset_container_create(void) {
return NULL;
}
// sizeof(__m256i) == 32
if (posix_memalign((void **)&bitset->array, 32,
sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS)) {
bitset->array = (uint64_t *) aligned_malloc(32, sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS);
if (! bitset->array) {
free(bitset);
return NULL;
}
Expand Down Expand Up @@ -108,8 +108,8 @@ bitset_container_t *bitset_container_clone(const bitset_container_t *src) {
return NULL;
}
// sizeof(__m256i) == 32
if (posix_memalign((void **)&bitset->array, 32,
sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS)) {
bitset->array = (uint64_t *) aligned_malloc(32, sizeof(uint64_t) * BITSET_CONTAINER_SIZE_IN_WORDS);
if (! bitset->array) {
free(bitset);
return NULL;
}
Expand Down Expand Up @@ -443,11 +443,11 @@ void* bitset_container_deserialize(const char *buf, size_t buf_len) {
if((ptr = (bitset_container_t *)malloc(sizeof(bitset_container_t))) != NULL) {
memcpy(ptr, buf, sizeof(bitset_container_t));
// sizeof(__m256i) == 32
if(posix_memalign((void **)&ptr->array, 32, l)) {
free(ptr);
return(NULL);
ptr->array = (uint64_t *) aligned_malloc(32, l);
if (! ptr->array) {
free(ptr);
return NULL;
}

memcpy(ptr->array, buf, l);
ptr->cardinality = bitset_container_compute_cardinality(ptr);
}
Expand Down

0 comments on commit 100397d

Please sign in to comment.