microzig icon indicating copy to clipboard operation
microzig copied to clipboard

Added general purpose bare metal allocator.

Open leibupro opened this issue 1 year ago • 4 comments

Howdy,

currently using this bare metal allocator on a M4 CPU micro controller. Perhaps you have a use for it too.

Wrote already a bunch of unit tests but plan to write further test cases when I find time.

Cheers P.

leibupro avatar Oct 19 '24 22:10 leibupro

Are you open to do some adjustments on the allocator?

ikskuh avatar Oct 20 '24 08:10 ikskuh

In general, yes. Depends a little on the change set size and currently available time.

leibupro avatar Oct 20 '24 09:10 leibupro

pub fn BareMetalAllocator( comptime memory_area: []u8 ) type

Can you write at least a doc comment on what memory_area is supposed to do? You allocate memory_area.len chunks of pointers, so i don't assume it's the memory area you use for user allocations, right?

ikskuh avatar Oct 20 '24 18:10 ikskuh

memory_area (assigned to memory_vault internally) is indeed a fat pointer to the memory area which is used for the memory allocations. The byte field which is managed by the allocator. The metadata_space is an array of memory cell metadata (specific memory slice and a pointer to the next cell) of length memory_area.len. This means, that in the extreme case, the allocator can hold n memory cells of 1 byte length. Where n is the length in bytes of the memory_area.

You're right, I'm going to add a descriptive doc comment like written in the previous paragraph.

leibupro avatar Oct 20 '24 20:10 leibupro

Just to get it right?


pub fn BareMetalAllocator( comptime memory_area: []u8 ) type {
    return struct {
        const MemoryCell = struct {
            memory: []u8,
            next: ?*MemoryCell,
        };
        memory_vault: []u8 = memory_area,
        metadata_space: [ memory_area.len ]MemoryCell = undefined,
    };
}

So i pass a slice with 16kB to memory_arena, when i want to have 16kB of heap? But wouldn't that mean that the allocator will be 12*16kB = 192kB bytes large (assuming 32 bit pointers), as MemoryCell is 12 bytes large

ikskuh avatar Oct 21 '24 18:10 ikskuh

Yes, that is the case. Since the smallest memory cell can be one byte and the allocator has to manage memory_area.len 1 byte cells theoretically. It is described before the test code starts, line 710 ff.

leibupro avatar Oct 21 '24 18:10 leibupro

It doesn't make sense to use such an allocator in a memory constraint area, considering that it has such a crazy memory overhead. It would allow you having less than 150 bytes of heap on a typical AVR device, and on a lot of other MCUs less than 2k

ikskuh avatar Oct 25 '24 16:10 ikskuh