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
1724struct rbs_allocator ;
@@ -26,13 +33,13 @@ void *rbs_allocator_calloc_impl(rbs_allocator_t *, size_t count, size_t size, si
2633void * 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
0 commit comments