Skip to content

Commit 03e4946

Browse files
committed
Add some documentation and manifest metadata.
1 parent 4f72caf commit 03e4946

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
name = "dlmalloc"
33
version = "0.1.0"
44
authors = ["Alex Crichton <alex@alexcrichton.com>"]
5+
license = "MIT/Apache-2.0"
6+
readme = "README.md"
7+
repository = "https://github.com/alexcrichton/dlmalloc-rs"
8+
homepage = "https://github.com/alexcrichton/dlmalloc-rs"
9+
documentation = "https://docs.rs/dlmalloc"
10+
description = """
11+
A Rust port of the dlmalloc allocator
12+
"""
513

614
[lib]
715
doctest = false

src/dlmalloc.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,6 @@ fn leftshift_for_tree_index(x: u32) -> u32 {
109109
}
110110

111111
impl Dlmalloc {
112-
pub fn new() -> Dlmalloc {
113-
DLMALLOC_INIT
114-
}
115-
116112
// TODO: can we get rid of this?
117113
pub fn malloc_alignment(&self) -> usize {
118114
mem::size_of::<usize>() * 2

src/global.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ use core::alloc::{AllocErr, Alloc};
77

88
use Dlmalloc;
99

10+
/// An instance of a "global allocator" backed by `Dlmalloc`
11+
///
12+
/// This API requires the `global` feature is activated, and this type
13+
/// implements the `GlobalAlloc` trait in the standard library.
1014
pub struct GlobalDlmalloc;
1115

1216
unsafe impl GlobalAlloc for GlobalDlmalloc {

src/lib.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1+
//! A Rust port of the `dlmalloc` allocator.
2+
//!
3+
//! The `dlmalloc` allocator is described at
4+
//! http://g.oswego.edu/dl/html/malloc.html and this Rust crate is a straight
5+
//! port of the C code for the allocator into Rust. The implementation is
6+
//! wrapped up in a `Dlmalloc` type and has support for Linux, OSX, and Wasm
7+
//! currently.
8+
//!
9+
//! The primary purpose of this crate is that it serves as the default memory
10+
//! allocator for the `wasm32-unknown-unknown` target in the standard library.
11+
//! Support for other platforms is largely untested and unused, but is used when
12+
//! testing this crate.
13+
114
#![cfg_attr(feature = "allocator-api", feature(allocator_api))]
215
#![cfg_attr(target_arch = "wasm32", feature(link_llvm_intrinsics))]
316
#![cfg_attr(not(feature = "allocator-api"), allow(dead_code))]
417
#![no_std]
18+
#![deny(missing_docs)]
519

620
#[cfg(feature = "allocator-api")]
721
use core::alloc::{Alloc, Layout, AllocErr};
@@ -15,8 +29,14 @@ pub use self::global::GlobalDlmalloc;
1529
mod global;
1630
mod dlmalloc;
1731

32+
/// An allocator instance
33+
///
34+
/// Instances of this type are used to allocate blocks of memory. For best
35+
/// results only use one of these. Currently doesn't implement `Drop` to release
36+
/// lingering memory back to the OS. That may happen eventually though!
1837
pub struct Dlmalloc(dlmalloc::Dlmalloc);
1938

39+
/// Constant initializer for `Dlmalloc` structure.
2040
pub const DLMALLOC_INIT: Dlmalloc = Dlmalloc(dlmalloc::DLMALLOC_INIT);
2141

2242
#[cfg(target_arch = "wasm32")]
@@ -32,10 +52,18 @@ mod sys;
3252
mod sys;
3353

3454
impl Dlmalloc {
55+
/// Creates a new instance of an allocator, same as `DLMALLOC_INIT`.
3556
pub fn new() -> Dlmalloc {
36-
Dlmalloc(dlmalloc::Dlmalloc::new())
57+
DLMALLOC_INIT
3758
}
3859

60+
/// Allocates `size` bytes with `align` align.
61+
///
62+
/// Returns a null pointer if allocation fails. Returns a valid pointer
63+
/// otherwise.
64+
///
65+
/// Safety and contracts are largely governed by the `GlobalAlloc::alloc`
66+
/// method contracts.
3967
#[inline]
4068
pub unsafe fn malloc(&mut self, size: usize, align: usize) -> *mut u8 {
4169
if align <= self.0.malloc_alignment() {
@@ -45,6 +73,8 @@ impl Dlmalloc {
4573
}
4674
}
4775

76+
/// Same as `malloc`, except if the allocation succeeds it's guaranteed to
77+
/// point to `size` bytes of zeros.
4878
#[inline]
4979
pub unsafe fn calloc(&mut self, size: usize, align: usize) -> *mut u8 {
5080
let ptr = self.malloc(size, align);
@@ -54,12 +84,26 @@ impl Dlmalloc {
5484
ptr
5585
}
5686

87+
/// Deallocates a `ptr` with `size` and `align` as the previous request used
88+
/// to allocate it.
89+
///
90+
/// Safety and contracts are largely governed by the `GlobalAlloc::dealloc`
91+
/// method contracts.
5792
#[inline]
5893
pub unsafe fn free(&mut self, ptr: *mut u8, size: usize, align: usize) {
5994
drop((size, align));
6095
self.0.free(ptr)
6196
}
6297

98+
/// Reallocates `ptr`, a previous allocation with `old_size` and
99+
/// `old_align`, to have `new_size` and the same alignment as before.
100+
///
101+
/// Returns a null pointer if the memory couldn't be reallocated, but `ptr`
102+
/// is still valid. Returns a valid pointer and frees `ptr` if the request
103+
/// is satisfied.
104+
///
105+
/// Safety and contracts are largely governed by the `GlobalAlloc::realloc`
106+
/// method contracts.
63107
#[inline]
64108
pub unsafe fn realloc(&mut self,
65109
ptr: *mut u8,

0 commit comments

Comments
 (0)