[rtems commit] score: Split up objects allocation

Sebastian Huber sebh at rtems.org
Wed Feb 12 15:11:59 UTC 2020


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

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Tue Dec 10 10:41:48 2019 +0100

score: Split up objects allocation

Split up the different objects allocation methods into separate
functions.  This helps to avoid a dependency on the workspace in case no
objects or a static set of objects is configured.

Change license to BSD-2-Clause according to file histories.

Update #3053.
Update #3835.

---

 cpukit/Makefile.am                         |   3 +
 cpukit/include/rtems/score/objectdata.h    |  48 +++++++++++++-
 cpukit/include/rtems/score/objectimpl.h    |  61 +++++++++++------
 cpukit/include/rtems/score/thread.h        |   3 +
 cpukit/score/src/objectallocate.c          | 101 ++++++++---------------------
 cpukit/score/src/objectallocatenone.c      |  45 +++++++++++++
 cpukit/score/src/objectallocatestatic.c    |  44 +++++++++++++
 cpukit/score/src/objectallocateunlimited.c |  79 ++++++++++++++++++++++
 8 files changed, 289 insertions(+), 95 deletions(-)

diff --git a/cpukit/Makefile.am b/cpukit/Makefile.am
index 06e98c3..25dbd58 100644
--- a/cpukit/Makefile.am
+++ b/cpukit/Makefile.am
@@ -858,6 +858,9 @@ librtemscpu_a_SOURCES += score/src/memoryfill.c
 librtemscpu_a_SOURCES += score/src/memoryzerobeforeuse.c
 librtemscpu_a_SOURCES += score/src/memoryzerofreeareas.c
 librtemscpu_a_SOURCES += score/src/objectallocate.c
+librtemscpu_a_SOURCES += score/src/objectallocatenone.c
+librtemscpu_a_SOURCES += score/src/objectallocatestatic.c
+librtemscpu_a_SOURCES += score/src/objectallocateunlimited.c
 librtemscpu_a_SOURCES += score/src/objectclose.c
 librtemscpu_a_SOURCES += score/src/objectextendinformation.c
 librtemscpu_a_SOURCES += score/src/objectfree.c
diff --git a/cpukit/include/rtems/score/objectdata.h b/cpukit/include/rtems/score/objectdata.h
index e44fb52..b923c45 100644
--- a/cpukit/include/rtems/score/objectdata.h
+++ b/cpukit/include/rtems/score/objectdata.h
@@ -162,6 +162,8 @@ typedef void ( *Objects_Thread_queue_Extract_callout )(
 );
 #endif
 
+typedef struct Objects_Information Objects_Information;
+
 /**
  * @brief The information structure used to manage each API class of objects.
  *
@@ -171,7 +173,7 @@ typedef void ( *Objects_Thread_queue_Extract_callout )(
  * allocated and pre-initialized instance for each API class providing zero
  * objects, see OBJECTS_INFORMATION_DEFINE_ZERO().
  */
-typedef struct {
+struct Objects_Information {
   /**
    * @brief This is the maximum valid ID of this object API class.
    *
@@ -193,6 +195,14 @@ typedef struct {
   Objects_Control **local_table;
 
   /**
+   * @brief Allocate an object.
+   *
+   * @see _Objects_Allocate_none(), _Objects_Allocate_static(), and
+   *   _Objects_Allocate_unlimited().
+   */
+  Objects_Control *( *allocate )( Objects_Information * );
+
+  /**
    * @brief This is the number of object control blocks on the inactive chain.
    *
    * This member is only used if unlimited objects are configured for this API
@@ -288,7 +298,38 @@ typedef struct {
    */
   RBTree_Control Global_by_name;
 #endif
-} Objects_Information;
+};
+
+/**
+ * @brief Always return NULL.
+ *
+ * @param information The objects information.
+ *
+ * @retval NULL Always.
+ */
+Objects_Control *_Objects_Allocate_none( Objects_Information *information );
+
+/**
+ * @brief Return an inactive object or NULL.
+ *
+ * @param information The objects information.
+ *
+ * @retval NULL No inactive object is available.
+ * @retval object An inactive object.
+ */
+Objects_Control *_Objects_Allocate_static( Objects_Information *information );
+
+/**
+ * @brief Return an inactive object or NULL.
+ *
+ * Try to extend the objects information if necessary.
+ *
+ * @param information The objects information.
+ *
+ * @retval NULL No inactive object is available.
+ * @retval object An inactive object.
+ */
+Objects_Control *_Objects_Allocate_unlimited( Objects_Information *information );
 
 #if defined(RTEMS_MULTIPROCESSING)
 #define OBJECTS_INFORMATION_MP( name, extract ) \
@@ -315,6 +356,7 @@ typedef struct {
 Objects_Information name##_Information = { \
   _Objects_Build_id( api, cls, 1, 0 ), \
   NULL, \
+  _Objects_Allocate_none, \
   0, \
   0, \
   0, \
@@ -351,6 +393,8 @@ static type name##_Objects[ _Objects_Maximum_per_allocation( max ) ]; \
 Objects_Information name##_Information = { \
   _Objects_Build_id( api, cls, 1, _Objects_Maximum_per_allocation( max ) ), \
   name##_Local_table, \
+  _Objects_Is_unlimited( max ) ? \
+    _Objects_Allocate_unlimited : _Objects_Allocate_static, \
   0, \
   _Objects_Is_unlimited( max ) ? _Objects_Maximum_per_allocation( max ) : 0, \
   sizeof( type ), \
diff --git a/cpukit/include/rtems/score/objectimpl.h b/cpukit/include/rtems/score/objectimpl.h
index 83e93a2b..a2548dd 100644
--- a/cpukit/include/rtems/score/objectimpl.h
+++ b/cpukit/include/rtems/score/objectimpl.h
@@ -23,7 +23,9 @@
 
 #include <rtems/score/objectdata.h>
 #include <rtems/score/apimutex.h>
+#include <rtems/score/assert.h>
 #include <rtems/score/isrlock.h>
+#include <rtems/score/sysstate.h>
 #include <rtems/score/threaddispatch.h>
 
 #ifdef __cplusplus
@@ -145,25 +147,6 @@ unsigned int _Objects_API_maximum_class(
 );
 
 /**
- * @brief Allocates an object without locking the allocator mutex.
- *
- * This function can be called in two contexts
- * - the executing thread is the owner of the object allocator mutex, or
- * - in case the system state is not up, e.g. during sequential system
- *   initialization.
- *
- * @param[in, out] information The object information block.
- *
- * @retval object The allocated object.
- * @retval NULL No object available.
- *
- * @see _Objects_Allocate() and _Objects_Free().
- */
-Objects_Control *_Objects_Allocate_unprotected(
-  Objects_Information *information
-);
-
-/**
  * @brief Allocates an object.
  *
  * This function locks the object allocator mutex via
@@ -703,6 +686,19 @@ RTEMS_INLINE_ROUTINE Objects_Maximum _Objects_Get_maximum_index(
 }
 
 /**
+ * @brief Get an inactive object or NULL.
+ *
+ * @retval NULL No inactive object is available.
+ * @retval object An inactive object.
+ */
+RTEMS_INLINE_ROUTINE Objects_Control *_Objects_Get_inactive(
+  Objects_Information *information
+)
+{
+  return (Objects_Control *) _Chain_Get_unprotected( &information->Inactive );
+}
+
+/**
  * @brief Checks if the automatic object extension (unlimited objects) is
  * enabled.
  *
@@ -902,6 +898,33 @@ RTEMS_INLINE_ROUTINE bool _Objects_Allocator_is_owner( void )
   return _RTEMS_Allocator_is_owner();
 }
 
+/**
+ * @brief Allocates an object without locking the allocator mutex.
+ *
+ * This function can be called in two contexts
+ * - the executing thread is the owner of the object allocator mutex, or
+ * - in case the system state is not up, e.g. during sequential system
+ *   initialization.
+ *
+ * @param[in, out] information The object information block.
+ *
+ * @retval object The allocated object.
+ * @retval NULL No object available.
+ *
+ * @see _Objects_Allocate() and _Objects_Free().
+ */
+RTEMS_INLINE_ROUTINE Objects_Control *_Objects_Allocate_unprotected(
+  Objects_Information *information
+)
+{
+  _Assert(
+    _Objects_Allocator_is_owner()
+      || !_System_state_Is_up( _System_state_Get() )
+  );
+
+  return ( *information->allocate )( information );
+}
+
 /** @} */
 
 #ifdef __cplusplus
diff --git a/cpukit/include/rtems/score/thread.h b/cpukit/include/rtems/score/thread.h
index e438549..3d97774 100644
--- a/cpukit/include/rtems/score/thread.h
+++ b/cpukit/include/rtems/score/thread.h
@@ -1023,6 +1023,7 @@ Thread_Information name##_Information = { \
   { \
     _Objects_Build_id( api, cls, 1, 0 ), \
     NULL, \
+    _Objects_Allocate_none, \
     0, \
     0, \
     0, \
@@ -1048,6 +1049,8 @@ Thread_Information name##_Information = { \
   { \
     _Objects_Build_id( api, cls, 1, _Objects_Maximum_per_allocation( max ) ), \
     name##_Local_table, \
+    _Objects_Is_unlimited( max ) ? \
+      _Objects_Allocate_unlimited : _Objects_Allocate_static, \
     0, \
     _Objects_Is_unlimited( max ) ? _Objects_Maximum_per_allocation( max ) : 0, \
     sizeof( Thread_Configured_control ), \
diff --git a/cpukit/score/src/objectallocate.c b/cpukit/score/src/objectallocate.c
index ac7579d..588c516 100644
--- a/cpukit/score/src/objectallocate.c
+++ b/cpukit/score/src/objectallocate.c
@@ -1,17 +1,36 @@
 /**
- *  @file
+ * @file
  *
- *  @brief Allocate Object
- *  @ingroup RTEMSScoreObject
+ * @ingroup RTEMSScoreObject
+ *
+ * @brief Allocate Object
  */
 
 /*
- *  COPYRIGHT (c) 1989-1999.
- *  On-Line Applications Research Corporation (OAR).
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (C) 2014 embedded brains GmbH
+ *
+ * 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.
  *
- *  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.
+ * 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.
  */
 
 #if HAVE_CONFIG_H
@@ -19,72 +38,6 @@
 #endif
 
 #include <rtems/score/objectimpl.h>
-#include <rtems/score/assert.h>
-#include <rtems/score/chainimpl.h>
-#include <rtems/score/sysstate.h>
-
-static Objects_Control *_Objects_Get_inactive(
-  Objects_Information *information
-)
-{
-  return (Objects_Control *) _Chain_Get_unprotected( &information->Inactive );
-}
-
-Objects_Control *_Objects_Allocate_unprotected(
-  Objects_Information *information
-)
-{
-  Objects_Control *the_object;
-
-  _Assert(
-    _Objects_Allocator_is_owner()
-      || !_System_state_Is_up( _System_state_Get() )
-  );
-
-  /*
-   *  If the application is using the optional manager stubs and
-   *  still attempts to create the object, the information block
-   *  should be all zeroed out because it is in the BSS.  So let's
-   *  check that code for this manager is even present.
-   */
-  if ( information->object_size == 0 )
-    return NULL;
-
-  /*
-   *  OK.  The manager should be initialized and configured to have objects.
-   *  With any luck, it is safe to attempt to allocate an object.
-   */
-  the_object = _Objects_Get_inactive( information );
-
-  if ( _Objects_Is_auto_extend( information ) ) {
-    /*
-     *  If the list is empty then we are out of objects and need to
-     *  extend information base.
-     */
-
-    if ( the_object == NULL ) {
-      _Objects_Extend_information( information );
-      the_object = _Objects_Get_inactive( information );
-    }
-
-    if ( the_object != NULL ) {
-      Objects_Maximum objects_per_block;
-      Objects_Maximum block;
-
-      objects_per_block = information->objects_per_block;
-      block = _Objects_Get_index( the_object->id ) - OBJECTS_INDEX_MINIMUM;
-
-      if ( block > objects_per_block ) {
-        block /= objects_per_block;
-
-        information->inactive_per_block[ block ]--;
-        information->inactive--;
-      }
-    }
-  }
-
-  return the_object;
-}
 
 Objects_Control *_Objects_Allocate( Objects_Information *information )
 {
diff --git a/cpukit/score/src/objectallocatenone.c b/cpukit/score/src/objectallocatenone.c
new file mode 100644
index 0000000..0669f99
--- /dev/null
+++ b/cpukit/score/src/objectallocatenone.c
@@ -0,0 +1,45 @@
+/**
+ * @file
+ *
+ * @ingroup RTEMSScoreObject
+ */
+
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (C) 2019 embedded brains GmbH
+ *
+ * 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.
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <rtems/score/objectdata.h>
+
+Objects_Control *_Objects_Allocate_none( Objects_Information *information )
+{
+  (void) information;
+
+  return NULL;
+}
diff --git a/cpukit/score/src/objectallocatestatic.c b/cpukit/score/src/objectallocatestatic.c
new file mode 100644
index 0000000..73790ba
--- /dev/null
+++ b/cpukit/score/src/objectallocatestatic.c
@@ -0,0 +1,44 @@
+/**
+ * @file
+ *
+ * @ingroup RTEMSScoreObject
+ */
+
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (C) 2019 embedded brains GmbH
+ *
+ * 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.
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <rtems/score/objectdata.h>
+#include <rtems/score/objectimpl.h>
+
+Objects_Control *_Objects_Allocate_static( Objects_Information *information )
+{
+  return _Objects_Get_inactive( information );
+}
diff --git a/cpukit/score/src/objectallocateunlimited.c b/cpukit/score/src/objectallocateunlimited.c
new file mode 100644
index 0000000..92727e1
--- /dev/null
+++ b/cpukit/score/src/objectallocateunlimited.c
@@ -0,0 +1,79 @@
+/**
+ * @file
+ *
+ * @ingroup RTEMSScoreObject
+ */
+
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (C) 1989, 2007 On-Line Applications Research Corporation (OAR)
+ *
+ * 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.
+ */
+
+#if HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <rtems/score/objectdata.h>
+#include <rtems/score/objectimpl.h>
+
+Objects_Control *_Objects_Allocate_unlimited( Objects_Information *information )
+{
+  Objects_Control *the_object;
+
+  _Assert( _Objects_Is_auto_extend( information ) );
+
+  /*
+   *  OK.  The manager should be initialized and configured to have objects.
+   *  With any luck, it is safe to attempt to allocate an object.
+   */
+  the_object = _Objects_Get_inactive( information );
+
+  /*
+   *  If the list is empty then we are out of objects and need to
+   *  extend information base.
+   */
+
+  if ( the_object == NULL ) {
+    _Objects_Extend_information( information );
+    the_object = _Objects_Get_inactive( information );
+  }
+
+  if ( the_object != NULL ) {
+    Objects_Maximum objects_per_block;
+    Objects_Maximum block;
+
+    objects_per_block = information->objects_per_block;
+    block = _Objects_Get_index( the_object->id ) - OBJECTS_INDEX_MINIMUM;
+
+    if ( block > objects_per_block ) {
+      block /= objects_per_block;
+
+      information->inactive_per_block[ block ]--;
+      information->inactive--;
+    }
+  }
+
+  return the_object;
+}



More information about the vc mailing list