Imported Upstream version 3.2.2
[debian/gnuradio] / mblock / src / lib / mb_message.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2008 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 along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <mblock/message.h>
26 #include <stdio.h>
27 #include <pmt_pool.h>
28
29 static const int CACHE_LINE_SIZE = 64;  // good guess
30 static const int MAX_MESSAGES =  1024;  // KLUDGE max number of messages in sys
31                                         //   0 -> no limit
32 #if MB_MESSAGE_LOCAL_ALLOCATOR
33
34 static pmt_pool 
35 global_msg_pool(sizeof(mb_message), CACHE_LINE_SIZE, 16*1024, MAX_MESSAGES);
36
37 void *
38 mb_message::operator new(size_t size)
39 {
40   void *p = global_msg_pool.malloc();
41
42   // fprintf(stderr, "mb_message::new p = %p\n", p);
43   assert((reinterpret_cast<intptr_t>(p) & (CACHE_LINE_SIZE - 1)) == 0);
44   return p;
45 }
46
47 void
48 mb_message::operator delete(void *p, size_t size)
49 {
50   global_msg_pool.free(p);
51 }
52
53 #endif
54
55
56 mb_message_sptr
57 mb_make_message(pmt_t signal, pmt_t data, pmt_t metadata, mb_pri_t priority)
58 {
59   return mb_message_sptr(new mb_message(signal, data, metadata, priority));
60 }
61
62 mb_message::mb_message(pmt_t signal, pmt_t data, pmt_t metadata, mb_pri_t priority)
63   : d_signal(signal), d_data(data), d_metadata(metadata), d_priority(priority),
64     d_port_id(PMT_NIL)
65 {
66 }
67
68 mb_message::~mb_message()
69 {
70   // NOP
71 }
72
73 std::ostream& 
74 operator<<(std::ostream& os, const mb_message &msg)
75 {
76   os << "<msg: signal=" << msg.signal()
77      << " port_id=" << msg.port_id()
78      << " data=" << msg.data()
79      << " metadata=" << msg.metadata()
80      << " pri=" << msg.priority()
81      << ">";
82   
83   return os;
84 }