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
use std::ptr; use std::cell::RefCell; use page::Page; thread_local!(static CURRENT_PAGE: RefCell<*mut Page> = RefCell::new(ptr::null_mut())); pub struct Region { current_page: *mut Page, } impl Region { pub fn get() -> Region { unsafe { let current_page = Region::get_page(); if current_page.is_null() { panic!("Out of memory."); } Region { current_page: current_page } } } unsafe fn get_page() -> *mut Page { let mut page: *mut Page = ptr::null_mut(); CURRENT_PAGE.with(|p| { let pagerefborrow = *p.borrow(); if pagerefborrow.is_null() { *p.borrow_mut() = Page::next(ptr::null_mut()); } page = *p.borrow(); }); page } pub fn get_current_page(&self) -> *mut Page { self.current_page } } impl Drop for Region { fn drop(&mut self) { unsafe { (*(self.current_page)).deallocate(); } } }