From 9295355985202c267b4326b5a6e2ad5158b48e5d Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 26 Apr 2023 13:41:02 -0700 Subject: LLVM backend: optimize memset with comptime-known element When the element is comptime-known, we can check if it has a repeated byte representation. In this case, `@memset` can be lowered with the LLVM intrinsic rather than with a loop. --- src/Sema.zig | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/Sema.zig') diff --git a/src/Sema.zig b/src/Sema.zig index 8b47f1877b..e05308b6c0 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -26953,9 +26953,11 @@ fn storePtrVal( defer sema.gpa.free(buffer); reinterpret.val_ptr.*.writeToMemory(mut_kit.ty, sema.mod, buffer) catch |err| switch (err) { error.ReinterpretDeclRef => unreachable, + error.IllDefinedMemoryLayout => unreachable, // Sema was supposed to emit a compile error already }; operand_val.writeToMemory(operand_ty, sema.mod, buffer[reinterpret.byte_offset..]) catch |err| switch (err) { error.ReinterpretDeclRef => unreachable, + error.IllDefinedMemoryLayout => unreachable, // Sema was supposed to emit a compile error already }; const arena = mut_kit.beginArena(sema.mod); @@ -27905,6 +27907,7 @@ fn bitCastVal( defer sema.gpa.free(buffer); val.writeToMemory(old_ty, sema.mod, buffer) catch |err| switch (err) { error.ReinterpretDeclRef => return null, + error.IllDefinedMemoryLayout => unreachable, // Sema was supposed to emit a compile error already }; return try Value.readFromMemory(new_ty, sema.mod, buffer[buffer_offset..], sema.arena); } -- cgit v1.2.3 From 82fc360613e7070ffe7124fb070b151a382bd31b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 26 Apr 2023 18:20:22 -0700 Subject: stage2: avoid panicking for unimplemented compiler code Prevents the compiler from crashing when using `@memset` on extern unions, for example. --- src/Sema.zig | 3 +++ src/value.zig | 10 ++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) (limited to 'src/Sema.zig') diff --git a/src/Sema.zig b/src/Sema.zig index e05308b6c0..0910b96ad4 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -26954,10 +26954,12 @@ fn storePtrVal( reinterpret.val_ptr.*.writeToMemory(mut_kit.ty, sema.mod, buffer) catch |err| switch (err) { error.ReinterpretDeclRef => unreachable, error.IllDefinedMemoryLayout => unreachable, // Sema was supposed to emit a compile error already + error.Unimplemented => return sema.fail(block, src, "TODO: implement writeToMemory for type '{}'", .{mut_kit.ty.fmt(sema.mod)}), }; operand_val.writeToMemory(operand_ty, sema.mod, buffer[reinterpret.byte_offset..]) catch |err| switch (err) { error.ReinterpretDeclRef => unreachable, error.IllDefinedMemoryLayout => unreachable, // Sema was supposed to emit a compile error already + error.Unimplemented => return sema.fail(block, src, "TODO: implement writeToMemory for type '{}'", .{mut_kit.ty.fmt(sema.mod)}), }; const arena = mut_kit.beginArena(sema.mod); @@ -27908,6 +27910,7 @@ fn bitCastVal( val.writeToMemory(old_ty, sema.mod, buffer) catch |err| switch (err) { error.ReinterpretDeclRef => return null, error.IllDefinedMemoryLayout => unreachable, // Sema was supposed to emit a compile error already + error.Unimplemented => return sema.fail(block, src, "TODO: implement writeToMemory for type '{}'", .{old_ty.fmt(sema.mod)}), }; return try Value.readFromMemory(new_ty, sema.mod, buffer[buffer_offset..], sema.arena); } diff --git a/src/value.zig b/src/value.zig index 2b9636f5e9..2f34749a50 100644 --- a/src/value.zig +++ b/src/value.zig @@ -1281,6 +1281,7 @@ pub const Value = extern union { pub fn writeToMemory(val: Value, ty: Type, mod: *Module, buffer: []u8) error{ ReinterpretDeclRef, IllDefinedMemoryLayout, + Unimplemented, }!void { const target = mod.getTarget(); const endian = target.cpu.arch.endian(); @@ -1370,19 +1371,19 @@ pub const Value = extern union { }, .Union => switch (ty.containerLayout()) { .Auto => return error.IllDefinedMemoryLayout, - .Extern => @panic("TODO implement writeToMemory for extern unions"), + .Extern => return error.Unimplemented, .Packed => { const byte_count = (@intCast(usize, ty.bitSize(target)) + 7) / 8; return writeToPackedMemory(val, ty, mod, buffer[0..byte_count], 0); }, }, .Pointer => { - assert(!ty.isSlice()); // No well defined layout. + if (ty.isSlice()) return error.IllDefinedMemoryLayout; if (val.isDeclRef()) return error.ReinterpretDeclRef; return val.writeToMemory(Type.usize, mod, buffer); }, .Optional => { - assert(ty.isPtrLikeOptional()); + if (!ty.isPtrLikeOptional()) return error.IllDefinedMemoryLayout; var buf: Type.Payload.ElemType = undefined; const child = ty.optionalChild(&buf); const opt_val = val.optionalValue(); @@ -1392,7 +1393,7 @@ pub const Value = extern union { return writeToMemory(Value.zero, Type.usize, mod, buffer); } }, - else => @panic("TODO implement writeToMemory for more types"), + else => return error.Unimplemented, } } @@ -5401,6 +5402,7 @@ pub const Value = extern union { // code late in compilation. So, this error handling is too aggressive and // causes some false negatives, causing less-than-ideal code generation. error.IllDefinedMemoryLayout => return null, + error.Unimplemented => return null, }; const first_byte = byte_buffer[0]; for (byte_buffer[1..]) |byte| { -- cgit v1.2.3 From 6261c1373168b265047db5704d9d0fd5f2e458f2 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 26 Apr 2023 13:57:08 -0700 Subject: update codebase to use `@memset` and `@memcpy` --- lib/std/Build.zig | 4 +- lib/std/Build/Cache.zig | 2 +- lib/std/Build/CompileStep.zig | 2 +- lib/std/Progress.zig | 2 +- lib/std/Thread.zig | 2 +- lib/std/array_hash_map.zig | 6 +-- lib/std/array_list.zig | 24 +++++------ lib/std/base64.zig | 4 +- lib/std/bit_set.zig | 2 +- lib/std/bounded_array.zig | 20 ++++----- lib/std/buf_set.zig | 2 +- lib/std/child_process.zig | 6 +-- lib/std/compress/deflate/compressor.zig | 12 +++--- lib/std/compress/deflate/decompressor.zig | 2 +- lib/std/compress/deflate/deflate_fast.zig | 4 +- lib/std/compress/lzma.zig | 6 +-- lib/std/compress/lzma/decode/rangecoder.zig | 2 +- lib/std/compress/lzma/vec2d.zig | 4 +- lib/std/compress/zstandard/decode/block.zig | 17 ++++---- lib/std/crypto/25519/ed25519.zig | 18 ++++---- lib/std/crypto/25519/edwards25519.zig | 8 ++-- lib/std/crypto/25519/scalar.zig | 6 +-- lib/std/crypto/25519/x25519.zig | 14 +++---- lib/std/crypto/Certificate.zig | 14 +++---- lib/std/crypto/aegis.zig | 50 +++++++++++----------- lib/std/crypto/aes_gcm.zig | 4 +- lib/std/crypto/aes_ocb.zig | 20 ++++----- lib/std/crypto/argon2.zig | 4 +- lib/std/crypto/ascon.zig | 14 +++---- lib/std/crypto/bcrypt.zig | 6 +-- lib/std/crypto/benchmark.zig | 4 +- lib/std/crypto/blake2.zig | 30 +++++++------- lib/std/crypto/blake3.zig | 8 ++-- lib/std/crypto/chacha20.zig | 8 ++-- lib/std/crypto/ecdsa.zig | 20 ++++----- lib/std/crypto/hkdf.zig | 2 +- lib/std/crypto/hmac.zig | 8 ++-- lib/std/crypto/isap.zig | 2 +- lib/std/crypto/keccak_p.zig | 16 ++++---- lib/std/crypto/kyber_d00.zig | 2 +- lib/std/crypto/md5.zig | 11 ++--- lib/std/crypto/modes.zig | 6 ++- lib/std/crypto/pbkdf2.zig | 4 +- lib/std/crypto/pcurves/common.zig | 4 +- lib/std/crypto/pcurves/p256.zig | 6 +-- lib/std/crypto/pcurves/p256/scalar.zig | 10 ++--- lib/std/crypto/pcurves/p384.zig | 6 +-- lib/std/crypto/pcurves/p384/scalar.zig | 8 ++-- lib/std/crypto/pcurves/secp256k1.zig | 6 +-- lib/std/crypto/pcurves/secp256k1/scalar.zig | 10 ++--- lib/std/crypto/phc_encoding.zig | 2 +- lib/std/crypto/salsa20.zig | 12 +++--- lib/std/crypto/sha1.zig | 8 ++-- lib/std/crypto/sha2.zig | 18 ++++---- lib/std/crypto/sha3.zig | 4 +- lib/std/crypto/siphash.zig | 9 ++-- lib/std/crypto/tls/Client.zig | 30 +++++++------- lib/std/crypto/utils.zig | 12 ++---- lib/std/cstr.zig | 4 +- lib/std/dynamic_library.zig | 2 +- lib/std/enums.zig | 4 +- lib/std/fifo.zig | 26 ++++++------ lib/std/fmt/errol.zig | 4 +- lib/std/fs/path.zig | 12 +++--- lib/std/hash/cityhash.zig | 4 +- lib/std/hash/wyhash.zig | 5 ++- lib/std/hash/xxhash.zig | 12 +++--- lib/std/heap/WasmAllocator.zig | 2 +- lib/std/heap/WasmPageAllocator.zig | 2 +- lib/std/heap/general_purpose_allocator.zig | 4 +- lib/std/io/buffered_reader.zig | 11 ++--- lib/std/io/buffered_writer.zig | 5 ++- lib/std/io/fixed_buffer_stream.zig | 6 +-- lib/std/io/writer.zig | 2 +- lib/std/json.zig | 4 +- lib/std/json/test.zig | 2 +- lib/std/math/big/int.zig | 64 ++++++++++++++--------------- lib/std/mem.zig | 10 +++-- lib/std/mem/Allocator.zig | 4 +- lib/std/multi_array_list.zig | 6 +-- lib/std/net.zig | 6 +-- lib/std/os.zig | 34 ++++++++------- lib/std/os/linux/io_uring.zig | 8 ++-- lib/std/os/linux/tls.zig | 2 +- lib/std/os/test.zig | 2 +- lib/std/process.zig | 2 +- lib/std/rand/ChaCha.zig | 13 +++--- lib/std/rand/Isaac64.zig | 4 +- lib/std/segmented_list.zig | 23 +++++------ lib/std/sort.zig | 59 ++++++++++++++++---------- lib/std/target.zig | 4 +- lib/std/testing/failing_allocator.zig | 2 +- lib/std/tz.zig | 2 +- lib/std/zig/render.zig | 4 +- lib/std/zig/system/NativeTargetInfo.zig | 8 ++-- lib/std/zig/system/windows.zig | 4 +- src/Compilation.zig | 6 +-- src/Liveness.zig | 8 ++-- src/Sema.zig | 44 ++++++++++---------- src/arch/aarch64/CodeGen.zig | 8 ++-- src/arch/arm/CodeGen.zig | 8 ++-- src/arch/riscv64/CodeGen.zig | 6 +-- src/arch/sparc64/CodeGen.zig | 6 +-- src/arch/x86_64/CodeGen.zig | 4 +- src/arch/x86_64/Encoding.zig | 2 +- src/arch/x86_64/abi.zig | 2 +- src/arch/x86_64/encoder.zig | 4 +- src/codegen/c.zig | 14 +++---- src/link/Coff.zig | 10 ++--- src/link/Dwarf.zig | 8 ++-- src/link/Elf.zig | 2 +- src/link/MachO.zig | 10 ++--- src/link/MachO/Object.zig | 12 +++--- src/link/MachO/Trie.zig | 2 +- src/link/MachO/UnwindInfo.zig | 2 +- src/link/MachO/zld.zig | 10 ++--- src/link/Wasm.zig | 2 +- src/objcopy.zig | 2 +- src/print_air.zig | 2 +- src/print_zir.zig | 2 +- src/value.zig | 4 +- 121 files changed, 558 insertions(+), 541 deletions(-) (limited to 'src/Sema.zig') diff --git a/lib/std/Build.zig b/lib/std/Build.zig index 34806adf48..f23b3ba5aa 100644 --- a/lib/std/Build.zig +++ b/lib/std/Build.zig @@ -1693,10 +1693,10 @@ pub fn constructCMacro(allocator: Allocator, name: []const u8, value: ?[]const u u8, name.len + if (value) |value_slice| value_slice.len + 1 else 0, ) catch |err| if (err == error.OutOfMemory) @panic("Out of memory") else unreachable; - mem.copy(u8, macro, name); + @memcpy(macro[0..name.len], name); if (value) |value_slice| { macro[name.len] = '='; - mem.copy(u8, macro[name.len + 1 ..], value_slice); + @memcpy(macro[name.len + 1 ..][0..value_slice.len], value_slice); } return macro; } diff --git a/lib/std/Build/Cache.zig b/lib/std/Build/Cache.zig index cae779a306..9139311785 100644 --- a/lib/std/Build/Cache.zig +++ b/lib/std/Build/Cache.zig @@ -388,7 +388,7 @@ pub const Manifest = struct { self.hash.hasher = hasher_init; self.hash.hasher.update(&bin_digest); - mem.copy(u8, &manifest_file_path, &self.hex_digest); + @memcpy(manifest_file_path[0..self.hex_digest.len], &self.hex_digest); manifest_file_path[hex_digest_len..][0..ext.len].* = ext.*; if (self.files.items.len == 0) { diff --git a/lib/std/Build/CompileStep.zig b/lib/std/Build/CompileStep.zig index b71298ce6a..d5a135e24b 100644 --- a/lib/std/Build/CompileStep.zig +++ b/lib/std/Build/CompileStep.zig @@ -1139,7 +1139,7 @@ fn appendModuleArgs( // We'll use this buffer to store the name we decide on var buf = try b.allocator.alloc(u8, dep.name.len + 32); // First, try just the exposed dependency name - std.mem.copy(u8, buf, dep.name); + @memcpy(buf[0..dep.name.len], dep.name); var name = buf[0..dep.name.len]; var n: usize = 0; while (names.contains(name)) { diff --git a/lib/std/Progress.zig b/lib/std/Progress.zig index dba7166398..e3c5fc20dd 100644 --- a/lib/std/Progress.zig +++ b/lib/std/Progress.zig @@ -374,7 +374,7 @@ fn bufWrite(self: *Progress, end: *usize, comptime format: []const u8, args: any self.columns_written += self.output_buffer.len - end.*; end.* = self.output_buffer.len; const suffix = "... "; - std.mem.copy(u8, self.output_buffer[self.output_buffer.len - suffix.len ..], suffix); + @memcpy(self.output_buffer[self.output_buffer.len - suffix.len ..], suffix); }, } } diff --git a/lib/std/Thread.zig b/lib/std/Thread.zig index f2a78100ad..119328b2a8 100644 --- a/lib/std/Thread.zig +++ b/lib/std/Thread.zig @@ -56,7 +56,7 @@ pub fn setName(self: Thread, name: []const u8) SetNameError!void { const name_with_terminator = blk: { var name_buf: [max_name_len:0]u8 = undefined; - std.mem.copy(u8, &name_buf, name); + @memcpy(name_buf[0..name.len], name); name_buf[name.len] = 0; break :blk name_buf[0..name.len :0]; }; diff --git a/lib/std/array_hash_map.zig b/lib/std/array_hash_map.zig index bf43f24bc0..55b9aac6e4 100644 --- a/lib/std/array_hash_map.zig +++ b/lib/std/array_hash_map.zig @@ -578,9 +578,9 @@ pub fn ArrayHashMapUnmanaged( self.entries.len = 0; if (self.index_header) |header| { switch (header.capacityIndexType()) { - .u8 => mem.set(Index(u8), header.indexes(u8), Index(u8).empty), - .u16 => mem.set(Index(u16), header.indexes(u16), Index(u16).empty), - .u32 => mem.set(Index(u32), header.indexes(u32), Index(u32).empty), + .u8 => @memset(header.indexes(u8), Index(u8).empty), + .u16 => @memset(header.indexes(u16), Index(u16).empty), + .u32 => @memset(header.indexes(u32), Index(u32).empty), } } } diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index 352aef14fc..cbca601b82 100644 --- a/lib/std/array_list.zig +++ b/lib/std/array_list.zig @@ -120,7 +120,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { } const new_memory = try allocator.alignedAlloc(T, alignment, self.items.len); - mem.copy(T, new_memory, self.items); + @memcpy(new_memory, self.items); @memset(self.items, undefined); self.clearAndFree(); return new_memory; @@ -170,7 +170,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { self.items.len += items.len; mem.copyBackwards(T, self.items[i + items.len .. self.items.len], self.items[i .. self.items.len - items.len]); - mem.copy(T, self.items[i .. i + items.len], items); + @memcpy(self.items[i..][0..items.len], items); } /// Replace range of elements `list[start..start+len]` with `new_items`. @@ -182,15 +182,15 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { const range = self.items[start..after_range]; if (range.len == new_items.len) - mem.copy(T, range, new_items) + @memcpy(range[0..new_items.len], new_items) else if (range.len < new_items.len) { const first = new_items[0..range.len]; const rest = new_items[range.len..]; - mem.copy(T, range, first); + @memcpy(range[0..first.len], first); try self.insertSlice(after_range, rest); } else { - mem.copy(T, range, new_items); + @memcpy(range[0..new_items.len], new_items); const after_subrange = start + new_items.len; for (self.items[after_range..], 0..) |item, i| { @@ -260,7 +260,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { const new_len = old_len + items.len; assert(new_len <= self.capacity); self.items.len = new_len; - mem.copy(T, self.items[old_len..], items); + @memcpy(self.items[old_len..][0..items.len], items); } /// Append an unaligned slice of items to the list. Allocates more @@ -401,7 +401,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { self.capacity = new_capacity; } else { const new_memory = try self.allocator.alignedAlloc(T, alignment, new_capacity); - mem.copy(T, new_memory, self.items); + @memcpy(new_memory[0..self.items.len], self.items); self.allocator.free(old_memory); self.items.ptr = new_memory.ptr; self.capacity = new_memory.len; @@ -600,7 +600,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ } const new_memory = try allocator.alignedAlloc(T, alignment, self.items.len); - mem.copy(T, new_memory, self.items); + @memcpy(new_memory, self.items); @memset(self.items, undefined); self.clearAndFree(allocator); return new_memory; @@ -651,7 +651,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ self.items.len += items.len; mem.copyBackwards(T, self.items[i + items.len .. self.items.len], self.items[i .. self.items.len - items.len]); - mem.copy(T, self.items[i .. i + items.len], items); + @memcpy(self.items[i..][0..items.len], items); } /// Replace range of elements `list[start..start+len]` with `new_items` @@ -720,7 +720,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ const new_len = old_len + items.len; assert(new_len <= self.capacity); self.items.len = new_len; - mem.copy(T, self.items[old_len..], items); + @memcpy(self.items[old_len..][0..items.len], items); } /// Append the slice of items to the list. Allocates more @@ -823,7 +823,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ }, }; - mem.copy(T, new_memory, self.items[0..new_len]); + @memcpy(new_memory, self.items[0..new_len]); allocator.free(old_memory); self.items = new_memory; self.capacity = new_memory.len; @@ -885,7 +885,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ self.capacity = new_capacity; } else { const new_memory = try allocator.alignedAlloc(T, alignment, new_capacity); - mem.copy(T, new_memory, self.items); + @memcpy(new_memory[0..self.items.len], self.items); allocator.free(old_memory); self.items.ptr = new_memory.ptr; self.capacity = new_memory.len; diff --git a/lib/std/base64.zig b/lib/std/base64.zig index d9bf7d4a07..758c7c8b58 100644 --- a/lib/std/base64.zig +++ b/lib/std/base64.zig @@ -309,11 +309,11 @@ test "base64 padding dest overflow" { const input = "foo"; var expect: [128]u8 = undefined; - std.mem.set(u8, &expect, 0); + @memset(&expect, 0); _ = url_safe.Encoder.encode(expect[0..url_safe.Encoder.calcSize(input.len)], input); var got: [128]u8 = undefined; - std.mem.set(u8, &got, 0); + @memset(&got, 0); _ = url_safe.Encoder.encode(&got, input); try std.testing.expectEqualSlices(u8, &expect, &got); diff --git a/lib/std/bit_set.zig b/lib/std/bit_set.zig index 292d099303..7a1f7feff8 100644 --- a/lib/std/bit_set.zig +++ b/lib/std/bit_set.zig @@ -738,7 +738,7 @@ pub const DynamicBitSetUnmanaged = struct { // fill in any new masks if (new_masks > old_masks) { const fill_value = std.math.boolMask(MaskInt, fill); - std.mem.set(MaskInt, self.masks[old_masks..new_masks], fill_value); + @memset(self.masks[old_masks..new_masks], fill_value); } } diff --git a/lib/std/bounded_array.zig b/lib/std/bounded_array.zig index 8a4c2db64b..be32093fe7 100644 --- a/lib/std/bounded_array.zig +++ b/lib/std/bounded_array.zig @@ -73,7 +73,7 @@ pub fn BoundedArrayAligned( /// Copy the content of an existing slice. pub fn fromSlice(m: []const T) error{Overflow}!Self { var list = try init(m.len); - std.mem.copy(T, list.slice(), m); + @memcpy(list.slice(), m); return list; } @@ -165,7 +165,7 @@ pub fn BoundedArrayAligned( try self.ensureUnusedCapacity(items.len); self.len += items.len; mem.copyBackwards(T, self.slice()[i + items.len .. self.len], self.constSlice()[i .. self.len - items.len]); - mem.copy(T, self.slice()[i .. i + items.len], items); + @memcpy(self.slice()[i..][0..items.len], items); } /// Replace range of elements `slice[start..start+len]` with `new_items`. @@ -181,14 +181,14 @@ pub fn BoundedArrayAligned( var range = self.slice()[start..after_range]; if (range.len == new_items.len) { - mem.copy(T, range, new_items); + @memcpy(range[0..new_items.len], new_items); } else if (range.len < new_items.len) { const first = new_items[0..range.len]; const rest = new_items[range.len..]; - mem.copy(T, range, first); + @memcpy(range[0..first.len], first); try self.insertSlice(after_range, rest); } else { - mem.copy(T, range, new_items); + @memcpy(range[0..new_items.len], new_items); const after_subrange = start + new_items.len; for (self.constSlice()[after_range..], 0..) |item, i| { self.slice()[after_subrange..][i] = item; @@ -243,9 +243,9 @@ pub fn BoundedArrayAligned( /// Append the slice of items to the slice, asserting the capacity is already /// enough to store the new items. pub fn appendSliceAssumeCapacity(self: *Self, items: []const T) void { - const oldlen = self.len; + const old_len = self.len; self.len += items.len; - mem.copy(T, self.slice()[oldlen..], items); + @memcpy(self.slice()[old_len..][0..items.len], items); } /// Append a value to the slice `n` times. @@ -253,7 +253,7 @@ pub fn BoundedArrayAligned( pub fn appendNTimes(self: *Self, value: T, n: usize) error{Overflow}!void { const old_len = self.len; try self.resize(old_len + n); - mem.set(T, self.slice()[old_len..self.len], value); + @memset(self.slice()[old_len..self.len], value); } /// Append a value to the slice `n` times. @@ -262,7 +262,7 @@ pub fn BoundedArrayAligned( const old_len = self.len; self.len += n; assert(self.len <= buffer_capacity); - mem.set(T, self.slice()[old_len..self.len], value); + @memset(self.slice()[old_len..self.len], value); } pub const Writer = if (T != u8) @@ -329,7 +329,7 @@ test "BoundedArray" { try testing.expectEqual(a.popOrNull(), 0); try testing.expectEqual(a.popOrNull(), null); var unused = a.unusedCapacitySlice(); - mem.set(u8, unused[0..8], 2); + @memset(unused[0..8], 2); unused[8] = 3; unused[9] = 4; try testing.expectEqual(unused.len, a.capacity()); diff --git a/lib/std/buf_set.zig b/lib/std/buf_set.zig index 9d6d7ed1e6..90ee86e12d 100644 --- a/lib/std/buf_set.zig +++ b/lib/std/buf_set.zig @@ -97,7 +97,7 @@ pub const BufSet = struct { fn copy(self: *const BufSet, value: []const u8) ![]const u8 { const result = try self.hash_map.allocator.alloc(u8, value.len); - mem.copy(u8, result, value); + @memcpy(result, value); return result; } }; diff --git a/lib/std/child_process.zig b/lib/std/child_process.zig index bd67d344c7..f5ca72ed39 100644 --- a/lib/std/child_process.zig +++ b/lib/std/child_process.zig @@ -259,7 +259,7 @@ pub const ChildProcess = struct { fn fifoToOwnedArrayList(fifo: *std.io.PollFifo) std.ArrayList(u8) { if (fifo.head > 0) { - std.mem.copy(u8, fifo.buf[0..fifo.count], fifo.buf[fifo.head .. fifo.head + fifo.count]); + @memcpy(fifo.buf[0..fifo.count], fifo.buf[fifo.head..][0..fifo.count]); } const result = std.ArrayList(u8){ .items = fifo.buf[0..fifo.count], @@ -1436,9 +1436,9 @@ pub fn createNullDelimitedEnvMap(arena: mem.Allocator, env_map: *const EnvMap) ! var i: usize = 0; while (it.next()) |pair| : (i += 1) { const env_buf = try arena.allocSentinel(u8, pair.key_ptr.len + pair.value_ptr.len + 1, 0); - mem.copy(u8, env_buf, pair.key_ptr.*); + @memcpy(env_buf[0..pair.key_ptr.len], pair.key_ptr.*); env_buf[pair.key_ptr.len] = '='; - mem.copy(u8, env_buf[pair.key_ptr.len + 1 ..], pair.value_ptr.*); + @memcpy(env_buf[pair.key_ptr.len + 1 ..][0..pair.value_ptr.len], pair.value_ptr.*); envp_buf[i] = env_buf.ptr; } assert(i == envp_count); diff --git a/lib/std/compress/deflate/compressor.zig b/lib/std/compress/deflate/compressor.zig index 6c21875941..4fceb096a7 100644 --- a/lib/std/compress/deflate/compressor.zig +++ b/lib/std/compress/deflate/compressor.zig @@ -543,7 +543,7 @@ pub fn Compressor(comptime WriterType: anytype) type { self.hash_offset = 1; self.tokens = try self.allocator.alloc(token.Token, max_flate_block_tokens); self.tokens_count = 0; - mem.set(token.Token, self.tokens, 0); + @memset(self.tokens, 0); self.length = min_match_length - 1; self.offset = 0; self.byte_available = false; @@ -841,9 +841,9 @@ pub fn Compressor(comptime WriterType: anytype) type { s.hash_head = try allocator.alloc(u32, hash_size); s.hash_prev = try allocator.alloc(u32, window_size); s.hash_match = try allocator.alloc(u32, max_match_length - 1); - mem.set(u32, s.hash_head, 0); - mem.set(u32, s.hash_prev, 0); - mem.set(u32, s.hash_match, 0); + @memset(s.hash_head, 0); + @memset(s.hash_prev, 0); + @memset(s.hash_match, 0); switch (options.level) { .no_compression => { @@ -936,8 +936,8 @@ pub fn Compressor(comptime WriterType: anytype) type { .best_compression, => { self.chain_head = 0; - mem.set(u32, self.hash_head, 0); - mem.set(u32, self.hash_prev, 0); + @memset(self.hash_head, 0); + @memset(self.hash_prev, 0); self.hash_offset = 1; self.index = 0; self.window_end = 0; diff --git a/lib/std/compress/deflate/decompressor.zig b/lib/std/compress/deflate/decompressor.zig index e5cfbb0f6b..5c0c8b1350 100644 --- a/lib/std/compress/deflate/decompressor.zig +++ b/lib/std/compress/deflate/decompressor.zig @@ -159,7 +159,7 @@ const HuffmanDecoder = struct { if (sanity) { // initialize to a known invalid chunk code (0) to see if we overwrite // this value later on - mem.set(u16, self.links[off], 0); + @memset(self.links[off], 0); } try self.sub_chunks.append(off); } diff --git a/lib/std/compress/deflate/deflate_fast.zig b/lib/std/compress/deflate/deflate_fast.zig index 2009af2611..2d78387d72 100644 --- a/lib/std/compress/deflate/deflate_fast.zig +++ b/lib/std/compress/deflate/deflate_fast.zig @@ -566,11 +566,11 @@ test "best speed match 2/2" { for (cases) |c| { var previous = try testing.allocator.alloc(u8, c.previous); defer testing.allocator.free(previous); - mem.set(u8, previous, 0); + @memset(previous, 0); var current = try testing.allocator.alloc(u8, c.current); defer testing.allocator.free(current); - mem.set(u8, current, 0); + @memset(current, 0); var e = DeflateFast{ .prev = previous, diff --git a/lib/std/compress/lzma.zig b/lib/std/compress/lzma.zig index 8bb8c19da1..ff05bc1c8b 100644 --- a/lib/std/compress/lzma.zig +++ b/lib/std/compress/lzma.zig @@ -75,9 +75,9 @@ pub fn Decompress(comptime ReaderType: type) type { } } const input = self.to_read.items; - const n = math.min(input.len, output.len); - mem.copy(u8, output[0..n], input[0..n]); - mem.copy(u8, input, input[n..]); + const n = @min(input.len, output.len); + @memcpy(output[0..n], input[0..n]); + @memcpy(input[0 .. input.len - n], input[n..]); self.to_read.shrinkRetainingCapacity(input.len - n); return n; } diff --git a/lib/std/compress/lzma/decode/rangecoder.zig b/lib/std/compress/lzma/decode/rangecoder.zig index 5df10be060..5a2f309fe4 100644 --- a/lib/std/compress/lzma/decode/rangecoder.zig +++ b/lib/std/compress/lzma/decode/rangecoder.zig @@ -143,7 +143,7 @@ pub fn BitTree(comptime num_bits: usize) type { } pub fn reset(self: *Self) void { - mem.set(u16, &self.probs, 0x400); + @memset(&self.probs, 0x400); } }; } diff --git a/lib/std/compress/lzma/vec2d.zig b/lib/std/compress/lzma/vec2d.zig index 1372d3592c..1ea3d1e8d5 100644 --- a/lib/std/compress/lzma/vec2d.zig +++ b/lib/std/compress/lzma/vec2d.zig @@ -13,7 +13,7 @@ pub fn Vec2D(comptime T: type) type { pub fn init(allocator: Allocator, value: T, size: struct { usize, usize }) !Self { const len = try math.mul(usize, size[0], size[1]); const data = try allocator.alloc(T, len); - mem.set(T, data, value); + @memset(data, value); return Self{ .data = data, .cols = size[1], @@ -26,7 +26,7 @@ pub fn Vec2D(comptime T: type) type { } pub fn fill(self: *Self, value: T) void { - mem.set(T, self.data, value); + @memset(self.data, value); } inline fn _get(self: Self, row: usize) ![]T { diff --git a/lib/std/compress/zstandard/decode/block.zig b/lib/std/compress/zstandard/decode/block.zig index 4b7353f63c..e2042650c6 100644 --- a/lib/std/compress/zstandard/decode/block.zig +++ b/lib/std/compress/zstandard/decode/block.zig @@ -293,10 +293,10 @@ pub const DecodeState = struct { try self.decodeLiteralsSlice(dest[write_pos..], sequence.literal_length); const copy_start = write_pos + sequence.literal_length - sequence.offset; - const copy_end = copy_start + sequence.match_length; - // NOTE: we ignore the usage message for std.mem.copy and copy with dest.ptr >= src.ptr - // to allow repeats - std.mem.copy(u8, dest[write_pos + sequence.literal_length ..], dest[copy_start..copy_end]); + for ( + dest[write_pos + sequence.literal_length ..][0..sequence.match_length], + dest[copy_start..][0..sequence.match_length], + ) |*d, s| d.* = s; self.written_count += sequence.match_length; } @@ -311,7 +311,6 @@ pub const DecodeState = struct { try self.decodeLiteralsRingBuffer(dest, sequence.literal_length); const copy_start = dest.write_index + dest.data.len - sequence.offset; const copy_slice = dest.sliceAt(copy_start, sequence.match_length); - // TODO: would std.mem.copy and figuring out dest slice be better/faster? for (copy_slice.first) |b| dest.writeAssumeCapacity(b); for (copy_slice.second) |b| dest.writeAssumeCapacity(b); self.written_count += sequence.match_length; @@ -444,9 +443,8 @@ pub const DecodeState = struct { switch (self.literal_header.block_type) { .raw => { - const literals_end = self.literal_written_count + len; - const literal_data = self.literal_streams.one[self.literal_written_count..literals_end]; - std.mem.copy(u8, dest, literal_data); + const literal_data = self.literal_streams.one[self.literal_written_count..][0..len]; + @memcpy(dest[0..len], literal_data); self.literal_written_count += len; self.written_count += len; }, @@ -615,8 +613,7 @@ pub fn decodeBlock( .raw => { if (src.len < block_size) return error.MalformedBlockSize; if (dest[written_count..].len < block_size) return error.DestTooSmall; - const data = src[0..block_size]; - std.mem.copy(u8, dest[written_count..], data); + @memcpy(dest[written_count..][0..block_size], src[0..block_size]); consumed_count.* += block_size; decode_state.written_count += block_size; return block_size; diff --git a/lib/std/crypto/25519/ed25519.zig b/lib/std/crypto/25519/ed25519.zig index 66dcf3705b..6b9c1028b6 100644 --- a/lib/std/crypto/25519/ed25519.zig +++ b/lib/std/crypto/25519/ed25519.zig @@ -79,8 +79,8 @@ pub const Ed25519 = struct { const r_bytes = r.toBytes(); var t: [64]u8 = undefined; - mem.copy(u8, t[0..32], &r_bytes); - mem.copy(u8, t[32..], &public_key.bytes); + t[0..32].* = r_bytes; + t[32..].* = public_key.bytes; var h = Sha512.init(.{}); h.update(&t); @@ -200,8 +200,8 @@ pub const Ed25519 = struct { /// Return the raw signature (r, s) in little-endian format. pub fn toBytes(self: Signature) [encoded_length]u8 { var bytes: [encoded_length]u8 = undefined; - mem.copy(u8, bytes[0 .. encoded_length / 2], &self.r); - mem.copy(u8, bytes[encoded_length / 2 ..], &self.s); + bytes[0 .. encoded_length / 2].* = self.r; + bytes[encoded_length / 2 ..].* = self.s; return bytes; } @@ -260,8 +260,8 @@ pub const Ed25519 = struct { const pk_p = Curve.basePoint.clampedMul(az[0..32].*) catch return error.IdentityElement; const pk_bytes = pk_p.toBytes(); var sk_bytes: [SecretKey.encoded_length]u8 = undefined; - mem.copy(u8, &sk_bytes, &ss); - mem.copy(u8, sk_bytes[seed_length..], &pk_bytes); + sk_bytes[0..ss.len].* = ss; + sk_bytes[seed_length..].* = pk_bytes; return KeyPair{ .public_key = PublicKey.fromBytes(pk_bytes) catch unreachable, .secret_key = try SecretKey.fromBytes(sk_bytes), @@ -373,7 +373,7 @@ pub const Ed25519 = struct { var z_batch: [count]Curve.scalar.CompressedScalar = undefined; for (&z_batch) |*z| { crypto.random.bytes(z[0..16]); - mem.set(u8, z[16..], 0); + @memset(z[16..], 0); } var zs_sum = Curve.scalar.zero; @@ -444,8 +444,8 @@ pub const Ed25519 = struct { }; var prefix: [64]u8 = undefined; - mem.copy(u8, prefix[0..32], h[32..64]); - mem.copy(u8, prefix[32..64], blind_h[32..64]); + prefix[0..32].* = h[32..64].*; + prefix[32..64].* = blind_h[32..64].*; const blind_secret_key = BlindSecretKey{ .prefix = prefix, diff --git a/lib/std/crypto/25519/edwards25519.zig b/lib/std/crypto/25519/edwards25519.zig index df4f8467f9..a8ca8e2fb6 100644 --- a/lib/std/crypto/25519/edwards25519.zig +++ b/lib/std/crypto/25519/edwards25519.zig @@ -306,7 +306,7 @@ pub const Edwards25519 = struct { var pcs: [count][9]Edwards25519 = undefined; var bpc: [9]Edwards25519 = undefined; - mem.copy(Edwards25519, bpc[0..], basePointPc[0..bpc.len]); + @memcpy(&bpc, basePointPc[0..bpc.len]); for (ps, 0..) |p, i| { if (p.is_base) { @@ -439,7 +439,7 @@ pub const Edwards25519 = struct { var u: [n * H.digest_length]u8 = undefined; var i: usize = 0; while (i < n * H.digest_length) : (i += H.digest_length) { - mem.copy(u8, u[i..][0..H.digest_length], u_0[0..]); + u[i..][0..H.digest_length].* = u_0; var j: usize = 0; while (i > 0 and j < H.digest_length) : (j += 1) { u[i + j] ^= u[i + j - H.digest_length]; @@ -455,8 +455,8 @@ pub const Edwards25519 = struct { var px: [n]Edwards25519 = undefined; i = 0; while (i < n) : (i += 1) { - mem.set(u8, u_0[0 .. H.digest_length - h_l], 0); - mem.copy(u8, u_0[H.digest_length - h_l ..][0..h_l], u[i * h_l ..][0..h_l]); + @memset(u_0[0 .. H.digest_length - h_l], 0); + u_0[H.digest_length - h_l ..][0..h_l].* = u[i * h_l ..][0..h_l].*; px[i] = fromHash(u_0); } return px; diff --git a/lib/std/crypto/25519/scalar.zig b/lib/std/crypto/25519/scalar.zig index ff2e6aff80..fd6d42aebe 100644 --- a/lib/std/crypto/25519/scalar.zig +++ b/lib/std/crypto/25519/scalar.zig @@ -83,8 +83,8 @@ pub fn add(a: CompressedScalar, b: CompressedScalar) CompressedScalar { pub fn neg(s: CompressedScalar) CompressedScalar { const fs: [64]u8 = field_order_s ++ [_]u8{0} ** 32; var sx: [64]u8 = undefined; - mem.copy(u8, sx[0..32], s[0..]); - mem.set(u8, sx[32..], 0); + sx[0..32].* = s; + @memset(sx[32..], 0); var carry: u32 = 0; var i: usize = 0; while (i < 64) : (i += 1) { @@ -593,7 +593,7 @@ const ScalarDouble = struct { limbs[i] = mem.readIntLittle(u64, bytes[i * 7 ..][0..8]) & 0xffffffffffffff; } limbs[i] = @as(u64, mem.readIntLittle(u32, bytes[i * 7 ..][0..4])); - mem.set(u64, limbs[5..], 0); + @memset(limbs[5..], 0); return ScalarDouble{ .limbs = limbs }; } diff --git a/lib/std/crypto/25519/x25519.zig b/lib/std/crypto/25519/x25519.zig index 22bcf00136..b746a51968 100644 --- a/lib/std/crypto/25519/x25519.zig +++ b/lib/std/crypto/25519/x25519.zig @@ -37,7 +37,7 @@ pub const X25519 = struct { break :sk random_seed; }; var kp: KeyPair = undefined; - mem.copy(u8, &kp.secret_key, sk[0..]); + kp.secret_key = sk; kp.public_key = try X25519.recoverPublicKey(sk); return kp; } @@ -120,8 +120,8 @@ test "x25519 rfc7748 one iteration" { var i: usize = 0; while (i < 1) : (i += 1) { const output = try X25519.scalarmult(k, u); - mem.copy(u8, u[0..], k[0..]); - mem.copy(u8, k[0..], output[0..]); + u = k; + k = output; } try std.testing.expectEqual(k, expected_output); @@ -142,8 +142,8 @@ test "x25519 rfc7748 1,000 iterations" { var i: usize = 0; while (i < 1000) : (i += 1) { const output = try X25519.scalarmult(&k, &u); - mem.copy(u8, u[0..], k[0..]); - mem.copy(u8, k[0..], output[0..]); + u = k; + k = output; } try std.testing.expectEqual(k, expected_output); @@ -163,8 +163,8 @@ test "x25519 rfc7748 1,000,000 iterations" { var i: usize = 0; while (i < 1000000) : (i += 1) { const output = try X25519.scalarmult(&k, &u); - mem.copy(u8, u[0..], k[0..]); - mem.copy(u8, k[0..], output[0..]); + u = k; + k = output; } try std.testing.expectEqual(k[0..], expected_output); diff --git a/lib/std/crypto/Certificate.zig b/lib/std/crypto/Certificate.zig index 0caffba363..ec4766322c 100644 --- a/lib/std/crypto/Certificate.zig +++ b/lib/std/crypto/Certificate.zig @@ -928,7 +928,7 @@ pub const rsa = struct { pub const PSSSignature = struct { pub fn fromBytes(comptime modulus_len: usize, msg: []const u8) [modulus_len]u8 { var result = [1]u8{0} ** modulus_len; - std.mem.copy(u8, &result, msg); + std.mem.copyForwards(u8, &result, msg); return result; } @@ -1025,9 +1025,9 @@ pub const rsa = struct { // initial zero octets. var m_p = try allocator.alloc(u8, 8 + Hash.digest_length + sLen); defer allocator.free(m_p); - std.mem.copy(u8, m_p, &([_]u8{0} ** 8)); - std.mem.copy(u8, m_p[8..], &mHash); - std.mem.copy(u8, m_p[(8 + Hash.digest_length)..], salt); + std.mem.copyForwards(u8, m_p, &([_]u8{0} ** 8)); + std.mem.copyForwards(u8, m_p[8..], &mHash); + std.mem.copyForwards(u8, m_p[(8 + Hash.digest_length)..], salt); // 13. Let H' = Hash(M'), an octet string of length hLen. var h_p: [Hash.digest_length]u8 = undefined; @@ -1047,7 +1047,7 @@ pub const rsa = struct { var hash = try allocator.alloc(u8, seed.len + c.len); defer allocator.free(hash); - std.mem.copy(u8, hash, seed); + std.mem.copyForwards(u8, hash, seed); var hashed: [Hash.digest_length]u8 = undefined; while (idx < len) { @@ -1056,10 +1056,10 @@ pub const rsa = struct { c[2] = @intCast(u8, (counter >> 8) & 0xFF); c[3] = @intCast(u8, counter & 0xFF); - std.mem.copy(u8, hash[seed.len..], &c); + std.mem.copyForwards(u8, hash[seed.len..], &c); Hash.hash(hash, &hashed, .{}); - std.mem.copy(u8, out[idx..], &hashed); + std.mem.copyForwards(u8, out[idx..], &hashed); idx += hashed.len; counter += 1; diff --git a/lib/std/crypto/aegis.zig b/lib/std/crypto/aegis.zig index d7305b444a..2d37bacc3a 100644 --- a/lib/std/crypto/aegis.zig +++ b/lib/std/crypto/aegis.zig @@ -152,8 +152,8 @@ fn Aegis128LGeneric(comptime tag_bits: u9) type { state.absorb(ad[i..][0..32]); } if (ad.len % 32 != 0) { - mem.set(u8, src[0..], 0); - mem.copy(u8, src[0 .. ad.len % 32], ad[i .. i + ad.len % 32]); + @memset(src[0..], 0); + @memcpy(src[0 .. ad.len % 32], ad[i..][0 .. ad.len % 32]); state.absorb(&src); } i = 0; @@ -161,10 +161,10 @@ fn Aegis128LGeneric(comptime tag_bits: u9) type { state.enc(c[i..][0..32], m[i..][0..32]); } if (m.len % 32 != 0) { - mem.set(u8, src[0..], 0); - mem.copy(u8, src[0 .. m.len % 32], m[i .. i + m.len % 32]); + @memset(src[0..], 0); + @memcpy(src[0 .. m.len % 32], m[i..][0 .. m.len % 32]); state.enc(&dst, &src); - mem.copy(u8, c[i .. i + m.len % 32], dst[0 .. m.len % 32]); + @memcpy(c[i..][0 .. m.len % 32], dst[0 .. m.len % 32]); } tag.* = state.mac(tag_bits, ad.len, m.len); } @@ -185,8 +185,8 @@ fn Aegis128LGeneric(comptime tag_bits: u9) type { state.absorb(ad[i..][0..32]); } if (ad.len % 32 != 0) { - mem.set(u8, src[0..], 0); - mem.copy(u8, src[0 .. ad.len % 32], ad[i .. i + ad.len % 32]); + @memset(src[0..], 0); + @memcpy(src[0 .. ad.len % 32], ad[i..][0 .. ad.len % 32]); state.absorb(&src); } i = 0; @@ -194,11 +194,11 @@ fn Aegis128LGeneric(comptime tag_bits: u9) type { state.dec(m[i..][0..32], c[i..][0..32]); } if (m.len % 32 != 0) { - mem.set(u8, src[0..], 0); - mem.copy(u8, src[0 .. m.len % 32], c[i .. i + m.len % 32]); + @memset(src[0..], 0); + @memcpy(src[0 .. m.len % 32], c[i..][0 .. m.len % 32]); state.dec(&dst, &src); - mem.copy(u8, m[i .. i + m.len % 32], dst[0 .. m.len % 32]); - mem.set(u8, dst[0 .. m.len % 32], 0); + @memcpy(m[i..][0 .. m.len % 32], dst[0 .. m.len % 32]); + @memset(dst[0 .. m.len % 32], 0); const blocks = &state.blocks; blocks[0] = blocks[0].xorBlocks(AesBlock.fromBytes(dst[0..16])); blocks[4] = blocks[4].xorBlocks(AesBlock.fromBytes(dst[16..32])); @@ -334,8 +334,8 @@ fn Aegis256Generic(comptime tag_bits: u9) type { state.enc(&dst, ad[i..][0..16]); } if (ad.len % 16 != 0) { - mem.set(u8, src[0..], 0); - mem.copy(u8, src[0 .. ad.len % 16], ad[i .. i + ad.len % 16]); + @memset(src[0..], 0); + @memcpy(src[0 .. ad.len % 16], ad[i..][0 .. ad.len % 16]); state.enc(&dst, &src); } i = 0; @@ -343,10 +343,10 @@ fn Aegis256Generic(comptime tag_bits: u9) type { state.enc(c[i..][0..16], m[i..][0..16]); } if (m.len % 16 != 0) { - mem.set(u8, src[0..], 0); - mem.copy(u8, src[0 .. m.len % 16], m[i .. i + m.len % 16]); + @memset(src[0..], 0); + @memcpy(src[0 .. m.len % 16], m[i..][0 .. m.len % 16]); state.enc(&dst, &src); - mem.copy(u8, c[i .. i + m.len % 16], dst[0 .. m.len % 16]); + @memcpy(c[i..][0 .. m.len % 16], dst[0 .. m.len % 16]); } tag.* = state.mac(tag_bits, ad.len, m.len); } @@ -367,8 +367,8 @@ fn Aegis256Generic(comptime tag_bits: u9) type { state.enc(&dst, ad[i..][0..16]); } if (ad.len % 16 != 0) { - mem.set(u8, src[0..], 0); - mem.copy(u8, src[0 .. ad.len % 16], ad[i .. i + ad.len % 16]); + @memset(src[0..], 0); + @memcpy(src[0 .. ad.len % 16], ad[i..][0 .. ad.len % 16]); state.enc(&dst, &src); } i = 0; @@ -376,11 +376,11 @@ fn Aegis256Generic(comptime tag_bits: u9) type { state.dec(m[i..][0..16], c[i..][0..16]); } if (m.len % 16 != 0) { - mem.set(u8, src[0..], 0); - mem.copy(u8, src[0 .. m.len % 16], c[i .. i + m.len % 16]); + @memset(src[0..], 0); + @memcpy(src[0 .. m.len % 16], c[i..][0 .. m.len % 16]); state.dec(&dst, &src); - mem.copy(u8, m[i .. i + m.len % 16], dst[0 .. m.len % 16]); - mem.set(u8, dst[0 .. m.len % 16], 0); + @memcpy(m[i..][0 .. m.len % 16], dst[0 .. m.len % 16]); + @memset(dst[0 .. m.len % 16], 0); const blocks = &state.blocks; blocks[0] = blocks[0].xorBlocks(AesBlock.fromBytes(&dst)); } @@ -457,7 +457,7 @@ fn AegisMac(comptime T: type) type { self.msg_len += b.len; const len_partial = @min(b.len, block_length - self.off); - mem.copy(u8, self.buf[self.off..][0..len_partial], b[0..len_partial]); + @memcpy(self.buf[self.off..][0..len_partial], b[0..len_partial]); self.off += len_partial; if (self.off < block_length) { return; @@ -470,7 +470,7 @@ fn AegisMac(comptime T: type) type { self.state.absorb(b[i..][0..block_length]); } if (i != b.len) { - mem.copy(u8, self.buf[0..], b[i..]); + @memcpy(self.buf[0..], b[i..]); self.off = b.len - i; } } @@ -479,7 +479,7 @@ fn AegisMac(comptime T: type) type { pub fn final(self: *Self, out: *[mac_length]u8) void { if (self.off > 0) { var pad = [_]u8{0} ** block_length; - mem.copy(u8, pad[0..], self.buf[0..self.off]); + @memcpy(pad[0..self.off], self.buf[0..self.off]); self.state.absorb(&pad); } out.* = self.state.mac(T.tag_length * 8, self.msg_len, 0); diff --git a/lib/std/crypto/aes_gcm.zig b/lib/std/crypto/aes_gcm.zig index 4ec53371e6..660073d3ae 100644 --- a/lib/std/crypto/aes_gcm.zig +++ b/lib/std/crypto/aes_gcm.zig @@ -31,7 +31,7 @@ fn AesGcm(comptime Aes: anytype) type { var t: [16]u8 = undefined; var j: [16]u8 = undefined; - mem.copy(u8, j[0..nonce_length], npub[0..]); + j[0..nonce_length].* = npub; mem.writeIntBig(u32, j[nonce_length..][0..4], 1); aes.encrypt(&t, &j); @@ -64,7 +64,7 @@ fn AesGcm(comptime Aes: anytype) type { var t: [16]u8 = undefined; var j: [16]u8 = undefined; - mem.copy(u8, j[0..nonce_length], npub[0..]); + j[0..nonce_length].* = npub; mem.writeIntBig(u32, j[nonce_length..][0..4], 1); aes.encrypt(&t, &j); diff --git a/lib/std/crypto/aes_ocb.zig b/lib/std/crypto/aes_ocb.zig index 83e33e5fca..6d5ce3779a 100644 --- a/lib/std/crypto/aes_ocb.zig +++ b/lib/std/crypto/aes_ocb.zig @@ -75,7 +75,7 @@ fn AesOcb(comptime Aes: anytype) type { if (leftover > 0) { xorWith(&offset, lx.star); var padded = [_]u8{0} ** 16; - mem.copy(u8, padded[0..leftover], a[i * 16 ..][0..leftover]); + @memcpy(padded[0..leftover], a[i * 16 ..][0..leftover]); padded[leftover] = 1; var e = xorBlocks(offset, padded); aes_enc_ctx.encrypt(&e, &e); @@ -88,7 +88,7 @@ fn AesOcb(comptime Aes: anytype) type { var nx = [_]u8{0} ** 16; nx[0] = @intCast(u8, @truncate(u7, tag_length * 8) << 1); nx[16 - nonce_length - 1] = 1; - mem.copy(u8, nx[16 - nonce_length ..], &npub); + nx[nx.len - nonce_length ..].* = npub; const bottom = @truncate(u6, nx[15]); nx[15] &= 0xc0; @@ -132,14 +132,14 @@ fn AesOcb(comptime Aes: anytype) type { xorWith(&offset, lt[@ctz(i + 1 + j)]); offsets[j] = offset; const p = m[(i + j) * 16 ..][0..16].*; - mem.copy(u8, es[j * 16 ..][0..16], &xorBlocks(p, offsets[j])); + es[j * 16 ..][0..16].* = xorBlocks(p, offsets[j]); xorWith(&sum, p); } aes_enc_ctx.encryptWide(wb, &es, &es); j = 0; while (j < wb) : (j += 1) { const e = es[j * 16 ..][0..16].*; - mem.copy(u8, c[(i + j) * 16 ..][0..16], &xorBlocks(e, offsets[j])); + c[(i + j) * 16 ..][0..16].* = xorBlocks(e, offsets[j]); } } while (i < full_blocks) : (i += 1) { @@ -147,7 +147,7 @@ fn AesOcb(comptime Aes: anytype) type { const p = m[i * 16 ..][0..16].*; var e = xorBlocks(p, offset); aes_enc_ctx.encrypt(&e, &e); - mem.copy(u8, c[i * 16 ..][0..16], &xorBlocks(e, offset)); + c[i * 16 ..][0..16].* = xorBlocks(e, offset); xorWith(&sum, p); } const leftover = m.len % 16; @@ -159,7 +159,7 @@ fn AesOcb(comptime Aes: anytype) type { c[i * 16 + j] = pad[j] ^ x; } var e = [_]u8{0} ** 16; - mem.copy(u8, e[0..leftover], m[i * 16 ..][0..leftover]); + @memcpy(e[0..leftover], m[i * 16 ..][0..leftover]); e[leftover] = 0x80; xorWith(&sum, e); } @@ -196,13 +196,13 @@ fn AesOcb(comptime Aes: anytype) type { xorWith(&offset, lt[@ctz(i + 1 + j)]); offsets[j] = offset; const q = c[(i + j) * 16 ..][0..16].*; - mem.copy(u8, es[j * 16 ..][0..16], &xorBlocks(q, offsets[j])); + es[j * 16 ..][0..16].* = xorBlocks(q, offsets[j]); } aes_dec_ctx.decryptWide(wb, &es, &es); j = 0; while (j < wb) : (j += 1) { const p = xorBlocks(es[j * 16 ..][0..16].*, offsets[j]); - mem.copy(u8, m[(i + j) * 16 ..][0..16], &p); + m[(i + j) * 16 ..][0..16].* = p; xorWith(&sum, p); } } @@ -212,7 +212,7 @@ fn AesOcb(comptime Aes: anytype) type { var e = xorBlocks(q, offset); aes_dec_ctx.decrypt(&e, &e); const p = xorBlocks(e, offset); - mem.copy(u8, m[i * 16 ..][0..16], &p); + m[i * 16 ..][0..16].* = p; xorWith(&sum, p); } const leftover = m.len % 16; @@ -224,7 +224,7 @@ fn AesOcb(comptime Aes: anytype) type { m[i * 16 + j] = pad[j] ^ x; } var e = [_]u8{0} ** 16; - mem.copy(u8, e[0..leftover], m[i * 16 ..][0..leftover]); + @memcpy(e[0..leftover], m[i * 16 ..][0..leftover]); e[leftover] = 0x80; xorWith(&sum, e); } diff --git a/lib/std/crypto/argon2.zig b/lib/std/crypto/argon2.zig index 0112e81c6a..ad8be0c778 100644 --- a/lib/std/crypto/argon2.zig +++ b/lib/std/crypto/argon2.zig @@ -494,7 +494,7 @@ pub fn kdf( if (params.t < 1 or params.p < 1) return KdfError.WeakParameters; var h0 = initHash(password, salt, params, derived_key.len, mode); - const memory = math.max( + const memory = @max( params.m / (sync_points * params.p) * (sync_points * params.p), 2 * sync_points * params.p, ); @@ -877,7 +877,7 @@ test "kdf" { .hash = "1640b932f4b60e272f5d2207b9a9c626ffa1bd88d2349016", }, }; - inline for (test_vectors) |v| { + for (test_vectors) |v| { var want: [24]u8 = undefined; _ = try std.fmt.hexToBytes(&want, v.hash); diff --git a/lib/std/crypto/ascon.zig b/lib/std/crypto/ascon.zig index f37d9acea5..ae4bb57d29 100644 --- a/lib/std/crypto/ascon.zig +++ b/lib/std/crypto/ascon.zig @@ -34,7 +34,7 @@ pub fn State(comptime endian: builtin.Endian) type { /// Initialize the state from a slice of bytes. pub fn init(initial_state: [block_bytes]u8) Self { var state = Self{ .st = undefined }; - mem.copy(u8, state.asBytes(), &initial_state); + @memcpy(state.asBytes(), &initial_state); state.endianSwap(); return state; } @@ -87,7 +87,7 @@ pub fn State(comptime endian: builtin.Endian) type { } if (i < bytes.len) { var padded = [_]u8{0} ** 8; - mem.copy(u8, padded[0 .. bytes.len - i], bytes[i..]); + @memcpy(padded[0 .. bytes.len - i], bytes[i..]); self.st[i / 8] = mem.readInt(u64, padded[0..], endian); } } @@ -109,7 +109,7 @@ pub fn State(comptime endian: builtin.Endian) type { } if (i < bytes.len) { var padded = [_]u8{0} ** 8; - mem.copy(u8, padded[0 .. bytes.len - i], bytes[i..]); + @memcpy(padded[0 .. bytes.len - i], bytes[i..]); self.st[i / 8] ^= mem.readInt(u64, padded[0..], endian); } } @@ -123,7 +123,7 @@ pub fn State(comptime endian: builtin.Endian) type { if (i < out.len) { var padded = [_]u8{0} ** 8; mem.writeInt(u64, padded[0..], self.st[i / 8], endian); - mem.copy(u8, out[i..], padded[0 .. out.len - i]); + @memcpy(out[i..], padded[0 .. out.len - i]); } } @@ -138,16 +138,16 @@ pub fn State(comptime endian: builtin.Endian) type { } if (i < in.len) { var padded = [_]u8{0} ** 8; - mem.copy(u8, padded[0 .. in.len - i], in[i..]); + @memcpy(padded[0 .. in.len - i], in[i..]); const x = mem.readIntNative(u64, &padded) ^ mem.nativeTo(u64, self.st[i / 8], endian); mem.writeIntNative(u64, &padded, x); - mem.copy(u8, out[i..], padded[0 .. in.len - i]); + @memcpy(out[i..], padded[0 .. in.len - i]); } } /// Set the words storing the bytes of a given range to zero. pub fn clear(self: *Self, from: usize, to: usize) void { - mem.set(u64, self.st[from / 8 .. (to + 7) / 8], 0); + @memset(self.st[from / 8 .. (to + 7) / 8], 0); } /// Clear the entire state, disabling compiler optimizations. diff --git a/lib/std/crypto/bcrypt.zig b/lib/std/crypto/bcrypt.zig index 2191ab0d9e..dda5f5e377 100644 --- a/lib/std/crypto/bcrypt.zig +++ b/lib/std/crypto/bcrypt.zig @@ -416,8 +416,8 @@ pub fn bcrypt( ) [dk_length]u8 { var state = State{}; var password_buf: [73]u8 = undefined; - const trimmed_len = math.min(password.len, password_buf.len - 1); - mem.copy(u8, password_buf[0..], password[0..trimmed_len]); + const trimmed_len = @min(password.len, password_buf.len - 1); + @memcpy(password_buf[0..trimmed_len], password[0..trimmed_len]); password_buf[trimmed_len] = 0; var passwordZ = password_buf[0 .. trimmed_len + 1]; state.expand(salt[0..], passwordZ); @@ -626,7 +626,7 @@ const CryptFormatHasher = struct { crypto.random.bytes(&salt); const hash = crypt_format.strHashInternal(password, salt, params); - mem.copy(u8, buf, &hash); + @memcpy(buf[0..hash.len], &hash); return buf[0..pwhash_str_length]; } diff --git a/lib/std/crypto/benchmark.zig b/lib/std/crypto/benchmark.zig index f512c513e7..696a9c3107 100644 --- a/lib/std/crypto/benchmark.zig +++ b/lib/std/crypto/benchmark.zig @@ -113,8 +113,8 @@ pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_c var i: usize = 0; while (i < exchange_count) : (i += 1) { const out = try DhKeyExchange.scalarmult(secret, public); - mem.copy(u8, secret[0..16], out[0..16]); - mem.copy(u8, public[0..16], out[16..32]); + secret[0..16].* = out[0..16].*; + public[0..16].* = out[16..32].*; mem.doNotOptimizeAway(&out); } } diff --git a/lib/std/crypto/blake2.zig b/lib/std/crypto/blake2.zig index 85c26ce599..316ea5e6b7 100644 --- a/lib/std/crypto/blake2.zig +++ b/lib/std/crypto/blake2.zig @@ -76,7 +76,7 @@ pub fn Blake2s(comptime out_bits: usize) type { comptime debug.assert(8 <= out_bits and out_bits <= 256); var d: Self = undefined; - mem.copy(u32, d.h[0..], iv[0..]); + d.h = iv; const key_len = if (options.key) |key| key.len else 0; // default parameters @@ -93,7 +93,7 @@ pub fn Blake2s(comptime out_bits: usize) type { d.h[7] ^= mem.readIntLittle(u32, context[4..8]); } if (key_len > 0) { - mem.set(u8, d.buf[key_len..], 0); + @memset(d.buf[key_len..], 0); d.update(options.key.?); d.buf_len = 64; } @@ -112,7 +112,7 @@ pub fn Blake2s(comptime out_bits: usize) type { // Partial buffer exists from previous update. Copy into buffer then hash. if (d.buf_len != 0 and d.buf_len + b.len > 64) { off += 64 - d.buf_len; - mem.copy(u8, d.buf[d.buf_len..], b[0..off]); + @memcpy(d.buf[d.buf_len..][0..off], b[0..off]); d.t += 64; d.round(d.buf[0..], false); d.buf_len = 0; @@ -125,16 +125,17 @@ pub fn Blake2s(comptime out_bits: usize) type { } // Copy any remainder for next pass. - mem.copy(u8, d.buf[d.buf_len..], b[off..]); - d.buf_len += @intCast(u8, b[off..].len); + const b_slice = b[off..]; + @memcpy(d.buf[d.buf_len..][0..b_slice.len], b_slice); + d.buf_len += @intCast(u8, b_slice.len); } pub fn final(d: *Self, out: *[digest_length]u8) void { - mem.set(u8, d.buf[d.buf_len..], 0); + @memset(d.buf[d.buf_len..], 0); d.t += d.buf_len; d.round(d.buf[0..], true); for (&d.h) |*x| x.* = mem.nativeToLittle(u32, x.*); - mem.copy(u8, out[0..], @ptrCast(*[digest_length]u8, &d.h)); + out.* = @ptrCast(*[digest_length]u8, &d.h).*; } fn round(d: *Self, b: *const [64]u8, last: bool) void { @@ -511,7 +512,7 @@ pub fn Blake2b(comptime out_bits: usize) type { comptime debug.assert(8 <= out_bits and out_bits <= 512); var d: Self = undefined; - mem.copy(u64, d.h[0..], iv[0..]); + d.h = iv; const key_len = if (options.key) |key| key.len else 0; // default parameters @@ -528,7 +529,7 @@ pub fn Blake2b(comptime out_bits: usize) type { d.h[7] ^= mem.readIntLittle(u64, context[8..16]); } if (key_len > 0) { - mem.set(u8, d.buf[key_len..], 0); + @memset(d.buf[key_len..], 0); d.update(options.key.?); d.buf_len = 128; } @@ -547,7 +548,7 @@ pub fn Blake2b(comptime out_bits: usize) type { // Partial buffer exists from previous update. Copy into buffer then hash. if (d.buf_len != 0 and d.buf_len + b.len > 128) { off += 128 - d.buf_len; - mem.copy(u8, d.buf[d.buf_len..], b[0..off]); + @memcpy(d.buf[d.buf_len..][0..off], b[0..off]); d.t += 128; d.round(d.buf[0..], false); d.buf_len = 0; @@ -560,16 +561,17 @@ pub fn Blake2b(comptime out_bits: usize) type { } // Copy any remainder for next pass. - mem.copy(u8, d.buf[d.buf_len..], b[off..]); - d.buf_len += @intCast(u8, b[off..].len); + const b_slice = b[off..]; + @memcpy(d.buf[d.buf_len..][0..b_slice.len], b_slice); + d.buf_len += @intCast(u8, b_slice.len); } pub fn final(d: *Self, out: *[digest_length]u8) void { - mem.set(u8, d.buf[d.buf_len..], 0); + @memset(d.buf[d.buf_len..], 0); d.t += d.buf_len; d.round(d.buf[0..], true); for (&d.h) |*x| x.* = mem.nativeToLittle(u64, x.*); - mem.copy(u8, out[0..], @ptrCast(*[digest_length]u8, &d.h)); + out.* = @ptrCast(*[digest_length]u8, &d.h).*; } fn round(d: *Self, b: *const [128]u8, last: bool) void { diff --git a/lib/std/crypto/blake3.zig b/lib/std/crypto/blake3.zig index 36d717387f..fb580fda13 100644 --- a/lib/std/crypto/blake3.zig +++ b/lib/std/crypto/blake3.zig @@ -253,7 +253,7 @@ const Output = struct { while (out_word_it.next()) |out_word| { var word_bytes: [4]u8 = undefined; mem.writeIntLittle(u32, &word_bytes, words[word_counter]); - mem.copy(u8, out_word, word_bytes[0..out_word.len]); + @memcpy(out_word, word_bytes[0..out_word.len]); word_counter += 1; } output_block_counter += 1; @@ -284,7 +284,7 @@ const ChunkState = struct { fn fillBlockBuf(self: *ChunkState, input: []const u8) []const u8 { const want = BLOCK_LEN - self.block_len; const take = math.min(want, input.len); - mem.copy(u8, self.block[self.block_len..][0..take], input[0..take]); + @memcpy(self.block[self.block_len..][0..take], input[0..take]); self.block_len += @truncate(u8, take); return input[take..]; } @@ -336,8 +336,8 @@ fn parentOutput( flags: u8, ) Output { var block_words: [16]u32 align(16) = undefined; - mem.copy(u32, block_words[0..8], left_child_cv[0..]); - mem.copy(u32, block_words[8..], right_child_cv[0..]); + block_words[0..8].* = left_child_cv; + block_words[8..].* = right_child_cv; return Output{ .input_chaining_value = key, .block_words = block_words, diff --git a/lib/std/crypto/chacha20.zig b/lib/std/crypto/chacha20.zig index aa0f148be9..bffc70f500 100644 --- a/lib/std/crypto/chacha20.zig +++ b/lib/std/crypto/chacha20.zig @@ -211,7 +211,7 @@ fn ChaChaVecImpl(comptime rounds_nb: usize) type { var buf: [64]u8 = undefined; hashToBytes(buf[0..], x); - mem.copy(u8, out[i..], buf[0 .. out.len - i]); + @memcpy(out[i..], buf[0 .. out.len - i]); } } @@ -372,7 +372,7 @@ fn ChaChaNonVecImpl(comptime rounds_nb: usize) type { var buf: [64]u8 = undefined; hashToBytes(buf[0..], x); - mem.copy(u8, out[i..], buf[0 .. out.len - i]); + @memcpy(out[i..], buf[0 .. out.len - i]); } } @@ -413,8 +413,8 @@ fn keyToWords(key: [32]u8) [8]u32 { fn extend(key: [32]u8, nonce: [24]u8, comptime rounds_nb: usize) struct { key: [32]u8, nonce: [12]u8 } { var subnonce: [12]u8 = undefined; - mem.set(u8, subnonce[0..4], 0); - mem.copy(u8, subnonce[4..], nonce[16..24]); + @memset(subnonce[0..4], 0); + subnonce[4..].* = nonce[16..24].*; return .{ .key = ChaChaImpl(rounds_nb).hchacha20(nonce[0..16].*, key), .nonce = subnonce, diff --git a/lib/std/crypto/ecdsa.zig b/lib/std/crypto/ecdsa.zig index 37ae57a7e6..e552af2e26 100644 --- a/lib/std/crypto/ecdsa.zig +++ b/lib/std/crypto/ecdsa.zig @@ -102,8 +102,8 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { /// Return the raw signature (r, s) in big-endian format. pub fn toBytes(self: Signature) [encoded_length]u8 { var bytes: [encoded_length]u8 = undefined; - mem.copy(u8, bytes[0 .. encoded_length / 2], &self.r); - mem.copy(u8, bytes[encoded_length / 2 ..], &self.s); + @memcpy(bytes[0 .. encoded_length / 2], &self.r); + @memcpy(bytes[encoded_length / 2 ..], &self.s); return bytes; } @@ -325,11 +325,11 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { fn reduceToScalar(comptime unreduced_len: usize, s: [unreduced_len]u8) Curve.scalar.Scalar { if (unreduced_len >= 48) { var xs = [_]u8{0} ** 64; - mem.copy(u8, xs[xs.len - s.len ..], s[0..]); + @memcpy(xs[xs.len - s.len ..], s[0..]); return Curve.scalar.Scalar.fromBytes64(xs, .Big); } var xs = [_]u8{0} ** 48; - mem.copy(u8, xs[xs.len - s.len ..], s[0..]); + @memcpy(xs[xs.len - s.len ..], s[0..]); return Curve.scalar.Scalar.fromBytes48(xs, .Big); } @@ -345,14 +345,13 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { const m_x = m[m_v.len + 1 + noise_length ..][0..secret_key.len]; const m_h = m[m.len - h.len ..]; - mem.set(u8, m_v, 0x01); + @memset(m_v, 0x01); m_i.* = 0x00; - if (noise) |n| mem.copy(u8, m_z, &n); - mem.copy(u8, m_x, &secret_key); - mem.copy(u8, m_h, &h); + if (noise) |n| @memcpy(m_z, &n); + @memcpy(m_x, &secret_key); + @memcpy(m_h, &h); Hmac.create(&k, &m, &k); Hmac.create(m_v, m_v, &k); - mem.copy(u8, m_v, m_v); m_i.* = 0x01; Hmac.create(&k, &m, &k); Hmac.create(m_v, m_v, &k); @@ -361,10 +360,9 @@ pub fn Ecdsa(comptime Curve: type, comptime Hash: type) type { while (t_off < t.len) : (t_off += m_v.len) { const t_end = @min(t_off + m_v.len, t.len); Hmac.create(m_v, m_v, &k); - std.mem.copy(u8, t[t_off..t_end], m_v[0 .. t_end - t_off]); + @memcpy(t[t_off..t_end], m_v[0 .. t_end - t_off]); } if (Curve.scalar.Scalar.fromBytes(t, .Big)) |s| return s else |_| {} - mem.copy(u8, m_v, m_v); m_i.* = 0x00; Hmac.create(&k, m[0 .. m_v.len + 1], &k); Hmac.create(m_v, m_v, &k); diff --git a/lib/std/crypto/hkdf.zig b/lib/std/crypto/hkdf.zig index 7102ffe780..9163ba9d15 100644 --- a/lib/std/crypto/hkdf.zig +++ b/lib/std/crypto/hkdf.zig @@ -63,7 +63,7 @@ pub fn Hkdf(comptime Hmac: type) type { st.update(&counter); var tmp: [prk_length]u8 = undefined; st.final(tmp[0..prk_length]); - mem.copy(u8, out[i..][0..left], tmp[0..left]); + @memcpy(out[i..][0..left], tmp[0..left]); } } }; diff --git a/lib/std/crypto/hmac.zig b/lib/std/crypto/hmac.zig index f279132ee8..8d0daa3032 100644 --- a/lib/std/crypto/hmac.zig +++ b/lib/std/crypto/hmac.zig @@ -38,12 +38,12 @@ pub fn Hmac(comptime Hash: type) type { // Normalize key length to block size of hash if (key.len > Hash.block_length) { Hash.hash(key, scratch[0..mac_length], .{}); - mem.set(u8, scratch[mac_length..Hash.block_length], 0); + @memset(scratch[mac_length..Hash.block_length], 0); } else if (key.len < Hash.block_length) { - mem.copy(u8, scratch[0..key.len], key); - mem.set(u8, scratch[key.len..Hash.block_length], 0); + @memcpy(scratch[0..key.len], key); + @memset(scratch[key.len..Hash.block_length], 0); } else { - mem.copy(u8, scratch[0..], key); + @memcpy(&scratch, key); } for (&ctx.o_key_pad, 0..) |*b, i| { diff --git a/lib/std/crypto/isap.zig b/lib/std/crypto/isap.zig index 0888cfa4dd..5b0da739de 100644 --- a/lib/std/crypto/isap.zig +++ b/lib/std/crypto/isap.zig @@ -43,7 +43,7 @@ pub const IsapA128A = struct { } } else { var padded = [_]u8{0} ** 8; - mem.copy(u8, padded[0..left], m[i..]); + @memcpy(padded[0..left], m[i..]); padded[left] = 0x80; isap.st.addBytes(&padded); isap.st.permute(); diff --git a/lib/std/crypto/keccak_p.zig b/lib/std/crypto/keccak_p.zig index af7c12c8c2..9226f2f6d4 100644 --- a/lib/std/crypto/keccak_p.zig +++ b/lib/std/crypto/keccak_p.zig @@ -68,7 +68,7 @@ pub fn KeccakF(comptime f: u11) type { } if (i < bytes.len) { var padded = [_]u8{0} ** @sizeOf(T); - mem.copy(u8, padded[0 .. bytes.len - i], bytes[i..]); + @memcpy(padded[0 .. bytes.len - i], bytes[i..]); self.st[i / @sizeOf(T)] = mem.readIntLittle(T, padded[0..]); } } @@ -87,7 +87,7 @@ pub fn KeccakF(comptime f: u11) type { } if (i < bytes.len) { var padded = [_]u8{0} ** @sizeOf(T); - mem.copy(u8, padded[0 .. bytes.len - i], bytes[i..]); + @memcpy(padded[0 .. bytes.len - i], bytes[i..]); self.st[i / @sizeOf(T)] ^= mem.readIntLittle(T, padded[0..]); } } @@ -101,7 +101,7 @@ pub fn KeccakF(comptime f: u11) type { if (i < out.len) { var padded = [_]u8{0} ** @sizeOf(T); mem.writeIntLittle(T, padded[0..], self.st[i / @sizeOf(T)]); - mem.copy(u8, out[i..], padded[0 .. out.len - i]); + @memcpy(out[i..], padded[0 .. out.len - i]); } } @@ -116,16 +116,16 @@ pub fn KeccakF(comptime f: u11) type { } if (i < in.len) { var padded = [_]u8{0} ** @sizeOf(T); - mem.copy(u8, padded[0 .. in.len - i], in[i..]); + @memcpy(padded[0 .. in.len - i], in[i..]); const x = mem.readIntNative(T, &padded) ^ mem.nativeToLittle(T, self.st[i / @sizeOf(T)]); mem.writeIntNative(T, &padded, x); - mem.copy(u8, out[i..], padded[0 .. in.len - i]); + @memcpy(out[i..], padded[0 .. in.len - i]); } } /// Set the words storing the bytes of a given range to zero. pub fn clear(self: *Self, from: usize, to: usize) void { - mem.set(T, self.st[from / @sizeOf(T) .. (to + @sizeOf(T) - 1) / @sizeOf(T)], 0); + @memset(self.st[from / @sizeOf(T) .. (to + @sizeOf(T) - 1) / @sizeOf(T)], 0); } /// Clear the entire state, disabling compiler optimizations. @@ -215,7 +215,7 @@ pub fn State(comptime f: u11, comptime capacity: u11, comptime delim: u8, compti var bytes = bytes_; if (self.offset > 0) { const left = math.min(rate - self.offset, bytes.len); - mem.copy(u8, self.buf[self.offset..], bytes[0..left]); + @memcpy(self.buf[self.offset..][0..left], bytes[0..left]); self.offset += left; if (self.offset == rate) { self.offset = 0; @@ -231,7 +231,7 @@ pub fn State(comptime f: u11, comptime capacity: u11, comptime delim: u8, compti bytes = bytes[rate..]; } if (bytes.len > 0) { - mem.copy(u8, &self.buf, bytes); + @memcpy(self.buf[0..bytes.len], bytes); self.offset = bytes.len; } } diff --git a/lib/std/crypto/kyber_d00.zig b/lib/std/crypto/kyber_d00.zig index 21fdb6ff17..6a0a17c311 100644 --- a/lib/std/crypto/kyber_d00.zig +++ b/lib/std/crypto/kyber_d00.zig @@ -1479,7 +1479,7 @@ test "MulHat" { const p2 = a.ntt().mulHat(b.ntt()).barrettReduce().invNTT().normalize(); var p: Poly = undefined; - mem.set(i16, &p.cs, 0); + @memset(&p.cs, 0); for (0..N) |i| { for (0..N) |j| { diff --git a/lib/std/crypto/md5.zig b/lib/std/crypto/md5.zig index 6276fadb43..bd4a78c032 100644 --- a/lib/std/crypto/md5.zig +++ b/lib/std/crypto/md5.zig @@ -66,7 +66,7 @@ pub const Md5 = struct { // Partial buffer exists from previous update. Copy into buffer then hash. if (d.buf_len != 0 and d.buf_len + b.len >= 64) { off += 64 - d.buf_len; - mem.copy(u8, d.buf[d.buf_len..], b[0..off]); + @memcpy(d.buf[d.buf_len..][0..off], b[0..off]); d.round(&d.buf); d.buf_len = 0; @@ -78,8 +78,9 @@ pub const Md5 = struct { } // Copy any remainder for next pass. - mem.copy(u8, d.buf[d.buf_len..], b[off..]); - d.buf_len += @intCast(u8, b[off..].len); + const b_slice = b[off..]; + @memcpy(d.buf[d.buf_len..][0..b_slice.len], b_slice); + d.buf_len += @intCast(u8, b_slice.len); // Md5 uses the bottom 64-bits for length padding d.total_len +%= b.len; @@ -87,7 +88,7 @@ pub const Md5 = struct { pub fn final(d: *Self, out: *[digest_length]u8) void { // The buffer here will never be completely full. - mem.set(u8, d.buf[d.buf_len..], 0); + @memset(d.buf[d.buf_len..], 0); // Append padding bits. d.buf[d.buf_len] = 0x80; @@ -96,7 +97,7 @@ pub const Md5 = struct { // > 448 mod 512 so need to add an extra round to wrap around. if (64 - d.buf_len < 8) { d.round(d.buf[0..]); - mem.set(u8, d.buf[0..], 0); + @memset(d.buf[0..], 0); } // Append message length. diff --git a/lib/std/crypto/modes.zig b/lib/std/crypto/modes.zig index 325d8c0ceb..eed803a899 100644 --- a/lib/std/crypto/modes.zig +++ b/lib/std/crypto/modes.zig @@ -38,8 +38,10 @@ pub fn ctr(comptime BlockCipher: anytype, block_cipher: BlockCipher, dst: []u8, if (i < src.len) { mem.writeInt(u128, &counter, counterInt, endian); var pad = [_]u8{0} ** block_length; - mem.copy(u8, &pad, src[i..]); + const src_slice = src[i..]; + @memcpy(pad[0..src_slice.len], src_slice); block_cipher.xor(&pad, &pad, counter); - mem.copy(u8, dst[i..], pad[0 .. src.len - i]); + const pad_slice = pad[0 .. src.len - i]; + @memcpy(dst[i..][0..pad_slice.len], pad_slice); } } diff --git a/lib/std/crypto/pbkdf2.zig b/lib/std/crypto/pbkdf2.zig index 6f9783df72..7c6df5444d 100644 --- a/lib/std/crypto/pbkdf2.zig +++ b/lib/std/crypto/pbkdf2.zig @@ -129,13 +129,13 @@ pub fn pbkdf2(dk: []u8, password: []const u8, salt: []const u8, rounds: u32, com const offset = block * h_len; const block_len = if (block != blocks_count - 1) h_len else r; const dk_block: []u8 = dk[offset..][0..block_len]; - mem.copy(u8, dk_block, prev_block[0..dk_block.len]); + @memcpy(dk_block, prev_block[0..dk_block.len]); var i: u32 = 1; while (i < rounds) : (i += 1) { // U_c = PRF (P, U_{c-1}) Prf.create(&new_block, prev_block[0..], password); - mem.copy(u8, prev_block[0..], new_block[0..]); + prev_block = new_block; // F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c for (dk_block, 0..) |_, j| { diff --git a/lib/std/crypto/pcurves/common.zig b/lib/std/crypto/pcurves/common.zig index 40f4a728c7..5d41bc190a 100644 --- a/lib/std/crypto/pcurves/common.zig +++ b/lib/std/crypto/pcurves/common.zig @@ -228,8 +228,8 @@ pub fn Field(comptime params: FieldParams) type { } if (iterations % 2 != 0) { fiat.divstep(&out1, &out2, &out3, &out4, &out5, d, f, g, v, r); - mem.copy(Word, &v, &out4); - mem.copy(Word, &f, &out2); + v = out4; + f = out2; } var v_opp: Limbs = undefined; fiat.opp(&v_opp, v); diff --git a/lib/std/crypto/pcurves/p256.zig b/lib/std/crypto/pcurves/p256.zig index a160d08016..0460515511 100644 --- a/lib/std/crypto/pcurves/p256.zig +++ b/lib/std/crypto/pcurves/p256.zig @@ -105,7 +105,7 @@ pub const P256 = struct { var out: [33]u8 = undefined; const xy = p.affineCoordinates(); out[0] = if (xy.y.isOdd()) 3 else 2; - mem.copy(u8, out[1..], &xy.x.toBytes(.Big)); + out[1..].* = xy.x.toBytes(.Big); return out; } @@ -114,8 +114,8 @@ pub const P256 = struct { var out: [65]u8 = undefined; out[0] = 4; const xy = p.affineCoordinates(); - mem.copy(u8, out[1..33], &xy.x.toBytes(.Big)); - mem.copy(u8, out[33..65], &xy.y.toBytes(.Big)); + out[1..33].* = xy.x.toBytes(.Big); + out[33..65].* = xy.y.toBytes(.Big); return out; } diff --git a/lib/std/crypto/pcurves/p256/scalar.zig b/lib/std/crypto/pcurves/p256/scalar.zig index ce019082ef..4e88d1fee7 100644 --- a/lib/std/crypto/pcurves/p256/scalar.zig +++ b/lib/std/crypto/pcurves/p256/scalar.zig @@ -192,20 +192,20 @@ const ScalarDouble = struct { var t = ScalarDouble{ .x1 = undefined, .x2 = Fe.zero, .x3 = Fe.zero }; { var b = [_]u8{0} ** encoded_length; - const len = math.min(s.len, 24); - mem.copy(u8, b[0..len], s[0..len]); + const len = @min(s.len, 24); + b[0..len].* = s[0..len].*; t.x1 = Fe.fromBytes(b, .Little) catch unreachable; } if (s_.len >= 24) { var b = [_]u8{0} ** encoded_length; - const len = math.min(s.len - 24, 24); - mem.copy(u8, b[0..len], s[24..][0..len]); + const len = @min(s.len - 24, 24); + b[0..len].* = s[24..][0..len].*; t.x2 = Fe.fromBytes(b, .Little) catch unreachable; } if (s_.len >= 48) { var b = [_]u8{0} ** encoded_length; const len = s.len - 48; - mem.copy(u8, b[0..len], s[48..][0..len]); + b[0..len].* = s[48..][0..len].*; t.x3 = Fe.fromBytes(b, .Little) catch unreachable; } return t; diff --git a/lib/std/crypto/pcurves/p384.zig b/lib/std/crypto/pcurves/p384.zig index 3aaf9e341f..6662fc0011 100644 --- a/lib/std/crypto/pcurves/p384.zig +++ b/lib/std/crypto/pcurves/p384.zig @@ -105,7 +105,7 @@ pub const P384 = struct { var out: [49]u8 = undefined; const xy = p.affineCoordinates(); out[0] = if (xy.y.isOdd()) 3 else 2; - mem.copy(u8, out[1..], &xy.x.toBytes(.Big)); + out[1..].* = xy.x.toBytes(.Big); return out; } @@ -114,8 +114,8 @@ pub const P384 = struct { var out: [97]u8 = undefined; out[0] = 4; const xy = p.affineCoordinates(); - mem.copy(u8, out[1..49], &xy.x.toBytes(.Big)); - mem.copy(u8, out[49..97], &xy.y.toBytes(.Big)); + out[1..49].* = xy.x.toBytes(.Big); + out[49..97].* = xy.y.toBytes(.Big); return out; } diff --git a/lib/std/crypto/pcurves/p384/scalar.zig b/lib/std/crypto/pcurves/p384/scalar.zig index ec71a52efa..ef257ab7ce 100644 --- a/lib/std/crypto/pcurves/p384/scalar.zig +++ b/lib/std/crypto/pcurves/p384/scalar.zig @@ -180,14 +180,14 @@ const ScalarDouble = struct { var t = ScalarDouble{ .x1 = undefined, .x2 = Fe.zero }; { var b = [_]u8{0} ** encoded_length; - const len = math.min(s.len, 32); - mem.copy(u8, b[0..len], s[0..len]); + const len = @min(s.len, 32); + b[0..len].* = s[0..len].*; t.x1 = Fe.fromBytes(b, .Little) catch unreachable; } if (s_.len >= 32) { var b = [_]u8{0} ** encoded_length; - const len = math.min(s.len - 32, 32); - mem.copy(u8, b[0..len], s[32..][0..len]); + const len = @min(s.len - 32, 32); + b[0..len].* = s[32..][0..len].*; t.x2 = Fe.fromBytes(b, .Little) catch unreachable; } return t; diff --git a/lib/std/crypto/pcurves/secp256k1.zig b/lib/std/crypto/pcurves/secp256k1.zig index 6998b0db82..753f929f44 100644 --- a/lib/std/crypto/pcurves/secp256k1.zig +++ b/lib/std/crypto/pcurves/secp256k1.zig @@ -158,7 +158,7 @@ pub const Secp256k1 = struct { var out: [33]u8 = undefined; const xy = p.affineCoordinates(); out[0] = if (xy.y.isOdd()) 3 else 2; - mem.copy(u8, out[1..], &xy.x.toBytes(.Big)); + out[1..].* = xy.x.toBytes(.Big); return out; } @@ -167,8 +167,8 @@ pub const Secp256k1 = struct { var out: [65]u8 = undefined; out[0] = 4; const xy = p.affineCoordinates(); - mem.copy(u8, out[1..33], &xy.x.toBytes(.Big)); - mem.copy(u8, out[33..65], &xy.y.toBytes(.Big)); + out[1..33].* = xy.x.toBytes(.Big); + out[33..65].* = xy.y.toBytes(.Big); return out; } diff --git a/lib/std/crypto/pcurves/secp256k1/scalar.zig b/lib/std/crypto/pcurves/secp256k1/scalar.zig index 0b7d6e952d..e0b5e053e3 100644 --- a/lib/std/crypto/pcurves/secp256k1/scalar.zig +++ b/lib/std/crypto/pcurves/secp256k1/scalar.zig @@ -192,20 +192,20 @@ const ScalarDouble = struct { var t = ScalarDouble{ .x1 = undefined, .x2 = Fe.zero, .x3 = Fe.zero }; { var b = [_]u8{0} ** encoded_length; - const len = math.min(s.len, 24); - mem.copy(u8, b[0..len], s[0..len]); + const len = @min(s.len, 24); + b[0..len].* = s[0..len].*; t.x1 = Fe.fromBytes(b, .Little) catch unreachable; } if (s_.len >= 24) { var b = [_]u8{0} ** encoded_length; - const len = math.min(s.len - 24, 24); - mem.copy(u8, b[0..len], s[24..][0..len]); + const len = @min(s.len - 24, 24); + b[0..len].* = s[24..][0..len].*; t.x2 = Fe.fromBytes(b, .Little) catch unreachable; } if (s_.len >= 48) { var b = [_]u8{0} ** encoded_length; const len = s.len - 48; - mem.copy(u8, b[0..len], s[48..][0..len]); + b[0..len].* = s[48..][0..len].*; t.x3 = Fe.fromBytes(b, .Little) catch unreachable; } return t; diff --git a/lib/std/crypto/phc_encoding.zig b/lib/std/crypto/phc_encoding.zig index 4b6965d040..cc0f10e395 100644 --- a/lib/std/crypto/phc_encoding.zig +++ b/lib/std/crypto/phc_encoding.zig @@ -35,7 +35,7 @@ pub fn BinValue(comptime max_len: usize) type { pub fn fromSlice(slice: []const u8) Error!Self { if (slice.len > capacity) return Error.NoSpaceLeft; var bin_value: Self = undefined; - mem.copy(u8, &bin_value.buf, slice); + @memcpy(bin_value.buf[0..slice.len], slice); bin_value.len = slice.len; return bin_value; } diff --git a/lib/std/crypto/salsa20.zig b/lib/std/crypto/salsa20.zig index 492b8e9988..7f57e6cecb 100644 --- a/lib/std/crypto/salsa20.zig +++ b/lib/std/crypto/salsa20.zig @@ -383,10 +383,10 @@ pub const XSalsa20Poly1305 = struct { debug.assert(c.len == m.len); const extended = extend(rounds, k, npub); var block0 = [_]u8{0} ** 64; - const mlen0 = math.min(32, m.len); - mem.copy(u8, block0[32..][0..mlen0], m[0..mlen0]); + const mlen0 = @min(32, m.len); + @memcpy(block0[32..][0..mlen0], m[0..mlen0]); Salsa20.xor(block0[0..], block0[0..], 0, extended.key, extended.nonce); - mem.copy(u8, c[0..mlen0], block0[32..][0..mlen0]); + @memcpy(c[0..mlen0], block0[32..][0..mlen0]); Salsa20.xor(c[mlen0..], m[mlen0..], 1, extended.key, extended.nonce); var mac = Poly1305.init(block0[0..32]); mac.update(ad); @@ -405,7 +405,7 @@ pub const XSalsa20Poly1305 = struct { const extended = extend(rounds, k, npub); var block0 = [_]u8{0} ** 64; const mlen0 = math.min(32, c.len); - mem.copy(u8, block0[32..][0..mlen0], c[0..mlen0]); + @memcpy(block0[32..][0..mlen0], c[0..mlen0]); Salsa20.xor(block0[0..], block0[0..], 0, extended.key, extended.nonce); var mac = Poly1305.init(block0[0..32]); mac.update(ad); @@ -420,7 +420,7 @@ pub const XSalsa20Poly1305 = struct { utils.secureZero(u8, &computedTag); return error.AuthenticationFailed; } - mem.copy(u8, m[0..mlen0], block0[32..][0..mlen0]); + @memcpy(m[0..mlen0], block0[32..][0..mlen0]); Salsa20.xor(m[mlen0..], c[mlen0..], 1, extended.key, extended.nonce); } }; @@ -533,7 +533,7 @@ pub const SealedBox = struct { debug.assert(c.len == m.len + seal_length); var ekp = try KeyPair.create(null); const nonce = createNonce(ekp.public_key, public_key); - mem.copy(u8, c[0..public_length], ekp.public_key[0..]); + c[0..public_length].* = ekp.public_key; try Box.seal(c[Box.public_length..], m, nonce, public_key, ekp.secret_key); utils.secureZero(u8, ekp.secret_key[0..]); } diff --git a/lib/std/crypto/sha1.zig b/lib/std/crypto/sha1.zig index 4d11b04eb5..1f5f3eaae2 100644 --- a/lib/std/crypto/sha1.zig +++ b/lib/std/crypto/sha1.zig @@ -62,7 +62,7 @@ pub const Sha1 = struct { // Partial buffer exists from previous update. Copy into buffer then hash. if (d.buf_len != 0 and d.buf_len + b.len >= 64) { off += 64 - d.buf_len; - mem.copy(u8, d.buf[d.buf_len..], b[0..off]); + @memcpy(d.buf[d.buf_len..][0..off], b[0..off]); d.round(d.buf[0..]); d.buf_len = 0; @@ -74,7 +74,7 @@ pub const Sha1 = struct { } // Copy any remainder for next pass. - mem.copy(u8, d.buf[d.buf_len..], b[off..]); + @memcpy(d.buf[d.buf_len..][0 .. b.len - off], b[off..]); d.buf_len += @intCast(u8, b[off..].len); d.total_len += b.len; @@ -82,7 +82,7 @@ pub const Sha1 = struct { pub fn final(d: *Self, out: *[digest_length]u8) void { // The buffer here will never be completely full. - mem.set(u8, d.buf[d.buf_len..], 0); + @memset(d.buf[d.buf_len..], 0); // Append padding bits. d.buf[d.buf_len] = 0x80; @@ -91,7 +91,7 @@ pub const Sha1 = struct { // > 448 mod 512 so need to add an extra round to wrap around. if (64 - d.buf_len < 8) { d.round(d.buf[0..]); - mem.set(u8, d.buf[0..], 0); + @memset(d.buf[0..], 0); } // Append message length. diff --git a/lib/std/crypto/sha2.zig b/lib/std/crypto/sha2.zig index ad99079852..bd5a7cc5d4 100644 --- a/lib/std/crypto/sha2.zig +++ b/lib/std/crypto/sha2.zig @@ -118,7 +118,7 @@ fn Sha2x32(comptime params: Sha2Params32) type { // Partial buffer exists from previous update. Copy into buffer then hash. if (d.buf_len != 0 and d.buf_len + b.len >= 64) { off += 64 - d.buf_len; - mem.copy(u8, d.buf[d.buf_len..], b[0..off]); + @memcpy(d.buf[d.buf_len..][0..off], b[0..off]); d.round(&d.buf); d.buf_len = 0; @@ -130,7 +130,8 @@ fn Sha2x32(comptime params: Sha2Params32) type { } // Copy any remainder for next pass. - mem.copy(u8, d.buf[d.buf_len..], b[off..]); + const b_slice = b[off..]; + @memcpy(d.buf[d.buf_len..][0..b_slice.len], b_slice); d.buf_len += @intCast(u8, b[off..].len); d.total_len += b.len; @@ -143,7 +144,7 @@ fn Sha2x32(comptime params: Sha2Params32) type { pub fn final(d: *Self, out: *[digest_length]u8) void { // The buffer here will never be completely full. - mem.set(u8, d.buf[d.buf_len..], 0); + @memset(d.buf[d.buf_len..], 0); // Append padding bits. d.buf[d.buf_len] = 0x80; @@ -152,7 +153,7 @@ fn Sha2x32(comptime params: Sha2Params32) type { // > 448 mod 512 so need to add an extra round to wrap around. if (64 - d.buf_len < 8) { d.round(&d.buf); - mem.set(u8, d.buf[0..], 0); + @memset(d.buf[0..], 0); } // Append message length. @@ -609,7 +610,7 @@ fn Sha2x64(comptime params: Sha2Params64) type { // Partial buffer exists from previous update. Copy into buffer then hash. if (d.buf_len != 0 and d.buf_len + b.len >= 128) { off += 128 - d.buf_len; - mem.copy(u8, d.buf[d.buf_len..], b[0..off]); + @memcpy(d.buf[d.buf_len..][0..off], b[0..off]); d.round(&d.buf); d.buf_len = 0; @@ -621,7 +622,8 @@ fn Sha2x64(comptime params: Sha2Params64) type { } // Copy any remainder for next pass. - mem.copy(u8, d.buf[d.buf_len..], b[off..]); + const b_slice = b[off..]; + @memcpy(d.buf[d.buf_len..][0..b_slice.len], b_slice); d.buf_len += @intCast(u8, b[off..].len); d.total_len += b.len; @@ -634,7 +636,7 @@ fn Sha2x64(comptime params: Sha2Params64) type { pub fn final(d: *Self, out: *[digest_length]u8) void { // The buffer here will never be completely full. - mem.set(u8, d.buf[d.buf_len..], 0); + @memset(d.buf[d.buf_len..], 0); // Append padding bits. d.buf[d.buf_len] = 0x80; @@ -643,7 +645,7 @@ fn Sha2x64(comptime params: Sha2Params64) type { // > 896 mod 1024 so need to add an extra round to wrap around. if (128 - d.buf_len < 16) { d.round(d.buf[0..]); - mem.set(u8, d.buf[0..], 0); + @memset(d.buf[0..], 0); } // Append message length. diff --git a/lib/std/crypto/sha3.zig b/lib/std/crypto/sha3.zig index 1f48f87c53..23f9e65534 100644 --- a/lib/std/crypto/sha3.zig +++ b/lib/std/crypto/sha3.zig @@ -149,7 +149,7 @@ fn ShakeLike(comptime security_level: u11, comptime delim: u8, comptime rounds: const left = self.buf.len - self.offset; if (left > 0) { const n = math.min(left, out.len); - mem.copy(u8, out[0..n], self.buf[self.offset..][0..n]); + @memcpy(out[0..n], self.buf[self.offset..][0..n]); out = out[n..]; self.offset += n; if (out.len == 0) { @@ -164,7 +164,7 @@ fn ShakeLike(comptime security_level: u11, comptime delim: u8, comptime rounds: } if (out.len > 0) { self.st.squeeze(self.buf[0..]); - mem.copy(u8, out[0..], self.buf[0..out.len]); + @memcpy(out[0..], self.buf[0..out.len]); self.offset = out.len; } } diff --git a/lib/std/crypto/siphash.zig b/lib/std/crypto/siphash.zig index 16388439d1..d91485cdfa 100644 --- a/lib/std/crypto/siphash.zig +++ b/lib/std/crypto/siphash.zig @@ -98,7 +98,7 @@ fn SipHashStateless(comptime T: type, comptime c_rounds: usize, comptime d_round self.msg_len +%= @truncate(u8, b.len); var buf = [_]u8{0} ** 8; - mem.copy(u8, buf[0..], b[0..]); + @memcpy(buf[0..b.len], b); buf[7] = self.msg_len; self.round(buf); @@ -203,7 +203,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) if (self.buf_len != 0 and self.buf_len + b.len >= 8) { off += 8 - self.buf_len; - mem.copy(u8, self.buf[self.buf_len..], b[0..off]); + @memcpy(self.buf[self.buf_len..][0..off], b[0..off]); self.state.update(self.buf[0..]); self.buf_len = 0; } @@ -212,8 +212,9 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) const aligned_len = remain_len - (remain_len % 8); self.state.update(b[off .. off + aligned_len]); - mem.copy(u8, self.buf[self.buf_len..], b[off + aligned_len ..]); - self.buf_len += @intCast(u8, b[off + aligned_len ..].len); + const b_slice = b[off + aligned_len ..]; + @memcpy(self.buf[self.buf_len..][0..b_slice.len], b_slice); + self.buf_len += @intCast(u8, b_slice.len); } pub fn peek(self: Self) [mac_length]u8 { diff --git a/lib/std/crypto/tls/Client.zig b/lib/std/crypto/tls/Client.zig index 1d94dfce31..c886bc7276 100644 --- a/lib/std/crypto/tls/Client.zig +++ b/lib/std/crypto/tls/Client.zig @@ -685,7 +685,7 @@ pub fn init(stream: anytype, ca_bundle: Certificate.Bundle, host: []const u8) In .application_cipher = app_cipher, .partially_read_buffer = undefined, }; - mem.copy(u8, &client.partially_read_buffer, leftover); + @memcpy(client.partially_read_buffer[0..leftover.len], leftover); return client; }, else => { @@ -809,7 +809,7 @@ fn prepareCiphertextRecord( .overhead_len = overhead_len, }; - mem.copy(u8, &cleartext_buf, bytes[bytes_i..][0..encrypted_content_len]); + @memcpy(cleartext_buf[0..encrypted_content_len], bytes[bytes_i..][0..encrypted_content_len]); cleartext_buf[encrypted_content_len] = @enumToInt(inner_content_type); bytes_i += encrypted_content_len; const ciphertext_len = encrypted_content_len + 1; @@ -1029,8 +1029,8 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) if (frag1.len < second_len) return finishRead2(c, first, frag1, vp.total); - mem.copy(u8, frag[0..in], first); - mem.copy(u8, frag[first.len..], frag1[0..second_len]); + @memcpy(frag[0..in], first); + @memcpy(frag[first.len..][0..second_len], frag1[0..second_len]); frag = frag[0..full_record_len]; frag1 = frag1[second_len..]; in = 0; @@ -1059,8 +1059,8 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) if (frag1.len < second_len) return finishRead2(c, first, frag1, vp.total); - mem.copy(u8, frag[0..in], first); - mem.copy(u8, frag[first.len..], frag1[0..second_len]); + @memcpy(frag[0..in], first); + @memcpy(frag[first.len..][0..second_len], frag1[0..second_len]); frag = frag[0..full_record_len]; frag1 = frag1[second_len..]; in = 0; @@ -1177,7 +1177,7 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) // We have already run out of room in iovecs. Continue // appending to `partially_read_buffer`. const dest = c.partially_read_buffer[c.partial_ciphertext_idx..]; - mem.copy(u8, dest, msg); + @memcpy(dest[0..msg.len], msg); c.partial_ciphertext_idx = @intCast(@TypeOf(c.partial_ciphertext_idx), c.partial_ciphertext_idx + msg.len); } else { const amt = vp.put(msg); @@ -1185,7 +1185,7 @@ pub fn readvAdvanced(c: *Client, stream: anytype, iovecs: []const std.os.iovec) const rest = msg[amt..]; c.partial_cleartext_idx = 0; c.partial_ciphertext_idx = @intCast(@TypeOf(c.partial_ciphertext_idx), rest.len); - mem.copy(u8, &c.partially_read_buffer, rest); + @memcpy(c.partially_read_buffer[0..rest.len], rest); } } } else { @@ -1213,12 +1213,12 @@ fn finishRead(c: *Client, frag: []const u8, in: usize, out: usize) usize { if (c.partial_ciphertext_idx > c.partial_cleartext_idx) { // There is cleartext at the beginning already which we need to preserve. c.partial_ciphertext_end = @intCast(@TypeOf(c.partial_ciphertext_end), c.partial_ciphertext_idx + saved_buf.len); - mem.copy(u8, c.partially_read_buffer[c.partial_ciphertext_idx..], saved_buf); + @memcpy(c.partially_read_buffer[c.partial_ciphertext_idx..][0..saved_buf.len], saved_buf); } else { c.partial_cleartext_idx = 0; c.partial_ciphertext_idx = 0; c.partial_ciphertext_end = @intCast(@TypeOf(c.partial_ciphertext_end), saved_buf.len); - mem.copy(u8, &c.partially_read_buffer, saved_buf); + @memcpy(c.partially_read_buffer[0..saved_buf.len], saved_buf); } return out; } @@ -1227,14 +1227,14 @@ fn finishRead2(c: *Client, first: []const u8, frag1: []const u8, out: usize) usi if (c.partial_ciphertext_idx > c.partial_cleartext_idx) { // There is cleartext at the beginning already which we need to preserve. c.partial_ciphertext_end = @intCast(@TypeOf(c.partial_ciphertext_end), c.partial_ciphertext_idx + first.len + frag1.len); - mem.copy(u8, c.partially_read_buffer[c.partial_ciphertext_idx..], first); - mem.copy(u8, c.partially_read_buffer[c.partial_ciphertext_idx + first.len ..], frag1); + @memcpy(c.partially_read_buffer[c.partial_ciphertext_idx..][0..first.len], first); + @memcpy(c.partially_read_buffer[c.partial_ciphertext_idx + first.len ..][0..frag1.len], frag1); } else { c.partial_cleartext_idx = 0; c.partial_ciphertext_idx = 0; c.partial_ciphertext_end = @intCast(@TypeOf(c.partial_ciphertext_end), first.len + frag1.len); - mem.copy(u8, &c.partially_read_buffer, first); - mem.copy(u8, c.partially_read_buffer[first.len..], frag1); + @memcpy(c.partially_read_buffer[0..first.len], first); + @memcpy(c.partially_read_buffer[first.len..][0..frag1.len], frag1); } return out; } @@ -1282,7 +1282,7 @@ const VecPut = struct { const v = vp.iovecs[vp.idx]; const dest = v.iov_base[vp.off..v.iov_len]; const src = bytes[bytes_i..][0..@min(dest.len, bytes.len - bytes_i)]; - mem.copy(u8, dest, src); + @memcpy(dest[0..src.len], src); bytes_i += src.len; vp.off += src.len; if (vp.off >= v.iov_len) { diff --git a/lib/std/crypto/utils.zig b/lib/std/crypto/utils.zig index 38dc236455..14a235e418 100644 --- a/lib/std/crypto/utils.zig +++ b/lib/std/crypto/utils.zig @@ -134,12 +134,8 @@ pub fn timingSafeSub(comptime T: type, a: []const T, b: []const T, result: []T, /// Sets a slice to zeroes. /// Prevents the store from being optimized out. -pub fn secureZero(comptime T: type, s: []T) void { - // TODO: implement `@memset` for non-byte-sized element type in the llvm backend - //@memset(@as([]volatile T, s), 0); - const ptr = @ptrCast([*]volatile u8, s.ptr); - const length = s.len * @sizeOf(T); - @memset(ptr[0..length], 0); +pub inline fn secureZero(comptime T: type, s: []T) void { + @memset(@as([]volatile T, s), 0); } test "crypto.utils.timingSafeEql" { @@ -148,7 +144,7 @@ test "crypto.utils.timingSafeEql" { random.bytes(a[0..]); random.bytes(b[0..]); try testing.expect(!timingSafeEql([100]u8, a, b)); - mem.copy(u8, a[0..], b[0..]); + a = b; try testing.expect(timingSafeEql([100]u8, a, b)); } @@ -201,7 +197,7 @@ test "crypto.utils.secureZero" { var a = [_]u8{0xfe} ** 8; var b = [_]u8{0xfe} ** 8; - mem.set(u8, a[0..], 0); + @memset(a[0..], 0); secureZero(u8, b[0..]); try testing.expectEqualSlices(u8, a[0..], b[0..]); diff --git a/lib/std/cstr.zig b/lib/std/cstr.zig index 52524c5084..8a203e5c8e 100644 --- a/lib/std/cstr.zig +++ b/lib/std/cstr.zig @@ -34,7 +34,7 @@ fn testCStrFnsImpl() !void { /// Caller owns the returned memory. pub fn addNullByte(allocator: mem.Allocator, slice: []const u8) ![:0]u8 { const result = try allocator.alloc(u8, slice.len + 1); - mem.copy(u8, result, slice); + @memcpy(result[0..slice.len], slice); result[slice.len] = 0; return result[0..slice.len :0]; } @@ -78,7 +78,7 @@ pub const NullTerminated2DArray = struct { for (slice) |inner| { index_buf[i] = buf.ptr + write_index; i += 1; - mem.copy(u8, buf[write_index..], inner); + @memcpy(buf[write_index..][0..inner.len], inner); write_index += inner.len; buf[write_index] = 0; write_index += 1; diff --git a/lib/std/dynamic_library.zig b/lib/std/dynamic_library.zig index 099ba63204..59ad7429cf 100644 --- a/lib/std/dynamic_library.zig +++ b/lib/std/dynamic_library.zig @@ -210,7 +210,7 @@ pub const ElfDynLib = struct { -1, 0, ); - mem.copy(u8, sect_mem, file_bytes[0..ph.p_filesz]); + @memcpy(sect_mem[0..ph.p_filesz], file_bytes[0..ph.p_filesz]); } }, else => {}, diff --git a/lib/std/enums.zig b/lib/std/enums.zig index a915d40f49..8e67c358b7 100644 --- a/lib/std/enums.zig +++ b/lib/std/enums.zig @@ -275,7 +275,7 @@ pub fn EnumMap(comptime E: type, comptime V: type) type { .bits = Self.BitSet.initFull(), .values = undefined, }; - std.mem.set(V, &result.values, value); + @memset(&result.values, value); return result; } /// Initializes a full mapping with supplied values. @@ -1175,7 +1175,7 @@ pub fn IndexedArray(comptime I: type, comptime V: type, comptime Ext: fn (type) pub fn initFill(v: Value) Self { var self: Self = undefined; - std.mem.set(Value, &self.values, v); + @memset(&self.values, v); return self; } diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index a6736e41a7..0861e19d14 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -86,19 +86,17 @@ pub fn LinearFifo( pub fn realign(self: *Self) void { if (self.buf.len - self.head >= self.count) { - // this copy overlaps - mem.copy(T, self.buf[0..self.count], self.buf[self.head..][0..self.count]); + mem.copyForwards(T, self.buf[0..self.count], self.buf[self.head..][0..self.count]); self.head = 0; } else { var tmp: [mem.page_size / 2 / @sizeOf(T)]T = undefined; while (self.head != 0) { - const n = math.min(self.head, tmp.len); + const n = @min(self.head, tmp.len); const m = self.buf.len - n; - mem.copy(T, tmp[0..n], self.buf[0..n]); - // this middle copy overlaps; the others here don't - mem.copy(T, self.buf[0..m], self.buf[n..][0..m]); - mem.copy(T, self.buf[m..], tmp[0..n]); + @memcpy(tmp[0..n], self.buf[0..n]); + mem.copyForwards(T, self.buf[0..m], self.buf[n..][0..m]); + @memcpy(self.buf[m..][0..n], tmp[0..n]); self.head -= n; } } @@ -223,8 +221,8 @@ pub fn LinearFifo( while (dst_left.len > 0) { const slice = self.readableSlice(0); if (slice.len == 0) break; - const n = math.min(slice.len, dst_left.len); - mem.copy(T, dst_left, slice[0..n]); + const n = @min(slice.len, dst_left.len); + @memcpy(dst_left[0..n], slice[0..n]); self.discard(n); dst_left = dst_left[n..]; } @@ -289,8 +287,8 @@ pub fn LinearFifo( while (src_left.len > 0) { const writable_slice = self.writableSlice(0); assert(writable_slice.len != 0); - const n = math.min(writable_slice.len, src_left.len); - mem.copy(T, writable_slice, src_left[0..n]); + const n = @min(writable_slice.len, src_left.len); + @memcpy(writable_slice[0..n], src_left[0..n]); self.update(n); src_left = src_left[n..]; } @@ -354,11 +352,11 @@ pub fn LinearFifo( const slice = self.readableSliceMut(0); if (src.len < slice.len) { - mem.copy(T, slice, src); + @memcpy(slice[0..src.len], src); } else { - mem.copy(T, slice, src[0..slice.len]); + @memcpy(slice, src[0..slice.len]); const slice2 = self.readableSliceMut(slice.len); - mem.copy(T, slice2, src[slice.len..]); + @memcpy(slice2[0 .. src.len - slice.len], src[slice.len..]); } } diff --git a/lib/std/fmt/errol.zig b/lib/std/fmt/errol.zig index 1c67175ede..9ad5d218a9 100644 --- a/lib/std/fmt/errol.zig +++ b/lib/std/fmt/errol.zig @@ -84,8 +84,8 @@ pub fn errol3(value: f64, buffer: []u8) FloatDecimal { const i = tableLowerBound(bits); if (i < enum3.len and enum3[i] == bits) { const data = enum3_data[i]; - const digits = buffer[1 .. data.str.len + 1]; - mem.copy(u8, digits, data.str); + const digits = buffer[1..][0..data.str.len]; + @memcpy(digits, data.str); return FloatDecimal{ .digits = digits, .exp = data.exp, diff --git a/lib/std/fs/path.zig b/lib/std/fs/path.zig index beb386f92c..4f3e05cd59 100644 --- a/lib/std/fs/path.zig +++ b/lib/std/fs/path.zig @@ -79,7 +79,7 @@ fn joinSepMaybeZ(allocator: Allocator, separator: u8, comptime sepPredicate: fn const buf = try allocator.alloc(u8, total_len); errdefer allocator.free(buf); - mem.copy(u8, buf, paths[first_path_index]); + @memcpy(buf[0..paths[first_path_index].len], paths[first_path_index]); var buf_index: usize = paths[first_path_index].len; var prev_path = paths[first_path_index]; assert(prev_path.len > 0); @@ -94,7 +94,7 @@ fn joinSepMaybeZ(allocator: Allocator, separator: u8, comptime sepPredicate: fn buf_index += 1; } const adjusted_path = if (prev_sep and this_sep) this_path[1..] else this_path; - mem.copy(u8, buf[buf_index..], adjusted_path); + @memcpy(buf[buf_index..][0..adjusted_path.len], adjusted_path); buf_index += adjusted_path.len; prev_path = this_path; } @@ -631,7 +631,7 @@ pub fn resolveWindows(allocator: Allocator, paths: []const []const u8) ![]u8 { real_result[i..][0..3].* = "..\\".*; i += 3; } - mem.copy(u8, real_result[i..], result.items); + @memcpy(real_result[i..][0..result.items.len], result.items); return real_result; } } @@ -710,7 +710,7 @@ pub fn resolvePosix(allocator: Allocator, paths: []const []const u8) Allocator.E real_result[i..][0..3].* = "../".*; i += 3; } - mem.copy(u8, real_result[i..], result.items); + @memcpy(real_result[i..][0..result.items.len], result.items); return real_result; } } @@ -1106,7 +1106,7 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ! while (rest_it.next()) |to_component| { result[result_index] = '\\'; result_index += 1; - mem.copy(u8, result[result_index..], to_component); + @memcpy(result[result_index..][0..to_component.len], to_component); result_index += to_component.len; } @@ -1151,7 +1151,7 @@ pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![] return allocator.realloc(result, result_index - 1); } - mem.copy(u8, result[result_index..], to_rest); + @memcpy(result[result_index..][0..to_rest.len], to_rest); return result; } diff --git a/lib/std/hash/cityhash.zig b/lib/std/hash/cityhash.zig index 23b3ce02da..d0884b135f 100644 --- a/lib/std/hash/cityhash.zig +++ b/lib/std/hash/cityhash.zig @@ -348,8 +348,8 @@ fn SMHasherTest(comptime hash_fn: anytype) u32 { var key: [256]u8 = undefined; var hashes_bytes: [256 * @sizeOf(HashResult)]u8 = undefined; - std.mem.set(u8, &key, 0); - std.mem.set(u8, &hashes_bytes, 0); + @memset(&key, 0); + @memset(&hashes_bytes, 0); var i: u32 = 0; while (i < 256) : (i += 1) { diff --git a/lib/std/hash/wyhash.zig b/lib/std/hash/wyhash.zig index 2f30c26b75..772395eab9 100644 --- a/lib/std/hash/wyhash.zig +++ b/lib/std/hash/wyhash.zig @@ -147,7 +147,7 @@ pub const Wyhash = struct { if (self.buf_len != 0 and self.buf_len + b.len >= 32) { off += 32 - self.buf_len; - mem.copy(u8, self.buf[self.buf_len..], b[0..off]); + @memcpy(self.buf[self.buf_len..][0..off], b[0..off]); self.state.update(self.buf[0..]); self.buf_len = 0; } @@ -156,7 +156,8 @@ pub const Wyhash = struct { const aligned_len = remain_len - (remain_len % 32); self.state.update(b[off .. off + aligned_len]); - mem.copy(u8, self.buf[self.buf_len..], b[off + aligned_len ..]); + const src = b[off + aligned_len ..]; + @memcpy(self.buf[self.buf_len..][0..src.len], src); self.buf_len += @intCast(u8, b[off + aligned_len ..].len); } diff --git a/lib/std/hash/xxhash.zig b/lib/std/hash/xxhash.zig index bf4877e029..88db1dc1bd 100644 --- a/lib/std/hash/xxhash.zig +++ b/lib/std/hash/xxhash.zig @@ -36,7 +36,7 @@ pub const XxHash64 = struct { pub fn update(self: *XxHash64, input: []const u8) void { if (input.len < 32 - self.buf_len) { - mem.copy(u8, self.buf[self.buf_len..], input); + @memcpy(self.buf[self.buf_len..][0..input.len], input); self.buf_len += input.len; return; } @@ -45,7 +45,7 @@ pub const XxHash64 = struct { if (self.buf_len > 0) { i = 32 - self.buf_len; - mem.copy(u8, self.buf[self.buf_len..], input[0..i]); + @memcpy(self.buf[self.buf_len..][0..i], input[0..i]); self.processStripe(&self.buf); self.buf_len = 0; } @@ -55,7 +55,7 @@ pub const XxHash64 = struct { } const remaining_bytes = input[i..]; - mem.copy(u8, &self.buf, remaining_bytes); + @memcpy(self.buf[0..remaining_bytes.len], remaining_bytes); self.buf_len = remaining_bytes.len; } @@ -165,7 +165,7 @@ pub const XxHash32 = struct { pub fn update(self: *XxHash32, input: []const u8) void { if (input.len < 16 - self.buf_len) { - mem.copy(u8, self.buf[self.buf_len..], input); + @memcpy(self.buf[self.buf_len..][0..input.len], input); self.buf_len += input.len; return; } @@ -174,7 +174,7 @@ pub const XxHash32 = struct { if (self.buf_len > 0) { i = 16 - self.buf_len; - mem.copy(u8, self.buf[self.buf_len..], input[0..i]); + @memcpy(self.buf[self.buf_len..][0..i], input[0..i]); self.processStripe(&self.buf); self.buf_len = 0; } @@ -184,7 +184,7 @@ pub const XxHash32 = struct { } const remaining_bytes = input[i..]; - mem.copy(u8, &self.buf, remaining_bytes); + @memcpy(self.buf[0..remaining_bytes.len], remaining_bytes); self.buf_len = remaining_bytes.len; } diff --git a/lib/std/heap/WasmAllocator.zig b/lib/std/heap/WasmAllocator.zig index 3b980ac9d3..efec116db4 100644 --- a/lib/std/heap/WasmAllocator.zig +++ b/lib/std/heap/WasmAllocator.zig @@ -230,7 +230,7 @@ test "shrink" { var slice = try test_ally.alloc(u8, 20); defer test_ally.free(slice); - mem.set(u8, slice, 0x11); + @memset(slice, 0x11); try std.testing.expect(test_ally.resize(slice, 17)); slice = slice[0..17]; diff --git a/lib/std/heap/WasmPageAllocator.zig b/lib/std/heap/WasmPageAllocator.zig index 4084eaa88e..1370af022c 100644 --- a/lib/std/heap/WasmPageAllocator.zig +++ b/lib/std/heap/WasmPageAllocator.zig @@ -153,7 +153,7 @@ fn freePages(start: usize, end: usize) void { extended.data = @intToPtr([*]u128, new_end * mem.page_size)[0 .. mem.page_size / @sizeOf(u128)]; // Since this is the first page being freed and we consume it, assume *nothing* is free. - mem.set(u128, extended.data, PageStatus.none_free); + @memset(extended.data, PageStatus.none_free); } const clamped_start = @max(extendedOffset(), start); extended.recycle(clamped_start - extendedOffset(), new_end - clamped_start); diff --git a/lib/std/heap/general_purpose_allocator.zig b/lib/std/heap/general_purpose_allocator.zig index e7ebe75812..de7bc7c423 100644 --- a/lib/std/heap/general_purpose_allocator.zig +++ b/lib/std/heap/general_purpose_allocator.zig @@ -448,7 +448,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type { fn collectStackTrace(first_trace_addr: usize, addresses: *[stack_n]usize) void { if (stack_n == 0) return; - mem.set(usize, addresses, 0); + @memset(addresses, 0); var stack_trace = StackTrace{ .instruction_addresses = addresses, .index = 0, @@ -1113,7 +1113,7 @@ test "shrink" { var slice = try allocator.alloc(u8, 20); defer allocator.free(slice); - mem.set(u8, slice, 0x11); + @memset(slice, 0x11); try std.testing.expect(allocator.resize(slice, 17)); slice = slice[0..17]; diff --git a/lib/std/io/buffered_reader.zig b/lib/std/io/buffered_reader.zig index 2f6677c6a5..efc1d78cc4 100644 --- a/lib/std/io/buffered_reader.zig +++ b/lib/std/io/buffered_reader.zig @@ -20,8 +20,8 @@ pub fn BufferedReader(comptime buffer_size: usize, comptime ReaderType: type) ty var dest_index: usize = 0; while (dest_index < dest.len) { - const written = std.math.min(dest.len - dest_index, self.end - self.start); - std.mem.copy(u8, dest[dest_index..], self.buf[self.start .. self.start + written]); + const written = @min(dest.len - dest_index, self.end - self.start); + @memcpy(dest[dest_index..][0..written], self.buf[self.start..][0..written]); if (written == 0) { // buf empty, fill it const n = try self.unbuffered_reader.read(self.buf[0..]); @@ -115,11 +115,8 @@ test "io.BufferedReader Block" { } fn read(self: *Self, dest: []u8) Error!usize { - if (self.curr_read >= self.reads_allowed) { - return 0; - } - std.debug.assert(dest.len >= self.block.len); - std.mem.copy(u8, dest, self.block); + if (self.curr_read >= self.reads_allowed) return 0; + @memcpy(dest[0..self.block.len], self.block); self.curr_read += 1; return self.block.len; diff --git a/lib/std/io/buffered_writer.zig b/lib/std/io/buffered_writer.zig index 497a1810a7..906d6cce49 100644 --- a/lib/std/io/buffered_writer.zig +++ b/lib/std/io/buffered_writer.zig @@ -30,8 +30,9 @@ pub fn BufferedWriter(comptime buffer_size: usize, comptime WriterType: type) ty return self.unbuffered_writer.write(bytes); } - mem.copy(u8, self.buf[self.end..], bytes); - self.end += bytes.len; + const new_end = self.end + bytes.len; + @memcpy(self.buf[self.end..new_end], bytes); + self.end = new_end; return bytes.len; } }; diff --git a/lib/std/io/fixed_buffer_stream.zig b/lib/std/io/fixed_buffer_stream.zig index 2fcb260c72..c170dd1f74 100644 --- a/lib/std/io/fixed_buffer_stream.zig +++ b/lib/std/io/fixed_buffer_stream.zig @@ -45,10 +45,10 @@ pub fn FixedBufferStream(comptime Buffer: type) type { } pub fn read(self: *Self, dest: []u8) ReadError!usize { - const size = std.math.min(dest.len, self.buffer.len - self.pos); + const size = @min(dest.len, self.buffer.len - self.pos); const end = self.pos + size; - mem.copy(u8, dest[0..size], self.buffer[self.pos..end]); + @memcpy(dest[0..size], self.buffer[self.pos..end]); self.pos = end; return size; @@ -67,7 +67,7 @@ pub fn FixedBufferStream(comptime Buffer: type) type { else self.buffer.len - self.pos; - mem.copy(u8, self.buffer[self.pos .. self.pos + n], bytes[0..n]); + @memcpy(self.buffer[self.pos..][0..n], bytes[0..n]); self.pos += n; if (n == 0) return error.NoSpaceLeft; diff --git a/lib/std/io/writer.zig b/lib/std/io/writer.zig index 3160c37ff2..f4b2643e77 100644 --- a/lib/std/io/writer.zig +++ b/lib/std/io/writer.zig @@ -35,7 +35,7 @@ pub fn Writer( pub fn writeByteNTimes(self: Self, byte: u8, n: usize) Error!void { var bytes: [256]u8 = undefined; - mem.set(u8, bytes[0..], byte); + @memset(bytes[0..], byte); var remaining: usize = n; while (remaining > 0) { diff --git a/lib/std/json.zig b/lib/std/json.zig index ca7fb5ad20..432f4e6911 100644 --- a/lib/std/json.zig +++ b/lib/std/json.zig @@ -1667,7 +1667,7 @@ fn parseInternal( const source_slice = stringToken.slice(tokens.slice, tokens.i - 1); if (r.len != stringToken.decodedLength()) return error.LengthMismatch; switch (stringToken.escapes) { - .None => mem.copy(u8, &r, source_slice), + .None => @memcpy(r[0..source_slice.len], source_slice), .Some => try unescapeValidString(&r, source_slice), } return r; @@ -1733,7 +1733,7 @@ fn parseInternal( try allocator.alloc(u8, len); errdefer allocator.free(output); switch (stringToken.escapes) { - .None => mem.copy(u8, output, source_slice), + .None => @memcpy(output[0..source_slice.len], source_slice), .Some => try unescapeValidString(output, source_slice), } diff --git a/lib/std/json/test.zig b/lib/std/json/test.zig index 452341535b..7b5e7e814d 100644 --- a/lib/std/json/test.zig +++ b/lib/std/json/test.zig @@ -2811,7 +2811,7 @@ test "json.serialize issue #5959" { // StreamingParser has multiple internal fields set to undefined. This causes issues when using // expectEqual so these are zeroed. We are testing for equality here only because this is a // known small test reproduction which hits the relevant LLVM issue. - std.mem.set(u8, @ptrCast([*]u8, &parser)[0..@sizeOf(StreamingParser)], 0); + @memset(@ptrCast([*]u8, &parser)[0..@sizeOf(StreamingParser)], 0); try std.testing.expectEqual(parser, parser); } diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig index f97a763d95..2406d669ec 100644 --- a/lib/std/math/big/int.zig +++ b/lib/std/math/big/int.zig @@ -176,7 +176,7 @@ pub const Mutable = struct { /// Asserts the value fits in the limbs buffer. pub fn copy(self: *Mutable, other: Const) void { if (self.limbs.ptr != other.limbs.ptr) { - mem.copy(Limb, self.limbs[0..], other.limbs[0..other.limbs.len]); + @memcpy(self.limbs[0..other.limbs.len], other.limbs[0..other.limbs.len]); } self.positive = other.positive; self.len = other.limbs.len; @@ -199,7 +199,7 @@ pub const Mutable = struct { /// can be modified separately from the original. /// Asserts that limbs is big enough to store the value. pub fn clone(other: Mutable, limbs: []Limb) Mutable { - mem.copy(Limb, limbs, other.limbs[0..other.len]); + @memcpy(limbs[0..other.len], other.limbs[0..other.len]); return .{ .limbs = limbs, .len = other.len, @@ -344,7 +344,7 @@ pub const Mutable = struct { .min => { // Negative bound, signed = -0x80. r.len = req_limbs; - mem.set(Limb, r.limbs[0 .. r.len - 1], 0); + @memset(r.limbs[0 .. r.len - 1], 0); r.limbs[r.len - 1] = signmask; r.positive = false; }, @@ -363,7 +363,7 @@ pub const Mutable = struct { const new_mask = (new_signmask << 1) -% 1; // 0b0..001..1 where the rightmost 0 is the sign bit. r.len = new_req_limbs; - std.mem.set(Limb, r.limbs[0 .. r.len - 1], maxInt(Limb)); + @memset(r.limbs[0 .. r.len - 1], maxInt(Limb)); r.limbs[r.len - 1] = new_mask; } }, @@ -376,7 +376,7 @@ pub const Mutable = struct { .max => { // Max bound, unsigned = 0xFF r.len = req_limbs; - std.mem.set(Limb, r.limbs[0 .. r.len - 1], maxInt(Limb)); + @memset(r.limbs[0 .. r.len - 1], maxInt(Limb)); r.limbs[r.len - 1] = mask; }, }, @@ -489,7 +489,7 @@ pub const Mutable = struct { if (msl < req_limbs) { r.limbs[msl] = 1; r.len = req_limbs; - mem.set(Limb, r.limbs[msl + 1 .. req_limbs], 0); + @memset(r.limbs[msl + 1 .. req_limbs], 0); } else { carry_truncated = true; } @@ -637,14 +637,14 @@ pub const Mutable = struct { const a_copy = if (rma.limbs.ptr == a.limbs.ptr) blk: { const start = buf_index; - mem.copy(Limb, limbs_buffer[buf_index..], a.limbs); + @memcpy(limbs_buffer[buf_index..][0..a.limbs.len], a.limbs); buf_index += a.limbs.len; break :blk a.toMutable(limbs_buffer[start..buf_index]).toConst(); } else a; const b_copy = if (rma.limbs.ptr == b.limbs.ptr) blk: { const start = buf_index; - mem.copy(Limb, limbs_buffer[buf_index..], b.limbs); + @memcpy(limbs_buffer[buf_index..][0..b.limbs.len], b.limbs); buf_index += b.limbs.len; break :blk b.toMutable(limbs_buffer[start..buf_index]).toConst(); } else b; @@ -676,7 +676,7 @@ pub const Mutable = struct { } } - mem.set(Limb, rma.limbs[0 .. a.limbs.len + b.limbs.len], 0); + @memset(rma.limbs[0 .. a.limbs.len + b.limbs.len], 0); llmulacc(.add, allocator, rma.limbs, a.limbs, b.limbs); @@ -708,7 +708,7 @@ pub const Mutable = struct { const a_copy = if (rma.limbs.ptr == a.limbs.ptr) blk: { const start = buf_index; const a_len = math.min(req_limbs, a.limbs.len); - mem.copy(Limb, limbs_buffer[buf_index..], a.limbs[0..a_len]); + @memcpy(limbs_buffer[buf_index..][0..a_len], a.limbs[0..a_len]); buf_index += a_len; break :blk a.toMutable(limbs_buffer[start..buf_index]).toConst(); } else a; @@ -716,7 +716,7 @@ pub const Mutable = struct { const b_copy = if (rma.limbs.ptr == b.limbs.ptr) blk: { const start = buf_index; const b_len = math.min(req_limbs, b.limbs.len); - mem.copy(Limb, limbs_buffer[buf_index..], b.limbs[0..b_len]); + @memcpy(limbs_buffer[buf_index..][0..b_len], b.limbs[0..b_len]); buf_index += b_len; break :blk a.toMutable(limbs_buffer[start..buf_index]).toConst(); } else b; @@ -751,7 +751,7 @@ pub const Mutable = struct { const a_limbs = a.limbs[0..math.min(req_limbs, a.limbs.len)]; const b_limbs = b.limbs[0..math.min(req_limbs, b.limbs.len)]; - mem.set(Limb, rma.limbs[0..req_limbs], 0); + @memset(rma.limbs[0..req_limbs], 0); llmulacc(.add, allocator, rma.limbs, a_limbs, b_limbs); rma.normalize(math.min(req_limbs, a.limbs.len + b.limbs.len)); @@ -919,7 +919,7 @@ pub const Mutable = struct { _ = opt_allocator; assert(rma.limbs.ptr != a.limbs.ptr); // illegal aliasing - mem.set(Limb, rma.limbs, 0); + @memset(rma.limbs, 0); llsquareBasecase(rma.limbs, a.limbs); @@ -1522,7 +1522,7 @@ pub const Mutable = struct { if (xy_trailing != 0) { // Manually shift here since we know its limb aligned. mem.copyBackwards(Limb, r.limbs[xy_trailing..], r.limbs[0..r.len]); - mem.set(Limb, r.limbs[0..xy_trailing], 0); + @memset(r.limbs[0..xy_trailing], 0); r.len += xy_trailing; } } @@ -1556,7 +1556,7 @@ pub const Mutable = struct { // for 0 <= j <= n - t, set q[j] to 0 q.len = shift + 1; q.positive = true; - mem.set(Limb, q.limbs[0..q.len], 0); + @memset(q.limbs[0..q.len], 0); // 2. // while x >= y * b^(n - t): @@ -1691,7 +1691,7 @@ pub const Mutable = struct { r.addScalar(a.abs(), -1); if (req_limbs > r.len) { - mem.set(Limb, r.limbs[r.len..req_limbs], 0); + @memset(r.limbs[r.len..req_limbs], 0); } assert(r.limbs.len >= req_limbs); @@ -1730,7 +1730,7 @@ pub const Mutable = struct { // Zero-extend the result if (req_limbs > r.len) { - mem.set(Limb, r.limbs[r.len..req_limbs], 0); + @memset(r.limbs[r.len..req_limbs], 0); } // Truncate to required number of limbs. @@ -1921,8 +1921,8 @@ pub const Const = struct { /// The result is an independent resource which is managed by the caller. pub fn toManaged(self: Const, allocator: Allocator) Allocator.Error!Managed { - const limbs = try allocator.alloc(Limb, math.max(Managed.default_capacity, self.limbs.len)); - mem.copy(Limb, limbs, self.limbs); + const limbs = try allocator.alloc(Limb, @max(Managed.default_capacity, self.limbs.len)); + @memcpy(limbs[0..self.limbs.len], self.limbs); return Managed{ .allocator = allocator, .limbs = limbs, @@ -1935,7 +1935,7 @@ pub const Const = struct { /// Asserts `limbs` is big enough to store the value. pub fn toMutable(self: Const, limbs: []Limb) Mutable { - mem.copy(Limb, limbs, self.limbs[0..self.limbs.len]); + @memcpy(limbs[0..self.limbs.len], self.limbs[0..self.limbs.len]); return .{ .limbs = limbs, .positive = self.positive, @@ -2253,7 +2253,7 @@ pub const Const = struct { .positive = true, // Make absolute by ignoring self.positive. .len = self.limbs.len, }; - mem.copy(Limb, q.limbs, self.limbs); + @memcpy(q.limbs[0..self.limbs.len], self.limbs); var r: Mutable = .{ .limbs = limbs_buffer[q.limbs.len..][0..self.limbs.len], @@ -2587,8 +2587,8 @@ pub const Managed = struct { .allocator = allocator, .metadata = other.metadata, .limbs = block: { - var limbs = try allocator.alloc(Limb, other.len()); - mem.copy(Limb, limbs[0..], other.limbs[0..other.len()]); + const limbs = try allocator.alloc(Limb, other.len()); + @memcpy(limbs, other.limbs[0..other.len()]); break :block limbs; }, }; @@ -2600,7 +2600,7 @@ pub const Managed = struct { if (self.limbs.ptr == other.limbs.ptr) return; try self.ensureCapacity(other.limbs.len); - mem.copy(Limb, self.limbs[0..], other.limbs[0..other.limbs.len]); + @memcpy(self.limbs[0..other.limbs.len], other.limbs[0..other.limbs.len]); self.setMetadata(other.positive, other.limbs.len); } @@ -3302,7 +3302,7 @@ fn llmulaccKaratsuba( // Note, we don't need to compute all of p2, just enough limbs to satisfy r. const p2_limbs = math.min(limbs_after_split, a1.len + b1.len); - mem.set(Limb, tmp[0..p2_limbs], 0); + @memset(tmp[0..p2_limbs], 0); llmulacc(.add, allocator, tmp[0..p2_limbs], a1[0..math.min(a1.len, p2_limbs)], b1[0..math.min(b1.len, p2_limbs)]); const p2 = tmp[0..llnormalize(tmp[0..p2_limbs])]; @@ -3317,7 +3317,7 @@ fn llmulaccKaratsuba( // Compute p0. // Since a0.len, b0.len <= split and r.len >= split * 2, the full width of p0 needs to be computed. const p0_limbs = a0.len + b0.len; - mem.set(Limb, tmp[0..p0_limbs], 0); + @memset(tmp[0..p0_limbs], 0); llmulacc(.add, allocator, tmp[0..p0_limbs], a0, b0); const p0 = tmp[0..llnormalize(tmp[0..p0_limbs])]; @@ -3341,7 +3341,7 @@ fn llmulaccKaratsuba( return; } - mem.set(Limb, tmp, 0); + @memset(tmp, 0); // p1 is nonzero, so compute the intermediary terms j0 = a0 - a1 and j1 = b1 - b0. // Note that in this case, we again need some storage for intermediary results @@ -3666,7 +3666,7 @@ fn llshl(r: []Limb, a: []const Limb, shift: usize) void { } r[limb_shift - 1] = carry; - mem.set(Limb, r[0 .. limb_shift - 1], 0); + @memset(r[0 .. limb_shift - 1], 0); } fn llshr(r: []Limb, a: []const Limb, shift: usize) void { @@ -4061,8 +4061,8 @@ fn llpow(r: []Limb, a: []const Limb, b: u32, tmp_limbs: []Limb) void { tmp2 = tmp_limbs; } - mem.copy(Limb, tmp1, a); - mem.set(Limb, tmp1[a.len..], 0); + @memcpy(tmp1[0..a.len], a); + @memset(tmp1[a.len..], 0); // Scan the exponent as a binary number, from left to right, dropping the // most significant bit set. @@ -4074,14 +4074,14 @@ fn llpow(r: []Limb, a: []const Limb, b: u32, tmp_limbs: []Limb) void { var i: usize = 0; while (i < exp_bits) : (i += 1) { // Square - mem.set(Limb, tmp2, 0); + @memset(tmp2, 0); llsquareBasecase(tmp2, tmp1[0..llnormalize(tmp1)]); mem.swap([]Limb, &tmp1, &tmp2); // Multiply by a const ov = @shlWithOverflow(exp, 1); exp = ov[0]; if (ov[1] != 0) { - mem.set(Limb, tmp2, 0); + @memset(tmp2, 0); llmulacc(.add, null, tmp2, tmp1[0..llnormalize(tmp1)], a); mem.swap([]Limb, &tmp1, &tmp2); } diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 4b796c851a..5dae48d059 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -192,12 +192,14 @@ test "Allocator.resize" { } } +/// Deprecated: use `copyForwards` +pub const copy = copyForwards; + /// Copy all of source into dest at position 0. /// dest.len must be >= source.len. /// If the slices overlap, dest.ptr must be <= src.ptr. -pub fn copy(comptime T: type, dest: []T, source: []const T) void { - for (dest[0..source.len], source) |*d, s| - d.* = s; +pub fn copyForwards(comptime T: type, dest: []T, source: []const T) void { + for (dest[0..source.len], source) |*d, s| d.* = s; } /// Copy all of source into dest at position 0. @@ -3124,7 +3126,7 @@ pub fn replace(comptime T: type, input: []const T, needle: []const T, replacemen var replacements: usize = 0; while (slide < input.len) { if (mem.startsWith(T, input[slide..], needle)) { - mem.copy(T, output[i .. i + replacement.len], replacement); + @memcpy(output[i..][0..replacement.len], replacement); i += replacement.len; slide += needle.len; replacements += 1; diff --git a/lib/std/mem/Allocator.zig b/lib/std/mem/Allocator.zig index b402fab3fa..5110534ed4 100644 --- a/lib/std/mem/Allocator.zig +++ b/lib/std/mem/Allocator.zig @@ -307,14 +307,14 @@ pub fn free(self: Allocator, memory: anytype) void { /// Copies `m` to newly allocated memory. Caller owns the memory. pub fn dupe(allocator: Allocator, comptime T: type, m: []const T) ![]T { const new_buf = try allocator.alloc(T, m.len); - mem.copy(T, new_buf, m); + @memcpy(new_buf, m); return new_buf; } /// Copies `m` to newly allocated memory, with a null-terminated element. Caller owns the memory. pub fn dupeZ(allocator: Allocator, comptime T: type, m: []const T) ![:0]T { const new_buf = try allocator.alloc(T, m.len + 1); - mem.copy(T, new_buf, m); + @memcpy(new_buf[0..m.len], m); new_buf[m.len] = 0; return new_buf[0..m.len :0]; } diff --git a/lib/std/multi_array_list.zig b/lib/std/multi_array_list.zig index 063e2e5620..322471bedf 100644 --- a/lib/std/multi_array_list.zig +++ b/lib/std/multi_array_list.zig @@ -380,7 +380,7 @@ pub fn MultiArrayList(comptime T: type) type { inline for (fields, 0..) |field_info, i| { if (@sizeOf(field_info.type) != 0) { const field = @intToEnum(Field, i); - mem.copy(field_info.type, other_slice.items(field), self_slice.items(field)); + @memcpy(other_slice.items(field), self_slice.items(field)); } } gpa.free(self.allocatedBytes()); @@ -441,7 +441,7 @@ pub fn MultiArrayList(comptime T: type) type { inline for (fields, 0..) |field_info, i| { if (@sizeOf(field_info.type) != 0) { const field = @intToEnum(Field, i); - mem.copy(field_info.type, other_slice.items(field), self_slice.items(field)); + @memcpy(other_slice.items(field), self_slice.items(field)); } } gpa.free(self.allocatedBytes()); @@ -460,7 +460,7 @@ pub fn MultiArrayList(comptime T: type) type { inline for (fields, 0..) |field_info, i| { if (@sizeOf(field_info.type) != 0) { const field = @intToEnum(Field, i); - mem.copy(field_info.type, result_slice.items(field), self_slice.items(field)); + @memcpy(result_slice.items(field), self_slice.items(field)); } } return result; diff --git a/lib/std/net.zig b/lib/std/net.zig index d59676ec17..70b4b2fd91 100644 --- a/lib/std/net.zig +++ b/lib/std/net.zig @@ -106,7 +106,7 @@ pub const Address = extern union { // Add 1 to ensure a terminating 0 is present in the path array for maximum portability. if (path.len + 1 > sock_addr.path.len) return error.NameTooLong; - mem.set(u8, &sock_addr.path, 0); + @memset(&sock_addr.path, 0); mem.copy(u8, &sock_addr.path, path); return Address{ .un = sock_addr }; @@ -346,7 +346,7 @@ pub const Ip6Address = extern struct { if (!saw_any_digits) { if (abbrv) return error.InvalidCharacter; // ':::' if (i != 0) abbrv = true; - mem.set(u8, ip_slice[index..], 0); + @memset(ip_slice[index..], 0); ip_slice = tail[0..]; index = 0; continue; @@ -465,7 +465,7 @@ pub const Ip6Address = extern struct { if (!saw_any_digits) { if (abbrv) return error.InvalidCharacter; // ':::' if (i != 0) abbrv = true; - mem.set(u8, ip_slice[index..], 0); + @memset(ip_slice[index..], 0); ip_slice = tail[0..]; index = 0; continue; diff --git a/lib/std/os.zig b/lib/std/os.zig index 9a0c05f457..32c73916d4 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -1881,9 +1881,9 @@ pub fn execvpeZ_expandArg0( while (it.next()) |search_path| { const path_len = search_path.len + file_slice.len + 1; if (path_buf.len < path_len + 1) return error.NameTooLong; - mem.copy(u8, &path_buf, search_path); + @memcpy(path_buf[0..search_path.len], search_path); path_buf[search_path.len] = '/'; - mem.copy(u8, path_buf[search_path.len + 1 ..], file_slice); + @memcpy(path_buf[search_path.len + 1 ..][0..file_slice.len], file_slice); path_buf[path_len] = 0; const full_path = path_buf[0..path_len :0].ptr; switch (arg0_expand) { @@ -1917,7 +1917,7 @@ pub fn getenv(key: []const u8) ?[]const u8 { if (builtin.link_libc) { var small_key_buf: [64]u8 = undefined; if (key.len < small_key_buf.len) { - mem.copy(u8, &small_key_buf, key); + @memcpy(small_key_buf[0..key.len], key); small_key_buf[key.len] = 0; const key0 = small_key_buf[0..key.len :0]; return getenvZ(key0); @@ -2022,8 +2022,9 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 { } else if (builtin.os.tag == .wasi and !builtin.link_libc) { const path = "."; if (out_buffer.len < path.len) return error.NameTooLong; - std.mem.copy(u8, out_buffer, path); - return out_buffer[0..path.len]; + const result = out_buffer[0..path.len]; + @memcpy(result, path); + return result; } const err = if (builtin.link_libc) blk: { @@ -2673,7 +2674,7 @@ pub fn renameatW( .FileNameLength = @intCast(u32, new_path_w.len * 2), // already checked error.NameTooLong .FileName = undefined, }; - std.mem.copy(u16, @as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w); + @memcpy(@as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w); var io_status_block: windows.IO_STATUS_BLOCK = undefined; @@ -5264,8 +5265,9 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { } const len = mem.indexOfScalar(u8, &kfile.path, 0) orelse MAX_PATH_BYTES; if (len == 0) return error.NameTooLong; - mem.copy(u8, out_buffer, kfile.path[0..len]); - return out_buffer[0..len]; + const result = out_buffer[0..len]; + @memcpy(result, kfile.path[0..len]); + return result; } else { // This fallback implementation reimplements libutil's `kinfo_getfile()`. // The motivation is to avoid linking -lutil when building zig or general @@ -5296,8 +5298,9 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 { if (kf.fd == fd) { len = mem.indexOfScalar(u8, &kf.path, 0) orelse MAX_PATH_BYTES; if (len == 0) return error.NameTooLong; - mem.copy(u8, out_buffer, kf.path[0..len]); - return out_buffer[0..len]; + const result = out_buffer[0..len]; + @memcpy(result, kf.path[0..len]); + return result; } i += @intCast(usize, kf.structsize); } @@ -5686,8 +5689,9 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 { if (builtin.os.tag == .linux) { const uts = uname(); const hostname = mem.sliceTo(&uts.nodename, 0); - mem.copy(u8, name_buffer, hostname); - return name_buffer[0..hostname.len]; + const result = name_buffer[0..hostname.len]; + @memcpy(result, hostname); + return result; } @compileError("TODO implement gethostname for this OS"); @@ -5725,7 +5729,7 @@ pub fn res_mkquery( @memset(q[0..n], 0); q[2] = @as(u8, op) * 8 + 1; q[5] = 1; - mem.copy(u8, q[13..], name); + @memcpy(q[13..][0..name.len], name); var i: usize = 13; var j: usize = undefined; while (q[i] != 0) : (i = j + 1) { @@ -5748,7 +5752,7 @@ pub fn res_mkquery( q[0] = @truncate(u8, id / 256); q[1] = @truncate(u8, id); - mem.copy(u8, buf, q[0..n]); + @memcpy(buf[0..n], q[0..n]); return n; } @@ -6755,7 +6759,7 @@ fn toMemFdPath(name: []const u8) ![MFD_MAX_NAME_LEN:0]u8 { var path_with_null: [MFD_MAX_NAME_LEN:0]u8 = undefined; // >= rather than > to make room for the null byte if (name.len >= MFD_MAX_NAME_LEN) return error.NameTooLong; - mem.copy(u8, &path_with_null, name); + @memcpy(path_with_null[0..name.len], name); path_with_null[name.len] = 0; return path_with_null; } diff --git a/lib/std/os/linux/io_uring.zig b/lib/std/os/linux/io_uring.zig index 20248fd65f..254bc08cb8 100644 --- a/lib/std/os/linux/io_uring.zig +++ b/lib/std/os/linux/io_uring.zig @@ -1855,7 +1855,7 @@ test "write_fixed/read_fixed" { var raw_buffers: [2][11]u8 = undefined; // First buffer will be written to the file. - std.mem.set(u8, &raw_buffers[0], 'z'); + @memset(&raw_buffers[0], 'z'); std.mem.copy(u8, &raw_buffers[0], "foobar"); var buffers = [2]os.iovec{ @@ -2966,7 +2966,7 @@ test "provide_buffers: read" { // Provide 1 buffer again // Deliberately put something we don't expect in the buffers - mem.set(u8, mem.sliceAsBytes(&buffers), 42); + @memset(mem.sliceAsBytes(&buffers), 42); const reprovided_buffer_id = 2; @@ -3155,7 +3155,7 @@ test "provide_buffers: accept/connect/send/recv" { // Do 4 recv which should consume all buffers // Deliberately put something we don't expect in the buffers - mem.set(u8, mem.sliceAsBytes(&buffers), 1); + @memset(mem.sliceAsBytes(&buffers), 1); var i: usize = 0; while (i < buffers.len) : (i += 1) { @@ -3235,7 +3235,7 @@ test "provide_buffers: accept/connect/send/recv" { // Final recv which should work // Deliberately put something we don't expect in the buffers - mem.set(u8, mem.sliceAsBytes(&buffers), 1); + @memset(mem.sliceAsBytes(&buffers), 1); { var sqe = try ring.recv(0xdfdfdfdf, socket_test_harness.client, .{ .buffer_selection = .{ .group_id = group_id, .len = buffer_len } }, 0); diff --git a/lib/std/os/linux/tls.zig b/lib/std/os/linux/tls.zig index cffdec0424..ea012aae15 100644 --- a/lib/std/os/linux/tls.zig +++ b/lib/std/os/linux/tls.zig @@ -275,7 +275,7 @@ inline fn alignPtrCast(comptime T: type, ptr: [*]u8) *T { /// architecture-specific value of the thread-pointer register pub fn prepareTLS(area: []u8) usize { // Clear the area we're going to use, just to be safe - mem.set(u8, area, 0); + @memset(area, 0); // Prepare the DTV const dtv = alignPtrCast(DTV, area.ptr + tls_image.dtv_offset); dtv.entries = 1; diff --git a/lib/std/os/test.zig b/lib/std/os/test.zig index 682bd4f6f8..f694ea277a 100644 --- a/lib/std/os/test.zig +++ b/lib/std/os/test.zig @@ -587,7 +587,7 @@ test "mmap" { try testing.expect(mem.eql(u8, data, &[_]u8{0x00} ** 1234)); // Make sure the memory is writeable as requested - std.mem.set(u8, data, 0x55); + @memset(data, 0x55); try testing.expect(mem.eql(u8, data, &[_]u8{0x55} ** 1234)); } diff --git a/lib/std/process.zig b/lib/std/process.zig index 56e3708f3b..504f9075eb 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -855,7 +855,7 @@ pub fn argsAlloc(allocator: Allocator) ![][:0]u8 { const result_slice_list = mem.bytesAsSlice([:0]u8, buf[0..slice_list_bytes]); const result_contents = buf[slice_list_bytes..]; - mem.copy(u8, result_contents, contents_slice); + @memcpy(result_contents[0..contents_slice.len], contents_slice); var contents_index: usize = 0; for (slice_sizes, 0..) |len, i| { diff --git a/lib/std/rand/ChaCha.zig b/lib/std/rand/ChaCha.zig index 0992aeb97e..3878fb25c8 100644 --- a/lib/std/rand/ChaCha.zig +++ b/lib/std/rand/ChaCha.zig @@ -40,7 +40,8 @@ pub fn addEntropy(self: *Self, bytes: []const u8) void { } if (i < bytes.len) { var k = [_]u8{0} ** Cipher.key_length; - mem.copy(u8, k[0..], bytes[i..]); + const src = bytes[i..]; + @memcpy(k[0..src.len], src); Cipher.xor( self.state[0..Cipher.key_length], self.state[0..Cipher.key_length], @@ -72,8 +73,8 @@ pub fn fill(self: *Self, buf_: []u8) void { if (avail > 0) { // Bytes from the current block const n = @min(avail, buf.len); - mem.copy(u8, buf[0..n], bytes[self.offset..][0..n]); - mem.set(u8, bytes[self.offset..][0..n], 0); + @memcpy(buf[0..n], bytes[self.offset..][0..n]); + @memset(bytes[self.offset..][0..n], 0); buf = buf[n..]; self.offset += n; } @@ -83,15 +84,15 @@ pub fn fill(self: *Self, buf_: []u8) void { // Full blocks while (buf.len >= bytes.len) { - mem.copy(u8, buf[0..bytes.len], bytes); + @memcpy(buf[0..bytes.len], bytes); buf = buf[bytes.len..]; self.refill(); } // Remaining bytes if (buf.len > 0) { - mem.copy(u8, buf, bytes[0..buf.len]); - mem.set(u8, bytes[0..buf.len], 0); + @memcpy(buf, bytes[0..buf.len]); + @memset(bytes[0..buf.len], 0); self.offset = buf.len; } } diff --git a/lib/std/rand/Isaac64.zig b/lib/std/rand/Isaac64.zig index 42242008fc..8c6205e1cd 100644 --- a/lib/std/rand/Isaac64.zig +++ b/lib/std/rand/Isaac64.zig @@ -87,7 +87,7 @@ fn next(self: *Isaac64) u64 { fn seed(self: *Isaac64, init_s: u64, comptime rounds: usize) void { // We ignore the multi-pass requirement since we don't currently expose full access to // seeding the self.m array completely. - mem.set(u64, self.m[0..], 0); + @memset(self.m[0..], 0); self.m[0] = init_s; // prescrambled golden ratio constants @@ -143,7 +143,7 @@ fn seed(self: *Isaac64, init_s: u64, comptime rounds: usize) void { } } - mem.set(u64, self.r[0..], 0); + @memset(self.r[0..], 0); self.a = 0; self.b = 0; self.c = 0; diff --git a/lib/std/segmented_list.zig b/lib/std/segmented_list.zig index 7c0a27b5b7..172fe4e7c3 100644 --- a/lib/std/segmented_list.zig +++ b/lib/std/segmented_list.zig @@ -230,7 +230,7 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type allocator.free(new_dynamic_segments); } else { // Good thing we allocated that new memory slice. - mem.copy([*]T, new_dynamic_segments, self.dynamic_segments[0..new_cap_shelf_count]); + @memcpy(new_dynamic_segments, self.dynamic_segments[0..new_cap_shelf_count]); allocator.free(self.dynamic_segments); self.dynamic_segments = new_dynamic_segments; } @@ -248,24 +248,21 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type var i = start; if (end <= prealloc_item_count) { - mem.copy(T, dest[i - start ..], self.prealloc_segment[i..end]); + const src = self.prealloc_segment[i..end]; + @memcpy(dest[i - start ..][0..src.len], src); return; } else if (i < prealloc_item_count) { - mem.copy(T, dest[i - start ..], self.prealloc_segment[i..]); + const src = self.prealloc_segment[i..]; + @memcpy(dest[i - start ..][0..src.len], src); i = prealloc_item_count; } while (i < end) { const shelf_index = shelfIndex(i); const copy_start = boxIndex(i, shelf_index); - const copy_end = std.math.min(shelfSize(shelf_index), copy_start + end - i); - - mem.copy( - T, - dest[i - start ..], - self.dynamic_segments[shelf_index][copy_start..copy_end], - ); - + const copy_end = @min(shelfSize(shelf_index), copy_start + end - i); + const src = self.dynamic_segments[shelf_index][copy_start..copy_end]; + @memcpy(dest[i - start ..][0..src.len], src); i += (copy_end - copy_start); } } @@ -498,11 +495,11 @@ fn testSegmentedList(comptime prealloc: usize) !void { control[@intCast(usize, i)] = i + 1; } - mem.set(i32, dest[0..], 0); + @memset(dest[0..], 0); list.writeToSlice(dest[0..], 0); try testing.expect(mem.eql(i32, control[0..], dest[0..])); - mem.set(i32, dest[0..], 0); + @memset(dest[0..], 0); list.writeToSlice(dest[50..], 50); try testing.expect(mem.eql(i32, control[50..], dest[50..])); } diff --git a/lib/std/sort.zig b/lib/std/sort.zig index d2b79db91b..080ab5200e 100644 --- a/lib/std/sort.zig +++ b/lib/std/sort.zig @@ -361,8 +361,10 @@ pub fn sort( if (lessThan(context, items[B1.end - 1], items[A1.start])) { // the two ranges are in reverse order, so copy them in reverse order into the cache - mem.copy(T, cache[B1.length()..], items[A1.start..A1.end]); - mem.copy(T, cache[0..], items[B1.start..B1.end]); + const a1_items = items[A1.start..A1.end]; + @memcpy(cache[B1.length()..][0..a1_items.len], a1_items); + const b1_items = items[B1.start..B1.end]; + @memcpy(cache[0..b1_items.len], b1_items); } else if (lessThan(context, items[B1.start], items[A1.end - 1])) { // these two ranges weren't already in order, so merge them into the cache mergeInto(T, items, A1, B1, context, lessThan, cache[0..]); @@ -371,23 +373,29 @@ pub fn sort( if (!lessThan(context, items[B2.start], items[A2.end - 1]) and !lessThan(context, items[A2.start], items[B1.end - 1])) continue; // copy A1 and B1 into the cache in the same order - mem.copy(T, cache[0..], items[A1.start..A1.end]); - mem.copy(T, cache[A1.length()..], items[B1.start..B1.end]); + const a1_items = items[A1.start..A1.end]; + @memcpy(cache[0..a1_items.len], a1_items); + const b1_items = items[B1.start..B1.end]; + @memcpy(cache[A1.length()..][0..b1_items.len], b1_items); } A1 = Range.init(A1.start, B1.end); // merge A2 and B2 into the cache if (lessThan(context, items[B2.end - 1], items[A2.start])) { // the two ranges are in reverse order, so copy them in reverse order into the cache - mem.copy(T, cache[A1.length() + B2.length() ..], items[A2.start..A2.end]); - mem.copy(T, cache[A1.length()..], items[B2.start..B2.end]); + const a2_items = items[A2.start..A2.end]; + @memcpy(cache[A1.length() + B2.length() ..][0..a2_items.len], a2_items); + const b2_items = items[B2.start..B2.end]; + @memcpy(cache[A1.length()..][0..b2_items.len], b2_items); } else if (lessThan(context, items[B2.start], items[A2.end - 1])) { // these two ranges weren't already in order, so merge them into the cache mergeInto(T, items, A2, B2, context, lessThan, cache[A1.length()..]); } else { // copy A2 and B2 into the cache in the same order - mem.copy(T, cache[A1.length()..], items[A2.start..A2.end]); - mem.copy(T, cache[A1.length() + A2.length() ..], items[B2.start..B2.end]); + const a2_items = items[A2.start..A2.end]; + @memcpy(cache[A1.length()..][0..a2_items.len], a2_items); + const b2_items = items[B2.start..B2.end]; + @memcpy(cache[A1.length() + A2.length() ..][0..b2_items.len], b2_items); } A2 = Range.init(A2.start, B2.end); @@ -397,15 +405,19 @@ pub fn sort( if (lessThan(context, cache[B3.end - 1], cache[A3.start])) { // the two ranges are in reverse order, so copy them in reverse order into the items - mem.copy(T, items[A1.start + A2.length() ..], cache[A3.start..A3.end]); - mem.copy(T, items[A1.start..], cache[B3.start..B3.end]); + const a3_items = cache[A3.start..A3.end]; + @memcpy(items[A1.start + A2.length() ..][0..a3_items.len], a3_items); + const b3_items = cache[B3.start..B3.end]; + @memcpy(items[A1.start..][0..b3_items.len], b3_items); } else if (lessThan(context, cache[B3.start], cache[A3.end - 1])) { // these two ranges weren't already in order, so merge them back into the items mergeInto(T, cache[0..], A3, B3, context, lessThan, items[A1.start..]); } else { // copy A3 and B3 into the items in the same order - mem.copy(T, items[A1.start..], cache[A3.start..A3.end]); - mem.copy(T, items[A1.start + A1.length() ..], cache[B3.start..B3.end]); + const a3_items = cache[A3.start..A3.end]; + @memcpy(items[A1.start..][0..a3_items.len], a3_items); + const b3_items = cache[B3.start..B3.end]; + @memcpy(items[A1.start + A1.length() ..][0..b3_items.len], b3_items); } } @@ -423,7 +435,8 @@ pub fn sort( mem.rotate(T, items[A.start..B.end], A.length()); } else if (lessThan(context, items[B.start], items[A.end - 1])) { // these two ranges weren't already in order, so we'll need to merge them! - mem.copy(T, cache[0..], items[A.start..A.end]); + const a_items = items[A.start..A.end]; + @memcpy(cache[0..a_items.len], a_items); mergeExternal(T, items, A, B, context, lessThan, cache[0..]); } } @@ -718,7 +731,8 @@ pub fn sort( // if the first unevenly sized A block fits into the cache, copy it there for when we go to Merge it // otherwise, if the second buffer is available, block swap the contents into that if (lastA.length() <= cache.len) { - mem.copy(T, cache[0..], items[lastA.start..lastA.end]); + const last_a_items = items[lastA.start..lastA.end]; + @memcpy(cache[0..last_a_items.len], last_a_items); } else if (buffer2.length() > 0) { blockSwap(T, items, lastA.start, buffer2.start, lastA.length()); } @@ -762,7 +776,7 @@ pub fn sort( if (buffer2.length() > 0 or block_size <= cache.len) { // copy the previous A block into the cache or buffer2, since that's where we need it to be when we go to merge it anyway if (block_size <= cache.len) { - mem.copy(T, cache[0..], items[blockA.start .. blockA.start + block_size]); + @memcpy(cache[0..block_size], items[blockA.start..][0..block_size]); } else { blockSwap(T, items, blockA.start, buffer2.start, block_size); } @@ -1122,7 +1136,8 @@ fn mergeInto( insert_index += 1; if (A_index == A_last) { // copy the remainder of B into the final array - mem.copy(T, into[insert_index..], from[B_index..B_last]); + const from_b = from[B_index..B_last]; + @memcpy(into[insert_index..][0..from_b.len], from_b); break; } } else { @@ -1131,7 +1146,8 @@ fn mergeInto( insert_index += 1; if (B_index == B_last) { // copy the remainder of A into the final array - mem.copy(T, into[insert_index..], from[A_index..A_last]); + const from_a = from[A_index..A_last]; + @memcpy(into[insert_index..][0..from_a.len], from_a); break; } } @@ -1171,7 +1187,8 @@ fn mergeExternal( } // copy the remainder of A into the final array - mem.copy(T, items[insert_index..], cache[A_index..A_last]); + const cache_a = cache[A_index..A_last]; + @memcpy(items[insert_index..][0..cache_a.len], cache_a); } fn swap( @@ -1305,7 +1322,7 @@ test "sort" { for (u8cases) |case| { var buf: [8]u8 = undefined; const slice = buf[0..case[0].len]; - mem.copy(u8, slice, case[0]); + @memcpy(slice, case[0]); sort(u8, slice, {}, asc_u8); try testing.expect(mem.eql(u8, slice, case[1])); } @@ -1340,7 +1357,7 @@ test "sort" { for (i32cases) |case| { var buf: [8]i32 = undefined; const slice = buf[0..case[0].len]; - mem.copy(i32, slice, case[0]); + @memcpy(slice, case[0]); sort(i32, slice, {}, asc_i32); try testing.expect(mem.eql(i32, slice, case[1])); } @@ -1377,7 +1394,7 @@ test "sort descending" { for (rev_cases) |case| { var buf: [8]i32 = undefined; const slice = buf[0..case[0].len]; - mem.copy(i32, slice, case[0]); + @memcpy(slice, case[0]); sort(i32, slice, {}, desc_i32); try testing.expect(mem.eql(i32, slice, case[1])); } diff --git a/lib/std/target.zig b/lib/std/target.zig index d9214f7be6..f23e32fbe7 100644 --- a/lib/std/target.zig +++ b/lib/std/target.zig @@ -1596,7 +1596,7 @@ pub const Target = struct { /// Asserts that the length is less than or equal to 255 bytes. pub fn set(self: *DynamicLinker, dl_or_null: ?[]const u8) void { if (dl_or_null) |dl| { - mem.copy(u8, &self.buffer, dl); + @memcpy(self.buffer[0..dl.len], dl); self.max_byte = @intCast(u8, dl.len - 1); } else { self.max_byte = null; @@ -1612,7 +1612,7 @@ pub const Target = struct { return r.*; } fn copy(r: *DynamicLinker, s: []const u8) DynamicLinker { - mem.copy(u8, &r.buffer, s); + @memcpy(r.buffer[0..s.len], s); r.max_byte = @intCast(u8, s.len - 1); return r.*; } diff --git a/lib/std/testing/failing_allocator.zig b/lib/std/testing/failing_allocator.zig index 914e405b11..2cdb78cd1d 100644 --- a/lib/std/testing/failing_allocator.zig +++ b/lib/std/testing/failing_allocator.zig @@ -66,7 +66,7 @@ pub const FailingAllocator = struct { const self = @ptrCast(*FailingAllocator, @alignCast(@alignOf(FailingAllocator), ctx)); if (self.index == self.fail_index) { if (!self.has_induced_failure) { - mem.set(usize, &self.stack_addresses, 0); + @memset(&self.stack_addresses, 0); var stack_trace = std.builtin.StackTrace{ .instruction_addresses = &self.stack_addresses, .index = 0, diff --git a/lib/std/tz.zig b/lib/std/tz.zig index 76eb3b06fc..0cb9cefa50 100644 --- a/lib/std/tz.zig +++ b/lib/std/tz.zig @@ -137,7 +137,7 @@ pub const Tz = struct { const name = std.mem.sliceTo(designators[tt.name_data[0]..], 0); // We are mandating the "SHOULD" 6-character limit so we can pack the struct better, and to conform to POSIX. if (name.len > 6) return error.Malformed; // rfc8536: Time zone designations SHOULD consist of at least three (3) and no more than six (6) ASCII characters. - std.mem.copy(u8, tt.name_data[0..], name); + @memcpy(tt.name_data[0..name.len], name); tt.name_data[name.len] = 0; } diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 2cb1d170b4..d8a8c8e9ed 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -1889,11 +1889,11 @@ fn renderArrayInit( // A place to store the width of each expression and its column's maximum const widths = try gpa.alloc(usize, row_exprs.len + row_size); defer gpa.free(widths); - mem.set(usize, widths, 0); + @memset(widths, 0); const expr_newlines = try gpa.alloc(bool, row_exprs.len); defer gpa.free(expr_newlines); - mem.set(bool, expr_newlines, false); + @memset(expr_newlines, false); const expr_widths = widths[0..row_exprs.len]; const column_widths = widths[row_exprs.len..]; diff --git a/lib/std/zig/system/NativeTargetInfo.zig b/lib/std/zig/system/NativeTargetInfo.zig index 987358ed5a..539ad96365 100644 --- a/lib/std/zig/system/NativeTargetInfo.zig +++ b/lib/std/zig/system/NativeTargetInfo.zig @@ -877,17 +877,17 @@ pub fn abiAndDynamicLinkerFromFile( const cpu_arch = @tagName(result.target.cpu.arch); const os_tag = @tagName(result.target.os.tag); const abi = @tagName(result.target.abi); - mem.copy(u8, path_buf[index..], prefix); + @memcpy(path_buf[index..][0..prefix.len], prefix); index += prefix.len; - mem.copy(u8, path_buf[index..], cpu_arch); + @memcpy(path_buf[index..][0..cpu_arch.len], cpu_arch); index += cpu_arch.len; path_buf[index] = '-'; index += 1; - mem.copy(u8, path_buf[index..], os_tag); + @memcpy(path_buf[index..][0..os_tag.len], os_tag); index += os_tag.len; path_buf[index] = '-'; index += 1; - mem.copy(u8, path_buf[index..], abi); + @memcpy(path_buf[index..][0..abi.len], abi); index += abi.len; const rpath = path_buf[0..index]; if (glibcVerFromRPath(rpath)) |ver| { diff --git a/lib/std/zig/system/windows.zig b/lib/std/zig/system/windows.zig index 45b44560a2..6039425b1a 100644 --- a/lib/std/zig/system/windows.zig +++ b/lib/std/zig/system/windows.zig @@ -171,10 +171,10 @@ fn getCpuInfoFromRegistry(core: usize, args: anytype) !void { const entry = @ptrCast([*]align(1) const u8, table[i + 1].EntryContext); switch (@field(args, field.name).value_type) { REG.DWORD, REG.DWORD_BIG_ENDIAN => { - mem.copy(u8, @field(args, field.name).value_buf[0..4], entry[0..4]); + @memcpy(@field(args, field.name).value_buf[0..4], entry[0..4]); }, REG.QWORD => { - mem.copy(u8, @field(args, field.name).value_buf[0..8], entry[0..8]); + @memcpy(@field(args, field.name).value_buf[0..8], entry[0..8]); }, else => unreachable, } diff --git a/src/Compilation.zig b/src/Compilation.zig index f11f158e1b..a5b785cc67 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -2165,7 +2165,7 @@ fn wholeCacheModeSetBinFilePath(comp: *Compilation, digest: *const [Cache.hex_di const digest_start = 2; // "o/[digest]/[basename]" if (comp.whole_bin_sub_path) |sub_path| { - mem.copy(u8, sub_path[digest_start..], digest); + @memcpy(sub_path[digest_start..][0..digest.len], digest); comp.bin_file.options.emit = .{ .directory = comp.local_cache_directory, @@ -2174,7 +2174,7 @@ fn wholeCacheModeSetBinFilePath(comp: *Compilation, digest: *const [Cache.hex_di } if (comp.whole_implib_sub_path) |sub_path| { - mem.copy(u8, sub_path[digest_start..], digest); + @memcpy(sub_path[digest_start..][0..digest.len], digest); comp.bin_file.options.implib_emit = .{ .directory = comp.local_cache_directory, @@ -4432,7 +4432,7 @@ pub fn addCCArgs( assert(prefix.len == prefix_len); var march_buf: [prefix_len + letters.len + 1]u8 = undefined; var march_index: usize = prefix_len; - mem.copy(u8, &march_buf, prefix); + @memcpy(march_buf[0..prefix.len], prefix); if (std.Target.riscv.featureSetHas(target.cpu.features, .e)) { march_buf[march_index] = 'e'; diff --git a/src/Liveness.zig b/src/Liveness.zig index 6990ade327..deb6c330c2 100644 --- a/src/Liveness.zig +++ b/src/Liveness.zig @@ -156,7 +156,7 @@ pub fn analyze(gpa: Allocator, air: Air) Allocator.Error!Liveness { errdefer a.special.deinit(gpa); defer a.extra.deinit(gpa); - std.mem.set(usize, a.tomb_bits, 0); + @memset(a.tomb_bits, 0); const main_body = air.getMainBody(); @@ -1841,7 +1841,7 @@ fn analyzeInstSwitchBr( var case_infos = try gpa.alloc(ControlBranchInfo, ncases + 1); // +1 for else defer gpa.free(case_infos); - std.mem.set(ControlBranchInfo, case_infos, .{}); + @memset(case_infos, .{}); defer for (case_infos) |*info| { info.branch_deaths.deinit(gpa); info.live_set.deinit(gpa); @@ -1898,7 +1898,7 @@ fn analyzeInstSwitchBr( const mirrored_deaths = try gpa.alloc(DeathList, ncases + 1); defer gpa.free(mirrored_deaths); - std.mem.set(DeathList, mirrored_deaths, .{}); + @memset(mirrored_deaths, .{}); defer for (mirrored_deaths) |*md| md.deinit(gpa); { @@ -1993,7 +1993,7 @@ fn AnalyzeBigOperands(comptime pass: LivenessPass) type { }; errdefer a.gpa.free(extra_tombs); - std.mem.set(u32, extra_tombs, 0); + @memset(extra_tombs, 0); const will_die_immediately: bool = switch (pass) { .loop_analysis => false, // track everything, since we don't have full liveness information yet diff --git a/src/Sema.zig b/src/Sema.zig index 0910b96ad4..d006a66fdd 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -206,9 +206,9 @@ pub const InstMap = struct { const start_diff = old_start - better_start; const new_items = try allocator.alloc(Air.Inst.Ref, better_capacity); - mem.set(Air.Inst.Ref, new_items[0..start_diff], .none); - mem.copy(Air.Inst.Ref, new_items[start_diff..], map.items); - mem.set(Air.Inst.Ref, new_items[start_diff + map.items.len ..], .none); + @memset(new_items[0..start_diff], .none); + @memcpy(new_items[start_diff..][0..map.items.len], map.items); + @memset(new_items[start_diff + map.items.len ..], .none); allocator.free(map.items); map.items = new_items; @@ -4307,7 +4307,7 @@ fn validateStructInit( // Maps field index to field_ptr index of where it was already initialized. const found_fields = try gpa.alloc(Zir.Inst.Index, struct_ty.structFieldCount()); defer gpa.free(found_fields); - mem.set(Zir.Inst.Index, found_fields, 0); + @memset(found_fields, 0); var struct_ptr_zir_ref: Zir.Inst.Ref = undefined; @@ -5113,7 +5113,7 @@ fn zirIntBig(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. const byte_count = int.len * @sizeOf(std.math.big.Limb); const limb_bytes = sema.code.string_bytes[int.start..][0..byte_count]; const limbs = try arena.alloc(std.math.big.Limb, int.len); - mem.copy(u8, mem.sliceAsBytes(limbs), limb_bytes); + @memcpy(mem.sliceAsBytes(limbs), limb_bytes); return sema.addConstant( Type.initTag(.comptime_int), @@ -5967,7 +5967,7 @@ fn addDbgVar( const elements_used = name.len / 4 + 1; try sema.air_extra.ensureUnusedCapacity(sema.gpa, elements_used); const buffer = mem.sliceAsBytes(sema.air_extra.unusedCapacitySlice()); - mem.copy(u8, buffer, name); + @memcpy(buffer[0..name.len], name); buffer[name.len] = 0; sema.air_extra.items.len += elements_used; @@ -10354,7 +10354,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError .Enum => { seen_enum_fields = try gpa.alloc(?Module.SwitchProngSrc, operand_ty.enumFieldCount()); empty_enum = seen_enum_fields.len == 0 and !operand_ty.isNonexhaustiveEnum(); - mem.set(?Module.SwitchProngSrc, seen_enum_fields, null); + @memset(seen_enum_fields, null); // `range_set` is used for non-exhaustive enum values that do not correspond to any tags. var extra_index: usize = special.end; @@ -12809,8 +12809,8 @@ fn analyzeTupleMul( } i = 0; while (i < factor) : (i += 1) { - mem.copy(Type, types[tuple_len * i ..], types[0..tuple_len]); - mem.copy(Value, values[tuple_len * i ..], values[0..tuple_len]); + mem.copyForwards(Type, types[tuple_len * i ..], types[0..tuple_len]); + mem.copyForwards(Value, values[tuple_len * i ..], values[0..tuple_len]); } break :rs runtime_src; }; @@ -12835,7 +12835,7 @@ fn analyzeTupleMul( } i = 1; while (i < factor) : (i += 1) { - mem.copy(Air.Inst.Ref, element_refs[tuple_len * i ..], element_refs[0..tuple_len]); + @memcpy(element_refs[tuple_len * i ..][0..tuple_len], element_refs[0..tuple_len]); } return block.addAggregateInit(tuple_ty, element_refs); @@ -15057,29 +15057,29 @@ fn zirAsm( sema.appendRefsAssumeCapacity(args); for (outputs) |o| { const buffer = mem.sliceAsBytes(sema.air_extra.unusedCapacitySlice()); - mem.copy(u8, buffer, o.c); + @memcpy(buffer[0..o.c.len], o.c); buffer[o.c.len] = 0; - mem.copy(u8, buffer[o.c.len + 1 ..], o.n); + @memcpy(buffer[o.c.len + 1 ..][0..o.n.len], o.n); buffer[o.c.len + 1 + o.n.len] = 0; sema.air_extra.items.len += (o.c.len + o.n.len + (2 + 3)) / 4; } for (inputs) |input| { const buffer = mem.sliceAsBytes(sema.air_extra.unusedCapacitySlice()); - mem.copy(u8, buffer, input.c); + @memcpy(buffer[0..input.c.len], input.c); buffer[input.c.len] = 0; - mem.copy(u8, buffer[input.c.len + 1 ..], input.n); + @memcpy(buffer[input.c.len + 1 ..][0..input.n.len], input.n); buffer[input.c.len + 1 + input.n.len] = 0; sema.air_extra.items.len += (input.c.len + input.n.len + (2 + 3)) / 4; } for (clobbers) |clobber| { const buffer = mem.sliceAsBytes(sema.air_extra.unusedCapacitySlice()); - mem.copy(u8, buffer, clobber); + @memcpy(buffer[0..clobber.len], clobber); buffer[clobber.len] = 0; sema.air_extra.items.len += clobber.len / 4 + 1; } { const buffer = mem.sliceAsBytes(sema.air_extra.unusedCapacitySlice()); - mem.copy(u8, buffer, asm_source); + @memcpy(buffer[0..asm_source.len], asm_source); sema.air_extra.items.len += (asm_source.len + 3) / 4; } return asm_air; @@ -17582,7 +17582,7 @@ fn structInitEmpty( // The init values to use for the struct instance. const field_inits = try gpa.alloc(Air.Inst.Ref, struct_ty.structFieldCount()); defer gpa.free(field_inits); - mem.set(Air.Inst.Ref, field_inits, .none); + @memset(field_inits, .none); return sema.finishStructInit(block, init_src, dest_src, field_inits, struct_ty, false); } @@ -17675,7 +17675,7 @@ fn zirStructInit( // The init values to use for the struct instance. const field_inits = try gpa.alloc(Air.Inst.Ref, resolved_ty.structFieldCount()); defer gpa.free(field_inits); - mem.set(Air.Inst.Ref, field_inits, .none); + @memset(field_inits, .none); var field_i: u32 = 0; var extra_index = extra.end; @@ -27079,7 +27079,7 @@ fn beginComptimePtrMutation( const array_len_including_sentinel = try sema.usizeCast(block, src, parent.ty.arrayLenIncludingSentinel()); const elems = try arena.alloc(Value, array_len_including_sentinel); - mem.set(Value, elems, Value.undef); + @memset(elems, Value.undef); val_ptr.* = try Value.Tag.aggregate.create(arena, elems); @@ -27277,7 +27277,7 @@ fn beginComptimePtrMutation( switch (parent.ty.zigTypeTag()) { .Struct => { const fields = try arena.alloc(Value, parent.ty.structFieldCount()); - mem.set(Value, fields, Value.undef); + @memset(fields, Value.undef); val_ptr.* = try Value.Tag.aggregate.create(arena, fields); @@ -28425,7 +28425,7 @@ fn coerceTupleToStruct( const fields = struct_ty.structFields(); const field_vals = try sema.arena.alloc(Value, fields.count()); const field_refs = try sema.arena.alloc(Air.Inst.Ref, field_vals.len); - mem.set(Air.Inst.Ref, field_refs, .none); + @memset(field_refs, .none); const inst_ty = sema.typeOf(inst); var runtime_src: ?LazySrcLoc = null; @@ -28514,7 +28514,7 @@ fn coerceTupleToTuple( const dest_field_count = tuple_ty.structFieldCount(); const field_vals = try sema.arena.alloc(Value, dest_field_count); const field_refs = try sema.arena.alloc(Air.Inst.Ref, field_vals.len); - mem.set(Air.Inst.Ref, field_refs, .none); + @memset(field_refs, .none); const inst_ty = sema.typeOf(inst); const inst_field_count = inst_ty.structFieldCount(); diff --git a/src/arch/aarch64/CodeGen.zig b/src/arch/aarch64/CodeGen.zig index 948dad73b9..649edd3b9c 100644 --- a/src/arch/aarch64/CodeGen.zig +++ b/src/arch/aarch64/CodeGen.zig @@ -1630,7 +1630,7 @@ fn allocRegs( const read_locks = locks[0..read_args.len]; const write_locks = locks[read_args.len..]; - std.mem.set(?RegisterLock, locks, null); + @memset(locks, null); defer for (locks) |lock| { if (lock) |locked_reg| self.register_manager.unlockReg(locked_reg); }; @@ -4395,7 +4395,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier if (args.len + 1 <= Liveness.bpi - 1) { var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1); buf[0] = callee; - std.mem.copy(Air.Inst.Ref, buf[1..], args); + @memcpy(buf[1..][0..args.len], args); return self.finishAir(inst, result, buf); } var bt = try self.iterateBigTomb(inst, 1 + args.len); @@ -5348,7 +5348,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { buf_index += 1; } if (buf_index + inputs.len > buf.len) break :simple; - std.mem.copy(Air.Inst.Ref, buf[buf_index..], inputs); + @memcpy(buf[buf_index..][0..inputs.len], inputs); return self.finishAir(inst, result, buf); } var bt = try self.iterateBigTomb(inst, outputs.len + inputs.len); @@ -6055,7 +6055,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { if (elements.len <= Liveness.bpi - 1) { var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1); - std.mem.copy(Air.Inst.Ref, &buf, elements); + @memcpy(buf[0..elements.len], elements); return self.finishAir(inst, result, buf); } var bt = try self.iterateBigTomb(inst, elements.len); diff --git a/src/arch/arm/CodeGen.zig b/src/arch/arm/CodeGen.zig index 3676b2a865..5353b78e4d 100644 --- a/src/arch/arm/CodeGen.zig +++ b/src/arch/arm/CodeGen.zig @@ -3114,7 +3114,7 @@ fn allocRegs( const read_locks = locks[0..read_args.len]; const write_locks = locks[read_args.len..]; - std.mem.set(?RegisterLock, locks, null); + @memset(locks, null); defer for (locks) |lock| { if (lock) |locked_reg| self.register_manager.unlockReg(locked_reg); }; @@ -4341,7 +4341,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier if (args.len <= Liveness.bpi - 2) { var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1); buf[0] = callee; - std.mem.copy(Air.Inst.Ref, buf[1..], args); + @memcpy(buf[1..][0..args.len], args); return self.finishAir(inst, result, buf); } var bt = try self.iterateBigTomb(inst, 1 + args.len); @@ -5263,7 +5263,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { buf_index += 1; } if (buf_index + inputs.len > buf.len) break :simple; - std.mem.copy(Air.Inst.Ref, buf[buf_index..], inputs); + @memcpy(buf[buf_index..][0..inputs.len], inputs); return self.finishAir(inst, result, buf); } var bt = try self.iterateBigTomb(inst, outputs.len + inputs.len); @@ -6000,7 +6000,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { if (elements.len <= Liveness.bpi - 1) { var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1); - std.mem.copy(Air.Inst.Ref, &buf, elements); + @memcpy(buf[0..elements.len], elements); return self.finishAir(inst, result, buf); } var bt = try self.iterateBigTomb(inst, elements.len); diff --git a/src/arch/riscv64/CodeGen.zig b/src/arch/riscv64/CodeGen.zig index a0ebc1becc..d4c7eb0c70 100644 --- a/src/arch/riscv64/CodeGen.zig +++ b/src/arch/riscv64/CodeGen.zig @@ -1784,7 +1784,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier if (args.len <= Liveness.bpi - 2) { var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1); buf[0] = callee; - std.mem.copy(Air.Inst.Ref, buf[1..], args); + @memcpy(buf[1..][0..args.len], args); return self.finishAir(inst, result, buf); } var bt = try self.iterateBigTomb(inst, 1 + args.len); @@ -2225,7 +2225,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { buf_index += 1; } if (buf_index + inputs.len > buf.len) break :simple; - std.mem.copy(Air.Inst.Ref, buf[buf_index..], inputs); + @memcpy(buf[buf_index..][0..inputs.len], inputs); return self.finishAir(inst, result, buf); } var bt = try self.iterateBigTomb(inst, outputs.len + inputs.len); @@ -2500,7 +2500,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { if (elements.len <= Liveness.bpi - 1) { var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1); - std.mem.copy(Air.Inst.Ref, &buf, elements); + @memcpy(buf[0..elements.len], elements); return self.finishAir(inst, result, buf); } var bt = try self.iterateBigTomb(inst, elements.len); diff --git a/src/arch/sparc64/CodeGen.zig b/src/arch/sparc64/CodeGen.zig index cc5c9e9832..2686852bab 100644 --- a/src/arch/sparc64/CodeGen.zig +++ b/src/arch/sparc64/CodeGen.zig @@ -843,7 +843,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { if (elements.len <= Liveness.bpi - 1) { var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1); - std.mem.copy(Air.Inst.Ref, &buf, elements); + @memcpy(buf[0..elements.len], elements); return self.finishAir(inst, result, buf); } var bt = try self.iterateBigTomb(inst, elements.len); @@ -987,7 +987,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { buf_index += 1; } if (buf_index + inputs.len > buf.len) break :simple; - std.mem.copy(Air.Inst.Ref, buf[buf_index..], inputs); + @memcpy(buf[buf_index..][0..inputs.len], inputs); return self.finishAir(inst, result, buf); } @@ -1314,7 +1314,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier if (args.len + 1 <= Liveness.bpi - 1) { var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1); buf[0] = callee; - std.mem.copy(Air.Inst.Ref, buf[1..], args); + @memcpy(buf[1..][0..args.len], args); return self.finishAir(inst, result, buf); } diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig index df0db882ba..be972d7aea 100644 --- a/src/arch/x86_64/CodeGen.zig +++ b/src/arch/x86_64/CodeGen.zig @@ -7117,7 +7117,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { buf_index += 1; } if (buf_index + inputs.len > buf.len) break :simple; - std.mem.copy(Air.Inst.Ref, buf[buf_index..], inputs); + @memcpy(buf[buf_index..][0..inputs.len], inputs); return self.finishAir(inst, result, buf); } var bt = self.liveness.iterateBigTomb(inst); @@ -8505,7 +8505,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { if (elements.len <= Liveness.bpi - 1) { var buf = [1]Air.Inst.Ref{.none} ** (Liveness.bpi - 1); - std.mem.copy(Air.Inst.Ref, &buf, elements); + @memcpy(buf[0..elements.len], elements); return self.finishAir(inst, result, buf); } var bt = self.liveness.iterateBigTomb(inst); diff --git a/src/arch/x86_64/Encoding.zig b/src/arch/x86_64/Encoding.zig index 21899b912b..ee868e4aea 100644 --- a/src/arch/x86_64/Encoding.zig +++ b/src/arch/x86_64/Encoding.zig @@ -546,7 +546,7 @@ fn estimateInstructionLength(prefix: Prefix, encoding: Encoding, ops: []const Op .encoding = encoding, .ops = [1]Operand{.none} ** 4, }; - std.mem.copy(Operand, &inst.ops, ops); + @memcpy(inst.ops[0..ops.len], ops); var cwriter = std.io.countingWriter(std.io.null_writer); inst.encode(cwriter.writer(), .{ .allow_frame_loc = true }) catch unreachable; // Not allowed to fail here unless OOM. diff --git a/src/arch/x86_64/abi.zig b/src/arch/x86_64/abi.zig index e9da09b999..ff1a0ee520 100644 --- a/src/arch/x86_64/abi.zig +++ b/src/arch/x86_64/abi.zig @@ -321,7 +321,7 @@ pub fn classifySystemV(ty: Type, target: Target, ctx: Context) [8]Class { byte_i = 0; result_i += 1; } - std.mem.copy(Class, result[result_i..], field_class); + @memcpy(result[result_i..][0..field_class.len], field_class); result_i += field_class.len; // If there are any bytes leftover, we have to try to combine // the next field with them. diff --git a/src/arch/x86_64/encoder.zig b/src/arch/x86_64/encoder.zig index 73b40ea3be..329dfca924 100644 --- a/src/arch/x86_64/encoder.zig +++ b/src/arch/x86_64/encoder.zig @@ -182,7 +182,7 @@ pub const Instruction = struct { .encoding = encoding, .ops = [1]Operand{.none} ** 4, }; - std.mem.copy(Operand, &inst.ops, ops); + @memcpy(inst.ops[0..ops.len], ops); return inst; } @@ -859,7 +859,7 @@ fn expectEqualHexStrings(expected: []const u8, given: []const u8, assembly: []co const idx = std.mem.indexOfDiff(u8, expected_fmt, given_fmt).?; var padding = try testing.allocator.alloc(u8, idx + 5); defer testing.allocator.free(padding); - std.mem.set(u8, padding, ' '); + @memset(padding, ' '); std.debug.print("\nASM: {s}\nEXP: {s}\nGIV: {s}\n{s}^ -- first differing byte\n", .{ assembly, expected_fmt, diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 9556419988..27b04cee8e 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -2411,9 +2411,9 @@ pub fn genErrDecls(o: *Object) !void { const name_buf = try o.dg.gpa.alloc(u8, name_prefix.len + max_name_len); defer o.dg.gpa.free(name_buf); - mem.copy(u8, name_buf, name_prefix); + @memcpy(name_buf[0..name_prefix.len], name_prefix); for (o.dg.module.error_name_list.items) |name| { - mem.copy(u8, name_buf[name_prefix.len..], name); + @memcpy(name_buf[name_prefix.len..][0..name.len], name); const identifier = name_buf[0 .. name_prefix.len + name.len]; var name_ty_pl = Type.Payload.Len{ .base = .{ .tag = .array_u8_sentinel_0 }, .data = name.len }; @@ -4877,7 +4877,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { const literal = mem.sliceTo(asm_source[src_i..], '%'); src_i += literal.len; - mem.copy(u8, fixed_asm_source[dst_i..], literal); + @memcpy(fixed_asm_source[dst_i..][0..literal.len], literal); dst_i += literal.len; if (src_i >= asm_source.len) break; @@ -4902,9 +4902,9 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { const name = desc[0..colon]; const modifier = desc[colon + 1 ..]; - mem.copy(u8, fixed_asm_source[dst_i..], modifier); + @memcpy(fixed_asm_source[dst_i..][0..modifier.len], modifier); dst_i += modifier.len; - mem.copy(u8, fixed_asm_source[dst_i..], name); + @memcpy(fixed_asm_source[dst_i..][0..name.len], name); dst_i += name.len; src_i += desc.len; @@ -7455,7 +7455,7 @@ fn formatIntLiteral( var int_buf: Value.BigIntSpace = undefined; const int = if (data.val.isUndefDeep()) blk: { undef_limbs = try allocator.alloc(BigIntLimb, BigInt.calcTwosCompLimbCount(data.int_info.bits)); - mem.set(BigIntLimb, undef_limbs, undefPattern(BigIntLimb)); + @memset(undef_limbs, undefPattern(BigIntLimb)); var undef_int = BigInt.Mutable{ .limbs = undef_limbs, @@ -7550,7 +7550,7 @@ fn formatIntLiteral( } else { try data.cty.renderLiteralPrefix(writer, data.kind); wrap.convertToTwosComplement(int, data.int_info.signedness, c_bits); - mem.set(BigIntLimb, wrap.limbs[wrap.len..], 0); + @memset(wrap.limbs[wrap.len..], 0); wrap.len = wrap.limbs.len; const limbs_per_c_limb = @divExact(wrap.len, c_limb_info.count); diff --git a/src/link/Coff.zig b/src/link/Coff.zig index ed3e213b0e..489d9e0443 100644 --- a/src/link/Coff.zig +++ b/src/link/Coff.zig @@ -2367,12 +2367,12 @@ pub fn getAtomIndexForSymbol(self: *const Coff, sym_loc: SymbolWithLoc) ?Atom.In fn setSectionName(self: *Coff, header: *coff.SectionHeader, name: []const u8) !void { if (name.len <= 8) { mem.copy(u8, &header.name, name); - mem.set(u8, header.name[name.len..], 0); + @memset(header.name[name.len..], 0); return; } const offset = try self.strtab.insert(self.base.allocator, name); const name_offset = fmt.bufPrint(&header.name, "/{d}", .{offset}) catch unreachable; - mem.set(u8, header.name[name_offset.len..], 0); + @memset(header.name[name_offset.len..], 0); } fn getSectionName(self: *const Coff, header: *const coff.SectionHeader) []const u8 { @@ -2386,16 +2386,16 @@ fn getSectionName(self: *const Coff, header: *const coff.SectionHeader) []const fn setSymbolName(self: *Coff, symbol: *coff.Symbol, name: []const u8) !void { if (name.len <= 8) { mem.copy(u8, &symbol.name, name); - mem.set(u8, symbol.name[name.len..], 0); + @memset(symbol.name[name.len..], 0); return; } const offset = try self.strtab.insert(self.base.allocator, name); - mem.set(u8, symbol.name[0..4], 0); + @memset(symbol.name[0..4], 0); mem.writeIntLittle(u32, symbol.name[4..8], offset); } fn logSymAttributes(sym: *const coff.Symbol, buf: *[4]u8) []const u8 { - mem.set(u8, buf[0..4], '_'); + @memset(buf[0..4], '_'); switch (sym.section_number) { .UNDEFINED => { buf[3] = 'u'; diff --git a/src/link/Dwarf.zig b/src/link/Dwarf.zig index 0f2dfbda0e..a867bf8d80 100644 --- a/src/link/Dwarf.zig +++ b/src/link/Dwarf.zig @@ -1189,7 +1189,7 @@ pub fn commitDeclState( if (needed_size > segment_size) { log.debug(" allocating {d} bytes for 'debug line' information", .{needed_size - segment_size}); try debug_line.resize(self.allocator, needed_size); - mem.set(u8, debug_line.items[segment_size..], 0); + @memset(debug_line.items[segment_size..], 0); } debug_line.items.len = needed_size; } @@ -1458,7 +1458,7 @@ fn writeDeclDebugInfo(self: *Dwarf, atom_index: Atom.Index, dbg_info_buf: []cons if (needed_size > segment_size) { log.debug(" allocating {d} bytes for 'debug info' information", .{needed_size - segment_size}); try debug_info.resize(self.allocator, needed_size); - mem.set(u8, debug_info.items[segment_size..], 0); + @memset(debug_info.items[segment_size..], 0); } debug_info.items.len = needed_size; } @@ -2076,9 +2076,9 @@ fn writeDbgInfoNopsToArrayList( buffer.items.len, offset + content.len + next_padding_size + 1, )); - mem.set(u8, buffer.items[offset - prev_padding_size .. offset], @enumToInt(AbbrevKind.pad1)); + @memset(buffer.items[offset - prev_padding_size .. offset], @enumToInt(AbbrevKind.pad1)); mem.copy(u8, buffer.items[offset..], content); - mem.set(u8, buffer.items[offset + content.len ..][0..next_padding_size], @enumToInt(AbbrevKind.pad1)); + @memset(buffer.items[offset + content.len ..][0..next_padding_size], @enumToInt(AbbrevKind.pad1)); if (trailing_zero) { buffer.items[offset + content.len + next_padding_size] = 0; diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 4a6bb99818..48d952b6cc 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -1997,7 +1997,7 @@ fn writeElfHeader(self: *Elf) !void { // OS ABI, often set to 0 regardless of target platform // ABI Version, possibly used by glibc but not by static executables // padding - mem.set(u8, hdr_buf[index..][0..9], 0); + @memset(hdr_buf[index..][0..9], 0); index += 9; assert(index == 16); diff --git a/src/link/MachO.zig b/src/link/MachO.zig index df9b8a768a..eebd2ad8a4 100644 --- a/src/link/MachO.zig +++ b/src/link/MachO.zig @@ -1454,7 +1454,7 @@ fn createThreadLocalDescriptorAtom(self: *MachO, sym_name: []const u8, target: S }); var code: [size]u8 = undefined; - mem.set(u8, &code, 0); + @memset(&code, 0); try self.writeAtom(atom_index, &code); return atom_index; @@ -3234,7 +3234,7 @@ fn writeDyldInfoData(self: *MachO) !void { var buffer = try gpa.alloc(u8, needed_size); defer gpa.free(buffer); - mem.set(u8, buffer, 0); + @memset(buffer, 0); var stream = std.io.fixedBufferStream(buffer); const writer = stream.writer(); @@ -3389,7 +3389,7 @@ fn writeStrtab(self: *MachO) !void { const buffer = try gpa.alloc(u8, math.cast(usize, needed_size_aligned) orelse return error.Overflow); defer gpa.free(buffer); - mem.set(u8, buffer, 0); + @memset(buffer, 0); mem.copy(u8, buffer, self.strtab.buffer.items); try self.base.file.?.pwriteAll(buffer, offset); @@ -4096,8 +4096,8 @@ pub fn logSections(self: *MachO) void { } fn logSymAttributes(sym: macho.nlist_64, buf: *[4]u8) []const u8 { - mem.set(u8, buf[0..4], '_'); - mem.set(u8, buf[4..], ' '); + @memset(buf[0..4], '_'); + @memset(buf[4..], ' '); if (sym.sect()) { buf[0] = 's'; } diff --git a/src/link/MachO/Object.zig b/src/link/MachO/Object.zig index e407457e03..7cc6f78c7d 100644 --- a/src/link/MachO/Object.zig +++ b/src/link/MachO/Object.zig @@ -156,7 +156,7 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch) // Prepopulate relocations per section lookup table. try self.section_relocs_lookup.resize(allocator, nsects); - mem.set(u32, self.section_relocs_lookup.items, 0); + @memset(self.section_relocs_lookup.items, 0); // Parse symtab. const symtab = while (it.next()) |cmd| switch (cmd.cmd()) { @@ -189,10 +189,10 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch) }; } - mem.set(i64, self.globals_lookup, -1); - mem.set(AtomIndex, self.atom_by_index_table, 0); - mem.set(Entry, self.source_section_index_lookup, .{}); - mem.set(Entry, self.relocs_lookup, .{}); + @memset(self.globals_lookup, -1); + @memset(self.atom_by_index_table, 0); + @memset(self.source_section_index_lookup, .{}); + @memset(self.relocs_lookup, .{}); // You would expect that the symbol table is at least pre-sorted based on symbol's type: // local < extern defined < undefined. Unfortunately, this is not guaranteed! For instance, @@ -252,7 +252,7 @@ pub fn parse(self: *Object, allocator: Allocator, cpu_arch: std.Target.Cpu.Arch) self.unwind_info_sect_id = self.getSourceSectionIndexByName("__LD", "__compact_unwind"); if (self.hasUnwindRecords()) { self.unwind_relocs_lookup = try allocator.alloc(Record, self.getUnwindRecords().len); - mem.set(Record, self.unwind_relocs_lookup, .{ .dead = true, .reloc = .{} }); + @memset(self.unwind_relocs_lookup, .{ .dead = true, .reloc = .{} }); } } diff --git a/src/link/MachO/Trie.zig b/src/link/MachO/Trie.zig index a97e18a186..34200db7dc 100644 --- a/src/link/MachO/Trie.zig +++ b/src/link/MachO/Trie.zig @@ -499,7 +499,7 @@ fn expectEqualHexStrings(expected: []const u8, given: []const u8) !void { const idx = mem.indexOfDiff(u8, expected_fmt, given_fmt).?; var padding = try testing.allocator.alloc(u8, idx + 5); defer testing.allocator.free(padding); - mem.set(u8, padding, ' '); + @memset(padding, ' '); std.debug.print("\nEXP: {s}\nGIV: {s}\n{s}^ -- first differing byte\n", .{ expected_fmt, given_fmt, padding }); return error.TestFailed; } diff --git a/src/link/MachO/UnwindInfo.zig b/src/link/MachO/UnwindInfo.zig index e59f5fe250..0071657f8b 100644 --- a/src/link/MachO/UnwindInfo.zig +++ b/src/link/MachO/UnwindInfo.zig @@ -659,7 +659,7 @@ pub fn write(info: *UnwindInfo, zld: *Zld) !void { const padding = buffer.items.len - cwriter.bytes_written; if (padding > 0) { const offset = math.cast(usize, cwriter.bytes_written) orelse return error.Overflow; - mem.set(u8, buffer.items[offset..], 0); + @memset(buffer.items[offset..], 0); } try zld.file.pwriteAll(buffer.items, sect.offset); diff --git a/src/link/MachO/zld.zig b/src/link/MachO/zld.zig index bc658fc8d2..ce19626ff4 100644 --- a/src/link/MachO/zld.zig +++ b/src/link/MachO/zld.zig @@ -2140,7 +2140,7 @@ pub const Zld = struct { var buffer = try gpa.alloc(u8, needed_size); defer gpa.free(buffer); - mem.set(u8, buffer, 0); + @memset(buffer, 0); var stream = std.io.fixedBufferStream(buffer); const writer = stream.writer(); @@ -2352,7 +2352,7 @@ pub const Zld = struct { const buffer = try self.gpa.alloc(u8, math.cast(usize, needed_size_aligned) orelse return error.Overflow); defer self.gpa.free(buffer); - mem.set(u8, buffer, 0); + @memset(buffer, 0); mem.copy(u8, buffer, mem.sliceAsBytes(out_dice.items)); log.debug("writing data-in-code from 0x{x} to 0x{x}", .{ offset, offset + needed_size_aligned }); @@ -2484,7 +2484,7 @@ pub const Zld = struct { const buffer = try self.gpa.alloc(u8, math.cast(usize, needed_size_aligned) orelse return error.Overflow); defer self.gpa.free(buffer); - mem.set(u8, buffer, 0); + @memset(buffer, 0); mem.copy(u8, buffer, self.strtab.buffer.items); try self.file.pwriteAll(buffer, offset); @@ -3199,7 +3199,7 @@ pub const Zld = struct { scoped_log.debug(" object({d}): {s}", .{ id, object.name }); if (object.in_symtab == null) continue; for (object.symtab, 0..) |sym, sym_id| { - mem.set(u8, &buf, '_'); + @memset(&buf, '_'); scoped_log.debug(" %{d}: {s} @{x} in sect({d}), {s}", .{ sym_id, object.getSymbolName(@intCast(u32, sym_id)), @@ -4007,7 +4007,7 @@ pub fn linkWithZld(macho_file: *MachO, comp: *Compilation, prog_node: *std.Progr log.debug("zeroing out zerofill area of length {x} at {x}", .{ size, start }); var padding = try zld.gpa.alloc(u8, size); defer zld.gpa.free(padding); - mem.set(u8, padding, 0); + @memset(padding, 0); try zld.file.pwriteAll(padding, start); } } diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index 0fe9ec5e3b..74bfd255bd 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -1976,7 +1976,7 @@ fn parseAtom(wasm: *Wasm, atom_index: Atom.Index, kind: Kind) !void { // We do not have to do this when exporting the memory (the default) because the runtime // will do it for us, and we do not emit the bss segment at all. if ((wasm.base.options.output_mode == .Obj or wasm.base.options.import_memory) and kind.data == .uninitialized) { - std.mem.set(u8, atom.code.items, 0); + @memset(atom.code.items, 0); } const should_merge = wasm.base.options.output_mode != .Obj; diff --git a/src/objcopy.zig b/src/objcopy.zig index 4a15af88e3..239f1f3ad7 100644 --- a/src/objcopy.zig +++ b/src/objcopy.zig @@ -1088,7 +1088,7 @@ fn ElfFile(comptime is_64: bool) type { const crc_offset = std.mem.alignForward(link.name.len + 1, 4); const buf = try allocator.alignedAlloc(u8, 4, crc_offset + 4); std.mem.copy(u8, buf[0..link.name.len], link.name); - std.mem.set(u8, buf[link.name.len..crc_offset], 0); + @memset(buf[link.name.len..crc_offset], 0); std.mem.copy(u8, buf[crc_offset..], std.mem.asBytes(&link.crc32)); break :payload buf; }; diff --git a/src/print_air.zig b/src/print_air.zig index db3e47c0dd..2d7995842f 100644 --- a/src/print_air.zig +++ b/src/print_air.zig @@ -846,7 +846,7 @@ const Writer = struct { else blk: { const slice = w.gpa.alloc([]const Air.Inst.Index, switch_br.data.cases_len + 1) catch @panic("out of memory"); - std.mem.set([]const Air.Inst.Index, slice, &.{}); + @memset(slice, &.{}); break :blk Liveness.SwitchBrTable{ .deaths = slice }; }; defer w.gpa.free(liveness.deaths); diff --git a/src/print_zir.zig b/src/print_zir.zig index 30bfdba347..922366dc85 100644 --- a/src/print_zir.zig +++ b/src/print_zir.zig @@ -682,7 +682,7 @@ const Writer = struct { const limbs = try self.gpa.alloc(std.math.big.Limb, inst_data.len); defer self.gpa.free(limbs); - mem.copy(u8, mem.sliceAsBytes(limbs), limb_bytes); + @memcpy(mem.sliceAsBytes(limbs), limb_bytes); const big_int: std.math.big.int.Const = .{ .limbs = limbs, .positive = true, diff --git a/src/value.zig b/src/value.zig index 2f34749a50..5294a9ceb4 100644 --- a/src/value.zig +++ b/src/value.zig @@ -875,7 +875,7 @@ pub const Value = extern union { .repeated => { const byte = @intCast(u8, val.castTag(.repeated).?.data.toUnsignedInt(target)); const result = try allocator.alloc(u8, @intCast(usize, ty.arrayLen())); - std.mem.set(u8, result, byte); + @memset(result, byte); return result; }, .decl_ref => { @@ -1287,7 +1287,7 @@ pub const Value = extern union { const endian = target.cpu.arch.endian(); if (val.isUndef()) { const size = @intCast(usize, ty.abiSize(target)); - std.mem.set(u8, buffer[0..size], 0xaa); + @memset(buffer[0..size], 0xaa); return; } switch (ty.zigTypeTag()) { -- cgit v1.2.3 From fee318c44ddac8d8d247aac78a8cfa5305ef6e69 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 27 Apr 2023 17:27:41 -0700 Subject: Sema: fix airMemset for comptime slices --- src/Sema.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Sema.zig') diff --git a/src/Sema.zig b/src/Sema.zig index d006a66fdd..327ff3800f 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -22039,7 +22039,7 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void if (try sema.resolveMaybeUndefVal(uncoerced_elem)) |_| { for (0..len) |i| { const elem_index = try sema.addIntUnsigned(Type.usize, i); - const elem_ptr = try sema.elemPtr( + const elem_ptr = try sema.elemPtrOneLayerOnly( block, src, dest_ptr, -- cgit v1.2.3