[PATCH] added malloc usable size and test
zack leung
zakthertemsdev at gmail.com
Sat Feb 26 02:42:21 UTC 2022
ping
*
On Sun, 20 Feb 2022 at 02:55, zack leung <zakthertemsdev at gmail.com> 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 0000000000..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 <rtems/libcsupport.h>
> +#include <rtems/malloc.h>
> +#include <rtems/score/heapimpl.h>
> +
> +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
>
>
More information about the devel
mailing list