secure_allocator.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 //
4 // MIT License
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //---------------------------------------------------------------------------//
24 
25 #ifndef CRYPTO3_SECURE_ALLOCATOR_HPP
26 #define CRYPTO3_SECURE_ALLOCATOR_HPP
27 
29 
30 namespace nil {
31  namespace crypto3 {
32  template<typename T>
34  public:
35  /*
36  * Assert exists to prevent someone from doing something that will
37  * probably crash anyway (like secure_vector<non_POD_t> where ~non_POD_t
38  * deletes a member pointer which was zeroed before it ran).
39  * MSVC in debug mode uses non-integral proxy types in container types
40  * like std::vector, thus we disable the check there.
41  */
42 #if !defined(_ITERATOR_DEBUG_LEVEL) || _ITERATOR_DEBUG_LEVEL == 0
43  static_assert(std::is_integral<T>::value, "secure_allocator supports only integer types");
44 #endif
45 
46  typedef T value_type;
47  typedef std::size_t size_type;
48 
49 #ifdef CRYPTO3_BUILD_COMPILER_IS_MSVC_2013
50  secure_allocator() = default;
51  secure_allocator(const secure_allocator &) = default;
52  secure_allocator &operator=(const secure_allocator &) = default;
53  ~secure_allocator() = default;
54 
55  template<typename U>
56  struct rebind {
57  typedef secure_allocator<U> other;
58  };
59 
60  void construct(value_type *mem, const value_type &value) {
61  std::_Construct(mem, value);
62  }
63 
64  void destroy(value_type *mem) {
65  std::_Destroy(mem);
66  }
67 #else
68 
69  secure_allocator() BOOST_NOEXCEPT = default;
70 
71  secure_allocator(const secure_allocator &) BOOST_NOEXCEPT = default;
72 
73  secure_allocator &operator=(const secure_allocator &) BOOST_NOEXCEPT = default;
74 
75  ~secure_allocator() BOOST_NOEXCEPT = default;
76 
77 #endif
78 
79  template<typename U>
80  secure_allocator(const secure_allocator<U> &) BOOST_NOEXCEPT {
81  }
82 
83  T *allocate(std::size_t n) {
84  return static_cast<T *>(allocate_memory(n, sizeof(T)));
85  }
86 
87  void deallocate(T *p, std::size_t n) {
88  deallocate_memory(p, n, sizeof(T));
89  }
90  };
91 
92  template<typename T, typename U>
93  inline bool operator==(const secure_allocator<T> &, const secure_allocator<U> &) {
94  return true;
95  }
96 
97  template<typename T, typename U>
98  inline bool operator!=(const secure_allocator<T> &, const secure_allocator<U> &) {
99  return false;
100  }
101  } // namespace crypto3
102 } // namespace nil
103 
104 #endif // CRYPTO3_SECURE_ALLOCATOR_HPP
Definition: secure_allocator.hpp:33
~secure_allocator() BOOST_NOEXCEPT=default
secure_allocator & operator=(const secure_allocator &) BOOST_NOEXCEPT=default
T value_type
Definition: secure_allocator.hpp:43
secure_allocator(const secure_allocator< U > &) BOOST_NOEXCEPT
Definition: secure_allocator.hpp:80
void deallocate(T *p, std::size_t n)
Definition: secure_allocator.hpp:87
secure_allocator() BOOST_NOEXCEPT=default
T * allocate(std::size_t n)
Definition: secure_allocator.hpp:83
std::size_t size_type
Definition: secure_allocator.hpp:47
BOOST_ATTRIBUTE_MALLOC_FUNCTION void * allocate_memory(size_t elems, size_t elem_size)
Definition: memory_operations.hpp:48
bool operator!=(const secure_allocator< T > &, const secure_allocator< U > &)
Definition: secure_allocator.hpp:98
bool operator==(const secure_allocator< T > &, const secure_allocator< U > &)
Definition: secure_allocator.hpp:93
void deallocate_memory(void *p, size_t elems, size_t elem_size)
Definition: memory_operations.hpp:110
Definition: pair.hpp:31