daily pastebin goal
54%
SHARE
TWEET

Untitled

a guest Dec 13th, 2018 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extern crate libc; // 0.2.43
  2. extern crate lazy_static; // 1.2.0
  3.  
  4. use libc::sbrk;
  5. use lazy_static::lazy_static;
  6.  
  7. use std::sync::Mutex;
  8. use std::collections::LinkedList;
  9. use std::alloc::{GlobalAlloc, Layout, alloc};
  10. use std::ptr;
  11. use std::mem;
  12.  
  13. struct Allocator {
  14.     blocks: BlockPtr
  15. }
  16.  
  17. impl Allocator {
  18.     fn find_block(&self, size: isize) -> Option<*mut Block> {
  19.         let mut last = None;
  20.         let mut current = self.blocks.0;
  21.         unsafe {
  22.             while !current.is_null() && !((*current).free && (*current).size >= size) {
  23.                 last = Some(current);
  24.                 current = (*current).next.0;
  25.             }
  26.         }
  27.        
  28.         if current.is_null() {
  29.             None
  30.         } else {
  31.             Some(current)
  32.         }
  33.     }
  34.    
  35.     unsafe fn new_block(&mut self, size: isize) -> *mut u8 {
  36.         let block_ptr = sbrk(size + mem::size_of::<Block>() as isize) as *mut Block;
  37.         if block_ptr.is_null() {
  38.             return ptr::null_mut();
  39.         }
  40.        
  41.         if self.blocks.0.is_null() {
  42.             self.blocks = BlockPtr(block_ptr);
  43.             return block_ptr as *mut u8;
  44.         }
  45.        
  46.         /*let mut current = self.blocks.0;
  47.         while !(*current).next.0.is_null() {
  48.             current = (*current).next.0;
  49.         }
  50.        
  51.         (*current).next.0 = block_ptr;*/
  52.         block_ptr as *mut u8
  53.     }
  54.     fn test(&self) {}
  55. }
  56.  
  57.  
  58. fn needed_size(layout: &Layout) -> isize {
  59.     let size = layout.size();
  60.     let align = layout.align();
  61.     // This is technically the same as `size % align` in this case.
  62.     let remainder = size & (align - 1);
  63.     let diff = align - remainder;
  64.     (size + diff) as isize
  65. }
  66.  
  67. struct Block {
  68.     size: isize,
  69.     free: bool,
  70.     next: BlockPtr,
  71. }
  72.  
  73. struct BlockPtr(*mut Block);
  74.  
  75. impl Default for BlockPtr {
  76.     fn default() -> Self {
  77.         BlockPtr(ptr::null_mut())
  78.     }
  79. }
  80.  
  81. unsafe impl Sync for BlockPtr {}
  82. unsafe impl Send for BlockPtr {}
  83.  
  84. unsafe impl GlobalAlloc for Allocator {
  85.     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
  86.         let needed = needed_size(&layout);
  87.         //allocator.find_block(needed);
  88.         /*match allocator.find_block(needed) {
  89.             Some(block) => block as *mut u8,
  90.             None => allocator.new_block(needed),
  91.         };*/
  92.         let mut evil = self as *const Allocator as *mut Allocator;
  93.         (*evil).new_block(needed)
  94.         //sbrk(needed + mem::size_of::<Block>() as isize) as *mut u8
  95.     }
  96.     unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
  97. }
  98.  
  99. #[global_allocator]
  100. static allocator: Allocator = Allocator { blocks: BlockPtr(0 as *mut Block) };
  101.  
  102. fn main() {
  103.     println!("Hello world!");
  104.     let size = 64 + 32;
  105.     let align = 64;
  106. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top