decoder_allocators.cpp 4.61 KB
Newer Older
Jens Auer's avatar
Jens Auer committed
1
/*
2
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
Jens Auer's avatar
Jens Auer committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

    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/>.
*/

30
#include "precompiled.hpp"
Jens Auer's avatar
Jens Auer committed
31 32 33 34 35 36
#include "decoder_allocators.hpp"

#include <cmath>

#include "msg.hpp"

37 38 39 40
zmq::shared_message_memory_allocator::shared_message_memory_allocator (std::size_t bufsize_) :
    buf(NULL),
    bufsize(0),
    max_size(bufsize_),
41
    msg_content(NULL),
evoskuil's avatar
evoskuil committed
42
    maxCounters (static_cast <size_t> (std::ceil (static_cast <double> (max_size) / static_cast <double> (msg_t::max_vsm_size))))
Jens Auer's avatar
Jens Auer committed
43 44 45
{
}

46 47 48 49
zmq::shared_message_memory_allocator::shared_message_memory_allocator (std::size_t bufsize_, std::size_t maxMessages) :
    buf(NULL),
    bufsize(0),
    max_size(bufsize_),
50
    msg_content(NULL),
51
    maxCounters(maxMessages)
Jens Auer's avatar
Jens Auer committed
52 53 54
{
}

55
zmq::shared_message_memory_allocator::~shared_message_memory_allocator ()
Jens Auer's avatar
Jens Auer committed
56
{
57
    deallocate();
Jens Auer's avatar
Jens Auer committed
58 59
}

60
unsigned char* zmq::shared_message_memory_allocator::allocate ()
Jens Auer's avatar
Jens Auer committed
61
{
62
    if (buf) {
Jens Auer's avatar
Jens Auer committed
63
        // release reference count to couple lifetime to messages
64
        zmq::atomic_counter_t* c = reinterpret_cast<zmq::atomic_counter_t* >(buf);
Jens Auer's avatar
Jens Auer committed
65 66 67 68

        // if refcnt drops to 0, there are no message using the buffer
        // because either all messages have been closed or only vsm-messages
        // were created
69
        if (c->sub (1)) {
Jens Auer's avatar
Jens Auer committed
70 71
            // 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
72
            release ();
Jens Auer's avatar
Jens Auer committed
73 74 75 76 77 78
        }
    }

    // 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
79 80
        std::size_t const allocationsize =
              max_size + sizeof (zmq::atomic_counter_t) +
81
              maxCounters * sizeof (zmq::msg_t::content_t);
Jens Auer's avatar
Jens Auer committed
82

83
        buf = static_cast <unsigned char *> (std::malloc (allocationsize));
Jens Auer's avatar
Jens Auer committed
84 85
        alloc_assert (buf);

86 87
        new (buf) atomic_counter_t (1);
    } else {
Jens Auer's avatar
Jens Auer committed
88
        // release reference count to couple lifetime to messages
89 90
        zmq::atomic_counter_t *c = reinterpret_cast <zmq::atomic_counter_t *> (buf);
        c->set (1);
Jens Auer's avatar
Jens Auer committed
91 92 93
    }

    bufsize = max_size;
94
    msg_content = reinterpret_cast <zmq::msg_t::content_t*> (buf + sizeof (atomic_counter_t) + max_size);
95
    return buf + sizeof (zmq::atomic_counter_t);
Jens Auer's avatar
Jens Auer committed
96 97
}

98
void zmq::shared_message_memory_allocator::deallocate ()
Jens Auer's avatar
Jens Auer committed
99
{
100 101 102 103 104
    zmq::atomic_counter_t* c = reinterpret_cast<zmq::atomic_counter_t* >(buf);
    if (buf && !c->sub(1)) {
        std::free(buf);
    }
    release();
Jens Auer's avatar
Jens Auer committed
105 106
}

107
unsigned char* zmq::shared_message_memory_allocator::release ()
Jens Auer's avatar
Jens Auer committed
108 109 110 111
{
    unsigned char* b = buf;
    buf = NULL;
    bufsize = 0;
112
    msg_content = NULL;
Jens Auer's avatar
Jens Auer committed
113 114 115 116

    return b;
}

117
void zmq::shared_message_memory_allocator::inc_ref ()
Jens Auer's avatar
Jens Auer committed
118
{
119
    (reinterpret_cast <zmq::atomic_counter_t*> (buf))->add (1);
Jens Auer's avatar
Jens Auer committed
120 121
}

122 123 124 125 126
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);
Jens Auer's avatar
Jens Auer committed
127

128 129 130
    if (!c->sub (1)) {
        c->~atomic_counter_t ();
        std::free (buf);
Jens Auer's avatar
Jens Auer committed
131 132 133 134 135
        buf = NULL;
    }
}


136
std::size_t zmq::shared_message_memory_allocator::size () const
Jens Auer's avatar
Jens Auer committed
137 138 139 140
{
    return bufsize;
}

141
unsigned char* zmq::shared_message_memory_allocator::data ()
Jens Auer's avatar
Jens Auer committed
142
{
143 144
    return buf + sizeof (zmq::atomic_counter_t);
}