@@ -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,7 +197,7 @@ 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
203
trait AllTypes { fn dummy ( & self ) { } }
@@ -224,8 +224,7 @@ impl<'longer_than_self> Arena<'longer_than_self> {
224
224
let new_min_chunk_size = cmp:: max ( n_bytes, self . chunk_size ( ) ) ;
225
225
self . chunks . borrow_mut ( ) . push ( self . copy_head . borrow ( ) . clone ( ) ) ;
226
226
227
- * self . copy_head . borrow_mut ( ) =
228
- chunk ( ( new_min_chunk_size + 1 ) . next_power_of_two ( ) , true ) ;
227
+ * self . copy_head . borrow_mut ( ) = chunk ( ( new_min_chunk_size + 1 ) . next_power_of_two ( ) , true ) ;
229
228
230
229
self . alloc_copy_inner ( n_bytes, align)
231
230
}
@@ -242,38 +241,32 @@ impl<'longer_than_self> Arena<'longer_than_self> {
242
241
let copy_head = self . copy_head . borrow ( ) ;
243
242
copy_head. fill . set ( end) ;
244
243
245
- unsafe {
246
- copy_head. as_ptr ( ) . offset ( start as isize )
247
- }
244
+ unsafe { copy_head. as_ptr ( ) . offset ( start as isize ) }
248
245
}
249
246
250
247
#[ inline]
251
248
fn alloc_copy < T , F > ( & self , op : F ) -> & mut T where F : FnOnce ( ) -> T {
252
249
unsafe {
253
- let ptr = self . alloc_copy_inner ( mem:: size_of :: < T > ( ) ,
254
- mem:: align_of :: < T > ( ) ) ;
250
+ let ptr = self . alloc_copy_inner ( mem:: size_of :: < T > ( ) , mem:: align_of :: < T > ( ) ) ;
255
251
let ptr = ptr as * mut T ;
256
252
ptr:: write ( & mut ( * ptr) , op ( ) ) ;
257
253
& mut * ptr
258
254
}
259
255
}
260
256
261
257
// 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 ) {
258
+ fn alloc_noncopy_grow ( & self , n_bytes : usize , align : usize ) -> ( * const u8 , * const u8 ) {
264
259
// Allocate a new chunk.
265
260
let new_min_chunk_size = cmp:: max ( n_bytes, self . chunk_size ( ) ) ;
266
261
self . chunks . borrow_mut ( ) . push ( self . head . borrow ( ) . clone ( ) ) ;
267
262
268
- * self . head . borrow_mut ( ) =
269
- chunk ( ( new_min_chunk_size + 1 ) . next_power_of_two ( ) , false ) ;
263
+ * self . head . borrow_mut ( ) = chunk ( ( new_min_chunk_size + 1 ) . next_power_of_two ( ) , false ) ;
270
264
271
265
self . alloc_noncopy_inner ( n_bytes, align)
272
266
}
273
267
274
268
#[ inline]
275
- fn alloc_noncopy_inner ( & self , n_bytes : usize ,
276
- align : usize ) -> ( * const u8 , * const u8 ) {
269
+ fn alloc_noncopy_inner ( & self , n_bytes : usize , align : usize ) -> ( * const u8 , * const u8 ) {
277
270
// Be careful to not maintain any `head` borrows active, because
278
271
// `alloc_noncopy_grow` borrows it mutably.
279
272
let ( start, end, tydesc_start, head_capacity) = {
@@ -297,24 +290,23 @@ impl<'longer_than_self> Arena<'longer_than_self> {
297
290
298
291
unsafe {
299
292
let buf = head. as_ptr ( ) ;
300
- ( buf. offset ( tydesc_start as isize ) , buf. offset ( start as isize ) )
293
+ ( buf. offset ( tydesc_start as isize ) ,
294
+ buf. offset ( start as isize ) )
301
295
}
302
296
}
303
297
304
298
#[ inline]
305
299
fn alloc_noncopy < T , F > ( & self , op : F ) -> & mut T where F : FnOnce ( ) -> T {
306
300
unsafe {
307
301
let tydesc = get_tydesc :: < T > ( ) ;
308
- let ( ty_ptr, ptr) =
309
- self . alloc_noncopy_inner ( mem:: size_of :: < T > ( ) ,
310
- mem:: align_of :: < T > ( ) ) ;
302
+ let ( ty_ptr, ptr) = self . alloc_noncopy_inner ( mem:: size_of :: < T > ( ) , mem:: align_of :: < T > ( ) ) ;
311
303
let ty_ptr = ty_ptr as * mut usize ;
312
304
let ptr = ptr as * mut T ;
313
305
// Write in our tydesc along with a bit indicating that it
314
306
// has *not* been initialized yet.
315
307
* ty_ptr = bitpack_tydesc_ptr ( tydesc, false ) ;
316
308
// Actually initialize it
317
- ptr:: write ( & mut ( * ptr) , op ( ) ) ;
309
+ ptr:: write ( & mut ( * ptr) , op ( ) ) ;
318
310
// Now that we are done, update the tydesc to indicate that
319
311
// the object is there.
320
312
* ty_ptr = bitpack_tydesc_ptr ( tydesc, true ) ;
@@ -358,10 +350,10 @@ fn test_arena_destructors_fail() {
358
350
for i in 0 ..10 {
359
351
// Arena allocate something with drop glue to make sure it
360
352
// doesn't leak.
361
- arena. alloc ( || { Rc :: new ( i) } ) ;
353
+ arena. alloc ( || Rc :: new ( i) ) ;
362
354
// Allocate something with funny size and alignment, to keep
363
355
// things interesting.
364
- arena. alloc ( || { [ 0u8 , 1 , 2 ] } ) ;
356
+ arena. alloc ( || [ 0u8 , 1 , 2 ] ) ;
365
357
}
366
358
// Now, panic while allocating
367
359
arena. alloc :: < Rc < i32 > , _ > ( || {
@@ -409,12 +401,13 @@ fn calculate_size<T>(capacity: usize) -> usize {
409
401
410
402
impl < T > TypedArenaChunk < T > {
411
403
#[ inline]
412
- unsafe fn new ( next : * mut TypedArenaChunk < T > , capacity : usize )
413
- -> * mut TypedArenaChunk < T > {
404
+ unsafe fn new ( next : * mut TypedArenaChunk < T > , capacity : usize ) -> * mut TypedArenaChunk < T > {
414
405
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 ( ) }
406
+ let chunk =
407
+ allocate ( size, mem:: align_of :: < TypedArenaChunk < T > > ( ) ) as * mut TypedArenaChunk < T > ;
408
+ if chunk. is_null ( ) {
409
+ alloc:: oom ( )
410
+ }
418
411
( * chunk) . next = next;
419
412
( * chunk) . capacity = capacity;
420
413
chunk
@@ -437,7 +430,8 @@ impl<T> TypedArenaChunk<T> {
437
430
let next = self . next ;
438
431
let size = calculate_size :: < T > ( self . capacity ) ;
439
432
let self_ptr: * mut TypedArenaChunk < T > = self ;
440
- deallocate ( self_ptr as * mut u8 , size,
433
+ deallocate ( self_ptr as * mut u8 ,
434
+ size,
441
435
mem:: align_of :: < TypedArenaChunk < T > > ( ) ) ;
442
436
if !next. is_null ( ) {
443
437
let capacity = ( * next) . capacity ;
@@ -449,9 +443,7 @@ impl<T> TypedArenaChunk<T> {
449
443
#[ inline]
450
444
fn start ( & self ) -> * const u8 {
451
445
let this: * const TypedArenaChunk < T > = self ;
452
- unsafe {
453
- round_up ( this. offset ( 1 ) as usize , mem:: align_of :: < T > ( ) ) as * const u8
454
- }
446
+ unsafe { round_up ( this. offset ( 1 ) as usize , mem:: align_of :: < T > ( ) ) as * const u8 }
455
447
}
456
448
457
449
// Returns a pointer to the end of the allocated space.
@@ -545,22 +537,29 @@ mod tests {
545
537
546
538
#[ test]
547
539
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 > ) }
540
+ struct Inner {
541
+ value : u8 ,
542
+ }
543
+ struct Outer < ' a > {
544
+ inner : & ' a Inner ,
545
+ }
546
+ enum EI < ' e > {
547
+ I ( Inner ) ,
548
+ O ( Outer < ' e > ) ,
549
+ }
551
550
552
551
struct Wrap < ' a > ( TypedArena < EI < ' a > > ) ;
553
552
554
553
impl < ' a > Wrap < ' a > {
555
- fn alloc_inner < F : Fn ( ) -> Inner > ( & self , f : F ) -> & Inner {
554
+ fn alloc_inner < F : Fn ( ) -> Inner > ( & self , f : F ) -> & Inner {
556
555
let r: & EI = self . 0 . alloc ( EI :: I ( f ( ) ) ) ;
557
556
if let & EI :: I ( ref i) = r {
558
557
i
559
558
} else {
560
559
panic ! ( "mismatch" ) ;
561
560
}
562
561
}
563
- fn alloc_outer < F : Fn ( ) -> Outer < ' a > > ( & self , f : F ) -> & Outer {
562
+ fn alloc_outer < F : Fn ( ) -> Outer < ' a > > ( & self , f : F ) -> & Outer {
564
563
let r: & EI = self . 0 . alloc ( EI :: O ( f ( ) ) ) ;
565
564
if let & EI :: O ( ref o) = r {
566
565
o
@@ -572,8 +571,9 @@ mod tests {
572
571
573
572
let arena = Wrap ( TypedArena :: new ( ) ) ;
574
573
575
- let result = arena. alloc_outer ( || Outer {
576
- inner : arena. alloc_inner ( || Inner { value : 10 } ) } ) ;
574
+ let result = arena. alloc_outer ( || {
575
+ Outer { inner : arena. alloc_inner ( || Inner { value : 10 } ) }
576
+ } ) ;
577
577
578
578
assert_eq ! ( result. inner. value, 10 ) ;
579
579
}
@@ -582,49 +582,27 @@ mod tests {
582
582
pub fn test_copy ( ) {
583
583
let arena = TypedArena :: new ( ) ;
584
584
for _ in 0 ..100000 {
585
- arena. alloc ( Point {
586
- x : 1 ,
587
- y : 2 ,
588
- z : 3 ,
589
- } ) ;
585
+ arena. alloc ( Point { x : 1 , y : 2 , z : 3 } ) ;
590
586
}
591
587
}
592
588
593
589
#[ bench]
594
590
pub fn bench_copy ( b : & mut Bencher ) {
595
591
let arena = TypedArena :: new ( ) ;
596
- b. iter ( || {
597
- arena. alloc ( Point {
598
- x : 1 ,
599
- y : 2 ,
600
- z : 3 ,
601
- } )
602
- } )
592
+ b. iter ( || arena. alloc ( Point { x : 1 , y : 2 , z : 3 } ) )
603
593
}
604
594
605
595
#[ bench]
606
596
pub fn bench_copy_nonarena ( b : & mut Bencher ) {
607
597
b. iter ( || {
608
- let _: Box < _ > = box Point {
609
- x : 1 ,
610
- y : 2 ,
611
- z : 3 ,
612
- } ;
598
+ let _: Box < _ > = box Point { x : 1 , y : 2 , z : 3 } ;
613
599
} )
614
600
}
615
601
616
602
#[ bench]
617
603
pub fn bench_copy_old_arena ( b : & mut Bencher ) {
618
604
let arena = Arena :: new ( ) ;
619
- b. iter ( || {
620
- arena. alloc ( || {
621
- Point {
622
- x : 1 ,
623
- y : 2 ,
624
- z : 3 ,
625
- }
626
- } )
627
- } )
605
+ b. iter ( || arena. alloc ( || Point { x : 1 , y : 2 , z : 3 } ) )
628
606
}
629
607
630
608
#[ allow( dead_code) ]
@@ -639,7 +617,7 @@ mod tests {
639
617
for _ in 0 ..100000 {
640
618
arena. alloc ( Noncopy {
641
619
string : "hello world" . to_string ( ) ,
642
- array : vec ! ( 1 , 2 , 3 , 4 , 5 ) ,
620
+ array : vec ! ( 1 , 2 , 3 , 4 , 5 ) ,
643
621
} ) ;
644
622
}
645
623
}
@@ -650,7 +628,7 @@ mod tests {
650
628
b. iter ( || {
651
629
arena. alloc ( Noncopy {
652
630
string : "hello world" . to_string ( ) ,
653
- array : vec ! ( 1 , 2 , 3 , 4 , 5 ) ,
631
+ array : vec ! ( 1 , 2 , 3 , 4 , 5 ) ,
654
632
} )
655
633
} )
656
634
}
@@ -660,7 +638,7 @@ mod tests {
660
638
b. iter ( || {
661
639
let _: Box < _ > = box Noncopy {
662
640
string : "hello world" . to_string ( ) ,
663
- array : vec ! ( 1 , 2 , 3 , 4 , 5 ) ,
641
+ array : vec ! ( 1 , 2 , 3 , 4 , 5 ) ,
664
642
} ;
665
643
} )
666
644
}
@@ -669,9 +647,11 @@ mod tests {
669
647
pub fn bench_noncopy_old_arena ( b : & mut Bencher ) {
670
648
let arena = Arena :: new ( ) ;
671
649
b. iter ( || {
672
- arena. alloc ( || Noncopy {
673
- string : "hello world" . to_string ( ) ,
674
- array : vec ! ( 1 , 2 , 3 , 4 , 5 ) ,
650
+ arena. alloc ( || {
651
+ Noncopy {
652
+ string : "hello world" . to_string ( ) ,
653
+ array : vec ! ( 1 , 2 , 3 , 4 , 5 ) ,
654
+ }
675
655
} )
676
656
} )
677
657
}
0 commit comments