forked from zeromq/libzmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_allocators.cpp
125 lines (105 loc) · 3.38 KB
/
decoder_allocators.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* SPDX-License-Identifier: MPL-2.0 */
#include "precompiled.hpp"
#include "decoder_allocators.hpp"
#include "msg.hpp"
zmq::shared_message_memory_allocator::shared_message_memory_allocator (
std::size_t bufsize_) :
_buf (NULL),
_buf_size (0),
_max_size (bufsize_),
_msg_content (NULL),
_max_counters ((_max_size + msg_t::max_vsm_size - 1) / msg_t::max_vsm_size)
{
}
zmq::shared_message_memory_allocator::shared_message_memory_allocator (
std::size_t bufsize_, std::size_t max_messages_) :
_buf (NULL),
_buf_size (0),
_max_size (bufsize_),
_msg_content (NULL),
_max_counters (max_messages_)
{
}
zmq::shared_message_memory_allocator::~shared_message_memory_allocator ()
{
deallocate ();
}
unsigned char *zmq::shared_message_memory_allocator::allocate ()
{
if (_buf) {
// release reference count to couple lifetime to messages
zmq::atomic_counter_t *c =
reinterpret_cast<zmq::atomic_counter_t *> (_buf);
// if refcnt drops to 0, there are no message using the buffer
// because either all messages have been closed or only vsm-messages
// were created
if (c->sub (1)) {
// buffer is still in use as message data. "Release" it and create a new one
// release pointer because we are going to create a new buffer
release ();
}
}
// if buf != NULL it is not used by any message so we can re-use it for the next run
if (!_buf) {
// allocate memory for reference counters together with reception buffer
std::size_t const allocationsize =
_max_size + sizeof (zmq::atomic_counter_t)
+ _max_counters * sizeof (zmq::msg_t::content_t);
_buf = static_cast<unsigned char *> (std::malloc (allocationsize));
alloc_assert (_buf);
new (_buf) atomic_counter_t (1);
} else {
// release reference count to couple lifetime to messages
zmq::atomic_counter_t *c =
reinterpret_cast<zmq::atomic_counter_t *> (_buf);
c->set (1);
}
_buf_size = _max_size;
_msg_content = reinterpret_cast<zmq::msg_t::content_t *> (
_buf + sizeof (atomic_counter_t) + _max_size);
return _buf + sizeof (zmq::atomic_counter_t);
}
void zmq::shared_message_memory_allocator::deallocate ()
{
zmq::atomic_counter_t *c = reinterpret_cast<zmq::atomic_counter_t *> (_buf);
if (_buf && !c->sub (1)) {
c->~atomic_counter_t ();
std::free (_buf);
}
clear ();
}
unsigned char *zmq::shared_message_memory_allocator::release ()
{
unsigned char *b = _buf;
clear ();
return b;
}
void zmq::shared_message_memory_allocator::clear ()
{
_buf = NULL;
_buf_size = 0;
_msg_content = NULL;
}
void zmq::shared_message_memory_allocator::inc_ref ()
{
(reinterpret_cast<zmq::atomic_counter_t *> (_buf))->add (1);
}
void zmq::shared_message_memory_allocator::call_dec_ref (void *, void *hint_)
{
zmq_assert (hint_);
unsigned char *buf = static_cast<unsigned char *> (hint_);
zmq::atomic_counter_t *c = reinterpret_cast<zmq::atomic_counter_t *> (buf);
if (!c->sub (1)) {
c->~atomic_counter_t ();
std::free (buf);
buf = NULL;
}
}
std::size_t zmq::shared_message_memory_allocator::size () const
{
return _buf_size;
}
unsigned char *zmq::shared_message_memory_allocator::data ()
{
return _buf + sizeof (zmq::atomic_counter_t);
}