File tree Expand file tree Collapse file tree 2 files changed +22
-6
lines changed Expand file tree Collapse file tree 2 files changed +22
-6
lines changed Original file line number Diff line number Diff line change @@ -20,19 +20,26 @@ fn arena() -> arena {
20
20
}
21
21
22
22
impl arena for arena {
23
- unsafe fn alloc < T > ( n_bytes : uint ) -> & self . T {
23
+ fn alloc ( n_bytes : uint , align : uint ) -> * ( ) {
24
+ let alignm1 = align - 1 u;
24
25
let mut head = list:: head ( self . chunks ) ;
25
- if head. fill + n_bytes > vec:: len ( head. data ) {
26
+
27
+ let mut start = head. fill ;
28
+ start = ( start + alignm1) & !alignm1;
29
+ let mut end = start + n_bytes;
30
+
31
+ if end > vec:: len ( head. data ) {
26
32
// Allocate a new chunk.
27
33
let new_min_chunk_size = uint:: max ( n_bytes, vec:: len ( head. data ) ) ;
28
34
head = chunk ( uint:: next_power_of_two ( new_min_chunk_size) ) ;
29
35
self . chunks = list:: cons ( head, @self . chunks ) ;
36
+ start = 0 u;
37
+ end = n_bytes;
30
38
}
31
39
32
- let start = vec:: unsafe:: to_ptr ( head. data ) ;
33
- let p = ptr:: offset ( start, head. fill ) ;
34
- head. fill += n_bytes;
35
- ret unsafe:: reinterpret_cast ( p) ;
40
+ let p = ptr:: offset ( ptr:: addr_of ( head. fill ) , start) ;
41
+ head. fill = end;
42
+ unsafe { ret unsafe:: reinterpret_cast ( p) ; }
36
43
}
37
44
}
38
45
Original file line number Diff line number Diff line change
1
+ use std;
2
+ import std:: arena:: arena;
3
+
4
+ fn main ( ) {
5
+ let p = & arena ( ) ;
6
+ let x = new ( * p) 4 u;
7
+ io:: print ( #fmt[ "%u" , * x] ) ;
8
+ assert * x == 4 u;
9
+ }
You can’t perform that action at this time.
0 commit comments