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(struct { char c; type member; }, member)
14- #endif
27+ /* Fallback using offset trick for other compilers */
28+ #define rbs_alignof (type ) offsetof( \
29+ struct { char c; type member; }, \
30+ member \
31+ )
1532#endif
1633
1734struct rbs_allocator ;
@@ -26,13 +43,13 @@ void *rbs_allocator_calloc_impl(rbs_allocator_t *, size_t count, size_t size, si
2643void *rbs_allocator_realloc_impl (rbs_allocator_t *, void *ptr, size_t old_size, size_t new_size, size_t alignment);
2744
2845// 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)))
46+ #define rbs_allocator_alloc (allocator, type ) ((type *) rbs_allocator_malloc_impl((allocator), sizeof (type), rbs_alignof (type)))
3047// Use this when allocating memory that will be immediately written to in full.
3148// 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)))
49+ #define rbs_allocator_alloc_many (allocator, count, type ) ((type *) rbs_allocator_malloc_many_impl((allocator), (count), sizeof (type), rbs_alignof (type)))
3350// Use this when allocating memory that will NOT be immediately written to in full.
3451// 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)))
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)))
3754
3855#endif
0 commit comments