[Note, This will get some improvements]
The Tiered Memory Allocator is a Rust-based memory management system designed to allocate and manage memory for multiple clients with different subscription tiers. It provides efficient memory allocation, deallocation, and automatic reclamation of unused memory.
The project demonstrates low-level memory management in Rust, using alloc
and dealloc
from the standard library's alloc
module. It also showcases how to build a higher-level memory management system on top of these low-level operations.
- Tiered memory allocation based on client subscription levels (Basic, Premium, Enterprise)
- Thread-safe memory allocation and deallocation
- Automatic memory reclamation for unused blocks
- Usage tracking for individual clients and overall system
- Background threads for periodic memory reclamation and usage updates
- This project uses unsafe rust in several places where the code needs to interact directly with memory. You will see keywords like
alloc()
which is used to allocate raw memory,dealloc()
which is used to free the allocated memory. unsafe impl Send for MemoryBlock {}
andunsafe impl Sync for MemoryBlock {}
are used to markMemoryBlock
as safe to send between threads and safe to share between threads, respectively.
Unsafe Rust is necessary here becasue these operations involve raw pointer manilulation and memory management, which the Rust compiler can't guarantee as safe.
PhantomData
is used in the MemoryBlock
struct:
struct MemoryBlock {
ptr: NonNull<u8>,
size: usize,
_marker: PhantomData<u8>,
}
PhantomData<u8>
is ued to indicate that MemoryBlock
logically owns a u8
value, even though it doesn't actually contain one. This is important for correct Drop
behavior and to prevent the compiler from thinking the type is invariant over u8
.
The ptr
field is a pointer to the beginning of the allocated memory block. It's of type NonNull<u8>
, which is a wrapper around a raw pointer that is guaranteed to be non-null.
NonNull<u8>
is used for the raw pointer inMemoryBlock
,NonNull
is a wrapper around raw pointers that's guaranteed to be a non-null.Arc<Mutex<TieredAllocator>>
is used in theMemory
struct to allow safe sharing of the allocator between threads.
- Rust (latest stable version recommended)
-
Include the Tiered Allocator in your project:
use tiered_allocator::Memory;
-
Create a new Memory instance:
let mem = Memory::new(1_000_000_000); // 1GB total memory
-
Add clients with their respective tiers:
mem.add_client("client_1", SubscriptionTier::Premium);
-
Allocate memory for clients:
let block = mem.allocate_for_client("client_1", 50_000_000).unwrap();
-
Deallocate memory when no longer needed:
mem.deallocate_for_client("client_1", block).unwrap();
-
Get resource usage information:
let (allocated, used) = mem.get_client_resource_usage("client_1").unwrap(); let (total_allocated, total_used) = mem.get_resource_usage();
- The allocator uses unsafe Rust for low-level memory management. Ensure that all memory blocks are properly deallocated to prevent memory leaks.
- The system automatically reclaims unused memory periodically, but it's good practice to deallocate memory explicitly when it's no longer needed.
- The allocator is thread-safe, but be cautious when sharing memory blocks between threads.
Contributions to the Tiered Memory Allocator are welcome! Please feel free to submit pull requests or open issues to suggest improvements or report bugs.