The factories for ImutAVLTree/ImmutableSet/ImmutableMap now take an (optional)
authorTed Kremenek <kremenek@apple.com>
Mon, 11 Feb 2008 23:11:12 +0000 (23:11 +0000)
committerTed Kremenek <kremenek@apple.com>
Mon, 11 Feb 2008 23:11:12 +0000 (23:11 +0000)
BumpPtrAllocator argument to their constructors.  This BumpPtrAllocator
will be used to allocate trees.  If no BumpPtrAllocator is provided, one
is created (as before).

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@46975 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/ImmutableMap.h
include/llvm/ADT/ImmutableSet.h

index 6e309c0db8a965865170dbdb814fc4837fa4a43f..27148e6083d81a409f3722f62aa8f5ff782d90bf 100644 (file)
@@ -84,6 +84,9 @@ public:
   public:
     Factory() {}
     
+    Factory(BumpPtrAllocator& Alloc)
+      : F(Alloc) {}
+    
     ImmutableMap GetEmptyMap() { return ImmutableMap(F.GetEmptyTree()); }
     
     ImmutableMap Add(ImmutableMap Old, key_type_ref K, data_type_ref D) {
index c339e3d4f96b62a6fcfed0e0bd18d30d476d7e51..717ec98ba995697fed5fea49d236ff09457c569f 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "llvm/Support/Allocator.h"
 #include "llvm/ADT/FoldingSet.h"
+#include "llvm/Support/DataTypes.h"
 #include <cassert>
 
 namespace llvm {
@@ -334,15 +335,31 @@ class ImutAVLFactory {
   
   typedef FoldingSet<TreeTy> CacheTy;
   
-  CacheTy Cache;  
-  BumpPtrAllocator Allocator;    
+  CacheTy Cache;
+  uintptr_t Allocator;
+  
+  bool ownsAllocator() const {
+    return Allocator & 0x1 ? false : true;
+  }
+
+  BumpPtrAllocator& getAllocator() const { 
+    return *reinterpret_cast<BumpPtrAllocator*>(Allocator & ~0x1);
+  }
   
   //===--------------------------------------------------===//    
   // Public interface.
   //===--------------------------------------------------===//
   
 public:
-  ImutAVLFactory() {}
+  ImutAVLFactory()
+    : Allocator(reinterpret_cast<uintptr_t>(new BumpPtrAllocator())) {}
+  
+  ImutAVLFactory(BumpPtrAllocator& Alloc)
+    : Allocator(reinterpret_cast<uintptr_t>(&Alloc) | 0x1) {}
+  
+  ~ImutAVLFactory() {
+    if (ownsAllocator()) delete &getAllocator();
+  }
   
   TreeTy* Add(TreeTy* T, value_type_ref V) {
     T = Add_internal(V,T);
@@ -358,8 +375,6 @@ public:
   
   TreeTy* GetEmptyTree() const { return NULL; }
   
-  BumpPtrAllocator& getAllocator() { return Allocator; }
-  
   //===--------------------------------------------------===//    
   // A bunch of quick helper functions used for reasoning
   // about the properties of trees and their children.
@@ -450,7 +465,8 @@ private:
     // Create it.
 
     // Allocate the new tree node and insert it into the cache.
-    TreeTy* T = (TreeTy*) Allocator.Allocate<TreeTy>();    
+    BumpPtrAllocator& A = getAllocator();
+    TreeTy* T = (TreeTy*) A.Allocate<TreeTy>();
     new (T) TreeTy(L,R,V,IncrementHeight(L,R));
 
     // We do not insert 'T' into the FoldingSet here.  This is because
@@ -930,6 +946,9 @@ public:
   public:
     Factory() {}
     
+    Factory(BumpPtrAllocator& Alloc)
+      : F(Alloc) {}
+    
     /// GetEmptySet - Returns an immutable set that contains no elements.
     ImmutableSet GetEmptySet() { return ImmutableSet(F.GetEmptyTree()); }