authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-11-18 13:06:35+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-11-18 13:06:35+02:00
log1e1a490600346490fabb69f67964e1ef62ee5e5f
treef39b759f5ab49d69f0e15cd21b022e670e735d12
parent27b73cc3958a5960691fd020551012bb6e3108d5
parent109a3ebcca2928b1d5f40accab9a8b6a03ea750b
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #7084 from xackus/mem-volatile

std.mem: make sliceAsBytes, etc. respect volatile

1 files changed, 88 insertions(+), 35 deletions(-)

lib/std/mem.zig+88-35
......@@ -7,13 +7,14 @@ const std = @import("std.zig");
77const debug = std.debug;
88const assert = debug.assert;
99const math = std.math;
10const builtin = @import("builtin");
10const builtin = std.builtin;
1111const mem = @This();
1212const meta = std.meta;
1313const trait = meta.trait;
1414const testing = std.testing;
1515
16/// https://github.com/ziglang/zig/issues/2564
16/// Compile time known minimum page size.
17/// https://github.com/ziglang/zig/issues/4082
1718pub const page_size = switch (builtin.arch) {
1819 .wasm32, .wasm64 => 64 * 1024,
1920 .aarch64 => switch (builtin.os.tag) {
......@@ -139,7 +140,7 @@ test "mem.Allocator basics" {
139140
140141/// Copy all of source into dest at position 0.
141142/// dest.len must be >= source.len.
142/// dest.ptr must be <= src.ptr.
143/// If the slices overlap, dest.ptr must be <= src.ptr.
143144pub fn copy(comptime T: type, dest: []T, source: []const T) void {
144145 // TODO instead of manually doing this check for the whole array
145146 // and turning off runtime safety, the compiler should detect loops like
......@@ -152,7 +153,7 @@ pub fn copy(comptime T: type, dest: []T, source: []const T) void {
152153
153154/// Copy all of source into dest at position 0.
154155/// dest.len must be >= source.len.
155/// dest.ptr must be >= src.ptr.
156/// If the slices overlap, dest.ptr must be >= src.ptr.
156157pub fn copyBackwards(comptime T: type, dest: []T, source: []const T) void {
157158 // TODO instead of manually doing this check for the whole array
158159 // and turning off runtime safety, the compiler should detect loops like
......@@ -1916,25 +1917,31 @@ pub fn nativeToBig(comptime T: type, x: T) T {
19161917 };
19171918}
19181919
1920fn CopyPtrAttrs(comptime source: type, comptime size: builtin.TypeInfo.Pointer.Size, comptime child: type) type {
1921 const info = @typeInfo(source).Pointer;
1922 return @Type(.{
1923 .Pointer = .{
1924 .size = size,
1925 .is_const = info.is_const,
1926 .is_volatile = info.is_volatile,
1927 .is_allowzero = info.is_allowzero,
1928 .alignment = info.alignment,
1929 .child = child,
1930 .sentinel = null,
1931 },
1932 });
1933}
1934
19191935fn AsBytesReturnType(comptime P: type) type {
19201936 if (!trait.isSingleItemPtr(P))
19211937 @compileError("expected single item pointer, passed " ++ @typeName(P));
19221938
19231939 const size = @sizeOf(meta.Child(P));
1924 const alignment = meta.alignment(P);
19251940
1926 if (alignment == 0) {
1927 if (trait.isConstPtr(P))
1928 return *const [size]u8;
1929 return *[size]u8;
1930 }
1931
1932 if (trait.isConstPtr(P))
1933 return *align(alignment) const [size]u8;
1934 return *align(alignment) [size]u8;
1941 return CopyPtrAttrs(P, .One, [size]u8);
19351942}
19361943
1937/// Given a pointer to a single item, returns a slice of the underlying bytes, preserving constness.
1944/// Given a pointer to a single item, returns a slice of the underlying bytes, preserving pointer attributes.
19381945pub fn asBytes(ptr: anytype) AsBytesReturnType(@TypeOf(ptr)) {
19391946 const P = @TypeOf(ptr);
19401947 return @ptrCast(AsBytesReturnType(P), ptr);
......@@ -1974,6 +1981,20 @@ test "asBytes" {
19741981 testing.expect(eql(u8, asBytes(&zero), ""));
19751982}
19761983
1984test "asBytes preserves pointer attributes" {
1985 const inArr: u32 align(16) = 0xDEADBEEF;
1986 const inPtr = @ptrCast(*align(16) const volatile u32, &inArr);
1987 const outSlice = asBytes(inPtr);
1988
1989 const in = @typeInfo(@TypeOf(inPtr)).Pointer;
1990 const out = @typeInfo(@TypeOf(outSlice)).Pointer;
1991
1992 testing.expectEqual(in.is_const, out.is_const);
1993 testing.expectEqual(in.is_volatile, out.is_volatile);
1994 testing.expectEqual(in.is_allowzero, out.is_allowzero);
1995 testing.expectEqual(in.alignment, out.alignment);
1996}
1997
19771998/// Given any value, returns a copy of its bytes in an array.
19781999pub fn toBytes(value: anytype) [@sizeOf(@TypeOf(value))]u8 {
19792000 return asBytes(&value).*;
......@@ -2003,13 +2024,11 @@ fn BytesAsValueReturnType(comptime T: type, comptime B: type) type {
20032024 @compileError(std.fmt.bufPrint(&buf, "expected *[{}]u8, passed " ++ @typeName(B), .{size}) catch unreachable);
20042025 }
20052026
2006 const alignment = comptime meta.alignment(B);
2007
2008 return if (comptime trait.isConstPtr(B)) *align(alignment) const T else *align(alignment) T;
2027 return CopyPtrAttrs(B, .One, T);
20092028}
20102029
20112030/// Given a pointer to an array of bytes, returns a pointer to a value of the specified type
2012/// backed by those bytes, preserving constness.
2031/// backed by those bytes, preserving pointer attributes.
20132032pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T, @TypeOf(bytes)) {
20142033 return @ptrCast(BytesAsValueReturnType(T, @TypeOf(bytes)), bytes);
20152034}
......@@ -2051,6 +2070,20 @@ test "bytesAsValue" {
20512070 testing.expect(meta.eql(inst, inst2.*));
20522071}
20532072
2073test "bytesAsValue preserves pointer attributes" {
2074 const inArr align(16) = [4]u8{ 0xDE, 0xAD, 0xBE, 0xEF };
2075 const inSlice = @ptrCast(*align(16) const volatile [4]u8, &inArr)[0..];
2076 const outPtr = bytesAsValue(u32, inSlice);
2077
2078 const in = @typeInfo(@TypeOf(inSlice)).Pointer;
2079 const out = @typeInfo(@TypeOf(outPtr)).Pointer;
2080
2081 testing.expectEqual(in.is_const, out.is_const);
2082 testing.expectEqual(in.is_volatile, out.is_volatile);
2083 testing.expectEqual(in.is_allowzero, out.is_allowzero);
2084 testing.expectEqual(in.alignment, out.alignment);
2085}
2086
20542087/// Given a pointer to an array of bytes, returns a value of the specified type backed by a
20552088/// copy of those bytes.
20562089pub fn bytesToValue(comptime T: type, bytes: anytype) T {
......@@ -2066,9 +2099,8 @@ test "bytesToValue" {
20662099 testing.expect(deadbeef == @as(u32, 0xDEADBEEF));
20672100}
20682101
2069//TODO copy also is_volatile, etc. I tried to use @typeInfo, modify child type, use @Type, but ran into issues.
20702102fn BytesAsSliceReturnType(comptime T: type, comptime bytesType: type) type {
2071 if (!(trait.isSlice(bytesType) and meta.Child(bytesType) == u8) and !(trait.isPtrTo(.Array)(bytesType) and meta.Child(meta.Child(bytesType)) == u8)) {
2103 if (!(trait.isSlice(bytesType) or trait.isPtrTo(.Array)(bytesType)) or meta.Elem(bytesType) != u8) {
20722104 @compileError("expected []u8 or *[_]u8, passed " ++ @typeName(bytesType));
20732105 }
20742106
......@@ -2076,11 +2108,11 @@ fn BytesAsSliceReturnType(comptime T: type, comptime bytesType: type) type {
20762108 @compileError("number of bytes in " ++ @typeName(bytesType) ++ " is not divisible by size of " ++ @typeName(T));
20772109 }
20782110
2079 const alignment = meta.alignment(bytesType);
2080
2081 return if (trait.isConstPtr(bytesType)) []align(alignment) const T else []align(alignment) T;
2111 return CopyPtrAttrs(bytesType, .Slice, T);
20822112}
20832113
2114/// Given a slice of bytes, returns a slice of the specified type
2115/// backed by those bytes, preserving pointer attributes.
20842116pub fn bytesAsSlice(comptime T: type, bytes: anytype) BytesAsSliceReturnType(T, @TypeOf(bytes)) {
20852117 // let's not give an undefined pointer to @ptrCast
20862118 // it may be equal to zero and fail a null check
......@@ -2088,10 +2120,7 @@ pub fn bytesAsSlice(comptime T: type, bytes: anytype) BytesAsSliceReturnType(T,
20882120 return &[0]T{};
20892121 }
20902122
2091 const Bytes = @TypeOf(bytes);
2092 const alignment = comptime meta.alignment(Bytes);
2093
2094 const cast_target = if (comptime trait.isConstPtr(Bytes)) [*]align(alignment) const T else [*]align(alignment) T;
2123 const cast_target = CopyPtrAttrs(@TypeOf(bytes), .Many, T);
20952124
20962125 return @ptrCast(cast_target, bytes)[0..@divExact(bytes.len, @sizeOf(T))];
20972126}
......@@ -2149,17 +2178,29 @@ test "bytesAsSlice with specified alignment" {
21492178 testing.expect(slice[0] == 0x33333333);
21502179}
21512180
2152//TODO copy also is_volatile, etc. I tried to use @typeInfo, modify child type, use @Type, but ran into issues.
2181test "bytesAsSlice preserves pointer attributes" {
2182 const inArr align(16) = [4]u8{ 0xDE, 0xAD, 0xBE, 0xEF };
2183 const inSlice = @ptrCast(*align(16) const volatile [4]u8, &inArr)[0..];
2184 const outSlice = bytesAsSlice(u16, inSlice);
2185
2186 const in = @typeInfo(@TypeOf(inSlice)).Pointer;
2187 const out = @typeInfo(@TypeOf(outSlice)).Pointer;
2188
2189 testing.expectEqual(in.is_const, out.is_const);
2190 testing.expectEqual(in.is_volatile, out.is_volatile);
2191 testing.expectEqual(in.is_allowzero, out.is_allowzero);
2192 testing.expectEqual(in.alignment, out.alignment);
2193}
2194
21532195fn SliceAsBytesReturnType(comptime sliceType: type) type {
21542196 if (!trait.isSlice(sliceType) and !trait.isPtrTo(.Array)(sliceType)) {
21552197 @compileError("expected []T or *[_]T, passed " ++ @typeName(sliceType));
21562198 }
21572199
2158 const alignment = meta.alignment(sliceType);
2159
2160 return if (trait.isConstPtr(sliceType)) []align(alignment) const u8 else []align(alignment) u8;
2200 return CopyPtrAttrs(sliceType, .Slice, u8);
21612201}
21622202
2203/// Given a slice, returns a slice of the underlying bytes, preserving pointer attributes.
21632204pub fn sliceAsBytes(slice: anytype) SliceAsBytesReturnType(@TypeOf(slice)) {
21642205 const Slice = @TypeOf(slice);
21652206
......@@ -2169,9 +2210,7 @@ pub fn sliceAsBytes(slice: anytype) SliceAsBytesReturnType(@TypeOf(slice)) {
21692210 return &[0]u8{};
21702211 }
21712212
2172 const alignment = comptime meta.alignment(Slice);
2173
2174 const cast_target = if (comptime trait.isConstPtr(Slice)) [*]align(alignment) const u8 else [*]align(alignment) u8;
2213 const cast_target = CopyPtrAttrs(Slice, .Many, u8);
21752214
21762215 return @ptrCast(cast_target, slice)[0 .. slice.len * @sizeOf(meta.Elem(Slice))];
21772216}
......@@ -2243,6 +2282,20 @@ test "sliceAsBytes and bytesAsSlice back" {
22432282 testing.expect(bytes[11] == math.maxInt(u8));
22442283}
22452284
2285test "sliceAsBytes preserves pointer attributes" {
2286 const inArr align(16) = [2]u16{ 0xDEAD, 0xBEEF };
2287 const inSlice = @ptrCast(*align(16) const volatile [2]u16, &inArr)[0..];
2288 const outSlice = sliceAsBytes(inSlice);
2289
2290 const in = @typeInfo(@TypeOf(inSlice)).Pointer;
2291 const out = @typeInfo(@TypeOf(outSlice)).Pointer;
2292
2293 testing.expectEqual(in.is_const, out.is_const);
2294 testing.expectEqual(in.is_volatile, out.is_volatile);
2295 testing.expectEqual(in.is_allowzero, out.is_allowzero);
2296 testing.expectEqual(in.alignment, out.alignment);
2297}
2298
22462299/// Round an address up to the nearest aligned address
22472300/// The alignment must be a power of 2 and greater than 0.
22482301pub fn alignForward(addr: usize, alignment: usize) usize {