GithubHelp home page GithubHelp logo

rcore-os / buddy_system_allocator Goto Github PK

View Code? Open in Web Editor NEW
82.0 7.0 22.0 63 KB

A buddy system allocator in pure Rust.

License: MIT License

Rust 100.00%
rust allocator buddy-system bare-metal-programming

buddy_system_allocator's Introduction

buddy_system_allocator

Crates.io version docs.rs

An (almost) drop-in replacement for phil-opp/linked-list-allocator. But it uses buddy system instead.

Usage

To use buddy_system_allocator for global allocator:

use buddy_system_allocator::LockedHeap;

#[global_allocator]
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::<32>::empty();

To init the allocator:

unsafe {
    HEAP_ALLOCATOR.lock().init(heap_start, heap_size);
    // or
    HEAP_ALLOCATOR.lock().add_to_heap(heap_start, heap_end);
}

You can also use FrameAllocator and LockedHeapWithRescue, see their documentation for usage.

Features

  • use_spin (default): Provide a LockedHeap type that implements the GlobalAlloc trait by using a spinlock.
  • const_fn (nightly only): Provide const fn version of LockedHeapWithRescue::new.

License

Some code comes from phil-opp's linked-list-allocator.

Licensed under MIT License. Thanks phill-opp's linked-list-allocator for inspirations and interface.

buddy_system_allocator's People

Contributors

dylan-dpc avatar fmckeogh avatar freax13 avatar jiegec avatar luojia65 avatar mrzleo avatar qix- avatar qwandor avatar stdrc avatar tsemo4917 avatar vinaychandra avatar wangrunji0408 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

buddy_system_allocator's Issues

Failure when use buddy_system_allocator

Hello! I'm playing around with buddy_system_allocator and have a minimal hello-world program, which I thought should work:

use buddy_system_allocator::LockedHeap;
use libc;
use core::ptr;
fn main() {

    #[global_allocator]
    static GLOBAL: LockedHeap::<32> = LockedHeap::<32>::empty();
    
    let address: *mut libc::c_void;
    unsafe {
        address = libc::mmap(ptr::null_mut(), 1<<29 as usize, libc::PROT_READ | libc::PROT_WRITE,
        libc::MAP_PRIVATE | libc::MAP_ANON, -1, 0);
    }
    if address == libc::MAP_FAILED {
        panic!("mmap: failed to get mapped memory area for heap");
    }
    unsafe {
        GLOBAL.lock().init(address as usize, 1<<29 as usize);
    }
    println!("Hello, world!");
}

This fails with message

memory allocation of 4 bytes failed
Aborted (core dumped)

Can you kindly point out what's the issue with my code?

Thanks very much

关于伙伴分配的数据结构

pub struct FrameAllocator {
// buddy system with max order of 32
free_list: [BTreeSet; 32],

// statistics
allocated: usize,
total: usize,

}

为什么free_list要选择BTreeSet?谢谢

Allow alignment to be specified for `FrameAllocator`

For some usecases, e.g. allocating MMIO space for PCI BARs, it is necessary to allocate address space with both a particular size and a particular alignment. It would be useful if FrameAllocator::alloc took an alignment parameter to specify what alignment is required.

index out of bounds: the len is 30 but the index is 30

There is a bug in lib.rs between L158 to L 178.

Provide the ORDER is 30. If the current_class is 29, i.e. the last index, the L174 will get error "Index out of bounds".

        while current_class < self.free_list.len() {
            let buddy = current_ptr ^ (1 << current_class);
            let mut flag = false;
            for block in self.free_list[current_class].iter_mut() {
                if block.value() as usize == buddy {
                    block.pop();
                    flag = true;
                    break;
                }
            }

            // Free buddy found
            if flag {
                self.free_list[current_class].pop();
                current_ptr = min(current_ptr, buddy);
                current_class += 1;
                self.free_list[current_class].push(current_ptr as *mut usize);
            } else {
                break;
            }
        }

Support Basic Statistics

We need some statistical information to profile our system.
For example: used memory, available memory, and other internal parameters...

Allocation spuriously fails

I got a spurious allocation failure even when there is enough space.

The flow is:

  1. An area of X bytes (8-bytes aligned) is used to initialize the buddy system allocator.
  2. At the very start of kernel initialization, right after init_heap, I try to allocate X bytes with
let g = alloc::boxed::Box::new([0u8; KERNEL_HEAP_SIZE]);
  1. The kernel reports a heap allocation error.

The problem disappears if I switch to linked_list_allocator.

Is there any caveat for using this allocator?

missing generics for struct `LockedHeap`

#[global_allocator]
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();

I use 0.8.0 version. And it reports that "missing generics for struct LockedHeap
expected 1 const argumen"

Order 32 is restricting for 64 bit machines

Order 32 of the linked list allows about a 4GB of heap for any process. This can easily be crossed when the crate is used under a 64 bit Virtual Space which can address upto 48 bits of information.

Please provide a larger order or an option to increase the order

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.