summaryrefslogtreecommitdiff
path: root/pmt/src
diff options
context:
space:
mode:
Diffstat (limited to 'pmt/src')
-rw-r--r--pmt/src/lib/pmt.cc6
-rw-r--r--pmt/src/lib/pmt.h5
-rw-r--r--pmt/src/lib/pmt_pool.cc17
-rw-r--r--pmt/src/lib/pmt_pool.h8
-rw-r--r--pmt/src/lib/qa_pmt_prims.cc14
-rw-r--r--pmt/src/lib/qa_pmt_prims.h2
6 files changed, 49 insertions, 3 deletions
diff --git a/pmt/src/lib/pmt.cc b/pmt/src/lib/pmt.cc
index a141224b3..537b7a05a 100644
--- a/pmt/src/lib/pmt.cc
+++ b/pmt/src/lib/pmt.cc
@@ -963,6 +963,12 @@ pmt_list6(pmt_t x1, pmt_t x2, pmt_t x3, pmt_t x4, pmt_t x5, pmt_t x6)
}
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)
{
return (pmt_car(pmt_car(pair)));
diff --git a/pmt/src/lib/pmt.h b/pmt/src/lib/pmt.h
index fa368a6a1..970dd6c7d 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 8f1329a7e..05d9c005b 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 ac0c07bb6..42276a14f 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 26b3e26d3..57db4a1a9 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 be49a30e4..919fc2dca 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();
};