https://github.com/alecmocatta/cap
An allocator that can track and limit memory usage.
https://github.com/alecmocatta/cap
allocation bound limit memory rust
Last synced: 3 months ago
JSON representation
An allocator that can track and limit memory usage.
- Host: GitHub
- URL: https://github.com/alecmocatta/cap
- Owner: alecmocatta
- License: apache-2.0
- Created: 2019-10-20T20:37:49.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-26T13:45:58.000Z (over 2 years ago)
- Last Synced: 2024-12-06T22:42:28.345Z (7 months ago)
- Topics: allocation, bound, limit, memory, rust
- Language: Rust
- Homepage:
- Size: 33.2 KB
- Stars: 205
- Watchers: 3
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE.txt
Awesome Lists containing this project
README
# cap
[](https://crates.io/crates/cap)
[](#License)
[](https://dev.azure.com/alecmocatta/cap/_build/latest?branchName=master)[Docs](https://docs.rs/cap/0.1)
An allocator that can track and limit memory usage.
This crate provides a generic allocator that wraps another allocator, tracking memory usage and enabling limits to be set.
## Example
It can be used by declaring a static and marking it with the `#[global_allocator]` attribute:
```rust
use std::alloc;
use cap::Cap;#[global_allocator]
static ALLOCATOR: Cap = Cap::new(alloc::System, usize::max_value());fn main() {
// Set the limit to 30MiB.
ALLOCATOR.set_limit(30 * 1024 * 1024).unwrap();
// ...
println!("Currently allocated: {}B", ALLOCATOR.allocated());
}
```## License
Licensed under either of* Apache License, Version 2.0, ([LICENSE-APACHE.txt](LICENSE-APACHE.txt) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT.txt](LICENSE-MIT.txt) or http://opensource.org/licenses/MIT)at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.