dense-heap

Crates.iodense-heap
lib.rsdense-heap
version0.1.2
sourcesrc
created_at2023-05-01 14:52:22.05525
updated_at2023-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
id853446
size16,941
Sam Belliveau (Sam-Belliveau)

documentation

README

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.

Features

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

Documentation

Documentation can be found here.

Usage

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();

Example

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

Safety

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.

Commit count: 0

cargo fmt