[Lldb-commits] [lldb] r118317 - /lldb/trunk/include/lldb/Utility/SharingPtr.h
Greg Clayton
gclayton at apple.com
Fri Nov 5 17:12:48 PDT 2010
Author: gclayton
Date: Fri Nov 5 19:12:48 2010
New Revision: 118317
URL: http://llvm.org/viewvc/llvm-project?rev=118317&view=rev
Log:
Howard Hinnant gave us changes for lldb_private::SharingPtr that gives us the ability have a single allocation contain both the class and the ref count without having to do intrusive pointer type stuff. They will intermingle correctly with other shared pointers as well. In order to take advantage of this you need to create your pointer in your class with the make_shared function:
lldb_private::SharingPtr<A> p = llvm::make_shared<A>(i, j);
Currently up to five constructor arguments are supported and each must be an LValue.
Modified:
lldb/trunk/include/lldb/Utility/SharingPtr.h
Modified: lldb/trunk/include/lldb/Utility/SharingPtr.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/SharingPtr.h?rev=118317&r1=118316&r2=118317&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/SharingPtr.h (original)
+++ lldb/trunk/include/lldb/Utility/SharingPtr.h Fri Nov 5 19:12:48 2010
@@ -11,7 +11,6 @@
#define utility_SharingPtr_h_
#include <algorithm>
-#include <memory>
namespace lldb_private {
@@ -57,6 +56,48 @@
delete data_;
}
+template <class T>
+class shared_ptr_emplace
+ : public shared_count
+{
+ T data_;
+public:
+
+ shared_ptr_emplace()
+ : data_() {}
+
+ template <class A0>
+ shared_ptr_emplace(A0& a0)
+ : data_(a0) {}
+
+ template <class A0, class A1>
+ shared_ptr_emplace(A0& a0, A1& a1)
+ : data_(a0, a1) {}
+
+ template <class A0, class A1, class A2>
+ shared_ptr_emplace(A0& a0, A1& a1, A2& a2)
+ : data_(a0, a1, a2) {}
+
+ template <class A0, class A1, class A2, class A3>
+ shared_ptr_emplace(A0& a0, A1& a1, A2& a2, A3& a3)
+ : data_(a0, a1, a2, a3) {}
+
+ template <class A0, class A1, class A2, class A3, class A4>
+ shared_ptr_emplace(A0& a0, A1& a1, A2& a2, A3& a3, A4& a4)
+ : data_(a0, a1, a2, a3, a4) {}
+
+private:
+ virtual void on_zero_shared();
+public:
+ T* get() {return &data_;}
+};
+
+template <class T>
+void
+shared_ptr_emplace<T>::on_zero_shared()
+{
+}
+
} // namespace
template<class T>
@@ -94,6 +135,23 @@
bool empty() const {return cntrl_ == 0;}
operator nat*() const {return (nat*)get();}
+ static SharingPtr<T> make_shared();
+
+ template<class A0>
+ static SharingPtr<T> make_shared(A0&);
+
+ template<class A0, class A1>
+ static SharingPtr<T> make_shared(A0&, A1&);
+
+ template<class A0, class A1, class A2>
+ static SharingPtr<T> make_shared(A0&, A1&, A2&);
+
+ template<class A0, class A1, class A2, class A3>
+ static SharingPtr<T> make_shared(A0&, A1&, A2&, A3&);
+
+ template<class A0, class A1, class A2, class A3, class A4>
+ static SharingPtr<T> make_shared(A0&, A1&, A2&, A3&, A4&);
+
private:
template <class U> friend class SharingPtr;
@@ -202,6 +260,126 @@
SharingPtr(p).swap(*this);
}
+template<class T>
+SharingPtr<T>
+SharingPtr<T>::make_shared()
+{
+ typedef imp::shared_ptr_emplace<T> CntrlBlk;
+ SharingPtr<T> r;
+ r.cntrl_ = new CntrlBlk();
+ r.ptr_ = r.cntrl_->get();
+ return r;
+}
+
+template<class T>
+template<class A0>
+SharingPtr<T>
+SharingPtr<T>::make_shared(A0& a0)
+{
+ typedef imp::shared_ptr_emplace<T> CntrlBlk;
+ SharingPtr<T> r;
+ r.cntrl_ = new CntrlBlk(a0);
+ r.ptr_ = static_cast<CntrlBlk*>(r.cntrl_)->get();
+ return r;
+}
+
+template<class T>
+template<class A0, class A1>
+SharingPtr<T>
+SharingPtr<T>::make_shared(A0& a0, A1& a1)
+{
+ typedef imp::shared_ptr_emplace<T> CntrlBlk;
+ SharingPtr<T> r;
+ r.cntrl_ = new CntrlBlk(a0, a1);
+ r.ptr_ = static_cast<CntrlBlk*>(r.cntrl_)->get();
+ return r;
+}
+
+template<class T>
+template<class A0, class A1, class A2>
+SharingPtr<T>
+SharingPtr<T>::make_shared(A0& a0, A1& a1, A2& a2)
+{
+ typedef imp::shared_ptr_emplace<T> CntrlBlk;
+ SharingPtr<T> r;
+ r.cntrl_ = new CntrlBlk(a0, a1, a2);
+ r.ptr_ = static_cast<CntrlBlk*>(r.cntrl_)->get();
+ return r;
+}
+
+template<class T>
+template<class A0, class A1, class A2, class A3>
+SharingPtr<T>
+SharingPtr<T>::make_shared(A0& a0, A1& a1, A2& a2, A3& a3)
+{
+ typedef imp::shared_ptr_emplace<T> CntrlBlk;
+ SharingPtr<T> r;
+ r.cntrl_ = new CntrlBlk(a0, a1, a2, a3);
+ r.ptr_ = static_cast<CntrlBlk*>(r.cntrl_)->get();
+ return r;
+}
+
+template<class T>
+template<class A0, class A1, class A2, class A3, class A4>
+SharingPtr<T>
+SharingPtr<T>::make_shared(A0& a0, A1& a1, A2& a2, A3& a3, A4& a4)
+{
+ typedef imp::shared_ptr_emplace<T> CntrlBlk;
+ SharingPtr<T> r;
+ r.cntrl_ = new CntrlBlk(a0, a1, a2, a3, a4);
+ r.ptr_ = static_cast<CntrlBlk*>(r.cntrl_)->get();
+ return r;
+}
+
+template<class T>
+inline
+SharingPtr<T>
+make_shared()
+{
+ return SharingPtr<T>::make_shared();
+}
+
+template<class T, class A0>
+inline
+SharingPtr<T>
+make_shared(A0& a0)
+{
+ return SharingPtr<T>::make_shared(a0);
+}
+
+template<class T, class A0, class A1>
+inline
+SharingPtr<T>
+make_shared(A0& a0, A1& a1)
+{
+ return SharingPtr<T>::make_shared(a0, a1);
+}
+
+template<class T, class A0, class A1, class A2>
+inline
+SharingPtr<T>
+make_shared(A0& a0, A1& a1, A2& a2)
+{
+ return SharingPtr<T>::make_shared(a0, a1, a2);
+}
+
+template<class T, class A0, class A1, class A2, class A3>
+inline
+SharingPtr<T>
+make_shared(A0& a0, A1& a1, A2& a2, A3& a3)
+{
+ return SharingPtr<T>::make_shared(a0, a1, a2, a3);
+}
+
+template<class T, class A0, class A1, class A2, class A3, class A4>
+inline
+SharingPtr<T>
+make_shared(A0& a0, A1& a1, A2& a2, A3& a3, A4& a4)
+{
+ return SharingPtr<T>::make_shared(a0, a1, a2, a3, a4);
+}
+
+
template<class T, class U>
inline
bool
More information about the lldb-commits
mailing list