/src/libzmq/src/polling_util.hpp
Line | Count | Source |
1 | | /* SPDX-License-Identifier: MPL-2.0 */ |
2 | | |
3 | | #ifndef __ZMQ_SOCKET_POLLING_UTIL_HPP_INCLUDED__ |
4 | | #define __ZMQ_SOCKET_POLLING_UTIL_HPP_INCLUDED__ |
5 | | |
6 | | #include <stdlib.h> |
7 | | #include <vector> |
8 | | |
9 | | #if defined ZMQ_HAVE_WINDOWS |
10 | | #include <winsock.h> |
11 | | #else |
12 | | #include <sys/select.h> |
13 | | #endif |
14 | | |
15 | | #include "macros.hpp" |
16 | | #include "stdint.hpp" |
17 | | #include "platform.hpp" |
18 | | #include "err.hpp" |
19 | | |
20 | | namespace zmq |
21 | | { |
22 | | template <typename T, size_t S> class fast_vector_t |
23 | | { |
24 | | public: |
25 | | explicit fast_vector_t (const size_t nitems_) |
26 | 0 | { |
27 | 0 | if (nitems_ > S) { |
28 | 0 | _buf = new (std::nothrow) T[nitems_]; |
29 | | // TODO since this function is called by a client, we could return errno == ENOMEM here |
30 | 0 | alloc_assert (_buf); |
31 | 0 | } else { |
32 | 0 | _buf = _static_buf; |
33 | 0 | } |
34 | 0 | } |
35 | | |
36 | 0 | T &operator[] (const size_t i) { return _buf[i]; } |
37 | | |
38 | | ~fast_vector_t () |
39 | 0 | { |
40 | 0 | if (_buf != _static_buf) |
41 | 0 | delete[] _buf; |
42 | 0 | } |
43 | | |
44 | | private: |
45 | | T _static_buf[S]; |
46 | | T *_buf; |
47 | | |
48 | | ZMQ_NON_COPYABLE_NOR_MOVABLE (fast_vector_t) |
49 | | }; |
50 | | |
51 | | template <typename T, size_t S> class resizable_fast_vector_t |
52 | | { |
53 | | public: |
54 | | resizable_fast_vector_t () : _dynamic_buf (NULL) {} |
55 | | |
56 | | void resize (const size_t nitems_) |
57 | | { |
58 | | if (_dynamic_buf) { |
59 | | _dynamic_buf->resize (nitems_); |
60 | | } else if (nitems_ > S) { |
61 | | _dynamic_buf = new (std::nothrow) std::vector<T> (nitems_); |
62 | | // TODO since this function is called by a client, we could return errno == ENOMEM here |
63 | | alloc_assert (_dynamic_buf); |
64 | | memcpy (&(*_dynamic_buf)[0], _static_buf, sizeof _static_buf); |
65 | | } |
66 | | } |
67 | | |
68 | | T *get_buf () |
69 | | { |
70 | | // e.g. MSVC 2008 does not have std::vector::data, so we use &...[0] |
71 | | return _dynamic_buf ? &(*_dynamic_buf)[0] : _static_buf; |
72 | | } |
73 | | |
74 | | T &operator[] (const size_t i) { return get_buf ()[i]; } |
75 | | |
76 | | ~resizable_fast_vector_t () { delete _dynamic_buf; } |
77 | | |
78 | | private: |
79 | | T _static_buf[S]; |
80 | | std::vector<T> *_dynamic_buf; |
81 | | |
82 | | ZMQ_NON_COPYABLE_NOR_MOVABLE (resizable_fast_vector_t) |
83 | | }; |
84 | | |
85 | | #if defined ZMQ_POLL_BASED_ON_POLL |
86 | | typedef int timeout_t; |
87 | | |
88 | | timeout_t |
89 | | compute_timeout (bool first_pass_, long timeout_, uint64_t now_, uint64_t end_); |
90 | | #endif |
91 | | #if (!defined ZMQ_POLL_BASED_ON_POLL && defined ZMQ_POLL_BASED_ON_SELECT) \ |
92 | | || defined ZMQ_HAVE_PPOLL |
93 | | #if defined ZMQ_HAVE_WINDOWS |
94 | | inline size_t valid_pollset_bytes (const fd_set &pollset_) |
95 | | { |
96 | | // On Windows we don't need to copy the whole fd_set. |
97 | | // SOCKETS are continuous from the beginning of fd_array in fd_set. |
98 | | // We just need to copy fd_count elements of fd_array. |
99 | | // We gain huge memcpy() improvement if number of used SOCKETs is much lower than FD_SETSIZE. |
100 | | return reinterpret_cast<const char *> ( |
101 | | &pollset_.fd_array[pollset_.fd_count]) |
102 | | - reinterpret_cast<const char *> (&pollset_); |
103 | | } |
104 | | #else |
105 | | inline size_t valid_pollset_bytes (const fd_set & /*pollset_*/) |
106 | 0 | { |
107 | 0 | return sizeof (fd_set); |
108 | 0 | } |
109 | | #endif |
110 | | |
111 | | |
112 | | #if defined ZMQ_HAVE_WINDOWS |
113 | | // struct fd_set { |
114 | | // u_int fd_count; |
115 | | // SOCKET fd_array[1]; |
116 | | // }; |
117 | | // NOTE: offsetof(fd_set, fd_array)==sizeof(SOCKET) on both x86 and x64 |
118 | | // due to alignment bytes for the latter. |
119 | | class optimized_fd_set_t |
120 | | { |
121 | | public: |
122 | | explicit optimized_fd_set_t (size_t nevents_) : _fd_set (1 + nevents_) {} |
123 | | |
124 | | fd_set *get () { return reinterpret_cast<fd_set *> (&_fd_set[0]); } |
125 | | |
126 | | private: |
127 | | fast_vector_t<SOCKET, 1 + ZMQ_POLLITEMS_DFLT> _fd_set; |
128 | | }; |
129 | | |
130 | | class resizable_optimized_fd_set_t |
131 | | { |
132 | | public: |
133 | | void resize (size_t nevents_) { _fd_set.resize (1 + nevents_); } |
134 | | |
135 | | fd_set *get () { return reinterpret_cast<fd_set *> (&_fd_set[0]); } |
136 | | |
137 | | private: |
138 | | resizable_fast_vector_t<SOCKET, 1 + ZMQ_POLLITEMS_DFLT> _fd_set; |
139 | | }; |
140 | | #else |
141 | | class optimized_fd_set_t |
142 | | { |
143 | | public: |
144 | 0 | explicit optimized_fd_set_t (size_t /*nevents_*/) {} |
145 | | |
146 | 0 | fd_set *get () { return &_fd_set; } |
147 | | |
148 | | private: |
149 | | fd_set _fd_set; |
150 | | }; |
151 | | |
152 | | class resizable_optimized_fd_set_t : public optimized_fd_set_t |
153 | | { |
154 | | public: |
155 | 0 | resizable_optimized_fd_set_t () : optimized_fd_set_t (0) {} |
156 | | |
157 | 0 | void resize (size_t /*nevents_*/) {} |
158 | | }; |
159 | | #endif |
160 | | #endif |
161 | | } |
162 | | |
163 | | #endif |