diff --git a/Assembly.zig b/Assembly.zig new file mode 100644 index 0000000000000000000000000000000000000000..d4bd9e3101b19edb90f38f7c948e73365904c8a5 --- /dev/null +++ b/Assembly.zig @@ -0,0 +1,41 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; + +data: []const u8, +text: []const u8, + +const Assembly = @This(); + +/// 释放Assembly对象占用的内存资源 +/// +/// 负责释放Assembly结构体中data和text字段动态分配的内存 +/// +/// Parameters: +/// - self: 指向Assembly实例的常量指针 +/// - gpa: 用于释放内存的通用分配器 +pub fn deinit(self: *const Assembly, gpa: Allocator) void { + gpa.free(self.data); + gpa.free(self.text); +} + +/// 将Assembly的data和text部分写入文件 +/// +/// 使用writevAll系统调用一次性写入数据和文本部分到指定文件 +/// +/// Parameters: +/// - self: Assembly实例(按值传递) +/// - file: 目标文件描述符 +/// +/// Returns: +/// - void: 成功时无返回值 +/// - error: 写入失败时返回系统错误 +/// +/// Error Conditions: +/// - 文件写入错误时返回相应的系统错误码 +pub fn writeToAllFile(self: Assembly, file: std.fs.File) !void { + var vec: [2]std.posix.iovec_const = .{ + .{ .base = self.data.ptr, .len = self.data.len }, + .{ .base = self.text.ptr, .len = self.text.len }, + }; + return file.writevAll(&vec); +}