Bug fix in BumpPtrAllocator: don't assume that all objects have the same alignment...
authorTed Kremenek <kremenek@apple.com>
Mon, 28 Apr 2008 17:58:07 +0000 (17:58 +0000)
committerTed Kremenek <kremenek@apple.com>
Mon, 28 Apr 2008 17:58:07 +0000 (17:58 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50362 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/Allocator.cpp

index 5961afe04c7ffd47b046a961e5ca81b5bc3ae4a6..8ccd3908446a4a47ecf96f49d49bdecb09bef673 100644 (file)
@@ -46,13 +46,16 @@ public:
   /// Allocate - Allocate and return at least the specified number of bytes.
   ///
   void *Allocate(unsigned AllocSize, unsigned Alignment, MemRegion **RegPtr) {
-    // Round size up to an even multiple of the alignment.
-    AllocSize = (AllocSize+Alignment-1) & ~(Alignment-1);
     
-    // If there is space in this region, return it.
-    if (unsigned(NextPtr+AllocSize-(char*)this) <= RegionSize) {
-      void *Result = NextPtr;
-      NextPtr += AllocSize;
+    char* Result = (char*) (((uintptr_t) (NextPtr+Alignment-1)) 
+                            & ~(Alignment-1));
+
+    // Speculate the new value of NextPtr.
+    char* NextPtrTmp = Result + AllocSize;
+    
+    // If we are still within the current region, return Result.
+    if (unsigned (NextPtrTmp - (char*) this) <= RegionSize) {
+      NextPtr = NextPtrTmp;
       return Result;
     }