Skip to content

Commit 9cb4236

Browse files
committed
Fix alignof compatibility issue
1 parent 7170e35 commit 9cb4236

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

include/rbs/util/rbs_allocator.h

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,33 @@
33

44
#include <stddef.h>
55

6-
#ifndef alignof
7-
#if defined(__GNUC__) || defined(__clang__)
8-
#define alignof(type) __alignof__(type)
6+
/* Include stdalign.h for C11 and later for alignof support */
7+
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
8+
#include <stdalign.h>
9+
#endif
10+
11+
/*
12+
* Define a portable alignment macro that works across all supported environments
13+
*/
14+
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
15+
/* C11 or later - use _Alignof directly (always available in C11+) */
16+
#define rbs_alignof(type) _Alignof(type)
17+
#elif defined(__cplusplus) && __cplusplus >= 201103L
18+
/* C++11 or later has alignof keyword */
19+
#define rbs_alignof(type) alignof(type)
20+
#elif defined(__GNUC__) || defined(__clang__)
21+
/* GCC and Clang provide __alignof__ */
22+
#define rbs_alignof(type) __alignof__(type)
923
#elif defined(_MSC_VER)
10-
#define alignof(type) __alignof(type)
24+
/* MSVC provides __alignof */
25+
#define rbs_alignof(type) __alignof(type)
1126
#else
12-
// Fallback using offset trick
13-
#define alignof(type) offsetof( \
14-
struct { char c; type member; }, \
15-
member \
27+
/* Fallback using offset trick for other compilers */
28+
#define rbs_alignof(type) offsetof( \
29+
struct { char c; type member; }, \
30+
member \
1631
)
1732
#endif
18-
#endif
1933

2034
struct rbs_allocator;
2135
typedef struct rbs_allocator rbs_allocator_t;
@@ -29,13 +43,13 @@ void *rbs_allocator_calloc_impl(rbs_allocator_t *, size_t count, size_t size, si
2943
void *rbs_allocator_realloc_impl(rbs_allocator_t *, void *ptr, size_t old_size, size_t new_size, size_t alignment);
3044

3145
// Use this when allocating memory for a single instance of a type.
32-
#define rbs_allocator_alloc(allocator, type) ((type *) rbs_allocator_malloc_impl((allocator), sizeof(type), alignof(type)))
46+
#define rbs_allocator_alloc(allocator, type) ((type *) rbs_allocator_malloc_impl((allocator), sizeof(type), rbs_alignof(type)))
3347
// Use this when allocating memory that will be immediately written to in full.
3448
// Such as allocating strings
35-
#define rbs_allocator_alloc_many(allocator, count, type) ((type *) rbs_allocator_malloc_many_impl((allocator), (count), sizeof(type), alignof(type)))
49+
#define rbs_allocator_alloc_many(allocator, count, type) ((type *) rbs_allocator_malloc_many_impl((allocator), (count), sizeof(type), rbs_alignof(type)))
3650
// Use this when allocating memory that will NOT be immediately written to in full.
3751
// Such as allocating buffers
38-
#define rbs_allocator_calloc(allocator, count, type) ((type *) rbs_allocator_calloc_impl((allocator), (count), sizeof(type), alignof(type)))
39-
#define rbs_allocator_realloc(allocator, ptr, old_size, new_size, type) ((type *) rbs_allocator_realloc_impl((allocator), (ptr), (old_size), (new_size), alignof(type)))
52+
#define rbs_allocator_calloc(allocator, count, type) ((type *) rbs_allocator_calloc_impl((allocator), (count), sizeof(type), rbs_alignof(type)))
53+
#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)))
4054

4155
#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)