From 9d1423b9506c89a51a10b6119d01ce9a82a13b0c Mon Sep 17 00:00:00 2001
From: eb <eb@221aa14e-8319-0410-a670-987f0aec2ac5>
Date: Wed, 30 Apr 2008 03:52:31 +0000
Subject: Merged features/inband-usb -r6431:8293 into trunk.

git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@8295 221aa14e-8319-0410-a670-987f0aec2ac5
---
 pmt/src/lib/pmt.cc          |  6 ++++++
 pmt/src/lib/pmt.h           |  5 +++++
 pmt/src/lib/pmt_pool.cc     | 17 +++++++++++++++--
 pmt/src/lib/pmt_pool.h      |  8 +++++++-
 pmt/src/lib/qa_pmt_prims.cc | 14 ++++++++++++++
 pmt/src/lib/qa_pmt_prims.h  |  2 ++
 6 files changed, 49 insertions(+), 3 deletions(-)

(limited to 'pmt/src/lib')

diff --git a/pmt/src/lib/pmt.cc b/pmt/src/lib/pmt.cc
index a141224b30..537b7a05a0 100644
--- a/pmt/src/lib/pmt.cc
+++ b/pmt/src/lib/pmt.cc
@@ -962,6 +962,12 @@ pmt_list6(pmt_t x1, pmt_t x2, pmt_t x3, pmt_t x4, pmt_t x5, pmt_t x6)
   return pmt_cons(x1, pmt_cons(x2, pmt_cons(x3, pmt_cons(x4, pmt_cons(x5, pmt_cons(x6, PMT_NIL))))));
 }
 
+pmt_t
+pmt_list_add(pmt_t list, pmt_t item)
+{
+  return pmt_reverse(pmt_cons(item, pmt_reverse(list)));
+}
+
 pmt_t
 pmt_caar(pmt_t pair)
 {
diff --git a/pmt/src/lib/pmt.h b/pmt/src/lib/pmt.h
index fa368a6a17..970dd6c7d9 100644
--- a/pmt/src/lib/pmt.h
+++ b/pmt/src/lib/pmt.h
@@ -609,6 +609,11 @@ pmt_t pmt_list5(pmt_t x1, pmt_t x2, pmt_t x3, pmt_t x4, pmt_t x5);
  */
 pmt_t pmt_list6(pmt_t x1, pmt_t x2, pmt_t x3, pmt_t x4, pmt_t x5, pmt_t x6);
 
+/*!
+ * \brief Return \p list with \p item added to it.
+ */
+pmt_t pmt_list_add(pmt_t list, pmt_t item);
+
 
 /*
  * ------------------------------------------------------------------------
diff --git a/pmt/src/lib/pmt_pool.cc b/pmt/src/lib/pmt_pool.cc
index 8f1329a7ea..05d9c005bc 100644
--- a/pmt/src/lib/pmt_pool.cc
+++ b/pmt/src/lib/pmt_pool.cc
@@ -32,10 +32,13 @@ ROUNDUP(size_t x, size_t stride)
   return ((((x) + (stride) - 1)/(stride)) * (stride));
 }
 
-pmt_pool::pmt_pool(size_t itemsize, size_t alignment, size_t allocation_size)
-  : d_itemsize(ROUNDUP(itemsize, alignment)),
+pmt_pool::pmt_pool(size_t itemsize, size_t alignment,
+		   size_t allocation_size, size_t max_items)
+  : d_cond(&d_mutex),
+    d_itemsize(ROUNDUP(itemsize, alignment)),
     d_alignment(alignment),
     d_allocation_size(std::max(allocation_size, 16 * itemsize)),
+    d_max_items(max_items), d_n_items(0),
     d_freelist(0)
 {
 }
@@ -53,9 +56,15 @@ pmt_pool::malloc()
   omni_mutex_lock l(d_mutex);
   item *p;
 
+  if (d_max_items != 0){
+    while (d_n_items >= d_max_items)
+      d_cond.wait();
+  }
+
   if (d_freelist){	// got something?
     p = d_freelist;
     d_freelist = p->d_next;
+    d_n_items++;
     return p;
   }
 
@@ -79,6 +88,7 @@ pmt_pool::malloc()
   // now return the first one
   p = d_freelist;
   d_freelist = p->d_next;
+  d_n_items++;
   return p;
 }
 
@@ -93,4 +103,7 @@ pmt_pool::free(void *foo)
   item *p = (item *) foo;
   p->d_next = d_freelist;
   d_freelist = p;
+  d_n_items--;
+  if (d_max_items != 0)
+    d_cond.signal();
 }
diff --git a/pmt/src/lib/pmt_pool.h b/pmt/src/lib/pmt_pool.h
index ac0c07bb61..42276a14f0 100644
--- a/pmt/src/lib/pmt_pool.h
+++ b/pmt/src/lib/pmt_pool.h
@@ -38,10 +38,13 @@ class pmt_pool {
   };
   
   omni_mutex	      d_mutex;
+  omni_condition      d_cond;
   
   size_t	      d_itemsize;
   size_t	      d_alignment;
   size_t	      d_allocation_size;
+  size_t	      d_max_items;
+  size_t	      d_n_items;
   item	       	     *d_freelist;
   std::vector<char *> d_allocations;
 
@@ -50,8 +53,11 @@ public:
    * \param itemsize size in bytes of the items to be allocated.
    * \param alignment alignment in bytes of all objects to be allocated (must be power-of-2).
    * \param allocation_size number of bytes to allocate at a time from the underlying allocator.
+   * \param max_items is the maximum number of items to allocate.  If this number is exceeded,
+   *	      the allocate blocks.  0 implies no limit.
    */
-  pmt_pool(size_t itemsize, size_t alignment = 16, size_t allocation_size = 4096);
+  pmt_pool(size_t itemsize, size_t alignment = 16,
+	   size_t allocation_size = 4096, size_t max_items = 0);
   ~pmt_pool();
 
   void *malloc();
diff --git a/pmt/src/lib/qa_pmt_prims.cc b/pmt/src/lib/qa_pmt_prims.cc
index 26b3e26d38..57db4a1a93 100644
--- a/pmt/src/lib/qa_pmt_prims.cc
+++ b/pmt/src/lib/qa_pmt_prims.cc
@@ -301,6 +301,20 @@ qa_pmt_prims::test_io()
   CPPUNIT_ASSERT_EQUAL(std::string("k0"), pmt_write_string(k0));
 }
 
+void
+qa_pmt_prims::test_lists()
+{
+  pmt_t s0 = pmt_intern("s0");
+  pmt_t s1 = pmt_intern("s1");
+  pmt_t s2 = pmt_intern("s2");
+  pmt_t s3 = pmt_intern("s3");
+
+  pmt_t l1 = pmt_list4(s0, s1, s2, s3);
+  pmt_t l2 = pmt_list3(s0, s1, s2);
+  pmt_t l3 = pmt_list_add(l2, s3);
+  CPPUNIT_ASSERT(pmt_equal(l1, l3));
+}
+
 // ------------------------------------------------------------------------
 
 // class foo is used in test_any below.
diff --git a/pmt/src/lib/qa_pmt_prims.h b/pmt/src/lib/qa_pmt_prims.h
index be49a30e49..919fc2dca4 100644
--- a/pmt/src/lib/qa_pmt_prims.h
+++ b/pmt/src/lib/qa_pmt_prims.h
@@ -40,6 +40,7 @@ class qa_pmt_prims : public CppUnit::TestCase {
   CPPUNIT_TEST(test_dict);
   CPPUNIT_TEST(test_any);
   CPPUNIT_TEST(test_io);
+  CPPUNIT_TEST(test_lists);
   CPPUNIT_TEST(test_serialize);
   CPPUNIT_TEST_SUITE_END();
 
@@ -56,6 +57,7 @@ class qa_pmt_prims : public CppUnit::TestCase {
   void test_dict();
   void test_any();
   void test_io();
+  void test_lists();
   void test_serialize();
 };
 
-- 
cgit v1.2.3