[RTEMS 5 1/2] score: Fix _Objects_Active_count()

2022-07-18 Thread Sebastian Huber
With unlimited objects the object maximum may be larger than the sum of active
and inactive objects.

Update #4676.
---
 cpukit/score/src/objectactivecount.c | 18 +-
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/cpukit/score/src/objectactivecount.c 
b/cpukit/score/src/objectactivecount.c
index c658fc21e3..12c15147c7 100644
--- a/cpukit/score/src/objectactivecount.c
+++ b/cpukit/score/src/objectactivecount.c
@@ -24,14 +24,22 @@ Objects_Maximum _Objects_Active_count(
   const Objects_Information *information
 )
 {
-  Objects_Maximum inactive;
-  Objects_Maximum maximum;
+  Objects_Maximum   active;
+  Objects_Maximum   index;
+  Objects_Maximum   maximum;
+  Objects_Control **local_table;
 
   _Assert( _Objects_Allocator_is_owner() );
 
-  inactive = (Objects_Maximum)
-_Chain_Node_count_unprotected( >Inactive );
+  active = 0;
   maximum  = _Objects_Get_maximum_index( information );
+  local_table = information->local_table;
 
-  return maximum - inactive;
+  for ( index = 0; index < maximum; ++index ) {
+if ( local_table[ index ] != NULL ) {
+  ++active;
+}
+  }
+
+  return active;
 }
-- 
2.35.3

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel


[RTEMS 5 2/2] score: Fix unlimited objects support

2022-07-18 Thread Sebastian Huber
Commit 21275b58a5a69c3c838082ffc8a7a3641f32ea9a ("score: Static
Objects_Information initialization") introduced an off-by-one error in the
maintenance of inactive objects.

Close #4676.
---
 cpukit/include/rtems/score/objectimpl.h | 29 +
 cpukit/score/src/objectfree.c   | 12 +-
 2 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/cpukit/include/rtems/score/objectimpl.h 
b/cpukit/include/rtems/score/objectimpl.h
index c540f90166..ed0ce2aa10 100644
--- a/cpukit/include/rtems/score/objectimpl.h
+++ b/cpukit/include/rtems/score/objectimpl.h
@@ -937,6 +937,25 @@ RTEMS_INLINE_ROUTINE void _Objects_Free(
   ( *information->deallocate )( information, the_object );
 }
 
+/**
+ * @brief Returns true, if the object associated with the zero-based index is
+ *   contained in an allocated block of objects, otherwise false.
+ *
+ * @param index is the zero-based object index.
+ * @param objects_per_block is the object count per block.
+ *
+ * @retval true The object associated with the zero-based index is in an
+ *   allocated block of objects.
+ * @retval false Otherwise.
+ */
+RTEMS_INLINE_ROUTINE bool _Objects_Is_in_allocated_block(
+  Objects_Maximum index,
+  Objects_Maximum objects_per_block
+)
+{
+  return index >= objects_per_block;
+}
+
 /**
  * @brief Activate the object.
  *
@@ -952,15 +971,17 @@ RTEMS_INLINE_ROUTINE void _Objects_Activate_unlimited(
 )
 {
   Objects_Maximum objects_per_block;
-  Objects_Maximum block;
+  Objects_Maximum index;
 
   _Assert( _Objects_Is_auto_extend( information ) );
 
   objects_per_block = information->objects_per_block;
-  block = _Objects_Get_index( the_object->id ) - OBJECTS_INDEX_MINIMUM;
+  index = _Objects_Get_index( the_object->id ) - OBJECTS_INDEX_MINIMUM;
+
+  if ( _Objects_Is_in_allocated_block( index, objects_per_block ) ) {
+Objects_Maximum block;
 
-  if ( block > objects_per_block ) {
-block /= objects_per_block;
+block = index / objects_per_block;
 
 information->inactive_per_block[ block ]--;
 information->inactive--;
diff --git a/cpukit/score/src/objectfree.c b/cpukit/score/src/objectfree.c
index 1f7aa37e3f..964adbc62a 100644
--- a/cpukit/score/src/objectfree.c
+++ b/cpukit/score/src/objectfree.c
@@ -30,14 +30,16 @@ void _Objects_Free_unlimited(
 
   if ( _Objects_Is_auto_extend( information ) ) {
 Objects_Maximum objects_per_block;
-Objects_Maximum block;
-Objects_Maximum inactive;
+Objects_Maximum index;
 
 objects_per_block = information->objects_per_block;
-block = _Objects_Get_index( the_object->id ) - OBJECTS_INDEX_MINIMUM;
+index = _Objects_Get_index( the_object->id ) - OBJECTS_INDEX_MINIMUM;
 
-if ( block > objects_per_block ) {
-  block /= objects_per_block;
+if ( _Objects_Is_in_allocated_block( index, objects_per_block ) ) {
+  Objects_Maximum block;
+  Objects_Maximum inactive;
+
+  block = index / objects_per_block;
 
   ++information->inactive_per_block[ block ];
 
-- 
2.35.3

___
devel mailing list
devel@rtems.org
http://lists.rtems.org/mailman/listinfo/devel