GNU Radio 3.6.4 C++ API
gr_buffer.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2009,2010,2011 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifndef INCLUDED_GR_BUFFER_H
24 #define INCLUDED_GR_BUFFER_H
25 
26 #include <gr_core_api.h>
27 #include <gr_runtime_types.h>
28 #include <boost/weak_ptr.hpp>
29 #include <gruel/thread.h>
30 #include <gr_tags.h>
31 #include <deque>
32 
33 class gr_vmcircbuf;
34 
35 /*!
36  * \brief Allocate a buffer that holds at least \p nitems of size \p sizeof_item.
37  *
38  * The total size of the buffer will be rounded up to a system
39  * dependent boundary. This is typically the system page size, but
40  * under MS windows is 64KB.
41  *
42  * \param nitems is the minimum number of items the buffer will hold.
43  * \param sizeof_item is the size of an item in bytes.
44  * \param link is the block that writes to this buffer.
45  */
46 GR_CORE_API gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item, gr_block_sptr link=gr_block_sptr());
47 
48 
49 /*!
50  * \brief Single writer, multiple reader fifo.
51  * \ingroup internal
52  */
54  public:
55 
56  virtual ~gr_buffer ();
57 
58  /*!
59  * \brief return number of items worth of space available for writing
60  */
61  int space_available ();
62 
63  /*!
64  * \brief return size of this buffer in items
65  */
66  int bufsize() const { return d_bufsize; }
67 
68  /*!
69  * \brief return pointer to write buffer.
70  *
71  * The return value points at space that can hold at least
72  * space_available() items.
73  */
74  void *write_pointer ();
75 
76  /*!
77  * \brief tell buffer that we wrote \p nitems into it
78  */
79  void update_write_pointer (int nitems);
80 
81  void set_done (bool done);
82  bool done () const { return d_done; }
83 
84  /*!
85  * \brief Return the block that writes to this buffer.
86  */
87  gr_block_sptr link() { return gr_block_sptr(d_link); }
88 
89  size_t nreaders() const { return d_readers.size(); }
90  gr_buffer_reader* reader(size_t index) { return d_readers[index]; }
91 
92  gruel::mutex *mutex() { return &d_mutex; }
93 
94  uint64_t nitems_written() { return d_abs_write_offset; }
95 
96  size_t get_sizeof_item() { return d_sizeof_item; }
97 
98  /*!
99  * \brief Adds a new tag to the buffer.
100  *
101  * \param tag the new tag
102  */
103  void add_item_tag(const gr_tag_t &tag);
104 
105  /*!
106  * \brief Removes an existing tag from the buffer.
107  *
108  * If no such tag is found, does nothing.
109  *
110  * \param tag the tag that needs to be removed
111  */
112  void remove_item_tag(const gr_tag_t &tag);
113 
114  /*!
115  * \brief Removes all tags before \p max_time from buffer
116  *
117  * \param max_time the time (item number) to trim up until.
118  */
119  void prune_tags(uint64_t max_time);
120 
121  std::deque<gr_tag_t>::iterator get_tags_begin() { return d_item_tags.begin(); }
122  std::deque<gr_tag_t>::iterator get_tags_end() { return d_item_tags.end(); }
123 
124  // -------------------------------------------------------------------------
125 
126  private:
127 
128  friend class gr_buffer_reader;
129  friend GR_CORE_API gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item, gr_block_sptr link);
131 
132  protected:
133  char *d_base; // base address of buffer
134  unsigned int d_bufsize; // in items
135  private:
136  gr_vmcircbuf *d_vmcircbuf;
137  size_t d_sizeof_item; // in bytes
138  std::vector<gr_buffer_reader *> d_readers;
139  boost::weak_ptr<gr_block> d_link; // block that writes to this buffer
140 
141  //
142  // The mutex protects d_write_index, d_abs_write_offset, d_done, d_item_tags
143  // and the d_read_index's and d_abs_read_offset's in the buffer readers.
144  //
145  gruel::mutex d_mutex;
146  unsigned int d_write_index; // in items [0,d_bufsize)
147  uint64_t d_abs_write_offset; // num items written since the start
148  bool d_done;
149  std::deque<gr_tag_t> d_item_tags;
150  uint64_t d_last_min_items_read;
151 
152  unsigned
153  index_add (unsigned a, unsigned b)
154  {
155  unsigned s = a + b;
156 
157  if (s >= d_bufsize)
158  s -= d_bufsize;
159 
160  assert (s < d_bufsize);
161  return s;
162  }
163 
164  unsigned
165  index_sub (unsigned a, unsigned b)
166  {
167  int s = a - b;
168 
169  if (s < 0)
170  s += d_bufsize;
171 
172  assert ((unsigned) s < d_bufsize);
173  return s;
174  }
175 
176  virtual bool allocate_buffer (int nitems, size_t sizeof_item);
177 
178  /*!
179  * \brief constructor is private. Use gr_make_buffer to create instances.
180  *
181  * Allocate a buffer that holds at least \p nitems of size \p sizeof_item.
182  *
183  * \param nitems is the minimum number of items the buffer will hold.
184  * \param sizeof_item is the size of an item in bytes.
185  * \param link is the block that writes to this buffer.
186  *
187  * The total size of the buffer will be rounded up to a system
188  * dependent boundary. This is typically the system page size, but
189  * under MS windows is 64KB.
190  */
191  gr_buffer (int nitems, size_t sizeof_item, gr_block_sptr link);
192 
193  /*!
194  * \brief disassociate \p reader from this buffer
195  */
196  void drop_reader (gr_buffer_reader *reader);
197 
198 };
199 
200 /*!
201  * \brief Create a new gr_buffer_reader and attach it to buffer \p buf
202  * \param buf is the buffer the \p gr_buffer_reader reads from.
203  * \param nzero_preload -- number of zero items to "preload" into buffer.
204  * \param link is the block that reads from the buffer using this gr_buffer_reader.
205  */
208 
209 //! returns # of gr_buffers currently allocated
211 
212 
213 // ---------------------------------------------------------------------------
214 
215 /*!
216  * \brief How we keep track of the readers of a gr_buffer.
217  * \ingroup internal
218  */
219 
221  public:
222 
223  ~gr_buffer_reader ();
224 
225  /*!
226  * \brief Return number of items available for reading.
227  */
228  int items_available () const;
229 
230  /*!
231  * \brief Return buffer this reader reads from.
232  */
233  gr_buffer_sptr buffer () const { return d_buffer; }
234 
235 
236  /*!
237  * \brief Return maximum number of items that could ever be available for reading.
238  * This is used as a sanity check in the scheduler to avoid looping forever.
239  */
240  int max_possible_items_available () const { return d_buffer->d_bufsize - 1; }
241 
242  /*!
243  * \brief return pointer to read buffer.
244  *
245  * The return value points to items_available() number of items
246  */
247  const void *read_pointer ();
248 
249  /*
250  * \brief tell buffer we read \p items from it
251  */
252  void update_read_pointer (int nitems);
253 
254  void set_done (bool done) { d_buffer->set_done (done); }
255  bool done () const { return d_buffer->done (); }
256 
257  gruel::mutex *mutex() { return d_buffer->mutex(); }
258 
259 
260  uint64_t nitems_read() { return d_abs_read_offset; }
261 
262  size_t get_sizeof_item() { return d_buffer->get_sizeof_item(); }
263 
264  /*!
265  * \brief Return the block that reads via this reader.
266  *
267  */
268  gr_block_sptr link() { return gr_block_sptr(d_link); }
269 
270 
271  /*!
272  * \brief Given a [start,end), returns a vector all tags in the range.
273  *
274  * Get a vector of tags in given range. Range of counts is from start to end-1.
275  *
276  * Tags are tuples of:
277  * (item count, source id, key, value)
278  *
279  * \param v a vector reference to return tags into
280  * \param abs_start a uint64 count of the start of the range of interest
281  * \param abs_end a uint64 count of the end of the range of interest
282  */
283  void get_tags_in_range(std::vector<gr_tag_t> &v,
284  uint64_t abs_start,
285  uint64_t abs_end);
286 
287  // -------------------------------------------------------------------------
288 
289  private:
290 
291  friend class gr_buffer;
293  gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload, gr_block_sptr link);
294 
295 
296  gr_buffer_sptr d_buffer;
297  unsigned int d_read_index; // in items [0,d->buffer.d_bufsize)
298  uint64_t d_abs_read_offset; // num items seen since the start
299  boost::weak_ptr<gr_block> d_link; // block that reads via this buffer reader
300 
301  //! constructor is private. Use gr_buffer::add_reader to create instances
302  gr_buffer_reader (gr_buffer_sptr buffer, unsigned int read_index, gr_block_sptr link);
303 };
304 
305 //! returns # of gr_buffer_readers currently allocated
307 
308 
309 #endif /* INCLUDED_GR_BUFFER_H */