From 7d1332bf86858995e6ce7b76e75d237b2073970c Mon Sep 17 00:00:00 2001 From: g41797 Date: Tue, 31 Dec 2024 11:33:07 +0200 Subject: [PATCH] Add deinit --- src/client_tests.zig | 4 ++-- src/job.zig | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/client_tests.zig b/src/client_tests.zig index 32abdb4..d50cf51 100644 --- a/src/client_tests.zig +++ b/src/client_tests.zig @@ -34,7 +34,7 @@ test "put-state-reserve-delete" { var job: Job = .{}; try job.init(std.testing.allocator); - defer job.free(); + defer job.deinit(); try cl.reserve(0, &job); const rid = job.id().?; @@ -91,7 +91,7 @@ test "all-staff" { var job: Job = .{}; try job.init(std.testing.allocator); - defer job.free(); + defer job.deinit(); try cl.reserve(0, &job); const rid = job.id().?; diff --git a/src/job.zig b/src/job.zig index cca3d1e..edd2c9e 100644 --- a/src/job.zig +++ b/src/job.zig @@ -49,6 +49,9 @@ pub const Job = struct { } pub fn alloc(job: *Job, len: usize) !void { + if (!job.ready) { + return error.NotInitialized; + } if (job.buffer == null) { job.len = roundlen(len); job.buffer = try job.allocator.alloc(u8, job.len); @@ -66,7 +69,11 @@ pub const Job = struct { return job.alloc(rlen); } - pub fn free(job: *Job) void { + pub fn deinit(job: *Job) void { + job.free(); + } + + fn free(job: *Job) void { if (job.buffer != null) { job.allocator.free(job.buffer.?); job.buffer = null;