Skip to content

Commit e564639

Browse files
committed
pmt: moves the rest of the PMT constants from static globals to using get_ functions.
1 parent 5fd3e3f commit e564639

File tree

4 files changed

+75
-27
lines changed

4 files changed

+75
-27
lines changed

gnuradio-runtime/include/pmt/pmt.h

+19-6
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ class PMT_API notimplemented : public exception
8888
notimplemented(const std::string &msg, pmt_t obj);
8989
};
9090

91+
92+
/*
93+
* ------------------------------------------------------------------------
94+
* Constants
95+
* ------------------------------------------------------------------------
96+
*/
97+
98+
PMT_API pmt_t get_PMT_NIL();
99+
PMT_API pmt_t get_PMT_T();
100+
PMT_API pmt_t get_PMT_F();
101+
PMT_API pmt_t get_PMT_EOF();
102+
103+
#define PMT_NIL get_PMT_NIL()
104+
#define PMT_T get_PMT_T()
105+
#define PMT_F get_PMT_F()
106+
#define PMT_EOF get_PMT_EOF()
107+
108+
109+
91110
/*
92111
* ------------------------------------------------------------------------
93112
* Booleans. Two constants, #t and #f.
@@ -96,8 +115,6 @@ class PMT_API notimplemented : public exception
96115
* I.e., there is a single false value, #f.
97116
* ------------------------------------------------------------------------
98117
*/
99-
extern PMT_API const pmt_t PMT_T; //< \#t : boolean true constant
100-
extern PMT_API const pmt_t PMT_F; //< \#f : boolean false constant
101118

102119
//! Return true if obj is \#t or \#f, else return false.
103120
PMT_API bool is_bool(pmt_t obj);
@@ -259,9 +276,6 @@ PMT_API std::complex<double> to_complex(pmt_t z);
259276
* ------------------------------------------------------------------------
260277
*/
261278

262-
#define PMT_NIL get_PMT_NIL()
263-
PMT_API pmt_t get_PMT_NIL();
264-
265279
//! Return true if \p x is the empty list, otherwise return false.
266280
PMT_API bool is_null(const pmt_t& x);
267281

@@ -815,7 +829,6 @@ PMT_API bool list_has(pmt_t list, const pmt_t& item);
815829
* read / write
816830
* ------------------------------------------------------------------------
817831
*/
818-
extern PMT_API const pmt_t PMT_EOF; //< The end of file object
819832

820833
//! return true if obj is the EOF object, otherwise return false.
821834
PMT_API bool is_eof_object(pmt_t obj);

gnuradio-runtime/lib/pmt/pmt.cc

+46-16
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,24 @@
3434

3535
namespace pmt {
3636

37-
static const int CACHE_LINE_SIZE = 64; // good guess
38-
3937
# if (PMT_LOCAL_ALLOCATOR)
4038

41-
static pmt_pool global_pmt_pool(sizeof(pmt_pair), CACHE_LINE_SIZE);
39+
static const int
40+
get_cache_line_size()
41+
{
42+
static const int CACHE_LINE_SIZE = 64; // good guess
43+
return CACHE_LINE_SIZE;
44+
}
45+
46+
static pmt_pool global_pmt_pool(sizeof(pmt_pair), get_cache_line_size());
4247

4348
void *
4449
pmt_base::operator new(size_t size)
4550
{
4651
void *p = global_pmt_pool.malloc();
4752

4853
// fprintf(stderr, "pmt_base::new p = %p\n", p);
49-
assert((reinterpret_cast<intptr_t>(p) & (CACHE_LINE_SIZE - 1)) == 0);
54+
assert((reinterpret_cast<intptr_t>(p) & (get_cache_line_size() - 1)) == 0);
5055
return p;
5156
}
5257

@@ -158,14 +163,28 @@ _any(pmt_t x)
158163
// Globals
159164
////////////////////////////////////////////////////////////////////////////
160165

161-
const pmt_t PMT_T = pmt_t(new pmt_bool()); // singleton
162-
const pmt_t PMT_F = pmt_t(new pmt_bool()); // singleton
163-
const pmt_t PMT_EOF = cons(PMT_NIL, PMT_NIL); // singleton
164-
165166
pmt_t get_PMT_NIL()
166167
{
167-
static pmt_t NIL = pmt_t(new pmt_null());
168-
return NIL;
168+
static pmt_t _NIL = pmt_t(new pmt_null());
169+
return _NIL;
170+
}
171+
172+
pmt_t get_PMT_T()
173+
{
174+
static const pmt_t _T = pmt_t(new pmt_bool());
175+
return _T;
176+
}
177+
178+
pmt_t get_PMT_F()
179+
{
180+
static const pmt_t _F = pmt_t(new pmt_bool());
181+
return _F;
182+
}
183+
184+
pmt_t get_PMT_EOF()
185+
{
186+
static const pmt_t _EOF = cons(get_PMT_NIL(), get_PMT_NIL());
187+
return _EOF;
169188
}
170189

171190
////////////////////////////////////////////////////////////////////////////
@@ -212,8 +231,19 @@ to_bool(pmt_t val)
212231
// Symbols
213232
////////////////////////////////////////////////////////////////////////////
214233

215-
static const unsigned int SYMBOL_HASH_TABLE_SIZE = 701;
216-
static std::vector<pmt_t> s_symbol_hash_table(SYMBOL_HASH_TABLE_SIZE);
234+
static const unsigned int
235+
get_symbol_hash_table_size()
236+
{
237+
static const unsigned int SYMBOL_HASH_TABLE_SIZE = 701;
238+
return SYMBOL_HASH_TABLE_SIZE;
239+
}
240+
241+
static std::vector<pmt_t>*
242+
get_symbol_hash_table()
243+
{
244+
static std::vector<pmt_t> s_symbol_hash_table(get_symbol_hash_table_size());
245+
return &s_symbol_hash_table;
246+
}
217247

218248
pmt_symbol::pmt_symbol(const std::string &name) : d_name(name){}
219249

@@ -244,18 +274,18 @@ is_symbol(const pmt_t& obj)
244274
pmt_t
245275
string_to_symbol(const std::string &name)
246276
{
247-
unsigned hash = hash_string(name) % SYMBOL_HASH_TABLE_SIZE;
277+
unsigned hash = hash_string(name) % get_symbol_hash_table_size();
248278

249279
// Does a symbol with this name already exist?
250-
for (pmt_t sym = s_symbol_hash_table[hash]; sym; sym = _symbol(sym)->next()){
280+
for (pmt_t sym = (*get_symbol_hash_table())[hash]; sym; sym = _symbol(sym)->next()){
251281
if (name == _symbol(sym)->name())
252282
return sym; // Yes. Return it
253283
}
254284

255285
// Nope. Make a new one.
256286
pmt_t sym = pmt_t(new pmt_symbol(name));
257-
_symbol(sym)->set_next(s_symbol_hash_table[hash]);
258-
s_symbol_hash_table[hash] = sym;
287+
_symbol(sym)->set_next((*get_symbol_hash_table())[hash]);
288+
(*get_symbol_hash_table())[hash] = sym;
259289
return sym;
260290
}
261291

gnuradio-runtime/python/pmt/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
# due to changes in the PMT_NIL singleton for static builds, we force
5252
# this into Python here.
5353
PMT_NIL = get_PMT_NIL()
54+
PMT_T = get_PMT_T()
55+
PMT_F = get_PMT_F()
56+
PMT_EOF = get_PMT_EOF()
5457

5558
from pmt_to_python import pmt_to_python as to_python
5659
from pmt_to_python import python_to_pmt as to_pmt

gnuradio-runtime/swig/pmt_swig.i

+7-5
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,15 @@ namespace pmt{
8080
swig_int_ptr.__repr__ = lambda self: write_string(self)
8181
%}
8282

83-
84-
extern const pmt_t PMT_T;
85-
extern const pmt_t PMT_F;
86-
extern const pmt_t PMT_EOF;
87-
8883
pmt_t get_PMT_NIL();
84+
pmt_t get_PMT_T();
85+
pmt_t get_PMT_F();
86+
pmt_t get_PMT_EOF();
87+
8988
#define PMT_NIL get_PMT_NIL()
89+
#define PMT_T get_PMT_T()
90+
#define PMT_F get_PMT_F()
91+
#define PMT_EOF get_PMT_EOF()
9092

9193
bool is_bool(pmt_t obj);
9294
bool is_true(pmt_t obj);

0 commit comments

Comments
 (0)