GNU Radio 3.5.1 C++ API
gr_buffer.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2004,2009,2010,2011 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef INCLUDED_GR_BUFFER_H
00024 #define INCLUDED_GR_BUFFER_H
00025 
00026 #include <gr_core_api.h>
00027 #include <gr_runtime_types.h>
00028 #include <boost/weak_ptr.hpp>
00029 #include <gruel/thread.h>
00030 #include <gr_tags.h>
00031 #include <deque>
00032 
00033 class gr_vmcircbuf;
00034 
00035 /*!
00036  * \brief Allocate a buffer that holds at least \p nitems of size \p sizeof_item.
00037  *
00038  * The total size of the buffer will be rounded up to a system
00039  * dependent boundary.  This is typically the system page size, but
00040  * under MS windows is 64KB.
00041  *
00042  * \param nitems is the minimum number of items the buffer will hold.
00043  * \param sizeof_item is the size of an item in bytes.
00044  * \param link is the block that writes to this buffer.
00045  */
00046 GR_CORE_API gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item, gr_block_sptr link=gr_block_sptr());
00047 
00048 
00049 /*!
00050  * \brief Single writer, multiple reader fifo.
00051  * \ingroup internal
00052  */
00053 class GR_CORE_API gr_buffer {
00054  public:
00055 
00056   virtual ~gr_buffer ();
00057 
00058   /*!
00059    * \brief return number of items worth of space available for writing
00060    */
00061   int space_available ();
00062 
00063   /*!
00064    * \brief return size of this buffer in items
00065    */
00066   int bufsize() const { return d_bufsize; }
00067 
00068   /*!
00069    * \brief return pointer to write buffer.
00070    *
00071    * The return value points at space that can hold at least
00072    * space_available() items.
00073    */
00074   void *write_pointer ();
00075 
00076   /*!
00077    * \brief tell buffer that we wrote \p nitems into it
00078    */
00079   void update_write_pointer (int nitems);
00080 
00081   void set_done (bool done);
00082   bool done () const { return d_done; }
00083 
00084   /*!
00085    * \brief Return the block that writes to this buffer.
00086    */
00087   gr_block_sptr link() { return gr_block_sptr(d_link); }
00088 
00089   size_t nreaders() const { return d_readers.size(); }
00090   gr_buffer_reader* reader(size_t index) { return d_readers[index]; }
00091 
00092   gruel::mutex *mutex() { return &d_mutex; }
00093 
00094   uint64_t nitems_written() { return d_abs_write_offset; }
00095 
00096 
00097   /*!
00098    * \brief  Adds a new tag to the buffer.
00099    * 
00100    * \param tag        the new tag
00101    */
00102   void add_item_tag(const gr_tag_t &tag);
00103 
00104   /*!
00105    * \brief  Removes all tags before \p max_time from buffer
00106    * 
00107    * \param max_time        the time (item number) to trim up until.
00108    */
00109   void prune_tags(uint64_t max_time);
00110 
00111   std::deque<gr_tag_t>::iterator get_tags_begin() { return d_item_tags.begin(); }
00112   std::deque<gr_tag_t>::iterator get_tags_end() { return d_item_tags.end(); }
00113 
00114   // -------------------------------------------------------------------------
00115 
00116  private:
00117 
00118   friend class gr_buffer_reader;
00119   friend GR_CORE_API gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item, gr_block_sptr link);
00120   friend GR_CORE_API gr_buffer_reader_sptr gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload, gr_block_sptr link);
00121 
00122  protected:
00123   char                                 *d_base;         // base address of buffer
00124   unsigned int                          d_bufsize;      // in items
00125  private:
00126   gr_vmcircbuf                         *d_vmcircbuf;
00127   size_t                                d_sizeof_item;  // in bytes
00128   std::vector<gr_buffer_reader *>       d_readers;
00129   boost::weak_ptr<gr_block>             d_link;         // block that writes to this buffer
00130 
00131   //
00132   // The mutex protects d_write_index, d_abs_write_offset, d_done, d_item_tags 
00133   // and the d_read_index's and d_abs_read_offset's in the buffer readers.
00134   //
00135   gruel::mutex                          d_mutex;
00136   unsigned int                          d_write_index;  // in items [0,d_bufsize)
00137   uint64_t                              d_abs_write_offset; // num items written since the start
00138   bool                                  d_done;
00139   std::deque<gr_tag_t>                  d_item_tags;
00140   uint64_t                              d_last_min_items_read;
00141   
00142   unsigned
00143   index_add (unsigned a, unsigned b)
00144   {
00145     unsigned s = a + b;
00146 
00147     if (s >= d_bufsize)
00148       s -= d_bufsize;
00149 
00150     assert (s < d_bufsize);
00151     return s;
00152   }
00153 
00154   unsigned
00155   index_sub (unsigned a, unsigned b)
00156   {
00157     int s = a - b;
00158 
00159     if (s < 0)
00160       s += d_bufsize;
00161 
00162     assert ((unsigned) s < d_bufsize);
00163     return s;
00164   }
00165 
00166   virtual bool allocate_buffer (int nitems, size_t sizeof_item);
00167 
00168   /*!
00169    * \brief constructor is private.  Use gr_make_buffer to create instances.
00170    *
00171    * Allocate a buffer that holds at least \p nitems of size \p sizeof_item.
00172    *
00173    * \param nitems is the minimum number of items the buffer will hold.
00174    * \param sizeof_item is the size of an item in bytes.
00175    * \param link is the block that writes to this buffer.
00176    *
00177    * The total size of the buffer will be rounded up to a system
00178    * dependent boundary.  This is typically the system page size, but
00179    * under MS windows is 64KB.
00180    */
00181   gr_buffer (int nitems, size_t sizeof_item, gr_block_sptr link);
00182 
00183   /*!
00184    * \brief disassociate \p reader from this buffer
00185    */
00186   void drop_reader (gr_buffer_reader *reader);
00187 
00188 };
00189 
00190 /*!
00191  * \brief Create a new gr_buffer_reader and attach it to buffer \p buf
00192  * \param buf is the buffer the \p gr_buffer_reader reads from.
00193  * \param nzero_preload -- number of zero items to "preload" into buffer.
00194  * \param link is the block that reads from the buffer using this gr_buffer_reader.
00195  */
00196 GR_CORE_API gr_buffer_reader_sptr 
00197 gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload, gr_block_sptr link=gr_block_sptr());
00198 
00199 //! returns # of gr_buffers currently allocated
00200 GR_CORE_API long gr_buffer_ncurrently_allocated ();
00201 
00202 
00203 // ---------------------------------------------------------------------------
00204 
00205 /*!
00206  * \brief How we keep track of the readers of a gr_buffer.
00207  * \ingroup internal
00208  */
00209 
00210 class GR_CORE_API gr_buffer_reader {
00211  public:
00212 
00213   ~gr_buffer_reader ();
00214 
00215   /*!
00216    * \brief Return number of items available for reading.
00217    */
00218   int items_available () const;
00219 
00220   /*!
00221    * \brief Return buffer this reader reads from.
00222    */
00223   gr_buffer_sptr buffer () const { return d_buffer; }
00224 
00225 
00226   /*!
00227    * \brief Return maximum number of items that could ever be available for reading.
00228    * This is used as a sanity check in the scheduler to avoid looping forever.
00229    */
00230   int max_possible_items_available () const { return d_buffer->d_bufsize - 1; }
00231 
00232   /*!
00233    * \brief return pointer to read buffer.
00234    *
00235    * The return value points to items_available() number of items
00236    */
00237   const void *read_pointer ();
00238 
00239   /*
00240    * \brief tell buffer we read \p items from it
00241    */
00242   void update_read_pointer (int nitems);
00243 
00244   void set_done (bool done)   { d_buffer->set_done (done); }
00245   bool done () const { return d_buffer->done (); }
00246 
00247   gruel::mutex *mutex() { return d_buffer->mutex(); }
00248 
00249 
00250   uint64_t nitems_read() { return d_abs_read_offset; }
00251 
00252   /*!
00253    * \brief Return the block that reads via this reader.
00254    *
00255    */
00256   gr_block_sptr link() { return gr_block_sptr(d_link); }
00257 
00258 
00259   /*!
00260    * \brief Given a [start,end), returns a vector all tags in the range.
00261    *
00262    * Get a vector of tags in given range. Range of counts is from start to end-1.
00263    *
00264    * Tags are tuples of:
00265    *      (item count, source id, key, value)
00266    *
00267    * \param v            a vector reference to return tags into
00268    * \param abs_start    a uint64 count of the start of the range of interest
00269    * \param abs_end      a uint64 count of the end of the range of interest
00270    */
00271   void get_tags_in_range(std::vector<gr_tag_t> &v,
00272                          uint64_t abs_start,
00273                          uint64_t abs_end);
00274 
00275   // -------------------------------------------------------------------------
00276 
00277  private:
00278 
00279   friend class gr_buffer;
00280   friend GR_CORE_API gr_buffer_reader_sptr 
00281   gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload, gr_block_sptr link);
00282 
00283 
00284   gr_buffer_sptr                d_buffer;
00285   unsigned int                  d_read_index;   // in items [0,d->buffer.d_bufsize)
00286   uint64_t                      d_abs_read_offset;  // num items seen since the start
00287   boost::weak_ptr<gr_block>     d_link;         // block that reads via this buffer reader
00288 
00289   //! constructor is private.  Use gr_buffer::add_reader to create instances
00290   gr_buffer_reader (gr_buffer_sptr buffer, unsigned int read_index, gr_block_sptr link);
00291 };
00292 
00293 //! returns # of gr_buffer_readers currently allocated
00294 GR_CORE_API long gr_buffer_reader_ncurrently_allocated ();
00295 
00296 
00297 #endif /* INCLUDED_GR_BUFFER_H */