[PATCH v2] libcsupport: Add sbrk greedy support to consume all sbrk memory
chrisj at rtems.org
chrisj at rtems.org
Tue Feb 9 05:37:25 UTC 2021
From: Chris Johns <chrisj at rtems.org>
- Move the heap sbrk code into a separate routnine.
- Update heap and workspace greedy allocators to use the common
sbrk greedy support.
Closes #3982
---
cpukit/Makefile.am | 1 +
cpukit/include/rtems/malloc.h | 13 ++++
cpukit/libcsupport/src/rtems_heap_greedy.c | 19 +++---
.../libcsupport/src/rtems_heap_sbrk_greedy.c | 63 +++++++++++++++++++
cpukit/rtems/src/workspacegreedy.c | 12 +++-
spec/build/cpukit/librtemscpu.yml | 1 +
6 files changed, 95 insertions(+), 14 deletions(-)
create mode 100644 cpukit/libcsupport/src/rtems_heap_sbrk_greedy.c
diff --git a/cpukit/Makefile.am b/cpukit/Makefile.am
index 565aa66ce1..14abc8bb6a 100644
--- a/cpukit/Makefile.am
+++ b/cpukit/Makefile.am
@@ -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
diff --git a/cpukit/include/rtems/malloc.h b/cpukit/include/rtems/malloc.h
index 13e94ac38a..ec6473a703 100644
--- a/cpukit/include/rtems/malloc.h
+++ b/cpukit/include/rtems/malloc.h
@@ -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
diff --git a/cpukit/libcsupport/src/rtems_heap_greedy.c b/cpukit/libcsupport/src/rtems_heap_greedy.c
index c02e48d962..94c28d7f16 100644
--- a/cpukit/libcsupport/src/rtems_heap_greedy.c
+++ b/cpukit/libcsupport/src/rtems_heap_greedy.c
@@ -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();
diff --git a/cpukit/libcsupport/src/rtems_heap_sbrk_greedy.c b/cpukit/libcsupport/src/rtems_heap_sbrk_greedy.c
new file mode 100644
index 0000000000..aad3188ac5
--- /dev/null
+++ b/cpukit/libcsupport/src/rtems_heap_sbrk_greedy.c
@@ -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 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 );
+ }
+ }
+ }
+}
diff --git a/cpukit/rtems/src/workspacegreedy.c b/cpukit/rtems/src/workspacegreedy.c
index 09204c2833..5ddf004787 100644
--- a/cpukit/rtems/src/workspacegreedy.c
+++ b/cpukit/rtems/src/workspacegreedy.c
@@ -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();
diff --git a/spec/build/cpukit/librtemscpu.yml b/spec/build/cpukit/librtemscpu.yml
index 097925398d..f54673f48c 100644
--- a/spec/build/cpukit/librtemscpu.yml
+++ b/spec/build/cpukit/librtemscpu.yml
@@ -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
--
2.24.1
More information about the devel
mailing list