]> git.lizzy.rs Git - rust.git/blob - src/libarena/lib.rs
core: Split apart the global `core` feature
[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 has two arenas implemented: `TypedArena`, which is a simpler
19 //! arena but can only hold objects of a single type, and `Arena`, which is a
20 //! more complex, slower arena which can hold objects of any type.
21
22 // Do not remove on snapshot creation. Needed for bootstrap. (Issue #22364)
23 #![cfg_attr(stage0, feature(custom_attribute))]
24 #![crate_name = "arena"]
25 #![unstable(feature = "rustc_private")]
26 #![staged_api]
27 #![crate_type = "rlib"]
28 #![crate_type = "dylib"]
29 #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
30        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
31        html_root_url = "http://doc.rust-lang.org/nightly/")]
32
33 #![feature(alloc)]
34 #![feature(box_syntax)]
35 #![feature(core_intrinsics)]
36 #![feature(ptr_as_ref)]
37 #![feature(raw)]
38 #![feature(staged_api)]
39 #![feature(unboxed_closures)]
40 #![cfg_attr(test, feature(test))]
41
42 extern crate alloc;
43
44 use std::cell::{Cell, RefCell};
45 use std::cmp;
46 use std::intrinsics;
47 use std::marker;
48 use std::mem;
49 use std::ptr;
50 use std::rc::Rc;
51 use std::rt::heap::{allocate, deallocate};
52
53 // The way arena uses arrays is really deeply awful. The arrays are
54 // allocated, and have capacities reserved, but the fill for the array
55 // will always stay at 0.
56 #[derive(Clone, PartialEq)]
57 struct Chunk {
58     data: Rc<RefCell<Vec<u8>>>,
59     fill: Cell<usize>,
60     is_copy: Cell<bool>,
61 }
62
63 impl Chunk {
64     fn capacity(&self) -> usize {
65         self.data.borrow().capacity()
66     }
67
68     unsafe fn as_ptr(&self) -> *const u8 {
69         self.data.borrow().as_ptr()
70     }
71 }
72
73 /// A slower reflection-based arena that can allocate objects of any type.
74 ///
75 /// This arena uses `Vec<u8>` as a backing store to allocate objects from. For
76 /// each allocated object, the arena stores a pointer to the type descriptor
77 /// followed by the object (potentially with alignment padding after each
78 /// element). When the arena is destroyed, it iterates through all of its
79 /// chunks, and uses the tydesc information to trace through the objects,
80 /// calling the destructors on them. One subtle point that needs to be
81 /// addressed is how to handle panics while running the user provided
82 /// initializer function. It is important to not run the destructor on
83 /// uninitialized objects, but how to detect them is somewhat subtle. Since
84 /// `alloc()` can be invoked recursively, it is not sufficient to simply exclude
85 /// the most recent object. To solve this without requiring extra space, we
86 /// use the low order bit of the tydesc pointer to encode whether the object
87 /// it describes has been fully initialized.
88 ///
89 /// As an optimization, objects with destructors are stored in different chunks
90 /// than objects without destructors. This reduces overhead when initializing
91 /// plain-old-data (`Copy` types) and means we don't need to waste time running
92 /// their destructors.
93 pub struct Arena<'longer_than_self> {
94     // The head is separated out from the list as a unbenchmarked
95     // microoptimization, to avoid needing to case on the list to access the
96     // head.
97     head: RefCell<Chunk>,
98     copy_head: RefCell<Chunk>,
99     chunks: RefCell<Vec<Chunk>>,
100     _marker: marker::PhantomData<*mut &'longer_than_self()>,
101 }
102
103 impl<'a> Arena<'a> {
104     /// Allocates a new Arena with 32 bytes preallocated.
105     pub fn new() -> Arena<'a> {
106         Arena::new_with_size(32)
107     }
108
109     /// Allocates a new Arena with `initial_size` bytes preallocated.
110     pub fn new_with_size(initial_size: usize) -> Arena<'a> {
111         Arena {
112             head: RefCell::new(chunk(initial_size, false)),
113             copy_head: RefCell::new(chunk(initial_size, true)),
114             chunks: RefCell::new(Vec::new()),
115             _marker: marker::PhantomData,
116         }
117     }
118 }
119
120 fn chunk(size: usize, is_copy: bool) -> Chunk {
121     Chunk {
122         data: Rc::new(RefCell::new(Vec::with_capacity(size))),
123         fill: Cell::new(0),
124         is_copy: Cell::new(is_copy),
125     }
126 }
127
128 impl<'longer_than_self> Drop for Arena<'longer_than_self> {
129     fn drop(&mut self) {
130         unsafe {
131             destroy_chunk(&*self.head.borrow());
132             for chunk in self.chunks.borrow().iter() {
133                 if !chunk.is_copy.get() {
134                     destroy_chunk(chunk);
135                 }
136             }
137         }
138     }
139 }
140
141 #[inline]
142 fn round_up(base: usize, align: usize) -> usize {
143     (base.checked_add(align - 1)).unwrap() & !(align - 1)
144 }
145
146 // Walk down a chunk, running the destructors for any objects stored
147 // in it.
148 unsafe fn destroy_chunk(chunk: &Chunk) {
149     let mut idx = 0;
150     let buf = chunk.as_ptr();
151     let fill = chunk.fill.get();
152
153     while idx < fill {
154         let tydesc_data: *const usize = mem::transmute(buf.offset(idx as isize));
155         let (tydesc, is_done) = un_bitpack_tydesc_ptr(*tydesc_data);
156         let (size, align) = ((*tydesc).size, (*tydesc).align);
157
158         let after_tydesc = idx + mem::size_of::<*const TyDesc>();
159
160         let start = round_up(after_tydesc, align);
161
162         //debug!("freeing object: idx = {}, size = {}, align = {}, done = {}",
163         //       start, size, align, is_done);
164         if is_done {
165             ((*tydesc).drop_glue)(buf.offset(start as isize) as *const i8);
166         }
167
168         // Find where the next tydesc lives
169         idx = round_up(start + size, mem::align_of::<*const TyDesc>());
170     }
171 }
172
173 // We encode whether the object a tydesc describes has been
174 // initialized in the arena in the low bit of the tydesc pointer. This
175 // is necessary in order to properly do cleanup if a panic occurs
176 // during an initializer.
177 #[inline]
178 fn bitpack_tydesc_ptr(p: *const TyDesc, is_done: bool) -> usize {
179     p as usize | (is_done as usize)
180 }
181 #[inline]
182 fn un_bitpack_tydesc_ptr(p: usize) -> (*const TyDesc, bool) {
183     ((p & !1) as *const TyDesc, p & 1 == 1)
184 }
185
186 // HACK(eddyb) TyDesc replacement using a trait object vtable.
187 // This could be replaced in the future with a custom DST layout,
188 // or `&'static (drop_glue, size, align)` created by a `const fn`.
189 struct TyDesc {
190     drop_glue: fn(*const i8),
191     size: usize,
192     align: usize
193 }
194
195 trait AllTypes { fn dummy(&self) { } }
196 impl<T:?Sized> AllTypes for T { }
197
198 unsafe fn get_tydesc<T>() -> *const TyDesc {
199     use std::raw::TraitObject;
200
201     let ptr = &*(1 as *const T);
202
203     // Can use any trait that is implemented for all types.
204     let obj = mem::transmute::<&AllTypes, TraitObject>(ptr);
205     obj.vtable as *const TyDesc
206 }
207
208 impl<'longer_than_self> Arena<'longer_than_self> {
209     fn chunk_size(&self) -> usize {
210         self.copy_head.borrow().capacity()
211     }
212
213     // Functions for the POD part of the arena
214     fn alloc_copy_grow(&self, n_bytes: usize, align: usize) -> *const u8 {
215         // Allocate a new chunk.
216         let new_min_chunk_size = cmp::max(n_bytes, self.chunk_size());
217         self.chunks.borrow_mut().push(self.copy_head.borrow().clone());
218
219         *self.copy_head.borrow_mut() =
220             chunk((new_min_chunk_size + 1).next_power_of_two(), true);
221
222         return self.alloc_copy_inner(n_bytes, align);
223     }
224
225     #[inline]
226     fn alloc_copy_inner(&self, n_bytes: usize, align: usize) -> *const u8 {
227         let start = round_up(self.copy_head.borrow().fill.get(), align);
228
229         let end = start + n_bytes;
230         if end > self.chunk_size() {
231             return self.alloc_copy_grow(n_bytes, align);
232         }
233
234         let copy_head = self.copy_head.borrow();
235         copy_head.fill.set(end);
236
237         unsafe {
238             copy_head.as_ptr().offset(start as isize)
239         }
240     }
241
242     #[inline]
243     fn alloc_copy<T, F>(&self, op: F) -> &mut T where F: FnOnce() -> T {
244         unsafe {
245             let ptr = self.alloc_copy_inner(mem::size_of::<T>(),
246                                             mem::min_align_of::<T>());
247             let ptr = ptr as *mut T;
248             ptr::write(&mut (*ptr), op());
249             return &mut *ptr;
250         }
251     }
252
253     // Functions for the non-POD part of the arena
254     fn alloc_noncopy_grow(&self, n_bytes: usize,
255                           align: usize) -> (*const u8, *const u8) {
256         // Allocate a new chunk.
257         let new_min_chunk_size = cmp::max(n_bytes, self.chunk_size());
258         self.chunks.borrow_mut().push(self.head.borrow().clone());
259
260         *self.head.borrow_mut() =
261             chunk((new_min_chunk_size + 1).next_power_of_two(), false);
262
263         return self.alloc_noncopy_inner(n_bytes, align);
264     }
265
266     #[inline]
267     fn alloc_noncopy_inner(&self, n_bytes: usize,
268                            align: usize) -> (*const u8, *const u8) {
269         // Be careful to not maintain any `head` borrows active, because
270         // `alloc_noncopy_grow` borrows it mutably.
271         let (start, end, tydesc_start, head_capacity) = {
272             let head = self.head.borrow();
273             let fill = head.fill.get();
274
275             let tydesc_start = fill;
276             let after_tydesc = fill + mem::size_of::<*const TyDesc>();
277             let start = round_up(after_tydesc, align);
278             let end = start + n_bytes;
279
280             (start, end, tydesc_start, head.capacity())
281         };
282
283         if end > head_capacity {
284             return self.alloc_noncopy_grow(n_bytes, align);
285         }
286
287         let head = self.head.borrow();
288         head.fill.set(round_up(end, mem::align_of::<*const TyDesc>()));
289
290         unsafe {
291             let buf = head.as_ptr();
292             return (buf.offset(tydesc_start as isize), buf.offset(start as isize));
293         }
294     }
295
296     #[inline]
297     fn alloc_noncopy<T, F>(&self, op: F) -> &mut T where F: FnOnce() -> T {
298         unsafe {
299             let tydesc = get_tydesc::<T>();
300             let (ty_ptr, ptr) =
301                 self.alloc_noncopy_inner(mem::size_of::<T>(),
302                                          mem::min_align_of::<T>());
303             let ty_ptr = ty_ptr as *mut usize;
304             let ptr = ptr as *mut T;
305             // Write in our tydesc along with a bit indicating that it
306             // has *not* been initialized yet.
307             *ty_ptr = mem::transmute(tydesc);
308             // Actually initialize it
309             ptr::write(&mut(*ptr), op());
310             // Now that we are done, update the tydesc to indicate that
311             // the object is there.
312             *ty_ptr = bitpack_tydesc_ptr(tydesc, true);
313
314             return &mut *ptr;
315         }
316     }
317
318     /// Allocates a new item in the arena, using `op` to initialize the value,
319     /// and returns a reference to it.
320     #[inline]
321     pub fn alloc<T:'longer_than_self, F>(&self, op: F) -> &mut T where F: FnOnce() -> T {
322         unsafe {
323             if intrinsics::needs_drop::<T>() {
324                 self.alloc_noncopy(op)
325             } else {
326                 self.alloc_copy(op)
327             }
328         }
329     }
330 }
331
332 #[test]
333 fn test_arena_destructors() {
334     let arena = Arena::new();
335     for i in 0..10 {
336         // Arena allocate something with drop glue to make sure it
337         // doesn't leak.
338         arena.alloc(|| Rc::new(i));
339         // Allocate something with funny size and alignment, to keep
340         // things interesting.
341         arena.alloc(|| [0u8, 1u8, 2u8]);
342     }
343 }
344
345 #[test]
346 #[should_panic]
347 fn test_arena_destructors_fail() {
348     let arena = Arena::new();
349     // Put some stuff in the arena.
350     for i in 0..10 {
351         // Arena allocate something with drop glue to make sure it
352         // doesn't leak.
353         arena.alloc(|| { Rc::new(i) });
354         // Allocate something with funny size and alignment, to keep
355         // things interesting.
356         arena.alloc(|| { [0u8, 1, 2] });
357     }
358     // Now, panic while allocating
359     arena.alloc::<Rc<i32>, _>(|| {
360         panic!();
361     });
362 }
363
364 /// A faster arena that can hold objects of only one type.
365 pub struct TypedArena<T> {
366     /// A pointer to the next object to be allocated.
367     ptr: Cell<*const T>,
368
369     /// A pointer to the end of the allocated area. When this pointer is
370     /// reached, a new chunk is allocated.
371     end: Cell<*const T>,
372
373     /// A pointer to the first arena segment.
374     first: RefCell<*mut TypedArenaChunk<T>>,
375
376     /// Marker indicating that dropping the arena causes its owned
377     /// instances of `T` to be dropped.
378     _own: marker::PhantomData<T>,
379 }
380
381 struct TypedArenaChunk<T> {
382     marker: marker::PhantomData<T>,
383
384     /// Pointer to the next arena segment.
385     next: *mut TypedArenaChunk<T>,
386
387     /// The number of elements that this chunk can hold.
388     capacity: usize,
389
390     // Objects follow here, suitably aligned.
391 }
392
393 fn calculate_size<T>(capacity: usize) -> usize {
394     let mut size = mem::size_of::<TypedArenaChunk<T>>();
395     size = round_up(size, mem::min_align_of::<T>());
396     let elem_size = mem::size_of::<T>();
397     let elems_size = elem_size.checked_mul(capacity).unwrap();
398     size = size.checked_add(elems_size).unwrap();
399     size
400 }
401
402 impl<T> TypedArenaChunk<T> {
403     #[inline]
404     unsafe fn new(next: *mut TypedArenaChunk<T>, capacity: usize)
405            -> *mut TypedArenaChunk<T> {
406         let size = calculate_size::<T>(capacity);
407         let chunk = allocate(size, mem::min_align_of::<TypedArenaChunk<T>>())
408                     as *mut TypedArenaChunk<T>;
409         if chunk.is_null() { alloc::oom() }
410         (*chunk).next = next;
411         (*chunk).capacity = capacity;
412         chunk
413     }
414
415     /// Destroys this arena chunk. If the type descriptor is supplied, the
416     /// drop glue is called; otherwise, drop glue is not called.
417     #[inline]
418     unsafe fn destroy(&mut self, len: usize) {
419         // Destroy all the allocated objects.
420         if intrinsics::needs_drop::<T>() {
421             let mut start = self.start();
422             for _ in 0..len {
423                 ptr::read(start as *const T); // run the destructor on the pointer
424                 start = start.offset(mem::size_of::<T>() as isize)
425             }
426         }
427
428         // Destroy the next chunk.
429         let next = self.next;
430         let size = calculate_size::<T>(self.capacity);
431         let self_ptr: *mut TypedArenaChunk<T> = self;
432         deallocate(self_ptr as *mut u8, size,
433                    mem::min_align_of::<TypedArenaChunk<T>>());
434         if !next.is_null() {
435             let capacity = (*next).capacity;
436             (*next).destroy(capacity);
437         }
438     }
439
440     // Returns a pointer to the first allocated object.
441     #[inline]
442     fn start(&self) -> *const u8 {
443         let this: *const TypedArenaChunk<T> = self;
444         unsafe {
445             mem::transmute(round_up(this.offset(1) as usize,
446                                     mem::min_align_of::<T>()))
447         }
448     }
449
450     // Returns a pointer to the end of the allocated space.
451     #[inline]
452     fn end(&self) -> *const u8 {
453         unsafe {
454             let size = mem::size_of::<T>().checked_mul(self.capacity).unwrap();
455             self.start().offset(size as isize)
456         }
457     }
458 }
459
460 impl<T> TypedArena<T> {
461     /// Creates a new `TypedArena` with preallocated space for eight objects.
462     #[inline]
463     pub fn new() -> TypedArena<T> {
464         TypedArena::with_capacity(8)
465     }
466
467     /// Creates a new `TypedArena` with preallocated space for the given number of
468     /// objects.
469     #[inline]
470     pub fn with_capacity(capacity: usize) -> TypedArena<T> {
471         unsafe {
472             let chunk = TypedArenaChunk::<T>::new(ptr::null_mut(), capacity);
473             TypedArena {
474                 ptr: Cell::new((*chunk).start() as *const T),
475                 end: Cell::new((*chunk).end() as *const T),
476                 first: RefCell::new(chunk),
477                 _own: marker::PhantomData,
478             }
479         }
480     }
481
482     /// Allocates an object in the `TypedArena`, returning a reference to it.
483     #[inline]
484     pub fn alloc(&self, object: T) -> &mut T {
485         if self.ptr == self.end {
486             self.grow()
487         }
488
489         let ptr: &mut T = unsafe {
490             let ptr: &mut T = mem::transmute(self.ptr.clone());
491             ptr::write(ptr, object);
492             self.ptr.set(self.ptr.get().offset(1));
493             ptr
494         };
495
496         ptr
497     }
498
499     /// Grows the arena.
500     #[inline(never)]
501     fn grow(&self) {
502         unsafe {
503             let chunk = *self.first.borrow_mut();
504             let new_capacity = (*chunk).capacity.checked_mul(2).unwrap();
505             let chunk = TypedArenaChunk::<T>::new(chunk, new_capacity);
506             self.ptr.set((*chunk).start() as *const T);
507             self.end.set((*chunk).end() as *const T);
508             *self.first.borrow_mut() = chunk
509         }
510     }
511 }
512
513 impl<T> Drop for TypedArena<T> {
514     fn drop(&mut self) {
515         unsafe {
516             // Determine how much was filled.
517             let start = self.first.borrow().as_ref().unwrap().start() as usize;
518             let end = self.ptr.get() as usize;
519             let diff = (end - start) / mem::size_of::<T>();
520
521             // Pass that to the `destroy` method.
522             (**self.first.borrow_mut()).destroy(diff)
523         }
524     }
525 }
526
527 #[cfg(test)]
528 mod tests {
529     extern crate test;
530     use self::test::Bencher;
531     use super::{Arena, TypedArena};
532
533     #[allow(dead_code)]
534     struct Point {
535         x: i32,
536         y: i32,
537         z: i32,
538     }
539
540     #[test]
541     fn test_arena_alloc_nested() {
542         struct Inner { value: u8 }
543         struct Outer<'a> { inner: &'a Inner }
544         enum EI<'e> { I(Inner), O(Outer<'e>) }
545
546         struct Wrap<'a>(TypedArena<EI<'a>>);
547
548         impl<'a> Wrap<'a> {
549             fn alloc_inner<F:Fn() -> Inner>(&self, f: F) -> &Inner {
550                 let r: &EI = self.0.alloc(EI::I(f()));
551                 if let &EI::I(ref i) = r {
552                     i
553                 } else {
554                     panic!("mismatch");
555                 }
556             }
557             fn alloc_outer<F:Fn() -> Outer<'a>>(&self, f: F) -> &Outer {
558                 let r: &EI = self.0.alloc(EI::O(f()));
559                 if let &EI::O(ref o) = r {
560                     o
561                 } else {
562                     panic!("mismatch");
563                 }
564             }
565         }
566
567         let arena = Wrap(TypedArena::new());
568
569         let result = arena.alloc_outer(|| Outer {
570             inner: arena.alloc_inner(|| Inner { value: 10 }) });
571
572         assert_eq!(result.inner.value, 10);
573     }
574
575     #[test]
576     pub fn test_copy() {
577         let arena = TypedArena::new();
578         for _ in 0..100000 {
579             arena.alloc(Point {
580                 x: 1,
581                 y: 2,
582                 z: 3,
583             });
584         }
585     }
586
587     #[bench]
588     pub fn bench_copy(b: &mut Bencher) {
589         let arena = TypedArena::new();
590         b.iter(|| {
591             arena.alloc(Point {
592                 x: 1,
593                 y: 2,
594                 z: 3,
595             })
596         })
597     }
598
599     #[bench]
600     pub fn bench_copy_nonarena(b: &mut Bencher) {
601         b.iter(|| {
602             let _: Box<_> = box Point {
603                 x: 1,
604                 y: 2,
605                 z: 3,
606             };
607         })
608     }
609
610     #[bench]
611     pub fn bench_copy_old_arena(b: &mut Bencher) {
612         let arena = Arena::new();
613         b.iter(|| {
614             arena.alloc(|| {
615                 Point {
616                     x: 1,
617                     y: 2,
618                     z: 3,
619                 }
620             })
621         })
622     }
623
624     #[allow(dead_code)]
625     struct Noncopy {
626         string: String,
627         array: Vec<i32>,
628     }
629
630     #[test]
631     pub fn test_noncopy() {
632         let arena = TypedArena::new();
633         for _ in 0..100000 {
634             arena.alloc(Noncopy {
635                 string: "hello world".to_string(),
636                 array: vec!( 1, 2, 3, 4, 5 ),
637             });
638         }
639     }
640
641     #[bench]
642     pub fn bench_noncopy(b: &mut Bencher) {
643         let arena = TypedArena::new();
644         b.iter(|| {
645             arena.alloc(Noncopy {
646                 string: "hello world".to_string(),
647                 array: vec!( 1, 2, 3, 4, 5 ),
648             })
649         })
650     }
651
652     #[bench]
653     pub fn bench_noncopy_nonarena(b: &mut Bencher) {
654         b.iter(|| {
655             let _: Box<_> = box Noncopy {
656                 string: "hello world".to_string(),
657                 array: vec!( 1, 2, 3, 4, 5 ),
658             };
659         })
660     }
661
662     #[bench]
663     pub fn bench_noncopy_old_arena(b: &mut Bencher) {
664         let arena = Arena::new();
665         b.iter(|| {
666             arena.alloc(|| Noncopy {
667                 string: "hello world".to_string(),
668                 array: vec!( 1, 2, 3, 4, 5 ),
669             })
670         })
671     }
672 }