Skip to content

Instantly share code, notes, and snippets.

@awreece
Last active August 29, 2015 14:05
Show Gist options
  • Save awreece/c9dc6b3036deb71e4b48 to your computer and use it in GitHub Desktop.
Save awreece/c9dc6b3036deb71e4b48 to your computer and use it in GitHub Desktop.
// Align p to a multiple of w bytes
static inline void* align(const void const* p, unsigned char w) {
return (void*)(((uintptr_t)(p) + (w-1)) & ~(w-1));
}
static inline uintptr_t
round_up(uintptr_t val, uintptr_t alignment) {
// Alignment must be a power of two.
ASSERT3(__builtin_popcount(alignment), ==, 1);
return ((val + (alignment-1)) & ~(alignment-1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment