From 95d7d5fa30a82cd2e2415c07fc324e17867e0b18 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Fri, 27 Oct 2023 12:45:42 +0200 Subject: [PATCH] Use `Allocator` instead of `*const Allocator`. (#57) Use Allocator instead of *const Allocator Co-authored-by: Abdel @ StarkWare <45264458+abdelhamidbakhta@users.noreply.github.com> --- src/cmd/cmd.zig | 2 +- src/vm/builtins/bitwise/bitwise.zig | 32 ++++++++------------- src/vm/core.zig | 44 ++++++++++++++--------------- src/vm/memory/memory.zig | 12 ++++---- src/vm/memory/segments.zig | 12 ++++---- src/vm/run_context.zig | 8 +++--- 6 files changed, 51 insertions(+), 59 deletions(-) diff --git a/src/cmd/cmd.zig b/src/cmd/cmd.zig index 6812f12b..4c4b59cf 100644 --- a/src/cmd/cmd.zig +++ b/src/cmd/cmd.zig @@ -105,7 +105,7 @@ fn execute(_: []const []const u8) !void { // Create a new VM instance. var vm = try vm_core.CairoVM.init( - &gpa_allocator, + gpa_allocator, config, ); defer vm.deinit(); // <-- This ensures that resources are freed when exiting the scope diff --git a/src/vm/builtins/bitwise/bitwise.zig b/src/vm/builtins/bitwise/bitwise.zig index 7fd667f5..b87e5268 100644 --- a/src/vm/builtins/bitwise/bitwise.zig +++ b/src/vm/builtins/bitwise/bitwise.zig @@ -25,7 +25,6 @@ const CELLS_PER_BITWISE: u64 = 5; const BITWISE_TOTAL_N_BITS = 251; const BITWISE_INPUT_CELLS_PER_INSTANCE = 2; - /// Retrieve the felt in memory that an address denotes as an integer. /// # Arguments /// - address: The address belonging to the Bitwise builtin's segment @@ -44,14 +43,14 @@ fn getValue(address: Relocatable, memory: *Memory) Error!u256 { if (felt.toInteger() > std.math.pow(u256, 2, BITWISE_TOTAL_N_BITS)) { return Error.UnsupportedNumberOfBits; } - + return felt.toInteger(); } /// Compute the auto-deduction rule for Bitwise /// # Arguments /// - address: The address belonging to the Bitwise builtin's segment -/// - memory: The cairo memory where addresses are looked up +/// - memory: The cairo memory where addresses are looked up /// # Returns /// The deduced value as a `MaybeRelocatable` pub fn deduce(address: Relocatable, memory: *Memory) Error!MaybeRelocatable { @@ -86,12 +85,11 @@ pub fn deduce(address: Relocatable, memory: *Memory) Error!MaybeRelocatable { const expectEqual = std.testing.expectEqual; const expectError = std.testing.expectError; - test "deduce when address.offset less than BITWISE_INPUT_CELLS_PER_INSTANCE" { // given var allocator = std.testing.allocator; - var mem = try Memory.init(&allocator); + var mem = try Memory.init(allocator); defer mem.deinit(); // when @@ -99,59 +97,53 @@ test "deduce when address.offset less than BITWISE_INPUT_CELLS_PER_INSTANCE" { // then try expectError(Error.InvalidBitwiseIndex, deduce(address, mem)); - } test "deduce when address points to nothing in memory" { // given var allocator = std.testing.allocator; - var mem = try Memory.init(&allocator); + var mem = try Memory.init(allocator); defer mem.deinit(); // when - var address = Relocatable.new(0,3); + var address = Relocatable.new(0, 3); // then try expectError(Error.InvalidAddressForBitwise, deduce(address, mem)); - } test "deduce when address points to relocatable variant of MaybeRelocatable " { // given var allocator = std.testing.allocator; - var mem = try Memory.init(&allocator); + var mem = try Memory.init(allocator); defer mem.deinit(); // when - var address = Relocatable.new(0,3); + var address = Relocatable.new(0, 3); try mem.set(Relocatable.new(0, 5), newFromRelocatable(address)); // then try expectError(Error.InvalidAddressForBitwise, deduce(address, mem)); - } - test "deduce when address points to felt greater than BITWISE_TOTAL_N_BITS" { // given const number = std.math.pow(u256, 2, BITWISE_TOTAL_N_BITS) + 1; var allocator = std.testing.allocator; - var mem = try Memory.init(&allocator); + var mem = try Memory.init(allocator); defer mem.deinit(); // when - var address = Relocatable.new(0,3); + var address = Relocatable.new(0, 3); try mem.set(Relocatable.new(0, 0), fromFelt(Felt252.fromInteger(number))); // then try expectError(Error.UnsupportedNumberOfBits, deduce(address, mem)); - - } // happy path tests graciously ported from https://github.com/lambdaclass/cairo-vm_in_go/blob/main/pkg/builtins/bitwise_test.go#L13 @@ -159,7 +151,7 @@ test "valid bitwise and" { // given var allocator = std.testing.allocator; - var mem = try Memory.init(&allocator); + var mem = try Memory.init(allocator); defer mem.deinit(); // when @@ -179,7 +171,7 @@ test "valid bitwise xor" { // given var allocator = std.testing.allocator; - var mem = try Memory.init(&allocator); + var mem = try Memory.init(allocator); defer mem.deinit(); // when @@ -199,7 +191,7 @@ test "valid bitwise or" { // given var allocator = std.testing.allocator; - var mem = try Memory.init(&allocator); + var mem = try Memory.init(allocator); defer mem.deinit(); // when diff --git a/src/vm/core.zig b/src/vm/core.zig index 4857a8b0..3699dc56 100644 --- a/src/vm/core.zig +++ b/src/vm/core.zig @@ -22,7 +22,7 @@ pub const CairoVM = struct { // ************************************************************ /// The memory allocator. Can be needed for the deallocation of the VM resources. - allocator: *const Allocator, + allocator: Allocator, /// The run context. run_context: *RunContext, /// The memory segment manager. @@ -45,7 +45,7 @@ pub const CairoVM = struct { /// # Errors /// - If a memory allocation fails. pub fn init( - allocator: *const Allocator, + allocator: Allocator, config: Config, ) !CairoVM { // Initialize the memory segment manager. @@ -53,7 +53,7 @@ pub const CairoVM = struct { // Initialize the run context. const run_context = try RunContext.init(allocator); // Initialize the trace context. - const trace_context = try TraceContext.init(allocator.*, config.enable_trace); + const trace_context = try TraceContext.init(allocator, config.enable_trace); return CairoVM{ .allocator = allocator, @@ -429,7 +429,7 @@ test "update pc regular no imm" { instruction.op_1_addr = instructions.Op1Src.AP; const operands = OperandsResult.default(); // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -462,7 +462,7 @@ test "update pc regular with imm" { instruction.op_1_addr = instructions.Op1Src.Imm; const operands = OperandsResult.default(); // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -495,7 +495,7 @@ test "update pc jump with operands res null" { var operands = OperandsResult.default(); operands.res = null; // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -519,7 +519,7 @@ test "update pc jump with operands res not relocatable" { var operands = OperandsResult.default(); operands.res = relocatable.fromU64(0); // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -546,7 +546,7 @@ test "update pc jump with operands res relocatable" { 42, )); // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -579,7 +579,7 @@ test "update pc jump rel with operands res null" { var operands = OperandsResult.default(); operands.res = null; // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -606,7 +606,7 @@ test "update pc jump rel with operands res not felt" { 42, )); // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -630,7 +630,7 @@ test "update pc jump rel with operands res felt" { var operands = OperandsResult.default(); operands.res = relocatable.fromU64(42); // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -663,7 +663,7 @@ test "update pc update jnz with operands dst zero" { var operands = OperandsResult.default(); operands.dst = relocatable.fromU64(0); // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -700,7 +700,7 @@ test "update pc update jnz with operands dst not zero op1 not felt" { 42, )); // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -728,7 +728,7 @@ test "update pc update jnz with operands dst not zero op1 felt" { operands.dst = relocatable.fromU64(1); operands.op_1 = relocatable.fromU64(42); // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -760,7 +760,7 @@ test "update ap add with operands res unconstrained" { var operands = OperandsResult.default(); operands.res = null; // Simulate unconstrained res // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -782,7 +782,7 @@ test "update ap add1" { instruction.ap_update = instructions.ApUpdate.Add1; var operands = OperandsResult.default(); // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -814,7 +814,7 @@ test "update ap add2" { instruction.ap_update = instructions.ApUpdate.Add2; var operands = OperandsResult.default(); // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -846,7 +846,7 @@ test "update fp appplus2" { instruction.fp_update = instructions.FpUpdate.APPlus2; var operands = OperandsResult.default(); // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -882,7 +882,7 @@ test "update fp dst relocatable" { 42, )); // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -915,7 +915,7 @@ test "update fp dst felt" { var operands = OperandsResult.default(); operands.dst = relocatable.fromU64(42); // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ @@ -948,7 +948,7 @@ test "trace is enabled" { var config = Config{ .proof_mode = false, .enable_trace = true }; var vm = try CairoVM.init( - &allocator, + allocator, config, ); defer vm.deinit(); @@ -975,7 +975,7 @@ test "trace is disabled" { var allocator = std.testing.allocator; // Create a new VM instance. - var vm = try CairoVM.init(&allocator, .{}); + var vm = try CairoVM.init(allocator, .{}); defer vm.deinit(); // ************************************************************ diff --git a/src/vm/memory/memory.zig b/src/vm/memory/memory.zig index 94d099c8..70a82fdf 100644 --- a/src/vm/memory/memory.zig +++ b/src/vm/memory/memory.zig @@ -14,7 +14,7 @@ pub const Memory = struct { // * FIELDS * // ************************************************************ /// The allocator used to allocate the memory. - allocator: *const Allocator, + allocator: Allocator, // The data in the memory. data: std.HashMap( relocatable.Relocatable, @@ -42,7 +42,7 @@ pub const Memory = struct { // - `allocator` - The allocator to use. // # Returns // The new memory. - pub fn init(allocator: *const Allocator) !*Memory { + pub fn init(allocator: Allocator) !*Memory { var memory = try allocator.create(Memory); memory.* = Memory{ @@ -50,12 +50,12 @@ pub const Memory = struct { .data = std.AutoHashMap( relocatable.Relocatable, relocatable.MaybeRelocatable, - ).init(allocator.*), + ).init(allocator), .num_segments = 0, .validated_addresses = std.AutoHashMap( relocatable.Relocatable, bool, - ).init(allocator.*), + ).init(allocator), }; return memory; } @@ -124,7 +124,7 @@ test "memory get without value raises error" { var allocator = std.testing.allocator; // Initialize a memory instance. - var memory = try Memory.init(&allocator); + var memory = try Memory.init(allocator); defer memory.deinit(); // Get a value from the memory at an address that doesn't exist. @@ -143,7 +143,7 @@ test "memory set and get" { var allocator = std.testing.allocator; // Initialize a memory instance. - var memory = try Memory.init(&allocator); + var memory = try Memory.init(allocator); defer memory.deinit(); const address_1 = relocatable.Relocatable.new( diff --git a/src/vm/memory/segments.zig b/src/vm/memory/segments.zig index f7cb59b3..b81b9b9d 100644 --- a/src/vm/memory/segments.zig +++ b/src/vm/memory/segments.zig @@ -15,7 +15,7 @@ pub const MemorySegmentManager = struct { // * FIELDS * // ************************************************************ /// The allocator used to allocate the memory. - allocator: *const Allocator, + allocator: Allocator, // The size of the used segments. segment_used_sizes: std.HashMap( u32, @@ -50,7 +50,7 @@ pub const MemorySegmentManager = struct { // * `allocator` - The allocator to use for the HashMaps. // # Returns // A new MemorySegmentManager. - pub fn init(allocator: *const Allocator) !*MemorySegmentManager { + pub fn init(allocator: Allocator) !*MemorySegmentManager { // Create the pointer to the MemorySegmentManager. var segment_manager = try allocator.create(MemorySegmentManager); // Initialize the values of the MemorySegmentManager struct. @@ -59,14 +59,14 @@ pub const MemorySegmentManager = struct { .segment_used_sizes = std.AutoHashMap( u32, u32, - ).init(allocator.*), + ).init(allocator), .segment_sizes = std.AutoHashMap( u32, u32, - ).init(allocator.*), + ).init(allocator), // Initialize the memory pointer. .memory = try Memory.init(allocator), - .public_memory_offsets = std.AutoHashMap(u32, u32).init(allocator.*), + .public_memory_offsets = std.AutoHashMap(u32, u32).init(allocator), }; // Return the pointer to the MemorySegmentManager. return segment_manager; @@ -112,7 +112,7 @@ test "memory segment manager" { var allocator = std.testing.allocator; // Initialize a memory segment manager. - var memory_segment_manager = try MemorySegmentManager.init(&allocator); + var memory_segment_manager = try MemorySegmentManager.init(allocator); defer memory_segment_manager.deinit(); //Allocate a memory segment. diff --git a/src/vm/run_context.zig b/src/vm/run_context.zig index 6252bb31..853889a1 100644 --- a/src/vm/run_context.zig +++ b/src/vm/run_context.zig @@ -11,7 +11,7 @@ const Instruction = @import("instructions.zig").Instruction; /// Contains the register states of the Cairo VM. pub const RunContext = struct { /// The allocator used to allocate the memory for the run context. - allocator: *const Allocator, + allocator: Allocator, /// Program counter (pc) contains the address in memory of the current Cairo /// instruction to be executed. pc: *Relocatable, @@ -37,7 +37,7 @@ pub const RunContext = struct { /// - The initialized run context. /// # Errors /// - If a memory allocation fails. - pub fn init(allocator: *const Allocator) !*RunContext { + pub fn init(allocator: Allocator) !*RunContext { var run_context = try allocator.create(RunContext); run_context.* = RunContext{ .allocator = allocator, @@ -62,7 +62,7 @@ pub const RunContext = struct { /// # Errors /// - If a memory allocation fails. pub fn init_with_values( - allocator: *Allocator, + allocator: Allocator, pc: Relocatable, ap: Relocatable, fp: Relocatable, @@ -220,7 +220,7 @@ test "compute_op1_addr for fp op1 addr" { }; const run_context = try RunContext.init_with_values( - &allocator, + allocator, Relocatable.new( 0, 4,