Commit 5b4b8a06 authored by Thomas Köppe's avatar Thomas Köppe

[decoder*] Style fixes for consistency

parent e83bad14
...@@ -30,16 +30,15 @@ ...@@ -30,16 +30,15 @@
#ifndef __ZMQ_DECODER_HPP_INCLUDED__ #ifndef __ZMQ_DECODER_HPP_INCLUDED__
#define __ZMQ_DECODER_HPP_INCLUDED__ #define __ZMQ_DECODER_HPP_INCLUDED__
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm> #include <algorithm>
#include <cstddef>
#include <cstdlib>
#include "decoder_allocators.hpp"
#include "err.hpp" #include "err.hpp"
#include "msg.hpp"
#include "i_decoder.hpp" #include "i_decoder.hpp"
#include "msg.hpp"
#include "stdint.hpp" #include "stdint.hpp"
#include "decoder_allocators.hpp"
namespace zmq namespace zmq
{ {
...@@ -60,26 +59,26 @@ namespace zmq ...@@ -60,26 +59,26 @@ namespace zmq
{ {
public: public:
inline decoder_base_t (A* allocator_) : explicit decoder_base_t (A *allocator_) :
next (NULL), next (NULL),
read_pos (NULL), read_pos (NULL),
to_read (0), to_read (0),
allocator( allocator_ ) allocator(allocator_)
{ {
buf = allocator->allocate(); buf = allocator->allocate ();
} }
// The destructor doesn't have to be virtual. It is mad virtual // The destructor doesn't have to be virtual. It is made virtual
// just to keep ICC and code checking tools from complaining. // just to keep ICC and code checking tools from complaining.
inline virtual ~decoder_base_t () virtual ~decoder_base_t ()
{ {
allocator->deallocate(); allocator->deallocate ();
} }
// Returns a buffer to be filled with binary data. // Returns a buffer to be filled with binary data.
inline void get_buffer (unsigned char **data_, size_t *size_) void get_buffer (unsigned char **data_, std::size_t *size_)
{ {
buf = allocator->allocate(); buf = allocator->allocate ();
// If we are expected to read large message, we'll opt for zero- // If we are expected to read large message, we'll opt for zero-
// copy, i.e. we'll ask caller to fill the data directly to the // copy, i.e. we'll ask caller to fill the data directly to the
...@@ -89,14 +88,14 @@ namespace zmq ...@@ -89,14 +88,14 @@ namespace zmq
// As a consequence, large messages being received won't block // As a consequence, large messages being received won't block
// other engines running in the same I/O thread for excessive // other engines running in the same I/O thread for excessive
// amounts of time. // amounts of time.
if (to_read >= allocator->size()) { if (to_read >= allocator->size ()) {
*data_ = read_pos; *data_ = read_pos;
*size_ = to_read; *size_ = to_read;
return; return;
} }
*data_ = buf; *data_ = buf;
*size_ = allocator->size(); *size_ = allocator->size ();
} }
// Processes the data in the buffer previously allocated using // Processes the data in the buffer previously allocated using
...@@ -105,8 +104,8 @@ namespace zmq ...@@ -105,8 +104,8 @@ namespace zmq
// whole message was decoded or 0 when more data is required. // whole message was decoded or 0 when more data is required.
// On error, -1 is returned and errno set accordingly. // On error, -1 is returned and errno set accordingly.
// Number of bytes processed is returned in byts_used_. // Number of bytes processed is returned in byts_used_.
inline int decode (const unsigned char *data_, size_t size_, int decode (const unsigned char *data_, std::size_t size_,
size_t &bytes_used_) std::size_t &bytes_used_)
{ {
bytes_used_ = 0; bytes_used_ = 0;
...@@ -120,7 +119,7 @@ namespace zmq ...@@ -120,7 +119,7 @@ namespace zmq
bytes_used_ = size_; bytes_used_ = size_;
while (!to_read) { while (!to_read) {
const int rc = (static_cast <T*> (this)->*next) (data_ + bytes_used_); const int rc = (static_cast <T *> (this)->*next) (data_ + bytes_used_);
if (rc != 0) if (rc != 0)
return rc; return rc;
} }
...@@ -129,11 +128,11 @@ namespace zmq ...@@ -129,11 +128,11 @@ namespace zmq
while (bytes_used_ < size_) { while (bytes_used_ < size_) {
// Copy the data from buffer to the message. // Copy the data from buffer to the message.
const size_t to_copy = std::min (to_read, size_ - bytes_used_); const std::size_t to_copy = std::min (to_read, size_ - bytes_used_);
// only copy when the destination address is different from the // only copy when the destination address is different from the
// current address in the buffer // current address in the buffer
if (read_pos != data_ + bytes_used_) { if (read_pos != data_ + bytes_used_) {
memcpy(read_pos, data_ + bytes_used_, to_copy); std::memcpy (read_pos, data_ + bytes_used_, to_copy);
} }
read_pos += to_copy; read_pos += to_copy;
...@@ -143,7 +142,7 @@ namespace zmq ...@@ -143,7 +142,7 @@ namespace zmq
// If none is available, return. // If none is available, return.
while (to_read == 0) { while (to_read == 0) {
// pass current address in the buffer // pass current address in the buffer
const int rc = (static_cast <T*> (this)->*next) (data_ + bytes_used_); const int rc = (static_cast <T *> (this)->*next) (data_ + bytes_used_);
if (rc != 0) if (rc != 0)
return rc; return rc;
} }
...@@ -152,22 +151,22 @@ namespace zmq ...@@ -152,22 +151,22 @@ namespace zmq
return 0; return 0;
} }
virtual void resize_buffer(size_t new_size) virtual void resize_buffer (std::size_t new_size)
{ {
allocator->resize(new_size); allocator->resize (new_size);
} }
protected: protected:
// Prototype of state machine action. Action should return false if // Prototype of state machine action. Action should return false if
// it is unable to push the data to the system. // it is unable to push the data to the system.
typedef int (T::*step_t) (unsigned char const*); typedef int (T:: *step_t) (unsigned char const *);
// This function should be called from derived class to read data // This function should be called from derived class to read data
// from the buffer and schedule next state machine action. // from the buffer and schedule next state machine action.
inline void next_step (void *read_pos_, size_t to_read_, step_t next_) void next_step (void *read_pos_, std::size_t to_read_, step_t next_)
{ {
read_pos = (unsigned char*) read_pos_; read_pos = static_cast <unsigned char*> (read_pos_);
to_read = to_read_; to_read = to_read_;
next = next_; next = next_;
} }
...@@ -183,16 +182,15 @@ namespace zmq ...@@ -183,16 +182,15 @@ namespace zmq
unsigned char *read_pos; unsigned char *read_pos;
// How much data to read before taking next step. // How much data to read before taking next step.
size_t to_read; std::size_t to_read;
// The duffer for data to decode. // The duffer for data to decode.
A* allocator; A *allocator;
unsigned char* buf; unsigned char *buf;
decoder_base_t (const decoder_base_t&); decoder_base_t (const decoder_base_t &);
const decoder_base_t &operator = (const decoder_base_t&); const decoder_base_t &operator = (const decoder_base_t &);
}; };
} }
#endif #endif
...@@ -33,79 +33,76 @@ ...@@ -33,79 +33,76 @@
#include "msg.hpp" #include "msg.hpp"
zmq::shared_message_memory_allocator::shared_message_memory_allocator(size_t bufsize_): zmq::shared_message_memory_allocator::shared_message_memory_allocator (std::size_t bufsize_) :
buf(NULL), buf(NULL),
bufsize( 0 ), bufsize(0),
max_size( bufsize_ ), max_size(bufsize_),
msg_refcnt( NULL ), msg_refcnt(NULL),
maxCounters( std::ceil( static_cast<double>(max_size) / static_cast<double>(msg_t::max_vsm_size)) ) maxCounters (std::ceil (static_cast <double> (max_size) / static_cast <double> (msg_t::max_vsm_size)))
{ {
} }
zmq::shared_message_memory_allocator::shared_message_memory_allocator(size_t bufsize_, size_t maxMessages): zmq::shared_message_memory_allocator::shared_message_memory_allocator (std::size_t bufsize_, std::size_t maxMessages) :
buf(NULL), buf(NULL),
bufsize( 0 ), bufsize(0),
max_size( bufsize_ ), max_size(bufsize_),
msg_refcnt( NULL ), msg_refcnt(NULL),
maxCounters( maxMessages ) maxCounters(maxMessages)
{ {
} }
zmq::shared_message_memory_allocator::~shared_message_memory_allocator() zmq::shared_message_memory_allocator::~shared_message_memory_allocator ()
{ {
deallocate(); deallocate();
} }
unsigned char* zmq::shared_message_memory_allocator::allocate() unsigned char* zmq::shared_message_memory_allocator::allocate ()
{ {
if (buf) if (buf) {
{
// release reference count to couple lifetime to messages // release reference count to couple lifetime to messages
zmq::atomic_counter_t *c = reinterpret_cast<zmq::atomic_counter_t *>(buf); zmq::atomic_counter_t* c = reinterpret_cast<zmq::atomic_counter_t* >(buf);
// if refcnt drops to 0, there are no message using the buffer // if refcnt drops to 0, there are no message using the buffer
// because either all messages have been closed or only vsm-messages // because either all messages have been closed or only vsm-messages
// were created // were created
if (c->sub(1)) { if (c->sub (1)) {
// buffer is still in use as message data. "Release" it and create a new one // buffer is still in use as message data. "Release" it and create a new one
// release pointer because we are going to create a new buffer // release pointer because we are going to create a new buffer
release(); release ();
} }
} }
// if buf != NULL it is not used by any message so we can re-use it for the next run // if buf != NULL it is not used by any message so we can re-use it for the next run
if (!buf) { if (!buf) {
// allocate memory for reference counters together with reception buffer // allocate memory for reference counters together with reception buffer
size_t const allocationsize = max_size + sizeof(zmq::atomic_counter_t) + maxCounters * sizeof(zmq::atomic_counter_t); std::size_t const allocationsize =
max_size + sizeof (zmq::atomic_counter_t) +
maxCounters * sizeof (zmq::atomic_counter_t);
buf = static_cast<unsigned char *>( malloc(allocationsize) ); buf = static_cast <unsigned char *> (std::malloc (allocationsize));
alloc_assert (buf); alloc_assert (buf);
new(buf) atomic_counter_t(1); new (buf) atomic_counter_t (1);
} } else {
else
{
// release reference count to couple lifetime to messages // release reference count to couple lifetime to messages
zmq::atomic_counter_t *c = reinterpret_cast<zmq::atomic_counter_t *>(buf); zmq::atomic_counter_t *c = reinterpret_cast <zmq::atomic_counter_t *> (buf);
c->set(1); c->set (1);
} }
bufsize = max_size; bufsize = max_size;
msg_refcnt = reinterpret_cast<zmq::atomic_counter_t*>( buf + sizeof(atomic_counter_t) + max_size ); msg_refcnt = reinterpret_cast <zmq::atomic_counter_t*> (buf + sizeof (atomic_counter_t) + max_size);
return buf + sizeof( zmq::atomic_counter_t); return buf + sizeof (zmq::atomic_counter_t);
} }
void zmq::shared_message_memory_allocator::deallocate() void zmq::shared_message_memory_allocator::deallocate ()
{ {
std::free(buf); std::free (buf);
buf = NULL; buf = NULL;
bufsize = 0; bufsize = 0;
msg_refcnt = NULL; msg_refcnt = NULL;
} }
unsigned char* zmq::shared_message_memory_allocator::release() unsigned char* zmq::shared_message_memory_allocator::release ()
{ {
unsigned char* b = buf; unsigned char* b = buf;
buf = NULL; buf = NULL;
...@@ -115,30 +112,31 @@ unsigned char* zmq::shared_message_memory_allocator::release() ...@@ -115,30 +112,31 @@ unsigned char* zmq::shared_message_memory_allocator::release()
return b; return b;
} }
void zmq::shared_message_memory_allocator::inc_ref() void zmq::shared_message_memory_allocator::inc_ref ()
{ {
(reinterpret_cast<zmq::atomic_counter_t*>(buf))->add(1); (reinterpret_cast <zmq::atomic_counter_t*> (buf))->add (1);
} }
void zmq::shared_message_memory_allocator::call_dec_ref(void*, void* hint) { void zmq::shared_message_memory_allocator::call_dec_ref(void*, void* hint)
zmq_assert( hint ); {
unsigned char* buf = static_cast<unsigned char*>(hint); zmq_assert (hint);
zmq::atomic_counter_t *c = reinterpret_cast<zmq::atomic_counter_t *>(buf); unsigned char* buf = static_cast <unsigned char*> (hint);
zmq::atomic_counter_t* c = reinterpret_cast <zmq::atomic_counter_t*> (buf);
if (!c->sub(1)) { if (!c->sub (1)) {
c->~atomic_counter_t(); c->~atomic_counter_t ();
free(buf); std::free (buf);
buf = NULL; buf = NULL;
} }
} }
size_t zmq::shared_message_memory_allocator::size() const std::size_t zmq::shared_message_memory_allocator::size () const
{ {
return bufsize; return bufsize;
} }
unsigned char* zmq::shared_message_memory_allocator::data() unsigned char* zmq::shared_message_memory_allocator::data ()
{ {
return buf + sizeof(zmq::atomic_counter_t); return buf + sizeof (zmq::atomic_counter_t);
} }
...@@ -27,13 +27,14 @@ ...@@ -27,13 +27,14 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef ZEROMQ_DECODER_ALLOCATORS_HPP #ifndef __ZMQ_DECODER_ALLOCATORS_HPP_INCLUDED__
#define ZEROMQ_DECODER_ALLOCATORS_HPP #define __ZMQ_DECODER_ALLOCATORS_HPP_INCLUDED__
#include <cstddef>
#include <cstdlib> #include <cstdlib>
#include "err.hpp"
#include "atomic_counter.hpp" #include "atomic_counter.hpp"
#include "err.hpp"
namespace zmq namespace zmq
{ {
...@@ -41,43 +42,42 @@ namespace zmq ...@@ -41,43 +42,42 @@ namespace zmq
class c_single_allocator class c_single_allocator
{ {
public: public:
explicit c_single_allocator(size_t bufsize_): explicit c_single_allocator (std::size_t bufsize_) :
bufsize(bufsize_), bufsize(bufsize_),
buf(static_cast<unsigned char*>( malloc (bufsize) )) buf(static_cast <unsigned char*> (std::malloc (bufsize)))
{ {
alloc_assert (buf); alloc_assert (buf);
} }
~c_single_allocator() ~c_single_allocator ()
{ {
std::free(buf); std::free (buf);
} }
unsigned char* allocate() unsigned char* allocate ()
{ {
return buf; return buf;
} }
void deallocate() void deallocate ()
{ {
} }
size_t size() const std::size_t size () const
{ {
return bufsize; return bufsize;
} }
void resize(size_t new_size) void resize (std::size_t new_size)
{ {
bufsize = new_size; bufsize = new_size;
} }
private: private:
size_t bufsize; std::size_t bufsize;
unsigned char* buf; unsigned char* buf;
c_single_allocator( c_single_allocator const& ); c_single_allocator (c_single_allocator const&);
c_single_allocator& operator=(c_single_allocator const&); c_single_allocator& operator = (c_single_allocator const&);
}; };
// This allocater allocates a reference counted buffer which is used by v2_decoder_t // This allocater allocates a reference counted buffer which is used by v2_decoder_t
...@@ -92,58 +92,58 @@ namespace zmq ...@@ -92,58 +92,58 @@ namespace zmq
class shared_message_memory_allocator class shared_message_memory_allocator
{ {
public: public:
explicit shared_message_memory_allocator(size_t bufsize_); explicit shared_message_memory_allocator (std::size_t bufsize_);
// Create an allocator for a maximum number of messages // Create an allocator for a maximum number of messages
shared_message_memory_allocator(size_t bufsize_, size_t maxMessages); shared_message_memory_allocator (std::size_t bufsize_, std::size_t maxMessages);
~shared_message_memory_allocator(); ~shared_message_memory_allocator ();
// Allocate a new buffer // Allocate a new buffer
// //
// This releases the current buffer to be bound to the lifetime of the messages // This releases the current buffer to be bound to the lifetime of the messages
// created on this bufer. // created on this bufer.
unsigned char* allocate(); unsigned char* allocate ();
// force deallocation of buffer. // force deallocation of buffer.
void deallocate(); void deallocate ();
// Give up ownership of the buffer. The buffer's lifetime is now coupled to // Give up ownership of the buffer. The buffer's lifetime is now coupled to
// the messages constructed on top of it. // the messages constructed on top of it.
unsigned char* release(); unsigned char* release ();
void inc_ref(); void inc_ref ();
static void call_dec_ref(void*, void* buffer); static void call_dec_ref (void*, void* buffer);
size_t size() const; std::size_t size () const;
// Return pointer to the first message data byte. // Return pointer to the first message data byte.
unsigned char* data(); unsigned char* data ();
// Return pointer to the first byte of the buffer. // Return pointer to the first byte of the buffer.
unsigned char* buffer() unsigned char* buffer ()
{ {
return buf; return buf;
} }
void resize(size_t new_size) void resize (std::size_t new_size)
{ {
bufsize = new_size; bufsize = new_size;
} }
// zmq::atomic_counter_t* create_refcnt ()
zmq::atomic_counter_t* create_refcnt()
{ {
return msg_refcnt++; return msg_refcnt++;
} }
private: private:
unsigned char* buf; unsigned char* buf;
size_t bufsize; std::size_t bufsize;
size_t max_size; std::size_t max_size;
zmq::atomic_counter_t* msg_refcnt; zmq::atomic_counter_t* msg_refcnt;
size_t maxCounters; std::size_t maxCounters;
}; };
} }
#endif //ZEROMQ_DECODER_ALLOCATORS_HPP
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment