BumpPtrAllocator: do the size check without moving any pointers
[oota-llvm.git] / include / llvm / Support / MathExtras.h
index adfca78fbeae46ee6fca2c153497d8aa72aadbe5..6a104e738fa7919b0f83bf89c60c3c7d642c8400 100644 (file)
@@ -558,9 +558,17 @@ inline uintptr_t alignAddr(void *Addr, size_t Alignment) {
   assert(Alignment && isPowerOf2_64((uint64_t)Alignment) &&
          "Alignment is not a power of two!");
 
+  assert((uintptr_t)Addr + Alignment - 1 >= (uintptr_t)Addr);
+
   return (((uintptr_t)Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1));
 }
 
+/// \brief Returns the necessary adjustment for aligning \c Ptr to \c Alignment
+/// bytes, rounding up.
+inline size_t alignmentAdjustment(void *Ptr, size_t Alignment) {
+  return alignAddr(Ptr, Alignment) - (uintptr_t)Ptr;
+}
+
 /// NextPowerOf2 - Returns the next power of two (in 64-bits)
 /// that is strictly greater than A.  Returns zero on overflow.
 inline uint64_t NextPowerOf2(uint64_t A) {