]> git.lizzy.rs Git - rust.git/blob - src/libarena/lib.rs
rustc: Remove #![unstable] annotation
[rust.git] / src / libarena / lib.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! The arena, a fast but limited type of allocator.
12 //!
13 //! Arenas are a type of allocator that destroy the objects within, all at
14 //! once, once the arena itself is destroyed. They do not support deallocation
15 //! of individual objects while the arena itself is still alive. The benefit
16 //! of an arena is very fast allocation; just a pointer bump.
17 //!
18 //! This crate implements `TypedArena`, a simple arena that can only hold
19 //! objects of a single type.
20
21 #![crate_name = "arena"]
22 #![cfg_attr(stage0, unstable(feature = "rustc_private", issue = "27812"))]
23 #![crate_type = "rlib"]
24 #![crate_type = "dylib"]
25 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
26        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
27        html_root_url = "https://doc.rust-lang.org/nightly/",
28        test(no_crate_inject, attr(deny(warnings))))]
29 #![deny(warnings)]
30
31 #![feature(alloc)]
32 #![feature(core_intrinsics)]
33 #![feature(dropck_eyepatch)]
34 #![feature(generic_param_attrs)]
35 #![cfg_attr(stage0, feature(staged_api))]
36 #![cfg_attr(test, feature(test))]
37
38 #![allow(deprecated)]
39
40 extern crate alloc;
41
42 use std::cell::{Cell, RefCell};
43 use std::cmp;
44 use std::intrinsics;
45 use std::marker::{PhantomData, Send};
46 use std::mem;
47 use std::ptr;
48 use std::slice;
49
50 use alloc::raw_vec::RawVec;
51
52 /// An arena that can hold objects of only one type.
53 pub struct TypedArena<T> {
54     /// A pointer to the next object to be allocated.
55     ptr: Cell<*mut T>,
56
57     /// A pointer to the end of the allocated area. When this pointer is
58     /// reached, a new chunk is allocated.
59     end: Cell<*mut T>,
60
61     /// A vector of arena chunks.
62     chunks: RefCell<Vec<TypedArenaChunk<T>>>,
63
64     /// Marker indicating that dropping the arena causes its owned
65     /// instances of `T` to be dropped.
66     _own: PhantomData<T>,
67 }
68
69 struct TypedArenaChunk<T> {
70     /// The raw storage for the arena chunk.
71     storage: RawVec<T>,
72 }
73
74 impl<T> TypedArenaChunk<T> {
75     #[inline]
76     unsafe fn new(capacity: usize) -> TypedArenaChunk<T> {
77         TypedArenaChunk { storage: RawVec::with_capacity(capacity) }
78     }
79
80     /// Destroys this arena chunk.
81     #[inline]
82     unsafe fn destroy(&mut self, len: usize) {
83         // The branch on needs_drop() is an -O1 performance optimization.
84         // Without the branch, dropping TypedArena<u8> takes linear time.
85         if intrinsics::needs_drop::<T>() {
86             let mut start = self.start();
87             // Destroy all allocated objects.
88             for _ in 0..len {
89                 ptr::drop_in_place(start);
90                 start = start.offset(1);
91             }
92         }
93     }
94
95     // Returns a pointer to the first allocated object.
96     #[inline]
97     fn start(&self) -> *mut T {
98         self.storage.ptr()
99     }
100
101     // Returns a pointer to the end of the allocated space.
102     #[inline]
103     fn end(&self) -> *mut T {
104         unsafe {
105             if mem::size_of::<T>() == 0 {
106                 // A pointer as large as possible for zero-sized elements.
107                 !0 as *mut T
108             } else {
109                 self.start().offset(self.storage.cap() as isize)
110             }
111         }
112     }
113 }
114
115 const PAGE: usize = 4096;
116
117 impl<T> TypedArena<T> {
118     /// Creates a new `TypedArena`.
119     #[inline]
120     pub fn new() -> TypedArena<T> {
121         TypedArena {
122             // We set both `ptr` and `end` to 0 so that the first call to
123             // alloc() will trigger a grow().
124             ptr: Cell::new(0 as *mut T),
125             end: Cell::new(0 as *mut T),
126             chunks: RefCell::new(vec![]),
127             _own: PhantomData,
128         }
129     }
130
131     /// Allocates an object in the `TypedArena`, returning a reference to it.
132     #[inline]
133     pub fn alloc(&self, object: T) -> &mut T {
134         if self.ptr == self.end {
135             self.grow(1)
136         }
137
138         unsafe {
139             if mem::size_of::<T>() == 0 {
140                 self.ptr.set(intrinsics::arith_offset(self.ptr.get() as *mut u8, 1) as *mut T);
141                 let ptr = mem::align_of::<T>() as *mut T;
142                 // Don't drop the object. This `write` is equivalent to `forget`.
143                 ptr::write(ptr, object);
144                 &mut *ptr
145             } else {
146                 let ptr = self.ptr.get();
147                 // Advance the pointer.
148                 self.ptr.set(self.ptr.get().offset(1));
149                 // Write into uninitialized memory.
150                 ptr::write(ptr, object);
151                 &mut *ptr
152             }
153         }
154     }
155
156     /// Allocates a slice of objects that are copy into the `TypedArena`, returning a mutable
157     /// reference to it. Will panic if passed a zero-sized types.
158     ///
159     /// Panics:
160     ///  - Zero-sized types
161     ///  - Zero-length slices
162     #[inline]
163     pub fn alloc_slice(&self, slice: &[T]) -> &mut [T]
164         where T: Copy {
165         assert!(mem::size_of::<T>() != 0);
166         assert!(slice.len() != 0);
167
168         let available_capacity_bytes = self.end.get() as usize - self.ptr.get() as usize;
169         let at_least_bytes = slice.len() * mem::size_of::<T>();
170         if available_capacity_bytes < at_least_bytes {
171             self.grow(slice.len());
172         }
173
174         unsafe {
175             let start_ptr = self.ptr.get();
176             let arena_slice = slice::from_raw_parts_mut(start_ptr, slice.len());
177             self.ptr.set(start_ptr.offset(arena_slice.len() as isize));
178             arena_slice.copy_from_slice(slice);
179             arena_slice
180         }
181     }
182
183     /// Grows the arena.
184     #[inline(never)]
185     #[cold]
186     fn grow(&self, n: usize) {
187         unsafe {
188             let mut chunks = self.chunks.borrow_mut();
189             let (chunk, mut new_capacity);
190             if let Some(last_chunk) = chunks.last_mut() {
191                 let used_bytes = self.ptr.get() as usize - last_chunk.start() as usize;
192                 let currently_used_cap = used_bytes / mem::size_of::<T>();
193                 if last_chunk.storage.reserve_in_place(currently_used_cap, n) {
194                     self.end.set(last_chunk.end());
195                     return;
196                 } else {
197                     new_capacity = last_chunk.storage.cap();
198                     loop {
199                         new_capacity = new_capacity.checked_mul(2).unwrap();
200                         if new_capacity >= currently_used_cap + n {
201                             break;
202                         }
203                     }
204                 }
205             } else {
206                 let elem_size = cmp::max(1, mem::size_of::<T>());
207                 new_capacity = cmp::max(n, PAGE / elem_size);
208             }
209             chunk = TypedArenaChunk::<T>::new(new_capacity);
210             self.ptr.set(chunk.start());
211             self.end.set(chunk.end());
212             chunks.push(chunk);
213         }
214     }
215
216     /// Clears the arena. Deallocates all but the longest chunk which may be reused.
217     pub fn clear(&mut self) {
218         unsafe {
219             // Clear the last chunk, which is partially filled.
220             let mut chunks_borrow = self.chunks.borrow_mut();
221             if let Some(mut last_chunk) = chunks_borrow.pop() {
222                 self.clear_last_chunk(&mut last_chunk);
223                 // If `T` is ZST, code below has no effect.
224                 for mut chunk in chunks_borrow.drain(..) {
225                     let cap = chunk.storage.cap();
226                     chunk.destroy(cap);
227                 }
228                 chunks_borrow.push(last_chunk);
229             }
230         }
231     }
232
233     // Drops the contents of the last chunk. The last chunk is partially empty, unlike all other
234     // chunks.
235     fn clear_last_chunk(&self, last_chunk: &mut TypedArenaChunk<T>) {
236         // Determine how much was filled.
237         let start = last_chunk.start() as usize;
238         // We obtain the value of the pointer to the first uninitialized element.
239         let end = self.ptr.get() as usize;
240         // We then calculate the number of elements to be dropped in the last chunk,
241         // which is the filled area's length.
242         let diff = if mem::size_of::<T>() == 0 {
243             // `T` is ZST. It can't have a drop flag, so the value here doesn't matter. We get
244             // the number of zero-sized values in the last and only chunk, just out of caution.
245             // Recall that `end` was incremented for each allocated value.
246             end - start
247         } else {
248             (end - start) / mem::size_of::<T>()
249         };
250         // Pass that to the `destroy` method.
251         unsafe {
252             last_chunk.destroy(diff);
253         }
254         // Reset the chunk.
255         self.ptr.set(last_chunk.start());
256     }
257 }
258
259 unsafe impl<#[may_dangle] T> Drop for TypedArena<T> {
260     fn drop(&mut self) {
261         unsafe {
262             // Determine how much was filled.
263             let mut chunks_borrow = self.chunks.borrow_mut();
264             if let Some(mut last_chunk) = chunks_borrow.pop() {
265                 // Drop the contents of the last chunk.
266                 self.clear_last_chunk(&mut last_chunk);
267                 // The last chunk will be dropped. Destroy all other chunks.
268                 for chunk in chunks_borrow.iter_mut() {
269                     let cap = chunk.storage.cap();
270                     chunk.destroy(cap);
271                 }
272             }
273             // RawVec handles deallocation of `last_chunk` and `self.chunks`.
274         }
275     }
276 }
277
278 unsafe impl<T: Send> Send for TypedArena<T> {}
279
280 pub struct DroplessArena {
281     /// A pointer to the next object to be allocated.
282     ptr: Cell<*mut u8>,
283
284     /// A pointer to the end of the allocated area. When this pointer is
285     /// reached, a new chunk is allocated.
286     end: Cell<*mut u8>,
287
288     /// A vector of arena chunks.
289     chunks: RefCell<Vec<TypedArenaChunk<u8>>>,
290 }
291
292 impl DroplessArena {
293     pub fn new() -> DroplessArena {
294         DroplessArena {
295             ptr: Cell::new(0 as *mut u8),
296             end: Cell::new(0 as *mut u8),
297             chunks: RefCell::new(vec![]),
298         }
299     }
300
301     pub fn in_arena<T: ?Sized>(&self, ptr: *const T) -> bool {
302         let ptr = ptr as *const u8 as *mut u8;
303         for chunk in &*self.chunks.borrow() {
304             if chunk.start() <= ptr && ptr < chunk.end() {
305                 return true;
306             }
307         }
308
309         false
310     }
311
312     fn align_for<T>(&self) {
313         let align = mem::align_of::<T>();
314         let final_address = ((self.ptr.get() as usize) + align - 1) & !(align - 1);
315         self.ptr.set(final_address as *mut u8);
316         assert!(self.ptr <= self.end);
317     }
318
319     #[inline(never)]
320     #[cold]
321     fn grow<T>(&self, n: usize) {
322         let needed_bytes = n * mem::size_of::<T>();
323         unsafe {
324             let mut chunks = self.chunks.borrow_mut();
325             let (chunk, mut new_capacity);
326             if let Some(last_chunk) = chunks.last_mut() {
327                 let used_bytes = self.ptr.get() as usize - last_chunk.start() as usize;
328                 if last_chunk.storage.reserve_in_place(used_bytes, needed_bytes) {
329                     self.end.set(last_chunk.end());
330                     return;
331                 } else {
332                     new_capacity = last_chunk.storage.cap();
333                     loop {
334                         new_capacity = new_capacity.checked_mul(2).unwrap();
335                         if new_capacity >= used_bytes + needed_bytes {
336                             break;
337                         }
338                     }
339                 }
340             } else {
341                 new_capacity = cmp::max(needed_bytes, PAGE);
342             }
343             chunk = TypedArenaChunk::<u8>::new(new_capacity);
344             self.ptr.set(chunk.start());
345             self.end.set(chunk.end());
346             chunks.push(chunk);
347         }
348     }
349
350     #[inline]
351     pub fn alloc<T>(&self, object: T) -> &mut T {
352         unsafe {
353             assert!(!intrinsics::needs_drop::<T>());
354             assert!(mem::size_of::<T>() != 0);
355
356             self.align_for::<T>();
357             let future_end = intrinsics::arith_offset(self.ptr.get(), mem::size_of::<T>() as isize);
358             if (future_end as *mut u8) >= self.end.get() {
359                 self.grow::<T>(1)
360             }
361
362             let ptr = self.ptr.get();
363             // Set the pointer past ourselves
364             self.ptr.set(intrinsics::arith_offset(
365                     self.ptr.get(), mem::size_of::<T>() as isize
366             ) as *mut u8);
367             // Write into uninitialized memory.
368             ptr::write(ptr as *mut T, object);
369             &mut *(ptr as *mut T)
370         }
371     }
372
373     /// Allocates a slice of objects that are copied into the `DroplessArena`, returning a mutable
374     /// reference to it. Will panic if passed a zero-sized type.
375     ///
376     /// Panics:
377     ///  - Zero-sized types
378     ///  - Zero-length slices
379     #[inline]
380     pub fn alloc_slice<T>(&self, slice: &[T]) -> &mut [T]
381         where T: Copy {
382         unsafe {
383             assert!(!intrinsics::needs_drop::<T>());
384         }
385         assert!(mem::size_of::<T>() != 0);
386         assert!(slice.len() != 0);
387         self.align_for::<T>();
388
389         let future_end = unsafe {
390             intrinsics::arith_offset(self.ptr.get(), (slice.len() * mem::size_of::<T>()) as isize)
391         };
392         if (future_end as *mut u8) >= self.end.get() {
393             self.grow::<T>(slice.len());
394         }
395
396         unsafe {
397             let arena_slice = slice::from_raw_parts_mut(self.ptr.get() as *mut T, slice.len());
398             self.ptr.set(intrinsics::arith_offset(
399                     self.ptr.get(), (slice.len() * mem::size_of::<T>()) as isize
400             ) as *mut u8);
401             arena_slice.copy_from_slice(slice);
402             arena_slice
403         }
404     }
405 }
406
407 #[cfg(test)]
408 mod tests {
409     extern crate test;
410     use self::test::Bencher;
411     use super::TypedArena;
412     use std::cell::Cell;
413
414     #[allow(dead_code)]
415     #[derive(Debug, Eq, PartialEq)]
416     struct Point {
417         x: i32,
418         y: i32,
419         z: i32,
420     }
421
422     #[test]
423     pub fn test_unused() {
424         let arena: TypedArena<Point> = TypedArena::new();
425         assert!(arena.chunks.borrow().is_empty());
426     }
427
428     #[test]
429     fn test_arena_alloc_nested() {
430         struct Inner {
431             value: u8,
432         }
433         struct Outer<'a> {
434             inner: &'a Inner,
435         }
436         enum EI<'e> {
437             I(Inner),
438             O(Outer<'e>),
439         }
440
441         struct Wrap<'a>(TypedArena<EI<'a>>);
442
443         impl<'a> Wrap<'a> {
444             fn alloc_inner<F: Fn() -> Inner>(&self, f: F) -> &Inner {
445                 let r: &EI = self.0.alloc(EI::I(f()));
446                 if let &EI::I(ref i) = r {
447                     i
448                 } else {
449                     panic!("mismatch");
450                 }
451             }
452             fn alloc_outer<F: Fn() -> Outer<'a>>(&self, f: F) -> &Outer {
453                 let r: &EI = self.0.alloc(EI::O(f()));
454                 if let &EI::O(ref o) = r {
455                     o
456                 } else {
457                     panic!("mismatch");
458                 }
459             }
460         }
461
462         let arena = Wrap(TypedArena::new());
463
464         let result =
465             arena.alloc_outer(|| Outer { inner: arena.alloc_inner(|| Inner { value: 10 }) });
466
467         assert_eq!(result.inner.value, 10);
468     }
469
470     #[test]
471     pub fn test_copy() {
472         let arena = TypedArena::new();
473         for _ in 0..100000 {
474             arena.alloc(Point { x: 1, y: 2, z: 3 });
475         }
476     }
477
478     #[bench]
479     pub fn bench_copy(b: &mut Bencher) {
480         let arena = TypedArena::new();
481         b.iter(|| arena.alloc(Point { x: 1, y: 2, z: 3 }))
482     }
483
484     #[bench]
485     pub fn bench_copy_nonarena(b: &mut Bencher) {
486         b.iter(|| {
487             let _: Box<_> = Box::new(Point { x: 1, y: 2, z: 3 });
488         })
489     }
490
491     #[allow(dead_code)]
492     struct Noncopy {
493         string: String,
494         array: Vec<i32>,
495     }
496
497     #[test]
498     pub fn test_noncopy() {
499         let arena = TypedArena::new();
500         for _ in 0..100000 {
501             arena.alloc(Noncopy {
502                 string: "hello world".to_string(),
503                 array: vec![1, 2, 3, 4, 5],
504             });
505         }
506     }
507
508     #[test]
509     pub fn test_typed_arena_zero_sized() {
510         let arena = TypedArena::new();
511         for _ in 0..100000 {
512             arena.alloc(());
513         }
514     }
515
516     #[test]
517     pub fn test_typed_arena_clear() {
518         let mut arena = TypedArena::new();
519         for _ in 0..10 {
520             arena.clear();
521             for _ in 0..10000 {
522                 arena.alloc(Point { x: 1, y: 2, z: 3 });
523             }
524         }
525     }
526
527     // Drop tests
528
529     struct DropCounter<'a> {
530         count: &'a Cell<u32>,
531     }
532
533     impl<'a> Drop for DropCounter<'a> {
534         fn drop(&mut self) {
535             self.count.set(self.count.get() + 1);
536         }
537     }
538
539     #[test]
540     fn test_typed_arena_drop_count() {
541         let counter = Cell::new(0);
542         {
543             let arena: TypedArena<DropCounter> = TypedArena::new();
544             for _ in 0..100 {
545                 // Allocate something with drop glue to make sure it doesn't leak.
546                 arena.alloc(DropCounter { count: &counter });
547             }
548         };
549         assert_eq!(counter.get(), 100);
550     }
551
552     #[test]
553     fn test_typed_arena_drop_on_clear() {
554         let counter = Cell::new(0);
555         let mut arena: TypedArena<DropCounter> = TypedArena::new();
556         for i in 0..10 {
557             for _ in 0..100 {
558                 // Allocate something with drop glue to make sure it doesn't leak.
559                 arena.alloc(DropCounter { count: &counter });
560             }
561             arena.clear();
562             assert_eq!(counter.get(), i * 100 + 100);
563         }
564     }
565
566     thread_local! {
567         static DROP_COUNTER: Cell<u32> = Cell::new(0)
568     }
569
570     struct SmallDroppable;
571
572     impl Drop for SmallDroppable {
573         fn drop(&mut self) {
574             DROP_COUNTER.with(|c| c.set(c.get() + 1));
575         }
576     }
577
578     #[test]
579     fn test_typed_arena_drop_small_count() {
580         DROP_COUNTER.with(|c| c.set(0));
581         {
582             let arena: TypedArena<SmallDroppable> = TypedArena::new();
583             for _ in 0..100 {
584                 // Allocate something with drop glue to make sure it doesn't leak.
585                 arena.alloc(SmallDroppable);
586             }
587             // dropping
588         };
589         assert_eq!(DROP_COUNTER.with(|c| c.get()), 100);
590     }
591
592     #[bench]
593     pub fn bench_noncopy(b: &mut Bencher) {
594         let arena = TypedArena::new();
595         b.iter(|| {
596             arena.alloc(Noncopy {
597                 string: "hello world".to_string(),
598                 array: vec![1, 2, 3, 4, 5],
599             })
600         })
601     }
602
603     #[bench]
604     pub fn bench_noncopy_nonarena(b: &mut Bencher) {
605         b.iter(|| {
606             let _: Box<_> = Box::new(Noncopy {
607                 string: "hello world".to_string(),
608                 array: vec![1, 2, 3, 4, 5],
609             });
610         })
611     }
612 }