aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid <seda18@rolmail.net>2025-05-10 10:37:21 +0200
committerGitHub <noreply@github.com>2025-05-10 10:37:21 +0200
commit2c241b263cf165cdde4b3b1df3ce551d915ee476 (patch)
tree881e74dc9d0408705a082cd26c2178602c52efb6 /lib
parent85431e745cda668bb4559b2839834d4899141e2e (diff)
downloadzig-2c241b263cf165cdde4b3b1df3ce551d915ee476.tar.gz
zig-2c241b263cf165cdde4b3b1df3ce551d915ee476.zip
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>
Diffstat (limited to 'lib')
-rw-r--r--lib/c.zig1
-rw-r--r--lib/c/strings.zig19
-rw-r--r--lib/libc/musl/src/string/bzero.c8
3 files changed, 20 insertions, 8 deletions
diff --git a/lib/c.zig b/lib/c.zig
index 156f471573..39684264ce 100644
--- a/lib/c.zig
+++ b/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()) {
diff --git a/lib/c/strings.zig b/lib/c/strings.zig
new file mode 100644
index 0000000000..30e945e7b1
--- /dev/null
+++ b/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));
+}
diff --git a/lib/libc/musl/src/string/bzero.c b/lib/libc/musl/src/string/bzero.c
deleted file mode 100644
index ba536b07e9..0000000000
--- a/lib/libc/musl/src/string/bzero.c
+++ /dev/null
@@ -1,8 +0,0 @@
-#define _BSD_SOURCE
-#include <string.h>
-#include <strings.h>
-
-void bzero(void *s, size_t n)
-{
- memset(s, 0, n);
-}