Re: [PATCH] added malloc usable size and test

2022-03-15 Thread Joel Sherrill
On Tue, Mar 15, 2022 at 10:25 AM Joel Sherrill  wrote:

>
>
> On Fri, Mar 4, 2022 at 8:58 PM zack leung 
> wrote:
>
>> closes #4503
>> ---
>>  cpukit/include/rtems/libcsupport.h|  5 +++
>>  cpukit/libcsupport/src/mallocusablesize.c | 48 +++
>>  spec/build/cpukit/librtemscpu.yml |  1 +
>>  testsuites/libtests/malloctest/init.c | 15 ++-
>>  4 files changed, 68 insertions(+), 1 deletion(-)
>>  create mode 100644 cpukit/libcsupport/src/mallocusablesize.c
>>
>> diff --git a/cpukit/include/rtems/libcsupport.h
>> b/cpukit/include/rtems/libcsupport.h
>> index f4be4cfc9a..5110ab0fbe 100644
>> --- a/cpukit/include/rtems/libcsupport.h
>> +++ b/cpukit/include/rtems/libcsupport.h
>> @@ -74,6 +74,11 @@ extern size_t malloc_free_space(void);
>>   */
>>  extern int malloc_info(Heap_Information_block *the_info);
>>
>> +/**
>> + * @brief Find the usable size of the block of memory .
>> + */
>> +extern size_t malloc_usable_size(void *ptr);
>> +
>>
>
> This should be in newlib's malloc.h to align with how it is done
> on Linux. This is an RTEMS specific header file.  I went ahead
> and posted the patch for this since it was small:
>
> https://sourceware.org/pipermail/newlib/2022/019430.html
>

Prototype is already in malloc.h  which is included in your tools.

Do not modify libcsupport.h and just include 

>
>
>
>>  /*
>>   *  Prototypes required to install newlib reentrancy user extension
>>   */
>> diff --git a/cpukit/libcsupport/src/mallocusablesize.c
>> b/cpukit/libcsupport/src/mallocusablesize.c
>> new file mode 100644
>> index 00..b7e573023a
>> --- /dev/null
>> +++ b/cpukit/libcsupport/src/mallocusablesize.c
>> @@ -0,0 +1,48 @@
>> +/*
>> + * SPDX-License-Identifier: BSD-2-Clause
>> + *
>> + *  Copyright (C) 2022 zacchaeus leung
>> + *
>> + * Redistribution and use in source and binary forms, with or without
>> + * modification, are permitted provided that the following conditions
>> + * are met:
>> + * 1. Redistributions of source code must retain the above copyright
>> + *notice, this list of conditions and the following disclaimer.
>> + * 2. Redistributions in binary form must reproduce the above copyright
>> + *notice, this list of conditions and the following disclaimer in the
>> + *documentation and/or other materials provided with the
>> distribution.
>> + *
>> + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
>> "AS
>> IS"
>> + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
>> THE
>> + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
>> PURPOSE
>> + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
>> BE
>> + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
>> + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
>> + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
>> BUSINESS
>> + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
>> IN
>> + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
>> OTHERWISE)
>> + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
>> THE
>> + * POSSIBILITY OF SUCH DAMAGE.
>> + */
>> +
>> +#ifdef HAVE_CONFIG_H
>> +#include "config.h"
>> +#endif
>> +
>> +#include 
>> +#include 
>> +#include 
>> +
>> +size_t malloc_usable_size( void *ptr ) {
>> +
>> +  Heap_Control *heap_ptr ;
>> +  size_t size;
>> +  if ( ptr == NULL ) {
>>
>
> Blank line above the if. Space between ) and {
>
>
>> +return 0;
>> +  }
>> +
>> +  heap_ptr = malloc_get_heap_pointer();
>> +  _Heap_Size_of_alloc_area( heap_ptr, ptr, &size );
>>
>>
> This should be _Protected_heap_Get_block_size() to ensure that
> locking is performed.
>
>
>> +  return size;
>> +}
>> diff --git a/spec/build/cpukit/librtemscpu.yml
>> b/spec/build/cpukit/librtemscpu.yml
>> index c224937348..2ef11e7e2d 100644
>> --- a/spec/build/cpukit/librtemscpu.yml
>> +++ b/spec/build/cpukit/librtemscpu.yml
>> @@ -670,6 +670,7 @@ source:
>>  - cpukit/libcsupport/src/lseek.c
>>  - cpukit/libcsupport/src/lstat.c
>>  - cpukit/libcsupport/src/malloc.c
>> +- cpukit/libcsupport/src/mallocusablesize.c
>>  - cpukit/libcsupport/src/malloc_deferred.c
>>  - cpukit/libcsupport/src/malloc_dirtier.c
>>  - cpukit/libcsupport/src/malloc_walk.c
>> diff --git a/testsuites/libtests/malloctest/init.c
>> b/testsuites/libtests/malloctest/init.c
>> index a33764177d..871edb540e 100644
>> --- a/testsuites/libtests/malloctest/init.c
>> +++ b/testsuites/libtests/malloctest/init.c
>> @@ -1362,6 +1362,18 @@ static void test_alloc_zero_size(void)
>>rtems_test_assert( p == NULL );
>>rtems_test_assert( errno == -1 );
>>  }
>> +static void test_usablesize(void)
>>
>
> Blank line above method.
>
>
>> +{
>> +  int * a = malloc(sizeof( int )*100);
>> +  int alloc_size=sizeof( int ) *100 ;
>>
>
> Spaces around =
>
> Inconsistent spaces around "*" between those two lines.
>
>
>> +  rtems_test_assert( malloc_usable_size( a )

Re: [PATCH] added malloc usable size and test

2022-03-15 Thread Joel Sherrill
On Fri, Mar 4, 2022 at 8:58 PM zack leung  wrote:

> closes #4503
> ---
>  cpukit/include/rtems/libcsupport.h|  5 +++
>  cpukit/libcsupport/src/mallocusablesize.c | 48 +++
>  spec/build/cpukit/librtemscpu.yml |  1 +
>  testsuites/libtests/malloctest/init.c | 15 ++-
>  4 files changed, 68 insertions(+), 1 deletion(-)
>  create mode 100644 cpukit/libcsupport/src/mallocusablesize.c
>
> diff --git a/cpukit/include/rtems/libcsupport.h
> b/cpukit/include/rtems/libcsupport.h
> index f4be4cfc9a..5110ab0fbe 100644
> --- a/cpukit/include/rtems/libcsupport.h
> +++ b/cpukit/include/rtems/libcsupport.h
> @@ -74,6 +74,11 @@ extern size_t malloc_free_space(void);
>   */
>  extern int malloc_info(Heap_Information_block *the_info);
>
> +/**
> + * @brief Find the usable size of the block of memory .
> + */
> +extern size_t malloc_usable_size(void *ptr);
> +
>

This should be in newlib's malloc.h to align with how it is done
on Linux. This is an RTEMS specific header file.  I went ahead
and posted the patch for this since it was small:

https://sourceware.org/pipermail/newlib/2022/019430.html


>  /*
>   *  Prototypes required to install newlib reentrancy user extension
>   */
> diff --git a/cpukit/libcsupport/src/mallocusablesize.c
> b/cpukit/libcsupport/src/mallocusablesize.c
> new file mode 100644
> index 00..b7e573023a
> --- /dev/null
> +++ b/cpukit/libcsupport/src/mallocusablesize.c
> @@ -0,0 +1,48 @@
> +/*
> + * SPDX-License-Identifier: BSD-2-Clause
> + *
> + *  Copyright (C) 2022 zacchaeus leung
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions
> + * are met:
> + * 1. Redistributions of source code must retain the above copyright
> + *notice, this list of conditions and the following disclaimer.
> + * 2. Redistributions in binary form must reproduce the above copyright
> + *notice, this list of conditions and the following disclaimer in the
> + *documentation and/or other materials provided with the distribution.
> + *
> + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
> IS"
> + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
> THE
> + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
> PURPOSE
> + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
> BE
> + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
> + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
> + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
> BUSINESS
> + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
> + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
> + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
> THE
> + * POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +#ifdef HAVE_CONFIG_H
> +#include "config.h"
> +#endif
> +
> +#include 
> +#include 
> +#include 
> +
> +size_t malloc_usable_size( void *ptr ) {
> +
> +  Heap_Control *heap_ptr ;
> +  size_t size;
> +  if ( ptr == NULL ) {
>

Blank line above the if. Space between ) and {


> +return 0;
> +  }
> +
> +  heap_ptr = malloc_get_heap_pointer();
> +  _Heap_Size_of_alloc_area( heap_ptr, ptr, &size );
>
>
This should be _Protected_heap_Get_block_size() to ensure that
locking is performed.


> +  return size;
> +}
> diff --git a/spec/build/cpukit/librtemscpu.yml
> b/spec/build/cpukit/librtemscpu.yml
> index c224937348..2ef11e7e2d 100644
> --- a/spec/build/cpukit/librtemscpu.yml
> +++ b/spec/build/cpukit/librtemscpu.yml
> @@ -670,6 +670,7 @@ source:
>  - cpukit/libcsupport/src/lseek.c
>  - cpukit/libcsupport/src/lstat.c
>  - cpukit/libcsupport/src/malloc.c
> +- cpukit/libcsupport/src/mallocusablesize.c
>  - cpukit/libcsupport/src/malloc_deferred.c
>  - cpukit/libcsupport/src/malloc_dirtier.c
>  - cpukit/libcsupport/src/malloc_walk.c
> diff --git a/testsuites/libtests/malloctest/init.c
> b/testsuites/libtests/malloctest/init.c
> index a33764177d..871edb540e 100644
> --- a/testsuites/libtests/malloctest/init.c
> +++ b/testsuites/libtests/malloctest/init.c
> @@ -1362,6 +1362,18 @@ static void test_alloc_zero_size(void)
>rtems_test_assert( p == NULL );
>rtems_test_assert( errno == -1 );
>  }
> +static void test_usablesize(void)
>

Blank line above method.


> +{
> +  int * a = malloc(sizeof( int )*100);
> +  int alloc_size=sizeof( int ) *100 ;
>

Spaces around =

Inconsistent spaces around "*" between those two lines.


> +  rtems_test_assert( malloc_usable_size( a ) <= alloc_size +
> CPU_HEAP_ALIGNMENT );
>

Should also be >= alloc_size and <= alloc_size + HEAP_ALIGNMENT.
Needs to be large enough to use.


> +  free(a);
> +
> +  char * b = malloc(sizeof ( char ) 100);
> +  int alloc_size 2= sizeof ( char ) *100 ;
>

Same

> +  rtems_test_assert( malloc_usable_size ( b ) <= alloc_size2 +
> CPU_HEAP_ALIGNMENT);
>

Re: [PATCH] added malloc usable size and test

2022-03-10 Thread zack leung
Ping

Il ven 4 mar 2022, 21:58 zack leung  ha scritto:

> closes #4503
> ---
>  cpukit/include/rtems/libcsupport.h|  5 +++
>  cpukit/libcsupport/src/mallocusablesize.c | 48 +++
>  spec/build/cpukit/librtemscpu.yml |  1 +
>  testsuites/libtests/malloctest/init.c | 15 ++-
>  4 files changed, 68 insertions(+), 1 deletion(-)
>  create mode 100644 cpukit/libcsupport/src/mallocusablesize.c
>
> diff --git a/cpukit/include/rtems/libcsupport.h
> b/cpukit/include/rtems/libcsupport.h
> index f4be4cfc9a..5110ab0fbe 100644
> --- a/cpukit/include/rtems/libcsupport.h
> +++ b/cpukit/include/rtems/libcsupport.h
> @@ -74,6 +74,11 @@ extern size_t malloc_free_space(void);
>   */
>  extern int malloc_info(Heap_Information_block *the_info);
>
> +/**
> + * @brief Find the usable size of the block of memory .
> + */
> +extern size_t malloc_usable_size(void *ptr);
> +
>  /*
>   *  Prototypes required to install newlib reentrancy user extension
>   */
> diff --git a/cpukit/libcsupport/src/mallocusablesize.c
> b/cpukit/libcsupport/src/mallocusablesize.c
> new file mode 100644
> index 00..b7e573023a
> --- /dev/null
> +++ b/cpukit/libcsupport/src/mallocusablesize.c
> @@ -0,0 +1,48 @@
> +/*
> + * SPDX-License-Identifier: BSD-2-Clause
> + *
> + *  Copyright (C) 2022 zacchaeus leung
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions
> + * are met:
> + * 1. Redistributions of source code must retain the above copyright
> + *notice, this list of conditions and the following disclaimer.
> + * 2. Redistributions in binary form must reproduce the above copyright
> + *notice, this list of conditions and the following disclaimer in the
> + *documentation and/or other materials provided with the distribution.
> + *
> + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
> "AS IS"
> + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
> THE
> + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
> PURPOSE
> + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
> BE
> + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
> + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
> + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
> BUSINESS
> + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
> + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
> + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
> THE
> + * POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +#ifdef HAVE_CONFIG_H
> +#include "config.h"
> +#endif
> +
> +#include 
> +#include 
> +#include 
> +
> +size_t malloc_usable_size( void *ptr ) {
> +
> +  Heap_Control *heap_ptr ;
> +  size_t size;
> +  if ( ptr == NULL ) {
> +return 0;
> +  }
> +
> +  heap_ptr = malloc_get_heap_pointer();
> +  _Heap_Size_of_alloc_area( heap_ptr, ptr, &size );
> +
> +  return size;
> +}
> diff --git a/spec/build/cpukit/librtemscpu.yml
> b/spec/build/cpukit/librtemscpu.yml
> index c224937348..2ef11e7e2d 100644
> --- a/spec/build/cpukit/librtemscpu.yml
> +++ b/spec/build/cpukit/librtemscpu.yml
> @@ -670,6 +670,7 @@ source:
>  - cpukit/libcsupport/src/lseek.c
>  - cpukit/libcsupport/src/lstat.c
>  - cpukit/libcsupport/src/malloc.c
> +- cpukit/libcsupport/src/mallocusablesize.c
>  - cpukit/libcsupport/src/malloc_deferred.c
>  - cpukit/libcsupport/src/malloc_dirtier.c
>  - cpukit/libcsupport/src/malloc_walk.c
> diff --git a/testsuites/libtests/malloctest/init.c
> b/testsuites/libtests/malloctest/init.c
> index a33764177d..871edb540e 100644
> --- a/testsuites/libtests/malloctest/init.c
> +++ b/testsuites/libtests/malloctest/init.c
> @@ -1362,6 +1362,18 @@ static void test_alloc_zero_size(void)
>rtems_test_assert( p == NULL );
>rtems_test_assert( errno == -1 );
>  }
> +static void test_usablesize(void)
> +{
> +  int * a = malloc(sizeof( int )*100);
> +  int alloc_size=sizeof( int ) *100 ;
> +  rtems_test_assert( malloc_usable_size( a ) <= alloc_size +
> CPU_HEAP_ALIGNMENT );
> +  free(a);
> +
> +  char * b = malloc(sizeof ( char ) 100);
> +  int alloc_size 2= sizeof ( char ) *100 ;
> +  rtems_test_assert( malloc_usable_size ( b ) <= alloc_size2 +
> CPU_HEAP_ALIGNMENT);
> +  free( b );
> +}
>
>  rtems_task Init(
>rtems_task_argument argument
> @@ -1405,6 +1417,7 @@ rtems_task Init(
>test_protected_heap_info();
>test_rtems_heap_allocate_aligned_with_boundary();
>test_rtems_malloc();
> +  test_usablesize();
>test_rtems_calloc();
>test_greedy_allocate();
>test_alloc_zero_size();
> @@ -1524,4 +1537,4 @@ RTEMS_SYSINIT_ITEM(
>test_early_malloc,
>RTEMS_SYSINIT_INITIAL_EXTENSIONS,
>RTEMS_SYSINIT_ORDER_FIRST
> -);
> +);
> \ No newline at end of file
> --
> 2.35.1
>
___
devel mailin

[PATCH] added malloc usable size and test

2022-03-04 Thread zack leung
closes #4503
---
 cpukit/include/rtems/libcsupport.h|  5 +++
 cpukit/libcsupport/src/mallocusablesize.c | 48 +++
 spec/build/cpukit/librtemscpu.yml |  1 +
 testsuites/libtests/malloctest/init.c | 15 ++-
 4 files changed, 68 insertions(+), 1 deletion(-)
 create mode 100644 cpukit/libcsupport/src/mallocusablesize.c

diff --git a/cpukit/include/rtems/libcsupport.h
b/cpukit/include/rtems/libcsupport.h
index f4be4cfc9a..5110ab0fbe 100644
--- a/cpukit/include/rtems/libcsupport.h
+++ b/cpukit/include/rtems/libcsupport.h
@@ -74,6 +74,11 @@ extern size_t malloc_free_space(void);
  */
 extern int malloc_info(Heap_Information_block *the_info);

+/**
+ * @brief Find the usable size of the block of memory .
+ */
+extern size_t malloc_usable_size(void *ptr);
+
 /*
  *  Prototypes required to install newlib reentrancy user extension
  */
diff --git a/cpukit/libcsupport/src/mallocusablesize.c
b/cpukit/libcsupport/src/mallocusablesize.c
new file mode 100644
index 00..b7e573023a
--- /dev/null
+++ b/cpukit/libcsupport/src/mallocusablesize.c
@@ -0,0 +1,48 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ *  Copyright (C) 2022 zacchaeus leung
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *notice, this list of conditions and the following disclaimer in the
+ *documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include 
+#include 
+#include 
+
+size_t malloc_usable_size( void *ptr ) {
+
+  Heap_Control *heap_ptr ;
+  size_t size;
+  if ( ptr == NULL ) {
+return 0;
+  }
+
+  heap_ptr = malloc_get_heap_pointer();
+  _Heap_Size_of_alloc_area( heap_ptr, ptr, &size );
+
+  return size;
+}
diff --git a/spec/build/cpukit/librtemscpu.yml
b/spec/build/cpukit/librtemscpu.yml
index c224937348..2ef11e7e2d 100644
--- a/spec/build/cpukit/librtemscpu.yml
+++ b/spec/build/cpukit/librtemscpu.yml
@@ -670,6 +670,7 @@ source:
 - cpukit/libcsupport/src/lseek.c
 - cpukit/libcsupport/src/lstat.c
 - cpukit/libcsupport/src/malloc.c
+- cpukit/libcsupport/src/mallocusablesize.c
 - cpukit/libcsupport/src/malloc_deferred.c
 - cpukit/libcsupport/src/malloc_dirtier.c
 - cpukit/libcsupport/src/malloc_walk.c
diff --git a/testsuites/libtests/malloctest/init.c
b/testsuites/libtests/malloctest/init.c
index a33764177d..871edb540e 100644
--- a/testsuites/libtests/malloctest/init.c
+++ b/testsuites/libtests/malloctest/init.c
@@ -1362,6 +1362,18 @@ static void test_alloc_zero_size(void)
   rtems_test_assert( p == NULL );
   rtems_test_assert( errno == -1 );
 }
+static void test_usablesize(void)
+{
+  int * a = malloc(sizeof( int )*100);
+  int alloc_size=sizeof( int ) *100 ;
+  rtems_test_assert( malloc_usable_size( a ) <= alloc_size +
CPU_HEAP_ALIGNMENT );
+  free(a);
+
+  char * b = malloc(sizeof ( char ) 100);
+  int alloc_size 2= sizeof ( char ) *100 ;
+  rtems_test_assert( malloc_usable_size ( b ) <= alloc_size2 +
CPU_HEAP_ALIGNMENT);
+  free( b );
+}

 rtems_task Init(
   rtems_task_argument argument
@@ -1405,6 +1417,7 @@ rtems_task Init(
   test_protected_heap_info();
   test_rtems_heap_allocate_aligned_with_boundary();
   test_rtems_malloc();
+  test_usablesize();
   test_rtems_calloc();
   test_greedy_allocate();
   test_alloc_zero_size();
@@ -1524,4 +1537,4 @@ RTEMS_SYSINIT_ITEM(
   test_early_malloc,
   RTEMS_SYSINIT_INITIAL_EXTENSIONS,
   RTEMS_SYSINIT_ORDER_FIRST
-);
+);
\ No newline at end of file
-- 
2.35.1
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


Re: [PATCH] added malloc usable size and test

2022-03-03 Thread Karel Gardas



Zack,

not in the position of reviewing your code, but you should probably fill 
the template markers (,  ) in 
the license comment below.


Thanks,
Karel

On 3/4/22 03:49, zack leung wrote:

+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (C) ,  

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


[PATCH] added malloc usable size and test

2022-03-03 Thread zack leung
---
 cpukit/include/rtems/libcsupport.h|  5 +++
 cpukit/libcsupport/src/mallocusablesize.c | 48 +++
 spec/build/cpukit/librtemscpu.yml |  1 +
 testsuites/libtests/malloctest/init.c | 15 ++-
 4 files changed, 68 insertions(+), 1 deletion(-)
 create mode 100644 cpukit/libcsupport/src/mallocusablesize.c

diff --git a/cpukit/include/rtems/libcsupport.h 
b/cpukit/include/rtems/libcsupport.h
index f4be4cfc9a..5110ab0fbe 100644
--- a/cpukit/include/rtems/libcsupport.h
+++ b/cpukit/include/rtems/libcsupport.h
@@ -74,6 +74,11 @@ extern size_t malloc_free_space(void);
  */
 extern int malloc_info(Heap_Information_block *the_info);
 
+/**
+ * @brief Find the usable size of the block of memory .
+ */
+extern size_t malloc_usable_size(void *ptr);
+
 /*
  *  Prototypes required to install newlib reentrancy user extension
  */
diff --git a/cpukit/libcsupport/src/mallocusablesize.c 
b/cpukit/libcsupport/src/mallocusablesize.c
new file mode 100644
index 00..b7e573023a
--- /dev/null
+++ b/cpukit/libcsupport/src/mallocusablesize.c
@@ -0,0 +1,48 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (C) ,  
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *notice, this list of conditions and the following disclaimer in the
+ *documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+ 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include 
+#include 
+#include 
+
+size_t malloc_usable_size( void *ptr ) {
+  
+  Heap_Control *heap_ptr ;
+  size_t size;
+  if ( ptr == NULL ) {
+return 0;
+  }
+  
+  heap_ptr = malloc_get_heap_pointer();
+  _Heap_Size_of_alloc_area( heap_ptr, ptr, &size );
+
+  return size;
+}
diff --git a/spec/build/cpukit/librtemscpu.yml 
b/spec/build/cpukit/librtemscpu.yml
index c224937348..2ef11e7e2d 100644
--- a/spec/build/cpukit/librtemscpu.yml
+++ b/spec/build/cpukit/librtemscpu.yml
@@ -670,6 +670,7 @@ source:
 - cpukit/libcsupport/src/lseek.c
 - cpukit/libcsupport/src/lstat.c
 - cpukit/libcsupport/src/malloc.c
+- cpukit/libcsupport/src/mallocusablesize.c 
 - cpukit/libcsupport/src/malloc_deferred.c
 - cpukit/libcsupport/src/malloc_dirtier.c
 - cpukit/libcsupport/src/malloc_walk.c
diff --git a/testsuites/libtests/malloctest/init.c 
b/testsuites/libtests/malloctest/init.c
index a33764177d..871edb540e 100644
--- a/testsuites/libtests/malloctest/init.c
+++ b/testsuites/libtests/malloctest/init.c
@@ -1362,6 +1362,18 @@ static void test_alloc_zero_size(void)
   rtems_test_assert( p == NULL );
   rtems_test_assert( errno == -1 );
 }
+static void test_usablesize(void)
+{
+  int * a = malloc(sizeof( int )*100);
+  int alloc_size=sizeof( int ) *100 ;
+  rtems_test_assert( malloc_usable_size( a ) <= alloc_size + 
CPU_HEAP_ALIGNMENT );
+  free(a);
+
+  char * b = malloc(sizeof ( char ) 100);
+  int alloc_size 2= sizeof ( char ) *100 ; 
+  rtems_test_assert( malloc_usable_size ( b ) <= alloc_size2 + 
CPU_HEAP_ALIGNMENT);
+  free( b );
+}
 
 rtems_task Init(
   rtems_task_argument argument
@@ -1405,6 +1417,7 @@ rtems_task Init(
   test_protected_heap_info();
   test_rtems_heap_allocate_aligned_with_boundary();
   test_rtems_malloc();
+  test_usablesize();
   test_rtems_calloc();
   test_greedy_allocate();
   test_alloc_zero_size();
@@ -1524,4 +1537,4 @@ RTEMS_SYSINIT_ITEM(
   test_early_malloc,
   RTEMS_SYSINIT_INITIAL_EXTENSIONS,
   RTEMS_SYSINIT_ORDER_FIRST
-);
+);
\ No newline at end of file
-- 
2.35.1

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


Re: [PATCH] added malloc usable size and test

2022-02-28 Thread Gedare Bloom
On Sat, Feb 19, 2022 at 7:55 PM zack leung  wrote:
>
> diff --git a/cpukit/include/rtems/libcsupport.h 
> b/cpukit/include/rtems/libcsupport.h
> index f4be4cfc9a..ec385bb71a 100644
> --- a/cpukit/include/rtems/libcsupport.h
> +++ b/cpukit/include/rtems/libcsupport.h
> @@ -73,7 +73,10 @@ extern size_t malloc_free_space(void);
>   * Find amount of free heap remaining.
>   */
>  extern int malloc_info(Heap_Information_block *the_info);
> -
Keep blank line between function declarations

> +/**
> + * @brief Find the usable size of the block of memory.
> + */
> +extern size_t malloc_usable_size( void *ptr );
Add blank line between function declarations. I think I've said these
before. Please fix/address all comments from prior reviews.

>  /*
>   *  Prototypes required to install newlib reentrancy user extension
>   */
> @@ -185,6 +188,7 @@ bool rtems_resource_snapshot_equal(
>   */
>  bool rtems_resource_snapshot_check(const rtems_resource_snapshot *snapshot);
>
> +
Don't add random whitespace changes.

>  /** @} */
>
>  #ifdef __cplusplus
> diff --git a/cpukit/libcsupport/src/mallocusablesize.c 
> b/cpukit/libcsupport/src/mallocusablesize.c
> new file mode 100644
> index 00..d9211b1390
> --- /dev/null
> +++ b/cpukit/libcsupport/src/mallocusablesize.c
> @@ -0,0 +1,51 @@
> +/*
> + * SPDX-License-Identifier: BSD-2-Clause
> + *
> + * Copyright (C) 2022 Zacchaeus Leung
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions
> + * are met:
> + * 1. Redistributions of source code must retain the above copyright
> + *notice, this list of conditions and the following disclaimer.
> + * 2. Redistributions in binary form must reproduce the above copyright
> + *notice, this list of conditions and the following disclaimer in the
> + *documentation and/or other materials provided with the distribution.
> + *
> + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
> IS"
> + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
> + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
> + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
> + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
> + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
> + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
> + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
> + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
> + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
> + * POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +#ifdef HAVE_CONFIG_H
> +#include "config.h"
> +#endif
> +
> +#include 
> +#include 
> +#include 
> +
> +size_t malloc_usable_size(void *ptr) {
> +  Heap_Control *heap_ptr ;
> +  size_t size;
Consider having a blank line between variable decls and start of code.
Not mandatory, but helpful.

> +  if (ptr == NULL) {
> +return 0;
> +  }
> +
> +
Avoid multiple blank lines in a row.

> +  heap_ptr = malloc_get_heap_pointer();
> +  bool rv=_Heap_Size_of_alloc_area(heap_ptr, ptr, &size);
spaces around =
> +  if( rv==false ){
spaces around ==
space after if
space after )

> +return 0;
> +  }
> +
> +  return size;
> +}
> \ No newline at end of file
line break after } but not a blank line

> diff --git a/spec/build/cpukit/librtemscpu.yml 
> b/spec/build/cpukit/librtemscpu.yml
> index 7d6dbae0db..5902008c94 100644
> --- a/spec/build/cpukit/librtemscpu.yml
> +++ b/spec/build/cpukit/librtemscpu.yml
> @@ -670,6 +670,7 @@ source:
>  - cpukit/libcsupport/src/lseek.c
>  - cpukit/libcsupport/src/lstat.c
>  - cpukit/libcsupport/src/malloc.c
> +- cpukit/libcsupport/src/mallocusablesize.c
>  - cpukit/libcsupport/src/malloc_deferred.c
>  - cpukit/libcsupport/src/malloc_dirtier.c
>  - cpukit/libcsupport/src/malloc_walk.c
> diff --git a/testsuites/libtests/malloctest/init.c 
> b/testsuites/libtests/malloctest/init.c
> index a33764177d..38f5554440 100644
> --- a/testsuites/libtests/malloctest/init.c
> +++ b/testsuites/libtests/malloctest/init.c
> @@ -1362,6 +1362,18 @@ static void test_alloc_zero_size(void)
>rtems_test_assert( p == NULL );
>rtems_test_assert( errno == -1 );
>  }
> +static void test_usablesize(void)
> +{
> +  int * a = malloc(sizeof( int )*100);
> +  int alloc_size=sizeof( int ) *100 ;
> +  rtems_test_assert(  malloc_usable_size(a) <= alloc_size + 
> CPU_HEAP_ALIGNMENT);
> +  free(a);
> +
> +  char * b = malloc(sizeof( char )* 100);
> +  int alloc_size2=sizeof( char ) * 100;
> +  rtems_test_assert( malloc_usable_size ( b ) <= alloc_size2 + 
> CPU_HEAP_ALIGNMENT);
> +  free(b);
> +}
>
>  rtems_task Init(
>rtems_task_argument argument
> @@ -1405,6 +1417,7 @@ rtems_task Init(
>test_protected_heap_info();
>test_rtems_heap_allocate_aligned_with_boundary();
>test_rtems_

Re: [PATCH] added malloc usable size and test

2022-02-25 Thread zack leung
ping


On Sat, 26 Feb 2022 at 02:42, zack leung  wrote:

> ping
> *
>
> On Sun, 20 Feb 2022 at 02:55, zack leung  wrote:
>
>> diff --git a/cpukit/include/rtems/libcsupport.h
>> b/cpukit/include/rtems/libcsupport.h
>> index f4be4cfc9a..ec385bb71a 100644
>> --- a/cpukit/include/rtems/libcsupport.h
>> +++ b/cpukit/include/rtems/libcsupport.h
>> @@ -73,7 +73,10 @@ extern size_t malloc_free_space(void);
>>   * Find amount of free heap remaining.
>>   */
>>  extern int malloc_info(Heap_Information_block *the_info);
>> -
>> +/**
>> + * @brief Find the usable size of the block of memory.
>> + */
>> +extern size_t malloc_usable_size( void *ptr );
>>  /*
>>   *  Prototypes required to install newlib reentrancy user extension
>>   */
>> @@ -185,6 +188,7 @@ bool rtems_resource_snapshot_equal(
>>   */
>>  bool rtems_resource_snapshot_check(const rtems_resource_snapshot
>> *snapshot);
>>
>> +
>>  /** @} */
>>
>>  #ifdef __cplusplus
>> diff --git a/cpukit/libcsupport/src/mallocusablesize.c
>> b/cpukit/libcsupport/src/mallocusablesize.c
>> new file mode 100644
>> index 00..d9211b1390
>> --- /dev/null
>> +++ b/cpukit/libcsupport/src/mallocusablesize.c
>> @@ -0,0 +1,51 @@
>> +/*
>> + * SPDX-License-Identifier: BSD-2-Clause
>> + *
>> + * Copyright (C) 2022 Zacchaeus Leung
>> + *
>> + * Redistribution and use in source and binary forms, with or without
>> + * modification, are permitted provided that the following conditions
>> + * are met:
>> + * 1. Redistributions of source code must retain the above copyright
>> + *notice, this list of conditions and the following disclaimer.
>> + * 2. Redistributions in binary form must reproduce the above copyright
>> + *notice, this list of conditions and the following disclaimer in the
>> + *documentation and/or other materials provided with the
>> distribution.
>> + *
>> + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
>> "AS IS"
>> + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
>> THE
>> + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
>> PURPOSE
>> + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
>> BE
>> + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
>> + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
>> + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
>> BUSINESS
>> + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
>> IN
>> + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
>> OTHERWISE)
>> + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
>> OF THE
>> + * POSSIBILITY OF SUCH DAMAGE.
>> + */
>> +
>> +#ifdef HAVE_CONFIG_H
>> +#include "config.h"
>> +#endif
>> +
>> +#include 
>> +#include 
>> +#include 
>> +
>> +size_t malloc_usable_size(void *ptr) {
>> +  Heap_Control *heap_ptr ;
>> +  size_t size;
>> +  if (ptr == NULL) {
>> +return 0;
>> +  }
>> +
>> +
>> +  heap_ptr = malloc_get_heap_pointer();
>> +  bool rv=_Heap_Size_of_alloc_area(heap_ptr, ptr, &size);
>> +  if( rv==false ){
>> +return 0;
>> +  }
>> +
>> +  return size;
>> +}
>> \ No newline at end of file
>> diff --git a/spec/build/cpukit/librtemscpu.yml
>> b/spec/build/cpukit/librtemscpu.yml
>> index 7d6dbae0db..5902008c94 100644
>> --- a/spec/build/cpukit/librtemscpu.yml
>> +++ b/spec/build/cpukit/librtemscpu.yml
>> @@ -670,6 +670,7 @@ source:
>>  - cpukit/libcsupport/src/lseek.c
>>  - cpukit/libcsupport/src/lstat.c
>>  - cpukit/libcsupport/src/malloc.c
>> +- cpukit/libcsupport/src/mallocusablesize.c
>>  - cpukit/libcsupport/src/malloc_deferred.c
>>  - cpukit/libcsupport/src/malloc_dirtier.c
>>  - cpukit/libcsupport/src/malloc_walk.c
>> diff --git a/testsuites/libtests/malloctest/init.c
>> b/testsuites/libtests/malloctest/init.c
>> index a33764177d..38f5554440 100644
>> --- a/testsuites/libtests/malloctest/init.c
>> +++ b/testsuites/libtests/malloctest/init.c
>> @@ -1362,6 +1362,18 @@ static void test_alloc_zero_size(void)
>>rtems_test_assert( p == NULL );
>>rtems_test_assert( errno == -1 );
>>  }
>> +static void test_usablesize(void)
>> +{
>> +  int * a = malloc(sizeof( int )*100);
>> +  int alloc_size=sizeof( int ) *100 ;
>> +  rtems_test_assert(  malloc_usable_size(a) <= alloc_size +
>> CPU_HEAP_ALIGNMENT);
>> +  free(a);
>> +
>> +  char * b = malloc(sizeof( char )* 100);
>> +  int alloc_size2=sizeof( char ) * 100;
>> +  rtems_test_assert( malloc_usable_size ( b ) <= alloc_size2 +
>> CPU_HEAP_ALIGNMENT);
>> +  free(b);
>> +}
>>
>>  rtems_task Init(
>>rtems_task_argument argument
>> @@ -1405,6 +1417,7 @@ rtems_task Init(
>>test_protected_heap_info();
>>test_rtems_heap_allocate_aligned_with_boundary();
>>test_rtems_malloc();
>> +  test_usablesize();
>>test_rtems_calloc();
>>test_greedy_allocate();
>>test_alloc_zero_size();
>> --
>> 2.35.1
>>
>>
___
devel mailing list
devel@rtems.org
http://lists.

Re: [PATCH] added malloc usable size and test

2022-02-25 Thread zack leung
ping
*

On Sun, 20 Feb 2022 at 02:55, zack leung  wrote:

> diff --git a/cpukit/include/rtems/libcsupport.h
> b/cpukit/include/rtems/libcsupport.h
> index f4be4cfc9a..ec385bb71a 100644
> --- a/cpukit/include/rtems/libcsupport.h
> +++ b/cpukit/include/rtems/libcsupport.h
> @@ -73,7 +73,10 @@ extern size_t malloc_free_space(void);
>   * Find amount of free heap remaining.
>   */
>  extern int malloc_info(Heap_Information_block *the_info);
> -
> +/**
> + * @brief Find the usable size of the block of memory.
> + */
> +extern size_t malloc_usable_size( void *ptr );
>  /*
>   *  Prototypes required to install newlib reentrancy user extension
>   */
> @@ -185,6 +188,7 @@ bool rtems_resource_snapshot_equal(
>   */
>  bool rtems_resource_snapshot_check(const rtems_resource_snapshot
> *snapshot);
>
> +
>  /** @} */
>
>  #ifdef __cplusplus
> diff --git a/cpukit/libcsupport/src/mallocusablesize.c
> b/cpukit/libcsupport/src/mallocusablesize.c
> new file mode 100644
> index 00..d9211b1390
> --- /dev/null
> +++ b/cpukit/libcsupport/src/mallocusablesize.c
> @@ -0,0 +1,51 @@
> +/*
> + * SPDX-License-Identifier: BSD-2-Clause
> + *
> + * Copyright (C) 2022 Zacchaeus Leung
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions
> + * are met:
> + * 1. Redistributions of source code must retain the above copyright
> + *notice, this list of conditions and the following disclaimer.
> + * 2. Redistributions in binary form must reproduce the above copyright
> + *notice, this list of conditions and the following disclaimer in the
> + *documentation and/or other materials provided with the distribution.
> + *
> + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
> "AS IS"
> + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
> THE
> + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
> PURPOSE
> + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
> BE
> + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
> + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
> + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
> BUSINESS
> + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
> + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
> + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
> THE
> + * POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +#ifdef HAVE_CONFIG_H
> +#include "config.h"
> +#endif
> +
> +#include 
> +#include 
> +#include 
> +
> +size_t malloc_usable_size(void *ptr) {
> +  Heap_Control *heap_ptr ;
> +  size_t size;
> +  if (ptr == NULL) {
> +return 0;
> +  }
> +
> +
> +  heap_ptr = malloc_get_heap_pointer();
> +  bool rv=_Heap_Size_of_alloc_area(heap_ptr, ptr, &size);
> +  if( rv==false ){
> +return 0;
> +  }
> +
> +  return size;
> +}
> \ No newline at end of file
> diff --git a/spec/build/cpukit/librtemscpu.yml
> b/spec/build/cpukit/librtemscpu.yml
> index 7d6dbae0db..5902008c94 100644
> --- a/spec/build/cpukit/librtemscpu.yml
> +++ b/spec/build/cpukit/librtemscpu.yml
> @@ -670,6 +670,7 @@ source:
>  - cpukit/libcsupport/src/lseek.c
>  - cpukit/libcsupport/src/lstat.c
>  - cpukit/libcsupport/src/malloc.c
> +- cpukit/libcsupport/src/mallocusablesize.c
>  - cpukit/libcsupport/src/malloc_deferred.c
>  - cpukit/libcsupport/src/malloc_dirtier.c
>  - cpukit/libcsupport/src/malloc_walk.c
> diff --git a/testsuites/libtests/malloctest/init.c
> b/testsuites/libtests/malloctest/init.c
> index a33764177d..38f5554440 100644
> --- a/testsuites/libtests/malloctest/init.c
> +++ b/testsuites/libtests/malloctest/init.c
> @@ -1362,6 +1362,18 @@ static void test_alloc_zero_size(void)
>rtems_test_assert( p == NULL );
>rtems_test_assert( errno == -1 );
>  }
> +static void test_usablesize(void)
> +{
> +  int * a = malloc(sizeof( int )*100);
> +  int alloc_size=sizeof( int ) *100 ;
> +  rtems_test_assert(  malloc_usable_size(a) <= alloc_size +
> CPU_HEAP_ALIGNMENT);
> +  free(a);
> +
> +  char * b = malloc(sizeof( char )* 100);
> +  int alloc_size2=sizeof( char ) * 100;
> +  rtems_test_assert( malloc_usable_size ( b ) <= alloc_size2 +
> CPU_HEAP_ALIGNMENT);
> +  free(b);
> +}
>
>  rtems_task Init(
>rtems_task_argument argument
> @@ -1405,6 +1417,7 @@ rtems_task Init(
>test_protected_heap_info();
>test_rtems_heap_allocate_aligned_with_boundary();
>test_rtems_malloc();
> +  test_usablesize();
>test_rtems_calloc();
>test_greedy_allocate();
>test_alloc_zero_size();
> --
> 2.35.1
>
>
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


[PATCH] added malloc usable size and test

2022-02-19 Thread zack leung
diff --git a/cpukit/include/rtems/libcsupport.h 
b/cpukit/include/rtems/libcsupport.h
index f4be4cfc9a..ec385bb71a 100644
--- a/cpukit/include/rtems/libcsupport.h
+++ b/cpukit/include/rtems/libcsupport.h
@@ -73,7 +73,10 @@ extern size_t malloc_free_space(void);
  * Find amount of free heap remaining.
  */
 extern int malloc_info(Heap_Information_block *the_info);
-
+/**
+ * @brief Find the usable size of the block of memory.
+ */
+extern size_t malloc_usable_size( void *ptr );
 /*
  *  Prototypes required to install newlib reentrancy user extension
  */
@@ -185,6 +188,7 @@ bool rtems_resource_snapshot_equal(
  */
 bool rtems_resource_snapshot_check(const rtems_resource_snapshot *snapshot);
 
+
 /** @} */
 
 #ifdef __cplusplus
diff --git a/cpukit/libcsupport/src/mallocusablesize.c 
b/cpukit/libcsupport/src/mallocusablesize.c
new file mode 100644
index 00..d9211b1390
--- /dev/null
+++ b/cpukit/libcsupport/src/mallocusablesize.c
@@ -0,0 +1,51 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (C) 2022 Zacchaeus Leung
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *notice, this list of conditions and the following disclaimer in the
+ *documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+ 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include 
+#include 
+#include 
+
+size_t malloc_usable_size(void *ptr) {
+  Heap_Control *heap_ptr ;
+  size_t size;
+  if (ptr == NULL) {
+return 0;
+  }
+
+  
+  heap_ptr = malloc_get_heap_pointer();
+  bool rv=_Heap_Size_of_alloc_area(heap_ptr, ptr, &size);
+  if( rv==false ){
+return 0;
+  }
+
+  return size;
+}
\ No newline at end of file
diff --git a/spec/build/cpukit/librtemscpu.yml 
b/spec/build/cpukit/librtemscpu.yml
index 7d6dbae0db..5902008c94 100644
--- a/spec/build/cpukit/librtemscpu.yml
+++ b/spec/build/cpukit/librtemscpu.yml
@@ -670,6 +670,7 @@ source:
 - cpukit/libcsupport/src/lseek.c
 - cpukit/libcsupport/src/lstat.c
 - cpukit/libcsupport/src/malloc.c
+- cpukit/libcsupport/src/mallocusablesize.c 
 - cpukit/libcsupport/src/malloc_deferred.c
 - cpukit/libcsupport/src/malloc_dirtier.c
 - cpukit/libcsupport/src/malloc_walk.c
diff --git a/testsuites/libtests/malloctest/init.c 
b/testsuites/libtests/malloctest/init.c
index a33764177d..38f5554440 100644
--- a/testsuites/libtests/malloctest/init.c
+++ b/testsuites/libtests/malloctest/init.c
@@ -1362,6 +1362,18 @@ static void test_alloc_zero_size(void)
   rtems_test_assert( p == NULL );
   rtems_test_assert( errno == -1 );
 }
+static void test_usablesize(void)
+{
+  int * a = malloc(sizeof( int )*100);
+  int alloc_size=sizeof( int ) *100 ;
+  rtems_test_assert(  malloc_usable_size(a) <= alloc_size + 
CPU_HEAP_ALIGNMENT);
+  free(a);
+
+  char * b = malloc(sizeof( char )* 100);
+  int alloc_size2=sizeof( char ) * 100; 
+  rtems_test_assert( malloc_usable_size ( b ) <= alloc_size2 + 
CPU_HEAP_ALIGNMENT);
+  free(b);
+}
 
 rtems_task Init(
   rtems_task_argument argument
@@ -1405,6 +1417,7 @@ rtems_task Init(
   test_protected_heap_info();
   test_rtems_heap_allocate_aligned_with_boundary();
   test_rtems_malloc();
+  test_usablesize();
   test_rtems_calloc();
   test_greedy_allocate();
   test_alloc_zero_size();
-- 
2.35.1

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


Re: [PATCH] added malloc usable size and test

2022-02-18 Thread Gedare Bloom
On Thu, Feb 17, 2022 at 8:14 PM zack leung  wrote:
>
> ---
>  cpukit/include/rtems/libcsupport.h|  5 ++-
>  cpukit/libcsupport/src/mallocusablesize.c | 47 +++
>  spec/build/cpukit/librtemscpu.yml |  1 +
>  testsuites/libtests/malloctest/init.c | 13 +++
>  4 files changed, 65 insertions(+), 1 deletion(-)
>  create mode 100644 cpukit/libcsupport/src/mallocusablesize.c
>
> diff --git a/cpukit/include/rtems/libcsupport.h 
> b/cpukit/include/rtems/libcsupport.h
> index f4be4cfc9a..5609c5bb94 100644
> --- a/cpukit/include/rtems/libcsupport.h
> +++ b/cpukit/include/rtems/libcsupport.h
> @@ -73,7 +73,10 @@ extern size_t malloc_free_space(void);
>   * Find amount of free heap remaining.
>   */
>  extern int malloc_info(Heap_Information_block *the_info);
> -
Keep the blank line

> +/**
> + * @brief Find the usable size of the block of memory .
Still a space before the period

> + */
> +extern size_t malloc_usable_size(void *ptr);
add a space

>  /*
>   *  Prototypes required to install newlib reentrancy user extension
>   */
> diff --git a/cpukit/libcsupport/src/mallocusablesize.c 
> b/cpukit/libcsupport/src/mallocusablesize.c
> new file mode 100644
> index 00..8a8d6467ad
> --- /dev/null
> +++ b/cpukit/libcsupport/src/mallocusablesize.c
> @@ -0,0 +1,47 @@
> +/*
> + * SPDX-License-Identifier: BSD-2-Clause
> + *
> + * Copyright (C) ,  
fill in the template

> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions
> + * are met:
> + * 1. Redistributions of source code must retain the above copyright
> + *notice, this list of conditions and the following disclaimer.
> + * 2. Redistributions in binary form must reproduce the above copyright
> + *notice, this list of conditions and the following disclaimer in the
> + *documentation and/or other materials provided with the distribution.
> + *
> + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
> IS"
> + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
> + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
> + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
> + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
> + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
> + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
> + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
> + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
> + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
> + * POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +#ifdef HAVE_CONFIG_H
> +#include "config.h"
> +#endif
> +
> +#include 
> +#include 
> +#include 
> +
> +size_t malloc_usable_size(void *ptr) {
> +  Heap_Control *heap_ptr ;
> +  size_t size;
> +  if (ptr == NULL) {
> +return 0;
> +  }
> +
> +  heap_ptr = malloc_get_heap_pointer();
> +  _Heap_Size_of_alloc_area(heap_ptr, ptr, &size);
check return value

> +
> +  return size;
> +}
> diff --git a/spec/build/cpukit/librtemscpu.yml 
> b/spec/build/cpukit/librtemscpu.yml
> index 7d6dbae0db..5902008c94 100644
> --- a/spec/build/cpukit/librtemscpu.yml
> +++ b/spec/build/cpukit/librtemscpu.yml
> @@ -670,6 +670,7 @@ source:
>  - cpukit/libcsupport/src/lseek.c
>  - cpukit/libcsupport/src/lstat.c
>  - cpukit/libcsupport/src/malloc.c
> +- cpukit/libcsupport/src/mallocusablesize.c
>  - cpukit/libcsupport/src/malloc_deferred.c
>  - cpukit/libcsupport/src/malloc_dirtier.c
>  - cpukit/libcsupport/src/malloc_walk.c
> diff --git a/testsuites/libtests/malloctest/init.c 
> b/testsuites/libtests/malloctest/init.c
> index a33764177d..ee6a670372 100644
> --- a/testsuites/libtests/malloctest/init.c
> +++ b/testsuites/libtests/malloctest/init.c
> @@ -1362,6 +1362,18 @@ static void test_alloc_zero_size(void)
>rtems_test_assert( p == NULL );
>rtems_test_assert( errno == -1 );
>  }
> +static void test_usablesize(void)
> +{
> +  int * a = malloc(sizeof( int )*100);
> +  int alloc_size=sizeof( int ) *100 ;
> +  rtems_test_assert(  malloc_usable_size(a) <= alloc_size + 
> CPU_HEAP_ALIGNMENT);
> +  free(a);
> +
> +  char * b = malloc(sizeof(char)*100);
> +  int alloc_size2=sizeof(char) *100 ;
> +  rtems_test_assert( malloc_usable_size ( b ) <= alloc_size2 + 
> CPU_HEAP_ALIGNMENT);
> +  free(b);
> +}
>
>  rtems_task Init(
>rtems_task_argument argument
> @@ -1405,6 +1417,7 @@ rtems_task Init(
>test_protected_heap_info();
>test_rtems_heap_allocate_aligned_with_boundary();
>test_rtems_malloc();
> +  test_usablesize();
>test_rtems_calloc();
>test_greedy_allocate();
>test_alloc_zero_size();
> --
> 2.35.1
>
> ___
> devel mailing list
> devel@rtems.org
> http://lists.rtems.org/mailman/listinfo/devel
___

[PATCH] added malloc usable size and test

2022-02-17 Thread zack leung
---
 cpukit/include/rtems/libcsupport.h|  5 ++-
 cpukit/libcsupport/src/mallocusablesize.c | 47 +++
 spec/build/cpukit/librtemscpu.yml |  1 +
 testsuites/libtests/malloctest/init.c | 13 +++
 4 files changed, 65 insertions(+), 1 deletion(-)
 create mode 100644 cpukit/libcsupport/src/mallocusablesize.c

diff --git a/cpukit/include/rtems/libcsupport.h 
b/cpukit/include/rtems/libcsupport.h
index f4be4cfc9a..5609c5bb94 100644
--- a/cpukit/include/rtems/libcsupport.h
+++ b/cpukit/include/rtems/libcsupport.h
@@ -73,7 +73,10 @@ extern size_t malloc_free_space(void);
  * Find amount of free heap remaining.
  */
 extern int malloc_info(Heap_Information_block *the_info);
-
+/**
+ * @brief Find the usable size of the block of memory .
+ */
+extern size_t malloc_usable_size(void *ptr);
 /*
  *  Prototypes required to install newlib reentrancy user extension
  */
diff --git a/cpukit/libcsupport/src/mallocusablesize.c 
b/cpukit/libcsupport/src/mallocusablesize.c
new file mode 100644
index 00..8a8d6467ad
--- /dev/null
+++ b/cpukit/libcsupport/src/mallocusablesize.c
@@ -0,0 +1,47 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (C) ,  
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *notice, this list of conditions and the following disclaimer in the
+ *documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+ 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include 
+#include 
+#include 
+
+size_t malloc_usable_size(void *ptr) {
+  Heap_Control *heap_ptr ;
+  size_t size;
+  if (ptr == NULL) {
+return 0;
+  }
+  
+  heap_ptr = malloc_get_heap_pointer();
+  _Heap_Size_of_alloc_area(heap_ptr, ptr, &size);
+
+  return size;
+}
diff --git a/spec/build/cpukit/librtemscpu.yml 
b/spec/build/cpukit/librtemscpu.yml
index 7d6dbae0db..5902008c94 100644
--- a/spec/build/cpukit/librtemscpu.yml
+++ b/spec/build/cpukit/librtemscpu.yml
@@ -670,6 +670,7 @@ source:
 - cpukit/libcsupport/src/lseek.c
 - cpukit/libcsupport/src/lstat.c
 - cpukit/libcsupport/src/malloc.c
+- cpukit/libcsupport/src/mallocusablesize.c 
 - cpukit/libcsupport/src/malloc_deferred.c
 - cpukit/libcsupport/src/malloc_dirtier.c
 - cpukit/libcsupport/src/malloc_walk.c
diff --git a/testsuites/libtests/malloctest/init.c 
b/testsuites/libtests/malloctest/init.c
index a33764177d..ee6a670372 100644
--- a/testsuites/libtests/malloctest/init.c
+++ b/testsuites/libtests/malloctest/init.c
@@ -1362,6 +1362,18 @@ static void test_alloc_zero_size(void)
   rtems_test_assert( p == NULL );
   rtems_test_assert( errno == -1 );
 }
+static void test_usablesize(void)
+{
+  int * a = malloc(sizeof( int )*100);
+  int alloc_size=sizeof( int ) *100 ;
+  rtems_test_assert(  malloc_usable_size(a) <= alloc_size + 
CPU_HEAP_ALIGNMENT);
+  free(a);
+
+  char * b = malloc(sizeof(char)*100);
+  int alloc_size2=sizeof(char) *100 ; 
+  rtems_test_assert( malloc_usable_size ( b ) <= alloc_size2 + 
CPU_HEAP_ALIGNMENT);
+  free(b);
+}
 
 rtems_task Init(
   rtems_task_argument argument
@@ -1405,6 +1417,7 @@ rtems_task Init(
   test_protected_heap_info();
   test_rtems_heap_allocate_aligned_with_boundary();
   test_rtems_malloc();
+  test_usablesize();
   test_rtems_calloc();
   test_greedy_allocate();
   test_alloc_zero_size();
-- 
2.35.1

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


Re: [PATCH] added malloc usable size and test

2022-02-16 Thread Gedare Bloom
add bit more description and ticket # in commit msg

On Sun, Feb 13, 2022 at 8:14 PM zack leung  wrote:
>
> ---
>  cpukit/include/rtems/libcsupport.h|  8 ++-
>  cpukit/libcsupport/src/mallocusablesize.c | 28 +++
>  spec/build/cpukit/librtemscpu.yml |  1 +
>  testsuites/libtests/malloctest/init.c | 15 +++-
>  4 files changed, 50 insertions(+), 2 deletions(-)
>  create mode 100644 cpukit/libcsupport/src/mallocusablesize.c
>
> diff --git a/cpukit/include/rtems/libcsupport.h 
> b/cpukit/include/rtems/libcsupport.h
> index f4be4cfc9a..1fe0f6735f 100644
> --- a/cpukit/include/rtems/libcsupport.h
> +++ b/cpukit/include/rtems/libcsupport.h
> @@ -73,7 +73,12 @@ extern size_t malloc_free_space(void);
>   * Find amount of free heap remaining.
>   */
>  extern int malloc_info(Heap_Information_block *the_info);
> -
> +/**
> + * @brief Get malloc status information.
this brief should probably just be the below statement?

> + *
> + * Find the usable size of the block of memory .
space before .

> + */
> +extern size_t malloc_usable_size(void *ptr);
>  /*
>   *  Prototypes required to install newlib reentrancy user extension
>   */
> @@ -185,6 +190,7 @@ bool rtems_resource_snapshot_equal(
>   */
>  bool rtems_resource_snapshot_check(const rtems_resource_snapshot *snapshot);
>
> +
avoid spurious whitespace changes

>  /** @} */
>
>  #ifdef __cplusplus
> diff --git a/cpukit/libcsupport/src/mallocusablesize.c 
> b/cpukit/libcsupport/src/mallocusablesize.c
> new file mode 100644
> index 00..10114182a6
> --- /dev/null
> +++ b/cpukit/libcsupport/src/mallocusablesize.c
> @@ -0,0 +1,28 @@
> +/*
> + *  COPYRIGHT (c) 1989-2010.
> + *  On-Line Applications Research Corporation (OAR).
Is this your code/copyright?

> + *
> + *  The license and distribution terms for this file may be
> + *  found in the file LICENSE in this distribution or at
> + *  http://www.rtems.org/license/LICENSE.
> + */
Prefer 2-bsd

> +#ifdef HAVE_CONFIG_H
> +#include "config.h"
> +#endif
> +
> +#include 
> +#include 
> +#include 
> +
> +Heap_Control *heap_ptr;
why a global variable here?

> +size_t malloc_usable_size(void *ptr) {
> +  uintptr_t size;

I'd prefer a blank space after the variables. I don't know if that is
part of our conventions.

> +  if (ptr == NULL) {
> +return 0;
> +  }
> +
> +  heap_ptr = malloc_get_heap_pointer();
> +  _Heap_Size_of_alloc_area(heap_ptr, ptr, &size);
> +
> +  return size;
type mismatch between size_t and uintptr_t

> +}
> diff --git a/spec/build/cpukit/librtemscpu.yml 
> b/spec/build/cpukit/librtemscpu.yml
> index 7d6dbae0db..5902008c94 100644
> --- a/spec/build/cpukit/librtemscpu.yml
> +++ b/spec/build/cpukit/librtemscpu.yml
> @@ -670,6 +670,7 @@ source:
>  - cpukit/libcsupport/src/lseek.c
>  - cpukit/libcsupport/src/lstat.c
>  - cpukit/libcsupport/src/malloc.c
> +- cpukit/libcsupport/src/mallocusablesize.c
>  - cpukit/libcsupport/src/malloc_deferred.c
>  - cpukit/libcsupport/src/malloc_dirtier.c
>  - cpukit/libcsupport/src/malloc_walk.c
> diff --git a/testsuites/libtests/malloctest/init.c 
> b/testsuites/libtests/malloctest/init.c
> index a33764177d..2202546719 100644
> --- a/testsuites/libtests/malloctest/init.c
> +++ b/testsuites/libtests/malloctest/init.c
> @@ -1362,6 +1362,18 @@ static void test_alloc_zero_size(void)
>rtems_test_assert( p == NULL );
>rtems_test_assert( errno == -1 );
>  }
> +static void test_usablesize(void)
> +{
> +  int * a = malloc(sizeof( int )*100);
> +  int alloc_size=sizeof( int ) *100 ;
> +  rtems_test_assert(  malloc_usable_size(a) <= alloc_size + 
> CPU_HEAP_ALIGNMENT);
> +  free(a);
> +
> +  char * b = malloc(sizeof(char)*100);
> +  int alloc_size2=sizeof(char) *100 ;
> +  rtems_test_assert( malloc_usable_size ( b ) <= alloc_size2 + 
> CPU_HEAP_ALIGNMENT);
> +  free(b);
> +}
>
>  rtems_task Init(
>rtems_task_argument argument
> @@ -1405,6 +1417,7 @@ rtems_task Init(
>test_protected_heap_info();
>test_rtems_heap_allocate_aligned_with_boundary();
>test_rtems_malloc();
> +  test_usablesize();
>test_rtems_calloc();
>test_greedy_allocate();
>test_alloc_zero_size();
> @@ -1524,4 +1537,4 @@ RTEMS_SYSINIT_ITEM(
>test_early_malloc,
>RTEMS_SYSINIT_INITIAL_EXTENSIONS,
>RTEMS_SYSINIT_ORDER_FIRST
> -);
> +);
whitespace change

> \ No newline at end of file
> --
> 2.35.1
>
> ___
> devel mailing list
> devel@rtems.org
> http://lists.rtems.org/mailman/listinfo/devel
___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


[PATCH] added malloc usable size and test

2022-02-13 Thread zack leung
---
 cpukit/include/rtems/libcsupport.h|  8 ++-
 cpukit/libcsupport/src/mallocusablesize.c | 28 +++
 spec/build/cpukit/librtemscpu.yml |  1 +
 testsuites/libtests/malloctest/init.c | 15 +++-
 4 files changed, 50 insertions(+), 2 deletions(-)
 create mode 100644 cpukit/libcsupport/src/mallocusablesize.c

diff --git a/cpukit/include/rtems/libcsupport.h 
b/cpukit/include/rtems/libcsupport.h
index f4be4cfc9a..1fe0f6735f 100644
--- a/cpukit/include/rtems/libcsupport.h
+++ b/cpukit/include/rtems/libcsupport.h
@@ -73,7 +73,12 @@ extern size_t malloc_free_space(void);
  * Find amount of free heap remaining.
  */
 extern int malloc_info(Heap_Information_block *the_info);
-
+/**
+ * @brief Get malloc status information.
+ * 
+ * Find the usable size of the block of memory .
+ */
+extern size_t malloc_usable_size(void *ptr);
 /*
  *  Prototypes required to install newlib reentrancy user extension
  */
@@ -185,6 +190,7 @@ bool rtems_resource_snapshot_equal(
  */
 bool rtems_resource_snapshot_check(const rtems_resource_snapshot *snapshot);
 
+
 /** @} */
 
 #ifdef __cplusplus
diff --git a/cpukit/libcsupport/src/mallocusablesize.c 
b/cpukit/libcsupport/src/mallocusablesize.c
new file mode 100644
index 00..10114182a6
--- /dev/null
+++ b/cpukit/libcsupport/src/mallocusablesize.c
@@ -0,0 +1,28 @@
+/*
+ *  COPYRIGHT (c) 1989-2010.
+ *  On-Line Applications Research Corporation (OAR).
+ *
+ *  The license and distribution terms for this file may be
+ *  found in the file LICENSE in this distribution or at
+ *  http://www.rtems.org/license/LICENSE.
+ */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include 
+#include 
+#include 
+
+Heap_Control *heap_ptr;
+size_t malloc_usable_size(void *ptr) {
+  uintptr_t size;
+  if (ptr == NULL) {
+return 0;
+  }
+  
+  heap_ptr = malloc_get_heap_pointer();
+  _Heap_Size_of_alloc_area(heap_ptr, ptr, &size);
+
+  return size;
+}
diff --git a/spec/build/cpukit/librtemscpu.yml 
b/spec/build/cpukit/librtemscpu.yml
index 7d6dbae0db..5902008c94 100644
--- a/spec/build/cpukit/librtemscpu.yml
+++ b/spec/build/cpukit/librtemscpu.yml
@@ -670,6 +670,7 @@ source:
 - cpukit/libcsupport/src/lseek.c
 - cpukit/libcsupport/src/lstat.c
 - cpukit/libcsupport/src/malloc.c
+- cpukit/libcsupport/src/mallocusablesize.c 
 - cpukit/libcsupport/src/malloc_deferred.c
 - cpukit/libcsupport/src/malloc_dirtier.c
 - cpukit/libcsupport/src/malloc_walk.c
diff --git a/testsuites/libtests/malloctest/init.c 
b/testsuites/libtests/malloctest/init.c
index a33764177d..2202546719 100644
--- a/testsuites/libtests/malloctest/init.c
+++ b/testsuites/libtests/malloctest/init.c
@@ -1362,6 +1362,18 @@ static void test_alloc_zero_size(void)
   rtems_test_assert( p == NULL );
   rtems_test_assert( errno == -1 );
 }
+static void test_usablesize(void)
+{
+  int * a = malloc(sizeof( int )*100);
+  int alloc_size=sizeof( int ) *100 ;
+  rtems_test_assert(  malloc_usable_size(a) <= alloc_size + 
CPU_HEAP_ALIGNMENT);
+  free(a);
+
+  char * b = malloc(sizeof(char)*100);
+  int alloc_size2=sizeof(char) *100 ; 
+  rtems_test_assert( malloc_usable_size ( b ) <= alloc_size2 + 
CPU_HEAP_ALIGNMENT);
+  free(b);
+}
 
 rtems_task Init(
   rtems_task_argument argument
@@ -1405,6 +1417,7 @@ rtems_task Init(
   test_protected_heap_info();
   test_rtems_heap_allocate_aligned_with_boundary();
   test_rtems_malloc();
+  test_usablesize();
   test_rtems_calloc();
   test_greedy_allocate();
   test_alloc_zero_size();
@@ -1524,4 +1537,4 @@ RTEMS_SYSINIT_ITEM(
   test_early_malloc,
   RTEMS_SYSINIT_INITIAL_EXTENSIONS,
   RTEMS_SYSINIT_ORDER_FIRST
-);
+);
\ No newline at end of file
-- 
2.35.1

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel