safe-allocator-api
A safe wrapper around the allocator_api
's Allocator trait.
This crate provides a wrapper around the returned results, ensuring that any allocated memory is automatically dropped when its lifetime expires.
Features
- Safe wrapper around raw allocations with known layout
- Automatic deallocation when values go out of scope
- Support for custom allocators
- Zero-initialization options
- Grow and shrink operations with proper error handling
- Thread-safe (implements
Send
andSync
)- Actual thread safety depends on safety of the allocator used internally, tread wisely.
In my case I wrote this crate to have a 'safe' way to make aligned allocations 😉.
Usage
Add this to your Cargo.toml
:
[]
= "0.1.0"
Examples
Basic Allocation
use *;
use RawAlloc;
Zero-Initialized Memory
use *;
use RawAlloc;
Growing and Shrinking Allocations
use *;
use RawAlloc;
Custom Allocators
use *;
use Global;
use RawAlloc;
Safety
This crate provides a safe interface by ensuring:
- The wrapped pointer is always non-null and properly aligned
- Memory is automatically deallocated when dropped
- Reallocation maintains proper alignment and size constraints
- Error conditions are properly handled and reported
Error Handling
Operations will return AllocError
in the following cases:
- The allocator reports an error
- Attempting to allocate zero bytes
- Growing to a smaller size
- Shrinking to a larger size
Development
For information on how to work with this codebase, see README-DEV.MD.
License
Licensed under MIT.
Learn more about Reloaded's general choice of licensing for projects..