Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Public Struct

BufferFirstAllocator

Public Struct

An allocator that attempts to allocate from the given buffer, falling back to fallback_allocator if this fails.

Fields

fallback_allocator: Allocator
fixed_buffer_allocator: FixedBufferAllocator

Namespaces

Types

Functions

pub fn init(buffer: []u8, fallback_allocator: Allocator) BufferFirstAllocator
pub fn allocator(self: *BufferFirstAllocator) Allocator
fn alloc(ctx: *anyopaque, len: usize, alignment: Alignment, ra: usize) ?[*]u8
fn resize(ctx: *anyopaque, buf: []u8, alignment: Alignment, new_len: usize, ra: usize) bool
fn remap(context: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, return_address: usize) ?[*]u8
fn free(ctx: *anyopaque, buf: []u8, alignment: Alignment, ra: usize) void

File

heap/BufferFirstAllocator.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//! An allocator that attempts to allocate from the given buffer, falling back to
//! `fallback_allocator` if this fails.

const std = @import("../std.zig");
const heap = std.heap;
const testing = std.testing;

const Alignment = std.mem.Alignment;
const Allocator = std.mem.Allocator;
const FixedBufferAllocator = std.heap.FixedBufferAllocator;

const BufferFirstAllocator = @This();

fallback_allocator: Allocator,
fixed_buffer_allocator: FixedBufferAllocator,

pub fn init(buffer: []u8, fallback_allocator: Allocator) BufferFirstAllocator {
    return .{
        .fallback_allocator = fallback_allocator,
        .fixed_buffer_allocator = .init(buffer),
    };
}

pub fn allocator(self: *BufferFirstAllocator) Allocator {
    return .{
        .ptr = self,
        .vtable = &.{
            .alloc = alloc,
            .resize = resize,
            .remap = remap,
            .free = free,
        },
    };
}

fn alloc(
    ctx: *anyopaque,
    len: usize,
    alignment: Alignment,
    ra: usize,
) ?[*]u8 {
    const self: *BufferFirstAllocator = @ptrCast(@alignCast(ctx));
    return FixedBufferAllocator.alloc(&self.fixed_buffer_allocator, len, alignment, ra) orelse
        return self.fallback_allocator.rawAlloc(len, alignment, ra);
}

fn resize(
    ctx: *anyopaque,
    buf: []u8,
    alignment: Alignment,
    new_len: usize,
    ra: usize,
) bool {
    const self: *BufferFirstAllocator = @ptrCast(@alignCast(ctx));
    if (self.fixed_buffer_allocator.ownsPtr(buf.ptr)) {
        return FixedBufferAllocator.resize(&self.fixed_buffer_allocator, buf, alignment, new_len, ra);
    } else {
        return self.fallback_allocator.rawResize(buf, alignment, new_len, ra);
    }
}

fn remap(
    context: *anyopaque,
    memory: []u8,
    alignment: Alignment,
    new_len: usize,
    return_address: usize,
) ?[*]u8 {
    const self: *BufferFirstAllocator = @ptrCast(@alignCast(context));
    if (self.fixed_buffer_allocator.ownsPtr(memory.ptr)) {
        return FixedBufferAllocator.remap(&self.fixed_buffer_allocator, memory, alignment, new_len, return_address);
    } else {
        return self.fallback_allocator.rawRemap(memory, alignment, new_len, return_address);
    }
}

fn free(
    ctx: *anyopaque,
    buf: []u8,
    alignment: Alignment,
    ra: usize,
) void {
    const self: *BufferFirstAllocator = @ptrCast(@alignCast(ctx));
    if (self.fixed_buffer_allocator.ownsPtr(buf.ptr)) {
        return FixedBufferAllocator.free(&self.fixed_buffer_allocator, buf, alignment, ra);
    } else {
        return self.fallback_allocator.rawFree(buf, alignment, ra);
    }
}

test "BufferFirstAllocator" {
    // Buffer first specific tests
    {
        var buffer: [10]u8 = undefined;
        var bfa_state: BufferFirstAllocator = .init(&buffer, std.testing.allocator);
        const bfa = bfa_state.allocator();

        // We're under the limit, so we should be allocated in the buffer
        const txt0 = "hellowrld";
        const buf0 = try bfa.create(@TypeOf(txt0.*));
        buf0.* = txt0.*;
        try testing.expect(bfa_state.fixed_buffer_allocator.ownsPtr(buf0.ptr));

        // We're now over the limit, so we should be allocated from the fallback
        const txt1 = "test!";
        const buf1 = try bfa.create(@TypeOf(txt1.*));
        buf1.* = txt1.*;
        try testing.expect(!bfa_state.fixed_buffer_allocator.ownsPtr(buf1.ptr));

        // Free the allocation that took up space in the buffer
        try testing.expectEqualStrings(txt0, buf0);
        bfa.destroy(buf0);

        // The next allocation would go in the buffer, but it's too big so it doesn't
        const txt2 = "qwertyqwerty";
        const buf2 = try bfa.create(@TypeOf(txt2.*));
        buf2.* = txt2.*;
        try testing.expect(!bfa_state.fixed_buffer_allocator.ownsPtr(buf2.ptr));

        // The next allocation is smaller and fits in the buffer
        const txt3 = "dvorak";
        const buf3 = try bfa.create(@TypeOf(txt3.*));
        buf3.* = txt3.*;
        try testing.expect(bfa_state.fixed_buffer_allocator.ownsPtr(buf3.ptr));

        // The remainder in the buffer is too small for the following allocation so it falls back
        const txt4 = "moretext";
        const buf4 = try bfa.create(@TypeOf(txt4.*));
        buf4.* = txt4.*;
        try testing.expect(!bfa_state.fixed_buffer_allocator.ownsPtr(buf4.ptr));

        // Check equality on the remaining buffers and free them
        try testing.expectEqualStrings(txt1, buf1);
        bfa.destroy(buf1);
        try testing.expectEqualStrings(txt2, buf2);
        bfa.destroy(buf2);
        try testing.expectEqualStrings(txt3, buf3);
        bfa.destroy(buf3);
        try testing.expectEqualStrings(txt4, buf4);
        bfa.destroy(buf4);

        try testing.expectEqual(0, bfa_state.fixed_buffer_allocator.end_index);
    }

    // Standard allocator tests
    {
        var buf: [4096]u8 = undefined;
        {
            var bfa: BufferFirstAllocator = .init(&buf, std.testing.allocator);
            try heap.testAllocator(bfa.allocator());
        }
        {
            var bfa: BufferFirstAllocator = .init(&buf, std.testing.allocator);
            try heap.testAllocatorAligned(bfa.allocator());
        }
        {
            var bfa: BufferFirstAllocator = .init(&buf, std.testing.allocator);
            try heap.testAllocatorLargeAlignment(bfa.allocator());
        }
        {
            var bfa: BufferFirstAllocator = .init(&buf, std.testing.allocator);
            try heap.testAllocatorAlignedShrink(bfa.allocator());
        }
    }
}