Monero
Loading...
Searching...
No Matches
mlocker.h
Go to the documentation of this file.
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
35namespace 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
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>
88}
static size_t page_size
Definition mlocker.h:51
static size_t get_num_locked_objects()
Definition mlocker.cpp:168
void * ptr
Definition mlocker.h:59
static boost::mutex & mutex()
Definition mlocker.cpp:97
static size_t get_num_locked_pages()
Definition mlocker.cpp:162
static void lock(void *ptr, size_t len)
Definition mlocker.cpp:127
static void unlock(void *ptr, size_t len)
Definition mlocker.cpp:145
~mlocker()
Definition mlocker.cpp:121
size_t len
Definition mlocker.h:60
static size_t num_locked_objects
Definition mlocker.h:52
static std::map< size_t, unsigned int > & map()
Definition mlocker.cpp:102
static void unlock_page(size_t page)
Definition mlocker.cpp:187
static size_t get_page_size()
Definition mlocker.cpp:108
static void lock_page(size_t page)
Definition mlocker.cpp:174
mlocker(void *ptr, size_t len)
Definition mlocker.cpp:116
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
T & unwrap(mlocked< T > &src)
Definition mlocker.h:81
mlocked< std::array< T, N > > mlocked_arr
Definition mlocker.h:87
Definition mlocker.h:68
mlocked(const mlocked< T > &mt)
Definition mlocker.h:73
mlocked< T > & operator=(const mlocked< T > &mt)
Definition mlocker.h:76
mlocked(const T &t)
Definition mlocker.h:72
~mlocked()
Definition mlocker.h:77
mlocked()
Definition mlocker.h:71
T type
Definition mlocker.h:69
mlocked(const T &&t)
Definition mlocker.h:74
mlocked(const mlocked< T > &&mt)
Definition mlocker.h:75
#define T(x)