#smart-pointers #memory #dense #heap #memory-allocator #heap-allocator


This code defines a custom allocator called DHeap (Dense Heap) and a smart pointer called DBox (Dense Box). The DHeap is responsible for managing memory allocation, while the DBox provides a way to access and manage the data stored in the DHeap. The main advantage of using this custom allocator is that it minimizes memory fragmentation by densely packing the allocated memory. The code also includes test cases to demonstrate the functionality of DHeap and DBox.

1 unstable release

0.1.2 May 1, 2023
0.1.1 May 1, 2023
0.1.0 May 1, 2023

#610 in Memory management

Download history 2/week @ 2024-02-14 15/week @ 2024-02-21 2/week @ 2024-02-28 25/week @ 2024-03-27 28/week @ 2024-04-03

53 downloads per month


209 lines

DHeap (Dense Heap) Allocator and DBox (Dense Box) Smart Pointer

This project provides a custom memory allocator called DHeap and a smart pointer called DBox. The primary goal of this allocator is to minimize memory fragmentation by densely packing the allocated memory.


  • Minimizes memory fragmentation
  • Minimizes memory usage for uniformly sized allocations
  • Smart pointer DBox for easy memory management


Documentation can be found here.


To use this custom allocator, first, create a DHeap instance with the desired capacity:

let heap: DHeap<i32> = DHeap::with_capacity(16);

To allocate memory in the DHeap, you can use the safe_new method:

let dbox = heap.safe_new(42).unwrap();

The DBox smart pointer is used to access and manage the data stored in the DHeap. You can dereference the DBox to access the underlying data:

assert_eq!(*dbox, 42);

The DBox smart pointer automatically deallocates the memory when it goes out of scope or when the into_inner method is called:

let inner_val = dbox.into_inner();


A basic example of using the DHeap allocator and DBox smart pointer can be found in the tests module within the source code.


The code uses unsafe Rust features to optimize performance, but these are limited and accompanied by explanations. The use of DBox ensures that the memory management is safe and prevents issues like double frees or use-after-free. However, be cautious when using the unsafe_new method, as it may invalidate existing references if the underlying vector needs to be resized. DBox's will always remain valid after a resize, however references to the values in those boxes will not.

No runtime deps