You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ma...@apache.org on 2021/07/05 05:24:11 UTC

[incubator-nuttx] branch master updated: mm: Add kmm_malloc_size and mm_malloc_size

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 75bfa45  mm: Add kmm_malloc_size and mm_malloc_size
75bfa45 is described below

commit 75bfa4584c3d571fe751f4dc14bc4cb2b384f5d5
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Wed Jun 30 20:33:29 2021 -0700

    mm: Add kmm_malloc_size and mm_malloc_size
    
    make malloc_size implementation align with malloc
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
    Change-Id: I8d7781925f06e58a880437a16569dccbfd2ea035
---
 arch/sim/src/sim/up_heap.c                         |  2 +-
 include/nuttx/kmalloc.h                            |  2 +
 include/nuttx/lib/lib.h                            | 44 ++++++++++++----------
 include/nuttx/mm/mm.h                              | 10 +++++
 mm/kmm_heap/Make.defs                              |  2 +-
 .../kmm_malloc_size.c}                             | 31 +++------------
 mm/mm_heap/mm_malloc_size.c                        |  3 +-
 mm/umm_heap/Make.defs                              |  2 +-
 .../umm_malloc_size.c}                             | 25 +-----------
 9 files changed, 48 insertions(+), 73 deletions(-)

diff --git a/arch/sim/src/sim/up_heap.c b/arch/sim/src/sim/up_heap.c
index 5192728..decc534 100644
--- a/arch/sim/src/sim/up_heap.c
+++ b/arch/sim/src/sim/up_heap.c
@@ -413,7 +413,7 @@ void mm_checkcorruption(FAR struct mm_heap_s *heap)
  * Name: malloc_size
  ****************************************************************************/
 
-size_t malloc_size(FAR void *mem)
+size_t mm_malloc_size(FAR void *mem)
 {
   return host_malloc_size(mem);
 }
diff --git a/include/nuttx/kmalloc.h b/include/nuttx/kmalloc.h
index d75e845..9ea6916 100644
--- a/include/nuttx/kmalloc.h
+++ b/include/nuttx/kmalloc.h
@@ -73,6 +73,7 @@ extern "C"
 
 #define kumm_calloc(n,s)         calloc(n,s);
 #define kumm_malloc(s)           malloc(s)
+#define kumm_malloc_size(p)      malloc_size(p)
 #define kumm_zalloc(s)           zalloc(s)
 #define kumm_realloc(p,s)        realloc(p,s)
 #define kumm_memalign(a,s)       memalign(a,s)
@@ -92,6 +93,7 @@ extern "C"
 
 #  define kmm_calloc(n,s)        calloc(n,s);
 #  define kmm_malloc(s)          malloc(s)
+#  define kmm_malloc_size(p)     malloc_size(p)
 #  define kmm_zalloc(s)          zalloc(s)
 #  define kmm_realloc(p,s)       realloc(p,s)
 #  define kmm_memalign(a,s)      memalign(a,s)
diff --git a/include/nuttx/lib/lib.h b/include/nuttx/lib/lib.h
index 51c71ec..2b2e1f6 100644
--- a/include/nuttx/lib/lib.h
+++ b/include/nuttx/lib/lib.h
@@ -45,37 +45,41 @@
 
   /* Domain-specific allocations */
 
-#  define lib_malloc(s)      kmm_malloc(s)
-#  define lib_zalloc(s)      kmm_zalloc(s)
-#  define lib_realloc(p,s)   kmm_realloc(p,s)
-#  define lib_memalign(p,s)  kmm_memalign(p,s)
-#  define lib_free(p)        kmm_free(p)
+#  define lib_malloc(s)       kmm_malloc(s)
+#  define lib_malloc_size(p)  kmm_malloc_size(p)
+#  define lib_zalloc(s)       kmm_zalloc(s)
+#  define lib_realloc(p,s)    kmm_realloc(p,s)
+#  define lib_memalign(p,s)   kmm_memalign(p,s)
+#  define lib_free(p)         kmm_free(p)
 
   /* User-accessible allocations */
 
-#  define lib_umalloc(s)     kumm_malloc(s)
-#  define lib_uzalloc(s)     kumm_zalloc(s)
-#  define lib_urealloc(p,s)  kumm_realloc(p,s)
-#  define lib_umemalign(p,s) kumm_memalign(p,s)
-#  define lib_ufree(p)       kumm_free(p)
+#  define lib_umalloc(s)      kumm_malloc(s)
+#  define lib_umalloc_size(p) kumm_malloc_size(p)
+#  define lib_uzalloc(s)      kumm_zalloc(s)
+#  define lib_urealloc(p,s)   kumm_realloc(p,s)
+#  define lib_umemalign(p,s)  kumm_memalign(p,s)
+#  define lib_ufree(p)        kumm_free(p)
 
 #else
 
   /* Domain-specific allocations */
 
-#  define lib_malloc(s)      malloc(s)
-#  define lib_zalloc(s)      zalloc(s)
-#  define lib_realloc(p,s)   realloc(p,s)
-#  define lib_memalign(p,s)  memalign(p,s)
-#  define lib_free(p)        free(p)
+#  define lib_malloc(s)       malloc(s)
+#  define lib_malloc_size(p)  malloc_size(p)
+#  define lib_zalloc(s)       zalloc(s)
+#  define lib_realloc(p,s)    realloc(p,s)
+#  define lib_memalign(p,s)   memalign(p,s)
+#  define lib_free(p)         free(p)
 
   /* User-accessible allocations */
 
-#  define lib_umalloc(s)     malloc(s)
-#  define lib_uzalloc(s)     zalloc(s)
-#  define lib_urealloc(p,s)  realloc(p,s)
-#  define lib_umemalign(p,s) memalign(p,s)
-#  define lib_ufree(p)       free(p)
+#  define lib_umalloc(s)      malloc(s)
+#  define lib_umalloc_size(p) malloc_size(p)
+#  define lib_uzalloc(s)      zalloc(s)
+#  define lib_urealloc(p,s)   realloc(p,s)
+#  define lib_umemalign(p,s)  memalign(p,s)
+#  define lib_ufree(p)        free(p)
 
 #endif
 
diff --git a/include/nuttx/mm/mm.h b/include/nuttx/mm/mm.h
index 872c781..eb5ffd9 100644
--- a/include/nuttx/mm/mm.h
+++ b/include/nuttx/mm/mm.h
@@ -193,6 +193,16 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size);
 FAR void *kmm_malloc(size_t size);
 #endif
 
+/* Functions contained in mm_malloc_size.c **********************************/
+
+size_t mm_malloc_size(FAR void *mem);
+
+/* Functions contained in kmm_malloc_size.c *********************************/
+
+#ifdef CONFIG_MM_KERNEL_HEAP
+size_t kmm_malloc_size(FAR void *mem);
+#endif
+
 /* Functions contained in mm_free.c *****************************************/
 
 void mm_free(FAR struct mm_heap_s *heap, FAR void *mem);
diff --git a/mm/kmm_heap/Make.defs b/mm/kmm_heap/Make.defs
index 8fcf7fc..135832f 100644
--- a/mm/kmm_heap/Make.defs
+++ b/mm/kmm_heap/Make.defs
@@ -22,7 +22,7 @@
 
 ifeq ($(CONFIG_MM_KERNEL_HEAP),y)
 
-CSRCS += kmm_initialize.c kmm_addregion.c
+CSRCS += kmm_initialize.c kmm_addregion.c kmm_malloc_size.c
 CSRCS += kmm_brkaddr.c kmm_calloc.c kmm_extend.c kmm_free.c kmm_mallinfo.c
 CSRCS += kmm_malloc.c kmm_memalign.c kmm_realloc.c kmm_zalloc.c kmm_heapmember.c
 
diff --git a/mm/mm_heap/mm_malloc_size.c b/mm/kmm_heap/kmm_malloc_size.c
similarity index 71%
copy from mm/mm_heap/mm_malloc_size.c
copy to mm/kmm_heap/kmm_malloc_size.c
index 40bc7b2..6d20045 100644
--- a/mm/mm_heap/mm_malloc_size.c
+++ b/mm/kmm_heap/kmm_malloc_size.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * mm/mm_heap/mm_malloc_size.c
+ * mm/kmm_heap/kmm_malloc_size.c
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -24,36 +24,17 @@
 
 #include <nuttx/config.h>
 
-#include <assert.h>
-#include <debug.h>
-#include <malloc.h>
-
 #include <nuttx/mm/mm.h>
 
-#include "mm_heap/mm.h"
+#ifdef CONFIG_MM_KERNEL_HEAP
 
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
 
-size_t malloc_size(FAR void *mem)
+size_t kmm_malloc_size(FAR void *mem)
 {
-  FAR struct mm_freenode_s *node;
-
-  /* Protect against attempts to query a NULL reference */
-
-  if (!mem)
-    {
-      return 0;
-    }
-
-  /* Map the memory chunk into a free node */
-
-  node = (FAR struct mm_freenode_s *)((FAR char *)mem - SIZEOF_MM_ALLOCNODE);
-
-  /* Sanity check against double-frees */
-
-  DEBUGASSERT(node->preceding & MM_ALLOC_BIT);
-
-  return node->size - SIZEOF_MM_ALLOCNODE;
+  return mm_malloc_size(mem);
 }
+
+#endif /* CONFIG_MM_KERNEL_HEAP */
diff --git a/mm/mm_heap/mm_malloc_size.c b/mm/mm_heap/mm_malloc_size.c
index 40bc7b2..98ffde6 100644
--- a/mm/mm_heap/mm_malloc_size.c
+++ b/mm/mm_heap/mm_malloc_size.c
@@ -26,7 +26,6 @@
 
 #include <assert.h>
 #include <debug.h>
-#include <malloc.h>
 
 #include <nuttx/mm/mm.h>
 
@@ -36,7 +35,7 @@
  * Public Functions
  ****************************************************************************/
 
-size_t malloc_size(FAR void *mem)
+size_t mm_malloc_size(FAR void *mem)
 {
   FAR struct mm_freenode_s *node;
 
diff --git a/mm/umm_heap/Make.defs b/mm/umm_heap/Make.defs
index 1446371..c992160 100644
--- a/mm/umm_heap/Make.defs
+++ b/mm/umm_heap/Make.defs
@@ -20,7 +20,7 @@
 
 # User heap allocator
 
-CSRCS += umm_globals.c umm_initialize.c umm_addregion.c
+CSRCS += umm_globals.c umm_initialize.c umm_addregion.c umm_malloc_size.c
 CSRCS += umm_brkaddr.c umm_calloc.c umm_extend.c umm_free.c umm_mallinfo.c
 CSRCS += umm_malloc.c umm_memalign.c umm_realloc.c umm_zalloc.c umm_heapmember.c
 
diff --git a/mm/mm_heap/mm_malloc_size.c b/mm/umm_heap/umm_malloc_size.c
similarity index 74%
copy from mm/mm_heap/mm_malloc_size.c
copy to mm/umm_heap/umm_malloc_size.c
index 40bc7b2..74c0e76 100644
--- a/mm/mm_heap/mm_malloc_size.c
+++ b/mm/umm_heap/umm_malloc_size.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * mm/mm_heap/mm_malloc_size.c
+ * mm/umm_heap/umm_malloc_size.c
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -24,36 +24,15 @@
 
 #include <nuttx/config.h>
 
-#include <assert.h>
-#include <debug.h>
 #include <malloc.h>
 
 #include <nuttx/mm/mm.h>
 
-#include "mm_heap/mm.h"
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
 
 size_t malloc_size(FAR void *mem)
 {
-  FAR struct mm_freenode_s *node;
-
-  /* Protect against attempts to query a NULL reference */
-
-  if (!mem)
-    {
-      return 0;
-    }
-
-  /* Map the memory chunk into a free node */
-
-  node = (FAR struct mm_freenode_s *)((FAR char *)mem - SIZEOF_MM_ALLOCNODE);
-
-  /* Sanity check against double-frees */
-
-  DEBUGASSERT(node->preceding & MM_ALLOC_BIT);
-
-  return node->size - SIZEOF_MM_ALLOCNODE;
+  return mm_malloc_size(mem);
 }