Skip to content

Commit 852c59b

Browse files
committed
Use smp_allocator when not linking with libc, remove usingnamespace usage, and make tests run again.
1 parent 9b82da8 commit 852c59b

File tree

5 files changed

+140
-152
lines changed

5 files changed

+140
-152
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 cryptocode
3+
Copyright (c) 2023-2025 cryptocode
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Resoures can be anything, such as scripts, images, text, templates config files
2020

2121
## Building the project
2222
To build with Zig 0.13, use the `zig-<version>` tag/release.
23-
To build with Zig 0.14 or master, use the main branch (last tested with Zig version `0.14.0-dev.3470+711b0fef5`)
23+
To build with Zig 0.14 or master, use the main branch (last tested with Zig version `0.15.0-dev.11+5c57e90ff`)
2424

2525
`zig build` will put a `bin` and `lib` directory in your output folder (e.g. zig-out)
2626

build.zig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ pub fn build(b: *std.Build) void {
1717
});
1818
b.installArtifact(lib);
1919

20-
// Uncommenting this will use the C allocator instead of heap_allocator as the backing allocator
21-
// lib.linkLibC();
22-
2320
const exe = b.addExecutable(.{
2421
.name = "stitch",
2522
.root_source_file = b.path("src/main.zig"),
@@ -39,12 +36,14 @@ pub fn build(b: *std.Build) void {
3936

4037
// Creates a step for unit testing.
4138
const main_tests = b.addTest(.{
39+
.name = "tests",
4240
.root_source_file = b.path("src/tests.zig"),
4341
.target = target,
4442
.optimize = optimize,
4543
});
4644
main_tests.root_module.addImport("stitch", stitch_mod);
4745

46+
const run_main_tests = b.addRunArtifact(main_tests);
4847
const test_step = b.step("test", "Run library tests");
49-
test_step.dependOn(&main_tests.step);
48+
test_step.dependOn(&run_main_tests.step);
5049
}

src/lib.zig

Lines changed: 135 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -705,163 +705,160 @@ pub fn generateUniqueFileName(allocator: std.mem.Allocator) ![]const u8 {
705705
/// To interact with stich from C, a writer or reader session must be created.
706706
/// All returned data is owned by the session. Copy any data you need to keep after the session is closed,
707707
/// or keep the session open until you are done with the data.
708-
pub const C_ABI = struct {
709-
pub export fn stitch_init_writer(input_executable_path: ?[*:0]const u8, output_executable_path: ?[*:0]const u8, error_code: *u64) callconv(.C) ?*anyopaque {
710-
error_code.* = 0;
711-
if (input_executable_path == null) {
712-
error_code.* = translateError(StitchError.CouldNotOpenInputFile);
713-
return null;
714-
}
715-
const allocator = if (builtin.link_libc) std.heap.c_allocator else std.heap.page_allocator;
716-
const writer = initWriter(allocator, std.mem.span(input_executable_path.?), if (output_executable_path) |path| std.mem.span(path) else std.mem.span(input_executable_path.?)) catch |err| {
717-
error_code.* = translateError(err);
718-
return null;
719-
};
720-
return writer.session;
708+
pub export fn stitch_init_writer(input_executable_path: ?[*:0]const u8, output_executable_path: ?[*:0]const u8, error_code: *u64) callconv(.C) ?*anyopaque {
709+
error_code.* = 0;
710+
if (input_executable_path == null) {
711+
error_code.* = translateError(StitchError.CouldNotOpenInputFile);
712+
return null;
721713
}
714+
const allocator = if (builtin.link_libc) std.heap.c_allocator else std.heap.smp_allocator;
715+
const writer = initWriter(allocator, std.mem.span(input_executable_path.?), if (output_executable_path) |path| std.mem.span(path) else std.mem.span(input_executable_path.?)) catch |err| {
716+
error_code.* = translateError(err);
717+
return null;
718+
};
719+
return writer.session;
720+
}
722721

723-
pub export fn stitch_init_reader(executable_path: ?[*:0]const u8, error_code: *u64) callconv(.C) ?*anyopaque {
724-
error_code.* = 0;
725-
if (executable_path == null) {
726-
error_code.* = translateError(StitchError.CouldNotOpenInputFile);
727-
return null;
728-
}
729-
const allocator = if (builtin.link_libc) std.heap.c_allocator else std.heap.page_allocator;
730-
const reader = initReader(allocator, if (executable_path) |p| std.mem.span(p) else null) catch |err| {
731-
error_code.* = translateError(err);
732-
return null;
733-
};
734-
return reader.session;
722+
pub export fn stitch_init_reader(executable_path: ?[*:0]const u8, error_code: *u64) callconv(.C) ?*anyopaque {
723+
error_code.* = 0;
724+
if (executable_path == null) {
725+
error_code.* = translateError(StitchError.CouldNotOpenInputFile);
726+
return null;
735727
}
728+
const allocator = if (builtin.link_libc) std.heap.c_allocator else std.heap.smp_allocator;
729+
const reader = initReader(allocator, if (executable_path) |p| std.mem.span(p) else null) catch |err| {
730+
error_code.* = translateError(err);
731+
return null;
732+
};
733+
return reader.session;
734+
}
736735

737-
pub export fn stitch_deinit(session: *anyopaque) callconv(.C) void {
738-
fromC(session).deinit();
739-
}
736+
pub export fn stitch_deinit(session: *anyopaque) callconv(.C) void {
737+
fromC(session).deinit();
738+
}
740739

741-
pub export fn stitch_reader_get_resource_count(reader: *anyopaque) callconv(.C) u64 {
742-
return fromC(reader).rw.reader.getResourceCount();
743-
}
740+
pub export fn stitch_reader_get_resource_count(reader: *anyopaque) callconv(.C) u64 {
741+
return fromC(reader).rw.reader.getResourceCount();
742+
}
744743

745-
pub export fn stitch_reader_get_format_version(reader: *anyopaque) callconv(.C) u8 {
746-
return fromC(reader).rw.reader.getFormatVersion();
747-
}
744+
pub export fn stitch_reader_get_format_version(reader: *anyopaque) callconv(.C) u8 {
745+
return fromC(reader).rw.reader.getFormatVersion();
746+
}
748747

749-
pub export fn stitch_reader_get_resource_index(reader: *anyopaque, name: [*:0]const u8, error_code: *u64) callconv(.C) u64 {
750-
return fromC(reader).rw.reader.getResourceIndex(std.mem.span(name)) catch |err| {
751-
error_code.* = translateError(err);
752-
return std.math.maxInt(u64);
753-
};
754-
}
748+
pub export fn stitch_reader_get_resource_index(reader: *anyopaque, name: [*:0]const u8, error_code: *u64) callconv(.C) u64 {
749+
return fromC(reader).rw.reader.getResourceIndex(std.mem.span(name)) catch |err| {
750+
error_code.* = translateError(err);
751+
return std.math.maxInt(u64);
752+
};
753+
}
755754

756-
pub export fn stitch_reader_get_resource_byte_len(reader: *anyopaque, resource_index: u64, error_code: *u64) callconv(.C) u64 {
757-
return fromC(reader).rw.reader.getResourceSize(resource_index) catch |err| {
758-
error_code.* = translateError(err);
759-
return std.math.maxInt(u64);
760-
};
761-
}
755+
pub export fn stitch_reader_get_resource_byte_len(reader: *anyopaque, resource_index: u64, error_code: *u64) callconv(.C) u64 {
756+
return fromC(reader).rw.reader.getResourceSize(resource_index) catch |err| {
757+
error_code.* = translateError(err);
758+
return std.math.maxInt(u64);
759+
};
760+
}
762761

763-
pub export fn stitch_reader_get_resource_bytes(reader: *anyopaque, resource_index: u64, error_code: *u64) callconv(.C) ?[*]const u8 {
764-
const slice = fromC(reader).rw.reader.getResourceAsSlice(resource_index) catch |err| {
765-
error_code.* = translateError(err);
766-
return null;
767-
};
768-
return slice.ptr;
769-
}
762+
pub export fn stitch_reader_get_resource_bytes(reader: *anyopaque, resource_index: u64, error_code: *u64) callconv(.C) ?[*]const u8 {
763+
const slice = fromC(reader).rw.reader.getResourceAsSlice(resource_index) catch |err| {
764+
error_code.* = translateError(err);
765+
return null;
766+
};
767+
return slice.ptr;
768+
}
770769

771-
pub export fn stitch_reader_get_scratch_bytes(reader: *anyopaque, resource_index: u64, error_code: *u64) callconv(.C) ?[*]const u8 {
772-
error_code.* = 0;
773-
const slice = fromC(reader).rw.reader.getScratchBytes(resource_index) catch |err| {
774-
error_code.* = translateError(err);
775-
return null;
776-
};
777-
return slice.ptr;
778-
}
770+
pub export fn stitch_reader_get_scratch_bytes(reader: *anyopaque, resource_index: u64, error_code: *u64) callconv(.C) ?[*]const u8 {
771+
error_code.* = 0;
772+
const slice = fromC(reader).rw.reader.getScratchBytes(resource_index) catch |err| {
773+
error_code.* = translateError(err);
774+
return null;
775+
};
776+
return slice.ptr;
777+
}
779778

780-
pub export fn stitch_writer_commit(writer: *anyopaque, error_code: *u64) callconv(.C) void {
781-
fromC(writer).rw.writer.commit() catch |err| {
782-
error_code.* = translateError(err);
783-
};
784-
}
779+
pub export fn stitch_writer_commit(writer: *anyopaque, error_code: *u64) callconv(.C) void {
780+
fromC(writer).rw.writer.commit() catch |err| {
781+
error_code.* = translateError(err);
782+
};
783+
}
785784

786-
pub export fn stitch_writer_add_resource_from_path(writer: *anyopaque, name: [*:0]const u8, path: [*:0]const u8, error_code: *u64) callconv(.C) u64 {
787-
return fromC(writer).rw.writer.addResourceFromPath(std.mem.span(name), std.mem.span(path)) catch |err| {
788-
error_code.* = translateError(err);
789-
return std.math.maxInt(u64);
790-
};
791-
}
785+
pub export fn stitch_writer_add_resource_from_path(writer: *anyopaque, name: [*:0]const u8, path: [*:0]const u8, error_code: *u64) callconv(.C) u64 {
786+
return fromC(writer).rw.writer.addResourceFromPath(std.mem.span(name), std.mem.span(path)) catch |err| {
787+
error_code.* = translateError(err);
788+
return std.math.maxInt(u64);
789+
};
790+
}
792791

793-
pub export fn stitch_writer_add_resource_from_bytes(writer: *anyopaque, name: [*:0]const u8, bytes: [*]const u8, len: usize, error_code: *u64) callconv(.C) u64 {
794-
return fromC(writer).rw.writer.addResourceFromSlice(std.mem.span(name), bytes[0..len]) catch |err| {
795-
error_code.* = translateError(err);
796-
return std.math.maxInt(u64);
797-
};
798-
}
792+
pub export fn stitch_writer_add_resource_from_bytes(writer: *anyopaque, name: [*:0]const u8, bytes: [*]const u8, len: usize, error_code: *u64) callconv(.C) u64 {
793+
return fromC(writer).rw.writer.addResourceFromSlice(std.mem.span(name), bytes[0..len]) catch |err| {
794+
error_code.* = translateError(err);
795+
return std.math.maxInt(u64);
796+
};
797+
}
799798

800-
pub export fn stitch_writer_set_scratch_bytes(writer: *anyopaque, resource_index: u64, bytes: [*]const u8, error_code: *u64) callconv(.C) void {
801-
fromC(writer).rw.writer.setScratchBytes(resource_index, bytes[0..8].*) catch |err| {
802-
error_code.* = translateError(err);
803-
};
804-
}
799+
pub export fn stitch_writer_set_scratch_bytes(writer: *anyopaque, resource_index: u64, bytes: [*]const u8, error_code: *u64) callconv(.C) void {
800+
fromC(writer).rw.writer.setScratchBytes(resource_index, bytes[0..8].*) catch |err| {
801+
error_code.* = translateError(err);
802+
};
803+
}
805804

806-
pub export fn stitch_read_entire_file(reader_or_writer: *anyopaque, path: [*:0]const u8, error_code: *u64) callconv(.C) ?[*]const u8 {
807-
const s = fromC(reader_or_writer);
808-
switch (s.rw) {
809-
inline else => |rw| {
810-
const slice = rw.session.readEntireFile(std.mem.span(path)) catch |err| {
811-
error_code.* = translateError(err);
812-
return null;
813-
};
814-
return slice.ptr;
815-
},
816-
}
817-
return null;
805+
pub export fn stitch_read_entire_file(reader_or_writer: *anyopaque, path: [*:0]const u8, error_code: *u64) callconv(.C) ?[*]const u8 {
806+
const s = fromC(reader_or_writer);
807+
switch (s.rw) {
808+
inline else => |rw| {
809+
const slice = rw.session.readEntireFile(std.mem.span(path)) catch |err| {
810+
error_code.* = translateError(err);
811+
return null;
812+
};
813+
return slice.ptr;
814+
},
818815
}
816+
return null;
817+
}
819818

820-
pub export fn stitch_get_last_error_diagnostic(session: ?*anyopaque) callconv(.C) ?[*:0]const u8 {
821-
if (session == null) return "Could not get diagnostic: Invalid session";
822-
var s = fromC(session.?);
823-
if (s.getDiagnostics()) |d| {
824-
const str = d.toOwnedString(s.arena.allocator()) catch return null;
825-
return s.arena.allocator().dupeZ(u8, str) catch return null;
826-
} else return null;
827-
}
828-
829-
pub export fn stitch_get_error_diagnostic(error_code: u64) callconv(.C) ?[*:0]const u8 {
830-
switch (error_code) {
831-
2 => return "Output file already exists",
832-
3 => return "Could not open input file",
833-
4 => return "Could not open output file",
834-
5 => return "Invalid executable format",
835-
6 => return "Resource not found",
836-
7 => return "I/O error",
837-
else => return "Unknown error code",
838-
}
839-
}
819+
pub export fn stitch_get_last_error_diagnostic(session: ?*anyopaque) callconv(.C) ?[*:0]const u8 {
820+
if (session == null) return "Could not get diagnostic: Invalid session";
821+
var s = fromC(session.?);
822+
if (s.getDiagnostics()) |d| {
823+
const str = d.toOwnedString(s.arena.allocator()) catch return null;
824+
return s.arena.allocator().dupeZ(u8, str) catch return null;
825+
} else return null;
826+
}
840827

841-
pub export fn stitch_test_setup() callconv(.C) void {
842-
testSetup() catch unreachable;
828+
pub export fn stitch_get_error_diagnostic(error_code: u64) callconv(.C) ?[*:0]const u8 {
829+
switch (error_code) {
830+
2 => return "Output file already exists",
831+
3 => return "Could not open input file",
832+
4 => return "Could not open output file",
833+
5 => return "Invalid executable format",
834+
6 => return "Resource not found",
835+
7 => return "I/O error",
836+
else => return "Unknown error code",
843837
}
838+
}
844839

845-
pub export fn stitch_test_teardown() callconv(.C) void {
846-
testTeardown();
847-
}
840+
pub export fn stitch_test_setup() callconv(.C) void {
841+
testSetup() catch unreachable;
842+
}
848843

849-
// Convert from a C ABI pointer to a Zig pointer to self
850-
fn fromC(session: *anyopaque) *Self {
851-
return @ptrCast(@alignCast(session));
852-
}
844+
pub export fn stitch_test_teardown() callconv(.C) void {
845+
testTeardown();
846+
}
853847

854-
// Map Zig errors to C error codes
855-
fn translateError(err: anyerror) u64 {
856-
return switch (err) {
857-
StitchError.OutputFileAlreadyExists => 2,
858-
StitchError.CouldNotOpenInputFile => 3,
859-
StitchError.CouldNotOpenOutputFile => 4,
860-
StitchError.InvalidExecutableFormat => 5,
861-
StitchError.ResourceNotFound => 6,
862-
StitchError.IoError => 7,
863-
else => 1,
864-
};
865-
}
866-
};
867-
usingnamespace C_ABI;
848+
// Convert from a C ABI pointer to a Zig pointer to self
849+
fn fromC(session: *anyopaque) *Self {
850+
return @ptrCast(@alignCast(session));
851+
}
852+
853+
// Map Zig errors to C error codes
854+
fn translateError(err: anyerror) u64 {
855+
return switch (err) {
856+
StitchError.OutputFileAlreadyExists => 2,
857+
StitchError.CouldNotOpenInputFile => 3,
858+
StitchError.CouldNotOpenOutputFile => 4,
859+
StitchError.InvalidExecutableFormat => 5,
860+
StitchError.ResourceNotFound => 6,
861+
StitchError.IoError => 7,
862+
else => 1,
863+
};
864+
}

src/tests.zig

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,3 @@ test "read invalid exe, too small" {
131131
try std.testing.expectError(StitchError.InvalidExecutableFormat, Stitch.initReader(arena.allocator(), random_name));
132132
}
133133
}
134-
135-
test {
136-
std.testing.refAllDecls(@This());
137-
std.testing.refAllDecls(Stitch.StitchReader);
138-
std.testing.refAllDecls(Stitch.StitchResourceReader);
139-
std.testing.refAllDecls(Stitch.StitchWriter);
140-
std.testing.refAllDecls(Stitch.C_ABI);
141-
}

0 commit comments

Comments
 (0)