secure_allocator.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #ifndef __ZMQ_SECURE_ALLOCATOR_HPP_INCLUDED__
4 #define __ZMQ_SECURE_ALLOCATOR_HPP_INCLUDED__
5 
6 #include "platform.hpp"
7 #include "macros.hpp"
8 
9 #ifdef ZMQ_HAVE_CURVE
10 
11 #if defined(ZMQ_USE_LIBSODIUM)
12 #include "sodium.h"
13 #endif
14 
15 #include <memory>
16 
17 namespace zmq
18 {
19 #if defined(ZMQ_USE_LIBSODIUM)
20 template <class T> struct secure_allocator_t
21 {
22  typedef T value_type;
23 
24  secure_allocator_t () ZMQ_DEFAULT;
25 
26  template <class U>
27  secure_allocator_t (const secure_allocator_t<U> &) ZMQ_NOEXCEPT
28  {
29  }
30  T *allocate (std::size_t n) ZMQ_NOEXCEPT
31  {
32  T *res = static_cast<T *> (sodium_allocarray (sizeof (T), n));
33  alloc_assert (res);
34  return res;
35  }
36  void deallocate (T *p, std::size_t) ZMQ_NOEXCEPT
37  {
38  if (p)
39  sodium_free (p);
40  }
41 
42  // the following is only required with C++98
43  // TODO maybe make this conditionally compiled
44  typedef T *pointer;
45  typedef const T *const_pointer;
46  typedef T &reference;
47  typedef const T &const_reference;
48  typedef std::size_t size_type;
49  typedef std::ptrdiff_t difference_type;
50  template <class U> struct rebind
51  {
52  typedef secure_allocator_t<U> other;
53  };
54 
55  void construct (pointer p, const_reference val)
56  {
57  new ((void *) p) value_type (val);
58  }
59  void destroy (pointer p) { p->~value_type (); }
60  size_type max_size () const { return SIZE_MAX; }
61 };
62 template <class T, class U>
63 bool operator== (const secure_allocator_t<T> &, const secure_allocator_t<U> &)
64 {
65  return true;
66 }
67 template <class T, class U>
68 bool operator!= (const secure_allocator_t<T> &, const secure_allocator_t<U> &)
69 {
70  return false;
71 }
72 #else
73 template <typename T> struct secure_allocator_t : std::allocator<T>
74 {
75  secure_allocator_t () ZMQ_DEFAULT;
76 
77  template <class U>
78  secure_allocator_t (const secure_allocator_t<U> &) ZMQ_NOEXCEPT
79  {
80  }
81 
82  template <class U> struct rebind
83  {
84  typedef secure_allocator_t<U> other;
85  };
86 };
87 #endif
88 }
89 
90 #endif
91 
92 #endif
benchmarks.python.py_benchmark.const
const
Definition: py_benchmark.py:14
ZMQ_DEFAULT
#define ZMQ_DEFAULT
Definition: macros.hpp:43
T
#define T(upbtypeconst, upbtype, ctype, default_value)
zmq::operator==
bool operator==(const detail::socket_base &a, const detail::socket_base &b) ZMQ_NOTHROW
Definition: zmq.hpp:2146
std::allocator
zmq
Definition: zmq.hpp:229
alloc_assert
#define alloc_assert(x)
Definition: err.hpp:146
ZMQ_NOEXCEPT
#define ZMQ_NOEXCEPT
Definition: macros.hpp:19
macros.hpp
p
const char * p
Definition: gmock-matchers_test.cc:3863
n
GLdouble n
Definition: glcorearb.h:4153
pointer
GLenum GLvoid ** pointer
Definition: glcorearb.h:3079
value_type
zend_class_entry * value_type
Definition: php/ext/google/protobuf/message.c:2546
zmq::operator!=
bool operator!=(const detail::socket_base &a, const detail::socket_base &b) ZMQ_NOTHROW
Definition: zmq.hpp:2150
val
GLuint GLfloat * val
Definition: glcorearb.h:3604


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