Heap less space then expected

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

Heap less space then expected

Postby wurstnase » Thu Jul 25, 2019 9:00 am

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.
\o/ Nico

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Heap less space then expected

Postby Giovanni » Thu Jul 25, 2019 5:05 pm

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
Contact:

Re: Heap less space then expected

Postby wurstnase » Fri Jul 26, 2019 5:41 am

Ah ok. This wasn't obvious.

I already changed it to a pool after the post. But thanks for the hint.
\o/ Nico


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 29 guests