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

commit d5ad01add0cd22765bd5d01f4ced5cf144d9921f
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Oct 27 09:37:33 2026 +0100

    libs/libc/stdlib: Make malloc_usable_size() a function.
    
    malloc_usable_size() was a macro of malloc_size() in <malloc.h>, and
    libc had no malloc_usable_size symbol.  A link test of the function
    (autoconf's AC_CHECK_FUNCS, for example) did not find it, and a program
    that declares the function itself did not link.
    
    Now malloc_usable_size() is a function of libc that calls malloc_size().
    It returns 0 for NULL, as on other systems.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/malloc.h                          |  5 +--
 libs/libc/libc.csv                        |  1 +
 libs/libc/stdlib/CMakeLists.txt           |  1 +
 libs/libc/stdlib/Make.defs                |  2 +-
 libs/libc/stdlib/lib_malloc_usable_size.c | 54 +++++++++++++++++++++++++++++++
 5 files changed, 58 insertions(+), 5 deletions(-)

diff --git a/include/malloc.h b/include/malloc.h
index cb238857f66..4738dc4d577 100644
--- a/include/malloc.h
+++ b/include/malloc.h
@@ -42,10 +42,6 @@
 #define PID_MM_LEAK    ((pid_t)-2)
 #define PID_MM_MEMPOOL ((pid_t)-1)
 
-/* For Linux and MacOS compatibility */
-
-#define malloc_usable_size malloc_size
-
 /* mallopt options that actually do something */
 
 #define M_TRIM_THRESHOLD    -1
@@ -102,6 +98,7 @@ extern "C"
 int mallopt(int, int);
 struct mallinfo mallinfo(void);
 size_t malloc_size(FAR void *ptr);
+size_t malloc_usable_size(FAR void *ptr);
 struct mallinfo_task mallinfo_task(FAR const struct malltask *task);
 
 #if defined(__cplusplus)
diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv
index 8984f1576c6..ee03d577bf3 100644
--- a/libs/libc/libc.csv
+++ b/libs/libc/libc.csv
@@ -177,6 +177,7 @@
 "mallinfo","malloc.h","","struct mallinfo","void"
 "malloc","stdlib.h","","FAR void *","size_t"
 "malloc_size","malloc.h","","size_t","FAR void *"
+"malloc_usable_size","malloc.h","","size_t","FAR void *"
 "mblen","stdlib.h","","int","FAR const char *","size_t"
 "mbrlen","wchar.h","","size_t","FAR const char *","size_t","FAR mbstate_t *"
 "mbrtowc","wchar.h","","size_t","FAR wchar_t *","FAR const char 
*","size_t","FAR mbstate_t *"
diff --git a/libs/libc/stdlib/CMakeLists.txt b/libs/libc/stdlib/CMakeLists.txt
index 28c1fa63843..3df38bf0df6 100644
--- a/libs/libc/stdlib/CMakeLists.txt
+++ b/libs/libc/stdlib/CMakeLists.txt
@@ -55,6 +55,7 @@ set(SRCS
     lib_aligned_alloc.c
     lib_posix_memalign.c
     lib_valloc.c
+    lib_malloc_usable_size.c
     lib_mblen.c
     lib_mbtowc.c
     lib_wctomb.c
diff --git a/libs/libc/stdlib/Make.defs b/libs/libc/stdlib/Make.defs
index 50063b2c37c..ae9d24db3fe 100644
--- a/libs/libc/stdlib/Make.defs
+++ b/libs/libc/stdlib/Make.defs
@@ -30,7 +30,7 @@ CSRCS += lib_strtoll.c lib_strtoul.c lib_strtoull.c 
lib_checkbase.c
 CSRCS += lib_mktemp.c lib_mkstemp.c lib_mkdtemp.c lib_aligned_alloc.c
 CSRCS += lib_posix_memalign.c lib_valloc.c lib_mblen.c lib_mbtowc.c
 CSRCS += lib_wctomb.c lib_mbstowcs.c lib_wcstombs.c lib_atexit.c
-CSRCS += lib_reallocarray.c lib_arc4random.c
+CSRCS += lib_reallocarray.c lib_arc4random.c lib_malloc_usable_size.c
 
 ifneq ($(CONFIG_LIBM_NONE),y)
 CSRCS += lib_strtold.c
diff --git a/libs/libc/stdlib/lib_malloc_usable_size.c 
b/libs/libc/stdlib/lib_malloc_usable_size.c
new file mode 100644
index 00000000000..9dde101fdd9
--- /dev/null
+++ b/libs/libc/stdlib/lib_malloc_usable_size.c
@@ -0,0 +1,54 @@
+/****************************************************************************
+ * libs/libc/stdlib/lib_malloc_usable_size.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <malloc.h>
+
+#include "libc.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: malloc_usable_size
+ *
+ * Description:
+ *   Return the number of usable bytes in the block of memory at ptr, which
+ *   malloc() or a related function returned.  This is the same as
+ *   malloc_size(), the name of the function on other systems.
+ *
+ * Input Parameters:
+ *   ptr - The block of memory, or NULL.
+ *
+ * Returned Value:
+ *   The number of usable bytes; 0 if ptr is NULL.
+ *
+ ****************************************************************************/
+
+size_t malloc_usable_size(FAR void *ptr)
+{
+  return lib_malloc_size(ptr);
+}

Reply via email to