[llvm] r204995 - [Allocator Cleanup] Move generic pointer alignment helper out of an

Chandler Carruth chandlerc at gmail.com
Fri Mar 28 02:08:15 PDT 2014


Author: chandlerc
Date: Fri Mar 28 04:08:14 2014
New Revision: 204995

URL: http://llvm.org/viewvc/llvm-project?rev=204995&view=rev
Log:
[Allocator Cleanup] Move generic pointer alignment helper out of an
out-of-line private static method and into the collection of inline
alignment helpers in MathExtras.h.

Modified:
    llvm/trunk/include/llvm/Support/Allocator.h
    llvm/trunk/include/llvm/Support/MathExtras.h
    llvm/trunk/lib/Support/Allocator.cpp

Modified: llvm/trunk/include/llvm/Support/Allocator.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Allocator.h?rev=204995&r1=204994&r2=204995&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Allocator.h (original)
+++ llvm/trunk/include/llvm/Support/Allocator.h Fri Mar 28 04:08:14 2014
@@ -138,12 +138,6 @@ class BumpPtrAllocator {
   /// for extremely heavy memory use scenarios.
   size_t NumSlabs;
 
-  /// \brief Aligns \c Ptr to \c Alignment bytes, rounding up.
-  ///
-  /// Alignment should be a power of two.  This method rounds up, so
-  /// AlignPtr(7, 4) == 8 and AlignPtr(8, 4) == 8.
-  static char *AlignPtr(char *Ptr, size_t Alignment);
-
   /// \brief Allocate a new slab and move the bump pointers over into the new
   /// slab, modifying CurPtr and End.
   void StartNewSlab();
@@ -219,7 +213,7 @@ public:
       char *End = Slab == Allocator.CurSlab ? Allocator.CurPtr
                                             : (char *)Slab + Slab->Size;
       for (char *Ptr = (char *)(Slab + 1); Ptr < End; Ptr += sizeof(T)) {
-        Ptr = Allocator.AlignPtr(Ptr, alignOf<T>());
+        Ptr = alignPtr(Ptr, alignOf<T>());
         if (Ptr + sizeof(T) <= End)
           reinterpret_cast<T *>(Ptr)->~T();
       }

Modified: llvm/trunk/include/llvm/Support/MathExtras.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/MathExtras.h?rev=204995&r1=204994&r2=204995&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/MathExtras.h (original)
+++ llvm/trunk/include/llvm/Support/MathExtras.h Fri Mar 28 04:08:14 2014
@@ -16,6 +16,7 @@
 
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/SwapByteOrder.h"
+#include <cassert>
 #include <cstring>
 #include <type_traits>
 
@@ -540,6 +541,18 @@ inline uint64_t MinAlign(uint64_t A, uin
   return (A | B) & (1 + ~(A | B));
 }
 
+/// \brief Aligns \c Ptr to \c Alignment bytes, rounding up.
+///
+/// Alignment should be a power of two.  This method rounds up, so
+/// AlignPtr(7, 4) == 8 and AlignPtr(8, 4) == 8.
+inline char *alignPtr(char *Ptr, size_t Alignment) {
+  assert(Alignment && isPowerOf2_64((uint64_t)Alignment) &&
+         "Alignment is not a power of two!");
+
+  return (char *)(((uintptr_t)Ptr + Alignment - 1) &
+                  ~(uintptr_t)(Alignment - 1));
+}
+
 /// 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) {

Modified: llvm/trunk/lib/Support/Allocator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Allocator.cpp?rev=204995&r1=204994&r2=204995&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Allocator.cpp (original)
+++ llvm/trunk/lib/Support/Allocator.cpp Fri Mar 28 04:08:14 2014
@@ -35,18 +35,6 @@ BumpPtrAllocator::~BumpPtrAllocator() {
   DeallocateSlabs(CurSlab);
 }
 
-/// AlignPtr - Align Ptr to Alignment bytes, rounding up.  Alignment should
-/// be a power of two.  This method rounds up, so AlignPtr(7, 4) == 8 and
-/// AlignPtr(8, 4) == 8.
-char *BumpPtrAllocator::AlignPtr(char *Ptr, size_t Alignment) {
-  assert(Alignment && (Alignment & (Alignment - 1)) == 0 &&
-         "Alignment is not a power of two!");
-
-  // Do the alignment.
-  return (char*)(((uintptr_t)Ptr + Alignment - 1) &
-                 ~(uintptr_t)(Alignment - 1));
-}
-
 /// StartNewSlab - Allocate a new slab and move the bump pointers over into
 /// the new slab.  Modifies CurPtr and End.
 void BumpPtrAllocator::StartNewSlab() {
@@ -110,7 +98,7 @@ void *BumpPtrAllocator::Allocate(size_t
   if (Alignment == 0) Alignment = 1;
 
   // Allocate the aligned space, going forwards from CurPtr.
-  char *Ptr = AlignPtr(CurPtr, Alignment);
+  char *Ptr = alignPtr(CurPtr, Alignment);
 
   // Check if we can hold it.
   if (Ptr + Size <= End) {
@@ -133,7 +121,7 @@ void *BumpPtrAllocator::Allocate(size_t
     NewSlab->NextPtr = CurSlab->NextPtr;
     CurSlab->NextPtr = NewSlab;
 
-    Ptr = AlignPtr((char*)(NewSlab + 1), Alignment);
+    Ptr = alignPtr((char*)(NewSlab + 1), Alignment);
     assert((uintptr_t)Ptr + Size <= (uintptr_t)NewSlab + NewSlab->Size);
     __msan_allocated_memory(Ptr, Size);
     return Ptr;
@@ -141,7 +129,7 @@ void *BumpPtrAllocator::Allocate(size_t
 
   // Otherwise, start a new slab and try again.
   StartNewSlab();
-  Ptr = AlignPtr(CurPtr, Alignment);
+  Ptr = alignPtr(CurPtr, Alignment);
   CurPtr = Ptr + Size;
   assert(CurPtr <= End && "Unable to allocate memory!");
   __msan_allocated_memory(Ptr, Size);





More information about the llvm-commits mailing list