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