CV-Bowen opened a new pull request, #21876:
URL: https://github.com/apache/nuttx/pull/21876

   ## Summary
   
   Extend mm/mempool so that a pool can be defined statically, bounded and
   waited on with a timeout:
   
   - `mempool_allocate(pool, timeout)`: `0` returns at once, `UINT_MAX` waits
     forever, other values wait up to `timeout` ms.
   - `maxalloc` caps the number of blocks a pool hands out (0 = no limit);
     shown in a new `maxalloc` column of `/proc/mempool` (line buffer raised
     from 80 to 128 to fit it).
   - `initialbase` lets the initial blocks live in a caller-provided buffer.
   - `mempool_navail()` returns how many more blocks may be allocated.
   - The pool name moves into `struct mempool_s`; `mempool_init()` drops its
     `name` argument.
   - `MEMPOOL_REALBLOCKSIZE()` takes a block size instead of a pool, so it
     can size static buffers.
   - `mempool_multiple_init()` zeroes the structure once with memset.
   - `MEMPOOL_DEFINE(name, blksize, prealloc, maxalloc, dynalloc)` defines a
     pool and its buffer statically; any mempool is initialized on first
     use, falling back to the user/kernel heap when no alloc/free/check
     callbacks are set.
   
   The last four commits fix the new code:
   - void pointer arithmetic in the `initialbase` range check;
   - `MEMPOOL_DEFINE()` sizes missing the pool header (one block short, no
     dynamic expansion);
   - `mempool_deinit()` not clearing `init`, so a pool could not be
     re-initialized;
   - static pools that cannot expand never waiting, because the wait test
     was `expandsize == 0`.
   
   The PR has 12 commits because it is one internal series kept as it was
   written: the eight API commits each change the same two files in turn,
   and the four fixes from other authors are kept separate to retain their
   authorship.
   
   ## Impact
   
   - API change: `mempool_init()` and `mempool_allocate()` signatures and the
     `MEMPOOL_REALBLOCKSIZE()` argument change. All in-tree callers are in
     `mm/mempool/` and are updated; no other users in nuttx or nuttx-apps.
   - `struct mempool_s` gains `initialbase`, `maxalloc`, `name`, `init`.
     Existing users that zero the structure keep the old behavior.
   - `/proc/mempool` gains a `maxalloc` column.
   - No Kconfig change.
   
   ## Testing
   
   Host: Ubuntu 22.04.5 LTS x86_64, QEMU version 10.2.1, sim: cc (Ubuntu 
13.1.0-8ubuntu1~22.04) 13.1.0, qemu-armv8a: aarch64-none-elf-gcc (Arm GNU 
Toolchain 12.2.Rel1 (Build arm-12.24)) 12.2.1 20221205
   
   A small test program (below) exercises the new API on sim:nsh, built
   twice: as is, and with `CONFIG_MM_BACKTRACE=8`,
   `CONFIG_MM_HEAP_MEMPOOL_THRESHOLD=256`,
   `CONFIG_MM_HEAP_MEMPOOL_WAIT_RELEASE=y` (heap mempool on, procfs list).
   It checks:
   
   - a `MEMPOOL_DEFINE` pool is not initialized until first access, then
     hands out exactly `maxalloc` blocks; `mempool_navail()` counts down and
     back up;
   - on a full static pool, `timeout=0` returns NULL immediately,
     `timeout=200` returns NULL after ~200 ms, and `UINT_MAX`/`1000` return
     the block another thread releases after 100 ms;
   - a dynamic pool expands, `mempool_deinit()` succeeds, and the same pool
     initializes and works again (two rounds);
   - malloc/free of 64 small sizes through the heap mempool.
   
   | | before the last four fixes (`21d9b51`) | this PR |
   |---|---|---|
   | mempooltest | `FAIL test_static_maxalloc:37 b[i] != NULL`, then hangs in 
the wait test | `PASS (0 failures)` in both builds |
   
   ```
   cmake -B build -DBOARD_CONFIG=sim:nsh -GNinja
   echo CONFIG_TESTING_MEMPOOL=y >> build/.config   # plus the three options 
above
   cmake --build build -t olddefconfig && cmake --build build
   ./build/nuttx
   nsh> mempooltest
   nsh> cat /proc/mempool
   ```
   
   <details><summary>sim:nsh with heap mempool</summary>
   
   ```
   nsh> mempooltest
   mempooltest: PASS (0 failures)
   nsh> cat /proc/mempool
                      total    bsize    nused    nfree   nifree  nwaiter 
maxalloc
       g_expool:        688      112        0        6        0        0        
6
           Umem:      18520      336       23       32        0        0        0
           Umem:      23760      208       68       46        0        0        0
   ```
   </details>
   
   <details><summary>test program (testing/mm/mempooltest/mempool_main.c, not 
part of this PR)</summary>
   
   ```c
   #include <nuttx/config.h>
   #include <nuttx/mm/mempool.h>
   #include <limits.h>
   #include <pthread.h>
   #include <stdio.h>
   #include <stdlib.h>
   #include <string.h>
   #include <time.h>
   #include <unistd.h>
   
   #define CHECK(c) do { if (!(c)) { printf("FAIL %s:%d %s\n", __func__, \
     __LINE__, #c); g_fail++; } } while (0)
   
   static int g_fail;
   
   MEMPOOL_DEFINE(g_expool, 32, 4, 6, 1);
   MEMPOOL_DEFINE(g_fixpool, 48, 2, 0, 0);
   
   static long now_ms(void)
   {
     struct timespec ts;
     clock_gettime(CLOCK_MONOTONIC, &ts);
     return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
   }
   
   static void test_static_maxalloc(void)
   {
     void *b[7];
     int i;
   
     CHECK(!g_expool.init);
     CHECK(mempool_navail(&g_expool) == 6);
     CHECK(g_expool.init);
     for (i = 0; i < 6; i++)
       {
         b[i] = mempool_allocate(&g_expool, 0);
         CHECK(b[i] != NULL);
         memset(b[i], 0xa5, 32);
       }
   
     CHECK(mempool_navail(&g_expool) == 0);
     b[6] = mempool_allocate(&g_expool, 0);
     CHECK(b[6] == NULL);
     mempool_release(&g_expool, b[0]);
     CHECK(mempool_navail(&g_expool) == 1);
     for (i = 1; i < 6; i++)
       {
         mempool_release(&g_expool, b[i]);
       }
   
     CHECK(mempool_navail(&g_expool) == 6);
   }
   
   static void *releaser(void *arg)
   {
     usleep(100 * 1000);
     mempool_release(&g_fixpool, arg);
     return NULL;
   }
   
   static void test_static_wait(void)
   {
     pthread_t th;
     void *a;
     void *b;
     void *c;
     long t;
   
     a = mempool_allocate(&g_fixpool, 0);
     b = mempool_allocate(&g_fixpool, 0);
     CHECK(a != NULL && b != NULL);
     CHECK(mempool_navail(&g_fixpool) == SIZE_MAX);
   
     t = now_ms();
     CHECK(mempool_allocate(&g_fixpool, 0) == NULL);
     CHECK(now_ms() - t < 20);
   
     t = now_ms();
     CHECK(mempool_allocate(&g_fixpool, 200) == NULL);
     t = now_ms() - t;
     CHECK(t >= 190 && t < 400);
   
     pthread_create(&th, NULL, releaser, a);
     t = now_ms();
     c = mempool_allocate(&g_fixpool, UINT_MAX);
     t = now_ms() - t;
     CHECK(c == a);
     CHECK(t >= 90 && t < 400);
     pthread_join(th, NULL);
   
     pthread_create(&th, NULL, releaser, b);
     c = mempool_allocate(&g_fixpool, 1000);
     CHECK(c == b);
     pthread_join(th, NULL);
   
     mempool_release(&g_fixpool, a);
     mempool_release(&g_fixpool, b);
     CHECK(mempool_deinit(&g_fixpool) == 0);
   }
   
   static void test_dynamic_reinit(void)
   {
     struct mempool_s pool;
     struct mempoolinfo_s info;
     void *blk[8];
     int round;
     int i;
   
     memset(&pool, 0, sizeof(pool));
     pool.blocksize   = 24;
     pool.initialsize = 4 * MEMPOOL_REALBLOCKSIZE(24) + MEMPOOL_HEADER_SIZE;
     pool.expandsize  = 2 * MEMPOOL_REALBLOCKSIZE(24) + MEMPOOL_HEADER_SIZE;
     pool.name        = "mptest";
   
     CHECK(mempool_init(&pool) == 0);
     for (round = 0; round < 2; round++)
       {
         for (i = 0; i < 8; i++)
           {
             blk[i] = mempool_allocate(&pool, 0);
             CHECK(blk[i] != NULL);
           }
   
         mempool_info(&pool, &info);
         CHECK(info.aordblks == 8);
         CHECK(info.maxalloc == 0);
         for (i = 0; i < 8; i++)
           {
             mempool_release(&pool, blk[i]);
           }
   
         CHECK(mempool_deinit(&pool) == 0);
         CHECK(!pool.init);
       }
   }
   
   static void test_heap_mempool(void)
   {
     void *p[64];
     int i;
   
     for (i = 0; i < 64; i++)
       {
         p[i] = malloc(8 + i * 3);
         CHECK(p[i] != NULL);
         memset(p[i], i, 8 + i * 3);
       }
   
     for (i = 0; i < 64; i++)
       {
         free(p[i]);
       }
   }
   
   int main(int argc, char *argv[])
   {
     test_static_maxalloc();
     test_static_wait();
     test_dynamic_reinit();
     test_heap_mempool();
     printf("mempooltest: %s (%d failures)\n", g_fail ? "FAIL" : "PASS",
            g_fail);
     return g_fail;
   }
   ```
   </details>
   
   Every commit builds with no warning on sim:nsh and on sim:nsh with the
   three options above.
   
   No regression in rpmsg: sim:rpserver/rpproxy and 
qemu-armv8a:rpserver/rpproxy boot and pass the fixed rpmsg cases (dump, ping, 
rpmsgfs; 15/15). qemu-armv8a was built with LIBC_ENVPATH and BINFMT_CHECKEXEC 
disabled, which current master needs for nxinit to start nsh there; unrelated 
to this change.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to