Crates.io | dense-heap |
lib.rs | dense-heap |
version | 0.1.2 |
source | src |
created_at | 2023-05-01 14:52:22.05525 |
updated_at | 2023-05-01 15:24:00.17074 |
description | 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`. |
homepage | |
repository | |
max_upload_size | |
id | 853446 |
size | 16,941 |
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.
DBox
for easy memory managementDocumentation 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.