Skip to content

Commit

Permalink
Fix C++ amalgamation errors (#547)
Browse files Browse the repository at this point in the history
* Explictly cast allocations in art and roaring64

* Move big endian conversion into portability.h

Also define a unique macro name to avoid conflicts with endian.h
  • Loading branch information
SLieve authored Jan 10, 2024
1 parent 3cf84e2 commit 4230b8a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 43 deletions.
Binary file added amalgamation_demo
Binary file not shown.
31 changes: 31 additions & 0 deletions include/roaring/portability.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,37 @@ static inline int roaring_hamming(uint64_t x) {
#endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#endif

// Host <-> big endian conversion.
#if CROARING_IS_BIG_ENDIAN
#define croaring_htobe64(x) (x)

#elif defined(_WIN32) || defined(_WIN64) // CROARING_IS_BIG_ENDIAN
#include <stdlib.h>
#define croaring_htobe64(x) _byteswap_uint64(x)

#elif defined(__APPLE__) // CROARING_IS_BIG_ENDIAN
#include <libkern/OSByteOrder.h>
#define croaring_htobe64(x) OSSwapInt64(x)

#elif defined(__has_include) && \
__has_include(<byteswap.h>) // CROARING_IS_BIG_ENDIAN
#include <byteswap.h>
#define croaring_htobe64(x) __bswap_64(x)

#else // CROARING_IS_BIG_ENDIAN
// Gets compiled to bswap or equivalent on most compilers.
#define croaring_htobe64(x) \
(((x & 0x00000000000000FFULL) << 56) | \
((x & 0x000000000000FF00ULL) << 40) | \
((x & 0x0000000000FF0000ULL) << 24) | \
((x & 0x00000000FF000000ULL) << 8) | ((x & 0x000000FF00000000ULL) >> 8) | \
((x & 0x0000FF0000000000ULL) >> 24) | \
((x & 0x00FF000000000000ULL) >> 40) | \
((x & 0xFF00000000000000ULL) >> 56))
#endif // CROARING_IS_BIG_ENDIAN
#define croaring_be64toh(x) croaring_htobe64(x)
// End of host <-> big endian conversion.

// Defines for the possible CROARING atomic implementations
#define CROARING_ATOMIC_IMPL_NONE 1
#define CROARING_ATOMIC_IMPL_CPP 2
Expand Down
9 changes: 5 additions & 4 deletions src/art/art.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ static art_node_t *art_node256_insert(art_node256_t *node, art_node_t *child,

static art_node4_t *art_node4_create(const art_key_chunk_t prefix[],
uint8_t prefix_size) {
art_node4_t *node = roaring_malloc(sizeof(art_node4_t));
art_node4_t *node = (art_node4_t *)roaring_malloc(sizeof(art_node4_t));
art_init_inner_node(&node->base, ART_NODE4_TYPE, prefix, prefix_size);
node->count = 0;
return node;
Expand Down Expand Up @@ -297,7 +297,7 @@ static inline art_indexed_child_t art_node4_lower_bound(

static art_node16_t *art_node16_create(const art_key_chunk_t prefix[],
uint8_t prefix_size) {
art_node16_t *node = roaring_malloc(sizeof(art_node16_t));
art_node16_t *node = (art_node16_t *)roaring_malloc(sizeof(art_node16_t));
art_init_inner_node(&node->base, ART_NODE16_TYPE, prefix, prefix_size);
node->count = 0;
return node;
Expand Down Expand Up @@ -446,7 +446,7 @@ static inline art_indexed_child_t art_node16_lower_bound(

static art_node48_t *art_node48_create(const art_key_chunk_t prefix[],
uint8_t prefix_size) {
art_node48_t *node = roaring_malloc(sizeof(art_node48_t));
art_node48_t *node = (art_node48_t *)roaring_malloc(sizeof(art_node48_t));
art_init_inner_node(&node->base, ART_NODE48_TYPE, prefix, prefix_size);
node->count = 0;
for (size_t i = 0; i < 256; ++i) {
Expand Down Expand Up @@ -589,7 +589,8 @@ static inline art_indexed_child_t art_node48_lower_bound(

static art_node256_t *art_node256_create(const art_key_chunk_t prefix[],
uint8_t prefix_size) {
art_node256_t *node = roaring_malloc(sizeof(art_node256_t));
art_node256_t *node =
(art_node256_t *)roaring_malloc(sizeof(art_node256_t));
art_init_inner_node(&node->base, ART_NODE256_TYPE, prefix, prefix_size);
node->count = 0;
for (size_t i = 0; i < 256; ++i) {
Expand Down
50 changes: 11 additions & 39 deletions src/roaring64.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,11 @@
#include <roaring/art/art.h>
#include <roaring/containers/containers.h>
#include <roaring/roaring64.h>
#include <roaring/portability.h>
#include <stdarg.h>
#include <stdint.h>
#include <string.h>

#if CROARING_IS_BIG_ENDIAN
#define htobe64(x) (x)

#elif defined(_WIN32) || defined(_WIN64) // CROARING_IS_BIG_ENDIAN
#include <stdlib.h>
#define htobe64(x) _byteswap_uint64(x)

#elif defined(__APPLE__) // CROARING_IS_BIG_ENDIAN
#include <libkern/OSByteOrder.h>
#define htobe64(x) OSSwapInt64(x)

#elif defined(__has_include) && \
__has_include(<byteswap.h>) // CROARING_IS_BIG_ENDIAN
#include <byteswap.h>
#define htobe64(x) __bswap_64(x)

#else // CROARING_IS_BIG_ENDIAN
// Gets compiled to bswap or equivalent on most compilers.
#define htobe64(x) \
(((x & 0x00000000000000FFULL) << 56) | \
((x & 0x000000000000FF00ULL) << 40) | \
((x & 0x0000000000FF0000ULL) << 24) | \
((x & 0x00000000FF000000ULL) << 8) | ((x & 0x000000FF00000000ULL) >> 8) | \
((x & 0x0000FF0000000000ULL) >> 24) | \
((x & 0x00FF000000000000ULL) >> 40) | \
((x & 0xFF00000000000000ULL) >> 56))
#endif // CROARING_IS_BIG_ENDIAN

#define betoh64(x) htobe64(x)

#ifdef __cplusplus
using namespace ::roaring::internal;

Expand Down Expand Up @@ -71,7 +42,7 @@ typedef struct roaring64_leaf_s leaf_t;
// Splits the given uint64 key into high 48 bit and low 16 bit components.
// Expects high48_out to be of length ART_KEY_BYTES.
static inline uint16_t split_key(uint64_t key, uint8_t high48_out[]) {
uint64_t tmp = htobe64(key);
uint64_t tmp = croaring_htobe64(key);
memcpy(high48_out, (uint8_t *)(&tmp), ART_KEY_BYTES);
return (uint16_t)key;
}
Expand All @@ -81,22 +52,22 @@ static inline uint16_t split_key(uint64_t key, uint8_t high48_out[]) {
static inline uint64_t combine_key(const uint8_t high48[], uint16_t low16) {
uint64_t result = 0;
memcpy((uint8_t *)(&result), high48, ART_KEY_BYTES);
return betoh64(result) | low16;
return croaring_be64toh(result) | low16;
}

static inline uint64_t minimum(uint64_t a, uint64_t b) {
return (a < b) ? a : b;
}

static inline leaf_t *create_leaf(container_t *container, uint8_t typecode) {
leaf_t *leaf = roaring_malloc(sizeof(leaf_t));
leaf_t *leaf = (leaf_t *)roaring_malloc(sizeof(leaf_t));
leaf->container = container;
leaf->typecode = typecode;
return leaf;
}

static inline leaf_t *copy_leaf_container(const leaf_t *leaf) {
leaf_t *result_leaf = roaring_malloc(sizeof(leaf_t));
leaf_t *result_leaf = (leaf_t *)roaring_malloc(sizeof(leaf_t));
result_leaf->typecode = leaf->typecode;
// get_copy_of_container modifies the typecode passed in.
result_leaf->container = get_copy_of_container(
Expand All @@ -112,7 +83,8 @@ static inline int compare_high48(art_key_chunk_t key1[],
}

roaring64_bitmap_t *roaring64_bitmap_create(void) {
roaring64_bitmap_t *r = roaring_malloc(sizeof(roaring64_bitmap_t));
roaring64_bitmap_t *r =
(roaring64_bitmap_t *)roaring_malloc(sizeof(roaring64_bitmap_t));
r->art.root = NULL;
r->flags = 0;
return r;
Expand Down Expand Up @@ -793,7 +765,7 @@ roaring64_bitmap_t *roaring64_bitmap_and(const roaring64_bitmap_t *r1,
int compare_result = compare_high48(it1.key, it2.key);
if (compare_result == 0) {
// Case 2: iterators at the same high key position.
leaf_t *result_leaf = roaring_malloc(sizeof(leaf_t));
leaf_t *result_leaf = (leaf_t *)roaring_malloc(sizeof(leaf_t));
leaf_t *leaf1 = (leaf_t *)it1.value;
leaf_t *leaf2 = (leaf_t *)it2.value;
result_leaf->container = container_and(
Expand Down Expand Up @@ -991,7 +963,7 @@ roaring64_bitmap_t *roaring64_bitmap_or(const roaring64_bitmap_t *r1,
// Case 3b: iterators at the same high key position.
leaf_t *leaf1 = (leaf_t *)it1.value;
leaf_t *leaf2 = (leaf_t *)it2.value;
leaf_t *result_leaf = roaring_malloc(sizeof(leaf_t));
leaf_t *result_leaf = (leaf_t *)roaring_malloc(sizeof(leaf_t));
result_leaf->container = container_or(
leaf1->container, leaf1->typecode, leaf2->container,
leaf2->typecode, &result_leaf->typecode);
Expand Down Expand Up @@ -1107,7 +1079,7 @@ roaring64_bitmap_t *roaring64_bitmap_xor(const roaring64_bitmap_t *r1,
// Case 3b: iterators at the same high key position.
leaf_t *leaf1 = (leaf_t *)it1.value;
leaf_t *leaf2 = (leaf_t *)it2.value;
leaf_t *result_leaf = roaring_malloc(sizeof(leaf_t));
leaf_t *result_leaf = (leaf_t *)roaring_malloc(sizeof(leaf_t));
result_leaf->container = container_xor(
leaf1->container, leaf1->typecode, leaf2->container,
leaf2->typecode, &result_leaf->typecode);
Expand Down Expand Up @@ -1242,7 +1214,7 @@ roaring64_bitmap_t *roaring64_bitmap_andnot(const roaring64_bitmap_t *r1,
compare_result = compare_high48(it1.key, it2.key);
if (compare_result == 0) {
// Case 2b: iterators at the same high key position.
leaf_t *result_leaf = roaring_malloc(sizeof(leaf_t));
leaf_t *result_leaf = (leaf_t *)roaring_malloc(sizeof(leaf_t));
leaf_t *leaf1 = (leaf_t *)it1.value;
leaf_t *leaf2 = (leaf_t *)it2.value;
result_leaf->container = container_andnot(
Expand Down

0 comments on commit 4230b8a

Please sign in to comment.