Added general purpose bare metal allocator.
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.
Are you open to do some adjustments on the allocator?
In general, yes. Depends a little on the change set size and currently available time.
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?
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.
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
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.
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