Skip to content
Permalink
Browse files

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>
  • Loading branch information...
Nicolas Pitre authored and nashif committed May 22, 2019
1 parent d888cb5 commit bc30f4f0194b74b8081a771e605ab5ec5c83d028
Showing with 5 additions and 8 deletions.
  1. +5 −8 kernel/mem_slab.c
@@ -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;

0 comments on commit bc30f4f

Please sign in to comment.
You can’t perform that action at this time.