From b71d60ad473cf4771dc845c0ce0096bfc7ce8d6d Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Fri, 2 Jul 2021 17:36:23 +0200 Subject: now i properly use dynamic memory, athough its very inefficient --- src/matrix.zig | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/matrix.zig') diff --git a/src/matrix.zig b/src/matrix.zig index eaf9685..e566bc4 100644 --- a/src/matrix.zig +++ b/src/matrix.zig @@ -1,5 +1,6 @@ const std = @import("std"); const ArrayList = std.ArrayList; +const ArenaAllocator = std.heap.ArenaAllocator; const matrix = struct { h: usize, @@ -7,11 +8,11 @@ const matrix = struct { x: ArrayList(f32), }; -pub fn new(h: usize, w: usize, x: []f32) matrix { +pub fn new(arena: *ArenaAllocator, h: usize, w: usize, x: []f32) matrix { var m = matrix{ .h = h, .w = w, - .x = ArrayList(f32).init(std.heap.page_allocator), + .x = ArrayList(f32).init(&arena.allocator), }; var i: usize = 0; @@ -37,8 +38,8 @@ pub fn dot(a: []f32, b: []f32, len: usize, step: usize) f32 { return x; } -pub fn multiply(a: matrix, b: matrix) matrix { - var x = ArrayList(f32).init(std.heap.page_allocator); +pub fn multiply(arena: *ArenaAllocator, a: matrix, b: matrix) matrix { + var x = ArrayList(f32).init(&arena.allocator); var i: usize = 0; var k: usize = 0; @@ -50,5 +51,5 @@ pub fn multiply(a: matrix, b: matrix) matrix { k += a.w; } - return new(a.h, b.w, x.items); + return new(arena, a.h, b.w, x.items); } -- cgit v1.2.3