decoder_allocators.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #ifndef __ZMQ_DECODER_ALLOCATORS_HPP_INCLUDED__
4 #define __ZMQ_DECODER_ALLOCATORS_HPP_INCLUDED__
5 
6 #include <cstddef>
7 #include <cstdlib>
8 
9 #include "atomic_counter.hpp"
10 #include "msg.hpp"
11 #include "err.hpp"
12 
13 namespace zmq
14 {
15 // Static buffer policy.
17 {
18  public:
19  explicit c_single_allocator (std::size_t bufsize_) :
20  _buf_size (bufsize_),
21  _buf (static_cast<unsigned char *> (std::malloc (_buf_size)))
22  {
24  }
25 
26  ~c_single_allocator () { std::free (_buf); }
27 
28  unsigned char *allocate () { return _buf; }
29 
30  void deallocate () {}
31 
32  std::size_t size () const { return _buf_size; }
33 
34  // This buffer is fixed, size must not be changed
35  void resize (std::size_t new_size_) { LIBZMQ_UNUSED (new_size_); }
36 
37  private:
38  std::size_t _buf_size;
39  unsigned char *_buf;
40 
42 };
43 
44 // This allocator allocates a reference counted buffer which is used by v2_decoder_t
45 // to use zero-copy msg::init_data to create messages with memory from this buffer as
46 // data storage.
47 //
48 // The buffer is allocated with a reference count of 1 to make sure that is is alive while
49 // decoding messages. Otherwise, it is possible that e.g. the first message increases the count
50 // from zero to one, gets passed to the user application, processed in the user thread and deleted
51 // which would then deallocate the buffer. The drawback is that the buffer may be allocated longer
52 // than necessary because it is only deleted when allocate is called the next time.
54 {
55  public:
56  explicit shared_message_memory_allocator (std::size_t bufsize_);
57 
58  // Create an allocator for a maximum number of messages
59  shared_message_memory_allocator (std::size_t bufsize_,
60  std::size_t max_messages_);
61 
63 
64  // Allocate a new buffer
65  //
66  // This releases the current buffer to be bound to the lifetime of the messages
67  // created on this buffer.
68  unsigned char *allocate ();
69 
70  // force deallocation of buffer.
71  void deallocate ();
72 
73  // Give up ownership of the buffer. The buffer's lifetime is now coupled to
74  // the messages constructed on top of it.
75  unsigned char *release ();
76 
77  void inc_ref ();
78 
79  static void call_dec_ref (void *, void *hint_);
80 
81  std::size_t size () const;
82 
83  // Return pointer to the first message data byte.
84  unsigned char *data ();
85 
86  // Return pointer to the first byte of the buffer.
87  unsigned char *buffer () { return _buf; }
88 
89  void resize (std::size_t new_size_) { _buf_size = new_size_; }
90 
92 
94 
95  private:
96  void clear ();
97 
98  unsigned char *_buf;
99  std::size_t _buf_size;
100  const std::size_t _max_size;
102  std::size_t _max_counters;
103 };
104 }
105 
106 #endif
zmq::shared_message_memory_allocator::deallocate
void deallocate()
Definition: decoder_allocators.cpp:74
zmq::c_single_allocator::size
std::size_t size() const
Definition: decoder_allocators.hpp:32
zmq::shared_message_memory_allocator::_buf_size
std::size_t _buf_size
Definition: decoder_allocators.hpp:99
zmq::shared_message_memory_allocator
Definition: decoder_allocators.hpp:53
zmq::shared_message_memory_allocator::call_dec_ref
static void call_dec_ref(void *, void *hint_)
Definition: decoder_allocators.cpp:103
atomic_counter.hpp
zmq::shared_message_memory_allocator::_msg_content
zmq::msg_t::content_t * _msg_content
Definition: decoder_allocators.hpp:101
zmq::shared_message_memory_allocator::~shared_message_memory_allocator
~shared_message_memory_allocator()
Definition: decoder_allocators.cpp:28
zmq::shared_message_memory_allocator::_max_counters
std::size_t _max_counters
Definition: decoder_allocators.hpp:102
zmq::shared_message_memory_allocator::_max_size
const std::size_t _max_size
Definition: decoder_allocators.hpp:100
zmq::shared_message_memory_allocator::data
unsigned char * data()
Definition: decoder_allocators.cpp:122
zmq::shared_message_memory_allocator::resize
void resize(std::size_t new_size_)
Definition: decoder_allocators.hpp:89
zmq
Definition: zmq.hpp:229
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::c_single_allocator
Definition: decoder_allocators.hpp:16
zmq::c_single_allocator::_buf_size
std::size_t _buf_size
Definition: decoder_allocators.hpp:38
LIBZMQ_UNUSED
#define LIBZMQ_UNUSED(object)
Definition: macros.hpp:6
zmq::shared_message_memory_allocator::inc_ref
void inc_ref()
Definition: decoder_allocators.cpp:98
ZMQ_NON_COPYABLE_NOR_MOVABLE
#define ZMQ_NON_COPYABLE_NOR_MOVABLE(classname)
Definition: macros.hpp:58
zmq::c_single_allocator::_buf
unsigned char * _buf
Definition: decoder_allocators.hpp:39
zmq::shared_message_memory_allocator::buffer
unsigned char * buffer()
Definition: decoder_allocators.hpp:87
zmq::msg_t::content_t
Definition: msg.hpp:43
msg.hpp
zmq::shared_message_memory_allocator::advance_content
void advance_content()
Definition: decoder_allocators.hpp:93
zmq::c_single_allocator::~c_single_allocator
~c_single_allocator()
Definition: decoder_allocators.hpp:26
std
err.hpp
zmq::c_single_allocator::allocate
unsigned char * allocate()
Definition: decoder_allocators.hpp:28
zmq::shared_message_memory_allocator::size
std::size_t size() const
Definition: decoder_allocators.cpp:117
zmq::c_single_allocator::c_single_allocator
c_single_allocator(std::size_t bufsize_)
Definition: decoder_allocators.hpp:19
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::c_single_allocator::deallocate
void deallocate()
Definition: decoder_allocators.hpp:30
zmq::shared_message_memory_allocator::provide_content
zmq::msg_t::content_t * provide_content()
Definition: decoder_allocators.hpp:91
zmq::shared_message_memory_allocator::clear
void clear()
Definition: decoder_allocators.cpp:91
zmq::c_single_allocator::resize
void resize(std::size_t new_size_)
Definition: decoder_allocators.hpp:35
zmq::shared_message_memory_allocator::_buf
unsigned char * _buf
Definition: decoder_allocators.hpp:98


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:49