#Does FixedBufferAllocator work on architectures below 32 bits?

1 messages · Page 1 of 1 (latest)

uncut briar
#

I'm writing a small program for a 328P based microcontroller, which has 8-bit registers, and i think it uses a 16-bit pointer size, but when i tried to create a FBA, the compiler complains about this pointer size, as seen in fig.1.

var alloc_data: [100]u8 = undefined;

pub fn main() !void {
    var fba = std.heap.FixedBufferAllocator.init(&alloc_data);
    const alloc = fba.allocator();
    _ = alloc;
}

The allocator is used to temporarily store formatted strings to display via uart and an LCD right after, which has a limited character length, so FBA is a good choice for this.

Is there a built-in allocator that would work for this use case? Otherwise, is there a recommended 3rd party allocator that would work?
0.10 is used instead of 0.11 or master because of issue #16417.

tidal wren
#

this appears to be a problem with how the allocator interface itself assumes the pointer size to be 32 or 64 bits (or greater)

#

there'd need to be a fix in the interface itself to allow for this use case to make it possible to use literally any allocator

uncut briar
#

so, there's no way to use any allocator? That means i'll likely have to implement my own Writer for both use cases; a little annoying

tidal wren
#

just to note, there is std.fmt.bufPrint

#

also, I've written a couple utility functions for bounded formatting:

pub fn fixedLenFmt(
    comptime fmt_str: []const u8,
    args: anytype,
    comptime reference_args: @TypeOf(args),
) error{ Overflow, Underflow }![std.fmt.count(fmt_str, reference_args)]u8 {
    const bounded = try boundedFmt(fmt_str, args, reference_args);
    if (bounded.len != bounded.buffer.len) return error.Underflow;
    return bounded.constSlice()[0..bounded.buffer.len].*;
}

pub fn boundedFmt(
    comptime fmt_str: []const u8,
    args: anytype,
    comptime reference_args: @TypeOf(args),
) error{Overflow}!std.BoundedArray(u8, std.fmt.count(fmt_str, reference_args)) {
    const len = comptime std.fmt.count(fmt_str, reference_args);
    var result: std.BoundedArray(u8, len) = .{};
    try result.writer().print(fmt_str, args);
    return result;
}

usage being:

const int_str = try boundedFmt("{d}", .{@as(u32, runtime_int)}, .{std.math.maxInt(u32)});
trail flower
#

You could just edit the standard library. At least the issue with alignBackwardAnyAlign could be solved with a cast.

tidal wren
#

At any rate, I would suggest filing an issue on github - that way this may be amended

uncut briar
tidal wren
uncut briar
#

yeah, i think llvm 17 has better support, but this is using llvm 15

tidal wren
#

? zig is on llvm 16 currently

uncut briar
#

this is 0.10, not 0.11

tidal wren
#

ahhh I see

trail flower
#

I think 0.12 is using llvm 17. I tried the example in issue #16417 that should reproduce the error and it seems to compile fine on the latest 0.12 dev release.

uncut briar
#

updated to 0.12, but i'm stuck with this issue

tidal wren
#

I believe you have to call functions using inline asm inside a naked function

#

see _start in the std.start file for an example

uncut briar
#

yeah, _start should be called by asm (".section .vectors\njmp _start\n")

tidal wren
#

well then does it even need to be .Naked?

uncut briar
#

good question

#

it appears that it doesn't need to

tidal wren
#

well then there ygo

#

though I wonder whether that assembly needs to do any setup then

uncut briar
#

it does do setup

tidal wren
#

fair enough, guess you're set then

uncut briar
#

not exactly
using the provided boundedFmt

#

and this happens when using either FBA or fmt.bufPrint

final raptor
#

zig's compiler-rt isn't fully feature complete, so its likely missing some of the functions for smaller bit-width architectures

uncut briar
#

i think this is bugged?

wise meteor
#

if x is comptime known then the type of the second argument is something like struct { comptime u32 = x } so when you tell the third argument to use the same type, it breaks because you can't change the comptime value