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 match self.0.alloc(EI::I(f())) {
57 _ => panic!("mismatch"),
60 fn alloc_outer<F: Fn() -> Outer<'a>>(&self, f: F) -> &Outer<'_> {
61 match self.0.alloc(EI::O(f())) {
63 _ => panic!("mismatch"),
68 let arena = Wrap(TypedArena::default());
70 let result = arena.alloc_outer(|| Outer { inner: arena.alloc_inner(|| Inner { value: 10 }) });
72 assert_eq!(result.inner.value, 10);
77 let arena = TypedArena::default();
79 const N: usize = 100000;
81 const N: usize = 1000;
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 const N: usize = 100000;
112 const N: usize = 1000;
114 arena.alloc(Noncopy { string: "hello world".to_string(), array: vec![1, 2, 3, 4, 5] });
119 pub fn test_typed_arena_zero_sized() {
120 let arena = TypedArena::default();
122 const N: usize = 100000;
124 const N: usize = 1000;
131 pub fn test_typed_arena_clear() {
132 let mut arena = TypedArena::default();
136 const N: usize = 10000;
138 const N: usize = 100;
140 arena.alloc(Point { x: 1, y: 2, z: 3 });
146 pub fn bench_typed_arena_clear(b: &mut Bencher) {
147 let mut arena = TypedArena::default();
149 arena.alloc(Point { x: 1, y: 2, z: 3 });
155 pub fn bench_typed_arena_clear_100(b: &mut Bencher) {
156 let mut arena = TypedArena::default();
159 arena.alloc(Point { x: 1, y: 2, z: 3 });
167 struct DropCounter<'a> {
168 count: &'a Cell<u32>,
171 impl Drop for DropCounter<'_> {
173 self.count.set(self.count.get() + 1);
178 fn test_typed_arena_drop_count() {
179 let counter = Cell::new(0);
181 let arena: TypedArena<DropCounter<'_>> = TypedArena::default();
183 // Allocate something with drop glue to make sure it doesn't leak.
184 arena.alloc(DropCounter { count: &counter });
187 assert_eq!(counter.get(), 100);
191 fn test_typed_arena_drop_on_clear() {
192 let counter = Cell::new(0);
193 let mut arena: TypedArena<DropCounter<'_>> = TypedArena::default();
196 // Allocate something with drop glue to make sure it doesn't leak.
197 arena.alloc(DropCounter { count: &counter });
200 assert_eq!(counter.get(), i * 100 + 100);
205 static DROP_COUNTER: Cell<u32> = Cell::new(0)
208 struct SmallDroppable;
210 impl Drop for SmallDroppable {
212 DROP_COUNTER.with(|c| c.set(c.get() + 1));
217 fn test_typed_arena_drop_small_count() {
218 DROP_COUNTER.with(|c| c.set(0));
220 let arena: TypedArena<SmallDroppable> = TypedArena::default();
222 // Allocate something with drop glue to make sure it doesn't leak.
223 arena.alloc(SmallDroppable);
227 assert_eq!(DROP_COUNTER.with(|c| c.get()), 100);
231 pub fn bench_noncopy(b: &mut Bencher) {
232 let arena = TypedArena::default();
234 arena.alloc(Noncopy { string: "hello world".to_string(), array: vec![1, 2, 3, 4, 5] })
239 pub fn bench_noncopy_nonarena(b: &mut Bencher) {
242 Box::new(Noncopy { string: "hello world".to_string(), array: vec![1, 2, 3, 4, 5] });