GNU Radio 3.5.3.2 C++ API
|
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 size_t get_sizeof_item() { return d_sizeof_item; } 00097 00098 /*! 00099 * \brief Adds a new tag to the buffer. 00100 * 00101 * \param tag the new tag 00102 */ 00103 void add_item_tag(const gr_tag_t &tag); 00104 00105 /*! 00106 * \brief Removes all tags before \p max_time from buffer 00107 * 00108 * \param max_time the time (item number) to trim up until. 00109 */ 00110 void prune_tags(uint64_t max_time); 00111 00112 std::deque<gr_tag_t>::iterator get_tags_begin() { return d_item_tags.begin(); } 00113 std::deque<gr_tag_t>::iterator get_tags_end() { return d_item_tags.end(); } 00114 00115 // ------------------------------------------------------------------------- 00116 00117 private: 00118 00119 friend class gr_buffer_reader; 00120 friend GR_CORE_API gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item, gr_block_sptr link); 00121 friend GR_CORE_API gr_buffer_reader_sptr gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload, gr_block_sptr link); 00122 00123 protected: 00124 char *d_base; // base address of buffer 00125 unsigned int d_bufsize; // in items 00126 private: 00127 gr_vmcircbuf *d_vmcircbuf; 00128 size_t d_sizeof_item; // in bytes 00129 std::vector<gr_buffer_reader *> d_readers; 00130 boost::weak_ptr<gr_block> d_link; // block that writes to this buffer 00131 00132 // 00133 // The mutex protects d_write_index, d_abs_write_offset, d_done, d_item_tags 00134 // and the d_read_index's and d_abs_read_offset's in the buffer readers. 00135 // 00136 gruel::mutex d_mutex; 00137 unsigned int d_write_index; // in items [0,d_bufsize) 00138 uint64_t d_abs_write_offset; // num items written since the start 00139 bool d_done; 00140 std::deque<gr_tag_t> d_item_tags; 00141 uint64_t d_last_min_items_read; 00142 00143 unsigned 00144 index_add (unsigned a, unsigned b) 00145 { 00146 unsigned s = a + b; 00147 00148 if (s >= d_bufsize) 00149 s -= d_bufsize; 00150 00151 assert (s < d_bufsize); 00152 return s; 00153 } 00154 00155 unsigned 00156 index_sub (unsigned a, unsigned b) 00157 { 00158 int s = a - b; 00159 00160 if (s < 0) 00161 s += d_bufsize; 00162 00163 assert ((unsigned) s < d_bufsize); 00164 return s; 00165 } 00166 00167 virtual bool allocate_buffer (int nitems, size_t sizeof_item); 00168 00169 /*! 00170 * \brief constructor is private. Use gr_make_buffer to create instances. 00171 * 00172 * Allocate a buffer that holds at least \p nitems of size \p sizeof_item. 00173 * 00174 * \param nitems is the minimum number of items the buffer will hold. 00175 * \param sizeof_item is the size of an item in bytes. 00176 * \param link is the block that writes to this buffer. 00177 * 00178 * The total size of the buffer will be rounded up to a system 00179 * dependent boundary. This is typically the system page size, but 00180 * under MS windows is 64KB. 00181 */ 00182 gr_buffer (int nitems, size_t sizeof_item, gr_block_sptr link); 00183 00184 /*! 00185 * \brief disassociate \p reader from this buffer 00186 */ 00187 void drop_reader (gr_buffer_reader *reader); 00188 00189 }; 00190 00191 /*! 00192 * \brief Create a new gr_buffer_reader and attach it to buffer \p buf 00193 * \param buf is the buffer the \p gr_buffer_reader reads from. 00194 * \param nzero_preload -- number of zero items to "preload" into buffer. 00195 * \param link is the block that reads from the buffer using this gr_buffer_reader. 00196 */ 00197 GR_CORE_API gr_buffer_reader_sptr 00198 gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload, gr_block_sptr link=gr_block_sptr()); 00199 00200 //! returns # of gr_buffers currently allocated 00201 GR_CORE_API long gr_buffer_ncurrently_allocated (); 00202 00203 00204 // --------------------------------------------------------------------------- 00205 00206 /*! 00207 * \brief How we keep track of the readers of a gr_buffer. 00208 * \ingroup internal 00209 */ 00210 00211 class GR_CORE_API gr_buffer_reader { 00212 public: 00213 00214 ~gr_buffer_reader (); 00215 00216 /*! 00217 * \brief Return number of items available for reading. 00218 */ 00219 int items_available () const; 00220 00221 /*! 00222 * \brief Return buffer this reader reads from. 00223 */ 00224 gr_buffer_sptr buffer () const { return d_buffer; } 00225 00226 00227 /*! 00228 * \brief Return maximum number of items that could ever be available for reading. 00229 * This is used as a sanity check in the scheduler to avoid looping forever. 00230 */ 00231 int max_possible_items_available () const { return d_buffer->d_bufsize - 1; } 00232 00233 /*! 00234 * \brief return pointer to read buffer. 00235 * 00236 * The return value points to items_available() number of items 00237 */ 00238 const void *read_pointer (); 00239 00240 /* 00241 * \brief tell buffer we read \p items from it 00242 */ 00243 void update_read_pointer (int nitems); 00244 00245 void set_done (bool done) { d_buffer->set_done (done); } 00246 bool done () const { return d_buffer->done (); } 00247 00248 gruel::mutex *mutex() { return d_buffer->mutex(); } 00249 00250 00251 uint64_t nitems_read() { return d_abs_read_offset; } 00252 00253 size_t get_sizeof_item() { return d_buffer->get_sizeof_item(); } 00254 00255 /*! 00256 * \brief Return the block that reads via this reader. 00257 * 00258 */ 00259 gr_block_sptr link() { return gr_block_sptr(d_link); } 00260 00261 00262 /*! 00263 * \brief Given a [start,end), returns a vector all tags in the range. 00264 * 00265 * Get a vector of tags in given range. Range of counts is from start to end-1. 00266 * 00267 * Tags are tuples of: 00268 * (item count, source id, key, value) 00269 * 00270 * \param v a vector reference to return tags into 00271 * \param abs_start a uint64 count of the start of the range of interest 00272 * \param abs_end a uint64 count of the end of the range of interest 00273 */ 00274 void get_tags_in_range(std::vector<gr_tag_t> &v, 00275 uint64_t abs_start, 00276 uint64_t abs_end); 00277 00278 // ------------------------------------------------------------------------- 00279 00280 private: 00281 00282 friend class gr_buffer; 00283 friend GR_CORE_API gr_buffer_reader_sptr 00284 gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload, gr_block_sptr link); 00285 00286 00287 gr_buffer_sptr d_buffer; 00288 unsigned int d_read_index; // in items [0,d->buffer.d_bufsize) 00289 uint64_t d_abs_read_offset; // num items seen since the start 00290 boost::weak_ptr<gr_block> d_link; // block that reads via this buffer reader 00291 00292 //! constructor is private. Use gr_buffer::add_reader to create instances 00293 gr_buffer_reader (gr_buffer_sptr buffer, unsigned int read_index, gr_block_sptr link); 00294 }; 00295 00296 //! returns # of gr_buffer_readers currently allocated 00297 GR_CORE_API long gr_buffer_reader_ncurrently_allocated (); 00298 00299 00300 #endif /* INCLUDED_GR_BUFFER_H */