Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

gr_buffer.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2004 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 2, 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., 59 Temple Place - Suite 330,
00020  * Boston, MA 02111-1307, USA.
00021  */
00022 
00023 #ifndef INCLUDED_GR_BUFFER_H
00024 #define INCLUDED_GR_BUFFER_H
00025 
00026 #include <gr_runtime.h>
00027 
00028 class gr_vmcircbuf;
00029 
00037 gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item);
00038 
00039 
00044 class gr_buffer {
00045  public:
00046   virtual ~gr_buffer ();
00047 
00051   int space_available () const;
00052 
00059   void *write_pointer ();
00060 
00064   void update_write_pointer (int nitems);
00065 
00066 
00067   void set_done (bool done)   { d_done = done; }
00068   bool done () const { return d_done; }
00069 
00070   // -------------------------------------------------------------------------
00071 
00072  private:
00073 
00074   friend class gr_buffer_reader;
00075   friend gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item);
00076   friend gr_buffer_reader_sptr gr_buffer_add_reader (gr_buffer_sptr buf, int history);
00077 
00078  protected:
00079   char                                 *d_base;         // base address of buffer
00080   unsigned int                          d_bufsize;      // in items
00081  private:
00082   gr_vmcircbuf                         *d_vmcircbuf;
00083   size_t                                d_sizeof_item;  // in bytes
00084   unsigned int                          d_write_index;  // in items [0,d_bufsize)
00085   std::vector<gr_buffer_reader *>       d_readers;
00086   bool                                  d_done;
00087   
00088   unsigned
00089   index_add (unsigned a, unsigned b)
00090   {
00091     unsigned s = a + b;
00092 
00093     if (s >= d_bufsize)
00094       s -= d_bufsize;
00095 
00096     assert (s < d_bufsize);
00097     return s;
00098   }
00099 
00100   unsigned
00101   index_sub (unsigned a, unsigned b)
00102   {
00103     int s = a - b;
00104 
00105     if (s < 0)
00106       s += d_bufsize;
00107 
00108     assert ((unsigned) s < d_bufsize);
00109     return s;
00110   }
00111 
00112   virtual bool allocate_buffer (int nitems, size_t sizeof_item);
00113 
00123   gr_buffer (int nitems, size_t sizeof_item);
00124 
00128   void drop_reader (gr_buffer_reader *reader);
00129 
00130 };
00131 
00133 gr_buffer_reader_sptr gr_buffer_add_reader (gr_buffer_sptr buf, int history);
00134 
00136 long gr_buffer_ncurrently_allocated ();
00137 
00138 
00139 // ---------------------------------------------------------------------------
00140 
00146 class gr_buffer_reader {
00147 
00148  public:
00149   ~gr_buffer_reader ();
00150 
00154   int items_available () const;
00155 
00160   int max_possible_items_available () const { return d_buffer->d_bufsize - 1; }
00161 
00167   const void *read_pointer ();
00168 
00169   /*
00170    * \brief tell buffer we read \p items from it
00171    */
00172   void update_read_pointer (int nitems);
00173 
00174   void set_done (bool done)   { d_buffer->set_done (done); }
00175   bool done () const { return d_buffer->done (); }
00176 
00177   // -------------------------------------------------------------------------
00178 
00179  private:
00180 
00181   friend class gr_buffer;
00182   friend gr_buffer_reader_sptr gr_buffer_add_reader (gr_buffer_sptr buf, int history);
00183 
00184 
00185   gr_buffer_sptr                d_buffer;
00186   unsigned int                  d_read_index;   // in items [0,d->buffer.d_bufsize)
00187 
00189   gr_buffer_reader (gr_buffer_sptr buffer, unsigned int read_index);
00190 };
00191 
00193 long gr_buffer_reader_ncurrently_allocated ();
00194 
00195 
00196 #endif /* INCLUDED_GR_BUFFER_H */

Generated on Sat Jul 8 17:04:51 2006 for GNU Radio 2.x by  doxygen 1.4.1