decoder_allocators.cpp 4.58 KB
Newer Older
Jens Auer's avatar
Jens Auer committed
1 2 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 30 31 32 33 34 35
/*
    Copyright (c) 2007-2015 Contributors as noted in the AUTHORS file

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

#include "decoder_allocators.hpp"

#include <cmath>

#include "msg.hpp"

36 37 38 39 40
zmq::shared_message_memory_allocator::shared_message_memory_allocator (std::size_t bufsize_) :
    buf(NULL),
    bufsize(0),
    max_size(bufsize_),
    msg_refcnt(NULL),
evoskuil's avatar
evoskuil committed
41
    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
42 43 44
{
}

45 46 47 48 49 50
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)
Jens Auer's avatar
Jens Auer committed
51 52 53
{
}

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

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

        // 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
68
        if (c->sub (1)) {
Jens Auer's avatar
Jens Auer committed
69 70
            // 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
71
            release ();
Jens Auer's avatar
Jens Auer committed
72 73 74 75 76 77
        }
    }

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

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

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

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

97
void zmq::shared_message_memory_allocator::deallocate ()
Jens Auer's avatar
Jens Auer committed
98
{
99 100 101 102 103
    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
104 105
}

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

    return b;
}

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

121 122 123 124 125
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
126

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


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

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