decoder_allocators.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #ifndef __ZMQ_DECODER_ALLOCATORS_HPP_INCLUDED__
  25. #define __ZMQ_DECODER_ALLOCATORS_HPP_INCLUDED__
  26. #include <cstddef>
  27. #include <cstdlib>
  28. #include "atomic_counter.hpp"
  29. #include "msg.hpp"
  30. #include "err.hpp"
  31. namespace zmq
  32. {
  33. // Static buffer policy.
  34. class c_single_allocator
  35. {
  36. public:
  37. explicit c_single_allocator (std::size_t bufsize_) :
  38. _buf_size (bufsize_),
  39. _buf (static_cast<unsigned char *> (std::malloc (_buf_size)))
  40. {
  41. alloc_assert (_buf);
  42. }
  43. ~c_single_allocator () { std::free (_buf); }
  44. unsigned char *allocate () { return _buf; }
  45. void deallocate () {}
  46. std::size_t size () const { return _buf_size; }
  47. // This buffer is fixed, size must not be changed
  48. void resize (std::size_t new_size_) { LIBZMQ_UNUSED (new_size_); }
  49. private:
  50. std::size_t _buf_size;
  51. unsigned char *_buf;
  52. ZMQ_NON_COPYABLE_NOR_MOVABLE (c_single_allocator)
  53. };
  54. // This allocator allocates a reference counted buffer which is used by v2_decoder_t
  55. // to use zero-copy msg::init_data to create messages with memory from this buffer as
  56. // data storage.
  57. //
  58. // The buffer is allocated with a reference count of 1 to make sure that is is alive while
  59. // decoding messages. Otherwise, it is possible that e.g. the first message increases the count
  60. // from zero to one, gets passed to the user application, processed in the user thread and deleted
  61. // which would then deallocate the buffer. The drawback is that the buffer may be allocated longer
  62. // than necessary because it is only deleted when allocate is called the next time.
  63. class shared_message_memory_allocator
  64. {
  65. public:
  66. explicit shared_message_memory_allocator (std::size_t bufsize_);
  67. // Create an allocator for a maximum number of messages
  68. shared_message_memory_allocator (std::size_t bufsize_,
  69. std::size_t max_messages_);
  70. ~shared_message_memory_allocator ();
  71. // Allocate a new buffer
  72. //
  73. // This releases the current buffer to be bound to the lifetime of the messages
  74. // created on this buffer.
  75. unsigned char *allocate ();
  76. // force deallocation of buffer.
  77. void deallocate ();
  78. // Give up ownership of the buffer. The buffer's lifetime is now coupled to
  79. // the messages constructed on top of it.
  80. unsigned char *release ();
  81. void inc_ref ();
  82. static void call_dec_ref (void *, void *hint_);
  83. std::size_t size () const;
  84. // Return pointer to the first message data byte.
  85. unsigned char *data ();
  86. // Return pointer to the first byte of the buffer.
  87. unsigned char *buffer () { return _buf; }
  88. void resize (std::size_t new_size_) { _buf_size = new_size_; }
  89. zmq::msg_t::content_t *provide_content () { return _msg_content; }
  90. void advance_content () { _msg_content++; }
  91. private:
  92. void clear ();
  93. unsigned char *_buf;
  94. std::size_t _buf_size;
  95. const std::size_t _max_size;
  96. zmq::msg_t::content_t *_msg_content;
  97. std::size_t _max_counters;
  98. };
  99. }
  100. #endif