mem_slab: rationalize block alignment assertion

The block alignment must be enforced for statically allocated slabs
as well as runtime initialized ones. It is best to implement this
check only once in create_free_list() which is invoked by both
k_mem_slab_init() and init_mem_slab_module(), where pointers are about
to be set for the first time. It is then unnecessary to perform this
test on every slab allocation as the alignment won't change at that
point.

And not only the block size needs to be aligned, but the buffer
as well.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
This commit is contained in:
Nicolas Pitre 2019-05-21 21:15:00 -04:00 committed by Anas Nashif
parent d888cb54f9
commit bc30f4f019

View file

@ -33,6 +33,11 @@ static void create_free_list(struct k_mem_slab *slab)
u32_t j;
char *p;
/* blocks must be word aligned */
__ASSERT(((slab->block_size | (uintptr_t)slab->buffer)
& (sizeof(void *) - 1)) == 0,
"slab at %p not word aligned", slab);
slab->free_list = NULL;
p = slab->buffer;
@ -68,10 +73,6 @@ SYS_INIT(init_mem_slab_module, PRE_KERNEL_1,
void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
size_t block_size, u32_t num_blocks)
{
/* block size must be word aligned */
__ASSERT((slab->block_size & (sizeof(void *) - 1)) == 0,
"block size not word aligned");
slab->num_blocks = num_blocks;
slab->block_size = block_size;
slab->buffer = buffer;
@ -88,10 +89,6 @@ int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem, s32_t timeout)
k_spinlock_key_t key = k_spin_lock(&lock);
int result;
/* block size must be word aligned */
__ASSERT((slab->block_size & (sizeof(void *) - 1)) == 0,
"block size not word aligned");
if (slab->free_list != NULL) {
/* take a free block */
*mem = slab->free_list;