| ... | @@ -957,12 +957,17 @@ pub var global_allocator = mem.Allocator { | ... | @@ -957,12 +957,17 @@ pub var global_allocator = mem.Allocator { |
| 957 | var some_mem: [100 * 1024]u8 = undefined; | 957 | var some_mem: [100 * 1024]u8 = undefined; |
| 958 | var some_mem_index: usize = 0; | 958 | var some_mem_index: usize = 0; |
| 959 | | 959 | |
| | 960 | error OutOfMemory; |
| | 961 | |
| 960 | fn globalAlloc(self: &mem.Allocator, n: usize, alignment: usize) -> %[]u8 { | 962 | fn globalAlloc(self: &mem.Allocator, n: usize, alignment: usize) -> %[]u8 { |
| 961 | const addr = @ptrToInt(&some_mem[some_mem_index]); | 963 | const addr = @ptrToInt(&some_mem[some_mem_index]); |
| 962 | const rem = @rem(addr, alignment); | 964 | const rem = @rem(addr, alignment); |
| 963 | const march_forward_bytes = if (rem == 0) 0 else (alignment - rem); | 965 | const march_forward_bytes = if (rem == 0) 0 else (alignment - rem); |
| 964 | const adjusted_index = some_mem_index + march_forward_bytes; | 966 | const adjusted_index = some_mem_index + march_forward_bytes; |
| 965 | const end_index = adjusted_index + n; | 967 | const end_index = adjusted_index + n; |
| | 968 | if (end_index > some_mem.len) { |
| | 969 | return error.OutOfMemory; |
| | 970 | } |
| 966 | const result = some_mem[adjusted_index .. end_index]; | 971 | const result = some_mem[adjusted_index .. end_index]; |
| 967 | some_mem_index = end_index; | 972 | some_mem_index = end_index; |
| 968 | return result; | 973 | return result; |