[PATCH] libcsupport: Add sbrk greedy support to consume all sbrk memory

chrisj at rtems.org chrisj at rtems.org
Tue Feb 9 02:31:40 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  | 37 +++++++++++++++++++
 cpukit/rtems/src/workspacegreedy.c            | 12 +++++-
 spec/build/cpukit/librtemscpu.yml             |  1 +
 6 files changed, 69 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..1fd82eed4f
--- /dev/null
+++ b/cpukit/libcsupport/src/rtems_heap_sbrk_greedy.c
@@ -0,0 +1,37 @@
+/**
+ *  @file
+ *
+ *  @brief Greedy Allocate that Empties the sbrk system call
+ *  @ingroup MallocSupport
+ */
+
+/*
+ * Copyright (c) 2021 Chris Johns.  All rights reserved.
+ *
+ * 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 "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