<commit_before>use std::prelude::v1::*;
use std::mem::{transmute};
use libc;
use rand;
use std::mem::size_of;

// The types we want to test:
use super::{ZoneAllocator, SlabPage, SlabAllocator, SlabPageProvider};

#[cfg(target_arch="x86_64")]
use x86::paging::{BASE_PAGE_SIZE};

use test::Bencher;

/// Page allocator based on mmap/munmap system calls for backing slab memory.
struct MmapPageProvider {
    currently_allocated: usize
}

impl MmapPageProvider {
    pub fn new() -> MmapPageProvider {
        MmapPageProvider{ currently_allocated: 0 }
    }
}

impl MmapPageProvider {
    pub fn currently_allocated(&self) -> usize {
        self.currently_allocated
    }
}

impl<'a> SlabPageProvider<'a> for MmapPageProvider {

    /// Allocates a new SlabPage from the system.
    ///
    /// Uses `mmap` to map a page and casts it to a SlabPage.
    fn allocate_slabpage(&mut self) -> Option<&'a mut SlabPage<'a>> {
        let mut addr: libc::c_void = libc::c_void::__variant1;
        let len: libc::size_t = BASE_PAGE_SIZE;
        let prot = libc::PROT_READ | libc::PROT_WRITE;
        let flags = libc::MAP_PRIVATE | libc::MAP_ANON;
        let fd = -1;
        let offset = 0;
        let r = unsafe { libc::mmap(&mut addr, len as libc::size_t, prot, flags, fd, offset) };
        if r == libc::MAP_FAILED {
            return None;
        }
        else {
            let mut slab_page: &'a mut SlabPage = unsafe { transmute(r as usize) };
            self.currently_allocated += 1;
            return Some(slab_page);
        }
    }

    /// Release a SlabPage back to the system.slab_page
    ///
    /// Uses `munmap` to release the page back to the OS.
    fn release_slabpage(&mut self, p: &'a mut SlabPage<'a>) {
        let addr: *mut libc::c_void = unsafe { transmute(p) };
        let len: libc::size_t = BASE_PAGE_SIZE;
        let r = unsafe { libc::munmap(addr, len) };
        if r != 0 {
            panic!("munmap failed!");
        }
        self.currently_allocated -= 1;
    }
<commit_msg>Fix tests.<commit_after>use std::prelude::v1::*;
use std::mem::{transmute};
use libc;
use rand;
use std::mem::size_of;


// The types we want to test:
use super::{ZoneAllocator, SlabPage, SlabAllocator, SlabPageProvider, BASE_PAGE_SIZE};

use test::Bencher;

/// Page allocator based on mmap/munmap system calls for backing slab memory.
struct MmapPageProvider {
    currently_allocated: usize
}

impl MmapPageProvider {
    pub fn new() -> MmapPageProvider {
        MmapPageProvider{ currently_allocated: 0 }
    }
}

impl MmapPageProvider {
    pub fn currently_allocated(&self) -> usize {
        self.currently_allocated
    }
}

impl<'a> SlabPageProvider<'a> for MmapPageProvider {

    /// Allocates a new SlabPage from the system.
    ///
    /// Uses `mmap` to map a page and casts it to a SlabPage.
    fn allocate_slabpage(&mut self) -> Option<&'a mut SlabPage<'a>> {
        let mut addr: libc::c_void = libc::c_void::__variant1;
        let len: libc::size_t = BASE_PAGE_SIZE as u64;
        let prot = libc::PROT_READ | libc::PROT_WRITE;
        let flags = libc::MAP_PRIVATE | libc::MAP_ANON;
        let fd = -1;
        let offset = 0;
        let r = unsafe { libc::mmap(&mut addr, len as libc::size_t, prot, flags, fd, offset) };
        if r == libc::MAP_FAILED {
            return None;
        }
        else {
            let mut slab_page: &'a mut SlabPage = unsafe { transmute(r as usize) };
            self.currently_allocated += 1;
            return Some(slab_page);
        }
    }

    /// Release a SlabPage back to the system.slab_page
    ///
    /// Uses `munmap` to release the page back to the OS.
    fn release_slabpage(&mut self, p: &'a mut SlabPage<'a>) {
        let addr: *mut libc::c_void = unsafe { transmute(p) };
        let len: libc::size_t = BASE_PAGE_SIZE as u64;
        let r = unsafe { libc::munmap(addr, len) };
        if r != 0 {
            panic!("munmap failed!");
        }
        self.currently_allocated -= 1;
    }

{'fraction_non_alphanumeric': 0.12428107026756689, 'fraction_numerical': 0.006501625406351588, 'mean_word_length': 2.5366931918656057, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '51035', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1340, 'n_words': 377}