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