GNU Radio 3.7.0-50 C++ API
buffer.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2009-2011,2013 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_RUNTIME_BUFFER_H
24 #define INCLUDED_GR_RUNTIME_BUFFER_H
25 
26 #include <gnuradio/api.h>
27 #include <gnuradio/runtime_types.h>
28 #include <gnuradio/tags.h>
29 #include <boost/weak_ptr.hpp>
30 #include <gnuradio/thread/thread.h>
31 #include <deque>
32 
33 namespace gr {
34 
35  class vmcircbuf;
36 
37  /*!
38  * \brief Allocate a buffer that holds at least \p nitems of size \p sizeof_item.
39  *
40  * The total size of the buffer will be rounded up to a system
41  * dependent boundary. This is typically the system page size, but
42  * under MS windows is 64KB.
43  *
44  * \param nitems is the minimum number of items the buffer will hold.
45  * \param sizeof_item is the size of an item in bytes.
46  * \param link is the block that writes to this buffer.
47  */
48  GR_RUNTIME_API buffer_sptr make_buffer(int nitems, size_t sizeof_item,
49  block_sptr link=block_sptr());
50 
51  /*!
52  * \brief Single writer, multiple reader fifo.
53  * \ingroup internal
54  */
56  {
57  public:
58  virtual ~buffer();
59 
60  /*!
61  * \brief return number of items worth of space available for writing
62  */
63  int space_available();
64 
65  /*!
66  * \brief return size of this buffer in items
67  */
68  int bufsize() const { return d_bufsize; }
69 
70  /*!
71  * \brief return pointer to write buffer.
72  *
73  * The return value points at space that can hold at least
74  * space_available() items.
75  */
76  void *write_pointer();
77 
78  /*!
79  * \brief tell buffer that we wrote \p nitems into it
80  */
81  void update_write_pointer(int nitems);
82 
83  void set_done(bool done);
84  bool done() const { return d_done; }
85 
86  /*!
87  * \brief Return the block that writes to this buffer.
88  */
89  block_sptr link() { return block_sptr(d_link); }
90 
91  size_t nreaders() const { return d_readers.size(); }
92  buffer_reader* reader(size_t index) { return d_readers[index]; }
93 
94  gr::thread::mutex *mutex() { return &d_mutex; }
95 
96  uint64_t nitems_written() { return d_abs_write_offset; }
97 
98  size_t get_sizeof_item() { return d_sizeof_item; }
99 
100  /*!
101  * \brief Adds a new tag to the buffer.
102  *
103  * \param tag the new tag
104  */
105  void add_item_tag(const tag_t &tag);
106 
107  /*!
108  * \brief Removes an existing tag from the buffer.
109  *
110  * If no such tag is found, does nothing.
111  * Note: Doesn't actually physically delete the tag, but
112  * marks it as deleted. For the user, this has the same effect:
113  * Any subsequent calls to get_tags_in_range() will not return
114  * the tag.
115  *
116  * \param tag the tag that needs to be removed
117  * \param id the unique ID of the block calling this function
118  */
119  void remove_item_tag(const tag_t &tag, long id);
120 
121  /*!
122  * \brief Removes all tags before \p max_time from buffer
123  *
124  * \param max_time the time (item number) to trim up until.
125  */
126  void prune_tags(uint64_t max_time);
127 
128  std::deque<tag_t>::iterator get_tags_begin() { return d_item_tags.begin(); }
129  std::deque<tag_t>::iterator get_tags_end() { return d_item_tags.end(); }
130 
131  // -------------------------------------------------------------------------
132 
133  private:
134  friend class buffer_reader;
135  friend GR_RUNTIME_API buffer_sptr make_buffer(int nitems, size_t sizeof_item, block_sptr link);
136  friend GR_RUNTIME_API buffer_reader_sptr buffer_add_reader(buffer_sptr buf, int nzero_preload, block_sptr link);
137 
138  protected:
139  char *d_base; // base address of buffer
140  unsigned int d_bufsize; // in items
141  private:
142  gr::vmcircbuf *d_vmcircbuf;
143  size_t d_sizeof_item; // in bytes
144  std::vector<buffer_reader *> d_readers;
145  boost::weak_ptr<block> d_link; // block that writes to this buffer
146 
147  //
148  // The mutex protects d_write_index, d_abs_write_offset, d_done, d_item_tags
149  // and the d_read_index's and d_abs_read_offset's in the buffer readers.
150  //
151  gr::thread::mutex d_mutex;
152  unsigned int d_write_index; // in items [0,d_bufsize)
153  uint64_t d_abs_write_offset; // num items written since the start
154  bool d_done;
155  std::deque<tag_t> d_item_tags;
156  uint64_t d_last_min_items_read;
157 
158  unsigned index_add(unsigned a, unsigned b)
159  {
160  unsigned s = a + b;
161 
162  if(s >= d_bufsize)
163  s -= d_bufsize;
164 
165  assert(s < d_bufsize);
166  return s;
167  }
168 
169  unsigned index_sub(unsigned a, unsigned b)
170  {
171  int s = a - b;
172 
173  if(s < 0)
174  s += d_bufsize;
175 
176  assert((unsigned) s < d_bufsize);
177  return s;
178  }
179 
180  virtual bool allocate_buffer(int nitems, size_t sizeof_item);
181 
182  /*!
183  * \brief constructor is private. Use gr_make_buffer to create instances.
184  *
185  * Allocate a buffer that holds at least \p nitems of size \p sizeof_item.
186  *
187  * \param nitems is the minimum number of items the buffer will hold.
188  * \param sizeof_item is the size of an item in bytes.
189  * \param link is the block that writes to this buffer.
190  *
191  * The total size of the buffer will be rounded up to a system
192  * dependent boundary. This is typically the system page size, but
193  * under MS windows is 64KB.
194  */
195  buffer(int nitems, size_t sizeof_item, block_sptr link);
196 
197  /*!
198  * \brief disassociate \p reader from this buffer
199  */
200  void drop_reader(buffer_reader *reader);
201  };
202 
203  /*!
204  * \brief Create a new gr::buffer_reader and attach it to buffer \p buf
205  * \param buf is the buffer the \p gr::buffer_reader reads from.
206  * \param nzero_preload -- number of zero items to "preload" into buffer.
207  * \param link is the block that reads from the buffer using this gr::buffer_reader.
208  */
209  GR_RUNTIME_API buffer_reader_sptr
210  buffer_add_reader(buffer_sptr buf, int nzero_preload, block_sptr link=block_sptr());
211 
212  //! returns # of buffers currently allocated
214 
215 
216  // ---------------------------------------------------------------------------
217 
218  /*!
219  * \brief How we keep track of the readers of a gr::buffer.
220  * \ingroup internal
221  */
223  {
224  public:
225  ~buffer_reader();
226 
227  /*!
228  * \brief Return number of items available for reading.
229  */
230  int items_available() const;
231 
232  /*!
233  * \brief Return buffer this reader reads from.
234  */
235  buffer_sptr buffer() const { return d_buffer; }
236 
237  /*!
238  * \brief Return maximum number of items that could ever be available for reading.
239  * This is used as a sanity check in the scheduler to avoid looping forever.
240  */
241  int max_possible_items_available() const { return d_buffer->d_bufsize - 1; }
242 
243  /*!
244  * \brief return pointer to read buffer.
245  *
246  * The return value points to items_available() number of items
247  */
248  const void *read_pointer();
249 
250  /*
251  * \brief tell buffer we read \p items from it
252  */
253  void update_read_pointer(int nitems);
254 
255  void set_done(bool done) { d_buffer->set_done(done); }
256  bool done() const { return d_buffer->done(); }
257 
258  gr::thread::mutex *mutex() { return d_buffer->mutex(); }
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  block_sptr link() { return block_sptr(d_link); }
269 
270  /*!
271  * \brief Given a [start,end), returns a vector all tags in the range.
272  *
273  * Get a vector of tags in given range. Range of counts is from start to end-1.
274  *
275  * Tags are tuples of:
276  * (item count, source id, key, value)
277  *
278  * \param v a vector reference to return tags into
279  * \param abs_start a uint64 count of the start of the range of interest
280  * \param abs_end a uint64 count of the end of the range of interest
281  * \param id the unique ID of the block to make sure already deleted tags are not returned
282  */
283  void get_tags_in_range(std::vector<tag_t> &v,
284  uint64_t abs_start,
285  uint64_t abs_end,
286  long id);
287 
288  // -------------------------------------------------------------------------
289 
290  private:
291  friend class buffer;
293  buffer_add_reader(buffer_sptr buf, int nzero_preload, block_sptr link);
294 
295  buffer_sptr d_buffer;
296  unsigned int d_read_index; // in items [0,d->buffer.d_bufsize)
297  uint64_t d_abs_read_offset; // num items seen since the start
298  boost::weak_ptr<block> d_link; // block that reads via this buffer reader
299 
300  //! constructor is private. Use gr::buffer::add_reader to create instances
301  buffer_reader(buffer_sptr buffer, unsigned int read_index, block_sptr link);
302  };
303 
304  //! returns # of buffer_readers currently allocated
306 
307 } /* namespace gr */
308 
309 #endif /* INCLUDED_GR_RUNTIME_BUFFER_H */