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

xiaoxiang781216 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 90da3b2d172 mm/mm_gran: Reserve only the granules that hold the range.
90da3b2d172 is described below

commit 90da3b2d172bbc4d7b1e23ce25884ce31e4691e4
Author: Alan Carvalho de Assis <[email protected]>
AuthorDate: Mon Nov 16 17:05:18 2026 -0300

    mm/mm_gran: Reserve only the granules that hold the range.
    
    gran_reserve() rounded the last byte of the range up to the start of
    the next granule, and then counted that granule too.  It reserved one
    granule more than the range needs:
    
    * A reservation of one granule marked two granules as used.
    * A reservation of the last granule of the heap failed, because the
      next granule is outside the heap.
    * gran_free() of the reserved range left the extra granule reserved.
    
    Round the last byte down to its granule, as for the first byte.
    
    Signed-off-by: Alan C. Assis <[email protected]>
    Assisted-by: Claude Code:claude-opus-5-5
---
 mm/mm_gran/mm_granreserve.c | 2 +-
 mm/mm_gran/mm_grantable.h   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/mm_gran/mm_granreserve.c b/mm/mm_gran/mm_granreserve.c
index f2d76269b0e..e2a5fdf7ad4 100644
--- a/mm/mm_gran/mm_granreserve.c
+++ b/mm/mm_gran/mm_granreserve.c
@@ -54,7 +54,7 @@ FAR void *gran_reserve(GRAN_HANDLE handle, uintptr_t start, 
size_t size)
       return NULL;
     }
 
-  /* align down/up start/ending addresses */
+  /* The granules that hold the first and the last byte */
 
   end = END_RSRV(gran, start, size);
   if (!GRAN_INRANGE(gran, end))
diff --git a/mm/mm_gran/mm_grantable.h b/mm/mm_gran/mm_grantable.h
index 235a26f9ff7..01824d2aefb 100644
--- a/mm/mm_gran/mm_grantable.h
+++ b/mm/mm_gran/mm_grantable.h
@@ -56,7 +56,7 @@
 /* gran_reserve related */
 
 #define MEM_RSRV(g, m)      ALIGNDN(g, m)
-#define END_RSRV(g, m, s)   ALIGNUP(g, (((size_t)m) + s - 1))
+#define END_RSRV(g, m, s)   ALIGNDN(g, (((size_t)m) + s - 1))
 #define NUM_RSRV(g, m, s)   (((END_RSRV(g, m, s) - MEM_RSRV(g, m)) \
                                  >> g->log2gran) + 1)
 #define LEN_RSRV(g, m, s)   ((size_t)(NUM_RSRV(g, m, s) << g->log2gran))

Reply via email to