Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
portable-dlmalloc
Portable Fork of Doug Lea's malloc
Implementation for Rust.
Introduction
This code is originally implemented by Doug Lea. The original source code is no longer available from the FTP URL listed in the website, but you can still find it through Wayback Machine.
You may use this crate to help you make a portable global allocator.
You will have to implement the eight C functions as described in Port To Your Platform chapter.
Global Allocator
To use this crate as your global allocator:
use DLMalloc;
static GLOBAL_ALLOCATOR:DLMalloc=DLMalloc;
Then you will be able to use alloc
crate.
extern crate alloc;
The default alignment of dlmalloc
is twice the pointer size (e.g.: 16 bytes on 64-bit systems).
If you need to use a different alignment, use dlmemalign
function to implement your GlobalAlloc
trait.
Alternate Allocator
The Allocator Trait is currently nightly-only.
Currently, this crate does not support this trait.
Raw FFI
The raw
module from this crate exports FFI bindings for dlmalloc
library.
use *;
For example, you may use dlmallopt
to adjust mmap
granularity (default is 2MiB in Rust crate):
dlmallopt; // Change `mmap` granularity to 128KiB.
You may use dlpvalloc
to allocate memory on page-granularity.
let p=dlpvalloc;
assert_eq!;
Warning: dlpvalloc
- as well as other routines that allocate memories with higher granularities - may cause serious memory fragmentation if you overrely on them.
// Assume 4096 is page size.
let p=dlpvalloc as usize;
let q=dlpvalloc as usize;
// Consecutive allocations do not guarantee them to be adjacent.
assert_eq!;
Port to Your Platform
To port dlmalloc
to your platform, implement the following procedures:
custom_mmap
/custom_munmap
: Allocate and free pages from the system.mmap
should return(void*)-1
to indicate failure instead ofNULL
.munmap
should return0
to indicate success, and-1
to indicate failure.unsafe extern "C" custom_mmap; unsafe extern "C" custom_munmap;
custom_direct_mmap
: Extend the allocated pages. This is optional. Return(void*)-1
to indicate failure/no-support.unsafe extern "C" custom_mmap;
init_lock
/final_lock
/acquire_lock
/release_lock
: Implement thread-safety fordlmalloc
. The minimal implementation can be a simple spinlock. You can leave the implementations empty for this set of routines if you do not need thread-safety.unsafe extern "C" init_lock; // Initialize the mutex. unsafe extern "C" final_lock; // Finalize the mutex. unsafe extern "C" acquire_lock; // Acquire the mutex. unsafe extern "C" release_lock; // Release the mutex.
custom_abort
: Implementabort()
routine.dlmalloc
callscustom_abort()
when internal assertion fails. You may use panic here.unsafe extern "C" custom_abort!;
memcpy
/memset
: I suppose no explanations are needed for these two.dlmalloc
uses these two routines, but they can be easily implemented anyway. You do not need to implement these two routines in Rust if your linker can find libraries that implement these two routines.
License
This crate is under the MIT license.