1/*
2 Copyright (c) 2007-2018 Contributors as noted in the AUTHORS file
3
4 This file is part of libzmq, the ZeroMQ core engine in C++.
5
6 libzmq is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License (LGPL) as published
8 by the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 As a special exception, the Contributors give you permission to link
12 this library with independent modules to produce an executable,
13 regardless of the license terms of these independent modules, and to
14 copy and distribute the resulting executable under terms of your choice,
15 provided that you also meet, for each linked independent module, the
16 terms and conditions of the license of that module. An independent
17 module is a module which is not derived from or based on this library.
18 If you modify this library, you must extend this exception to your
19 version of the library.
20
21 libzmq is distributed in the hope that it will be useful, but WITHOUT
22 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24 License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#ifndef __ZMQ_SOCKET_POLLING_UTIL_HPP_INCLUDED__
31#define __ZMQ_SOCKET_POLLING_UTIL_HPP_INCLUDED__
32
33#include <stdlib.h>
34#include <vector>
35
36#include "macros.hpp"
37#include "stdint.hpp"
38#include "platform.hpp"
39#include "err.hpp"
40
41namespace zmq
42{
43template <typename T, size_t S> class fast_vector_t
44{
45 public:
46 explicit fast_vector_t (const size_t nitems_)
47 {
48 if (nitems_ > S) {
49 _buf = static_cast<T *> (malloc (nitems_ * sizeof (T)));
50 // TODO since this function is called by a client, we could return errno == ENOMEM here
51 alloc_assert (_buf);
52 } else {
53 _buf = _static_buf;
54 }
55 }
56
57 T &operator[] (const size_t i) { return _buf[i]; }
58
59 ~fast_vector_t ()
60 {
61 if (_buf != _static_buf)
62 free (_buf);
63 }
64
65 private:
66 T _static_buf[S];
67 T *_buf;
68
69 ZMQ_NON_COPYABLE_NOR_MOVABLE (fast_vector_t)
70};
71
72template <typename T, size_t S> class resizable_fast_vector_t
73{
74 public:
75 resizable_fast_vector_t () : _dynamic_buf (NULL) {}
76
77 void resize (const size_t nitems_)
78 {
79 if (_dynamic_buf)
80 _dynamic_buf->resize (nitems_);
81 if (nitems_ > S) {
82 _dynamic_buf = new (std::nothrow) std::vector<T>;
83 // TODO since this function is called by a client, we could return errno == ENOMEM here
84 alloc_assert (_dynamic_buf);
85 }
86 }
87
88 T *get_buf ()
89 {
90 // e.g. MSVC 2008 does not have std::vector::data, so we use &...[0]
91 return _dynamic_buf ? &(*_dynamic_buf)[0] : _static_buf;
92 }
93
94 T &operator[] (const size_t i) { return get_buf ()[i]; }
95
96 ~resizable_fast_vector_t () { delete _dynamic_buf; }
97
98 private:
99 T _static_buf[S];
100 std::vector<T> *_dynamic_buf;
101
102 ZMQ_NON_COPYABLE_NOR_MOVABLE (resizable_fast_vector_t)
103};
104
105#if defined ZMQ_POLL_BASED_ON_POLL
106typedef int timeout_t;
107
108timeout_t compute_timeout (const bool first_pass_,
109 const long timeout_,
110 const uint64_t now_,
111 const uint64_t end_);
112
113#elif defined ZMQ_POLL_BASED_ON_SELECT
114inline size_t valid_pollset_bytes (const fd_set &pollset_)
115{
116#if defined ZMQ_HAVE_WINDOWS
117 // On Windows we don't need to copy the whole fd_set.
118 // SOCKETS are continuous from the beginning of fd_array in fd_set.
119 // We just need to copy fd_count elements of fd_array.
120 // We gain huge memcpy() improvement if number of used SOCKETs is much lower than FD_SETSIZE.
121 return reinterpret_cast<const char *> (
122 &pollset_.fd_array[pollset_.fd_count])
123 - reinterpret_cast<const char *> (&pollset_);
124#else
125 return sizeof (fd_set);
126#endif
127}
128
129#if defined ZMQ_HAVE_WINDOWS
130// struct fd_set {
131// u_int fd_count;
132// SOCKET fd_array[1];
133// };
134// NOTE: offsetof(fd_set, fd_array)==sizeof(SOCKET) on both x86 and x64
135// due to alignment bytes for the latter.
136class optimized_fd_set_t
137{
138 public:
139 explicit optimized_fd_set_t (size_t nevents_) : _fd_set (1 + nevents_) {}
140
141 fd_set *get () { return reinterpret_cast<fd_set *> (&_fd_set[0]); }
142
143 private:
144 fast_vector_t<SOCKET, 1 + ZMQ_POLLITEMS_DFLT> _fd_set;
145};
146
147class resizable_optimized_fd_set_t
148{
149 public:
150 void resize (size_t nevents_) { _fd_set.resize (1 + nevents_); }
151
152 fd_set *get () { return reinterpret_cast<fd_set *> (&_fd_set[0]); }
153
154 private:
155 resizable_fast_vector_t<SOCKET, 1 + ZMQ_POLLITEMS_DFLT> _fd_set;
156};
157#else
158class optimized_fd_set_t
159{
160 public:
161 explicit optimized_fd_set_t (size_t /*nevents_*/) {}
162
163 fd_set *get () { return &_fd_set; }
164
165 private:
166 fd_set _fd_set;
167};
168
169class resizable_optimized_fd_set_t : public optimized_fd_set_t
170{
171 public:
172 resizable_optimized_fd_set_t () : optimized_fd_set_t (0) {}
173
174 void resize (size_t /*nevents_*/) {}
175};
176#endif
177#endif
178}
179
180#endif
181