libcsupport: Add sbrk greedy support to consume all sbrk memory

- Move the heap sbrk code into a separate routnine.

- Update heap and workspace greedy allocators to use the common
  sbrk greedy support.

Closes #3982
This commit is contained in:
Chris Johns
2021-02-10 17:28:32 +11:00
parent 570992dc27
commit 03dff2019f
6 changed files with 95 additions and 14 deletions
+1
View File
@@ -217,6 +217,7 @@ librtemscpu_a_SOURCES += libcsupport/src/rtems_heap_extend.c
librtemscpu_a_SOURCES += libcsupport/src/rtems_heap_extend_via_sbrk.c
librtemscpu_a_SOURCES += libcsupport/src/rtems_heap_greedy.c
librtemscpu_a_SOURCES += libcsupport/src/rtems_heap_null_extend.c
librtemscpu_a_SOURCES += libcsupport/src/rtems_heap_sbrk_greedy.c
librtemscpu_a_SOURCES += libcsupport/src/rtems_memalign.c
librtemscpu_a_SOURCES += libcsupport/src/rtems_mkdir.c
librtemscpu_a_SOURCES += libcsupport/src/rtems_putc.c
+13
View File
@@ -68,6 +68,19 @@ void *rtems_heap_extend_via_sbrk(
size_t alloc_size
);
/**
* @brief Greedy allocate that empties the sbrk memory
*
* Afterwards all the sbrk avialable memory will have been allocated
* to the provided heap.
*
* @see rtems_heap_extend_via_sbrk().
*/
void rtems_heap_sbrk_greedy_allocate(
Heap_Control *heap,
size_t alloc_size
);
void *rtems_heap_null_extend(
Heap_Control *heap,
size_t alloc_size
+7 -12
View File
@@ -25,25 +25,17 @@
#include "malloc_p.h"
#define SBRK_ALLOC_SIZE (128 * 1024UL * 1024UL)
void *rtems_heap_greedy_allocate(
const uintptr_t *block_sizes,
size_t block_count
)
{
Heap_Control *heap = RTEMS_Malloc_Heap;
size_t size = 128 * 1024 * 1024;
void *opaque;
while ( size > 0 ) {
opaque = (*rtems_malloc_extend_handler)( heap, size );
if ( opaque == NULL ) {
size >>= 1;
} else {
if ( rtems_malloc_dirty_helper != NULL ) {
(*rtems_malloc_dirty_helper)( opaque, size );
}
}
}
rtems_heap_sbrk_greedy_allocate( heap, SBRK_ALLOC_SIZE );
_RTEMS_Lock_allocator();
opaque = _Heap_Greedy_allocate( RTEMS_Malloc_Heap, block_sizes, block_count );
@@ -56,11 +48,14 @@ void *rtems_heap_greedy_allocate_all_except_largest(
uintptr_t *allocatable_size
)
{
Heap_Control *heap = RTEMS_Malloc_Heap;
void *opaque;
rtems_heap_sbrk_greedy_allocate( heap, SBRK_ALLOC_SIZE );
_RTEMS_Lock_allocator();
opaque = _Heap_Greedy_allocate_all_except_largest(
RTEMS_Malloc_Heap,
heap,
allocatable_size
);
_RTEMS_Unlock_allocator();
@@ -0,0 +1,63 @@
/* SPDX-License-Identifier: BSD-2-Clause */
/**
* @file
*
* @brief Greedy Allocate that Empties the sbrk system call
*
* @ingroup MallocSupport
*
* @brief Greedy allocation os sbrk system call memory
*
* The call consumes all the avialable sbrk memory extending
* the supplied heap with it. There is no free as sbrk memory
* is only ever allocated.
*/
/*
* Copyright (c) 2021 Chris Johns. All rights reserved.
*
* 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 "malloc_p.h"
void rtems_heap_sbrk_greedy_allocate(
Heap_Control *heap,
size_t alloc_size
)
{
while ( alloc_size > 0 ) {
void *p = (*rtems_malloc_extend_handler)( heap, alloc_size );
if ( p == NULL ) {
alloc_size >>= 1;
} else {
if ( rtems_malloc_dirty_helper != NULL ) {
(*rtems_malloc_dirty_helper)( p, alloc_size );
}
}
}
}
+10 -2
View File
@@ -33,15 +33,20 @@
#include <rtems/score/threaddispatch.h>
#include <rtems/score/wkspace.h>
#define SBRK_ALLOC_SIZE (128 * 1024UL * 1024UL)
void *rtems_workspace_greedy_allocate(
const uintptr_t *block_sizes,
size_t block_count
)
{
Heap_Control *heap = &_Workspace_Area;
void *opaque;
rtems_heap_sbrk_greedy_allocate( heap, SBRK_ALLOC_SIZE );
_RTEMS_Lock_allocator();
opaque = _Heap_Greedy_allocate( &_Workspace_Area, block_sizes, block_count );
opaque = _Heap_Greedy_allocate( heap, block_sizes, block_count );
_RTEMS_Unlock_allocator();
return opaque;
@@ -51,11 +56,14 @@ void *rtems_workspace_greedy_allocate_all_except_largest(
uintptr_t *allocatable_size
)
{
Heap_Control *heap = &_Workspace_Area;
void *opaque;
rtems_heap_sbrk_greedy_allocate( heap, SBRK_ALLOC_SIZE );
_RTEMS_Lock_allocator();
opaque = _Heap_Greedy_allocate_all_except_largest(
&_Workspace_Area,
heap,
allocatable_size
);
_RTEMS_Unlock_allocator();
+1
View File
@@ -713,6 +713,7 @@ source:
- cpukit/libcsupport/src/rtems_heap_extend_via_sbrk.c
- cpukit/libcsupport/src/rtems_heap_greedy.c
- cpukit/libcsupport/src/rtems_heap_null_extend.c
- cpukit/libcsupport/src/rtems_heap_sbrk_greedy.c
- cpukit/libcsupport/src/rtems_memalign.c
- cpukit/libcsupport/src/rtems_mkdir.c
- cpukit/libcsupport/src/rtems_putc.c