2016-05-17 13:32:43 -07:00
|
|
|
const assert = @import("debug.zig").assert;
|
2016-07-24 18:35:50 -07:00
|
|
|
const math = @import("math.zig");
|
2017-03-26 03:39:28 -07:00
|
|
|
const os = @import("os/index.zig");
|
2016-07-24 18:35:50 -07:00
|
|
|
const io = @import("io.zig");
|
2016-05-17 13:32:43 -07:00
|
|
|
|
2016-08-11 22:25:13 -07:00
|
|
|
pub const Cmp = math.Cmp;
|
|
|
|
|
2016-12-21 20:34:14 -08:00
|
|
|
error NoMem;
|
2016-05-08 01:34:00 -07:00
|
|
|
|
|
|
|
pub type Context = u8;
|
2016-12-18 16:40:26 -08:00
|
|
|
pub const Allocator = struct {
|
2016-08-16 22:42:50 -07:00
|
|
|
allocFn: fn (self: &Allocator, n: usize) -> %[]u8,
|
|
|
|
reallocFn: fn (self: &Allocator, old_mem: []u8, new_size: usize) -> %[]u8,
|
|
|
|
freeFn: fn (self: &Allocator, mem: []u8),
|
2016-05-08 01:34:00 -07:00
|
|
|
context: ?&Context,
|
2016-07-24 18:35:50 -07:00
|
|
|
|
|
|
|
/// Aborts the program if an allocation fails.
|
2017-01-22 16:51:37 -08:00
|
|
|
fn checkedAlloc(self: &Allocator, comptime T: type, n: usize) -> []T {
|
2016-07-24 18:35:50 -07:00
|
|
|
alloc(self, T, n) %% |err| {
|
|
|
|
// TODO var args printf
|
|
|
|
%%io.stderr.write("allocation failure: ");
|
2016-09-05 14:03:11 -07:00
|
|
|
%%io.stderr.write(@errorName(err));
|
2016-07-24 18:35:50 -07:00
|
|
|
%%io.stderr.printf("\n");
|
|
|
|
os.abort()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
fn alloc(self: &Allocator, comptime T: type, n: usize) -> %[]T {
|
2016-08-16 22:42:50 -07:00
|
|
|
const byte_count = %return math.mulOverflow(usize, @sizeOf(T), n);
|
|
|
|
([]T)(%return self.allocFn(self, byte_count))
|
2016-07-24 18:35:50 -07:00
|
|
|
}
|
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
fn realloc(self: &Allocator, comptime T: type, old_mem: []T, n: usize) -> %[]T {
|
2016-08-16 22:42:50 -07:00
|
|
|
const byte_count = %return math.mulOverflow(usize, @sizeOf(T), n);
|
|
|
|
([]T)(%return self.reallocFn(self, ([]u8)(old_mem), byte_count))
|
2016-07-24 18:35:50 -07:00
|
|
|
}
|
|
|
|
|
2017-01-22 21:11:21 -08:00
|
|
|
fn free(self: &Allocator, mem: var) {
|
2016-08-16 22:42:50 -07:00
|
|
|
self.freeFn(self, ([]u8)(mem));
|
2016-07-24 18:35:50 -07:00
|
|
|
}
|
2016-12-18 16:40:26 -08:00
|
|
|
};
|
2016-05-08 01:34:00 -07:00
|
|
|
|
2016-05-17 13:32:43 -07:00
|
|
|
/// Copy all of source into dest at position 0.
|
|
|
|
/// dest.len must be >= source.len.
|
2017-01-22 16:51:37 -08:00
|
|
|
pub fn copy(comptime T: type, dest: []T, source: []const T) {
|
2017-02-07 14:19:51 -08:00
|
|
|
// TODO instead of manually doing this check for the whole array
|
|
|
|
// and turning off debug safety, the compiler should detect loops like
|
|
|
|
// this and automatically omit safety checks for loops
|
2016-12-11 21:31:35 -08:00
|
|
|
@setDebugSafety(this, false);
|
2016-05-17 13:32:43 -07:00
|
|
|
assert(dest.len >= source.len);
|
2016-12-11 21:31:35 -08:00
|
|
|
for (source) |s, i| dest[i] = s;
|
|
|
|
}
|
|
|
|
|
2017-01-22 16:51:37 -08:00
|
|
|
pub fn set(comptime T: type, dest: []T, value: T) {
|
2016-12-11 21:31:35 -08:00
|
|
|
for (dest) |*d| *d = value;
|
2016-05-17 13:32:43 -07:00
|
|
|
}
|
2016-08-11 22:25:13 -07:00
|
|
|
|
|
|
|
/// Return < 0, == 0, or > 0 if memory a is less than, equal to, or greater than,
|
|
|
|
/// memory b, respectively.
|
2017-01-22 16:51:37 -08:00
|
|
|
pub fn cmp(comptime T: type, a: []const T, b: []const T) -> Cmp {
|
2016-09-09 05:58:39 -07:00
|
|
|
const n = math.min(a.len, b.len);
|
2016-08-11 22:25:13 -07:00
|
|
|
var i: usize = 0;
|
|
|
|
while (i < n; i += 1) {
|
|
|
|
if (a[i] == b[i]) continue;
|
|
|
|
return if (a[i] > b[i]) Cmp.Greater else if (a[i] < b[i]) Cmp.Less else Cmp.Equal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return if (a.len > b.len) Cmp.Greater else if (a.len < b.len) Cmp.Less else Cmp.Equal;
|
|
|
|
}
|
2016-09-22 23:00:23 -07:00
|
|
|
|
2017-02-12 14:22:35 -08:00
|
|
|
/// Compares two slices and returns whether they are equal.
|
|
|
|
pub fn eql(comptime T: type, a: []const T, b: []const T) -> bool {
|
|
|
|
if (a.len != b.len) return false;
|
|
|
|
for (a) |item, index| {
|
|
|
|
if (b[index] != item) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reads an integer from memory with size equal to bytes.len.
|
|
|
|
/// T specifies the return type, which must be large enough to store
|
|
|
|
/// the result.
|
|
|
|
pub fn readInt(bytes: []const u8, comptime T: type, big_endian: bool) -> T {
|
|
|
|
var result: T = 0;
|
|
|
|
if (big_endian) {
|
|
|
|
for (bytes) |b| {
|
|
|
|
result = (result << 8) | b;
|
|
|
|
}
|
2016-09-22 23:00:23 -07:00
|
|
|
} else {
|
2017-02-12 14:22:35 -08:00
|
|
|
for (bytes) |b, index| {
|
|
|
|
result = result | (T(b) << T(index * 8));
|
2016-09-22 23:00:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-02-12 14:22:35 -08:00
|
|
|
/// Writes an integer to memory with size equal to bytes.len. Pads with zeroes
|
|
|
|
/// to fill the entire buffer provided.
|
|
|
|
/// value must be an integer.
|
|
|
|
pub fn writeInt(buf: []u8, value: var, big_endian: bool) {
|
|
|
|
const uint = @intType(false, @typeOf(value).bit_count);
|
|
|
|
var bits = @truncate(uint, value);
|
|
|
|
if (big_endian) {
|
|
|
|
var index: usize = buf.len;
|
|
|
|
while (index != 0) {
|
|
|
|
index -= 1;
|
|
|
|
|
|
|
|
buf[index] = @truncate(u8, bits);
|
|
|
|
bits >>= 8;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (buf) |*b| {
|
|
|
|
*b = @truncate(u8, bits);
|
|
|
|
bits >>= 8;
|
|
|
|
}
|
2017-02-07 14:19:51 -08:00
|
|
|
}
|
2017-02-12 14:22:35 -08:00
|
|
|
assert(bits == 0);
|
2017-02-07 14:19:51 -08:00
|
|
|
}
|
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "testStringEquality" {
|
2017-02-12 14:22:35 -08:00
|
|
|
assert(eql(u8, "abcd", "abcd"));
|
|
|
|
assert(!eql(u8, "abcdef", "abZdef"));
|
|
|
|
assert(!eql(u8, "abcdefg", "abcdef"));
|
2017-02-07 14:19:51 -08:00
|
|
|
}
|
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "testReadInt" {
|
2017-02-12 14:22:35 -08:00
|
|
|
testReadIntImpl();
|
|
|
|
comptime testReadIntImpl();
|
|
|
|
}
|
|
|
|
fn testReadIntImpl() {
|
|
|
|
{
|
|
|
|
const bytes = []u8{ 0x12, 0x34, 0x56, 0x78 };
|
|
|
|
assert(readInt(bytes, u32, true) == 0x12345678);
|
|
|
|
assert(readInt(bytes, u32, false) == 0x78563412);
|
|
|
|
}
|
2016-09-22 23:00:23 -07:00
|
|
|
{
|
|
|
|
const buf = []u8{0x00, 0x00, 0x12, 0x34};
|
2017-02-12 14:22:35 -08:00
|
|
|
const answer = readInt(buf, u64, true);
|
2016-09-22 23:00:23 -07:00
|
|
|
assert(answer == 0x00001234);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const buf = []u8{0x12, 0x34, 0x00, 0x00};
|
2017-02-12 14:22:35 -08:00
|
|
|
const answer = readInt(buf, u64, false);
|
2016-09-22 23:00:23 -07:00
|
|
|
assert(answer == 0x00003412);
|
|
|
|
}
|
|
|
|
}
|
2017-02-12 14:22:35 -08:00
|
|
|
|
2017-03-16 13:02:35 -07:00
|
|
|
test "testWriteInt" {
|
2017-02-12 14:22:35 -08:00
|
|
|
testWriteIntImpl();
|
|
|
|
comptime testWriteIntImpl();
|
|
|
|
}
|
|
|
|
fn testWriteIntImpl() {
|
|
|
|
var bytes: [4]u8 = undefined;
|
|
|
|
|
|
|
|
writeInt(bytes[0...], u32(0x12345678), true);
|
|
|
|
assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));
|
|
|
|
|
|
|
|
writeInt(bytes[0...], u32(0x78563412), false);
|
|
|
|
assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));
|
|
|
|
|
|
|
|
writeInt(bytes[0...], u16(0x1234), true);
|
|
|
|
assert(eql(u8, bytes, []u8{ 0x00, 0x00, 0x12, 0x34 }));
|
|
|
|
|
|
|
|
writeInt(bytes[0...], u16(0x1234), false);
|
|
|
|
assert(eql(u8, bytes, []u8{ 0x34, 0x12, 0x00, 0x00 }));
|
|
|
|
}
|