@@ -12,6 +12,7 @@ use winapi::um::winnt::{
12
12
unsafe impl Send for Memory { }
13
13
unsafe impl Sync for Memory { }
14
14
15
+ /// Data for a sized and protected region of memory.
15
16
#[ derive( Debug ) ]
16
17
pub struct Memory {
17
18
ptr : * mut u8 ,
@@ -20,6 +21,7 @@ pub struct Memory {
20
21
}
21
22
22
23
impl Memory {
24
+ /// Create a new memory from the given path value and protection.
23
25
pub fn with_size_protect ( size : usize , protection : Protect ) -> Result < Self , String > {
24
26
if size == 0 {
25
27
return Ok ( Self {
@@ -52,6 +54,7 @@ impl Memory {
52
54
}
53
55
}
54
56
57
+ /// Create a new memory with the given size.
55
58
pub fn with_size ( size : usize ) -> Result < Self , MemoryCreationError > {
56
59
if size == 0 {
57
60
return Ok ( Self {
@@ -79,6 +82,7 @@ impl Memory {
79
82
}
80
83
}
81
84
85
+ /// Protect this memory with the given range bounds and protection.
82
86
pub unsafe fn protect (
83
87
& mut self ,
84
88
range : impl RangeBounds < usize > ,
@@ -120,6 +124,7 @@ impl Memory {
120
124
}
121
125
}
122
126
127
+ /// Split this memory into multiple memories by the given offset.
123
128
pub fn split_at ( mut self , offset : usize ) -> ( Memory , Memory ) {
124
129
let page_size = page_size:: get ( ) ;
125
130
if offset % page_size == 0 {
@@ -140,22 +145,27 @@ impl Memory {
140
145
}
141
146
}
142
147
148
+ /// Gets the size of this memory.
143
149
pub fn size ( & self ) -> usize {
144
150
self . size
145
151
}
146
152
153
+ /// Gets a slice for this memory.
147
154
pub unsafe fn as_slice ( & self ) -> & [ u8 ] {
148
155
slice:: from_raw_parts ( self . ptr , self . size )
149
156
}
150
157
158
+ /// Gets a mutable slice for this memory.
151
159
pub unsafe fn as_slice_mut ( & mut self ) -> & mut [ u8 ] {
152
160
slice:: from_raw_parts_mut ( self . ptr , self . size )
153
161
}
154
162
163
+ /// Gets the protect kind of this memory.
155
164
pub fn protection ( & self ) -> Protect {
156
165
self . protection
157
166
}
158
167
168
+ /// Gets mutable pointer to the memory.
159
169
pub fn as_ptr ( & self ) -> * mut u8 {
160
170
self . ptr
161
171
}
@@ -192,12 +202,17 @@ impl Clone for Memory {
192
202
}
193
203
}
194
204
205
+ /// Kinds of memory protection.
195
206
#[ derive( Serialize , Deserialize , Debug , Copy , Clone , PartialEq , Eq ) ]
196
207
#[ allow( dead_code) ]
197
208
pub enum Protect {
209
+ /// Read/write/exec allowed.
198
210
None ,
211
+ /// Read only.
199
212
Read ,
213
+ /// Read/write only.
200
214
ReadWrite ,
215
+ /// Read/exec only.
201
216
ReadExec ,
202
217
}
203
218
@@ -211,13 +226,15 @@ impl Protect {
211
226
}
212
227
}
213
228
229
+ /// Returns true if this memory is readable.
214
230
pub fn is_readable ( self ) -> bool {
215
231
match self {
216
232
Protect :: Read | Protect :: ReadWrite | Protect :: ReadExec => true ,
217
233
_ => false ,
218
234
}
219
235
}
220
236
237
+ /// Returns true if this memory is writable.
221
238
pub fn is_writable ( self ) -> bool {
222
239
match self {
223
240
Protect :: ReadWrite => true ,
0 commit comments