membase 0.2.1

Ultra-high performance async runtime with fast task cycling, lock-free scheduling, and zero-overhead performance
Documentation
//! Huge page support for memory mapping.
//!
//! This module provides functionality for checking and using huge pages
//! for memory mapping.
#![allow(unused_variables)]

use crate::advanced::HugePageSize;

/// Check if huge pages are supported on the current system.
#[inline]
pub fn is_supported() -> bool {
    #[cfg(target_os = "linux")]
    {
        // Check if huge pages are enabled in the kernel
        if let Ok(mut file) = File::open("/proc/meminfo") {
            let mut contents = String::new();
            if file.read_to_string(&mut contents).is_ok() {
                return contents.contains("HugePages_Total") && !contents.contains("HugePages_Total: 0");
            }
        }
        false
    }
    
    #[cfg(windows)]
    {
        // Check if large pages are enabled in Windows
        // This is a simplified check - in a real implementation, you would
        // check for the SeLockMemoryPrivilege privilege
        false
    }
    
    #[cfg(not(any(target_os = "linux", windows)))]
    {
        // Huge pages are not supported on other platforms
        false
    }
}

/// Get the default huge page size on the current system.
#[inline]
pub fn default_huge_page_size() -> Option<HugePageSize> {
    #[cfg(target_os = "linux")]
    {
        // Check the default huge page size on Linux
        if let Ok(mut file) = File::open("/proc/meminfo") {
            let mut contents = String::new();
            if file.read_to_string(&mut contents).is_ok() {
                // Look for the Hugepagesize line
                for line in contents.lines() {
                    if line.starts_with("Hugepagesize:") {
                        let parts: Vec<&str> = line.split_whitespace().collect();
                        if parts.len() >= 2 {
                            if let Ok(size) = parts[1].parse::<usize>() {
                                // Size is in KB
                                if size >= 1048576 {
                                    // 1GB or larger
                                    return Some(HugePageSize::OneGB);
                                } else if size >= 2048 {
                                    // 2MB or larger
                                    return Some(HugePageSize::TwoMB);
                                }
                            }
                        }
                    }
                }
            }
        }
        // Default to 2MB if we couldn't determine the size
        Some(HugePageSize::TwoMB)
    }
    
    #[cfg(windows)]
    {
        // Windows typically uses 2MB large pages
        Some(HugePageSize::TwoMB)
    }
    
    #[cfg(not(any(target_os = "linux", windows)))]
    {
        // Huge pages are not supported on other platforms
        None
    }
}

/// Check if 1GB huge pages are supported on the current system.
#[inline]
pub fn is_1gb_supported() -> bool {
    #[cfg(target_os = "linux")]
    {
        // Check if 1GB huge pages are enabled in the kernel
        if let Ok(mut file) = File::open("/sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages") {
            let mut contents = String::new();
            if file.read_to_string(&mut contents).is_ok() {
                if let Ok(count) = contents.trim().parse::<usize>() {
                    return count > 0;
                }
            }
        }
        false
    }
    
    #[cfg(not(target_os = "linux"))]
    {
        // 1GB huge pages are primarily a Linux feature
        false
    }
}