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