Debian Codegraph @

contrib/epee/include/mlocker.h

raw symbols SCIP
1 // Copyright (c) 2018-2022, The Monero Project
2
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #pragma once
31
32 #include <map>
33 #include <boost/thread/mutex.hpp>
34
35 namespace epee
36 {
37 class mlocker
38 {
39 public:
40 mlocker(void *ptr, size_t len);
41 ~mlocker();
42
43 static size_t get_page_size();
44 static size_t get_num_locked_pages();
45 static size_t get_num_locked_objects();
46
47 static void lock(void *ptr, size_t len);
48 static void unlock(void *ptr, size_t len);
49
50 private:
51 static size_t page_size;
52 static size_t num_locked_objects;
53
54 static boost::mutex &mutex();
55 static std::map<size_t, unsigned int> &map();
56 static void lock_page(size_t page);
57 static void unlock_page(size_t page);
58
59 void *ptr;
60 size_t len;
61 };
62
63 /// Locks memory while in scope
64 ///
65 /// Primarily useful for making sure that private keys don't get swapped out
66 // to disk
67 template <class T>
68 struct mlocked : public T {
69 using type = T;
70
71 mlocked(): T() { mlocker::lock(this, sizeof(T)); }
72 mlocked(const T &t): T(t) { mlocker::lock(this, sizeof(T)); }
73 mlocked(const mlocked<T> &mt): T(mt) { mlocker::lock(this, sizeof(T)); }
74 mlocked(const T &&t): T(t) { mlocker::lock(this, sizeof(T)); }
75 mlocked(const mlocked<T> &&mt): T(mt) { mlocker::lock(this, sizeof(T)); }
76 mlocked<T> &operator=(const mlocked<T> &mt) { T::operator=(mt); return *this; }
77 ~mlocked() { try { mlocker::unlock(this, sizeof(T)); } catch (...) { /* do not propagate */ } }
78 };
79
80 template<typename T>
81 T& unwrap(mlocked<T>& src) { return src; }
82
83 template<typename T>
84 const T& unwrap(mlocked<T> const& src) { return src; }
85
86 template <class T, size_t N>
87 using mlocked_arr = mlocked<std::array<T, N>>;
88 }