GNU Radio C++ API Reference  gcd20ee2
The Free & Open Software Radio Ecosystem
buffer_double_mapped.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2020 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * SPDX-License-Identifier: GPL-3.0-or-later
8  *
9  */
10 
11 #ifndef INCLUDED_GR_RUNTIME_BUFFER_DOUBLE_MAPPED_H
12 #define INCLUDED_GR_RUNTIME_BUFFER_DOUBLE_MAPPED_H
13 
14 #include <gnuradio/api.h>
15 #include <gnuradio/buffer.h>
16 #include <gnuradio/buffer_type.h>
17 #include <gnuradio/logger.h>
18 #include <gnuradio/runtime_types.h>
19 
20 namespace gr {
21 
22 class vmcircbuf;
23 
24 /*!
25  * \brief Single writer, multiple reader fifo.
26  * \ingroup internal
27  */
29 {
30 public:
31  static buffer_type type;
32 
33  static buffer_sptr make_buffer(int nitems,
34  size_t sizeof_item,
35  uint64_t downstream_lcm_nitems,
36  uint32_t downstream_max_out_mult,
37  block_sptr link = block_sptr(),
38  block_sptr buf_owner = block_sptr());
39 
41 
42  /*!
43  * \brief return the buffer's buffer_type
44  */
46 
47  /*!
48  * \brief return number of items worth of space available for writing
49  */
50  int space_available() override;
51 
52  /*!
53  * Inherited from buffer class.
54  * @param nitems is the number of items produced by the general_work() function.
55  */
56  void post_work( int nitems) override {}
57 
58 protected:
59  /*!
60  * sets d_vmcircbuf, d_base, d_bufsize.
61  * returns true iff successful.
62  */
63  bool allocate_buffer(int nitems) override;
64 
65  unsigned index_add(unsigned a, unsigned b) override
66  {
67  unsigned s = a + b;
68 
69  if (s >= d_bufsize)
70  s -= d_bufsize;
71 
72  assert(s < d_bufsize);
73  return s;
74  }
75 
76  unsigned index_sub(unsigned a, unsigned b) override
77  {
78  int s = a - b;
79 
80  if (s < 0)
81  s += d_bufsize;
82 
83  assert((unsigned)s < d_bufsize);
84  return s;
85  }
86 
87 private:
88  friend class buffer_reader;
89 
90  friend GR_RUNTIME_API buffer_sptr make_buffer(int nitems,
91  size_t sizeof_item,
92  uint64_t downstream_lcm_nitems,
93  block_sptr link,
94  block_sptr buf_owner);
95  friend GR_RUNTIME_API buffer_sptr
97  size_t sizeof_item,
98  uint64_t downstream_lcm_nitems,
99  uint32_t downstream_max_out_mult,
100  block_sptr link,
101  block_sptr buf_owner);
102 
103  std::unique_ptr<gr::vmcircbuf> d_vmcircbuf;
104 
105 protected:
106  /*!
107  * \brief constructor is protected. Use gr_make_buffer to create instances.
108  *
109  * Allocate a buffer that holds at least \p nitems of size \p sizeof_item.
110  *
111  * \param nitems is the minimum number of items the buffer will hold.
112  * \param sizeof_item is the size of an item in bytes.
113  * \param downstream_lcm_nitems is the least common multiple of the items to
114  * read by downstream blocks
115  * \param downstream_max_out_mult is the maximum output multiple of all
116  * downstream blocks
117  * \param link is the block that writes to this buffer.
118  *
119  * The total size of the buffer will be rounded up to a system
120  * dependent boundary. This is typically the system page size, but
121  * under MS windows is 64KB.
122  */
124  size_t sizeof_item,
125  uint64_t downstream_lcm_nitems,
126  uint32_t downstream_max_out_mult,
127  block_sptr link);
128 
129  /*!
130  * \brief Tag type for derived-class constructors that handle their own
131  * buffer allocation (skipping the default vmcircbuf path).
132  */
133  enum class defer_alloc_t { defer_alloc };
134 
135  /*!
136  * \brief Protected constructor that defers buffer allocation.
137  *
138  * Initialises the base buffer state and loggers but does NOT call
139  * allocate_buffer(). Derived classes that provide their own memory
140  * (e.g. CUDA VMM) use this constructor and perform allocation in
141  * their own constructor body, where virtual dispatch is available.
142  */
144  size_t sizeof_item,
145  uint64_t downstream_lcm_nitems,
146  uint32_t downstream_max_out_mult,
147  block_sptr link,
148  defer_alloc_t);
149 };
150 
151 } /* namespace gr */
152 
153 
154 #endif /* INCLUDED_GR_RUNTIME_BUFFER_DOUBLE_MAPPED_H */
Single writer, multiple reader fifo.
Definition: buffer_double_mapped.h:29
unsigned index_sub(unsigned a, unsigned b) override
Decrement read or write index for this buffer.
Definition: buffer_double_mapped.h:76
unsigned index_add(unsigned a, unsigned b) override
Increment read or write index for this buffer.
Definition: buffer_double_mapped.h:65
friend GR_RUNTIME_API buffer_sptr make_buffer(int nitems, size_t sizeof_item, uint64_t downstream_lcm_nitems, block_sptr link, block_sptr buf_owner)
friend GR_RUNTIME_API buffer_sptr make_buffer_double_mapped(int nitems, size_t sizeof_item, uint64_t downstream_lcm_nitems, uint32_t downstream_max_out_mult, block_sptr link, block_sptr buf_owner)
~buffer_double_mapped() override
bool allocate_buffer(int nitems) override
buffer_double_mapped(int nitems, size_t sizeof_item, uint64_t downstream_lcm_nitems, uint32_t downstream_max_out_mult, block_sptr link)
constructor is protected. Use gr_make_buffer to create instances.
buffer_double_mapped(int nitems, size_t sizeof_item, uint64_t downstream_lcm_nitems, uint32_t downstream_max_out_mult, block_sptr link, defer_alloc_t)
Protected constructor that defers buffer allocation.
buffer_type get_buffer_type() const override
return the buffer's buffer_type
Definition: buffer_double_mapped.h:45
static buffer_sptr make_buffer(int nitems, size_t sizeof_item, uint64_t downstream_lcm_nitems, uint32_t downstream_max_out_mult, block_sptr link=block_sptr(), block_sptr buf_owner=block_sptr())
defer_alloc_t
Tag type for derived-class constructors that handle their own buffer allocation (skipping the default...
Definition: buffer_double_mapped.h:133
static buffer_type type
Definition: buffer_double_mapped.h:31
int space_available() override
return number of items worth of space available for writing
void post_work(int nitems) override
Definition: buffer_double_mapped.h:56
How we keep track of the readers of a gr::buffer.
Definition: buffer_reader.h:53
Base class for describing a buffer's type.
Definition: buffer_type.h:28
Single writer, multiple reader fifo.
Definition: buffer.h:68
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
GNU Radio logging wrapper.
Definition: basic_block.h:29