[cfe-commits] r97978 - in /cfe/trunk: include/clang/AST/RecordLayout.h lib/AST/ASTContext.cpp lib/AST/CMakeLists.txt lib/AST/RecordLayout.cpp lib/AST/RecordLayoutBuilder.cpp

Ted Kremenek kremenek at apple.com
Mon Mar 8 12:56:29 PST 2010


Author: kremenek
Date: Mon Mar  8 14:56:29 2010
New Revision: 97978

URL: http://llvm.org/viewvc/llvm-project?rev=97978&view=rev
Log:
Allocate ASTRecordLayout objects using the allocator associated with ASTContext.
This allows them to be allocated using a BumpPtrAllocated
in the common case.

Added:
    cfe/trunk/lib/AST/RecordLayout.cpp
Modified:
    cfe/trunk/include/clang/AST/RecordLayout.h
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/AST/CMakeLists.txt
    cfe/trunk/lib/AST/RecordLayoutBuilder.cpp

Modified: cfe/trunk/include/clang/AST/RecordLayout.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/RecordLayout.h?rev=97978&r1=97977&r2=97978&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/RecordLayout.h (original)
+++ cfe/trunk/include/clang/AST/RecordLayout.h Mon Mar  8 14:56:29 2010
@@ -128,47 +128,24 @@
   friend class ASTContext;
   friend class ASTRecordLayoutBuilder;
 
-  ASTRecordLayout(uint64_t size, unsigned alignment, unsigned datasize,
-                  const uint64_t *fieldoffsets, unsigned fieldcount)
-  : Size(size), DataSize(datasize), FieldOffsets(0), Alignment(alignment),
-    FieldCount(fieldcount), CXXInfo(0) {
-    if (FieldCount > 0)  {
-      FieldOffsets = new uint64_t[FieldCount];
-      for (unsigned i = 0; i < FieldCount; ++i)
-        FieldOffsets[i] = fieldoffsets[i];
-    }
-  }
+  ASTRecordLayout(ASTContext &Ctx, uint64_t size, unsigned alignment,
+                  unsigned datasize, const uint64_t *fieldoffsets,
+                  unsigned fieldcount);
 
   // Constructor for C++ records.
-  ASTRecordLayout(uint64_t size, unsigned alignment, uint64_t datasize,
+  ASTRecordLayout(ASTContext &Ctx,
+                  uint64_t size, unsigned alignment, uint64_t datasize,
                   const uint64_t *fieldoffsets, unsigned fieldcount,
                   uint64_t nonvirtualsize, unsigned nonvirtualalign,
                   const PrimaryBaseInfo &PrimaryBase,
                   const std::pair<const CXXRecordDecl *, uint64_t> *bases,
                   unsigned numbases,
                   const std::pair<const CXXRecordDecl *, uint64_t> *vbases,
-                  unsigned numvbases)
-  : Size(size), DataSize(datasize), FieldOffsets(0), Alignment(alignment),
-  FieldCount(fieldcount), CXXInfo(new CXXRecordLayoutInfo) {
-    if (FieldCount > 0)  {
-      FieldOffsets = new uint64_t[FieldCount];
-      for (unsigned i = 0; i < FieldCount; ++i)
-        FieldOffsets[i] = fieldoffsets[i];
-    }
-
-    CXXInfo->PrimaryBase = PrimaryBase;
-    CXXInfo->NonVirtualSize = nonvirtualsize;
-    CXXInfo->NonVirtualAlign = nonvirtualalign;
-    for (unsigned i = 0; i != numbases; ++i)
-      CXXInfo->BaseOffsets[bases[i].first] = bases[i].second;
-    for (unsigned i = 0; i != numvbases; ++i)
-      CXXInfo->VBaseOffsets[vbases[i].first] = vbases[i].second;
-  }
-
-  ~ASTRecordLayout() {
-    delete [] FieldOffsets;
-    delete CXXInfo;
-  }
+                  unsigned numvbases);
+
+  ~ASTRecordLayout() {}
+
+  void Destroy(ASTContext &Ctx);
 
   ASTRecordLayout(const ASTRecordLayout&);   // DO NOT IMPLEMENT
   void operator=(const ASTRecordLayout&); // DO NOT IMPLEMENT

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=97978&r1=97977&r2=97978&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Mon Mar  8 14:56:29 2010
@@ -78,21 +78,21 @@
       // Increment in loop to prevent using deallocated memory.
       Deallocate(&*I++);
     }
-  }
 
-  for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
-       I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
-    // Increment in loop to prevent using deallocated memory.
-    ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
-    delete R;
-  }
+    for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
+         I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
+      // Increment in loop to prevent using deallocated memory.
+      if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
+        R->Destroy(*this);
+    }
 
-  for (llvm::DenseMap<const ObjCContainerDecl*,
-                      const ASTRecordLayout*>::iterator
-       I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) {
-    // Increment in loop to prevent using deallocated memory.
-    ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
-    delete R;
+    for (llvm::DenseMap<const ObjCContainerDecl*,
+         const ASTRecordLayout*>::iterator
+         I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; ) {
+      // Increment in loop to prevent using deallocated memory.
+      if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
+        R->Destroy(*this);
+    }
   }
 
   // Destroy nested-name-specifiers.

Modified: cfe/trunk/lib/AST/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/CMakeLists.txt?rev=97978&r1=97977&r2=97978&view=diff
==============================================================================
--- cfe/trunk/lib/AST/CMakeLists.txt (original)
+++ cfe/trunk/lib/AST/CMakeLists.txt Mon Mar  8 14:56:29 2010
@@ -4,8 +4,8 @@
   APValue.cpp
   ASTConsumer.cpp
   ASTContext.cpp
-  ASTImporter.cpp
   ASTDiagnostic.cpp
+  ASTImporter.cpp
   AttrImpl.cpp
   CXXInheritance.cpp
   Decl.cpp
@@ -23,6 +23,7 @@
   InheritViz.cpp
   NestedNameSpecifier.cpp
   ParentMap.cpp
+  RecordLayout.cpp
   RecordLayoutBuilder.cpp
   Stmt.cpp
   StmtDumper.cpp

Added: cfe/trunk/lib/AST/RecordLayout.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/RecordLayout.cpp?rev=97978&view=auto
==============================================================================
--- cfe/trunk/lib/AST/RecordLayout.cpp (added)
+++ cfe/trunk/lib/AST/RecordLayout.cpp Mon Mar  8 14:56:29 2010
@@ -0,0 +1,69 @@
+//===-- RecordLayout.cpp - Layout information for a struct/union -*- C++ -*-==//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines the RecordLayout interface.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/RecordLayout.h"
+
+using namespace clang;
+
+void ASTRecordLayout::Destroy(ASTContext &Ctx) {
+  if (FieldOffsets)
+    Ctx.Deallocate(FieldOffsets);
+  if (CXXInfo)
+    Ctx.Deallocate(CXXInfo);
+  this->~ASTRecordLayout();
+  Ctx.Deallocate(this);
+}
+
+ASTRecordLayout::ASTRecordLayout(ASTContext &Ctx, uint64_t size, unsigned alignment,
+                unsigned datasize, const uint64_t *fieldoffsets,
+                unsigned fieldcount)
+  : Size(size), DataSize(datasize), FieldOffsets(0), Alignment(alignment),
+    FieldCount(fieldcount), CXXInfo(0) {
+  if (FieldCount > 0)  {
+    FieldOffsets = new (Ctx) uint64_t[FieldCount];
+    for (unsigned i = 0; i < FieldCount; ++i)
+      FieldOffsets[i] = fieldoffsets[i];
+  }
+}
+
+// Constructor for C++ records.
+ASTRecordLayout::ASTRecordLayout(ASTContext &Ctx,
+                       uint64_t size, unsigned alignment,
+                       uint64_t datasize,
+                       const uint64_t *fieldoffsets,
+                       unsigned fieldcount,
+                       uint64_t nonvirtualsize,
+                       unsigned nonvirtualalign,
+                       const PrimaryBaseInfo &PrimaryBase,
+                       const std::pair<const CXXRecordDecl *, uint64_t> *bases,
+                       unsigned numbases,
+                       const std::pair<const CXXRecordDecl *, uint64_t> *vbases,
+                       unsigned numvbases)
+  : Size(size), DataSize(datasize), FieldOffsets(0), Alignment(alignment),
+    FieldCount(fieldcount), CXXInfo(new (Ctx) CXXRecordLayoutInfo)
+{
+  if (FieldCount > 0)  {
+    FieldOffsets = new (Ctx) uint64_t[FieldCount];
+    for (unsigned i = 0; i < FieldCount; ++i)
+      FieldOffsets[i] = fieldoffsets[i];
+  }
+
+  CXXInfo->PrimaryBase = PrimaryBase;
+  CXXInfo->NonVirtualSize = nonvirtualsize;
+  CXXInfo->NonVirtualAlign = nonvirtualalign;
+  for (unsigned i = 0; i != numbases; ++i)
+    CXXInfo->BaseOffsets[bases[i].first] = bases[i].second;
+  for (unsigned i = 0; i != numvbases; ++i)
+    CXXInfo->VBaseOffsets[vbases[i].first] = vbases[i].second;
+}

Modified: cfe/trunk/lib/AST/RecordLayoutBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/RecordLayoutBuilder.cpp?rev=97978&r1=97977&r2=97978&view=diff
==============================================================================
--- cfe/trunk/lib/AST/RecordLayoutBuilder.cpp (original)
+++ cfe/trunk/lib/AST/RecordLayoutBuilder.cpp Mon Mar  8 14:56:29 2010
@@ -675,9 +675,10 @@
   Builder.Layout(D);
 
   if (!isa<CXXRecordDecl>(D))
-    return new ASTRecordLayout(Builder.Size, Builder.Alignment, Builder.Size,
-                               Builder.FieldOffsets.data(),
-                               Builder.FieldOffsets.size());
+    return new (Ctx) ASTRecordLayout(Ctx, Builder.Size, Builder.Alignment,
+                                     Builder.Size,
+                                     Builder.FieldOffsets.data(),
+                                     Builder.FieldOffsets.size());
 
   // FIXME: This is not always correct. See the part about bitfields at
   // http://www.codesourcery.com/public/cxx-abi/abi.html#POD for more info.
@@ -690,16 +691,16 @@
   uint64_t NonVirtualSize =
     IsPODForThePurposeOfLayout ? DataSize : Builder.NonVirtualSize;
 
-  return new ASTRecordLayout(Builder.Size, Builder.Alignment, DataSize,
-                             Builder.FieldOffsets.data(),
-                             Builder.FieldOffsets.size(),
-                             NonVirtualSize,
-                             Builder.NonVirtualAlignment,
-                             Builder.PrimaryBase,
-                             Builder.Bases.data(),
-                             Builder.Bases.size(),
-                             Builder.VBases.data(),
-                             Builder.VBases.size());
+  return new (Ctx) ASTRecordLayout(Ctx, Builder.Size, Builder.Alignment,
+                                   DataSize, Builder.FieldOffsets.data(),
+                                   Builder.FieldOffsets.size(),
+                                   NonVirtualSize,
+                                   Builder.NonVirtualAlignment,
+                                   Builder.PrimaryBase,
+                                   Builder.Bases.data(),
+                                   Builder.Bases.size(),
+                                   Builder.VBases.data(),
+                                   Builder.VBases.size());
 }
 
 const ASTRecordLayout *
@@ -710,10 +711,10 @@
 
   Builder.Layout(D, Impl);
 
-  return new ASTRecordLayout(Builder.Size, Builder.Alignment,
-                             Builder.DataSize,
-                             Builder.FieldOffsets.data(),
-                             Builder.FieldOffsets.size());
+  return new (Ctx) ASTRecordLayout(Ctx, Builder.Size, Builder.Alignment,
+                                   Builder.DataSize,
+                                   Builder.FieldOffsets.data(),
+                                   Builder.FieldOffsets.size());
 }
 
 const CXXMethodDecl *





More information about the cfe-commits mailing list