Index: trunk/kernel/libk/bits.c
===================================================================
--- trunk/kernel/libk/bits.c	(revision 690)
+++ trunk/kernel/libk/bits.c	(revision 1)
@@ -1,7 +1,7 @@
 /*
- * bits.c - bitmap API implementation
+ * bits.c - bits manipulation functions implementation
  *
  * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
- *         Alain Greiner    (2016,2017,2018,2019,2020)
+ *         Alain Greiner    (2016)
  *
  * Copyright (c) UPMC Sorbonne Universites
@@ -23,22 +23,6 @@
  */
 
-#include <hal_kernel_types.h>
+#include <hal_types.h>
 #include <bits.h>
-
-//////////////////////////////////////////////////////////////////////////////
-//////////////     local access functions     ///////////////////////////////
-//////////////////////////////////////////////////////////////////////////////
-
-////////////////////////////////////
-void bitmap_init( bitmap_t * bitmap,
-                  uint32_t   len )
-{
-    uint32_t word;
-    uint32_t nwords = BITMAP_SIZE( len );
-    for( word = 0 ; word < nwords ; word++ )
-    {
-        bitmap[word] = 0;
-    }
-}  // end bitmap_init()
 
 //////////////////////////////////////////
@@ -46,6 +30,6 @@
                         uint32_t   index )
 {
-	uint32_t  word = index >> 5;
-	uint32_t  bit  = index & 0x1F;
+	uint32_t  word = index / 32;
+	uint32_t  bit  = index % 32;
 
 	bitmap[word] |= ( 1 << bit );
@@ -56,6 +40,6 @@
                           uint32_t   index )
 {
-	uint32_t  word = index >> 5;
-	uint32_t  bit  = index & 0x1F;
+	uint32_t  word = index / 32;
+	uint32_t  bit  = index % 32;
 
 	bitmap[word] &= ~( 1 << bit );
@@ -66,44 +50,9 @@
                             uint32_t   index )
 {
-	uint32_t  word = index >> 5;
-	uint32_t  bit  = index & 0x1F;
-
-	return (bitmap[word] & ( 1 << bit )) != 0;
-}
-
-/////////////////////////////////////////
-uint32_t bitmap_alloc( bitmap_t * bitmap,
-                       uint32_t   size )
-{
-    uint32_t max_word;
-    uint32_t max_bit; 
-	uint32_t word;
-	uint32_t bit;
- 
-    if( size )
-    {
-        max_word = ( (size-1) >>5 ) + 1;
-
-        for( word = 0 ; word < max_word ; word++ )
-	    {
-            max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32;
-
-		    if(bitmap[word] != 0XFFFFFFFF)
-		    {
-			    for(bit = 0 ; bit < max_bit ; bit++)
-			    {
-				    if( (bitmap[word] & (1 << bit)) == 0 )
-                    {
-                        bitmap[word] |= (1 << bit);
-                        return (word*32 + bit);
-                    }
-			    }
-            }
-		}
-	}
-
-	return -1;
-
-}  // end bitmap_alloc()
+	uint32_t  word = index / 32;
+	uint32_t  bit  = index % 32;
+
+	return (bitmap[word] & ( 1 << bit )) && 1;
+}
 
 //////////////////////////////////////////
@@ -120,7 +69,5 @@
 		if((len + bit) >= 32)
 		{
-            if( bit == 0 ) val = 0xFFFFFFFF;
-            else           val = (uint32_t)((1 << (32 - bit)) - 1);
-
+			val = (bit == 0) ? 0xFFFFFFFF : (1 << (32 - bit)) - 1;
 			bitmap[word] |= (val << bit);
 			word++;
@@ -134,5 +81,5 @@
 		}
 	}
-}  // bitmap_set_range()
+}
 
 ///////////////////////////////////////////
@@ -149,7 +96,5 @@
 		if((len + bit) >= 32)
 		{
-            if( bit == 0 ) val = 0xFFFFFFFF;
-            else           val = (uint32_t)((1 << (32 - bit)) - 1);
-
+			val = (bit == 0) ? 0xFFFFFFFF : (1 << (32 - bit)) - 1;
 			bitmap[word] &= ~(val << bit);
 			word++;
@@ -163,5 +108,5 @@
 		}
 	}
-}  // bitmap_clear_range()
+}
 
 ///////////////////////////////////////
@@ -170,36 +115,28 @@
                       uint32_t   size )
 {
-    uint32_t max_word;
 	uint32_t word = index / 32;  
 	uint32_t bit  = index % 32; 
  
-    if( index < size )
-    {
-	    if( bit != 0 )
-	    {
-		    for( ; bit < 32; bit++)
-		    {
-			    if((bitmap[word] & (1 << bit)) ) return (word*32 + bit);
-	  	    }
-		    word++;
-	    }
-
-        max_word = ( (size-1) >>5 ) + 1;
-
-        for( ; word < max_word ; word++ )
-	    {
-		    if(bitmap[word] != 0)
-		    {
-			    for(bit = 0 ; bit < 32 ; bit++)
-			    {
-				    if( bitmap[word] & (1 << bit) ) return (word*32 + bit);
-			    }
-            }
-		}
-	}
-
-	return -1;
-
-}  // bitmap_ffs2()
+	if(bit != 0)
+	{
+		for(; bit < 32; bit++)
+		{
+			if(bitmap[word] & (1 << bit)) return (word*32 + bit);
+		}
+		word++;
+	}
+
+	for(; word < size/32; word++)
+	{
+		if(bitmap[word] != 0)
+		{
+			for(bit = 0; bit < 32; bit++)
+			{
+				if(bitmap[word] & (1 << bit)) return (word*32 + bit);
+			}
+		}
+	}
+	return 0xFFFFFFFF;
+}
 
 ///////////////////////////////////////
@@ -208,36 +145,28 @@
                       uint32_t   size )
 {
-    uint32_t max_word;
 	uint32_t word = index / 32;
 	uint32_t bit  = index % 32;
-    
-    if( index < size )
-    {
-	    if( bit != 0 )
-	    {
-		    for( ; bit < 32; bit++)
-		    {
-			    if( (bitmap[word] & (1 << bit)) == 0) return (word*32 + bit);
-	  	    }
-		    word++;
-	    }
-
-        max_word = ( (size-1) >>5 ) + 1;
-
-        for( ; word < max_word ; word++ )
-	    {
-		    if(bitmap[word] != 0xFFFFFFFF)
-		    {
-			    for(bit = 0 ; bit < 32 ; bit++)
-			    {
-				    if( (bitmap[word] & (1 << bit)) == 0 ) return (word*32 + bit);
-			    }
-            }
-		}
-	}
-
-	return -1;
-
-}  // bitmap_ffc2()
+ 
+	if(bit != 0)
+	{
+		for(; bit < 32; bit++)
+		{
+			if((bitmap[word] & (1 << bit)) == 0) return (word*32 + bit);
+		}
+		word++;
+	}
+
+	for(; word < size/32; word++)
+	{
+		if(bitmap[word] != 0xFFFFFFFF)
+		{
+			for(bit = 0; bit < 32; bit++)
+			{
+				if((bitmap[word] & (1 << bit)) == 0) return (word*32 + bit);
+			}
+		}
+	}
+	return 0xFFFFFFFF;
+}
 
 //////////////////////////////////////
@@ -245,30 +174,19 @@
                      uint32_t   size )
 {
-    uint32_t max_word;
-    uint32_t max_bit;
 	uint32_t word;
 	uint32_t bit;
-
-    if( size )
-    {
-        max_word = ( (size-1) >>5 ) + 1;
-
-        for( word = 0 ; word < max_word ; word++ )
-	    {
-            max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32;
-
-		    if(bitmap[word] != 0)
-		    {
-			    for(bit = 0 ; bit < max_bit ; bit++)
-			    {
-				    if( bitmap[word] & (1 << bit) ) return (word*32 + bit);
-			    }
-            }
-		}
-	}
-
-	return -1;
-
-}  // bitmap_ffs()
+ 
+	for(word = 0 ; word < size/32 ; word++)
+	{
+		if(bitmap[word] != 0)
+		{
+			for(bit = 0 ; bit < 32 ; bit++)
+			{
+				if( bitmap[word] & (1 << bit) ) return (word*32 + bit);
+			}
+		}
+	}
+	return 0xFFFFFFFF;
+}
 
 //////////////////////////////////////
@@ -276,158 +194,18 @@
                      uint32_t   size )
 {
-    uint32_t max_word;
-    uint32_t max_bit;
 	uint32_t word;
 	uint32_t bit;
  
-    if( size )
-    {
-        max_word = ( (size-1) >>5 ) + 1;
-
-        for( word = 0 ; word < max_word ; word++ )
-	    {
-            max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32;
-
-		    if(bitmap[word] != 0XFFFFFFFF)
-		    {
-			    for(bit = 0 ; bit < max_bit ; bit++)
-			    {
-				    if( (bitmap[word] & (1 << bit)) == 0 ) return (word*32 + bit);
-			    }
-            }
-		}
-	}
-
-	return -1;
-
-}  // bitmap_ffc()
-
-
-//////////////////////////////////////////////////////////////////////////////
-//////////////     remote access functions     ///////////////////////////////
-//////////////////////////////////////////////////////////////////////////////
-
-////////////////////////////////////////////
-void bitmap_remote_init( xptr_t   bitmap_xp,
-                         uint32_t len )
-{
-    bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp );
-    cxy_t      bitmap_cxy = GET_CXY( bitmap_xp );
-
-    uint32_t word;
-    uint32_t nwords = BITMAP_SIZE( len );
-
-    for( word = 0 ; word < nwords ; word++ )
-    {
-        hal_remote_s32( XPTR( bitmap_cxy , &bitmap_ptr[word] ) , 0 );
-    }
-}
-
-////////////////////////////////////////////////////
-inline void bitmap_remote_set( xptr_t     bitmap_xp,
-                               uint32_t   index )
-{
-    bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp );
-    cxy_t      bitmap_cxy = GET_CXY( bitmap_xp );
-
-	uint32_t  word = index / 32;
-	uint32_t  bit  = index % 32;
-
-    hal_remote_atomic_or( XPTR( bitmap_cxy , &bitmap_ptr[word] ) , (1 <<bit) );
-}
-
-//////////////////////////////////////////////////////
-inline void bitmap_remote_clear( xptr_t     bitmap_xp, 
-                                 uint32_t   index )
-{
-    bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp );
-    cxy_t      bitmap_cxy = GET_CXY( bitmap_xp );
-
-	uint32_t  word = index / 32;
-	uint32_t  bit  = index % 32;
-
-    hal_remote_atomic_and( XPTR( bitmap_cxy , &bitmap_ptr[word] ) , ~(1 <<bit) );
-}
-
-///////////////////////////////////////////////////
-uint32_t bitmap_remote_alloc( xptr_t     bitmap_xp,
-                              uint32_t   size )
-{
-    uint32_t max_word;
-    uint32_t max_bit; 
-	uint32_t word;
-	uint32_t bit;
-    xptr_t   word_xp;
-    uint32_t value;
- 
-    bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp );
-    cxy_t      bitmap_cxy = GET_CXY( bitmap_xp );
-
-    if( size )
-    {
-        max_word = ( (size-1) >>5 ) + 1;
-
-        for( word = 0 ; word < max_word ; word++ )
-	    {
-            max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32;
-
-            word_xp = XPTR( bitmap_cxy , &bitmap_ptr[word] );
-
-            value   = hal_remote_l32( word_xp );
-
-		    if( value != 0XFFFFFFFF )
-		    {
-			    for(bit = 0 ; bit < max_bit ; bit++)
-			    {
-				    if( (value & (1 << bit)) == 0 )
-                    {
-                        hal_remote_s32( word_xp , value | (1 << bit) );
-                        return (word*32 + bit);
-                    }
-			    }
-            }
-		}
-	}
-
-	return -1;
-
-}  // end bitmap_alloc()
-
-///////////////////////////////////////////////
-uint32_t bitmap_remote_ffc( xptr_t   bitmap_xp, 
-                                     uint32_t size )
-{
-    uint32_t max_word;
-    uint32_t max_bit;
-	uint32_t word;
-	uint32_t bit;
-    uint32_t value;
- 
-    bitmap_t * bitmap_ptr = GET_PTR( bitmap_xp );
-    cxy_t      bitmap_cxy = GET_CXY( bitmap_xp );
-
-    if( size )
-    {
-        max_word = ( (size-1) >>5 ) + 1;
-
-        for( word = 0 ; word < max_word ; word++ )
-	    {
-            max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32;
-
-            value = hal_remote_l32( XPTR( bitmap_cxy , &bitmap_ptr[word] ) );
-
-		    if( value != 0xFFFFFFFF )
-		    {
-			    for(bit = 0 ; bit < max_bit ; bit++)
-			    {
-				    if( (value & (1 << bit)) == 0 ) return (word*32 + bit);
-			    }
-            }
-		}
-	}
-
-	return -1;
-
-}  // bitmap_remote_ffc()
-
-
+	for(word = 0 ; word < size/32 ; word++)
+	{
+		if(bitmap[word] != 0)
+		{
+			for(bit = 0 ; bit < 32 ; bit++)
+			{
+				if( (bitmap[word] & (1 << bit)) == 0 ) return (word*32 + bit);
+			}
+		}
+	}
+	return 0xFFFFFFFF;
+}
+
