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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use crateMemoryError;
use ;
/// Allocates page-alined memory dynamically.
///
/// This function provides a cross-platform abstraction for memory allocation,
/// using `mmap` on Unix-like systems and `VirtualAlloc` on Windows. The allocated
/// memory is anonymous, meaning it is not backed by a file and is zero-initialized
/// by the OS.
///
/// # Arguments
///
/// * `len` - The size of the memory allocation in bytes.
///
/// # Returns
///
/// * `Ok(*mut T)` - A pointer to the allocated memory if the allocation succeeds.
/// * `Err(MemoryError)` - A memory allocation error if the operation fails.
///
/// # Platform-specific Behavior
///
/// * **Unix**: Uses `mmap` with `PROT_READ | PROT_WRITE` and `MAP_PRIVATE | MAP_ANONYMOUS`.
/// * **Windows**: Uses `VirtualAlloc` with `MEM_COMMIT | MEM_RESERVE` and `PAGE_READWRITE`.
///
/// # Safety
///
/// The returned pointer is uninitialized and must be properly managed by the caller.
/// Ensure that the allocated memory is deallocated appropriately to prevent memory leaks.
/// Deallocates previously allocated page-aligned memory.
///
/// This function provides a cross-platform abstraction for memory deallocation,
/// using `munmap` on Unix-like systems and `VirtualFree` on Windows. It ensures that
/// memory is properly released and can be reclaimed by the operating system.
///
/// # Arguments
///
/// * `ptr` - A pointer to the memory that was previously allocated.
/// * `len` - The size of the allocated memory in bytes. This must match the size
/// originally passed to [`mem_alloc`](fn.mem_alloc.html).
///
/// # Returns
///
/// * `Ok(())` - If the deallocation succeeds.
/// * `Err(MemoryError)` - If the operation fails.
///
/// # Platform-specific Behavior
///
/// * **Unix**: Uses `munmap` to release the memory.
/// * **Windows**: Uses `VirtualFree` with `MEM_DECOMMIT` to deallocate the memory.
///
/// # Safety
///
/// * The `ptr` must be a valid, non-null pointer returned by [`mem_alloc`](fn.mem_alloc.html).
/// * The `len` must be correct, as passing an incorrect size may cause undefined behavior.
/// * After deallocation, the pointer must not be accessed again.
/// Marks a memory region as inaccessible.
///
/// This function changes the protection settings of a previously allocated memory region
/// to prevent any read, write, or execute access.
///
/// # Arguments
///
/// * `ptr` - A pointer to the memory region.
/// * `len` - The size of the memory region in bytes.
///
/// # Returns
///
/// * `Ok(())` - If the operation succeeds.
/// * `Err(MemoryError)` - If the operation fails.
///
/// # Platform-specific Behavior
///
/// * **Unix**: Uses `mprotect` with `PROT_NONE`, making the memory completely inaccessible.
/// * **Windows**: Uses `VirtualProtect` with `PAGE_NOACCESS`, denying all access.
///
/// # Safety
///
/// * `ptr` must be a valid, non-null pointer to an allocated memory region.
/// * `len` must be correct, matching the size of the allocated region.
/// * Accessing the memory after calling this function will trigger a segmentation fault (Unix) or
/// access violation (Windows).
/// Marks a memory region as read-only.
///
/// This function modifies the protection settings of a previously allocated memory region
/// to allow read access while preventing writes.
///
/// # Arguments
///
/// * `ptr` - A pointer to the memory region.
/// * `len` - The size of the memory region in bytes.
///
/// # Returns
///
/// * `Ok(())` - If the operation succeeds.
/// * `Err(MemoryError)` - If the operation fails.
///
/// # Platform-specific Behavior
///
/// * **Unix**: Uses `mprotect` with `PROT_READ`, making the memory readable but not writable or executable.
/// * **Windows**: Uses `VirtualProtect` with `PAGE_READONLY`, allowing only read access.
///
/// # Safety
///
/// * `ptr` must be a valid, non-null pointer to an allocated memory region.
/// * `len` must be correct, matching the size of the allocated region.
/// * Writing to the memory after calling this function will trigger a segmentation fault
/// (Unix) or an access violation (Windows).
/// Marks a memory region as readable and writable.
///
/// This function modifies the protection settings of a previously allocated memory region
/// to allow both read and write access.
///
/// # Arguments
///
/// * `ptr` - A pointer to the memory region.
/// * `len` - The size of the memory region in bytes.
///
/// # Returns
///
/// * `Ok(())` - If the operation succeeds.
/// * `Err(MemoryError)` - If the operation fails.
///
/// # Platform-specific Behavior
///
/// * **Unix**: Uses `mprotect` with `PROT_READ | PROT_WRITE`, enabling both read and write access.
/// * **Windows**: Uses `VirtualProtect` with `PAGE_READWRITE`, allowing both read and write operations.
///
/// # Safety
///
/// * `ptr` must be a valid, non-null pointer to an allocated memory region.
/// * `len` must be correct, matching the size of the allocated region.
/// Locks a memory region to prevent it from being paged out into swap memory.
///
/// This function ensures that the specified memory region remains in physical
/// Random Access Memory (RAM) and is not swapped to disk by the operating system.
///
/// # Arguments
///
/// * `ptr` - A pointer to the memory region.
/// * `len` - The size of the memory region in bytes.
///
/// # Returns
///
/// * `Ok(())` - If the operation succeeds.
/// * `Err(MemoryError)` - If the operation fails.
///
/// # Platform-specific Behavior
///
/// * **Unix**: Uses `mlock`, which prevents the specified memory range from being swapped out.
/// * **Windows**: Uses `VirtualLock`, which locks the memory in RAM and prevents paging.
/// - **Windows Limitation**:
/// - All pages in the specified region must be committed.
/// - Memory protected with `PAGE_NOACCESS` cannot be locked.
///
/// # Safety
///
/// * `ptr` must be a valid, non-null pointer to an allocated memory region.
/// * `len` must be correct, matching the size of the allocated region.
/// * Excessive use of locked memory may cause system-wide performance degradation.
/// * On some systems, locking memory may require elevated privileges.
/// Unlocks a previously locked memory region, allowing it to be paged out.
///
/// This function reverses the effect of [`mem_lock`](fn.mem_lock.html), allowing the operating system
/// to move the specified memory region back into the page file or swap space if necessary.
///
/// # Arguments
///
/// * `ptr` - A pointer to the memory region.
/// * `len` - The size of the memory region in bytes.
///
/// # Returns
///
/// * `Ok(())` - If the operation succeeds.
/// * `Err(MemoryError)` - If the operation fails.
///
/// # Platform-specific Behavior
///
/// * **Unix**: Uses `munlock`, allowing the specified memory range to be swapped out.
/// * **Windows**: Uses `VirtualUnlock`, allowing the memory to be paged.
///
/// # Safety
///
/// * `ptr` must be a valid, non-null pointer to an allocated memory region.
/// * `len` must be correct, matching the size of the locked region.
/// * Unlocking memory that was never locked may result in undefined behavior on some platforms.