@@ -105,7 +105,7 @@ pub struct Arena<'longer_than_self> {
105
105
head : RefCell < Chunk > ,
106
106
copy_head : RefCell < Chunk > ,
107
107
chunks : RefCell < Vec < Chunk > > ,
108
- _marker : marker:: PhantomData < * mut & ' longer_than_self ( ) > ,
108
+ _marker : marker:: PhantomData < * mut & ' longer_than_self ( ) > ,
109
109
}
110
110
111
111
impl < ' a > Arena < ' a > {
@@ -197,10 +197,11 @@ fn un_bitpack_tydesc_ptr(p: usize) -> (*const TyDesc, bool) {
197
197
struct TyDesc {
198
198
drop_glue : fn ( * const i8 ) ,
199
199
size : usize ,
200
- align : usize
200
+ align : usize ,
201
201
}
202
202
203
- trait AllTypes { fn dummy ( & self ) { } }
203
+ trait AllTypes { fn dummy ( & self ) {
204
+ } }
204
205
impl < T : ?Sized > AllTypes for T { }
205
206
206
207
unsafe fn get_tydesc < T > ( ) -> * const TyDesc {
@@ -224,8 +225,7 @@ impl<'longer_than_self> Arena<'longer_than_self> {
224
225
let new_min_chunk_size = cmp:: max ( n_bytes, self . chunk_size ( ) ) ;
225
226
self . chunks . borrow_mut ( ) . push ( self . copy_head . borrow ( ) . clone ( ) ) ;
226
227
227
- * self . copy_head . borrow_mut ( ) =
228
- chunk ( ( new_min_chunk_size + 1 ) . next_power_of_two ( ) , true ) ;
228
+ * self . copy_head . borrow_mut ( ) = chunk ( ( new_min_chunk_size + 1 ) . next_power_of_two ( ) , true ) ;
229
229
230
230
self . alloc_copy_inner ( n_bytes, align)
231
231
}
@@ -242,38 +242,34 @@ impl<'longer_than_self> Arena<'longer_than_self> {
242
242
let copy_head = self . copy_head . borrow ( ) ;
243
243
copy_head. fill . set ( end) ;
244
244
245
- unsafe {
246
- copy_head. as_ptr ( ) . offset ( start as isize )
247
- }
245
+ unsafe { copy_head. as_ptr ( ) . offset ( start as isize ) }
248
246
}
249
247
250
248
#[ inline]
251
- fn alloc_copy < T , F > ( & self , op : F ) -> & mut T where F : FnOnce ( ) -> T {
249
+ fn alloc_copy < T , F > ( & self , op : F ) -> & mut T
250
+ where F : FnOnce ( ) -> T
251
+ {
252
252
unsafe {
253
- let ptr = self . alloc_copy_inner ( mem:: size_of :: < T > ( ) ,
254
- mem:: align_of :: < T > ( ) ) ;
253
+ let ptr = self . alloc_copy_inner ( mem:: size_of :: < T > ( ) , mem:: align_of :: < T > ( ) ) ;
255
254
let ptr = ptr as * mut T ;
256
255
ptr:: write ( & mut ( * ptr) , op ( ) ) ;
257
256
& mut * ptr
258
257
}
259
258
}
260
259
261
260
// Functions for the non-POD part of the arena
262
- fn alloc_noncopy_grow ( & self , n_bytes : usize ,
263
- align : usize ) -> ( * const u8 , * const u8 ) {
261
+ fn alloc_noncopy_grow ( & self , n_bytes : usize , align : usize ) -> ( * const u8 , * const u8 ) {
264
262
// Allocate a new chunk.
265
263
let new_min_chunk_size = cmp:: max ( n_bytes, self . chunk_size ( ) ) ;
266
264
self . chunks . borrow_mut ( ) . push ( self . head . borrow ( ) . clone ( ) ) ;
267
265
268
- * self . head . borrow_mut ( ) =
269
- chunk ( ( new_min_chunk_size + 1 ) . next_power_of_two ( ) , false ) ;
266
+ * self . head . borrow_mut ( ) = chunk ( ( new_min_chunk_size + 1 ) . next_power_of_two ( ) , false ) ;
270
267
271
268
self . alloc_noncopy_inner ( n_bytes, align)
272
269
}
273
270
274
271
#[ inline]
275
- fn alloc_noncopy_inner ( & self , n_bytes : usize ,
276
- align : usize ) -> ( * const u8 , * const u8 ) {
272
+ fn alloc_noncopy_inner ( & self , n_bytes : usize , align : usize ) -> ( * const u8 , * const u8 ) {
277
273
// Be careful to not maintain any `head` borrows active, because
278
274
// `alloc_noncopy_grow` borrows it mutably.
279
275
let ( start, end, tydesc_start, head_capacity) = {
@@ -297,24 +293,25 @@ impl<'longer_than_self> Arena<'longer_than_self> {
297
293
298
294
unsafe {
299
295
let buf = head. as_ptr ( ) ;
300
- ( buf. offset ( tydesc_start as isize ) , buf. offset ( start as isize ) )
296
+ ( buf. offset ( tydesc_start as isize ) ,
297
+ buf. offset ( start as isize ) )
301
298
}
302
299
}
303
300
304
301
#[ inline]
305
- fn alloc_noncopy < T , F > ( & self , op : F ) -> & mut T where F : FnOnce ( ) -> T {
302
+ fn alloc_noncopy < T , F > ( & self , op : F ) -> & mut T
303
+ where F : FnOnce ( ) -> T
304
+ {
306
305
unsafe {
307
306
let tydesc = get_tydesc :: < T > ( ) ;
308
- let ( ty_ptr, ptr) =
309
- self . alloc_noncopy_inner ( mem:: size_of :: < T > ( ) ,
310
- mem:: align_of :: < T > ( ) ) ;
307
+ let ( ty_ptr, ptr) = self . alloc_noncopy_inner ( mem:: size_of :: < T > ( ) , mem:: align_of :: < T > ( ) ) ;
311
308
let ty_ptr = ty_ptr as * mut usize ;
312
309
let ptr = ptr as * mut T ;
313
310
// Write in our tydesc along with a bit indicating that it
314
311
// has *not* been initialized yet.
315
312
* ty_ptr = bitpack_tydesc_ptr ( tydesc, false ) ;
316
313
// Actually initialize it
317
- ptr:: write ( & mut ( * ptr) , op ( ) ) ;
314
+ ptr:: write ( & mut ( * ptr) , op ( ) ) ;
318
315
// Now that we are done, update the tydesc to indicate that
319
316
// the object is there.
320
317
* ty_ptr = bitpack_tydesc_ptr ( tydesc, true ) ;
@@ -326,7 +323,9 @@ impl<'longer_than_self> Arena<'longer_than_self> {
326
323
/// Allocates a new item in the arena, using `op` to initialize the value,
327
324
/// and returns a reference to it.
328
325
#[ inline]
329
- pub fn alloc < T : ' longer_than_self , F > ( & self , op : F ) -> & mut T where F : FnOnce ( ) -> T {
326
+ pub fn alloc < T : ' longer_than_self , F > ( & self , op : F ) -> & mut T
327
+ where F : FnOnce ( ) -> T
328
+ {
330
329
unsafe {
331
330
if intrinsics:: needs_drop :: < T > ( ) {
332
331
self . alloc_noncopy ( op)
@@ -358,10 +357,10 @@ fn test_arena_destructors_fail() {
358
357
for i in 0 ..10 {
359
358
// Arena allocate something with drop glue to make sure it
360
359
// doesn't leak.
361
- arena. alloc ( || { Rc :: new ( i) } ) ;
360
+ arena. alloc ( || Rc :: new ( i) ) ;
362
361
// Allocate something with funny size and alignment, to keep
363
362
// things interesting.
364
- arena. alloc ( || { [ 0u8 , 1 , 2 ] } ) ;
363
+ arena. alloc ( || [ 0u8 , 1 , 2 ] ) ;
365
364
}
366
365
// Now, panic while allocating
367
366
arena. alloc :: < Rc < i32 > , _ > ( || {
@@ -393,9 +392,7 @@ struct TypedArenaChunk<T> {
393
392
next : * mut TypedArenaChunk < T > ,
394
393
395
394
/// The number of elements that this chunk can hold.
396
- capacity : usize ,
397
-
398
- // Objects follow here, suitably aligned.
395
+ capacity : usize , // Objects follow here, suitably aligned.
399
396
}
400
397
401
398
fn calculate_size < T > ( capacity : usize ) -> usize {
@@ -409,12 +406,13 @@ fn calculate_size<T>(capacity: usize) -> usize {
409
406
410
407
impl < T > TypedArenaChunk < T > {
411
408
#[ inline]
412
- unsafe fn new ( next : * mut TypedArenaChunk < T > , capacity : usize )
413
- -> * mut TypedArenaChunk < T > {
409
+ unsafe fn new ( next : * mut TypedArenaChunk < T > , capacity : usize ) -> * mut TypedArenaChunk < T > {
414
410
let size = calculate_size :: < T > ( capacity) ;
415
- let chunk = allocate ( size, mem:: align_of :: < TypedArenaChunk < T > > ( ) )
416
- as * mut TypedArenaChunk < T > ;
417
- if chunk. is_null ( ) { alloc:: oom ( ) }
411
+ let chunk =
412
+ allocate ( size, mem:: align_of :: < TypedArenaChunk < T > > ( ) ) as * mut TypedArenaChunk < T > ;
413
+ if chunk. is_null ( ) {
414
+ alloc:: oom ( )
415
+ }
418
416
( * chunk) . next = next;
419
417
( * chunk) . capacity = capacity;
420
418
chunk
@@ -437,7 +435,8 @@ impl<T> TypedArenaChunk<T> {
437
435
let next = self . next ;
438
436
let size = calculate_size :: < T > ( self . capacity ) ;
439
437
let self_ptr: * mut TypedArenaChunk < T > = self ;
440
- deallocate ( self_ptr as * mut u8 , size,
438
+ deallocate ( self_ptr as * mut u8 ,
439
+ size,
441
440
mem:: align_of :: < TypedArenaChunk < T > > ( ) ) ;
442
441
if !next. is_null ( ) {
443
442
let capacity = ( * next) . capacity ;
@@ -449,9 +448,7 @@ impl<T> TypedArenaChunk<T> {
449
448
#[ inline]
450
449
fn start ( & self ) -> * const u8 {
451
450
let this: * const TypedArenaChunk < T > = self ;
452
- unsafe {
453
- round_up ( this. offset ( 1 ) as usize , mem:: align_of :: < T > ( ) ) as * const u8
454
- }
451
+ unsafe { round_up ( this. offset ( 1 ) as usize , mem:: align_of :: < T > ( ) ) as * const u8 }
455
452
}
456
453
457
454
// Returns a pointer to the end of the allocated space.
@@ -545,22 +542,29 @@ mod tests {
545
542
546
543
#[ test]
547
544
fn test_arena_alloc_nested ( ) {
548
- struct Inner { value : u8 }
549
- struct Outer < ' a > { inner : & ' a Inner }
550
- enum EI < ' e > { I ( Inner ) , O ( Outer < ' e > ) }
545
+ struct Inner {
546
+ value : u8 ,
547
+ }
548
+ struct Outer < ' a > {
549
+ inner : & ' a Inner ,
550
+ }
551
+ enum EI < ' e > {
552
+ I ( Inner ) ,
553
+ O ( Outer < ' e > ) ,
554
+ }
551
555
552
556
struct Wrap < ' a > ( TypedArena < EI < ' a > > ) ;
553
557
554
558
impl < ' a > Wrap < ' a > {
555
- fn alloc_inner < F : Fn ( ) -> Inner > ( & self , f : F ) -> & Inner {
559
+ fn alloc_inner < F : Fn ( ) -> Inner > ( & self , f : F ) -> & Inner {
556
560
let r: & EI = self . 0 . alloc ( EI :: I ( f ( ) ) ) ;
557
561
if let & EI :: I ( ref i) = r {
558
562
i
559
563
} else {
560
564
panic ! ( "mismatch" ) ;
561
565
}
562
566
}
563
- fn alloc_outer < F : Fn ( ) -> Outer < ' a > > ( & self , f : F ) -> & Outer {
567
+ fn alloc_outer < F : Fn ( ) -> Outer < ' a > > ( & self , f : F ) -> & Outer {
564
568
let r: & EI = self . 0 . alloc ( EI :: O ( f ( ) ) ) ;
565
569
if let & EI :: O ( ref o) = r {
566
570
o
@@ -572,8 +576,9 @@ mod tests {
572
576
573
577
let arena = Wrap ( TypedArena :: new ( ) ) ;
574
578
575
- let result = arena. alloc_outer ( || Outer {
576
- inner : arena. alloc_inner ( || Inner { value : 10 } ) } ) ;
579
+ let result = arena. alloc_outer ( || {
580
+ Outer { inner : arena. alloc_inner ( || Inner { value : 10 } ) }
581
+ } ) ;
577
582
578
583
assert_eq ! ( result. inner. value, 10 ) ;
579
584
}
@@ -582,49 +587,27 @@ mod tests {
582
587
pub fn test_copy ( ) {
583
588
let arena = TypedArena :: new ( ) ;
584
589
for _ in 0 ..100000 {
585
- arena. alloc ( Point {
586
- x : 1 ,
587
- y : 2 ,
588
- z : 3 ,
589
- } ) ;
590
+ arena. alloc ( Point { x : 1 , y : 2 , z : 3 } ) ;
590
591
}
591
592
}
592
593
593
594
#[ bench]
594
595
pub fn bench_copy ( b : & mut Bencher ) {
595
596
let arena = TypedArena :: new ( ) ;
596
- b. iter ( || {
597
- arena. alloc ( Point {
598
- x : 1 ,
599
- y : 2 ,
600
- z : 3 ,
601
- } )
602
- } )
597
+ b. iter ( || arena. alloc ( Point { x : 1 , y : 2 , z : 3 } ) )
603
598
}
604
599
605
600
#[ bench]
606
601
pub fn bench_copy_nonarena ( b : & mut Bencher ) {
607
602
b. iter ( || {
608
- let _: Box < _ > = box Point {
609
- x : 1 ,
610
- y : 2 ,
611
- z : 3 ,
612
- } ;
603
+ let _: Box < _ > = box Point { x : 1 , y : 2 , z : 3 } ;
613
604
} )
614
605
}
615
606
616
607
#[ bench]
617
608
pub fn bench_copy_old_arena ( b : & mut Bencher ) {
618
609
let arena = Arena :: new ( ) ;
619
- b. iter ( || {
620
- arena. alloc ( || {
621
- Point {
622
- x : 1 ,
623
- y : 2 ,
624
- z : 3 ,
625
- }
626
- } )
627
- } )
610
+ b. iter ( || arena. alloc ( || Point { x : 1 , y : 2 , z : 3 } ) )
628
611
}
629
612
630
613
#[ allow( dead_code) ]
@@ -639,7 +622,7 @@ mod tests {
639
622
for _ in 0 ..100000 {
640
623
arena. alloc ( Noncopy {
641
624
string : "hello world" . to_string ( ) ,
642
- array : vec ! ( 1 , 2 , 3 , 4 , 5 ) ,
625
+ array : vec ! ( 1 , 2 , 3 , 4 , 5 ) ,
643
626
} ) ;
644
627
}
645
628
}
@@ -650,7 +633,7 @@ mod tests {
650
633
b. iter ( || {
651
634
arena. alloc ( Noncopy {
652
635
string : "hello world" . to_string ( ) ,
653
- array : vec ! ( 1 , 2 , 3 , 4 , 5 ) ,
636
+ array : vec ! ( 1 , 2 , 3 , 4 , 5 ) ,
654
637
} )
655
638
} )
656
639
}
@@ -660,7 +643,7 @@ mod tests {
660
643
b. iter ( || {
661
644
let _: Box < _ > = box Noncopy {
662
645
string : "hello world" . to_string ( ) ,
663
- array : vec ! ( 1 , 2 , 3 , 4 , 5 ) ,
646
+ array : vec ! ( 1 , 2 , 3 , 4 , 5 ) ,
664
647
} ;
665
648
} )
666
649
}
@@ -669,9 +652,11 @@ mod tests {
669
652
pub fn bench_noncopy_old_arena ( b : & mut Bencher ) {
670
653
let arena = Arena :: new ( ) ;
671
654
b. iter ( || {
672
- arena. alloc ( || Noncopy {
673
- string : "hello world" . to_string ( ) ,
674
- array : vec ! ( 1 , 2 , 3 , 4 , 5 ) ,
655
+ arena. alloc ( || {
656
+ Noncopy {
657
+ string : "hello world" . to_string ( ) ,
658
+ array : vec ! ( 1 , 2 , 3 , 4 , 5 ) ,
659
+ }
675
660
} )
676
661
} )
677
662
}
0 commit comments