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
SendandSync)- 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 😉.
Installation
Add this to your Cargo.toml:
[]
= "0.5.0"
Feature Flags
| Feature | Description |
|---|---|
std |
Enable standard library support (enabled by default) |
nightly |
Enable nightly allocator_api feature |
Usage
Basic Allocation
use *;
use RawAlloc;
Zero-Initialized Memory
use *;
use RawAlloc;
Growing and Shrinking Allocations
use *;
use RawAlloc;
Custom Allocators
use *;
use RawAlloc;
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
i.e. This is a thin wrapper around the existing API, so we reuse error types from std.
License
Licensed under MIT.