1 /* -*- c++ -*- */
2 /*
3  * Copyright 2003,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 GR_VMCIRCBUF_H
24 #define GR_VMCIRCBUF_H
25 
26 #include <gnuradio/api.h>
27 #include <gnuradio/thread/thread.h>
28 #include <vector>
29 
30 extern gr::thread::mutex s_vm_mutex;
31 
32 namespace gr {
33 
34 /*!
35  * \brief abstract class to implement doubly mapped virtual memory circular buffers
36  * \ingroup internal
37  */
38 class GR_RUNTIME_API vmcircbuf
39 {
40 protected:
41     int d_size;
42     char* d_base;
43 
44     // CREATORS
vmcircbuf(int size)45     vmcircbuf(int size) : d_size(size), d_base(0){};
46 
47 public:
48     virtual ~vmcircbuf();
49 
50     // ACCESSORS
pointer_to_first_copy()51     void* pointer_to_first_copy() const { return d_base; }
pointer_to_second_copy()52     void* pointer_to_second_copy() const { return d_base + d_size; }
53 };
54 
55 /*!
56  * \brief abstract factory for creating circular buffers
57  */
58 class GR_RUNTIME_API vmcircbuf_factory
59 {
60 protected:
vmcircbuf_factory()61     vmcircbuf_factory(){};
62     virtual ~vmcircbuf_factory();
63 
64 public:
65     /*!
66      * \brief return name of this factory
67      */
68     virtual const char* name() const = 0;
69 
70     /*!
71      * \brief return granularity of mapping, typically equal to page size
72      */
73     virtual int granularity() = 0;
74 
75     /*!
76      * \brief return a gr::vmcircbuf, or 0 if unable.
77      *
78      * Call this to create a doubly mapped circular buffer.
79      */
80     virtual vmcircbuf* make(int size) = 0;
81 };
82 
83 /*
84  * \brief pulls together all implementations of gr::vmcircbuf
85  */
86 class GR_RUNTIME_API vmcircbuf_sysconfig
87 {
88 public:
89     /*
90      * \brief return the single instance of the default factory.
91      *
92      * returns the default factory to use if it's already defined,
93      * else find the first working factory and use it.
94      */
95     static vmcircbuf_factory* get_default_factory();
96 
granularity()97     static int granularity() { return get_default_factory()->granularity(); }
make(int size)98     static vmcircbuf* make(int size) { return get_default_factory()->make(size); }
99 
100     // N.B. not all factories are guaranteed to work.
101     // It's too hard to check everything at config time, so we check at runtime
102     static std::vector<vmcircbuf_factory*> all_factories();
103 
104     // make this factory the default
105     static void set_default_factory(vmcircbuf_factory* f);
106 
107     /*!
108      * \brief  Does this factory really work?
109      *
110      * verbose = 0: silent
111      * verbose = 1: names of factories tested and results
112      * verbose = 2: all intermediate results
113      */
114     static bool test_factory(vmcircbuf_factory* f, int verbose);
115 
116     /*!
117      * \brief Test all factories, return true if at least one of them works
118      * verbose = 0: silent
119      * verbose = 1: names of factories tested and results
120      * verbose = 2: all intermediate results
121      */
122     static bool test_all_factories(int verbose);
123 };
124 
125 } /* namespace gr */
126 
127 #endif /* GR_VMCIRCBUF_H */
128