FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
hmac-cpp/include/hmac_cpp/page_lock_registry.hpp at main · LimiNode/hmac-cpp · GitHub
LimiNode
/
hmac-cpp
Public
Notifications
You must be signed in to change notification settings
Fork
6
Star
11
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
hmac-cpp
/
include
/
hmac_cpp
/
page_lock_registry.hpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
237 lines (210 loc) · 8.03 KB
Breadcrumbs
hmac-cpp
/
include
/
hmac_cpp
/
page_lock_registry.hpp
Copy path
File metadata and controls
237 lines (210 loc) · 8.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#
ifndef
HMAC_CPP_PAGE_LOCK_REGISTRY_HPP
#
define
HMAC_CPP_PAGE_LOCK_REGISTRY_HPP
#
include
<
cstddef
>
#
include
<
cstdint
>
#
include
<
limits
>
#
include
<
map
>
#
include
<
memory
>
#
include
<
mutex
>
#
include
<
utility
>
namespace
hmac_cpp
{
namespace
detail
{
struct
page_lock_page_state
{
std::
size_t
owners{};
bool
os_locked{};
bool
pending_lock{};
};
//
/ \brief Process-wide ownership registry for page-granular memory locks.
//
/ \tparam PageLocker Type exposing lock_page() and unlock_page().
//
/ \tparam Mutex Mutex type used to synchronize access to the registry.
//
/ \tparam PageStateAllocator Allocator used by the registry's page-state map.
template
<
class
PageLocker
,
class
Mutex
= std::mutex,
class
PageStateAllocator
= std::allocator<std::pair<
const
std::
uintptr_t
, page_lock_page_state>>>
class
page_lock_registry
{
public:
static_assert
(
noexcept
(std::declval<PageLocker&>().lock_page(
nullptr
, std::
size_t
{})),
"
PageLocker::lock_page must be noexcept
"
);
static_assert
(
noexcept
(std::declval<PageLocker&>().unlock_page(
nullptr
, std::
size_t
{})),
"
PageLocker::unlock_page must be noexcept
"
);
explicit
page_lock_registry
(std::
size_t
page_size, PageLocker locker = PageLocker())
: page_size_(page_size), locker_(locker) {}
bool
lock
(
void
* ptr, std::
size_t
len)
noexcept
{
std::
uintptr_t
first =
0
;
std::
uintptr_t
last =
0
;
if
(!
normalize
(ptr, len, first, last)) {
return
false
;
}
try
{
std::lock_guard<Mutex>
guard
(mutex_);
return
lock_range
(first, last);
}
catch
(...) {
return
false
;
}
}
bool
unlock
(
void
* ptr, std::
size_t
len)
noexcept
{
std::
uintptr_t
first =
0
;
std::
uintptr_t
last =
0
;
if
(!
normalize
(ptr, len, first, last)) {
return
false
;
}
try
{
std::lock_guard<Mutex>
guard
(mutex_);
if
(!
all_pages_owned
(first, last)) {
return
false
;
}
bool
success =
true
;
for_each_page
(first, last, [
this
, &success](std::
uintptr_t
page) {
typename
page_states_type::iterator entry = page_states_.
find
(page);
page_state& state = entry->
second
;
if
(state.
owners
>
1
) {
--state.
owners
;
return
true
;
}
state.
owners
=
0
;
if
(!state.
os_locked
|| locker_.
unlock_page
(
reinterpret_cast
<
void
*>(page), page_size_)) {
page_states_.
erase
(entry);
}
else
{
success =
false
;
}
return
true
;
});
return
success;
}
catch
(...) {
return
false
;
}
}
private:
using
page_state = page_lock_page_state;
using
page_states_type = std::map<std::
uintptr_t
,
page_state,
std::less<std::
uintptr_t
>,
PageStateAllocator>;
bool
normalize
(
void
* ptr, std::
size_t
len, std::
uintptr_t
& first,
std::
uintptr_t
& last)
const
noexcept
{
if
(ptr ==
nullptr
|| len ==
0
|| page_size_ ==
0
) {
return
false
;
}
const
std::
uintptr_t
address =
reinterpret_cast
<std::
uintptr_t
>(ptr);
if
(len -
1
> std::numeric_limits<std::
uintptr_t
>::
max
() - address) {
return
false
;
}
const
std::
uintptr_t
end = address + len -
1
;
first = address - address % page_size_;
last = end - end % page_size_;
return
true
;
}
template
<
class
Visitor
>
void
for_each_page
(std::
uintptr_t
first, std::
uintptr_t
last, Visitor visitor) {
for
(std::
uintptr_t
page = first;; page += page_size_) {
if
(!
visitor
(page) || page == last) {
return
;
}
}
}
bool
all_pages_owned
(std::
uintptr_t
first, std::
uintptr_t
last)
const
noexcept
{
for
(std::
uintptr_t
page = first;; page += page_size_) {
const
typename
page_states_type::const_iterator entry = page_states_.
find
(page);
if
(entry == page_states_.
end
() || entry->
second
.
owners
==
0
|| entry->
second
.
pending_lock
) {
return
false
;
}
if
(page == last) {
return
true
;
}
}
}
bool
lock_range
(std::
uintptr_t
first, std::
uintptr_t
last)
noexcept
{
try
{
for_each_page
(first, last, [
this
](std::
uintptr_t
page) {
page_states_.
emplace
(page, page_state{});
return
true
;
});
}
catch
(...) {
erase_unlocked_pages
(first, last);
return
false
;
}
bool
overflow =
false
;
for_each_page
(first, last, [
this
, &overflow](std::
uintptr_t
page) {
const
typename
page_states_type::const_iterator entry = page_states_.
find
(page);
if
(entry->
second
.
owners
== std::numeric_limits<std::
size_t
>::
max
()) {
overflow =
true
;
return
false
;
}
return
true
;
});
if
(overflow) {
erase_unlocked_pages
(first, last);
return
false
;
}
for_each_page
(first, last, [
this
](std::
uintptr_t
page) {
typename
page_states_type::iterator entry = page_states_.
find
(page);
page_state& state = entry->
second
;
if
(state.
owners
!=
0
|| state.
os_locked
) {
return
true
;
}
if
(!locker_.
lock_page
(
reinterpret_cast
<
void
*>(page), page_size_)) {
return
false
;
}
state.
os_locked
=
true
;
state.
pending_lock
=
true
;
return
true
;
});
if
(
has_unlocked_page
(first, last)) {
rollback_pending_locks
(first, last);
return
false
;
}
for_each_page
(first, last, [
this
](std::
uintptr_t
page) {
typename
page_states_type::iterator entry = page_states_.
find
(page);
++entry->
second
.
owners
;
entry->
second
.
pending_lock
=
false
;
return
true
;
});
return
true
;
}
bool
has_unlocked_page
(std::
uintptr_t
first, std::
uintptr_t
last)
const
noexcept
{
for
(std::
uintptr_t
page = first;; page += page_size_) {
const
typename
page_states_type::const_iterator entry = page_states_.
find
(page);
if
(entry == page_states_.
end
() || (!entry->
second
.
os_locked
&& entry->
second
.
owners
==
0
)) {
return
true
;
}
if
(page == last) {
return
false
;
}
}
}
void
erase_unlocked_pages
(std::
uintptr_t
first, std::
uintptr_t
last)
noexcept
{
for_each_page
(first, last, [
this
](std::
uintptr_t
page) {
typename
page_states_type::iterator entry = page_states_.
find
(page);
if
(entry != page_states_.
end
() && entry->
second
.
owners
==
0
&& !entry->
second
.
os_locked
) {
page_states_.
erase
(entry);
}
return
true
;
});
}
void
rollback_pending_locks
(std::
uintptr_t
first, std::
uintptr_t
last)
noexcept
{
for_each_page
(first, last, [
this
](std::
uintptr_t
page) {
typename
page_states_type::iterator entry = page_states_.
find
(page);
if
(entry == page_states_.
end
()) {
return
true
;
}
page_state& state = entry->
second
;
if
(state.
pending_lock
) {
if
(locker_.
unlock_page
(
reinterpret_cast
<
void
*>(page), page_size_)) {
page_states_.
erase
(entry);
}
else
{
state.
pending_lock
=
false
;
}
}
else
if
(state.
owners
==
0
&& !state.
os_locked
) {
page_states_.
erase
(entry);
}
return
true
;
});
}
std::
size_t
page_size_{};
PageLocker locker_;
page_states_type page_states_;
Mutex mutex_;
};
}
//
namespace detail
}
//
namespace hmac_cpp
#
endif
//
HMAC_CPP_PAGE_LOCK_REGISTRY_HPP
Back
|
FazBrowse Home
|
New Git URL