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
41 namespace zmq
42 {
43 template <typename T, size_t S> class fast_vector_t
44 {
45 public:
fast_vector_t(const size_t nitems_)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
operator [](const size_t i)57 T &operator[] (const size_t i) { return _buf[i]; }
58
~fast_vector_t()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
72 template <typename T, size_t S> class resizable_fast_vector_t
73 {
74 public:
resizable_fast_vector_t()75 resizable_fast_vector_t () : _dynamic_buf (NULL) {}
76
resize(const size_t nitems_)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
get_buf()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
operator [](const size_t i)94 T &operator[] (const size_t i) { return get_buf ()[i]; }
95
~resizable_fast_vector_t()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
106 typedef int timeout_t;
107
108 timeout_t
109 compute_timeout (bool first_pass_, long timeout_, uint64_t now_, uint64_t end_);
110
111 #elif defined ZMQ_POLL_BASED_ON_SELECT
valid_pollset_bytes(const fd_set & pollset_)112 inline size_t valid_pollset_bytes (const fd_set &pollset_)
113 {
114 #if defined ZMQ_HAVE_WINDOWS
115 // On Windows we don't need to copy the whole fd_set.
116 // SOCKETS are continuous from the beginning of fd_array in fd_set.
117 // We just need to copy fd_count elements of fd_array.
118 // We gain huge memcpy() improvement if number of used SOCKETs is much lower than FD_SETSIZE.
119 return reinterpret_cast<const char *> (
120 &pollset_.fd_array[pollset_.fd_count])
121 - reinterpret_cast<const char *> (&pollset_);
122 #else
123 return sizeof (fd_set);
124 #endif
125 }
126
127 #if defined ZMQ_HAVE_WINDOWS
128 // struct fd_set {
129 // u_int fd_count;
130 // SOCKET fd_array[1];
131 // };
132 // NOTE: offsetof(fd_set, fd_array)==sizeof(SOCKET) on both x86 and x64
133 // due to alignment bytes for the latter.
134 class optimized_fd_set_t
135 {
136 public:
optimized_fd_set_t(size_t nevents_)137 explicit optimized_fd_set_t (size_t nevents_) : _fd_set (1 + nevents_) {}
138
get()139 fd_set *get () { return reinterpret_cast<fd_set *> (&_fd_set[0]); }
140
141 private:
142 fast_vector_t<SOCKET, 1 + ZMQ_POLLITEMS_DFLT> _fd_set;
143 };
144
145 class resizable_optimized_fd_set_t
146 {
147 public:
resize(size_t nevents_)148 void resize (size_t nevents_) { _fd_set.resize (1 + nevents_); }
149
get()150 fd_set *get () { return reinterpret_cast<fd_set *> (&_fd_set[0]); }
151
152 private:
153 resizable_fast_vector_t<SOCKET, 1 + ZMQ_POLLITEMS_DFLT> _fd_set;
154 };
155 #else
156 class optimized_fd_set_t
157 {
158 public:
optimized_fd_set_t(size_t)159 explicit optimized_fd_set_t (size_t /*nevents_*/) {}
160
get()161 fd_set *get () { return &_fd_set; }
162
163 private:
164 fd_set _fd_set;
165 };
166
167 class resizable_optimized_fd_set_t : public optimized_fd_set_t
168 {
169 public:
resizable_optimized_fd_set_t()170 resizable_optimized_fd_set_t () : optimized_fd_set_t (0) {}
171
resize(size_t)172 void resize (size_t /*nevents_*/) {}
173 };
174 #endif
175 #endif
176 }
177
178 #endif
179