[rtems commit] posix: Delete POSIX_Keys_Freechain type

Sebastian Huber sebh at rtems.org
Thu Aug 8 12:11:18 UTC 2013


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

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Tue Aug  6 15:28:59 2013 +0200

posix: Delete POSIX_Keys_Freechain type

Use the POSIX configuration value directly.  Use right type early and
avoid casts.  Use proper unlimited objects API.  Check workspace
allocation.  Make functions static.

---

 cpukit/posix/include/rtems/posix/keyimpl.h |   31 +++++-----
 cpukit/posix/src/key.c                     |   88 ++++++++++++++-------------
 cpukit/posix/src/keyfreememory.c           |    5 +-
 cpukit/posix/src/keyrundestructors.c       |   28 ++++-----
 cpukit/posix/src/keysetspecific.c          |    4 +-
 5 files changed, 76 insertions(+), 80 deletions(-)

diff --git a/cpukit/posix/include/rtems/posix/keyimpl.h b/cpukit/posix/include/rtems/posix/keyimpl.h
index b6b34b2..f01eda4 100644
--- a/cpukit/posix/include/rtems/posix/keyimpl.h
+++ b/cpukit/posix/include/rtems/posix/keyimpl.h
@@ -35,14 +35,6 @@ extern "C" {
  */
 
 /**
- * @brief POSIX_Keys_Freechain is used in Freechain structure
- */
-typedef struct {
-    Freechain_Control super_fc;
-    size_t bump_count;
-} POSIX_Keys_Freechain;
-
-/**
  * @brief The information control block used to manage this class of objects.
  */
 POSIX_EXTERN Objects_Information  _POSIX_Keys_Information;
@@ -55,7 +47,7 @@ POSIX_EXTERN RBTree_Control _POSIX_Keys_Key_value_lookup_tree;
 /**
  * @brief This freechain is used as a memory pool for POSIX_Keys_Key_value_pair.
  */
-POSIX_EXTERN POSIX_Keys_Freechain _POSIX_Keys_Keypool;
+POSIX_EXTERN Freechain_Control _POSIX_Keys_Keypool;
 
 /**
  * @brief POSIX key manager initialization.
@@ -65,14 +57,6 @@ POSIX_EXTERN POSIX_Keys_Freechain _POSIX_Keys_Keypool;
 void _POSIX_Key_Manager_initialization(void);
 
 /**
- * @brief POSIX key Freechain extend handle
- *
- * This routine extend freechain node, which is called in freechain_get
- * automatically.
- */
-bool _POSIX_Keys_Freechain_extend(Freechain_Control *freechain);
-
-/**
  * @brief POSIX keys Red-Black tree node comparison.
  *
  * This routine compares the rbtree node
@@ -168,6 +152,19 @@ RTEMS_INLINE_ROUTINE POSIX_Keys_Control *_POSIX_Keys_Get (
     _Objects_Get( &_POSIX_Keys_Information, (Objects_Id) id, location );
 }
 
+RTEMS_INLINE_ROUTINE POSIX_Keys_Key_value_pair *
+_POSIX_Keys_Key_value_pair_allocate( void )
+{
+  return (POSIX_Keys_Key_value_pair *) _Freechain_Get( &_POSIX_Keys_Keypool );
+}
+
+RTEMS_INLINE_ROUTINE void _POSIX_Keys_Key_value_pair_free(
+  POSIX_Keys_Key_value_pair *key_value_pair
+)
+{
+  _Freechain_Put( &_POSIX_Keys_Keypool, key_value_pair );
+}
+
 /** @} */
 
 #ifdef __cplusplus
diff --git a/cpukit/posix/src/key.c b/cpukit/posix/src/key.c
index 99654c2..2b32b50 100644
--- a/cpukit/posix/src/key.c
+++ b/cpukit/posix/src/key.c
@@ -22,6 +22,7 @@
 #include <rtems/posix/keyimpl.h>
 #include <rtems/posix/config.h>
 #include <rtems/score/chainimpl.h>
+#include <rtems/score/objectimpl.h>
 #include <rtems/score/wkspace.h>
 
 /**
@@ -69,57 +70,60 @@ int _POSIX_Keys_Key_value_lookup_tree_compare_function(
   return 0;
 }
 
-/**
- * @brief This routine does user side freechain initialization
- */
-static void _POSIX_Keys_Freechain_init(Freechain_Control *freechain)
+static uint32_t _POSIX_Keys_Get_keypool_bump_count( void )
 {
-  POSIX_Keys_Freechain *psx_freechain_p = (POSIX_Keys_Freechain *)freechain;
-  psx_freechain_p->bump_count =
-    Configuration_POSIX_API.maximum_key_value_pairs & 0x7FFFFFFF;
-  size_t size = psx_freechain_p->bump_count * sizeof(POSIX_Keys_Key_value_pair);
-  POSIX_Keys_Key_value_pair *nodes = _Workspace_Allocate(size);
+  uint32_t max = Configuration_POSIX_API.maximum_key_value_pairs;
 
-  _Chain_Initialize(
-                    &freechain->Freechain,
-                    nodes,
-                    psx_freechain_p->bump_count,
-                    sizeof(POSIX_Keys_Key_value_pair)
-                    );
+  return _Objects_Is_unlimited( max ) ?
+    _Objects_Maximum_per_allocation( max ) : 0;
 }
 
-/**
- * @brief This routine does keypool initialize, keypool contains all
- * POSIX_Keys_Key_value_pair
- */
-
-static void _POSIX_Keys_Keypool_init(void)
+static uint32_t _POSIX_Keys_Get_initial_keypool_size( void )
 {
-  _Freechain_Initialize((Freechain_Control *)&_POSIX_Keys_Keypool,
-                       &_POSIX_Keys_Freechain_extend);
+  uint32_t max = Configuration_POSIX_API.maximum_key_value_pairs;
 
-  _POSIX_Keys_Freechain_init((Freechain_Control *)&_POSIX_Keys_Keypool);
+  return _Objects_Maximum_per_allocation( max );
 }
 
-/**
- * @brief This routine is user defined freechain extension handle
- */
-bool _POSIX_Keys_Freechain_extend(Freechain_Control *freechain)
+static bool _POSIX_Keys_Keypool_extend( Freechain_Control *keypool )
 {
-  POSIX_Keys_Freechain *psx_freechain_p = (POSIX_Keys_Freechain *)freechain;
-  size_t node_size = sizeof(POSIX_Keys_Key_value_pair);
-  size_t size = psx_freechain_p->bump_count * node_size;
-  int i;
-  POSIX_Keys_Key_value_pair *nodes = _Workspace_Allocate(size);
-
-  if (!nodes)
-    return false;
-
-  for ( i = 0; i < psx_freechain_p->bump_count; i++ ) {
-      _Freechain_Put(freechain,
-                     nodes + i);
+  size_t bump_count = _POSIX_Keys_Get_keypool_bump_count();
+  bool ok = bump_count > 0;
+
+  if ( ok ) {
+    size_t size = bump_count * sizeof( POSIX_Keys_Key_value_pair );
+    POSIX_Keys_Key_value_pair *nodes = _Workspace_Allocate( size );
+
+    ok = nodes != NULL;
+
+    if ( ok ) {
+      _Chain_Initialize(
+        &keypool->Freechain,
+        nodes,
+        bump_count,
+        sizeof( *nodes )
+      );
+    }
   }
-  return true;
+
+  return ok;
+}
+
+static void _POSIX_Keys_Initialize_keypool( void )
+{
+  Freechain_Control *keypool = &_POSIX_Keys_Keypool;
+  size_t initial_count = _POSIX_Keys_Get_initial_keypool_size();
+  size_t size = initial_count * sizeof( POSIX_Keys_Key_value_pair );
+  POSIX_Keys_Key_value_pair *nodes = _Workspace_Allocate_or_fatal_error( size );
+
+  _Freechain_Initialize( keypool, _POSIX_Keys_Keypool_extend );
+
+  _Chain_Initialize(
+    &keypool->Freechain,
+    nodes,
+    initial_count,
+    sizeof( *nodes )
+  );
 }
 
 /**
@@ -150,5 +154,5 @@ void _POSIX_Key_Manager_initialization(void)
       true
   );
 
-  _POSIX_Keys_Keypool_init();
+  _POSIX_Keys_Initialize_keypool();
 }
diff --git a/cpukit/posix/src/keyfreememory.c b/cpukit/posix/src/keyfreememory.c
index d971d0a..8c24948 100644
--- a/cpukit/posix/src/keyfreememory.c
+++ b/cpukit/posix/src/keyfreememory.c
@@ -54,9 +54,8 @@ void _POSIX_Keys_Free_memory(
     next = _RBTree_Next_unprotected( iter, RBT_RIGHT );
     _RBTree_Extract_unprotected( &_POSIX_Keys_Key_value_lookup_tree, iter );
     _Chain_Extract_unprotected( &p->Key_values_per_thread_node );
-    /* append the node to _POSIX_Keys_Keypool */
-    _Freechain_Put( &_POSIX_Keys_Keypool.super_fc,
-                    ( void * )p);
+    _POSIX_Keys_Key_value_pair_free( p );
+
     iter = next;
     p = _RBTree_Container_of( iter, POSIX_Keys_Key_value_pair, Key_value_lookup_node );
   }
diff --git a/cpukit/posix/src/keyrundestructors.c b/cpukit/posix/src/keyrundestructors.c
index 078f8f6..5f0a699 100644
--- a/cpukit/posix/src/keyrundestructors.c
+++ b/cpukit/posix/src/keyrundestructors.c
@@ -38,7 +38,7 @@ void _POSIX_Keys_Run_destructors(
 )
 {
   Chain_Control *chain;
-  Chain_Node *iter, *next;
+  POSIX_Keys_Key_value_pair *iter, *next;
   void *value;
   void (*destructor) (void *);
   POSIX_Keys_Control *the_key;
@@ -49,9 +49,11 @@ void _POSIX_Keys_Run_destructors(
   chain = &(
       (POSIX_API_Control *)thread->API_Extensions[ THREAD_API_POSIX ]
   )->Key_Chain;
-  iter = _Chain_First( chain );
-  while ( !_Chain_Is_tail( chain, iter ) ) {
-    next = _Chain_Next( iter );
+  iter = (POSIX_Keys_Key_value_pair *) _Chain_First( chain );
+  while ( !_Chain_Is_tail( chain, &iter->Key_values_per_thread_node ) ) {
+    next = (POSIX_Keys_Key_value_pair *)
+      _Chain_Next( &iter->Key_values_per_thread_node );
+
     /**
      * remove key from rbtree and chain.
      * here Chain_Node *iter can be convert to POSIX_Keys_Key_value_pair *,
@@ -60,31 +62,25 @@ void _POSIX_Keys_Run_destructors(
      */
     _RBTree_Extract_unprotected(
         &_POSIX_Keys_Key_value_lookup_tree,
-        &((POSIX_Keys_Key_value_pair *)iter)->Key_value_lookup_node
+        &iter->Key_value_lookup_node
     );
-    _Chain_Extract_unprotected( iter );
+    _Chain_Extract_unprotected( &iter->Key_values_per_thread_node );
 
     /**
      * run key value's destructor if destructor and value are both non-null.
      */
-    the_key = _POSIX_Keys_Get(
-        ((POSIX_Keys_Key_value_pair *)iter)->key,
-        &location
-    );
+    the_key = _POSIX_Keys_Get( iter->key, &location );
     destructor = the_key->destructor;
-    value = ((POSIX_Keys_Key_value_pair *)iter)->value;
+    value = iter->value;
     if ( destructor != NULL && value != NULL )
       (*destructor)( value );
 
     _Objects_Put( &the_key->Object );
 
-    /**
-     * put back this node to keypool
-     */
-    _Freechain_Put( &_POSIX_Keys_Keypool.super_fc,
-                    (void *)iter );
+    _POSIX_Keys_Key_value_pair_free( iter );
 
     iter = next;
   }
+
   _Thread_Enable_dispatch();
 }
diff --git a/cpukit/posix/src/keysetspecific.c b/cpukit/posix/src/keysetspecific.c
index ee718a4..8f5ce72 100644
--- a/cpukit/posix/src/keysetspecific.c
+++ b/cpukit/posix/src/keysetspecific.c
@@ -43,8 +43,8 @@ int pthread_setspecific(
   switch ( location ) {
 
     case OBJECTS_LOCAL:
-      value_pair_ptr = ( POSIX_Keys_Key_value_pair * )
-        _Freechain_Get( &_POSIX_Keys_Keypool.super_fc );
+      value_pair_ptr = _POSIX_Keys_Key_value_pair_allocate();
+
       if ( !value_pair_ptr ) {
         _Objects_Put( &the_key->Object );
 




More information about the vc mailing list