7 #[derive(Debug, Eq, PartialEq)]
14 impl<T> TypedArena<T> {
15 /// Clears the arena. Deallocates all but the longest chunk which may be reused.
18 // Clear the last chunk, which is partially filled.
19 let mut chunks_borrow = self.chunks.borrow_mut();
20 if let Some(mut last_chunk) = chunks_borrow.last_mut() {
21 self.clear_last_chunk(&mut last_chunk);
22 let len = chunks_borrow.len();
23 // If `T` is ZST, code below has no effect.
24 for mut chunk in chunks_borrow.drain(..len - 1) {
25 chunk.destroy(chunk.entries);
33 pub fn test_unused() {
34 let arena: TypedArena<Point> = TypedArena::default();
35 assert!(arena.chunks.borrow().is_empty());
39 fn test_arena_alloc_nested() {
51 struct Wrap<'a>(TypedArena<EI<'a>>);
54 fn alloc_inner<F: Fn() -> Inner>(&self, f: F) -> &Inner {
55 let r: &EI<'_> = self.0.alloc(EI::I(f()));
56 if let &EI::I(ref i) = r {
62 fn alloc_outer<F: Fn() -> Outer<'a>>(&self, f: F) -> &Outer<'_> {
63 let r: &EI<'_> = self.0.alloc(EI::O(f()));
64 if let &EI::O(ref o) = r {
72 let arena = Wrap(TypedArena::default());
74 let result = arena.alloc_outer(|| Outer { inner: arena.alloc_inner(|| Inner { value: 10 }) });
76 assert_eq!(result.inner.value, 10);
81 let arena = TypedArena::default();
83 arena.alloc(Point { x: 1, y: 2, z: 3 });
88 pub fn bench_copy(b: &mut Bencher) {
89 let arena = TypedArena::default();
90 b.iter(|| arena.alloc(Point { x: 1, y: 2, z: 3 }))
94 pub fn bench_copy_nonarena(b: &mut Bencher) {
96 let _: Box<_> = Box::new(Point { x: 1, y: 2, z: 3 });
107 pub fn test_noncopy() {
108 let arena = TypedArena::default();
110 arena.alloc(Noncopy { string: "hello world".to_string(), array: vec![1, 2, 3, 4, 5] });
115 pub fn test_typed_arena_zero_sized() {
116 let arena = TypedArena::default();
123 pub fn test_typed_arena_clear() {
124 let mut arena = TypedArena::default();
128 arena.alloc(Point { x: 1, y: 2, z: 3 });
134 pub fn bench_typed_arena_clear(b: &mut Bencher) {
135 let mut arena = TypedArena::default();
137 arena.alloc(Point { x: 1, y: 2, z: 3 });
143 pub fn bench_typed_arena_clear_100(b: &mut Bencher) {
144 let mut arena = TypedArena::default();
147 arena.alloc(Point { x: 1, y: 2, z: 3 });
155 struct DropCounter<'a> {
156 count: &'a Cell<u32>,
159 impl Drop for DropCounter<'_> {
161 self.count.set(self.count.get() + 1);
166 fn test_typed_arena_drop_count() {
167 let counter = Cell::new(0);
169 let arena: TypedArena<DropCounter<'_>> = TypedArena::default();
171 // Allocate something with drop glue to make sure it doesn't leak.
172 arena.alloc(DropCounter { count: &counter });
175 assert_eq!(counter.get(), 100);
179 fn test_typed_arena_drop_on_clear() {
180 let counter = Cell::new(0);
181 let mut arena: TypedArena<DropCounter<'_>> = TypedArena::default();
184 // Allocate something with drop glue to make sure it doesn't leak.
185 arena.alloc(DropCounter { count: &counter });
188 assert_eq!(counter.get(), i * 100 + 100);
193 static DROP_COUNTER: Cell<u32> = Cell::new(0)
196 struct SmallDroppable;
198 impl Drop for SmallDroppable {
200 DROP_COUNTER.with(|c| c.set(c.get() + 1));
205 fn test_typed_arena_drop_small_count() {
206 DROP_COUNTER.with(|c| c.set(0));
208 let arena: TypedArena<SmallDroppable> = TypedArena::default();
210 // Allocate something with drop glue to make sure it doesn't leak.
211 arena.alloc(SmallDroppable);
215 assert_eq!(DROP_COUNTER.with(|c| c.get()), 100);
219 pub fn bench_noncopy(b: &mut Bencher) {
220 let arena = TypedArena::default();
222 arena.alloc(Noncopy { string: "hello world".to_string(), array: vec![1, 2, 3, 4, 5] })
227 pub fn bench_noncopy_nonarena(b: &mut Bencher) {
230 Box::new(Noncopy { string: "hello world".to_string(), array: vec![1, 2, 3, 4, 5] });