aboutsummaryrefslogtreecommitdiff
path: root/lib/compiler_rt/memset.zig
diff options
context:
space:
mode:
authorLuuk de Gram <luuk@degram.dev>2022-05-28 15:42:05 +0200
committerAndrew Kelley <andrew@ziglang.org>2022-10-15 07:02:38 -0700
commitf5edaa96dd7c820ffb7c5b8fc8da849a620d69b1 (patch)
tree92f3bbfab91e1f1d288bc66fc6b5af6d83a16a6b /lib/compiler_rt/memset.zig
parent8bb2e96ac3b61a8aa393f250144fb9e1195ca60a (diff)
downloadzig-f5edaa96dd7c820ffb7c5b8fc8da849a620d69b1.tar.gz
zig-f5edaa96dd7c820ffb7c5b8fc8da849a620d69b1.zip
compiler_rt: Move mem implementations from c.zig
This moves functions that LLVM generates calls to, to the compiler_rt implementation itself, rather than c.zig. This is a prerequisite for native backends to link with compiler-rt. This also allows native backends to generate calls to `memcpy` and the like.
Diffstat (limited to 'lib/compiler_rt/memset.zig')
-rw-r--r--lib/compiler_rt/memset.zig30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/compiler_rt/memset.zig b/lib/compiler_rt/memset.zig
new file mode 100644
index 0000000000..1b535fee97
--- /dev/null
+++ b/lib/compiler_rt/memset.zig
@@ -0,0 +1,30 @@
+const std = @import("std");
+const common = @import("./common.zig");
+
+comptime {
+ @export(memset, .{ .name = "memset", .linkage = common.linkage });
+ @export(__memset, .{ .name = "__memset", .linkage = common.linkage });
+}
+
+pub fn memset(dest: ?[*]u8, c: u8, len: usize) callconv(.C) ?[*]u8 {
+ @setRuntimeSafety(false);
+
+ if (len != 0) {
+ var d = dest.?;
+ var n = len;
+ while (true) {
+ d[0] = c;
+ n -= 1;
+ if (n == 0) break;
+ d += 1;
+ }
+ }
+
+ return dest;
+}
+
+pub fn __memset(dest: ?[*]u8, c: u8, n: usize, dest_n: usize) callconv(.C) ?[*]u8 {
+ if (dest_n < n)
+ @panic("buffer overflow");
+ return memset(dest, c, n);
+}