polling_util.hpp
Go to the documentation of this file.
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  {
27  if (nitems_ > S) {
28  _buf = new (std::nothrow) T[nitems_];
29  // TODO since this function is called by a client, we could return errno == ENOMEM here
31  } else {
32  _buf = _static_buf;
33  }
34  }
35 
36  T &operator[] (const size_t i) { return _buf[i]; }
37 
39  {
40  if (_buf != _static_buf)
41  delete[] _buf;
42  }
43 
44  private:
46  T *_buf;
47 
49 };
50 
51 template <typename T, size_t S> class resizable_fast_vector_t
52 {
53  public:
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
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 
77 
78  private:
80  std::vector<T> *_dynamic_buf;
81 
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 {
107  return sizeof (fd_set);
108 }
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  explicit optimized_fd_set_t (size_t /*nevents_*/) {}
145 
146  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  resizable_optimized_fd_set_t () : optimized_fd_set_t (0) {}
156 
157  void resize (size_t /*nevents_*/) {}
158 };
159 #endif
160 #endif
161 }
162 
163 #endif
zmq::resizable_fast_vector_t
Definition: polling_util.hpp:51
zmq::resizable_fast_vector_t::get_buf
T * get_buf()
Definition: polling_util.hpp:68
NULL
NULL
Definition: test_security_zap.cpp:405
get
ROSCPP_DECL bool get(const std::string &key, bool &b)
zmq::fast_vector_t::_buf
T * _buf
Definition: polling_util.hpp:46
zmq::resizable_fast_vector_t::_static_buf
T _static_buf[S]
Definition: polling_util.hpp:79
T
#define T(upbtypeconst, upbtype, ctype, default_value)
zmq::fast_vector_t::operator[]
T & operator[](const size_t i)
Definition: polling_util.hpp:36
zmq::fast_vector_t
Definition: polling_util.hpp:22
zmq::fast_vector_t::~fast_vector_t
~fast_vector_t()
Definition: polling_util.hpp:38
zmq
Definition: zmq.hpp:229
alloc_assert
#define alloc_assert(x)
Definition: err.hpp:146
S
#define S(n, x)
Definition: sha1.c:50
zmq::resizable_fast_vector_t::~resizable_fast_vector_t
~resizable_fast_vector_t()
Definition: polling_util.hpp:76
macros.hpp
stdint.hpp
ZMQ_NON_COPYABLE_NOR_MOVABLE
#define ZMQ_NON_COPYABLE_NOR_MOVABLE(classname)
Definition: macros.hpp:58
zmq::resizable_fast_vector_t::_dynamic_buf
std::vector< T > * _dynamic_buf
Definition: polling_util.hpp:80
zmq::resizable_fast_vector_t::operator[]
T & operator[](const size_t i)
Definition: polling_util.hpp:74
i
int i
Definition: gmock-matchers_test.cc:764
zmq::fast_vector_t::_static_buf
T _static_buf[S]
Definition: polling_util.hpp:45
zmq::resizable_fast_vector_t::resizable_fast_vector_t
resizable_fast_vector_t()
Definition: polling_util.hpp:54
zmq::resizable_fast_vector_t::resize
void resize(const size_t nitems_)
Definition: polling_util.hpp:56
err.hpp
zmq::fast_vector_t::fast_vector_t
fast_vector_t(const size_t nitems_)
Definition: polling_util.hpp:25


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:57