GNU Radio 3.5.3.2 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 all tags before \p max_time from buffer
107  *
108  * \param max_time the time (item number) to trim up until.
109  */
110  void prune_tags(uint64_t max_time);
111 
112  std::deque<gr_tag_t>::iterator get_tags_begin() { return d_item_tags.begin(); }
113  std::deque<gr_tag_t>::iterator get_tags_end() { return d_item_tags.end(); }
114 
115  // -------------------------------------------------------------------------
116 
117  private:
118 
119  friend class gr_buffer_reader;
120  friend GR_CORE_API gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item, gr_block_sptr link);
122 
123  protected:
124  char *d_base; // base address of buffer
125  unsigned int d_bufsize; // in items
126  private:
127  gr_vmcircbuf *d_vmcircbuf;
128  size_t d_sizeof_item; // in bytes
129  std::vector<gr_buffer_reader *> d_readers;
130  boost::weak_ptr<gr_block> d_link; // block that writes to this buffer
131 
132  //
133  // The mutex protects d_write_index, d_abs_write_offset, d_done, d_item_tags
134  // and the d_read_index's and d_abs_read_offset's in the buffer readers.
135  //
136  gruel::mutex d_mutex;
137  unsigned int d_write_index; // in items [0,d_bufsize)
138  uint64_t d_abs_write_offset; // num items written since the start
139  bool d_done;
140  std::deque<gr_tag_t> d_item_tags;
141  uint64_t d_last_min_items_read;
142 
143  unsigned
144  index_add (unsigned a, unsigned b)
145  {
146  unsigned s = a + b;
147 
148  if (s >= d_bufsize)
149  s -= d_bufsize;
150 
151  assert (s < d_bufsize);
152  return s;
153  }
154 
155  unsigned
156  index_sub (unsigned a, unsigned b)
157  {
158  int s = a - b;
159 
160  if (s < 0)
161  s += d_bufsize;
162 
163  assert ((unsigned) s < d_bufsize);
164  return s;
165  }
166 
167  virtual bool allocate_buffer (int nitems, size_t sizeof_item);
168 
169  /*!
170  * \brief constructor is private. Use gr_make_buffer to create instances.
171  *
172  * Allocate a buffer that holds at least \p nitems of size \p sizeof_item.
173  *
174  * \param nitems is the minimum number of items the buffer will hold.
175  * \param sizeof_item is the size of an item in bytes.
176  * \param link is the block that writes to this buffer.
177  *
178  * The total size of the buffer will be rounded up to a system
179  * dependent boundary. This is typically the system page size, but
180  * under MS windows is 64KB.
181  */
182  gr_buffer (int nitems, size_t sizeof_item, gr_block_sptr link);
183 
184  /*!
185  * \brief disassociate \p reader from this buffer
186  */
187  void drop_reader (gr_buffer_reader *reader);
188 
189 };
190 
191 /*!
192  * \brief Create a new gr_buffer_reader and attach it to buffer \p buf
193  * \param buf is the buffer the \p gr_buffer_reader reads from.
194  * \param nzero_preload -- number of zero items to "preload" into buffer.
195  * \param link is the block that reads from the buffer using this gr_buffer_reader.
196  */
199 
200 //! returns # of gr_buffers currently allocated
202 
203 
204 // ---------------------------------------------------------------------------
205 
206 /*!
207  * \brief How we keep track of the readers of a gr_buffer.
208  * \ingroup internal
209  */
210 
212  public:
213 
214  ~gr_buffer_reader ();
215 
216  /*!
217  * \brief Return number of items available for reading.
218  */
219  int items_available () const;
220 
221  /*!
222  * \brief Return buffer this reader reads from.
223  */
224  gr_buffer_sptr buffer () const { return d_buffer; }
225 
226 
227  /*!
228  * \brief Return maximum number of items that could ever be available for reading.
229  * This is used as a sanity check in the scheduler to avoid looping forever.
230  */
231  int max_possible_items_available () const { return d_buffer->d_bufsize - 1; }
232 
233  /*!
234  * \brief return pointer to read buffer.
235  *
236  * The return value points to items_available() number of items
237  */
238  const void *read_pointer ();
239 
240  /*
241  * \brief tell buffer we read \p items from it
242  */
243  void update_read_pointer (int nitems);
244 
245  void set_done (bool done) { d_buffer->set_done (done); }
246  bool done () const { return d_buffer->done (); }
247 
248  gruel::mutex *mutex() { return d_buffer->mutex(); }
249 
250 
251  uint64_t nitems_read() { return d_abs_read_offset; }
252 
253  size_t get_sizeof_item() { return d_buffer->get_sizeof_item(); }
254 
255  /*!
256  * \brief Return the block that reads via this reader.
257  *
258  */
259  gr_block_sptr link() { return gr_block_sptr(d_link); }
260 
261 
262  /*!
263  * \brief Given a [start,end), returns a vector all tags in the range.
264  *
265  * Get a vector of tags in given range. Range of counts is from start to end-1.
266  *
267  * Tags are tuples of:
268  * (item count, source id, key, value)
269  *
270  * \param v a vector reference to return tags into
271  * \param abs_start a uint64 count of the start of the range of interest
272  * \param abs_end a uint64 count of the end of the range of interest
273  */
274  void get_tags_in_range(std::vector<gr_tag_t> &v,
275  uint64_t abs_start,
276  uint64_t abs_end);
277 
278  // -------------------------------------------------------------------------
279 
280  private:
281 
282  friend class gr_buffer;
284  gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload, gr_block_sptr link);
285 
286 
287  gr_buffer_sptr d_buffer;
288  unsigned int d_read_index; // in items [0,d->buffer.d_bufsize)
289  uint64_t d_abs_read_offset; // num items seen since the start
290  boost::weak_ptr<gr_block> d_link; // block that reads via this buffer reader
291 
292  //! constructor is private. Use gr_buffer::add_reader to create instances
293  gr_buffer_reader (gr_buffer_sptr buffer, unsigned int read_index, gr_block_sptr link);
294 };
295 
296 //! returns # of gr_buffer_readers currently allocated
298 
299 
300 #endif /* INCLUDED_GR_BUFFER_H */