Merge pull request #1468 from tkoeppe/master

[decoder*] Style fixes for consistency
This commit is contained in:
Pieter Hintjens 2015-07-06 01:12:58 +02:00
commit d60040ddea
3 changed files with 113 additions and 117 deletions

View File

@ -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 "err.hpp"
#include "msg.hpp"
#include "i_decoder.hpp"
#include "stdint.hpp"
#include "decoder_allocators.hpp" #include "decoder_allocators.hpp"
#include "err.hpp"
#include "i_decoder.hpp"
#include "msg.hpp"
#include "stdint.hpp"
namespace zmq namespace zmq
{ {
@ -60,7 +59,7 @@ 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),
@ -69,15 +68,15 @@ namespace zmq
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 ();
@ -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;
@ -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;
@ -152,7 +151,7 @@ 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);
} }
@ -165,9 +164,9 @@ namespace zmq
// 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,7 +182,7 @@ 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;
@ -195,4 +194,3 @@ namespace zmq
} }
#endif #endif

View File

@ -33,24 +33,22 @@
#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 ()
@ -60,8 +58,7 @@ zmq::shared_message_memory_allocator::~shared_message_memory_allocator()
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);
@ -78,15 +75,15 @@ unsigned char* zmq::shared_message_memory_allocator::allocate()
// 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);
@ -120,20 +117,21 @@ 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); zmq_assert (hint);
unsigned char* buf = static_cast <unsigned char*> (hint); unsigned char* buf = static_cast <unsigned char*> (hint);
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 (!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;
} }

View File

@ -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,9 +42,9 @@ 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);
} }
@ -60,20 +61,19 @@ namespace zmq
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&);
@ -92,10 +92,10 @@ 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 ();
@ -116,7 +116,7 @@ namespace zmq
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 ();
@ -127,12 +127,11 @@ namespace zmq
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++;
@ -140,10 +139,11 @@ namespace zmq
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