Memory pool object alignment

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
iggarpe
Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

Memory pool object alignment

Post by iggarpe »

Hi all,

I guess like many other newbies before, I've had to deal with the memory pool object aligment problem. I'm using MDK-ARM on a cortex-M4F and the following code will halt the system if debug checks are enabled (and crash and burn if not when using the pool):

Code: Select all

typedef struct {
    struct pool_header *phdr;
    uint32_t a;
    uint32_t b;
} mpobj_t;

static mpobj_t mpobj[100];

MEMORYPOOL_DECL(mpobj_pool, sizeof(mpobj), NULL);

int main(void)  {
    int i;
    for (i = 0; i < 100; i++)
        chPoolFree(&mpobj_pool, &mpobj[i]);
}



First, I'm using 2.4.3 and it's a true pain in the ass to have to code a loop to fill the pool, though I've noticed it's been taken care of in the trunk.

The halt (or havoc if debug checks not enabled) is caused by the fact that mobj_t size is 12, and the arm v7 port:

1- Defines stkalign_t to int64 (i.e. 8 bytes alignent).
2- Rounds up the pool object size to a multiple of alignment (i.e. will asume managed objects' size is 16 bytes).
3- Debug checks each object pased to chPoolFree to be aligned to 8 bytes.

Now, I've seen the comments in chcore_v7m.h:

Code: Select all

/**
 * @brief   Stack and memory alignment enforcement.
 * @note    In this architecture the stack alignment is enforced to 64 bits,
 *          32 bits alignment is supported by hardware but deprecated by ARM,
 *          the implementation choice is to not offer the option.
 */
typedef uint64_t stkalign_t;


So my question would be: if the actual requirement of the memory pool objects is JUST that they are aligned to contain a pointer to void, the hardware supports 4 byte aligment of pointers, THE COMPILER by default aligns to 4 bytes boundaries... why on earth make the programmer's life miserable by requiring specific compiler black magic to align objects to be placed in a memory pool?. Really, why?.

Rgds.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Memory pool object alignment

Post by Giovanni »

Hi,

The version 2.5.x no more requires for structures to be aligned to 64 bits, just requires an alignment suitable for a pointer. I will backport the subsystem to 2.4.x.

Giovanni
Post Reply