From cf974371fbccb66939c5b73e546ada6aacf9021c Mon Sep 17 00:00:00 2001 From: Nicolas Pitre Date: Wed, 26 Jun 2019 11:32:58 -0400 Subject: [PATCH] mempool: make alignment/rounding 64-bit compatible Minimum alignment and rounding must be done on a word boundary. Let's replace _ALIGN4() with WB_UP() which is equivalent on 32-bit targets, and 64-bit aware. Also enforce a minimal alignment on the memory pool. This is making a difference mostly on64-bit targets where the widely used 4-byte alignment is not sufficient. The _ALIGN4() macro has no users left so it is removed. Signed-off-by: Nicolas Pitre --- include/kernel.h | 4 ++-- include/sys/mempool.h | 6 +++--- include/sys/mempool_base.h | 2 -- lib/libc/minimal/source/stdlib/malloc.c | 2 +- lib/os/mempool.c | 6 +++--- 5 files changed, 9 insertions(+), 11 deletions(-) diff --git a/include/kernel.h b/include/kernel.h index fe58675098d..2a38e284905 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -4159,13 +4159,13 @@ struct k_mem_pool { * @req K-MPOOL-001 */ #define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \ - char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz) * nmax \ + char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \ + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \ struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \ Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \ .base = { \ .buf = _mpool_buf_##name, \ - .max_sz = _ALIGN4(maxsz), \ + .max_sz = WB_UP(maxsz), \ .n_max = nmax, \ .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \ .levels = _mpool_lvls_##name, \ diff --git a/include/sys/mempool.h b/include/sys/mempool.h index d863a3f7174..cf2b29d111d 100644 --- a/include/sys/mempool.h +++ b/include/sys/mempool.h @@ -47,15 +47,15 @@ struct sys_mem_pool_block { * @param section Destination binary section for pool data */ #define SYS_MEM_POOL_DEFINE(name, ignored, minsz, maxsz, nmax, align, section) \ - char __aligned(align) Z_GENERIC_SECTION(section) \ - _mpool_buf_##name[_ALIGN4(maxsz) * nmax \ + char __aligned(WB_UP(align)) Z_GENERIC_SECTION(section) \ + _mpool_buf_##name[WB_UP(maxsz) * nmax \ + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \ struct sys_mem_pool_lvl Z_GENERIC_SECTION(section) \ _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \ Z_GENERIC_SECTION(section) struct sys_mem_pool name = { \ .base = { \ .buf = _mpool_buf_##name, \ - .max_sz = _ALIGN4(maxsz), \ + .max_sz = WB_UP(maxsz), \ .n_max = nmax, \ .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \ .levels = _mpool_lvls_##name, \ diff --git a/include/sys/mempool_base.h b/include/sys/mempool_base.h index 66e27a7190d..9b66d63b016 100644 --- a/include/sys/mempool_base.h +++ b/include/sys/mempool_base.h @@ -36,8 +36,6 @@ struct sys_mem_pool_base { u8_t flags; }; -#define _ALIGN4(n) ((((n)+3)/4)*4) - #define Z_MPOOL_HAVE_LVL(maxsz, minsz, l) (((maxsz) >> (2*(l))) \ >= (minsz) ? 1 : 0) diff --git a/lib/libc/minimal/source/stdlib/malloc.c b/lib/libc/minimal/source/stdlib/malloc.c index 39a7c17a28b..c081257d291 100644 --- a/lib/libc/minimal/source/stdlib/malloc.c +++ b/lib/libc/minimal/source/stdlib/malloc.c @@ -107,7 +107,7 @@ void *realloc(void *ptr, size_t requested_size) */ block_size = blk->pool->base.max_sz; for (int i = 1; i <= blk->level; i++) { - block_size = _ALIGN4(block_size / 4); + block_size = WB_UP(block_size / 4); } /* We really need this much memory */ diff --git a/lib/os/mempool.c b/lib/os/mempool.c index 4184639330d..75f42cc0187 100644 --- a/lib/os/mempool.c +++ b/lib/os/mempool.c @@ -111,7 +111,7 @@ void z_sys_mem_pool_base_init(struct sys_mem_pool_base *p) bits += (nblocks + 31)/32; } - sz = _ALIGN4(sz / 4); + sz = WB_UP(sz / 4); } for (i = 0; i < p->n_max; i++) { @@ -259,7 +259,7 @@ int z_sys_mem_pool_block_alloc(struct sys_mem_pool_base *p, size_t size, lsizes[0] = p->max_sz; for (i = 0; i < p->n_levels; i++) { if (i > 0) { - lsizes[i] = _ALIGN4(lsizes[i-1] / 4); + lsizes[i] = WB_UP(lsizes[i-1] / 4); } if (lsizes[i] < size) { @@ -331,7 +331,7 @@ void z_sys_mem_pool_block_free(struct sys_mem_pool_base *p, u32_t level, */ lsizes[0] = p->max_sz; for (i = 1; i <= level; i++) { - lsizes[i] = _ALIGN4(lsizes[i-1] / 4); + lsizes[i] = WB_UP(lsizes[i-1] / 4); } block_free(p, level, lsizes, block);