Commit 2c241b263c

David <seda18@rolmail.net>
2025-05-10 10:37:21
Introduce common `bzero` libc implementation. (#23812)
* Introduce common `bzero` libc implementation. * Update test name according to review Co-authored-by: Linus Groh <mail@linusgroh.de> * address code review - import common implementation when musl or wasi is included - don't use `c_builtins`, use `@memset` * bzero calling conv to .c * Apply review Co-authored-by: Veikka Tuominen <git@vexu.eu> --------- Co-authored-by: Linus Groh <mail@linusgroh.de> Co-authored-by: Veikka Tuominen <git@vexu.eu>
1 parent 85431e7
Changed files (5)
lib
c
libc
musl
src
string
src
lib/c/strings.zig
@@ -0,0 +1,19 @@
+const std = @import("std");
+const common = @import("common.zig");
+
+comptime {
+    @export(&bzero, .{ .name = "bzero", .linkage = common.linkage, .visibility = common.visibility });
+}
+
+fn bzero(s: *anyopaque, n: usize) callconv(.c) void {
+    const s_cast: [*]u8 = @ptrCast(s);
+    @memset(s_cast[0..n], 0);
+}
+
+test bzero {
+    var array: [10]u8 = [_]u8{ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
+    var a = std.mem.zeroes([array.len]u8);
+    a[9] = '0';
+    bzero(&array[0], 9);
+    try std.testing.expect(std.mem.eql(u8, &array, &a));
+}
lib/libc/musl/src/string/bzero.c
@@ -1,8 +0,0 @@
-#define _BSD_SOURCE
-#include <string.h>
-#include <strings.h>
-
-void bzero(void *s, size_t n)
-{
-	memset(s, 0, n);
-}
lib/c.zig
@@ -17,6 +17,7 @@ comptime {
     if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
         // Files specific to musl and wasi-libc.
         _ = @import("c/string.zig");
+        _ = @import("c/strings.zig");
     }
 
     if (builtin.target.isMuslLibC()) {
src/musl.zig
@@ -1840,7 +1840,6 @@ const src_files = [_][]const u8{
     "musl/src/string/arm/__aeabi_memset.s",
     "musl/src/string/bcmp.c",
     "musl/src/string/bcopy.c",
-    "musl/src/string/bzero.c",
     "musl/src/string/explicit_bzero.c",
     "musl/src/string/i386/memset.s",
     "musl/src/string/index.c",
src/wasi_libc.zig
@@ -1044,7 +1044,6 @@ const libc_top_half_src_files = [_][]const u8{
     "musl/src/stdlib/qsort_nr.c",
     "musl/src/string/bcmp.c",
     "musl/src/string/bcopy.c",
-    "musl/src/string/bzero.c",
     "musl/src/string/explicit_bzero.c",
     "musl/src/string/index.c",
     "musl/src/string/memccpy.c",