aboutsummaryrefslogtreecommitdiff
path: root/std/mem.zig
blob: ba65679c73a59bb3507f8a444e8e31073ace2365 (plain)
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
const assert = @import("debug.zig").assert;
const math = @import("math.zig");
const os = @import("os.zig");
const io = @import("io.zig");

pub const Cmp = math.Cmp;

pub error NoMem;

pub type Context = u8;
pub struct Allocator {
    allocFn: fn (self: &Allocator, n: usize) -> %[]u8,
    reallocFn: fn (self: &Allocator, old_mem: []u8, new_size: usize) -> %[]u8,
    freeFn: fn (self: &Allocator, mem: []u8),
    context: ?&Context,

    /// Aborts the program if an allocation fails.
    fn checkedAlloc(self: &Allocator, inline T: type, n: usize) -> []T {
        alloc(self, T, n) %% |err| {
            // TODO var args printf
            %%io.stderr.write("allocation failure: ");
            %%io.stderr.write(@errorName(err));
            %%io.stderr.printf("\n");
            os.abort()
        }
    }

    fn alloc(self: &Allocator, inline T: type, n: usize) -> %[]T {
        const byte_count = %return math.mulOverflow(usize, @sizeOf(T), n);
        ([]T)(%return self.allocFn(self, byte_count))
    }

    fn realloc(self: &Allocator, inline T: type, old_mem: []T, n: usize) -> %[]T {
        const byte_count = %return math.mulOverflow(usize, @sizeOf(T), n);
        ([]T)(%return self.reallocFn(self, ([]u8)(old_mem), byte_count))
    }

    // TODO mem: []var and get rid of 2nd param
    fn free(self: &Allocator, inline T: type, mem: []T) {
        self.freeFn(self, ([]u8)(mem));
    }
}

/// Copy all of source into dest at position 0.
/// dest.len must be >= source.len.
pub fn copy(inline T: type, dest: []T, source: []const T) {
    assert(dest.len >= source.len);
    @memcpy(dest.ptr, source.ptr, @sizeOf(T) * source.len);
}

/// Return < 0, == 0, or > 0 if memory a is less than, equal to, or greater than,
/// memory b, respectively.
pub fn cmp(inline T: type, a: []const T, b: []const T) -> Cmp {
    const n = math.min(a.len, b.len);
    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;
}

pub fn sliceAsInt(buf: []u8, is_be: bool, inline T: type) -> T {
    var result: T = zeroes;
    const result_slice = ([]u8)((&result)[0...1]);
    const padding = @sizeOf(T) - buf.len;

    if (is_be == @compileVar("is_big_endian")) {
        copy(u8, result_slice, buf);
    } else {
        for (buf) |b, i| {
            const index = result_slice.len - i - 1 - padding;
            result_slice[index] = b;
        }
    }
    return result;
}

fn testSliceAsInt() {
    @setFnTest(this, true);
    {
        const buf = []u8{0x00, 0x00, 0x12, 0x34};
        const answer = sliceAsInt(buf[0...], true, u64);
        assert(answer == 0x00001234);
    }
    {
        const buf = []u8{0x12, 0x34, 0x00, 0x00};
        const answer = sliceAsInt(buf[0...], false, u64);
        assert(answer == 0x00003412);
    }
}