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 const N: usize = 100000;
85 const N: usize = 1000;
87 arena.alloc(Point { x: 1, y: 2, z: 3 });
92 pub fn bench_copy(b: &mut Bencher) {
93 let arena = TypedArena::default();
94 b.iter(|| arena.alloc(Point { x: 1, y: 2, z: 3 }))
98 pub fn bench_copy_nonarena(b: &mut Bencher) {
100 let _: Box<_> = Box::new(Point { x: 1, y: 2, z: 3 });
111 pub fn test_noncopy() {
112 let arena = TypedArena::default();
114 const N: usize = 100000;
116 const N: usize = 1000;
118 arena.alloc(Noncopy { string: "hello world".to_string(), array: vec![1, 2, 3, 4, 5] });
123 pub fn test_typed_arena_zero_sized() {
124 let arena = TypedArena::default();
126 const N: usize = 100000;
128 const N: usize = 1000;
135 pub fn test_typed_arena_clear() {
136 let mut arena = TypedArena::default();
140 const N: usize = 10000;
142 const N: usize = 100;
144 arena.alloc(Point { x: 1, y: 2, z: 3 });
150 pub fn bench_typed_arena_clear(b: &mut Bencher) {
151 let mut arena = TypedArena::default();
153 arena.alloc(Point { x: 1, y: 2, z: 3 });
159 pub fn bench_typed_arena_clear_100(b: &mut Bencher) {
160 let mut arena = TypedArena::default();
163 arena.alloc(Point { x: 1, y: 2, z: 3 });
171 struct DropCounter<'a> {
172 count: &'a Cell<u32>,
175 impl Drop for DropCounter<'_> {
177 self.count.set(self.count.get() + 1);
182 fn test_typed_arena_drop_count() {
183 let counter = Cell::new(0);
185 let arena: TypedArena<DropCounter<'_>> = TypedArena::default();
187 // Allocate something with drop glue to make sure it doesn't leak.
188 arena.alloc(DropCounter { count: &counter });
191 assert_eq!(counter.get(), 100);
195 fn test_typed_arena_drop_on_clear() {
196 let counter = Cell::new(0);
197 let mut arena: TypedArena<DropCounter<'_>> = TypedArena::default();
200 // Allocate something with drop glue to make sure it doesn't leak.
201 arena.alloc(DropCounter { count: &counter });
204 assert_eq!(counter.get(), i * 100 + 100);
209 static DROP_COUNTER: Cell<u32> = Cell::new(0)
212 struct SmallDroppable;
214 impl Drop for SmallDroppable {
216 DROP_COUNTER.with(|c| c.set(c.get() + 1));
221 fn test_typed_arena_drop_small_count() {
222 DROP_COUNTER.with(|c| c.set(0));
224 let arena: TypedArena<SmallDroppable> = TypedArena::default();
226 // Allocate something with drop glue to make sure it doesn't leak.
227 arena.alloc(SmallDroppable);
231 assert_eq!(DROP_COUNTER.with(|c| c.get()), 100);
235 pub fn bench_noncopy(b: &mut Bencher) {
236 let arena = TypedArena::default();
238 arena.alloc(Noncopy { string: "hello world".to_string(), array: vec![1, 2, 3, 4, 5] })
243 pub fn bench_noncopy_nonarena(b: &mut Bencher) {
246 Box::new(Noncopy { string: "hello world".to_string(), array: vec![1, 2, 3, 4, 5] });