authorgravatar for seda18@rolmail.netDavid Senoner <seda18@rolmail.net> 2025-05-10 10:37:21+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-05-10 10:37:21+02:00
log2c241b263cf165cdde4b3b1df3ce551d915ee476
tree881e74dc9d0408705a082cd26c2178602c52efb6
parent85431e745cda668bb4559b2839834d4899141e2e
signaturebadge-check Signed by PGP key B5690EEEBB952194

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>

5 files changed, 20 insertions(+), 10 deletions(-)

lib/c.zig+1
......@@ -17,6 +17,7 @@ comptime {
1717 if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
1818 // Files specific to musl and wasi-libc.
1919 _ = @import("c/string.zig");
20 _ = @import("c/strings.zig");
2021 }
2122
2223 if (builtin.target.isMuslLibC()) {
lib/c/strings.zig created+19
......@@ -0,0 +1,19 @@
1const std = @import("std");
2const common = @import("common.zig");
3
4comptime {
5 @export(&bzero, .{ .name = "bzero", .linkage = common.linkage, .visibility = common.visibility });
6}
7
8fn bzero(s: *anyopaque, n: usize) callconv(.c) void {
9 const s_cast: [*]u8 = @ptrCast(s);
10 @memset(s_cast[0..n], 0);
11}
12
13test bzero {
14 var array: [10]u8 = [_]u8{ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
15 var a = std.mem.zeroes([array.len]u8);
16 a[9] = '0';
17 bzero(&array[0], 9);
18 try std.testing.expect(std.mem.eql(u8, &array, &a));
19}
lib/libc/musl/src/string/bzero.c deleted-8
......@@ -1,8 +0,0 @@
1#define _BSD_SOURCE
2#include <string.h>
3#include <strings.h>
4
5void bzero(void *s, size_t n)
6{
7 memset(s, 0, n);
8}
src/musl.zig-1
......@@ -1840,7 +1840,6 @@ const src_files = [_][]const u8{
18401840 "musl/src/string/arm/__aeabi_memset.s",
18411841 "musl/src/string/bcmp.c",
18421842 "musl/src/string/bcopy.c",
1843 "musl/src/string/bzero.c",
18441843 "musl/src/string/explicit_bzero.c",
18451844 "musl/src/string/i386/memset.s",
18461845 "musl/src/string/index.c",
src/wasi_libc.zig-1
......@@ -1044,7 +1044,6 @@ const libc_top_half_src_files = [_][]const u8{
10441044 "musl/src/stdlib/qsort_nr.c",
10451045 "musl/src/string/bcmp.c",
10461046 "musl/src/string/bcopy.c",
1047 "musl/src/string/bzero.c",
10481047 "musl/src/string/explicit_bzero.c",
10491048 "musl/src/string/index.c",
10501049 "musl/src/string/memccpy.c",