Line data Source code
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:
46 0 : explicit c_single_allocator (std::size_t bufsize_) :
47 : bufsize(bufsize_),
48 0 : buf(static_cast <unsigned char*> (std::malloc (bufsize)))
49 : {
50 0 : alloc_assert (buf);
51 0 : }
52 :
53 : ~c_single_allocator ()
54 : {
55 0 : std::free (buf);
56 : }
57 :
58 : unsigned char* allocate ()
59 : {
60 : return buf;
61 : }
62 :
63 : void deallocate ()
64 : {
65 : }
66 :
67 : std::size_t size () const
68 : {
69 : return bufsize;
70 : }
71 :
72 : void resize (std::size_t new_size)
73 : {
74 0 : bufsize = new_size;
75 : }
76 : private:
77 : std::size_t bufsize;
78 : unsigned char* buf;
79 :
80 : c_single_allocator (c_single_allocator const&);
81 : c_single_allocator& operator = (c_single_allocator const&);
82 : };
83 :
84 : // This allocator allocates a reference counted buffer which is used by v2_decoder_t
85 : // to use zero-copy msg::init_data to create messages with memory from this buffer as
86 : // data storage.
87 : //
88 : // The buffer is allocated with a reference count of 1 to make sure that is is alive while
89 : // decoding messages. Otherwise, it is possible that e.g. the first message increases the count
90 : // from zero to one, gets passed to the user application, processed in the user thread and deleted
91 : // which would then deallocate the buffer. The drawback is that the buffer may be allocated longer
92 : // than necessary because it is only deleted when allocate is called the next time.
93 : class shared_message_memory_allocator
94 : {
95 : public:
96 : explicit shared_message_memory_allocator (std::size_t bufsize_);
97 :
98 : // Create an allocator for a maximum number of messages
99 : shared_message_memory_allocator (std::size_t bufsize_, std::size_t maxMessages);
100 :
101 : ~shared_message_memory_allocator ();
102 :
103 : // Allocate a new buffer
104 : //
105 : // This releases the current buffer to be bound to the lifetime of the messages
106 : // created on this buffer.
107 : unsigned char* allocate ();
108 :
109 : // force deallocation of buffer.
110 : void deallocate ();
111 :
112 : // Give up ownership of the buffer. The buffer's lifetime is now coupled to
113 : // the messages constructed on top of it.
114 : unsigned char* release ();
115 :
116 : void inc_ref ();
117 :
118 : static void call_dec_ref (void*, void* buffer);
119 :
120 : std::size_t size () const;
121 :
122 : // Return pointer to the first message data byte.
123 : unsigned char* data ();
124 :
125 : // Return pointer to the first byte of the buffer.
126 : unsigned char* buffer ()
127 : {
128 : return buf;
129 : }
130 :
131 : void resize (std::size_t new_size)
132 : {
133 5427 : bufsize = new_size;
134 : }
135 :
136 : zmq::msg_t::content_t* provide_content ()
137 : {
138 : return msg_content;
139 : }
140 :
141 : void advance_content ()
142 : {
143 1233 : msg_content++;
144 : }
145 :
146 : private:
147 : unsigned char* buf;
148 : std::size_t bufsize;
149 : std::size_t max_size;
150 : zmq::msg_t::content_t* msg_content;
151 : std::size_t maxCounters;
152 : };
153 : }
154 :
155 : #endif
|