From d054c6e15dda83fd5f90ae76aa1e7601002dfd35 Mon Sep 17 00:00:00 2001 From: Jerzy Kasenberg Date: Fri, 23 Aug 2024 11:45:49 +0200 Subject: [PATCH] kernel/os: Fix msys allocation with multiply mpools When msys was configured with several mpools and one of the pools run out of buffers, os_mbuf_get() returned NULL even though other mpool did have buffers available. Now search for suitable buffer checks if suitable pool is empty and if not search continues. --- kernel/os/src/os_msys.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/kernel/os/src/os_msys.c b/kernel/os/src/os_msys.c index 3b287a31a8..fcb56f1a12 100644 --- a/kernel/os/src/os_msys.c +++ b/kernel/os/src/os_msys.c @@ -86,30 +86,32 @@ os_msys_reset(void) STAILQ_INIT(&g_msys_pool_list); } +static struct os_mbuf_pool *os_msys_find_pool(uint16_t dsize); + static struct os_mbuf_pool * os_msys_find_biggest_pool(void) { - /* Mempools are sorted by the blocksize, so just return last one */ - return STAILQ_LAST(&g_msys_pool_list, os_mbuf_pool, omp_next); + return os_msys_find_pool(0xFFFF); } static struct os_mbuf_pool * os_msys_find_pool(uint16_t dsize) { struct os_mbuf_pool *pool; + struct os_mbuf_pool *pool_with_free_blocks = NULL; + uint16_t pool_free_blocks; - pool = NULL; STAILQ_FOREACH(pool, &g_msys_pool_list, omp_next) { - if (dsize <= pool->omp_databuf_len) { - break; + pool_free_blocks = pool->omp_pool->mp_num_free; + if (pool_free_blocks != 0) { + pool_with_free_blocks = pool; + if (dsize <= pool->omp_databuf_len) { + break; + } } } - if (!pool) { - pool = STAILQ_LAST(&g_msys_pool_list, os_mbuf_pool, omp_next); - } - - return (pool); + return pool_with_free_blocks; }