authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-22 03:28:42-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-22 03:49:50-04:00
log1eda2ada9ac115c9dbff1bba60b3670f3fbfff57
tree4931c048abd81f009a347e0f0dbd3cdc4f67db57
parentd58233b3612fe2d6bdd00859973aabdeb8c8574b

std.math.big.Int: don't rely on the allocator when printing


2 files changed, 16 insertions(+), 12 deletions(-)

lib/std/math/big/int.zig+13-11
...@@ -143,12 +143,15 @@ pub const Int = struct {...@@ -143,12 +143,15 @@ pub const Int = struct {
143 /// Clones an Int and returns a new Int with the same value. The new Int is a deep copy and143 /// Clones an Int and returns a new Int with the same value. The new Int is a deep copy and
144 /// can be modified separately from the original.144 /// can be modified separately from the original.
145 pub fn clone(other: Int) !Int {145 pub fn clone(other: Int) !Int {
146 other.assertWritable();146 return other.clone2(other.allocator.?);
147 }
148
149 pub fn clone2(other: Int, allocator: *Allocator) !Int {
147 return Int{150 return Int{
148 .allocator = other.allocator,151 .allocator = allocator,
149 .metadata = other.metadata,152 .metadata = other.metadata,
150 .limbs = block: {153 .limbs = block: {
151 var limbs = try other.allocator.?.alloc(Limb, other.len());154 var limbs = try allocator.alloc(Limb, other.len());
152 mem.copy(Limb, limbs[0..], other.limbs[0..other.len()]);155 mem.copy(Limb, limbs[0..], other.limbs[0..other.len()]);
153 break :block limbs;156 break :block limbs;
154 },157 },
...@@ -470,8 +473,8 @@ pub const Int = struct {...@@ -470,8 +473,8 @@ pub const Int = struct {
470 break;473 break;
471 }474 }
472 }475 }
473 } // Non power-of-two: batch divisions per word size.476 } else {
474 else {477 // Non power-of-two: batch divisions per word size.
475 const digits_per_limb = math.log(Limb, base, maxInt(Limb));478 const digits_per_limb = math.log(Limb, base, maxInt(Limb));
476 var limb_base: Limb = 1;479 var limb_base: Limb = 1;
477 var j: usize = 0;480 var j: usize = 0;
...@@ -479,7 +482,7 @@ pub const Int = struct {...@@ -479,7 +482,7 @@ pub const Int = struct {
479 limb_base *= base;482 limb_base *= base;
480 }483 }
481484
482 var q = try self.clone();485 var q = try self.clone2(allocator);
483 defer q.deinit();486 defer q.deinit();
484 q.abs();487 q.abs();
485 var r = try Int.init(allocator);488 var r = try Int.init(allocator);
...@@ -522,15 +525,13 @@ pub const Int = struct {...@@ -522,15 +525,13 @@ pub const Int = struct {
522525
523 /// To allow `std.fmt.printf` to work with Int.526 /// To allow `std.fmt.printf` to work with Int.
524 /// TODO make this non-allocating527 /// TODO make this non-allocating
528 /// TODO support read-only fixed integers
525 pub fn format(529 pub fn format(
526 self: Int,530 self: Int,
527 comptime fmt: []const u8,531 comptime fmt: []const u8,
528 options: std.fmt.FormatOptions,532 options: std.fmt.FormatOptions,
529 out_stream: var,533 out_stream: var,
530 ) !void {534 ) !void {
531 self.assertWritable();
532 // TODO support read-only fixed integers
533
534 comptime var radix = 10;535 comptime var radix = 10;
535 comptime var uppercase = false;536 comptime var uppercase = false;
536537
...@@ -550,8 +551,9 @@ pub const Int = struct {...@@ -550,8 +551,9 @@ pub const Int = struct {
550 @compileError("Unknown format string: '" ++ fmt ++ "'");551 @compileError("Unknown format string: '" ++ fmt ++ "'");
551 }552 }
552553
553 const str = self.toString(self.allocator.?, radix, uppercase) catch @panic("TODO make this non allocating");554 var buf: [4096]u8 = undefined;
554 defer self.allocator.?.free(str);555 var fba = std.heap.FixedBufferAllocator.init(&buf);
556 const str = self.toString(&fba.allocator, radix, uppercase) catch @panic("TODO make this non allocating");
555 return out_stream.writeAll(str);557 return out_stream.writeAll(str);
556 }558 }
557559
src-self-hosted/ir.zig+3-1
...@@ -714,7 +714,9 @@ pub fn main() anyerror!void {...@@ -714,7 +714,9 @@ pub fn main() anyerror!void {
714 var new_zir_module = try text.emit_zir(allocator, analyzed_module);714 var new_zir_module = try text.emit_zir(allocator, analyzed_module);
715 defer new_zir_module.deinit(allocator);715 defer new_zir_module.deinit(allocator);
716716
717 new_zir_module.dump();717 var bos = std.io.bufferedOutStream(std.io.getStdOut().outStream());
718 try new_zir_module.writeToStream(allocator, bos.outStream());
719 try bos.flush();
718}720}
719721
720fn findLineColumn(source: []const u8, byte_offset: usize) struct { line: usize, column: usize } {722fn findLineColumn(source: []const u8, byte_offset: usize) struct { line: usize, column: usize } {