Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- #define G_UNLIKELY(x) __builtin_expect(!!(x), 0)
- #define SPICE_STRLOC__(L) #L
- #define SPICE_STRLOC_(F,L) F ":" SPICE_STRLOC__(L)
- #define SPICE_STRLOC SPICE_STRLOC_(__FILE__, __LINE__)
- void spice_alignment_warning(const char *loc, void *p, unsigned sz)
- {
- printf("warning\n");
- }
- void spice_alignment_debug(const char *loc, void *p, unsigned sz)
- {
- printf("debug\n");
- }
- static inline void *spice_alignment_check(const char *loc,
- void *ptr, unsigned sz)
- {
- if (G_UNLIKELY(((uintptr_t) ptr & (sz-1U)) != 0))
- spice_alignment_warning(loc, ptr, sz);
- return ptr;
- }
- static inline void *spice_alignment_weak_check(const char *loc,
- void *ptr, unsigned sz)
- {
- if (G_UNLIKELY(((uintptr_t) ptr & (sz-1U)) != 0))
- spice_alignment_debug(loc, ptr, sz);
- return ptr;
- }
- #define SPICE_ALIGNED_CAST(type, value) \
- ((type)spice_alignment_check(SPICE_STRLOC, \
- (void *)(value), __alignof(type)))
- #define SPICE_UNALIGNED_CAST(type, value) \
- ((type)spice_alignment_weak_check(SPICE_STRLOC, \
- (void *)(value), __alignof(type)))
- int main()
- {
- char *buffer = malloc(27);
- int *aligned = SPICE_ALIGNED_CAST(int *, buffer);
- if (buffer == aligned)
- return 0;
- fprintf(stderr, "kaboom\n");
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment