Skip to content

Commit 6e95693

Browse files
committed
Fix alignof compatibility issue
1 parent 6753d91 commit 6e95693

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

include/rbs/util/rbs_allocator.h

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33

44
#include <stddef.h>
55

6-
#ifndef alignof
6+
#if !defined(alignof) && !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L)
7+
/* Only define our own alignof if we're not in C11 mode and not in C++ mode */
78
#if defined(__GNUC__) || defined(__clang__)
8-
#define alignof(type) __alignof__(type)
9+
#define rbs_alignof(type) __alignof__(type)
910
#elif defined(_MSC_VER)
10-
#define alignof(type) __alignof(type)
11+
#define rbs_alignof(type) __alignof(type)
1112
#else
12-
// Fallback using offset trick
13-
#define alignof(type) offsetof(struct { char c; type member; }, member)
13+
/* Fallback using offset trick */
14+
#define rbs_alignof(type) offsetof( \
15+
struct { char c; type member; }, \
16+
member \
17+
)
1418
#endif
19+
#else
20+
/* In C11 or C++, use the built-in alignof */
21+
#define rbs_alignof(type) alignof(type)
1522
#endif
1623

1724
struct rbs_allocator;
@@ -26,13 +33,13 @@ void *rbs_allocator_calloc_impl(rbs_allocator_t *, size_t count, size_t size, si
2633
void *rbs_allocator_realloc_impl(rbs_allocator_t *, void *ptr, size_t old_size, size_t new_size, size_t alignment);
2734

2835
// Use this when allocating memory for a single instance of a type.
29-
#define rbs_allocator_alloc(allocator, type) ((type *) rbs_allocator_malloc_impl((allocator), sizeof(type), alignof(type)))
36+
#define rbs_allocator_alloc(allocator, type) ((type *) rbs_allocator_malloc_impl((allocator), sizeof(type), rbs_alignof(type)))
3037
// Use this when allocating memory that will be immediately written to in full.
3138
// Such as allocating strings
32-
#define rbs_allocator_alloc_many(allocator, count, type) ((type *) rbs_allocator_malloc_many_impl((allocator), (count), sizeof(type), alignof(type)))
39+
#define rbs_allocator_alloc_many(allocator, count, type) ((type *) rbs_allocator_malloc_many_impl((allocator), (count), sizeof(type), rbs_alignof(type)))
3340
// Use this when allocating memory that will NOT be immediately written to in full.
3441
// Such as allocating buffers
35-
#define rbs_allocator_calloc(allocator, count, type) ((type *) rbs_allocator_calloc_impl((allocator), (count), sizeof(type), alignof(type)))
36-
#define rbs_allocator_realloc(allocator, ptr, old_size, new_size, type) ((type *) rbs_allocator_realloc_impl((allocator), (ptr), (old_size), (new_size), alignof(type)))
42+
#define rbs_allocator_calloc(allocator, count, type) ((type *) rbs_allocator_calloc_impl((allocator), (count), sizeof(type), rbs_alignof(type)))
43+
#define rbs_allocator_realloc(allocator, ptr, old_size, new_size, type) ((type *) rbs_allocator_realloc_impl((allocator), (ptr), (old_size), (new_size), rbs_alignof(type)))
3744

3845
#endif

src/location.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
void rbs_loc_alloc_children(rbs_allocator_t *allocator, rbs_location_t *loc, size_t capacity) {
99
rbs_assert(capacity <= sizeof(rbs_loc_entry_bitmap) * 8, "Capacity %zu is too large. Max is %zu", capacity, sizeof(rbs_loc_entry_bitmap) * 8);
1010

11-
loc->children = rbs_allocator_malloc_impl(allocator, RBS_LOC_CHILDREN_SIZE(capacity), alignof(rbs_loc_children));
11+
loc->children = rbs_allocator_malloc_impl(allocator, RBS_LOC_CHILDREN_SIZE(capacity), rbs_alignof(rbs_loc_children));
1212

1313
loc->children->len = 0;
1414
loc->children->required_p = 0;

0 commit comments

Comments
 (0)