summaryrefslogtreecommitdiffstats
path: root/host
diff options
context:
space:
mode:
authorJosh Blum <josh@joshknows.com>2010-05-27 21:53:17 -0700
committerJosh Blum <josh@joshknows.com>2010-05-28 18:44:50 -0700
commit8c0759df03520f010203fdeb3979f82fc41b72f7 (patch)
treeaeff5b6d54d868baf5ff78d6bb8cc6d58099be90 /host
parent6665d7eb90264f12abdce86302fffe968879d94d (diff)
downloaduhd-8c0759df03520f010203fdeb3979f82fc41b72f7.tar.gz
uhd-8c0759df03520f010203fdeb3979f82fc41b72f7.tar.bz2
uhd-8c0759df03520f010203fdeb3979f82fc41b72f7.zip
work on alignment buffer, got unit test working
Diffstat (limited to 'host')
-rw-r--r--host/include/uhd/transport/alignment_buffer.hpp49
-rw-r--r--host/include/uhd/transport/bounded_buffer.hpp16
-rw-r--r--host/test/CMakeLists.txt2
-rw-r--r--host/test/buffer_test.cpp (renamed from host/test/bounded_buffer_test.cpp)22
4 files changed, 40 insertions, 49 deletions
diff --git a/host/include/uhd/transport/alignment_buffer.hpp b/host/include/uhd/transport/alignment_buffer.hpp
index b33b80da9..dc6ccc3ed 100644
--- a/host/include/uhd/transport/alignment_buffer.hpp
+++ b/host/include/uhd/transport/alignment_buffer.hpp
@@ -36,22 +36,12 @@ namespace uhd{ namespace transport{
typedef boost::shared_ptr<alignment_buffer<elem_type, seq_type> > sptr;
/*!
- * Create the alignment buffer.
+ * Make a new alignment buffer object.
* \param capacity the maximum elements per index
* \param width the number of elements to align
*/
- alignment_buffer(size_t capacity, size_t width){
- for (size_t i = 0; i < width; i++){
- _buffs.push_back(bounded_buffer_sptr(new bounded_buffer_type(capacity)));
- _all_indexes.push_back(i);
- }
- }
-
- /*!
- * Destroy this alignment buffer.
- */
- ~alignment_buffer(void){
- /* NOP */
+ static sptr make(size_t capacity, size_t width){
+ return sptr(new alignment_buffer(capacity, width));
}
/*!
@@ -80,29 +70,25 @@ namespace uhd{ namespace transport{
buff_contents_type buff_contents_tmp;
std::list<size_t> indexes_to_do(_all_indexes);
- //the seq identifier to align with
- seq_type expected_seq_id = seq_type();
- bool expected_seq_id_valid = false;
+ //do an initial pop to load an initial sequence id
+ size_t index = indexes_to_do.front();
+ if (not _buffs[index]->pop_with_timed_wait(buff_contents_tmp, time)) return false;
+ elems[index] = buff_contents_tmp.first;
+ seq_type expected_seq_id = buff_contents_tmp.second;
+ indexes_to_do.pop_front();
//get an aligned set of elements from the buffers:
while(indexes_to_do.size() != 0){
- size_t index = indexes_to_do.back();
-
//pop an element off for this index
+ index = indexes_to_do.front();
if (not _buffs[index]->pop_with_timed_wait(buff_contents_tmp, time)) return false;
- //grab the current sequence id if not valid
- if (not expected_seq_id_valid){
- expected_seq_id_valid = true;
- expected_seq_id = buff_contents_tmp.second;
- }
-
//if the sequence id matches:
// store the popped element into the output,
// remove this index from the list and continue
if (buff_contents_tmp.second == expected_seq_id){
elems[index] = buff_contents_tmp.first;
- indexes_to_do.pop_back();
+ indexes_to_do.pop_front();
continue;
}
@@ -113,10 +99,13 @@ namespace uhd{ namespace transport{
}
//if the sequence id is newer:
- // start from scratch at the new sequence number
+ // store the popped element into the output,
+ // add all other indexes back into the list
if (buff_contents_tmp.second > expected_seq_id){
+ elems[index] = buff_contents_tmp.first;
expected_seq_id = buff_contents_tmp.second;
indexes_to_do = _all_indexes;
+ indexes_to_do.remove(index);
continue;
}
}
@@ -130,6 +119,14 @@ namespace uhd{ namespace transport{
typedef boost::shared_ptr<bounded_buffer_type> bounded_buffer_sptr;
std::vector<bounded_buffer_sptr> _buffs;
std::list<size_t> _all_indexes;
+
+ //private constructor
+ alignment_buffer(size_t capacity, size_t width){
+ for (size_t i = 0; i < width; i++){
+ _buffs.push_back(bounded_buffer_type::make(capacity));
+ _all_indexes.push_back(i);
+ }
+ }
};
}} //namespace
diff --git a/host/include/uhd/transport/bounded_buffer.hpp b/host/include/uhd/transport/bounded_buffer.hpp
index c50a626fb..cdec05849 100644
--- a/host/include/uhd/transport/bounded_buffer.hpp
+++ b/host/include/uhd/transport/bounded_buffer.hpp
@@ -38,18 +38,11 @@ namespace uhd{ namespace transport{
typedef boost::shared_ptr<bounded_buffer<elem_type> > sptr;
/*!
- * Create a new bounded_buffer of a given size.
+ * Make a new bounded buffer object.
* \param capacity the bounded_buffer capacity
*/
- bounded_buffer(size_t capacity) : _buffer(capacity){
- /* NOP */
- }
-
- /*!
- * Destroy this bounded_buffer.
- */
- ~bounded_buffer(void){
- /* NOP */
+ static sptr make(size_t capacity){
+ return sptr(new bounded_buffer(capacity));
}
/*!
@@ -139,6 +132,9 @@ namespace uhd{ namespace transport{
bool not_full(void) const{return not _buffer.full();}
bool not_empty(void) const{return not _buffer.empty();}
+
+ //private constructor
+ bounded_buffer(size_t capacity) : _buffer(capacity){}
};
}} //namespace
diff --git a/host/test/CMakeLists.txt b/host/test/CMakeLists.txt
index c7c6d7fad..24778d13e 100644
--- a/host/test/CMakeLists.txt
+++ b/host/test/CMakeLists.txt
@@ -21,7 +21,7 @@
ADD_EXECUTABLE(main_test
main_test.cpp
addr_test.cpp
- bounded_buffer_test.cpp
+ buffer_test.cpp
dict_test.cpp
error_test.cpp
gain_handler_test.cpp
diff --git a/host/test/bounded_buffer_test.cpp b/host/test/buffer_test.cpp
index dba1a4258..aadb3f951 100644
--- a/host/test/bounded_buffer_test.cpp
+++ b/host/test/buffer_test.cpp
@@ -17,13 +17,16 @@
#include <boost/test/unit_test.hpp>
#include <uhd/transport/bounded_buffer.hpp>
+#include <uhd/transport/alignment_buffer.hpp>
+#include <boost/assign/list_of.hpp>
+using namespace boost::assign;
using namespace uhd::transport;
static const boost::posix_time::milliseconds timeout(10);
BOOST_AUTO_TEST_CASE(test_bounded_buffer_with_timed_wait){
- bounded_buffer<int>::sptr bb(new bounded_buffer<int>(3));
+ bounded_buffer<int>::sptr bb(bounded_buffer<int>::make(3));
//push elements, check for timeout
BOOST_CHECK(bb->push_with_timed_wait(0, timeout));
@@ -43,7 +46,7 @@ BOOST_AUTO_TEST_CASE(test_bounded_buffer_with_timed_wait){
}
BOOST_AUTO_TEST_CASE(test_bounded_buffer_with_pop_on_full){
- bounded_buffer<int>::sptr bb(new bounded_buffer<int>(3));
+ bounded_buffer<int>::sptr bb(bounded_buffer<int>::make(3));
//push elements, check for timeout
BOOST_CHECK(bb->push_with_pop_on_full(0));
@@ -61,13 +64,8 @@ BOOST_AUTO_TEST_CASE(test_bounded_buffer_with_pop_on_full){
BOOST_CHECK_EQUAL(val, 3);
}
-#include <uhd/transport/alignment_buffer.hpp>
-#include <boost/assign/list_of.hpp>
-
-using namespace boost::assign;
-
-BOOST_AUTO_TEST_CASE(test_alignment_buffer_tmp){
- alignment_buffer<int, size_t>::sptr ab(new alignment_buffer<int, size_t>(5, 3));
+BOOST_AUTO_TEST_CASE(test_alignment_buffer){
+ alignment_buffer<int, size_t>::sptr ab(alignment_buffer<int, size_t>::make(7, 3));
//load index 0 with all good seq numbers
BOOST_CHECK(ab->push_with_pop_on_full(0, 0, 0));
BOOST_CHECK(ab->push_with_pop_on_full(1, 1, 0));
@@ -92,14 +90,14 @@ BOOST_AUTO_TEST_CASE(test_alignment_buffer_tmp){
//readback aligned values
std::vector<int> aligned_elems(3);
- std::vector<int> expected_elems0 = list_of(0)(10)(20);
+ static const std::vector<int> expected_elems0 = list_of(0)(10)(20);
BOOST_CHECK(ab->pop_elems_with_timed_wait(aligned_elems, timeout));
BOOST_CHECK_EQUAL_COLLECTIONS(
aligned_elems.begin(), aligned_elems.end(),
expected_elems0.begin(), expected_elems0.end()
);
- std::vector<int> expected_elems1 = list_of(1)(11)(21);
+ static const std::vector<int> expected_elems1 = list_of(1)(11)(21);
BOOST_CHECK(ab->pop_elems_with_timed_wait(aligned_elems, timeout));
BOOST_CHECK_EQUAL_COLLECTIONS(
aligned_elems.begin(), aligned_elems.end(),
@@ -108,7 +106,7 @@ BOOST_AUTO_TEST_CASE(test_alignment_buffer_tmp){
//there was a skip now find 4
- std::vector<int> expected_elems4 = list_of(4)(14)(24);
+ static const std::vector<int> expected_elems4 = list_of(4)(14)(24);
BOOST_CHECK(ab->pop_elems_with_timed_wait(aligned_elems, timeout));
BOOST_CHECK_EQUAL_COLLECTIONS(
aligned_elems.begin(), aligned_elems.end(),