2015-06-14 19:00:52 +02:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
|
2015-06-14 19:00:52 +02:00
|
|
|
|
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
|
|
|
|
|
|
|
libzmq is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License (LGPL) as published
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
As a special exception, the Contributors give you permission to link
|
|
|
|
this library with independent modules to produce an executable,
|
|
|
|
regardless of the license terms of these independent modules, and to
|
|
|
|
copy and distribute the resulting executable under terms of your choice,
|
|
|
|
provided that you also meet, for each linked independent module, the
|
|
|
|
terms and conditions of the license of that module. An independent
|
|
|
|
module is a module which is not derived from or based on this library.
|
|
|
|
If you modify this library, you must extend this exception to your
|
|
|
|
version of the library.
|
|
|
|
|
|
|
|
libzmq is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-07-06 01:01:52 +02:00
|
|
|
#ifndef __ZMQ_DECODER_ALLOCATORS_HPP_INCLUDED__
|
|
|
|
#define __ZMQ_DECODER_ALLOCATORS_HPP_INCLUDED__
|
2015-06-14 19:00:52 +02:00
|
|
|
|
2015-07-06 01:01:52 +02:00
|
|
|
#include <cstddef>
|
2015-07-05 23:19:41 +02:00
|
|
|
#include <cstdlib>
|
2015-06-14 19:00:52 +02:00
|
|
|
|
|
|
|
#include "atomic_counter.hpp"
|
2016-01-29 09:45:44 +01:00
|
|
|
#include "msg.hpp"
|
2015-07-06 01:01:52 +02:00
|
|
|
#include "err.hpp"
|
2015-06-14 19:00:52 +02:00
|
|
|
|
|
|
|
namespace zmq
|
|
|
|
{
|
|
|
|
// Static buffer policy.
|
|
|
|
class c_single_allocator
|
|
|
|
{
|
|
|
|
public:
|
2015-07-06 01:01:52 +02:00
|
|
|
explicit c_single_allocator (std::size_t bufsize_) :
|
2015-06-14 19:00:52 +02:00
|
|
|
bufsize(bufsize_),
|
2015-07-06 01:01:52 +02:00
|
|
|
buf(static_cast <unsigned char*> (std::malloc (bufsize)))
|
2015-06-14 19:00:52 +02:00
|
|
|
{
|
|
|
|
alloc_assert (buf);
|
|
|
|
}
|
|
|
|
|
2015-07-06 01:01:52 +02:00
|
|
|
~c_single_allocator ()
|
2015-06-14 19:00:52 +02:00
|
|
|
{
|
2015-07-06 01:01:52 +02:00
|
|
|
std::free (buf);
|
2015-06-14 19:00:52 +02:00
|
|
|
}
|
|
|
|
|
2015-07-06 01:01:52 +02:00
|
|
|
unsigned char* allocate ()
|
2015-06-14 19:00:52 +02:00
|
|
|
{
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2015-07-06 01:01:52 +02:00
|
|
|
void deallocate ()
|
2015-06-14 19:00:52 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-07-06 01:01:52 +02:00
|
|
|
std::size_t size () const
|
2015-06-14 19:00:52 +02:00
|
|
|
{
|
|
|
|
return bufsize;
|
|
|
|
}
|
|
|
|
|
2015-07-06 01:01:52 +02:00
|
|
|
void resize (std::size_t new_size)
|
2015-06-14 19:00:52 +02:00
|
|
|
{
|
|
|
|
bufsize = new_size;
|
|
|
|
}
|
|
|
|
private:
|
2015-07-06 01:01:52 +02:00
|
|
|
std::size_t bufsize;
|
2015-06-14 19:00:52 +02:00
|
|
|
unsigned char* buf;
|
|
|
|
|
2015-07-06 01:01:52 +02:00
|
|
|
c_single_allocator (c_single_allocator const&);
|
|
|
|
c_single_allocator& operator = (c_single_allocator const&);
|
2015-06-14 19:00:52 +02:00
|
|
|
};
|
|
|
|
|
2015-09-06 18:46:32 +02:00
|
|
|
// This allocator allocates a reference counted buffer which is used by v2_decoder_t
|
2015-06-14 19:00:52 +02:00
|
|
|
// to use zero-copy msg::init_data to create messages with memory from this buffer as
|
|
|
|
// data storage.
|
|
|
|
//
|
|
|
|
// The buffer is allocated with a reference count of 1 to make sure that is is alive while
|
|
|
|
// decoding messages. Otherwise, it is possible that e.g. the first message increases the count
|
|
|
|
// from zero to one, gets passed to the user application, processed in the user thread and deleted
|
|
|
|
// which would then deallocate the buffer. The drawback is that the buffer may be allocated longer
|
|
|
|
// than necessary because it is only deleted when allocate is called the next time.
|
|
|
|
class shared_message_memory_allocator
|
|
|
|
{
|
|
|
|
public:
|
2015-07-06 01:01:52 +02:00
|
|
|
explicit shared_message_memory_allocator (std::size_t bufsize_);
|
2015-06-14 19:00:52 +02:00
|
|
|
|
|
|
|
// Create an allocator for a maximum number of messages
|
2015-07-06 01:01:52 +02:00
|
|
|
shared_message_memory_allocator (std::size_t bufsize_, std::size_t maxMessages);
|
2015-06-14 19:00:52 +02:00
|
|
|
|
2015-07-06 01:01:52 +02:00
|
|
|
~shared_message_memory_allocator ();
|
2015-06-14 19:00:52 +02:00
|
|
|
|
|
|
|
// Allocate a new buffer
|
|
|
|
//
|
|
|
|
// This releases the current buffer to be bound to the lifetime of the messages
|
2015-09-06 18:46:32 +02:00
|
|
|
// created on this buffer.
|
2015-07-06 01:01:52 +02:00
|
|
|
unsigned char* allocate ();
|
2015-06-14 19:00:52 +02:00
|
|
|
|
|
|
|
// force deallocation of buffer.
|
2015-07-06 01:01:52 +02:00
|
|
|
void deallocate ();
|
2015-06-14 19:00:52 +02:00
|
|
|
|
|
|
|
// Give up ownership of the buffer. The buffer's lifetime is now coupled to
|
|
|
|
// the messages constructed on top of it.
|
2015-07-06 01:01:52 +02:00
|
|
|
unsigned char* release ();
|
2015-06-14 19:00:52 +02:00
|
|
|
|
2015-07-06 01:01:52 +02:00
|
|
|
void inc_ref ();
|
2015-06-14 19:00:52 +02:00
|
|
|
|
2015-07-06 01:01:52 +02:00
|
|
|
static void call_dec_ref (void*, void* buffer);
|
2015-06-14 19:00:52 +02:00
|
|
|
|
2015-07-06 01:01:52 +02:00
|
|
|
std::size_t size () const;
|
2015-06-14 19:00:52 +02:00
|
|
|
|
|
|
|
// Return pointer to the first message data byte.
|
2015-07-06 01:01:52 +02:00
|
|
|
unsigned char* data ();
|
2015-06-14 19:00:52 +02:00
|
|
|
|
|
|
|
// Return pointer to the first byte of the buffer.
|
2015-07-06 01:01:52 +02:00
|
|
|
unsigned char* buffer ()
|
2015-06-14 19:00:52 +02:00
|
|
|
{
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2015-07-06 01:01:52 +02:00
|
|
|
void resize (std::size_t new_size)
|
2015-06-14 19:00:52 +02:00
|
|
|
{
|
|
|
|
bufsize = new_size;
|
|
|
|
}
|
|
|
|
|
2016-01-29 09:45:44 +01:00
|
|
|
zmq::msg_t::content_t* provide_content ()
|
2015-06-14 19:00:52 +02:00
|
|
|
{
|
2016-01-29 09:45:44 +01:00
|
|
|
return msg_content;
|
2015-07-22 15:19:22 +02:00
|
|
|
}
|
|
|
|
|
2016-01-29 09:45:44 +01:00
|
|
|
void advance_content ()
|
2015-07-22 15:19:22 +02:00
|
|
|
{
|
2016-01-29 09:45:44 +01:00
|
|
|
msg_content++;
|
2015-06-14 19:00:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned char* buf;
|
2015-07-06 01:01:52 +02:00
|
|
|
std::size_t bufsize;
|
|
|
|
std::size_t max_size;
|
2016-01-29 09:45:44 +01:00
|
|
|
zmq::msg_t::content_t* msg_content;
|
2015-07-06 01:01:52 +02:00
|
|
|
std::size_t maxCounters;
|
2015-06-14 19:00:52 +02:00
|
|
|
};
|
|
|
|
}
|
2015-07-06 01:01:52 +02:00
|
|
|
|
|
|
|
#endif
|