Skip to content

Commit

Permalink
mem_slab: rationalize block alignment assertion
Browse files Browse the repository at this point in the history
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 Jun 20, 2019
1 parent d888cb5 commit bc30f4f
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions kernel/mem_slab.c
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit bc30f4f

Please sign in to comment.