3个版本
0.1.2 | 2020年10月29日 |
---|---|
0.1.1 | 2020年10月29日 |
0.1.0 | 2020年10月29日 |
#236 in 内存管理
98KB
2K SLoC
带有附加功能的竞技场存储
具有自己的基本分配器的竞技场存储,具有管理释放顺序、高效且可选的释放执行、通用字符串、列表、数组以及自定义结构/数据支持。
更多详细信息请参阅 crate 文档。
一些使用示例
use memur::{Memory, Arena, UStr};
fn main() {
// memory pool which can be shared between threads
let mem = Memory::new();
{
let arena = Arena::new(&mem).unwrap();
// Zero-terminated utf-8 string that does not need
// to be dropped:
let text = UStr::from_str(&arena, "Hello").unwrap();
assert_eq!("Hello", &text);
// Since it is zero-terminated,
// it can be used in C APIs without conversion:
assert_eq!(unsafe { CStr::from_bytes_with_nul_unchecked(b"Hello\n") }, &text);
// Simple struct wrapper, items added this way end up
// near each other in memory:
let a = N::new(&arena, "hello").unwrap();
let b = N::new(&arena, "world").unwrap();
// This also adds another item "c" to the same arena, which
// will be dropped after the "a" is dropped.
// This is useful for ensuring the drop order.
let c = a.outlives("c").unwrap();
// Fixed size array example, initialized
// from exact size iterator:
let a = Array::new(&arena, (0..2).into_iter()).unwrap();
assert_eq!(a.len(), Some(2));
// The unsafe C-way to initialize array is also available:
let mut uninitialized_array = Array::<i32>::with_capacity(&arena, 2).unwrap();
unsafe { *(uninitialized_array.data_mut().offset(0)) = 1 }
unsafe { *(uninitialized_array.data_mut().offset(1)) = 2 }
let a = unsafe { uninitialized_array.initialized_to_len(2) };
assert_eq!(a.len(), Some(2));
assert_eq!(a[0], 1);
assert_eq!(a[1], 2);
// The drop functions are executed when the Arena goes out of scope,
// and the created objects are aware of that because they keep
// WeakArena inside.
// The memory blocks are returned back to Memory when
// all WeakArena holders go out of scope.
}
}
许可证
许可方式为以下之一
- Apache许可证版本2.0,(LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
任选其一。
贡献
除非你明确表示,否则根据Apache-2.0许可证定义,你故意提交的任何旨在包含在该作品中的贡献,将按照上述方式双重许可,不附加任何额外条款或条件。