]> git.lizzy.rs Git - rust.git/blob - src/libarena/lib.rs
introduce Guard enum
[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 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
22        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
23        html_root_url = "https://doc.rust-lang.org/nightly/",
24        test(no_crate_inject, attr(deny(warnings))))]
25
26 #![feature(alloc)]
27 #![feature(core_intrinsics)]
28 #![feature(dropck_eyepatch)]
29 #![cfg_attr(not(stage0), feature(nll))]
30 #![feature(raw_vec_internals)]
31 #![cfg_attr(test, feature(test))]
32
33 #![allow(deprecated)]
34
35 extern crate alloc;
36 extern crate rustc_data_structures;
37
38 use rustc_data_structures::sync::MTLock;
39
40 use std::cell::{Cell, RefCell};
41 use std::cmp;
42 use std::intrinsics;
43 use std::marker::{PhantomData, Send};
44 use std::mem;
45 use std::ptr;
46 use std::slice;
47
48 use alloc::raw_vec::RawVec;
49
50 /// An arena that can hold objects of only one type.
51 pub struct TypedArena<T> {
52     /// A pointer to the next object to be allocated.
53     ptr: Cell<*mut T>,
54
55     /// A pointer to the end of the allocated area. When this pointer is
56     /// reached, a new chunk is allocated.
57     end: Cell<*mut T>,
58
59     /// A vector of arena chunks.
60     chunks: RefCell<Vec<TypedArenaChunk<T>>>,
61
62     /// Marker indicating that dropping the arena causes its owned
63     /// instances of `T` to be dropped.
64     _own: PhantomData<T>,
65 }
66
67 struct TypedArenaChunk<T> {
68     /// The raw storage for the arena chunk.
69     storage: RawVec<T>,
70 }
71
72 impl<T> TypedArenaChunk<T> {
73     #[inline]
74     unsafe fn new(capacity: usize) -> TypedArenaChunk<T> {
75         TypedArenaChunk {
76             storage: RawVec::with_capacity(capacity),
77         }
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 mem::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
141                     .set(intrinsics::arith_offset(self.ptr.get() as *mut u8, 1)
142                         as *mut T);
143                 let ptr = mem::align_of::<T>() as *mut T;
144                 // Don't drop the object. This `write` is equivalent to `forget`.
145                 ptr::write(ptr, object);
146                 &mut *ptr
147             } else {
148                 let ptr = self.ptr.get();
149                 // Advance the pointer.
150                 self.ptr.set(self.ptr.get().offset(1));
151                 // Write into uninitialized memory.
152                 ptr::write(ptr, object);
153                 &mut *ptr
154             }
155         }
156     }
157
158     /// Allocates a slice of objects that are copied into the `TypedArena`, returning a mutable
159     /// reference to it. Will panic if passed a zero-sized types.
160     ///
161     /// Panics:
162     ///
163     ///  - Zero-sized types
164     ///  - Zero-length slices
165     #[inline]
166     pub fn alloc_slice(&self, slice: &[T]) -> &mut [T]
167     where
168         T: Copy,
169     {
170         assert!(mem::size_of::<T>() != 0);
171         assert!(slice.len() != 0);
172
173         let available_capacity_bytes = self.end.get() as usize - self.ptr.get() as usize;
174         let at_least_bytes = slice.len() * mem::size_of::<T>();
175         if available_capacity_bytes < at_least_bytes {
176             self.grow(slice.len());
177         }
178
179         unsafe {
180             let start_ptr = self.ptr.get();
181             let arena_slice = slice::from_raw_parts_mut(start_ptr, slice.len());
182             self.ptr.set(start_ptr.offset(arena_slice.len() as isize));
183             arena_slice.copy_from_slice(slice);
184             arena_slice
185         }
186     }
187
188     /// Grows the arena.
189     #[inline(never)]
190     #[cold]
191     fn grow(&self, n: usize) {
192         unsafe {
193             let mut chunks = self.chunks.borrow_mut();
194             let (chunk, mut new_capacity);
195             if let Some(last_chunk) = chunks.last_mut() {
196                 let used_bytes = self.ptr.get() as usize - last_chunk.start() as usize;
197                 let currently_used_cap = used_bytes / mem::size_of::<T>();
198                 if last_chunk.storage.reserve_in_place(currently_used_cap, n) {
199                     self.end.set(last_chunk.end());
200                     return;
201                 } else {
202                     new_capacity = last_chunk.storage.cap();
203                     loop {
204                         new_capacity = new_capacity.checked_mul(2).unwrap();
205                         if new_capacity >= currently_used_cap + n {
206                             break;
207                         }
208                     }
209                 }
210             } else {
211                 let elem_size = cmp::max(1, mem::size_of::<T>());
212                 new_capacity = cmp::max(n, PAGE / elem_size);
213             }
214             chunk = TypedArenaChunk::<T>::new(new_capacity);
215             self.ptr.set(chunk.start());
216             self.end.set(chunk.end());
217             chunks.push(chunk);
218         }
219     }
220
221     /// Clears the arena. Deallocates all but the longest chunk which may be reused.
222     pub fn clear(&mut self) {
223         unsafe {
224             // Clear the last chunk, which is partially filled.
225             let mut chunks_borrow = self.chunks.borrow_mut();
226             if let Some(mut last_chunk) = chunks_borrow.pop() {
227                 self.clear_last_chunk(&mut last_chunk);
228                 // If `T` is ZST, code below has no effect.
229                 for mut chunk in chunks_borrow.drain(..) {
230                     let cap = chunk.storage.cap();
231                     chunk.destroy(cap);
232                 }
233                 chunks_borrow.push(last_chunk);
234             }
235         }
236     }
237
238     // Drops the contents of the last chunk. The last chunk is partially empty, unlike all other
239     // chunks.
240     fn clear_last_chunk(&self, last_chunk: &mut TypedArenaChunk<T>) {
241         // Determine how much was filled.
242         let start = last_chunk.start() as usize;
243         // We obtain the value of the pointer to the first uninitialized element.
244         let end = self.ptr.get() as usize;
245         // We then calculate the number of elements to be dropped in the last chunk,
246         // which is the filled area's length.
247         let diff = if mem::size_of::<T>() == 0 {
248             // `T` is ZST. It can't have a drop flag, so the value here doesn't matter. We get
249             // the number of zero-sized values in the last and only chunk, just out of caution.
250             // Recall that `end` was incremented for each allocated value.
251             end - start
252         } else {
253             (end - start) / mem::size_of::<T>()
254         };
255         // Pass that to the `destroy` method.
256         unsafe {
257             last_chunk.destroy(diff);
258         }
259         // Reset the chunk.
260         self.ptr.set(last_chunk.start());
261     }
262 }
263
264 unsafe impl<#[may_dangle] T> Drop for TypedArena<T> {
265     fn drop(&mut self) {
266         unsafe {
267             // Determine how much was filled.
268             let mut chunks_borrow = self.chunks.borrow_mut();
269             if let Some(mut last_chunk) = chunks_borrow.pop() {
270                 // Drop the contents of the last chunk.
271                 self.clear_last_chunk(&mut last_chunk);
272                 // The last chunk will be dropped. Destroy all other chunks.
273                 for chunk in chunks_borrow.iter_mut() {
274                     let cap = chunk.storage.cap();
275                     chunk.destroy(cap);
276                 }
277             }
278             // RawVec handles deallocation of `last_chunk` and `self.chunks`.
279         }
280     }
281 }
282
283 unsafe impl<T: Send> Send for TypedArena<T> {}
284
285 pub struct DroplessArena {
286     /// A pointer to the next object to be allocated.
287     ptr: Cell<*mut u8>,
288
289     /// A pointer to the end of the allocated area. When this pointer is
290     /// reached, a new chunk is allocated.
291     end: Cell<*mut u8>,
292
293     /// A vector of arena chunks.
294     chunks: RefCell<Vec<TypedArenaChunk<u8>>>,
295 }
296
297 unsafe impl Send for DroplessArena {}
298
299 impl DroplessArena {
300     pub fn new() -> DroplessArena {
301         DroplessArena {
302             ptr: Cell::new(0 as *mut u8),
303             end: Cell::new(0 as *mut u8),
304             chunks: RefCell::new(vec![]),
305         }
306     }
307
308     pub fn in_arena<T: ?Sized>(&self, ptr: *const T) -> bool {
309         let ptr = ptr as *const u8 as *mut u8;
310         for chunk in &*self.chunks.borrow() {
311             if chunk.start() <= ptr && ptr < chunk.end() {
312                 return true;
313             }
314         }
315
316         false
317     }
318
319     fn align(&self, align: usize) {
320         let final_address = ((self.ptr.get() as usize) + align - 1) & !(align - 1);
321         self.ptr.set(final_address as *mut u8);
322         assert!(self.ptr <= self.end);
323     }
324
325     #[inline(never)]
326     #[cold]
327     fn grow(&self, needed_bytes: usize) {
328         unsafe {
329             let mut chunks = self.chunks.borrow_mut();
330             let (chunk, mut new_capacity);
331             if let Some(last_chunk) = chunks.last_mut() {
332                 let used_bytes = self.ptr.get() as usize - last_chunk.start() as usize;
333                 if last_chunk
334                     .storage
335                     .reserve_in_place(used_bytes, needed_bytes)
336                 {
337                     self.end.set(last_chunk.end());
338                     return;
339                 } else {
340                     new_capacity = last_chunk.storage.cap();
341                     loop {
342                         new_capacity = new_capacity.checked_mul(2).unwrap();
343                         if new_capacity >= used_bytes + needed_bytes {
344                             break;
345                         }
346                     }
347                 }
348             } else {
349                 new_capacity = cmp::max(needed_bytes, PAGE);
350             }
351             chunk = TypedArenaChunk::<u8>::new(new_capacity);
352             self.ptr.set(chunk.start());
353             self.end.set(chunk.end());
354             chunks.push(chunk);
355         }
356     }
357
358     #[inline]
359     pub fn alloc_raw(&self, bytes: usize, align: usize) -> &mut [u8] {
360         unsafe {
361             assert!(bytes != 0);
362
363             self.align(align);
364
365             let future_end = intrinsics::arith_offset(self.ptr.get(), bytes as isize);
366             if (future_end as *mut u8) >= self.end.get() {
367                 self.grow(bytes);
368             }
369
370             let ptr = self.ptr.get();
371             // Set the pointer past ourselves
372             self.ptr.set(
373                 intrinsics::arith_offset(self.ptr.get(), bytes as isize) as *mut u8,
374             );
375             slice::from_raw_parts_mut(ptr, bytes)
376         }
377     }
378
379     #[inline]
380     pub fn alloc<T>(&self, object: T) -> &mut T {
381         assert!(!mem::needs_drop::<T>());
382
383         let mem = self.alloc_raw(
384             mem::size_of::<T>(),
385             mem::align_of::<T>()) as *mut _ as *mut T;
386
387         unsafe {
388             // Write into uninitialized memory.
389             ptr::write(mem, object);
390             &mut *mem
391         }
392     }
393
394     /// Allocates a slice of objects that are copied into the `DroplessArena`, returning a mutable
395     /// reference to it. Will panic if passed a zero-sized type.
396     ///
397     /// Panics:
398     ///
399     ///  - Zero-sized types
400     ///  - Zero-length slices
401     #[inline]
402     pub fn alloc_slice<T>(&self, slice: &[T]) -> &mut [T]
403     where
404         T: Copy,
405     {
406         assert!(!mem::needs_drop::<T>());
407         assert!(mem::size_of::<T>() != 0);
408         assert!(slice.len() != 0);
409
410         let mem = self.alloc_raw(
411             slice.len() * mem::size_of::<T>(),
412             mem::align_of::<T>()) as *mut _ as *mut T;
413
414         unsafe {
415             let arena_slice = slice::from_raw_parts_mut(mem, slice.len());
416             arena_slice.copy_from_slice(slice);
417             arena_slice
418         }
419     }
420 }
421
422 pub struct SyncTypedArena<T> {
423     lock: MTLock<TypedArena<T>>,
424 }
425
426 impl<T> SyncTypedArena<T> {
427     #[inline(always)]
428     pub fn new() -> SyncTypedArena<T> {
429         SyncTypedArena {
430             lock: MTLock::new(TypedArena::new())
431         }
432     }
433
434     #[inline(always)]
435     pub fn alloc(&self, object: T) -> &mut T {
436         // Extend the lifetime of the result since it's limited to the lock guard
437         unsafe { &mut *(self.lock.lock().alloc(object) as *mut T) }
438     }
439
440     #[inline(always)]
441     pub fn alloc_slice(&self, slice: &[T]) -> &mut [T]
442     where
443         T: Copy,
444     {
445         // Extend the lifetime of the result since it's limited to the lock guard
446         unsafe { &mut *(self.lock.lock().alloc_slice(slice) as *mut [T]) }
447     }
448
449     #[inline(always)]
450     pub fn clear(&mut self) {
451         self.lock.get_mut().clear();
452     }
453 }
454
455 pub struct SyncDroplessArena {
456     lock: MTLock<DroplessArena>,
457 }
458
459 impl SyncDroplessArena {
460     #[inline(always)]
461     pub fn new() -> SyncDroplessArena {
462         SyncDroplessArena {
463             lock: MTLock::new(DroplessArena::new())
464         }
465     }
466
467     #[inline(always)]
468     pub fn in_arena<T: ?Sized>(&self, ptr: *const T) -> bool {
469         self.lock.lock().in_arena(ptr)
470     }
471
472     #[inline(always)]
473     pub fn alloc_raw(&self, bytes: usize, align: usize) -> &mut [u8] {
474         // Extend the lifetime of the result since it's limited to the lock guard
475         unsafe { &mut *(self.lock.lock().alloc_raw(bytes, align) as *mut [u8]) }
476     }
477
478     #[inline(always)]
479     pub fn alloc<T>(&self, object: T) -> &mut T {
480         // Extend the lifetime of the result since it's limited to the lock guard
481         unsafe { &mut *(self.lock.lock().alloc(object) as *mut T) }
482     }
483
484     #[inline(always)]
485     pub fn alloc_slice<T>(&self, slice: &[T]) -> &mut [T]
486     where
487         T: Copy,
488     {
489         // Extend the lifetime of the result since it's limited to the lock guard
490         unsafe { &mut *(self.lock.lock().alloc_slice(slice) as *mut [T]) }
491     }
492 }
493
494 #[cfg(test)]
495 mod tests {
496     extern crate test;
497     use self::test::Bencher;
498     use super::TypedArena;
499     use std::cell::Cell;
500
501     #[allow(dead_code)]
502     #[derive(Debug, Eq, PartialEq)]
503     struct Point {
504         x: i32,
505         y: i32,
506         z: i32,
507     }
508
509     #[test]
510     pub fn test_unused() {
511         let arena: TypedArena<Point> = TypedArena::new();
512         assert!(arena.chunks.borrow().is_empty());
513     }
514
515     #[test]
516     fn test_arena_alloc_nested() {
517         struct Inner {
518             value: u8,
519         }
520         struct Outer<'a> {
521             inner: &'a Inner,
522         }
523         enum EI<'e> {
524             I(Inner),
525             O(Outer<'e>),
526         }
527
528         struct Wrap<'a>(TypedArena<EI<'a>>);
529
530         impl<'a> Wrap<'a> {
531             fn alloc_inner<F: Fn() -> Inner>(&self, f: F) -> &Inner {
532                 let r: &EI = self.0.alloc(EI::I(f()));
533                 if let &EI::I(ref i) = r {
534                     i
535                 } else {
536                     panic!("mismatch");
537                 }
538             }
539             fn alloc_outer<F: Fn() -> Outer<'a>>(&self, f: F) -> &Outer {
540                 let r: &EI = self.0.alloc(EI::O(f()));
541                 if let &EI::O(ref o) = r {
542                     o
543                 } else {
544                     panic!("mismatch");
545                 }
546             }
547         }
548
549         let arena = Wrap(TypedArena::new());
550
551         let result = arena.alloc_outer(|| Outer {
552             inner: arena.alloc_inner(|| Inner { value: 10 }),
553         });
554
555         assert_eq!(result.inner.value, 10);
556     }
557
558     #[test]
559     pub fn test_copy() {
560         let arena = TypedArena::new();
561         for _ in 0..100000 {
562             arena.alloc(Point { x: 1, y: 2, z: 3 });
563         }
564     }
565
566     #[bench]
567     pub fn bench_copy(b: &mut Bencher) {
568         let arena = TypedArena::new();
569         b.iter(|| arena.alloc(Point { x: 1, y: 2, z: 3 }))
570     }
571
572     #[bench]
573     pub fn bench_copy_nonarena(b: &mut Bencher) {
574         b.iter(|| {
575             let _: Box<_> = Box::new(Point { x: 1, y: 2, z: 3 });
576         })
577     }
578
579     #[allow(dead_code)]
580     struct Noncopy {
581         string: String,
582         array: Vec<i32>,
583     }
584
585     #[test]
586     pub fn test_noncopy() {
587         let arena = TypedArena::new();
588         for _ in 0..100000 {
589             arena.alloc(Noncopy {
590                 string: "hello world".to_string(),
591                 array: vec![1, 2, 3, 4, 5],
592             });
593         }
594     }
595
596     #[test]
597     pub fn test_typed_arena_zero_sized() {
598         let arena = TypedArena::new();
599         for _ in 0..100000 {
600             arena.alloc(());
601         }
602     }
603
604     #[test]
605     pub fn test_typed_arena_clear() {
606         let mut arena = TypedArena::new();
607         for _ in 0..10 {
608             arena.clear();
609             for _ in 0..10000 {
610                 arena.alloc(Point { x: 1, y: 2, z: 3 });
611             }
612         }
613     }
614
615     // Drop tests
616
617     struct DropCounter<'a> {
618         count: &'a Cell<u32>,
619     }
620
621     impl<'a> Drop for DropCounter<'a> {
622         fn drop(&mut self) {
623             self.count.set(self.count.get() + 1);
624         }
625     }
626
627     #[test]
628     fn test_typed_arena_drop_count() {
629         let counter = Cell::new(0);
630         {
631             let arena: TypedArena<DropCounter> = TypedArena::new();
632             for _ in 0..100 {
633                 // Allocate something with drop glue to make sure it doesn't leak.
634                 arena.alloc(DropCounter { count: &counter });
635             }
636         };
637         assert_eq!(counter.get(), 100);
638     }
639
640     #[test]
641     fn test_typed_arena_drop_on_clear() {
642         let counter = Cell::new(0);
643         let mut arena: TypedArena<DropCounter> = TypedArena::new();
644         for i in 0..10 {
645             for _ in 0..100 {
646                 // Allocate something with drop glue to make sure it doesn't leak.
647                 arena.alloc(DropCounter { count: &counter });
648             }
649             arena.clear();
650             assert_eq!(counter.get(), i * 100 + 100);
651         }
652     }
653
654     thread_local! {
655         static DROP_COUNTER: Cell<u32> = Cell::new(0)
656     }
657
658     struct SmallDroppable;
659
660     impl Drop for SmallDroppable {
661         fn drop(&mut self) {
662             DROP_COUNTER.with(|c| c.set(c.get() + 1));
663         }
664     }
665
666     #[test]
667     fn test_typed_arena_drop_small_count() {
668         DROP_COUNTER.with(|c| c.set(0));
669         {
670             let arena: TypedArena<SmallDroppable> = TypedArena::new();
671             for _ in 0..100 {
672                 // Allocate something with drop glue to make sure it doesn't leak.
673                 arena.alloc(SmallDroppable);
674             }
675             // dropping
676         };
677         assert_eq!(DROP_COUNTER.with(|c| c.get()), 100);
678     }
679
680     #[bench]
681     pub fn bench_noncopy(b: &mut Bencher) {
682         let arena = TypedArena::new();
683         b.iter(|| {
684             arena.alloc(Noncopy {
685                 string: "hello world".to_string(),
686                 array: vec![1, 2, 3, 4, 5],
687             })
688         })
689     }
690
691     #[bench]
692     pub fn bench_noncopy_nonarena(b: &mut Bencher) {
693         b.iter(|| {
694             let _: Box<_> = Box::new(Noncopy {
695                 string: "hello world".to_string(),
696                 array: vec![1, 2, 3, 4, 5],
697             });
698         })
699     }
700 }