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

Chris Johns chrisj at rtems.org
Wed Feb 10 06:30:25 UTC 2021


Module:    rtems
Branch:    master
Commit:    03dff2019f6972f9b202b15d57ab295d706d86cd
Changeset: http://git.rtems.org/rtems/commit/?id=03dff2019f6972f9b202b15d57ab295d706d86cd

Author:    Chris Johns <chrisj at rtems.org>
Date:      Tue Feb  9 12:48:07 2021 +1100

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

---

 cpukit/Makefile.am                              |  1 +
 cpukit/include/rtems/malloc.h                   | 13 +++++
 cpukit/libcsupport/src/rtems_heap_greedy.c      | 19 +++-----
 cpukit/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(-)

diff --git a/cpukit/Makefile.am b/cpukit/Makefile.am
index 565aa66..14abc8b 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 13e94ac..ec6473a 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 c02e48d..94c28d7 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 0000000..49ae2a7
--- /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 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 );
+      }
+    }
+  }
+}
diff --git a/cpukit/rtems/src/workspacegreedy.c b/cpukit/rtems/src/workspacegreedy.c
index 09204c2..5ddf004 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 0979253..f54673f 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



More information about the vc mailing list