Heap less space then expected

Discussions and support about ChibiOS/RT, the free embedded RTOS.
Post Reply
User avatar
wurstnase
Posts: 121
Joined: Tue Oct 17, 2017 2:24 pm
Has thanked: 43 times
Been thanked: 30 times

Heap less space then expected

Post by wurstnase »

Hi,

I'm using the latest stable_19.1.x branch.
my code looks like:

Code: Select all

#define ALLOC_SIZE 256
#define MB_BUFFER_SIZE 8

typedef struct {
    uint16_t size;
    uint8_t buf[ALLOC_SIZE - 2];
} uart_msg_t;

static memory_heap_t heap_reader;
static uart_msg_t heap_reader_buffer[MB_BUFFER_SIZE];

static THD_FUNCTION(uart_reader, arg) {
    (void)arg;
    static uart_msg_t *uart_msg, *uart_msg_new;
    chHeapObjectInit(&heap_reader, heap_reader_buffer,
                     sizeof(heap_reader_buffer));

    uart_msg = chHeapAlloc(&heap_reader, sizeof(uart_msg_t));
    while (true) {
        do_stuff();
        uart_msg = chHeapAlloc(&heap_reader, sizeof(uart_msg_t));
        if (NULL == uart_msg)
        // breakpoint


I always jump in the breakpoint after 7 allocations. I expect it should hit it after 8 allocs.
Any hints are welcome.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Heap less space then expected

Post by Giovanni »

Hi,

Two points:

1) The heap allocator puts an 8 bytes header below each allocated block, you need to consider the overhead.

2) If you allocate objects of the same size, then a Pool is the preferred allocator (way way better in that scenario).

Giovanni
User avatar
wurstnase
Posts: 121
Joined: Tue Oct 17, 2017 2:24 pm
Has thanked: 43 times
Been thanked: 30 times

Re: Heap less space then expected

Post by wurstnase »

Ah ok. This wasn't obvious.

I already changed it to a pool after the post. But thanks for the hint.
Post Reply