flipperzero-zig/lib/flipperzero/heap.zig

49 lines
1.2 KiB
Zig
Raw Normal View History

2024-08-22 02:27:21 +00:00
const std = @import("std");
const Allocator = std.mem.Allocator;
const flipperzero = @import("flipperzero.zig");
const c = flipperzero.c;
pub const FuriAllocator = struct {
fn alloc(
_: *anyopaque,
len: usize,
log2_align: u8,
return_address: usize,
) ?[*]u8 {
_ = return_address;
const alignment = @as(usize, 1) << @intCast(log2_align);
return @ptrCast(c.aligned_malloc(len, alignment));
}
fn resize(
_: *anyopaque,
buf: []u8,
log2_buf_align: u8,
new_len: usize,
return_address: usize,
) bool {
_ = return_address;
_ = log2_buf_align;
return new_len <= buf.len;
}
fn free(
_: *anyopaque,
buf: []u8,
log2_buf_align: u8,
return_address: usize,
) void {
_ = return_address;
_ = log2_buf_align;
c.aligned_free(buf.ptr);
}
};
const furi_allocator_vtable = Allocator.VTable{
.alloc = FuriAllocator.alloc,
.resize = FuriAllocator.resize,
.free = FuriAllocator.free,
};
pub const furi_allocator = Allocator{ .ptr = undefined, .vtable = &furi_allocator_vtable };