membase 0.2.1

Ultra-high performance async runtime with fast task cycling, lock-free scheduling, and zero-overhead performance
Documentation
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Core memory mapping functionality.
//!
//! This module provides the primary interfaces for memory mapping operations,
//! with a focus on performance and safety.

use std::fs::File;
use std::ops::{Deref, DerefMut};
use std::slice;
use std::sync::atomic::{AtomicUsize, Ordering};

use crate::error::{Error, Result};
use crate::platform;
use crate::advanced::{HugePageSize, NumaPolicy, PrefetchStrategy};

/// Statistics for memory mapping operations
static TOTAL_MAPPED_MEMORY: AtomicUsize = AtomicUsize::new(0);
static ACTIVE_MAPPINGS: AtomicUsize = AtomicUsize::new(0);

/// Configuration options for memory mapping.
#[derive(Debug, Clone)]
pub struct MmapOptions {
    /// The offset within the file at which the memory map will start.
    offset: u64,
    
    /// The length of the memory map.
    len: Option<usize>,
    
    /// Whether the memory map should be readable.
    pub readable: bool,
    
    /// Whether the memory map should be writable.
    pub writable: bool,
    
    /// Whether the memory map should be executable.
    pub executable: bool,
    
    /// Whether the memory map should be backed by huge pages.
    pub huge_pages: Option<HugePageSize>,
    
    /// NUMA policy for the memory map.
    pub numa_policy: Option<NumaPolicy>,
    
    /// Prefetching strategy for the memory map.
    pub prefetch: Option<PrefetchStrategy>,
    
    /// Stack support (MAP_STACK on Unix).
    pub stack: bool,
    
    /// Whether to use copy-on-write semantics.
    pub copy_on_write: bool,
    
    /// Whether to populate (prefault) the map.
    pub populate: bool,
    
    /// Custom alignment for the memory map.
    pub alignment: Option<usize>,
}

impl Default for MmapOptions {
    fn default() -> MmapOptions {
        MmapOptions {
            offset: 0,
            len: None,
            readable: true,
            writable: false,
            executable: false,
            huge_pages: None,
            numa_policy: None,
            prefetch: None,
            stack: false,
            copy_on_write: false,
            populate: false,
            alignment: None,
        }
    }
}

impl MmapOptions {
    /// Create a new set of options for configuring memory maps.
    #[inline]
    pub fn new() -> MmapOptions {
        MmapOptions::default()
    }

    /// Set the offset from the start of the file to start the memory map.
    ///
    /// This offset will be rounded down to the nearest multiple of the page size.
    #[inline]
    pub fn offset(mut self, offset: u64) -> MmapOptions {
        self.offset = offset;
        self
    }

    /// Set the length of the memory map.
    #[inline]
    pub fn len(mut self, len: usize) -> MmapOptions {
        self.len = Some(len);
        self
    }

    /// Configure the memory map to be readable.
    #[inline]
    pub fn read(mut self, readable: bool) -> MmapOptions {
        self.readable = readable;
        self
    }

    /// Configure the memory map to be writable.
    #[inline]
    pub fn write(mut self, writable: bool) -> MmapOptions {
        self.writable = writable;
        self
    }

    /// Configure the memory map to be executable.
    #[inline]
    pub fn exec(mut self, executable: bool) -> MmapOptions {
        self.executable = executable;
        self
    }

    /// Configure the memory map to use huge pages.
    #[inline]
    pub fn huge_pages(mut self, size: HugePageSize) -> MmapOptions {
        self.huge_pages = Some(size);
        self
    }

    /// Configure the memory map with a NUMA policy.
    #[inline]
    pub fn numa_policy(mut self, policy: NumaPolicy) -> MmapOptions {
        self.numa_policy = Some(policy);
        self
    }

    /// Configure the prefetching strategy.
    #[inline]
    pub fn prefetch(mut self, strategy: PrefetchStrategy) -> MmapOptions {
        self.prefetch = Some(strategy);
        self
    }

    /// Configure the memory map for stack usage (MAP_STACK on Unix).
    #[inline]
    pub fn stack(mut self, stack: bool) -> MmapOptions {
        self.stack = stack;
        self
    }

    /// Configure the memory map to use copy-on-write semantics.
    #[inline]
    pub fn copy_on_write(mut self, cow: bool) -> MmapOptions {
        self.copy_on_write = cow;
        self
    }

    /// Configure the memory map to be pre-populated (prefaulted).
    #[inline]
    pub fn populate(mut self, populate: bool) -> MmapOptions {
        self.populate = populate;
        self
    }

    /// Configure a custom alignment for the memory map.
    #[inline]
    pub fn alignment(mut self, alignment: usize) -> MmapOptions {
        self.alignment = Some(alignment);
        self
    }

    /// Create a read-only memory map backed by a file.
    ///
    /// # Safety
    ///
    /// This function is unsafe because it creates a memory map that can be accessed
    /// while the file is being modified by external processes, which might lead to
    /// undefined behavior.
    #[inline]
    pub unsafe fn map(&self, file: &File) -> Result<Mmap> {
        self.map_impl(file).map(|raw| Mmap { inner: raw })
    }

    /// Create a writable memory map backed by a file.
    ///
    /// # Safety
    ///
    /// This function is unsafe because it creates a memory map that can modify the
    /// file, which might lead to undefined behavior if the file is being accessed
    /// by other processes.
    #[inline]
    pub unsafe fn map_mut(&self, file: &File) -> Result<MmapMut> {
        let mut options = self.clone();
        options.writable = true;
        options.map_impl(file).map(|raw| MmapMut { inner: raw })
    }

    /// Create an anonymous memory map not backed by a file.
    ///
    /// # Safety
    ///
    /// This function is unsafe because it creates a memory map that can be accessed
    /// and modified, which might lead to undefined behavior if not used correctly.
    #[inline]
    pub unsafe fn map_anon(&self, len: usize) -> Result<MmapMut> {
        let mut options = self.clone();
        options.len = Some(len);
        options.map_anon_impl().map(|raw| MmapMut { inner: raw })
    }

    /// Implementation of file-backed memory mapping.
    unsafe fn map_impl(&self, file: &File) -> Result<MmapRaw> {
        // Validate options
        if let Some(len) = self.len {
            if len == 0 {
                return Err(Error::ZeroSizedMapping);
            }
        }

        // Get file length if not specified
        let len = match self.len {
            Some(len) => len,
            None => {
                let metadata = file.metadata()?;
                metadata.len().try_into().map_err(|_| Error::SizeExceedsSystemLimit)?
            }
        };

        // Perform platform-specific mapping
        let raw = platform::map_file(
            file,
            self.offset,
            len,
            self.readable,
            self.writable,
            self.executable,
            self.huge_pages,
            self.numa_policy.clone(),  // Clone here
            self.stack,
            self.copy_on_write,
            self.populate,
            self.alignment,
        )?;

        // Update statistics
        TOTAL_MAPPED_MEMORY.fetch_add(len, Ordering::Relaxed);
        ACTIVE_MAPPINGS.fetch_add(1, Ordering::Relaxed);

        // Apply prefetching if requested
        if let Some(strategy) = self.prefetch {
            crate::advanced::prefetch::apply_strategy(raw.ptr, len, strategy);
        }

        Ok(raw)
    }

    /// Implementation of anonymous memory mapping.
    unsafe fn map_anon_impl(&self) -> Result<MmapRaw> {
        let len = self.len.ok_or(Error::InvalidArgument("Length must be specified for anonymous mapping".into()))?;
        
        if len == 0 {
            return Err(Error::ZeroSizedMapping);
        }

        // Perform platform-specific anonymous mapping
        let raw = platform::map_anon(
            len,
            self.readable,
            self.writable,
            self.executable,
            self.huge_pages,
            self.numa_policy,
            self.stack,
            self.populate,
            self.alignment,
        )?;

        // Update statistics
        TOTAL_MAPPED_MEMORY.fetch_add(len, Ordering::Relaxed);
        ACTIVE_MAPPINGS.fetch_add(1, Ordering::Relaxed);

        // Apply prefetching if requested
        if let Some(strategy) = self.prefetch {
            crate::advanced::prefetch::apply_strategy(raw.ptr, len, strategy);
        }

        Ok(raw)
    }
}

/// Raw memory map handle.
#[derive(Debug)]
pub struct MmapRaw {
    /// Pointer to the mapped memory.
    pub(crate) ptr: *mut u8,
    
    /// Length of the mapped memory.
    pub(crate) len: usize,
}

impl MmapRaw {
    /// Flush the memory map to disk.
    ///
    /// This function will flush the entire memory map to disk, ensuring that all
    /// changes are persisted.
    #[inline]
    pub fn flush(&self) -> Result<()> {
        unsafe { platform::flush(self.ptr, self.len, false) }
    }

    /// Flush the memory map to disk asynchronously.
    ///
    /// This function will initiate an asynchronous flush of the entire memory map
    /// to disk, but may return before the flush is complete.
    #[inline]
    pub fn flush_async(&self) -> Result<()> {
        unsafe { platform::flush(self.ptr, self.len, true) }
    }

    /// Advise the kernel about how the memory map will be accessed.
    #[inline]
    pub fn advise(&self, advice: platform::Advice) -> Result<()> {
        unsafe { platform::advise(self.ptr, self.len, advice) }
    }
}

impl Drop for MmapRaw {
    fn drop(&mut self) {
        // Safety: We're ensuring proper cleanup of the memory map
        unsafe {
            if !self.ptr.is_null() {
                // Update statistics
                ACTIVE_MAPPINGS.fetch_sub(1, Ordering::Relaxed);
                TOTAL_MAPPED_MEMORY.fetch_sub(self.len, Ordering::Relaxed);
                
                // Unmap the memory
                let _ = platform::unmap(self.ptr, self.len);
            }
        }
    }
}

/// A read-only memory map.
#[derive(Debug)]
pub struct Mmap {
    inner: MmapRaw,
}

impl Mmap {
    /// Create a read-only memory map backed by a file.
    ///
    /// # Safety
    ///
    /// This function is unsafe because it creates a memory map that can be accessed
    /// while the file is being modified by external processes, which might lead to
    /// undefined behavior.
    #[inline]
    pub unsafe fn map(file: &File) -> Result<Mmap> {
        MmapOptions::new().map(file)
    }

    /// Return the length of the memory map.
    #[inline]
    pub fn len(&self) -> usize {
        self.inner.len
    }

    /// Return true if the memory map is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.inner.len == 0
    }

    /// Return a pointer to the memory map.
    ///
    /// # Safety
    ///
    /// The caller must ensure that the memory map outlives any pointers derived
    /// from this function.
    #[inline]
    pub fn as_ptr(&self) -> *const u8 {
        self.inner.ptr
    }

    /// Flush the memory map to disk.
    #[inline]
    pub fn flush(&self) -> Result<()> {
        self.inner.flush()
    }

    /// Flush the memory map to disk asynchronously.
    #[inline]
    pub fn flush_async(&self) -> Result<()> {
        self.inner.flush_async()
    }

    /// Advise the kernel about how the memory map will be accessed.
    #[inline]
    pub fn advise(&self, advice: platform::Advice) -> Result<()> {
        self.inner.advise(advice)
    }
}

impl Deref for Mmap {
    type Target = [u8];

    #[inline]
    fn deref(&self) -> &[u8] {
        unsafe { slice::from_raw_parts(self.inner.ptr, self.inner.len) }
    }
}

impl AsRef<[u8]> for Mmap {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self
    }
}

/// A writable memory map.
#[derive(Debug)]
pub struct MmapMut {
    inner: MmapRaw,
}

impl MmapMut {
    /// Create a writable memory map backed by a file.
    ///
    /// # Safety
    ///
    /// This function is unsafe because it creates a memory map that can modify the
    /// file, which might lead to undefined behavior if the file is being accessed
    /// by other processes.
    #[inline]
    pub unsafe fn map(file: &File) -> Result<MmapMut> {
        MmapOptions::new().write(true).map_mut(file)
    }

    /// Create an anonymous memory map not backed by a file.
    ///
    /// # Safety
    ///
    /// This function is unsafe because it creates a memory map that can be accessed
    /// and modified, which might lead to undefined behavior if not used correctly.
    #[inline]
    pub unsafe fn map_anon(len: usize) -> Result<MmapMut> {
        MmapOptions::new().write(true).map_anon(len)
    }

    /// Return the length of the memory map.
    #[inline]
    pub fn len(&self) -> usize {
        self.inner.len
    }

    /// Return true if the memory map is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.inner.len == 0
    }

    /// Return a pointer to the memory map.
    ///
    /// # Safety
    ///
    /// The caller must ensure that the memory map outlives any pointers derived
    /// from this function.
    #[inline]
    pub fn as_ptr(&self) -> *const u8 {
        self.inner.ptr
    }

    /// Return a mutable pointer to the memory map.
    ///
    /// # Safety
    ///
    /// The caller must ensure that the memory map outlives any pointers derived
    /// from this function, and that no data races occur.
    #[inline]
    pub fn as_mut_ptr(&mut self) -> *mut u8 {
        self.inner.ptr
    }

    /// Flush the memory map to disk.
    #[inline]
    pub fn flush(&self) -> Result<()> {
        self.inner.flush()
    }

    /// Flush the memory map to disk asynchronously.
    #[inline]
    pub fn flush_async(&self) -> Result<()> {
        self.inner.flush_async()
    }

    /// Advise the kernel about how the memory map will be accessed.
    #[inline]
    pub fn advise(&self, advice: platform::Advice) -> Result<()> {
        self.inner.advise(advice)
    }
}

impl Deref for MmapMut {
    type Target = [u8];

    #[inline]
    fn deref(&self) -> &[u8] {
        unsafe { slice::from_raw_parts(self.inner.ptr, self.inner.len) }
    }
}

impl DerefMut for MmapMut {
    #[inline]
    fn deref_mut(&mut self) -> &mut [u8] {
        unsafe { slice::from_raw_parts_mut(self.inner.ptr, self.inner.len) }
    }
}

impl AsRef<[u8]> for MmapMut {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self
    }
}

impl AsMut<[u8]> for MmapMut {
    #[inline]
    fn as_mut(&mut self) -> &mut [u8] {
        self
    }
}

/// Get the total amount of memory currently mapped.
#[inline]
pub fn total_mapped_memory() -> usize {
    TOTAL_MAPPED_MEMORY.load(Ordering::Relaxed)
}

/// Get the number of active memory mappings.
#[inline]
pub fn active_mappings() -> usize {
    ACTIVE_MAPPINGS.load(Ordering::Relaxed)
}