Index: trunk/kernel/libk/bits.c
===================================================================
--- trunk/kernel/libk/bits.c	(revision 635)
+++ trunk/kernel/libk/bits.c	(revision 657)
@@ -1,7 +1,7 @@
 /*
- * bits.c - bits manipulation functions implementation
+ * bits.c - bitmap API implementation
  *
  * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
- *         Alain Greiner    (2016,2017,2018,2019)
+ *         Alain Greiner    (2016,2017,2018,2019,2020)
  *
  * Copyright (c) UPMC Sorbonne Universites
@@ -26,4 +26,8 @@
 #include <bits.h>
 
+//////////////////////////////////////////////////////////////////////////////
+//////////////     local access functions     ///////////////////////////////
+//////////////////////////////////////////////////////////////////////////////
+
 ////////////////////////////////////
 void bitmap_init( bitmap_t * bitmap,
@@ -42,6 +46,6 @@
                         uint32_t   index )
 {
-	uint32_t  word = index / 32;
-	uint32_t  bit  = index % 32;
+	uint32_t  word = index >> 5;
+	uint32_t  bit  = index & 0x1F;
 
 	bitmap[word] |= ( 1 << bit );
@@ -52,6 +56,6 @@
                           uint32_t   index )
 {
-	uint32_t  word = index / 32;
-	uint32_t  bit  = index % 32;
+	uint32_t  word = index >> 5;
+	uint32_t  bit  = index & 0x1F;
 
 	bitmap[word] &= ~( 1 << bit );
@@ -62,9 +66,44 @@
                             uint32_t   index )
 {
-	uint32_t  word = index / 32;
-	uint32_t  bit  = index % 32;
+	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()
 
 //////////////////////////////////////////
@@ -207,4 +246,5 @@
 {
     uint32_t max_word;
+    uint32_t max_bit;
 	uint32_t word;
 	uint32_t bit;
@@ -216,7 +256,9 @@
         for( word = 0 ; word < max_word ; word++ )
 	    {
+            max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32;
+
 		    if(bitmap[word] != 0)
 		    {
-			    for(bit = 0 ; bit < 32 ; bit++)
+			    for(bit = 0 ; bit < max_bit ; bit++)
 			    {
 				    if( bitmap[word] & (1 << bit) ) return (word*32 + bit);
@@ -235,4 +277,5 @@
 {
     uint32_t max_word;
+    uint32_t max_bit;
 	uint32_t word;
 	uint32_t bit;
@@ -244,7 +287,9 @@
         for( word = 0 ; word < max_word ; word++ )
 	    {
+            max_bit = (word == (max_word - 1)) ? (size & 0x1F) : 32;
+
 		    if(bitmap[word] != 0XFFFFFFFF)
 		    {
-			    for(bit = 0 ; bit < 32 ; bit++)
+			    for(bit = 0 ; bit < max_bit ; bit++)
 			    {
 				    if( (bitmap[word] & (1 << bit)) == 0 ) return (word*32 + bit);
@@ -258,2 +303,131 @@
 }  // 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()
+
+
