This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new de88b47fecd mm/mm_gran: Count granules in 32 bits.
de88b47fecd is described below

commit de88b47fecd5869549a3414b9a17294d7be1d492
Author: Marco Casaroli <[email protected]>
AuthorDate: Sun Oct 11 11:49:33 2026 +0200

    mm/mm_gran: Count granules in 32 bits.
    
    struct gran_s, struct graninfo_s and struct pginfo_s stored granule and
    page counts in uint16_t, so a granule allocator could manage at most
    65535 granules.  For the page allocator of a kernel build with 4 KiB
    pages that is less than 256 MiB of memory for all programs, whatever
    the size of the RAM.  gran_initialize() rejected a larger pool.
    
    Store the counts in uint32_t, reject only an empty pool, and widen the
    page counts before sysinfo() shifts them to bytes.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/nuttx/mm/gran.h     | 6 +++---
 include/nuttx/pgalloc.h     | 6 +++---
 mm/mm_gran/mm_gran.h        | 2 +-
 mm/mm_gran/mm_graninfo.c    | 2 +-
 mm/mm_gran/mm_graninit.c    | 6 +++---
 sched/sched/sched_sysinfo.c | 4 ++--
 6 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/include/nuttx/mm/gran.h b/include/nuttx/mm/gran.h
index 01d8b0141e0..666a4cadd2e 100644
--- a/include/nuttx/mm/gran.h
+++ b/include/nuttx/mm/gran.h
@@ -63,9 +63,9 @@ typedef FAR void *GRAN_HANDLE;
 struct graninfo_s
 {
   uint8_t   log2gran;  /* Log base 2 of the size of one granule */
-  uint16_t  ngranules; /* The total number of (aligned) granules in the heap */
-  uint16_t  nfree;     /* The number of free granules */
-  uint16_t  mxfree;    /* The longest sequence of free granules */
+  uint32_t  ngranules; /* The total number of (aligned) granules in the heap */
+  uint32_t  nfree;     /* The number of free granules */
+  uint32_t  mxfree;    /* The longest sequence of free granules */
 };
 
 /****************************************************************************
diff --git a/include/nuttx/pgalloc.h b/include/nuttx/pgalloc.h
index bec03d9b4e7..4726b00d496 100644
--- a/include/nuttx/pgalloc.h
+++ b/include/nuttx/pgalloc.h
@@ -95,9 +95,9 @@
 
 struct pginfo_s
 {
-  uint16_t  ntotal;  /* The total number of pages */
-  uint16_t  nfree;   /* The number of free pages */
-  uint16_t  mxfree;  /* The longest sequence of free pages */
+  uint32_t  ntotal;  /* The total number of pages */
+  uint32_t  nfree;   /* The number of free pages */
+  uint32_t  mxfree;  /* The longest sequence of free pages */
 };
 
 /****************************************************************************
diff --git a/mm/mm_gran/mm_gran.h b/mm/mm_gran/mm_gran.h
index 4257cd78855..c56e1f517ff 100644
--- a/mm/mm_gran/mm_gran.h
+++ b/mm/mm_gran/mm_gran.h
@@ -69,7 +69,7 @@ struct gran_s
 {
   uint8_t    log2gran;  /* Log base 2 of the size of one granule */
   uint8_t    log2align; /* Log base 2 of required alignment */
-  uint16_t   ngranules; /* The total number of (aligned) granules in the heap 
*/
+  uint32_t   ngranules; /* The total number of (aligned) granules in the heap 
*/
 #ifdef CONFIG_GRAN_INTR
   irqstate_t irqstate;  /* For exclusive access to the GAT */
   spinlock_t lock;
diff --git a/mm/mm_gran/mm_graninfo.c b/mm/mm_gran/mm_graninfo.c
index 73775e77385..489d198c70b 100644
--- a/mm/mm_gran/mm_graninfo.c
+++ b/mm/mm_gran/mm_graninfo.c
@@ -488,7 +488,7 @@ void gran_info(GRAN_HANDLE handle, FAR struct graninfo_s 
*info)
   FAR struct gran_s *priv = (FAR struct gran_s *)handle;
   uint32_t mask;
   uint32_t value;
-  uint16_t mxfree;
+  uint32_t mxfree;
   unsigned int nbits;
   unsigned int granidx;
   unsigned int gatidx;
diff --git a/mm/mm_gran/mm_graninit.c b/mm/mm_gran/mm_graninit.c
index ade271c2c46..c4cf626715e 100644
--- a/mm/mm_gran/mm_graninit.c
+++ b/mm/mm_gran/mm_graninit.c
@@ -124,10 +124,10 @@ GRAN_HANDLE gran_initialize(FAR void *heapstart, size_t 
heapsize,
   alignedsize  = (heapend - alignedstart) & ~mask;
   ngranules    = alignedsize >> log2gran;
 
-  /* Reject oversized pools. */
+  /* Reject empty pools. */
 
-  DEBUGASSERT(ngranules > 0 && ngranules <= UINT16_MAX);
-  if (ngranules == 0 || ngranules > UINT16_MAX)
+  DEBUGASSERT(ngranules > 0);
+  if (ngranules == 0)
     {
       return NULL;
     }
diff --git a/sched/sched/sched_sysinfo.c b/sched/sched/sched_sysinfo.c
index 94d380a6b48..cb07779d45a 100644
--- a/sched/sched/sched_sysinfo.c
+++ b/sched/sched/sched_sysinfo.c
@@ -101,8 +101,8 @@ int sysinfo(FAR struct sysinfo *info)
 #ifdef CONFIG_MM_PGALLOC
       mm_pginfo(&pginfo);
 
-      info->totalram += pginfo.ntotal << MM_PGSHIFT;
-      info->freeram  += pginfo.nfree  << MM_PGSHIFT;
+      info->totalram += (unsigned long)pginfo.ntotal << MM_PGSHIFT;
+      info->freeram  += (unsigned long)pginfo.nfree  << MM_PGSHIFT;
 #endif
 
       info->uptime   = TICK2SEC(clock_systime_ticks());

Reply via email to