尝试从给定缓冲区分配内存,失败时回退到
fallback_allocator 的分配器。
字段
fallback_allocator: Allocator
fixed_buffer_allocator: FixedBufferAllocator
std.heap.BufferFirstAllocator.Alignment
别名
别名目标 std.mem.Alignment
std.heap.BufferFirstAllocator.alloc
函数
fn alloc( ctx: *anyopaque, len: usize, alignment: Alignment, ra: usize, ) ?[*]u8
参数
std.heap.BufferFirstAllocator.allocator
函数
pub fn allocator(self: *BufferFirstAllocator) Allocator
参数
self: *BufferFirstAllocator
std.heap.BufferFirstAllocator.Allocator
别名
别名目标 std.mem.Allocator
std.heap.BufferFirstAllocator.BufferFirstAllocator
别名
std.heap.BufferFirstAllocator.FixedBufferAllocator
别名
std.heap.BufferFirstAllocator.free
函数
fn free( ctx: *anyopaque, buf: []u8, alignment: Alignment, ra: usize, ) void
参数
std.heap.BufferFirstAllocator.heap
别名
别名目标 std.heap
std.heap.BufferFirstAllocator.init
函数
pub fn init(buffer: []u8, fallback_allocator: Allocator) BufferFirstAllocator
参数
buffer: []u8
fallback_allocator: Allocator
std.heap.BufferFirstAllocator.remap
函数
fn remap( context: *anyopaque, memory: []u8, alignment: Alignment, new_len: usize, return_address: usize, ) ?[*]u8
参数
std.heap.BufferFirstAllocator.resize
函数
fn resize( ctx: *anyopaque, buf: []u8, alignment: Alignment, new_len: usize, ra: usize, ) bool
参数
std.heap.BufferFirstAllocator.std
别名
别名目标 std
std.heap.BufferFirstAllocator.testing
别名
别名目标 std.testing
源码
std/heap/BufferFirstAllocator.zig
//! 尝试从给定缓冲区分配内存,失败时回退到
//! `fallback_allocator` 的分配器。
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" {
// 优先针对缓冲区的专项测试
{
var buffer: [10]u8 = undefined;
var bfa_state: BufferFirstAllocator = .init(&buffer, std.testing.allocator);
const bfa = bfa_state.allocator();
// 未达到限制,因此应在缓冲区中完成分配。
const txt0 = "hellowrld";
const buf0 = try bfa.create(@TypeOf(txt0.*));
buf0.* = txt0.*;
try testing.expect(bfa_state.fixed_buffer_allocator.ownsPtr(buf0.ptr));
// 现在已超过限制,因此应从备用分配器分配
const txt1 = "test!";
const buf1 = try bfa.create(@TypeOf(txt1.*));
buf1.* = txt1.*;
try testing.expect(!bfa_state.fixed_buffer_allocator.ownsPtr(buf1.ptr));
// 释放占用缓冲区空间的分配
try testing.expectEqualStrings(txt0, buf0);
bfa.destroy(buf0);
// 下一次分配本应放入缓冲区,但因过大而未放入
const txt2 = "qwertyqwerty";
const buf2 = try bfa.create(@TypeOf(txt2.*));
buf2.* = txt2.*;
try testing.expect(!bfa_state.fixed_buffer_allocator.ownsPtr(buf2.ptr));
// 下一次分配更小,能容纳于缓冲区
const txt3 = "dvorak";
const buf3 = try bfa.create(@TypeOf(txt3.*));
buf3.* = txt3.*;
try testing.expect(bfa_state.fixed_buffer_allocator.ownsPtr(buf3.ptr));
// 缓冲区剩余空间不足以容纳后续分配,因此转用回退路径。
const txt4 = "moretext";
const buf4 = try bfa.create(@TypeOf(txt4.*));
buf4.* = txt4.*;
try testing.expect(!bfa_state.fixed_buffer_allocator.ownsPtr(buf4.ptr));
// 比较剩余缓冲区是否相等,并释放它们
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);
}
// 标准分配器测试
{
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());
}
}
}