Skip to content

Commit

Permalink
Switch model SQAUSHME
Browse files Browse the repository at this point in the history
  • Loading branch information
ticki committed Aug 5, 2016
1 parent c8adc1e commit 22e763e
Show file tree
Hide file tree
Showing 24 changed files with 806 additions and 834 deletions.
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ralloc"
version = "0.1.0"
version = "1.0.0"
authors = ["ticki <ticki@users.noreply.github.com>"]

# URLs and paths
Expand All @@ -12,13 +12,16 @@ readme = "README.md"
keywords = ["alloc", "malloc", "allocator", "ralloc", "redox"]
license = "MIT"

[dependencies]
ralloc_shim = { path = "shim" }
[dependencies.ralloc_shim]
path = "shim"

[dependencies.clippy]
git = "https://github.com/Manishearth/rust-clippy.git"
version = "0.0.80"
optional = true

[dependencies.unborrow]
git = "https://github.com/durka/unborrow.git"

[profile.release]
panic = "abort"
opt-level = 3
Expand Down
75 changes: 20 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ Note that `ralloc` cannot coexist with another allocator, unless they're deliber

## Features

### Thread-local allocation

Ralloc makes use of a global-local model allowing one to allocate or deallocate
without locks, syncronization, or atomic writes. This provides reasonable
performance, while preserving flexibility and ability to multithread.

### Custom out-of-memory handlers

You can set custom OOM handlers, by:
Expand Down Expand Up @@ -99,6 +105,8 @@ fn main() {

### Debug check: memory leaks.

TODO Temporarily disabled.

`ralloc` got memleak superpowers too! Enable `debug_tools` and do:

```rust
Expand All @@ -112,7 +120,7 @@ fn main() {
// We start by allocating some stuff.
let a = Box::new(500u32);
// We then leak `a`.
let b = mem::forget(a);
mem::forget(a);
}
// The box is now leaked, and the destructor won't be called.

Expand Down Expand Up @@ -149,30 +157,6 @@ fn main() {
}
```

### Separate deallocation

Another cool feature is that you can deallocate things that weren't even
allocated buffers in the first place!

Consider that you got a unused static variable, that you want to put into the
allocation pool:

```rust
extern crate ralloc;

static mut BUFFER: [u8; 256] = [2; 256];

fn main() {
// Throw `BUFFER` into the memory pool.
unsafe {
ralloc::lock().free(&mut BUFFER as *mut u8, 256);
}

// Do some allocation.
assert_eq!(*Box::new(0xDEED), 0xDEED);
}
```

### Top notch security

If you are willing to trade a little performance, for extra security you can
Expand Down Expand Up @@ -209,27 +193,6 @@ it is important that the code is reviewed and verified.
6. Manual reviewing. One or more persons reviews patches to ensure high
security.

### Lock reuse

Acquiring a lock sequentially multiple times can be expensive. Therefore,
`ralloc` allows you to lock the allocator once, and reuse that:

```rust
extern crate ralloc;

fn main() {
// Get that lock!
let lock = ralloc::lock();

// All in one:
let _ = lock.alloc(4, 2);
let _ = lock.alloc(4, 2);
let _ = lock.alloc(4, 2);

// The lock is automatically released through its destructor.
}
```

### Security through the type system

`ralloc` makes heavy use of Rust's type system, to make safety guarantees.
Expand All @@ -246,21 +209,23 @@ This is just one of many examples.
interface for platform dependent functions. An default implementation of
`ralloc_shim` is provided (supporting Mac OS, Linux, and BSD).

### Local allocators
### Forcing inplace reallocation

Inplace reallocation can be significantly faster than memcpy'ing reallocation.
A limitation of libc is that you cannot do reallocation inplace-only (a
failable method that guarantees the absence of memcpy of the buffer).

`ralloc` allows you to create non-global allocators, for e.g. thread specific purposes:
Having a failable way to do inplace reallocation provides some interesting possibilities.

```rust
extern crate ralloc;

fn main() {
// We create an allocator.
let my_alloc = ralloc::Allocator::new();
let buf = ralloc::alloc(40, 1);
// BRK'ing 20 bytes...
let ptr = unsafe { ralloc::inplace_realloc(buf, 40, 45).unwrap() };

// Allocate some stuff through our local allocator.
let _ = my_alloc.alloc(4, 2);
let _ = my_alloc.alloc(4, 2);
let _ = my_alloc.alloc(4, 2);
// The buffer is now 45 bytes long!
}
```

Expand Down Expand Up @@ -315,7 +280,7 @@ especially true when dealing with very big allocation.
extern crate ralloc;

fn main() {
let buf = ralloc::lock().try_alloc(8, 4);
let buf = ralloc::try_alloc(8, 4);
// `buf` is a Result: It is Err(()) if the allocation failed.
}
```
21 changes: 0 additions & 21 deletions benches/no_lock.rs

This file was deleted.

4 changes: 4 additions & 0 deletions shim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ rpath = false
lto = true
debug-assertions = false
codegen-units = 1

[dependencies.libc]
version = "0.2"
default-features = false
4 changes: 2 additions & 2 deletions shim/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! This crate provides implementation/import of these in Linux, BSD, and Mac OS.
#![feature(lang_items, linkage)]
#![feature(linkage)]
#![no_std]
#![warn(missing_docs)]

Expand All @@ -12,7 +12,7 @@ pub use libc::sched_yield;

extern {
/// Change the data segment. See `man sbrk`.
pub fn sbrk(libc::intptr_t) -> *const libc::c_void;
pub fn sbrk(_: libc::intptr_t) -> *const libc::c_void;
}

/// Thread destructors for Linux.
Expand Down
Loading

0 comments on commit 22e763e

Please sign in to comment.