commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] [gnuradio] 20/148: Created sbuff class to house chunks


From: git
Subject: [Commit-gnuradio] [gnuradio] 20/148: Created sbuff class to house chunks of memory, their length, and possible callback for freeing/cleanup. Switched ring and transport to make use of the sbuff on receive.
Date: Mon, 15 Aug 2016 00:47:20 +0000 (UTC)

This is an automated email from the git hooks/post-receive script.

nwest pushed a commit to annotated tag old_usrp_devel_udp
in repository gnuradio.

commit c2a861b88249fe6dfb8f6b50b956962bb03e09ac
Author: Josh Blum <address@hidden>
Date:   Thu Nov 5 12:42:53 2009 -0800

    Created sbuff class to house chunks of memory, their length, and possible 
callback for freeing/cleanup.
    Switched ring and transport to make use of the sbuff on receive.
    
    Onto the data transport...
---
 usrp2/host/lib/Makefile.am           |  1 +
 usrp2/host/lib/eth_ctrl_transport.cc |  9 ++---
 usrp2/host/lib/eth_ctrl_transport.h  |  2 +-
 usrp2/host/lib/eth_data_transport.cc | 20 +++++++----
 usrp2/host/lib/eth_data_transport.h  |  2 +-
 usrp2/host/lib/ring.cc               | 16 ++++-----
 usrp2/host/lib/ring.h                | 17 +++------
 usrp2/host/lib/sbuff.h               | 70 ++++++++++++++++++++++++++++++++++++
 usrp2/host/lib/transport.cc          | 12 +++----
 usrp2/host/lib/transport.h           | 10 +++---
 usrp2/host/lib/usrp2_impl.cc         | 29 ++++++++-------
 usrp2/host/lib/usrp2_impl.h          |  2 +-
 12 files changed, 126 insertions(+), 64 deletions(-)

diff --git a/usrp2/host/lib/Makefile.am b/usrp2/host/lib/Makefile.am
index 5567192..9d65257 100644
--- a/usrp2/host/lib/Makefile.am
+++ b/usrp2/host/lib/Makefile.am
@@ -65,6 +65,7 @@ noinst_HEADERS = \
        open_usrp2_socket.h \
        pktfilter.h \
        ring.h \
+       sbuff.h \
        transport.h \
        usrp2_bytesex.h \
        usrp2_impl.h
diff --git a/usrp2/host/lib/eth_ctrl_transport.cc 
b/usrp2/host/lib/eth_ctrl_transport.cc
index 4d067d9..4efe840 100644
--- a/usrp2/host/lib/eth_ctrl_transport.cc
+++ b/usrp2/host/lib/eth_ctrl_transport.cc
@@ -76,13 +76,14 @@ int usrp2::eth_ctrl_transport::sendv(const iovec *iov, 
size_t iovlen){
     return d_eth_ctrl->write_packetv(all_iov, all_iov_len);
 }
 
-int usrp2::eth_ctrl_transport::recv(void **buff){
+usrp2::sbuff::sptr usrp2::eth_ctrl_transport::recv(){
     int recv_len = d_eth_ctrl->read_packet_dont_block(d_buff, sizeof(d_buff));
     //strip the ethernet headers from the buffer
     if (recv_len > sizeof(u2_eth_packet_only_t)){
-        *buff = d_buff + sizeof(u2_eth_packet_only_t);
-        return recv_len - sizeof(u2_eth_packet_only_t);
+        return sbuff::make(
+            d_buff + sizeof(u2_eth_packet_only_t),
+            recv_len - sizeof(u2_eth_packet_only_t));
     }
     boost::this_thread::sleep(gruel::get_new_timeout(0.05)); //50ms timeout
-    return 0; //nothing yet
+    return sbuff::make(); //nothing yet
 }
diff --git a/usrp2/host/lib/eth_ctrl_transport.h 
b/usrp2/host/lib/eth_ctrl_transport.h
index 2a784e3..4e67046 100644
--- a/usrp2/host/lib/eth_ctrl_transport.h
+++ b/usrp2/host/lib/eth_ctrl_transport.h
@@ -37,7 +37,7 @@ namespace usrp2{
         eth_ctrl_transport(const std::string &ifc, u2_mac_addr_t mac);
         ~eth_ctrl_transport();
         int sendv(const iovec *iov, size_t iovlen);
-        int recv(void **buff);
+        sbuff::sptr recv();
 };
 
 
diff --git a/usrp2/host/lib/eth_data_transport.cc 
b/usrp2/host/lib/eth_data_transport.cc
index d1185cf..7760430 100644
--- a/usrp2/host/lib/eth_data_transport.cc
+++ b/usrp2/host/lib/eth_data_transport.cc
@@ -78,7 +78,7 @@ int usrp2::eth_data_transport::sendv(const iovec *iov, size_t 
iovlen){
     return d_eth_data->tx_framev(all_iov, all_iov_len);
 }
 
-int usrp2::eth_data_transport::recv(void **buff){
+usrp2::sbuff::sptr usrp2::eth_data_transport::recv(){
     void *base;
 
     DEBUG_LOG(":");
@@ -86,8 +86,10 @@ int usrp2::eth_data_transport::recv(void **buff){
     // process control frames, enqueue data packets in channel
     // rings, and signal blocked API threads
     int len = d_eth_data->rx_frame(&base, 100); // FIXME magic timeout
-    
-    u2_eth_samples_t *pkt = (u2_eth_samples_t *)base;
+
+    if (len <= 0) return sbuff::make();
+
+    u2_eth_packet_only_t *hdr = (u2_eth_packet_only_t *)base;
     d_num_rx_frames++;
     d_num_rx_bytes += len;
     
@@ -95,7 +97,7 @@ int usrp2::eth_data_transport::recv(void **buff){
 
     if (d_rx_seqno != -1) {
       int expected_seqno = (d_rx_seqno + 1) & 0xFF;
-      int seqno = pkt->hdrs.thdr.seqno; 
+      int seqno = hdr->thdr.seqno; 
       
       if (seqno != expected_seqno) {
         DEBUG_LOG("S"); // missing sequence number
@@ -107,9 +109,13 @@ int usrp2::eth_data_transport::recv(void **buff){
       }
     }
 
-    d_rx_seqno = pkt->hdrs.thdr.seqno;
+    d_rx_seqno = hdr->thdr.seqno;
 
     /* --- end of fake transport layer handler --- */
-    
-    return 0;
+
+    //drop the ethernet and transport headers
+    return sbuff::make(
+        (uint8_t*)base + sizeof(u2_eth_packet_only_t),
+        len - sizeof(u2_eth_packet_only_t),
+        boost::bind(&eth_buffer::release_frame, d_eth_data, base));
 }
diff --git a/usrp2/host/lib/eth_data_transport.h 
b/usrp2/host/lib/eth_data_transport.h
index 9780a43..cf44896 100644
--- a/usrp2/host/lib/eth_data_transport.h
+++ b/usrp2/host/lib/eth_data_transport.h
@@ -44,7 +44,7 @@ namespace usrp2{
         eth_data_transport(const std::string &ifc, u2_mac_addr_t mac, size_t 
rx_bufsize);
         ~eth_data_transport();
         int sendv(const iovec *iov, size_t iovlen);
-        int recv(void **buff);
+        sbuff::sptr recv();
         void init();
 };
 
diff --git a/usrp2/host/lib/ring.cc b/usrp2/host/lib/ring.cc
index 9d06fc5..334ddb4 100644
--- a/usrp2/host/lib/ring.cc
+++ b/usrp2/host/lib/ring.cc
@@ -32,8 +32,7 @@ namespace usrp2 {
       d_mutex(), d_not_empty()
   {
     for (unsigned int i = 0; i < entries; i++) {
-      d_ring[i].d_base = 0;
-      d_ring[i].d_len = 0;
+      d_ring[i] = sbuff::make(); //load empty sbuff
     }
   }
 
@@ -46,15 +45,13 @@ namespace usrp2 {
   }
 
   bool
-  ring::enqueue(void *p, size_t len, cb_t cb)
+  ring::enqueue(sbuff::sptr sb)
   {
     gruel::scoped_lock l(d_mutex);
     if (full())
       return false;
       
-    d_ring[d_write_ind].d_len = len;
-    d_ring[d_write_ind].d_base = p;
-    d_ring[d_write_ind].d_cb = cb;
+    d_ring[d_write_ind] = sb;
 
     inc_write_ind();
     d_not_empty.notify_one();
@@ -62,15 +59,14 @@ namespace usrp2 {
   }
 
   bool
-  ring::dequeue(void **p, size_t *len, cb_t *cb)
+  ring::dequeue(sbuff::sptr *sb)
   {
     gruel::scoped_lock l(d_mutex);
     if (empty())
       return false;
       
-    *p   = d_ring[d_read_ind].d_base;
-    *len = d_ring[d_read_ind].d_len;
-    *cb = d_ring[d_read_ind].d_cb;
+    *sb = d_ring[d_read_ind];
+    d_ring[d_read_ind] = sbuff::make(); //replace it with an empty sbuff
 
     inc_read_ind();
     return true;
diff --git a/usrp2/host/lib/ring.h b/usrp2/host/lib/ring.h
index 5bd78c2..2111134 100644
--- a/usrp2/host/lib/ring.h
+++ b/usrp2/host/lib/ring.h
@@ -25,6 +25,7 @@
 #include <vector>
 #include <boost/shared_ptr.hpp>
 #include <gruel/thread.h>
+#include "sbuff.h"
 
 namespace usrp2 {
 
@@ -33,23 +34,13 @@ namespace usrp2 {
 
   class ring
   {
-  public:
-    //typedef for void no argument function
-    typedef boost::function<void()> cb_t;
-
   private:
  
     size_t d_max;
     size_t d_read_ind;
     size_t d_write_ind;
 
-    struct ring_desc
-    {
-      void *d_base;
-      size_t d_len;
-      cb_t d_cb;
-    };
-    std::vector<ring_desc> d_ring;
+    std::vector<sbuff::sptr> d_ring;
 
     gruel::mutex d_mutex;
     gruel::condition_variable d_not_empty;
@@ -79,8 +70,8 @@ namespace usrp2 {
 
     void wait_for_not_empty();
 
-    bool enqueue(void *p, size_t len, cb_t cb);
-    bool dequeue(void **p, size_t *len, cb_t *cb);
+    bool enqueue(sbuff::sptr sb);
+    bool dequeue(sbuff::sptr *sb);
   };
 
 }  // namespace usrp2
diff --git a/usrp2/host/lib/sbuff.h b/usrp2/host/lib/sbuff.h
new file mode 100644
index 0000000..0b95bee
--- /dev/null
+++ b/usrp2/host/lib/sbuff.h
@@ -0,0 +1,70 @@
+/* -*- c++ -*- */
+/*
+ * Copyright 2009 Free Software Foundation, Inc.
+ * 
+ * This file is part of GNU Radio
+ * 
+ * GNU Radio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3, or (at your option)
+ * any later version.
+ * 
+ * GNU Radio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef INCLUDED_USRP2_SBUFF_H
+#define INCLUDED_USRP2_SBUFF_H
+
+#include <boost/bind.hpp>
+#include <boost/shared_ptr.hpp>
+#include <cstdio>
+
+namespace usrp2 {
+
+    /*******************************************************************
+     * This smart buffer class holds a buffer and its length in bytes
+     * A special callback can be passed into the sbuff as well.
+     * The callback (if set) will be called on deconstruction.
+     * 
+     * Typically, the callback will free the memory held by the buffer.
+     * But this is all up to the creator of the sbuff.
+    *******************************************************************/
+    class sbuff{
+    public:
+        //typedef for void no argument function
+        typedef boost::function<void()> cb_t;
+        typedef boost::shared_ptr<sbuff> sptr;
+    private:
+        void *d_buff;
+        size_t d_len;
+        cb_t d_cb;
+    public:
+        static sptr make(void *buff, size_t len, cb_t cb){
+            return sptr(new sbuff(buff, len, cb));
+        }
+        static sptr make(void *buff, size_t len){
+            return sptr(new sbuff(buff, len, NULL));
+        }
+        static sptr make(){
+            return sptr(new sbuff(NULL, 0, NULL));
+        }
+        sbuff(void *buff, size_t len, cb_t cb)
+         : d_buff(buff), d_len(len), d_cb(cb){}
+        ~sbuff(){if (d_cb) d_cb();}
+        //access methods
+        void *buff(){return d_buff;}
+        size_t len(){return d_len;}
+
+    };
+
+}  // namespace usrp2
+
+
+#endif /* INCLUDED_USRP2_SBUFF_H */
diff --git a/usrp2/host/lib/transport.cc b/usrp2/host/lib/transport.cc
index 25ca99b..9b7c0b2 100644
--- a/usrp2/host/lib/transport.cc
+++ b/usrp2/host/lib/transport.cc
@@ -59,13 +59,12 @@ void usrp2::transport::stop(){
 
 void usrp2::transport::run(){
     init();
-    void *buff;
     while (d_running){
         try{
-            // call recv to get a pointer into memory
+            // call recv to get a new sbuffer
             // pass the buffer into the callback
-            int len = recv(&buff);
-            if (len > 0) d_cb(buff, len);
+            usrp2::sbuff::sptr sb = recv();
+            if (sb->len()) d_cb(sb);
         //catch thread interrupts from join, sleep, etc
         //the running condition will be re-checked
         }catch(boost::thread_interrupted const &){}
@@ -76,7 +75,6 @@ int usrp2::transport::sendv(const iovec *iov, size_t iovlen){
     return -1; //NOP
 }
 
-int usrp2::transport::recv(void **buff){
-    *buff = NULL;
-    return -1; //NOP
+usrp2::sbuff::sptr usrp2::transport::recv(){
+    return usrp2::sbuff::make(); //NOP
 }
diff --git a/usrp2/host/lib/transport.h b/usrp2/host/lib/transport.h
index 113a0a9..bddd9ff 100644
--- a/usrp2/host/lib/transport.h
+++ b/usrp2/host/lib/transport.h
@@ -22,12 +22,13 @@
 #include <boost/thread.hpp>
 #include <cstring>
 #include <sys/uio.h>
+#include "sbuff.h"
 
 namespace usrp2 {
 
   class transport {
   public:
-    typedef boost::function<void(void*, size_t)> callback_t;
+    typedef boost::function<void(sbuff::sptr)> callback_t;
     typedef boost::shared_ptr<transport> sptr;
   private:
     std::string              d_type_str;
@@ -69,11 +70,10 @@ namespace usrp2 {
      */
     virtual int sendv(const iovec *iov, size_t iovlen);
     /*!
-     * \brief receive data into the buffer (override in a subclass)
-     * \param buff a pointer to a pointer into memory
-     * \return the number of bytes received, -1 for error
+     * \brief receive data into the sbuffer (override in a subclass)
+     * \return a new sbuff, for now, an empty sbuff means nothing was recvd
      */
-    virtual int recv(void **buff);
+    virtual sbuff::sptr recv();
   };
   
 } // namespace usrp2
diff --git a/usrp2/host/lib/usrp2_impl.cc b/usrp2/host/lib/usrp2_impl.cc
index 075b331..cdeab7e 100644
--- a/usrp2/host/lib/usrp2_impl.cc
+++ b/usrp2/host/lib/usrp2_impl.cc
@@ -152,7 +152,7 @@ namespace usrp2 {
       d_data_running(false),
       d_ctrl_transport(ctrl_transport)
   {
-    
d_ctrl_transport->set_callback(boost::bind(&usrp2::impl::handle_control_packet, 
this, _1, _2));
+    
d_ctrl_transport->set_callback(boost::bind(&usrp2::impl::handle_control_packet, 
this, _1));
     d_ctrl_transport->start();
 
     if (!d_eth_data->open(ifc, htons(U2_DATA_ETHERTYPE)))
@@ -397,8 +397,11 @@ namespace usrp2 {
   }
 
   void
-  usrp2::impl::handle_control_packet(const void *base, size_t len)
+  usrp2::impl::handle_control_packet(sbuff::sptr sb)
   {
+    void *base = sb->buff();
+    size_t len = sb->len();
+    
     // point to beginning of payload (subpackets)
     unsigned char *p = (unsigned char *)base + sizeof(u2_fixed_hdr_t);
     
@@ -473,8 +476,8 @@ namespace usrp2 {
       
       size_t offset = offsetof(u2_eth_samples_t, hdrs.fixed);
       
-      ring::cb_t callback = boost::bind(&eth_buffer::release_frame, 
d_eth_data, (void*)base);
-      if (d_channel_rings[chan]->enqueue(&pkt->hdrs.fixed, len-offset, 
callback)) {
+      sbuff::cb_t callback = boost::bind(&eth_buffer::release_frame, 
d_eth_data, (void*)base);
+      if (d_channel_rings[chan]->enqueue(sbuff::make(&pkt->hdrs.fixed, 
len-offset, callback))) {
        inc_enqueued();
        DEBUG_LOG("+");
        return data_handler::KEEP;      // channel ring runner will mark frame 
done
@@ -738,19 +741,17 @@ namespace usrp2 {
     DEBUG_LOG("s");
     
     // Iterate through frames and present to user
-    void *p;
-    ring::cb_t callback;
-    size_t frame_len_in_bytes;
-    while (rp->dequeue(&p, &frame_len_in_bytes, &callback)) {
+    sbuff::sptr sb;
+    while (rp->dequeue(&sb)) {
       uint32_t        *items;                  // points to beginning of data 
items
       size_t           nitems_in_uint32s;
       rx_metadata      md;
-      if (!parse_rx_metadata(p, frame_len_in_bytes, &items, 
&nitems_in_uint32s, &md))
+      if (!parse_rx_metadata(sb->buff(), sb->len(), &items, 
&nitems_in_uint32s, &md))
        return false;
 
       bool want_more = (*handler)(items, nitems_in_uint32s, &md);
       DEBUG_LOG("-");
-      callback();
+      sb.reset(); //reset shared ptr so sbuff decontructs
       dec_enqueued();
 
       if (!want_more)
@@ -780,11 +781,9 @@ namespace usrp2 {
     }
 
     // Iterate through frames and drop them
-    void *p;
-    ring::cb_t callback;
-    size_t frame_len_in_bytes;
-    while (rp->dequeue(&p, &frame_len_in_bytes, &callback)) {
-      callback();
+    sbuff::sptr sb;
+    while (rp->dequeue(&sb)) {
+      sb.reset(); //reset shared ptr so sbuff decontructs
       dec_enqueued();
     }
     return true;
diff --git a/usrp2/host/lib/usrp2_impl.h b/usrp2/host/lib/usrp2_impl.h
index 25c7c60..ac26256 100644
--- a/usrp2/host/lib/usrp2_impl.h
+++ b/usrp2/host/lib/usrp2_impl.h
@@ -113,7 +113,7 @@ namespace usrp2 {
     bool transmit_cmd_and_wait(void *cmd, size_t len, pending_reply *p, double 
secs=0.0);
     bool transmit_cmd(void *cmd, size_t len);
     virtual data_handler::result operator()(const void *base, size_t len);
-    void handle_control_packet(const void *base, size_t len);
+    void handle_control_packet(sbuff::sptr sb);
     data_handler::result handle_data_packet(const void *base, size_t len);
     bool dboard_info();
     bool reset_db();



reply via email to

[Prev in Thread] Current Thread [Next in Thread]