polling_util.hpp 5.23 KB
Newer Older
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
/*
    Copyright (c) 2007-2018 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/>.
*/

#ifndef __ZMQ_SOCKET_POLLING_UTIL_HPP_INCLUDED__
#define __ZMQ_SOCKET_POLLING_UTIL_HPP_INCLUDED__

#include <stdlib.h>
34
#include <vector>
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

#include "stdint.hpp"
#include "platform.hpp"
#include "err.hpp"

namespace zmq
{
template <typename T, size_t S> class fast_vector_t
{
  public:
    explicit fast_vector_t (const size_t nitems_)
    {
        if (nitems_ > S) {
            _buf = static_cast<T *> (malloc (nitems_ * sizeof (T)));
            //  TODO since this function is called by a client, we could return errno == ENOMEM here
            alloc_assert (_buf);
        } else {
            _buf = _static_buf;
        }
    }

    T &operator[] (const size_t i) { return _buf[i]; }

    ~fast_vector_t ()
    {
        if (_buf != _static_buf)
            free (_buf);
    }

  private:
    fast_vector_t (const fast_vector_t &);
    fast_vector_t &operator= (const fast_vector_t &);

    T _static_buf[S];
    T *_buf;
};

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
template <typename T, size_t S> class resizable_fast_vector_t
{
  public:
    resizable_fast_vector_t () : _dynamic_buf (NULL) {}

    void resize (const size_t nitems_)
    {
        if (_dynamic_buf)
            _dynamic_buf->resize (nitems_);
        if (nitems_ > S) {
            _dynamic_buf = new (std::nothrow) std::vector<T>;
            //  TODO since this function is called by a client, we could return errno == ENOMEM here
            alloc_assert (_dynamic_buf);
        }
    }

    T *get_buf ()
    {
        // e.g. MSVC 2008 does not have std::vector::data, so we use &...[0]
        return _dynamic_buf ? &(*_dynamic_buf)[0] : _static_buf;
    }

    T &operator[] (const size_t i) { return get_buf ()[i]; }

    ~resizable_fast_vector_t () { delete _dynamic_buf; }

  private:
    resizable_fast_vector_t (const resizable_fast_vector_t &);
    resizable_fast_vector_t &operator= (const resizable_fast_vector_t &);

    T _static_buf[S];
    std::vector<T> *_dynamic_buf;
};

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#if defined ZMQ_POLL_BASED_ON_POLL
typedef int timeout_t;

timeout_t compute_timeout (const bool first_pass_,
                           const long timeout_,
                           const uint64_t now_,
                           const uint64_t end_);

#elif defined ZMQ_POLL_BASED_ON_SELECT
inline size_t valid_pollset_bytes (const fd_set &pollset_)
{
#if defined ZMQ_HAVE_WINDOWS
    // On Windows we don't need to copy the whole fd_set.
    // SOCKETS are continuous from the beginning of fd_array in fd_set.
    // We just need to copy fd_count elements of fd_array.
    // We gain huge memcpy() improvement if number of used SOCKETs is much lower than FD_SETSIZE.
    return reinterpret_cast<const char *> (
             &pollset_.fd_array[pollset_.fd_count])
           - reinterpret_cast<const char *> (&pollset_);
#else
    return sizeof (fd_set);
#endif
}

#if defined ZMQ_HAVE_WINDOWS
131 132 133 134 135 136
// struct fd_set {
//  u_int   fd_count;
//  SOCKET  fd_array[1];
// };
// NOTE: offsetof(fd_set, fd_array)==sizeof(SOCKET) on both x86 and x64
//       due to alignment bytes for the latter.
137 138 139
class optimized_fd_set_t
{
  public:
140
    explicit optimized_fd_set_t (size_t nevents_) : _fd_set (1 + nevents_) {}
141 142 143 144

    fd_set *get () { return reinterpret_cast<fd_set *> (&_fd_set[0]); }

  private:
145
    fast_vector_t<SOCKET, 1 + ZMQ_POLLITEMS_DFLT> _fd_set;
146
};
147 148 149 150

class resizable_optimized_fd_set_t
{
  public:
151
    void resize (size_t nevents_) { _fd_set.resize (1 + nevents_); }
152 153 154 155

    fd_set *get () { return reinterpret_cast<fd_set *> (&_fd_set[0]); }

  private:
156
    resizable_fast_vector_t<SOCKET, 1 + ZMQ_POLLITEMS_DFLT> _fd_set;
157
};
158 159 160 161 162 163 164 165 166 167 168
#else
class optimized_fd_set_t
{
  public:
    explicit optimized_fd_set_t (size_t /*nevents_*/) {}

    fd_set *get () { return &_fd_set; }

  private:
    fd_set _fd_set;
};
169 170 171 172 173 174 175 176

class resizable_optimized_fd_set_t : public optimized_fd_set_t
{
  public:
    resizable_optimized_fd_set_t () : optimized_fd_set_t (0) {}

    void resize (size_t /*nevents_*/) {}
};
177 178 179 180 181
#endif
#endif
}

#endif