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 {
143143 /// Clones an Int and returns a new Int with the same value. The new Int is a deep copy and
144144 /// can be modified separately from the original.
145145 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 {
147150 return Int{
148 .allocator = other.allocator,
151 .allocator = allocator,
149152 .metadata = other.metadata,
150153 .limbs = block: {
151 var limbs = try other.allocator.?.alloc(Limb, other.len());
154 var limbs = try allocator.alloc(Limb, other.len());
152155 mem.copy(Limb, limbs[0..], other.limbs[0..other.len()]);
153156 break :block limbs;
154157 },
......@@ -470,8 +473,8 @@ pub const Int = struct {
470473 break;
471474 }
472475 }
473 } // Non power-of-two: batch divisions per word size.
474 else {
476 } else {
477 // Non power-of-two: batch divisions per word size.
475478 const digits_per_limb = math.log(Limb, base, maxInt(Limb));
476479 var limb_base: Limb = 1;
477480 var j: usize = 0;
......@@ -479,7 +482,7 @@ pub const Int = struct {
479482 limb_base *= base;
480483 }
481484
482 var q = try self.clone();
485 var q = try self.clone2(allocator);
483486 defer q.deinit();
484487 q.abs();
485488 var r = try Int.init(allocator);
......@@ -522,15 +525,13 @@ pub const Int = struct {
522525
523526 /// To allow `std.fmt.printf` to work with Int.
524527 /// TODO make this non-allocating
528 /// TODO support read-only fixed integers
525529 pub fn format(
526530 self: Int,
527531 comptime fmt: []const u8,
528532 options: std.fmt.FormatOptions,
529533 out_stream: var,
530534 ) !void {
531 self.assertWritable();
532 // TODO support read-only fixed integers
533
534535 comptime var radix = 10;
535536 comptime var uppercase = false;
536537
......@@ -550,8 +551,9 @@ pub const Int = struct {
550551 @compileError("Unknown format string: '" ++ fmt ++ "'");
551552 }
552553
553 const str = self.toString(self.allocator.?, radix, uppercase) catch @panic("TODO make this non allocating");
554 defer self.allocator.?.free(str);
554 var buf: [4096]u8 = undefined;
555 var fba = std.heap.FixedBufferAllocator.init(&buf);
556 const str = self.toString(&fba.allocator, radix, uppercase) catch @panic("TODO make this non allocating");
555557 return out_stream.writeAll(str);
556558 }
557559
src-self-hosted/ir.zig+3-1
......@@ -714,7 +714,9 @@ pub fn main() anyerror!void {
714714 var new_zir_module = try text.emit_zir(allocator, analyzed_module);
715715 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();
718720}
719721
720722fn findLineColumn(source: []const u8, byte_offset: usize) struct { line: usize, column: usize } {