File tree Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ Add ` MremapFlags::MREMAP_DONTUNMAP ` to ` sys::mman::mremap ` for linux target.
Original file line number Diff line number Diff line change @@ -205,6 +205,10 @@ libc_bitflags! {
205
205
/// Place the mapping at exactly the address specified in `new_address`.
206
206
#[ cfg( target_os = "linux" ) ]
207
207
MREMAP_FIXED ;
208
+ /// Works in conjunction with `MREMAP_MAYMOVE` but does not unmap `old_address`.
209
+ /// Note that, in this case, `old_size` and `new_size` must be the same.
210
+ #[ cfg( target_os = "linux" ) ]
211
+ MREMAP_DONTUNMAP ;
208
212
/// Place the mapping at exactly the address specified in `new_address`.
209
213
#[ cfg( target_os = "netbsd" ) ]
210
214
MAP_FIXED ;
Original file line number Diff line number Diff line change @@ -119,3 +119,40 @@ fn test_mremap_shrink() {
119
119
// The first KB should still be accessible and have the old data in it.
120
120
assert_eq ! ( slice[ ONE_K - 1 ] , 0xFF ) ;
121
121
}
122
+
123
+ #[ test]
124
+ #[ cfg( target_os = "linux" ) ]
125
+ fn test_mremap_dontunmap ( ) {
126
+ use nix:: libc:: size_t;
127
+ use nix:: sys:: mman:: { mremap, MRemapFlags } ;
128
+ use std:: num:: NonZeroUsize ;
129
+ use std:: ptr:: NonNull ;
130
+
131
+ const ONE_K : size_t = 1024 ;
132
+ let one_k_non_zero = NonZeroUsize :: new ( ONE_K ) . unwrap ( ) ;
133
+
134
+ let slice: & mut [ u8 ] = unsafe {
135
+ let mem = mmap_anonymous (
136
+ None ,
137
+ one_k_non_zero,
138
+ ProtFlags :: PROT_READ | ProtFlags :: PROT_WRITE ,
139
+ MapFlags :: MAP_PRIVATE ,
140
+ )
141
+ . unwrap ( ) ;
142
+ std:: slice:: from_raw_parts_mut ( mem. as_ptr ( ) . cast ( ) , ONE_K )
143
+ } ;
144
+
145
+ // because we do not unmap `slice`, `old_size` and `new_size`
146
+ // need to be equal or `EINVAL` is set.
147
+ let _new_slice: & mut [ u8 ] = unsafe {
148
+ let mem = mremap (
149
+ NonNull :: from ( & mut slice[ ..] ) . cast ( ) ,
150
+ ONE_K ,
151
+ ONE_K ,
152
+ MRemapFlags :: MREMAP_MAYMOVE | MRemapFlags :: MREMAP_DONTUNMAP ,
153
+ None ,
154
+ )
155
+ . unwrap ( ) ;
156
+ std:: slice:: from_raw_parts_mut ( mem. cast ( ) . as_ptr ( ) , 10 * ONE_K )
157
+ } ;
158
+ }
You can’t perform that action at this time.
0 commit comments