-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmem.zig
144 lines (129 loc) · 4.35 KB
/
mem.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const std = @import("std");
const utils = @import("utils.zig");
const Error = utils.Error;
/// Returns true if the contents of the slices `a` and `b` are the same.
pub fn memory_compare(a: []const u8, b: []const u8) callconv(.Inline) bool {
if (a.len != b.len) return false;
for (a[0..]) |value, i| {
if (value != b[i]) return false;
}
return true;
}
/// Copy contents from `source` to `destination`.
///
/// If `source.len > destination.len` then the copy is truncated.
pub fn memory_copy_truncate(destination: []u8, source: []const u8) callconv(.Inline) usize {
const size = @minimum(destination.len, source.len);
for (destination[0..size]) |*ptr, i| {
ptr.* = source[i];
}
return size;
}
pub fn memory_copy_error(destination: []u8, source: []const u8) callconv(.Inline) Error!usize {
if (destination.len < source.len) {
return Error.NotEnoughDestination;
}
const size = source.len;
for (destination[0..size]) |*ptr, i| {
ptr.* = source[i];
}
return size;
}
pub fn memory_copy_anyptr(destination: []u8, source: anytype) callconv(.Inline) void {
const s = @ptrCast([*]const u8, source);
for (destination[0..]) |*ptr, i| {
ptr.* = s[i];
}
}
/// Set all the elements of `destination` to `value`.
pub fn memory_set(destination: []u8, value: u8) callconv(.Inline) void {
for (destination[0..]) |*ptr| {
ptr.* = value;
}
}
pub fn to_bytes(value: anytype) callconv(.Inline) []u8 {
const Type = @TypeOf(value);
const Traits = @typeInfo(Type);
switch (Traits) {
std.builtin.TypeId.Pointer => |pointer_type| {
const count = switch (pointer_type.size) {
.One => 1,
.Slice => value.len,
else => {
@compileLog("Unsupported Type is ", @typeName(Type));
@compileError("Unsupported Type");
}
};
return @ptrCast([*]u8, value)[0.. @sizeOf(pointer_type.child) * count];
},
else => {
@compileLog("Unsupported Type is ", @typeName(Type));
@compileError("Unsupported Type");
}
}
}
pub fn to_const_bytes(value: anytype) callconv(.Inline) []const u8 {
const Type = @TypeOf(value);
const Traits = @typeInfo(Type);
switch (Traits) {
std.builtin.TypeId.Pointer => |pointer_type| {
const count = switch (pointer_type.size) {
.One => 1,
.Slice => value.len,
else => {
@compileLog("Unsupported Type is ", @typeName(Type));
@compileError("Unsupported Type");
}
};
return @ptrCast([*]const u8, value)[0.. @sizeOf(pointer_type.child) * count];
},
else => {
@compileLog("Unsupported Type is ", @typeName(Type));
@compileError("Unsupported Type");
}
}
}
pub fn empty_slice(comptime Type: type, ptr: anytype) callconv(.Inline) []const Type {
const PtrType = @TypeOf(ptr);
var rv: []const Type = undefined;
rv.len = 0;
rv.ptr = switch (@typeInfo(PtrType)) {
std.builtin.TypeId.Pointer => @ptrCast([*]const Type, ptr),
std.builtin.TypeId.Int => @intToPtr([*]const Type, ptr),
std.builtin.TypeId.ComptimeInt => @intToPtr([*]const Type, @as(usize, ptr)),
else => {
@compileLog("Unsupported Type is ", @typeName(PtrType));
@compileError("Unsupported Type");
}
};
return rv;
}
pub const TestAlloc = struct {
impl: std.heap.GeneralPurposeAllocator(.{}) = .{},
has_deinit: bool = false,
pub fn alloc(self: *TestAlloc) std.mem.Allocator {
return self.impl.allocator();
}
pub const ShouldPanic = enum {
Panic,
NoPanic,
};
pub fn deinit(self: *TestAlloc, should_panic: ShouldPanic) void {
if (!self.has_deinit) {
const leaks = self.impl.deinit();
if (should_panic == .Panic) {
std.testing.expect(!leaks) catch @panic("leak(s) detected");
}
self.has_deinit = true;
}
}
};
test "TestAlloc example usage" {
var ta = utils.TestAlloc{};
defer ta.deinit(.Panic);
errdefer ta.deinit(.NoPanic);
const alloc = ta.alloc();
const int = try alloc.create(u32);
int.* = 13;
alloc.destroy(int);
}