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__
#define __ZMQ_DECODER_HPP_INCLUDED__
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <cstddef>
#include <cstdlib>
#include "err.hpp"
#include "msg.hpp"
#include "i_decoder.hpp"
#include "stdint.hpp"
#include "decoder_allocators.hpp"
#include "err.hpp"
#include "i_decoder.hpp"
#include "msg.hpp"
#include "stdint.hpp"
namespace zmq
{
@ -60,7 +59,7 @@ namespace zmq
{
public:
inline decoder_base_t (A* allocator_) :
explicit decoder_base_t (A *allocator_) :
next (NULL),
read_pos (NULL),
to_read (0),
@ -69,15 +68,15 @@ namespace zmq
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.
inline virtual ~decoder_base_t ()
virtual ~decoder_base_t ()
{
allocator->deallocate ();
}
// 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 ();
@ -105,8 +104,8 @@ namespace zmq
// whole message was decoded or 0 when more data is required.
// On error, -1 is returned and errno set accordingly.
// Number of bytes processed is returned in byts_used_.
inline int decode (const unsigned char *data_, size_t size_,
size_t &bytes_used_)
int decode (const unsigned char *data_, std::size_t size_,
std::size_t &bytes_used_)
{
bytes_used_ = 0;
@ -129,11 +128,11 @@ namespace zmq
while (bytes_used_ < size_) {
// 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
// current address in the buffer
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;
@ -152,7 +151,7 @@ namespace zmq
return 0;
}
virtual void resize_buffer(size_t new_size)
virtual void resize_buffer (std::size_t new_size)
{
allocator->resize (new_size);
}
@ -165,9 +164,9 @@ namespace zmq
// This function should be called from derived class to read data
// 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_;
next = next_;
}
@ -183,7 +182,7 @@ namespace zmq
unsigned char *read_pos;
// How much data to read before taking next step.
size_t to_read;
std::size_t to_read;
// The duffer for data to decode.
A *allocator;
@ -195,4 +194,3 @@ namespace zmq
}
#endif

View File

@ -33,24 +33,22 @@
#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),
bufsize(0),
max_size(bufsize_),
msg_refcnt(NULL),
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),
bufsize(0),
max_size(bufsize_),
msg_refcnt(NULL),
maxCounters(maxMessages)
{
}
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 ()
{
if (buf)
{
if (buf) {
// release reference count to couple lifetime to messages
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) {
// 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);
new (buf) atomic_counter_t (1);
}
else
{
} else {
// release reference count to couple lifetime to messages
zmq::atomic_counter_t *c = reinterpret_cast <zmq::atomic_counter_t *> (buf);
c->set (1);
@ -120,20 +117,21 @@ void zmq::shared_message_memory_allocator::inc_ref()
(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::atomic_counter_t* c = reinterpret_cast <zmq::atomic_counter_t*> (buf);
if (!c->sub (1)) {
c->~atomic_counter_t ();
free(buf);
std::free (buf);
buf = NULL;
}
}
size_t zmq::shared_message_memory_allocator::size() const
std::size_t zmq::shared_message_memory_allocator::size () const
{
return bufsize;
}

View File

@ -27,13 +27,14 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ZEROMQ_DECODER_ALLOCATORS_HPP
#define ZEROMQ_DECODER_ALLOCATORS_HPP
#ifndef __ZMQ_DECODER_ALLOCATORS_HPP_INCLUDED__
#define __ZMQ_DECODER_ALLOCATORS_HPP_INCLUDED__
#include <cstddef>
#include <cstdlib>
#include "err.hpp"
#include "atomic_counter.hpp"
#include "err.hpp"
namespace zmq
{
@ -41,9 +42,9 @@ namespace zmq
class c_single_allocator
{
public:
explicit c_single_allocator(size_t bufsize_):
explicit c_single_allocator (std::size_t bufsize_) :
bufsize(bufsize_),
buf(static_cast<unsigned char*>( malloc (bufsize) ))
buf(static_cast <unsigned char*> (std::malloc (bufsize)))
{
alloc_assert (buf);
}
@ -60,20 +61,19 @@ namespace zmq
void deallocate ()
{
}
size_t size() const
std::size_t size () const
{
return bufsize;
}
void resize(size_t new_size)
void resize (std::size_t new_size)
{
bufsize = new_size;
}
private:
size_t bufsize;
std::size_t bufsize;
unsigned char* buf;
c_single_allocator (c_single_allocator const&);
@ -92,10 +92,10 @@ namespace zmq
class shared_message_memory_allocator
{
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
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 ();
@ -116,7 +116,7 @@ namespace zmq
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.
unsigned char* data ();
@ -127,12 +127,11 @@ namespace zmq
return buf;
}
void resize(size_t new_size)
void resize (std::size_t new_size)
{
bufsize = new_size;
}
//
zmq::atomic_counter_t* create_refcnt ()
{
return msg_refcnt++;
@ -140,10 +139,11 @@ namespace zmq
private:
unsigned char* buf;
size_t bufsize;
size_t max_size;
std::size_t bufsize;
std::size_t max_size;
zmq::atomic_counter_t* msg_refcnt;
size_t maxCounters;
std::size_t maxCounters;
};
}
#endif //ZEROMQ_DECODER_ALLOCATORS_HPP
#endif