[rtems commit] malloc: Use dedicated lock for deferred frees

Sebastian Huber sebh at rtems.org
Thu Feb 25 10:55:09 UTC 2016


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

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Thu Feb 25 08:03:21 2016 +0100

malloc: Use dedicated lock for deferred frees

---

 cpukit/libcsupport/src/malloc_deferred.c | 35 ++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 9 deletions(-)

diff --git a/cpukit/libcsupport/src/malloc_deferred.c b/cpukit/libcsupport/src/malloc_deferred.c
index e85b264..395ed64 100644
--- a/cpukit/libcsupport/src/malloc_deferred.c
+++ b/cpukit/libcsupport/src/malloc_deferred.c
@@ -30,7 +30,9 @@
 #include <rtems/score/sysstate.h>
 #include <rtems/score/threaddispatch.h>
 
-RTEMS_CHAIN_DEFINE_EMPTY(RTEMS_Malloc_GC_list);
+static RTEMS_CHAIN_DEFINE_EMPTY( _Malloc_GC_list );
+
+RTEMS_INTERRUPT_LOCK_DEFINE( static, _Malloc_GC_lock, "Malloc GC" )
 
 bool malloc_is_system_state_OK(void)
 {
@@ -38,21 +40,36 @@ bool malloc_is_system_state_OK(void)
     || _Thread_Dispatch_is_enabled();
 }
 
-void malloc_deferred_frees_process(void)
+static void *_Malloc_Get_deferred_free( void )
 {
-  rtems_chain_node  *to_be_freed;
+  rtems_interrupt_lock_context lock_context;
+  void *p;
+
+  rtems_interrupt_lock_acquire( &_Malloc_GC_lock, &lock_context );
+  p = rtems_chain_get_unprotected( &_Malloc_GC_list );
+  rtems_interrupt_lock_release( &_Malloc_GC_lock, &lock_context );
+
+  return p;
+}
+
+void malloc_deferred_frees_process( void )
+{
+  rtems_chain_node *to_be_freed;
 
   /*
    *  If some free's have been deferred, then do them now.
    */
-  while ((to_be_freed = rtems_chain_get(&RTEMS_Malloc_GC_list)) != NULL)
-    free(to_be_freed);
+  while ( ( to_be_freed = _Malloc_Get_deferred_free() ) != NULL ) {
+    free( to_be_freed );
+  }
 }
 
-void malloc_deferred_free(
-  void *pointer
-)
+void malloc_deferred_free( void *p )
 {
-  rtems_chain_append(&RTEMS_Malloc_GC_list, (rtems_chain_node *)pointer);
+  rtems_interrupt_lock_context lock_context;
+
+  rtems_interrupt_lock_acquire( &_Malloc_GC_lock, &lock_context );
+  rtems_chain_append_unprotected( &_Malloc_GC_list, (rtems_chain_node *) p );
+  rtems_interrupt_lock_release( &_Malloc_GC_lock, &lock_context );
 }
 #endif




More information about the vc mailing list