From aa9e2ec7abd4f70f73ca9b83dc00c59cd7ba45d1 Mon Sep 17 00:00:00 2001
From: Andrej Rode <mail@andrejro.de>
Date: Mon, 25 Mar 2019 11:04:50 +0100
Subject: pmt: remove intrusive_ptr

---
 gnuradio-runtime/include/pmt/pmt.h               |  43 ++++++++--
 gnuradio-runtime/lib/pmt/pmt.cc                  |  97 +--------------------
 gnuradio-runtime/lib/pmt/pmt_int.h               |  45 ----------
 gnuradio-runtime/python/gnuradio/gr/tag_utils.py |  16 ++--
 gnuradio-runtime/swig/CMakeLists.txt             |   1 -
 gnuradio-runtime/swig/gr_intrusive_ptr.i         | 102 -----------------------
 gnuradio-runtime/swig/pmt_swig.i                 |  18 ++--
 7 files changed, 52 insertions(+), 270 deletions(-)
 delete mode 100644 gnuradio-runtime/swig/gr_intrusive_ptr.i

diff --git a/gnuradio-runtime/include/pmt/pmt.h b/gnuradio-runtime/include/pmt/pmt.h
index cb2191dd87..f7631f66d3 100644
--- a/gnuradio-runtime/include/pmt/pmt.h
+++ b/gnuradio-runtime/include/pmt/pmt.h
@@ -24,7 +24,7 @@
 #define INCLUDED_PMT_H
 
 #include <pmt/api.h>
-#include <boost/intrusive_ptr.hpp>
+#include <boost/noncopyable.hpp>
 #include <boost/shared_ptr.hpp>
 #include <boost/any.hpp>
 #include <complex>
@@ -53,16 +53,47 @@ namespace pmt {
 /*!
  * \brief base class of all pmt types
  */
-class pmt_base;
+class PMT_API pmt_base : boost::noncopyable {
+
+ public:
+  pmt_base(){};
+  virtual ~pmt_base();
+
+  virtual bool is_bool()    const { return false; }
+  virtual bool is_symbol()  const { return false; }
+  virtual bool is_number()  const { return false; }
+  virtual bool is_integer() const { return false; }
+  virtual bool is_uint64()  const { return false; }
+  virtual bool is_real()    const { return false; }
+  virtual bool is_complex() const { return false; }
+  virtual bool is_null()    const { return false; }
+  virtual bool is_pair()    const { return false; }
+  virtual bool is_tuple()   const { return false; }
+  virtual bool is_vector()  const { return false; }
+  virtual bool is_dict()    const { return false; }
+  virtual bool is_any()     const { return false; }
+
+  virtual bool is_uniform_vector() const { return false; }
+  virtual bool is_u8vector()  const { return false; }
+  virtual bool is_s8vector()  const { return false; }
+  virtual bool is_u16vector() const { return false; }
+  virtual bool is_s16vector() const { return false; }
+  virtual bool is_u32vector() const { return false; }
+  virtual bool is_s32vector() const { return false; }
+  virtual bool is_u64vector() const { return false; }
+  virtual bool is_s64vector() const { return false; }
+  virtual bool is_f32vector() const { return false; }
+  virtual bool is_f64vector() const { return false; }
+  virtual bool is_c32vector() const { return false; }
+  virtual bool is_c64vector() const { return false; }
+
+};
 
 /*!
  * \brief typedef for shared pointer (transparent reference counting).
  * See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
  */
-typedef boost::intrusive_ptr<pmt_base> pmt_t;
-
-extern PMT_API void intrusive_ptr_add_ref(pmt_base*);
-extern PMT_API void intrusive_ptr_release(pmt_base*);
+typedef boost::shared_ptr<pmt_base> pmt_t;
 
 class PMT_API exception : public std::logic_error
 {
diff --git a/gnuradio-runtime/lib/pmt/pmt.cc b/gnuradio-runtime/lib/pmt/pmt.cc
index 763007132f..e0f50b5b0b 100644
--- a/gnuradio-runtime/lib/pmt/pmt.cc
+++ b/gnuradio-runtime/lib/pmt/pmt.cc
@@ -34,47 +34,6 @@
 
 namespace pmt {
 
-# if (PMT_LOCAL_ALLOCATOR)
-
-static const int
-get_cache_line_size()
-{
-  static const int CACHE_LINE_SIZE = 64;		// good guess
-  return CACHE_LINE_SIZE;
-}
-
-static pmt_pool global_pmt_pool(sizeof(pmt_pair), get_cache_line_size());
-
-void *
-pmt_base::operator new(size_t size)
-{
-  void *p = global_pmt_pool.malloc();
-
-  // fprintf(stderr, "pmt_base::new p = %p\n", p);
-  assert((reinterpret_cast<intptr_t>(p) & (get_cache_line_size() - 1)) == 0);
-  return p;
-}
-
-void
-pmt_base::operator delete(void *p, size_t size)
-{
-  global_pmt_pool.free(p);
-}
-
-#endif
-
-void intrusive_ptr_add_ref(pmt_base* p)
-{
-  p->refcount_.fetch_add(1, boost::memory_order_relaxed);
-}
-
-void intrusive_ptr_release(pmt_base* p) {
-  if (p->refcount_.fetch_sub(1, boost::memory_order_release) == 1) {
-    boost::atomic_thread_fence(boost::memory_order_acquire);
-    delete p;
-  }
-}
- 
 pmt_base::~pmt_base()
 {
   // nop -- out of line virtual destructor
@@ -290,7 +249,7 @@ string_to_symbol(const std::string &name)
     if (name == _symbol(sym)->name())
       return sym;		// Yes.  Return it
   }
-  
+
   // Lock the table on insert for thread safety:
   static boost::mutex thread_safety;
   boost::mutex::scoped_lock lock(thread_safety);
@@ -300,7 +259,7 @@ string_to_symbol(const std::string &name)
     if (name == _symbol(sym)->name())
       return sym;		// Yes.  Return it
   }
-  
+
   // Nope.  Make a new one.
   pmt_t sym = pmt_t(new pmt_symbol(name));
   _symbol(sym)->set_next((*get_symbol_hash_table())[hash]);
@@ -1087,27 +1046,6 @@ eqv(const pmt_t& x, const pmt_t& y)
   return false;
 }
 
-bool
-eqv_raw(pmt_base *x, pmt_base *y)
-{
-  if (x == y)
-    return true;
-
-  if (x->is_integer() && y->is_integer())
-    return _integer(x)->value() == _integer(y)->value();
-
-  if (x->is_uint64() && y->is_uint64())
-    return _uint64(x)->value() == _uint64(y)->value();
-
-  if (x->is_real() && y->is_real())
-    return _real(x)->value() == _real(y)->value();
-
-  if (x->is_complex() && y->is_complex())
-    return _complex(x)->value() == _complex(y)->value();
-
-  return false;
-}
-
 bool
 equal(const pmt_t& x, const pmt_t& y)
 {
@@ -1213,35 +1151,6 @@ assq(pmt_t obj, pmt_t alist)
   return PMT_F;
 }
 
-/*
- * This avoids a bunch of shared_pointer reference count manipulation.
- */
-pmt_t
-assv_raw(pmt_base *obj, pmt_base *alist)
-{
-  while (alist->is_pair()){
-    pmt_base *p = ((pmt_pair *)alist)->d_car.get();
-    if (!p->is_pair())		// malformed alist
-      return PMT_F;
-
-    if (eqv_raw(obj, ((pmt_pair *)p)->d_car.get()))
-      return ((pmt_pair *)alist)->d_car;
-
-    alist = (((pmt_pair *)alist)->d_cdr).get();
-  }
-  return PMT_F;
-}
-
-#if 1
-
-pmt_t
-assv(pmt_t obj, pmt_t alist)
-{
-  return assv_raw(obj.get(), alist.get());
-}
-
-#else
-
 pmt_t
 assv(pmt_t obj, pmt_t alist)
 {
@@ -1258,8 +1167,6 @@ assv(pmt_t obj, pmt_t alist)
   return PMT_F;
 }
 
-#endif
-
 
 pmt_t
 assoc(pmt_t obj, pmt_t alist)
diff --git a/gnuradio-runtime/lib/pmt/pmt_int.h b/gnuradio-runtime/lib/pmt/pmt_int.h
index ead058598d..5f02518a62 100644
--- a/gnuradio-runtime/lib/pmt/pmt_int.h
+++ b/gnuradio-runtime/lib/pmt/pmt_int.h
@@ -33,53 +33,8 @@
  * See pmt.h for the public interface
  */
 
-#define PMT_LOCAL_ALLOCATOR 0		// define to 0 or 1
 namespace pmt {
 
-class PMT_API pmt_base : boost::noncopyable {
-  mutable boost::atomic<int> refcount_;
-
-protected:
-  pmt_base() : refcount_(0) {};
-  virtual ~pmt_base();
-
-public:
-  virtual bool is_bool()    const { return false; }
-  virtual bool is_symbol()  const { return false; }
-  virtual bool is_number()  const { return false; }
-  virtual bool is_integer() const { return false; }
-  virtual bool is_uint64()  const { return false; }
-  virtual bool is_real()    const { return false; }
-  virtual bool is_complex() const { return false; }
-  virtual bool is_null()    const { return false; }
-  virtual bool is_pair()    const { return false; }
-  virtual bool is_tuple()   const { return false; }
-  virtual bool is_vector()  const { return false; }
-  virtual bool is_dict()    const { return false; }
-  virtual bool is_any()     const { return false; }
-
-  virtual bool is_uniform_vector() const { return false; }
-  virtual bool is_u8vector()  const { return false; }
-  virtual bool is_s8vector()  const { return false; }
-  virtual bool is_u16vector() const { return false; }
-  virtual bool is_s16vector() const { return false; }
-  virtual bool is_u32vector() const { return false; }
-  virtual bool is_s32vector() const { return false; }
-  virtual bool is_u64vector() const { return false; }
-  virtual bool is_s64vector() const { return false; }
-  virtual bool is_f32vector() const { return false; }
-  virtual bool is_f64vector() const { return false; }
-  virtual bool is_c32vector() const { return false; }
-  virtual bool is_c64vector() const { return false; }
-
-  friend void intrusive_ptr_add_ref(pmt_base* p);
-  friend void intrusive_ptr_release(pmt_base* p);
-
-# if (PMT_LOCAL_ALLOCATOR)
-  void *operator new(size_t);
-  void operator delete(void *, size_t);
-#endif
-};
 
 class pmt_bool : public pmt_base
 {
diff --git a/gnuradio-runtime/python/gnuradio/gr/tag_utils.py b/gnuradio-runtime/python/gnuradio/gr/tag_utils.py
index d054865c22..cdb3350ed0 100644
--- a/gnuradio-runtime/python/gnuradio/gr/tag_utils.py
+++ b/gnuradio-runtime/python/gnuradio/gr/tag_utils.py
@@ -58,17 +58,17 @@ def python_to_tag(tag_struct):
                 good = True
 
         if('key' in tag_struct):
-            if(isinstance(tag_struct['key'], pmt.swig_int_ptr)):
+            if(isinstance(tag_struct['key'], pmt.swig_pmt_ptr)):
                 tag.key = tag_struct['key']
                 good = True
 
         if('value' in tag_struct):
-            if(isinstance(tag_struct['value'], pmt.swig_int_ptr)):
+            if(isinstance(tag_struct['value'], pmt.swig_pmt_ptr)):
                 tag.value = tag_struct['value']
                 good = True
 
         if('srcid' in tag_struct):
-            if(isinstance(tag_struct['srcid'], pmt.swig_int_ptr)):
+            if(isinstance(tag_struct['srcid'], pmt.swig_pmt_ptr)):
                 tag.srcid = tag_struct['srcid']
                 good = True
 
@@ -78,15 +78,15 @@ def python_to_tag(tag_struct):
                 tag.offset = tag_struct[0]
                 good = True
 
-            if(isinstance(tag_struct[1], pmt.swig_int_ptr)):
+            if(isinstance(tag_struct[1], pmt.swig_pmt_ptr)):
                 tag.key = tag_struct[1]
                 good = True
 
-            if(isinstance(tag_struct[2], pmt.swig_int_ptr)):
+            if(isinstance(tag_struct[2], pmt.swig_pmt_ptr)):
                 tag.value = tag_struct[2]
                 good = True
 
-            if(isinstance(tag_struct[3], pmt.swig_int_ptr)):
+            if(isinstance(tag_struct[3], pmt.swig_pmt_ptr)):
                 tag.srcid = tag_struct[3]
                 good = True
 
@@ -95,11 +95,11 @@ def python_to_tag(tag_struct):
                 tag.offset = tag_struct[0]
                 good = True
 
-            if(isinstance(tag_struct[1], pmt.swig_int_ptr)):
+            if(isinstance(tag_struct[1], pmt.swig_pmt_ptr)):
                 tag.key = tag_struct[1]
                 good = True
 
-            if(isinstance(tag_struct[2], pmt.swig_int_ptr)):
+            if(isinstance(tag_struct[2], pmt.swig_pmt_ptr)):
                 tag.value = tag_struct[2]
                 good = True
 
diff --git a/gnuradio-runtime/swig/CMakeLists.txt b/gnuradio-runtime/swig/CMakeLists.txt
index a6c11b5a26..b78735930d 100644
--- a/gnuradio-runtime/swig/CMakeLists.txt
+++ b/gnuradio-runtime/swig/CMakeLists.txt
@@ -90,7 +90,6 @@ install(
     feval.i
     gr_ctrlport.i
     gr_extras.i
-    gr_intrusive_ptr.i
     gr_logger.i
     gr_shared_ptr.i
     gr_swig_block_magic.i
diff --git a/gnuradio-runtime/swig/gr_intrusive_ptr.i b/gnuradio-runtime/swig/gr_intrusive_ptr.i
deleted file mode 100644
index 3b3c9242ab..0000000000
--- a/gnuradio-runtime/swig/gr_intrusive_ptr.i
+++ /dev/null
@@ -1,102 +0,0 @@
-// This file was borrowed from the SWIG project to allow use to
-// wrap PMTs that use intrusive pointers. This is only necessary
-// to support backwards compatibility with older distributions of
-// Linux that do not natively support a new enough version of SWIG.
-// We do this to prevent having to update our dependency on a new
-// SWIG. Eventually, the need for this should go away.
-
-// Allow for different namespaces for shared_ptr / intrusive_ptr - they could be boost or std or std::tr1
-// For example for std::tr1, use:
-// #define SWIG_SHARED_PTR_NAMESPACE std
-// #define SWIG_SHARED_PTR_SUBNAMESPACE tr1
-// #define SWIG_INTRUSIVE_PTR_NAMESPACE boost
-// #define SWIG_INTRUSIVE_PTR_SUBNAMESPACE
-
-#if !defined(SWIG_INTRUSIVE_PTR_NAMESPACE)
-# define SWIG_INTRUSIVE_PTR_NAMESPACE boost
-#endif
-
-#if defined(SWIG_INTRUSIVE_PTR_SUBNAMESPACE)
-# define SWIG_INTRUSIVE_PTR_QNAMESPACE SWIG_INTRUSIVE_PTR_NAMESPACE::SWIG_INTRUSIVE_PTR_SUBNAMESPACE
-#else
-# define SWIG_INTRUSIVE_PTR_QNAMESPACE SWIG_INTRUSIVE_PTR_NAMESPACE
-#endif
-
-namespace SWIG_INTRUSIVE_PTR_NAMESPACE {
-#if defined(SWIG_INTRUSIVE_PTR_SUBNAMESPACE)
-  namespace SWIG_INTRUSIVE_PTR_SUBNAMESPACE {
-#endif
-    template <class T> class intrusive_ptr {
-    };
-#if defined(SWIG_INTRUSIVE_PTR_SUBNAMESPACE)
-  }
-#endif
-}
-
-%fragment("SWIG_intrusive_deleter", "header") {
-template<class T> struct SWIG_intrusive_deleter {
-    void operator()(T *p) {
-        if (p)
-          intrusive_ptr_release(p);
-    }
-};
-}
-
-%fragment("SWIG_null_deleter", "header") {
-struct SWIG_null_deleter {
-  void operator() (void const *) const {
-  }
-};
-%#define SWIG_NO_NULL_DELETER_0 , SWIG_null_deleter()
-%#define SWIG_NO_NULL_DELETER_1
-}
-
-// Main user macro for defining intrusive_ptr typemaps for both const and non-const pointer types
-// For plain classes, do not use for derived classes
-%define SWIG_INTRUSIVE_PTR(PROXYCLASS, TYPE...)
-SWIG_INTRUSIVE_PTR_TYPEMAPS(PROXYCLASS, , TYPE)
-SWIG_INTRUSIVE_PTR_TYPEMAPS(PROXYCLASS, const, TYPE)
-%enddef
-
-// Main user macro for defining intrusive_ptr typemaps for both const and non-const pointer types
-// For derived classes
-%define SWIG_INTRUSIVE_PTR_DERIVED(PROXYCLASS, BASECLASSTYPE, TYPE...)
-SWIG_INTRUSIVE_PTR_TYPEMAPS(PROXYCLASS, , TYPE)
-SWIG_INTRUSIVE_PTR_TYPEMAPS(PROXYCLASS, const, TYPE)
-%types(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > = SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< BASECLASSTYPE >) %{
-  *newmemory = SWIG_CAST_NEW_MEMORY;
-  return (void *) new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< BASECLASSTYPE >(*(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *)$from);
-  %}
-%extend TYPE {
-  static SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< BASECLASSTYPE > SWIGSharedPtrUpcast(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > swigSharedPtrUpcast) {
-    return swigSharedPtrUpcast;
-  }
-}
-%enddef
-
-// Extra user macro for including classes in intrusive_ptr typemaps for both const and non-const pointer types
-// This caters for classes which cannot be wrapped by intrusive_ptrs but are still part of the class hierarchy
-// For plain classes, do not use for derived classes
-%define SWIG_INTRUSIVE_PTR_NO_WRAP(PROXYCLASS, TYPE...)
-SWIG_INTRUSIVE_PTR_TYPEMAPS_NO_WRAP(PROXYCLASS, , TYPE)
-SWIG_INTRUSIVE_PTR_TYPEMAPS_NO_WRAP(PROXYCLASS, const, TYPE)
-%enddef
-
-// Extra user macro for including classes in intrusive_ptr typemaps for both const and non-const pointer types
-// This caters for classes which cannot be wrapped by intrusive_ptrs but are still part of the class hierarchy
-// For derived classes
-%define SWIG_INTRUSIVE_PTR_DERIVED_NO_WRAP(PROXYCLASS, BASECLASSTYPE, TYPE...)
-SWIG_INTRUSIVE_PTR_TYPEMAPS_NO_WRAP(PROXYCLASS, , TYPE)
-SWIG_INTRUSIVE_PTR_TYPEMAPS_NO_WRAP(PROXYCLASS, const, TYPE)
-%types(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > = SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< BASECLASSTYPE >) %{
-  *newmemory = SWIG_CAST_NEW_MEMORY;
-  return (void *) new SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< BASECLASSTYPE >(*(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > *)$from);
-%}
-%extend TYPE {
-  static SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< BASECLASSTYPE > SWIGSharedPtrUpcast(SWIG_SHARED_PTR_QNAMESPACE::shared_ptr< TYPE > swigSharedPtrUpcast) {
-    return swigSharedPtrUpcast;
-  }
-}
-%enddef
-
-
diff --git a/gnuradio-runtime/swig/pmt_swig.i b/gnuradio-runtime/swig/pmt_swig.i
index f03ce1289a..c6ac444fba 100644
--- a/gnuradio-runtime/swig/pmt_swig.i
+++ b/gnuradio-runtime/swig/pmt_swig.i
@@ -30,7 +30,6 @@
 %}
 
 %{
-#include <boost/intrusive_ptr.hpp>
 #include <boost/shared_ptr.hpp>
 #include <boost/any.hpp>
 #include <complex>
@@ -68,6 +67,7 @@ namespace pmt {
 
 %include <std_complex.i>
 %include <std_vector.i>
+%include <gr_shared_ptr.i>
 %template(pmt_vector_int8) std::vector<int8_t>;
 %template(pmt_vector_uint8) std::vector<uint8_t>;
 %template(pmt_vector_int16) std::vector<int16_t>;
@@ -85,22 +85,15 @@ namespace pmt {
 // Language independent exception handler
 ////////////////////////////////////////////////////////////////////////
 
-// Template intrusive_ptr for Swig to avoid dereferencing issues
-namespace pmt{
-    class pmt_base;
-}
-//%import <intrusive_ptr.i>
-%import <gr_intrusive_ptr.i>
-%template(swig_int_ptr) boost::intrusive_ptr<pmt::pmt_base>;
+%template(swig_pmt_ptr) boost::shared_ptr<pmt::pmt_base>;
 
 namespace pmt{
+  class pmt_base;
+  typedef boost::shared_ptr<pmt::pmt_base> pmt_t;
 
-  typedef boost::intrusive_ptr<pmt_base> pmt_t;
-
-  // Allows Python to directly print a PMT object
   %pythoncode
   %{
-    swig_int_ptr.__repr__ = lambda self: write_string(self)
+    swig_pmt_ptr.__repr__ = lambda self: write_string(self)
   %}
 
   pmt_t get_PMT_NIL();
@@ -346,4 +339,3 @@ namespace pmt{
     return _deserialize_str_u8(tuple(x for x in pmt_str))
 
 %}
-
-- 
cgit v1.2.3