Index: trunk/kernel/libk/bits.h
===================================================================
--- trunk/kernel/libk/bits.h	(revision 690)
+++ trunk/kernel/libk/bits.h	(revision 1)
@@ -1,7 +1,7 @@
 /*
- * bits.h - bitmap API definition
+ * bits.h - bits manipulation helper functions
  *
  * Author   Ghassan Almaless (2008,2009,2010,2011,2012)
- *          Alain Greiner    (2016,2017,2018,2019,2020)
+ *          Alain Greiner    (2016)
  *
  * Copyright (c) UPMC Sorbonne Universites
@@ -26,23 +26,17 @@
 #define _BITS_H_
 
-#include <kernel_config.h>
-#include <hal_kernel_types.h>
-#include <hal_remote.h>
-
-/**********************************************************************************************
- * This file defines the API to access a generic bitmap, that can be local or remote.
- * It is implemented as an array of uint32_t words.
- * The number of entries in this array is statically defined at compile time
- * and defines the max number of items that can be registered in the bitmap.
- * The remote accesses are used in the VFS by the inum allocator.
- *********************************************************************************************/
-
-typedef uint32_t    bitmap_t;
-
-/**********************************************************************************************
- * This macro returns the number of 32 bits words required to register <size> entries.
- *********************************************************************************************/
-
-#define BITMAP_SIZE(size) ( ((size) & 31) ? (((size)>>5) + 1) : ((size)>>5) )
+#include <almos_config.h>
+#include <hal_types.h>
+
+/*********************************************************************************************
+ * These macros are NOT used by the bitmat, but can be useful in other contexts... [AG]
+ *********************************************************************************************/
+
+#define ARROUND_UP(val, size) (((val) & ((size) -1)) ? ((val) & ~((size)-1)) + (size) : (val))
+#define ARROUND_DOWN(val, size)  ((val) & ~((size) - 1))
+
+#define ABS(x) (((x) < 0) ? -(x) : (x))
+#define MIN(x,y) (((x) < (y)) ? (x) : (y))
+#define MAX(x,y) (((x) < (y)) ? (y) : (x))
 
 /**********************************************************************************************
@@ -50,5 +44,4 @@
  * It returns 0xFFFFFFFF if data is larger than 0x80000000.
  *********************************************************************************************/
-
 #define POW2_ROUNDUP(data) ( (data <= 0x00000001) ? 0x00000001  : \
                              (data <= 0x00000002) ? 0x00000002  : \
@@ -84,165 +77,141 @@
                              (data <= 0x80000000) ? 0x80000000  : 0xFFFFFFFF )
 
-/*********************************************************************************************
- * These macros are NOT used by the bitmap, but are useful in other contexts... [AG]
- *********************************************************************************************/
-
-#define ARROUND_UP(val, size) (((val) & ((size) -1)) ? ((val) & ~((size)-1)) + (size) : (val))
-#define ARROUND_DOWN(val, size)  ((val) & ~((size) - 1))
-
-#define ABS(x) (((x) < 0) ? -(x) : (x))
-#define MIN(x,y) (((x) < (y)) ? (x) : (y))
-#define MAX(x,y) (((x) < (y)) ? (y) : (x))
-
-/*********************************************************************************************
- * This function reset all bits in a local or remote bitmap. 
- *********************************************************************************************
- * @ bitmap  : pointer on first word in the bitmap.
- * @ size    : number of bits in bitmap. 
- ********************************************************************************************/
-extern void bitmap_init( bitmap_t * bitmap,
-                         uint32_t   size );
-
-extern void bitmap_remote_init( xptr_t   bitmap_xp,
-                                uint32_t size );
-
-/*********************************************************************************************
- * These functions set a specific bit in a local or remote bitmap.
- *********************************************************************************************
+/**********************************************************************************************
+ * This macro returns the number of 32 bits words required to register size entries.
+ *********************************************************************************************/
+
+#define BITMAP_SIZE(size) ( ((size) & 31) ? (((size)>>5) + 1) : ((size)>>5) )
+ 
+/**********************************************************************************************
+ * This macro declare a bitmap data structure. It is actually an array of uint32_t.
+ * size is the total number of entries in the bitmap.
+ *********************************************************************************************/
+
+#define BITMAP( name , size )     uint32_t name[BITMAP_SIZE(size)]
+
+typedef uint32_t    bitmap_t;
+
+/**********************************************************************************************
+ * This function set a specific bit in a bitmap.
+ **********************************************************************************************
+
  * @ bitmap  : pointer on the bitmap
  * @ index   : bit index in the bitmap
- ********************************************************************************************/
+ *********************************************************************************************/
 extern inline void bitmap_set( bitmap_t * bitmap,
                                uint32_t   index );
 
-extern inline void bitmap_remote_set( xptr_t    bitmap_xp,
-                                      uint32_t  index );
-
-/*********************************************************************************************
- * These functions clear a specific bit in a local or remote bitmap.
- *********************************************************************************************
+/**********************************************************************************************
+ * This function clear a specific bit in a bitmap.
+ **********************************************************************************************
  * @ bitmap  : pointer on the bitmap
  * @ index   : bit index in the bitmap
- ********************************************************************************************/
+ *********************************************************************************************/
 extern inline void bitmap_clear( bitmap_t * bitmap, 
                                  uint32_t   index );
 
-extern inline void bitmap_remote_clear( xptr_t     bitmap_xp, 
-                                        uint32_t   index );
-
-/*********************************************************************************************
- * These functions search the first bit non-set in a local or remote bitmap, in the
- * range [0 , size-1], set this bit, and return the index of the found bit.
- * The lock protecting the bitmap must be taken by the caller.
- *********************************************************************************************
- * @ bitmap  : pointer on the bitmap.
- * @ size    : number of bits to scan.
- * @ returns index of found bit / returns 0xFFFFFFFF if not found.
- ********************************************************************************************/
-extern uint32_t bitmap_alloc( bitmap_t * bitmap, 
-                              uint32_t   size );
-
-extern uint32_t bitmap_remote_alloc( xptr_t    bitmap_xp,
-                                     uint32_t  size );
-
-/*********************************************************************************************
- * This function returns the index of aa specific bit in a bitmap.
- *********************************************************************************************
+/**********************************************************************************************
+ * This function returns a specific bit in a bitmap.
+ **********************************************************************************************
  * @ bitmap  : pointer on the bitmap
  * @ index   : bit index in the bitmap
- * @ returns true if bitmap[index] is set
- ********************************************************************************************/
+ * @ returns tru if bitmap[index] is set
+ *********************************************************************************************/
 extern inline bool_t bitmap_state( bitmap_t * bitmap, 
                                    uint32_t   index );
 
-/*********************************************************************************************
+/**********************************************************************************************
  * This function set a range of bits in a bitmap : [index ... (index + len)[ 
- *********************************************************************************************
+ **********************************************************************************************
  * @ bitmap  : pointer on the bitmap
  * @ index   : first bit index in the bitmap
  * @ len     : number of bits to set
- ********************************************************************************************/
+ *********************************************************************************************/
 extern void bitmap_set_range( bitmap_t * bitmap, 
                               uint32_t   index, 
                               uint32_t   len );
 
-/*********************************************************************************************
+/**********************************************************************************************
  * This function reset a range of bits in a bitmap : [index ... (index + len)[ 
- *********************************************************************************************
+ **********************************************************************************************
  * @ bitmap  : pointer on the bitmap
  * @ index   : first bit index in the bitmap
  * @ len     : number of bits to clear
- ********************************************************************************************/
+ *********************************************************************************************/
 extern void bitmap_clear_range( bitmap_t * bitmap, 
                                 uint32_t   index, 
                                 uint32_t   len );
 
-/*********************************************************************************************
+/**********************************************************************************************
  * This function returns the index of first bit set in a bitmap, starting from index.
- *********************************************************************************************
+ **********************************************************************************************
  * @ bitmap  : pointer on the bitmap
  * @ index   : first bit to analyse in the bitmap
  * @ size    : number of bits to analyse in bitmap
  * @ returns index if found / returns 0xFFFFFFFF if bit not found
- ********************************************************************************************/
+ *********************************************************************************************/
 extern uint32_t bitmap_ffs2( bitmap_t * bitmap,
                              uint32_t   index, 
                              uint32_t   size );
 
-/*********************************************************************************************
+/**********************************************************************************************
  * This function returns the index of first bit cleared in a bitmap, starting from index.
- *********************************************************************************************
+ **********************************************************************************************
  * @ bitmap  : pointer on the bitmap
  * @ index   : first bit to analyse in the bitmap
  * @ size    : number of bits to analyse in bitmap
  * @ returns index if found / returns 0xFFFFFFFF if bit not found
- ********************************************************************************************/
+ *********************************************************************************************/
 extern uint32_t bitmap_ffc2( bitmap_t * bitmap,
                              uint32_t   index, 
                              uint32_t   size );
 
-/*********************************************************************************************
+/**********************************************************************************************
  * This function returns the index of first bit set in a bitmap, starting from bit 0.
- *********************************************************************************************
+ **********************************************************************************************
  * @ bitmap  : pointer on the bitmap
  * @ size    : number of bits to analyse in bitmap
  * @ returns index if found / returns 0xFFFFFFFF if bit not found
- ********************************************************************************************/
+ *********************************************************************************************/
 extern uint32_t bitmap_ffs( bitmap_t * bitmap,
                             uint32_t   size );
 
-/*********************************************************************************************
- * These functions return the index of first bit cleared in a local or remote bitmap,
- * starting from bit 0.
- *********************************************************************************************
- * @ bitmap  : pointer on the bitmap
- * @ size    : number of bits to analyse in bitmap
- * @ returns index if found / returns 0xFFFFFFFF if bit not found
- ********************************************************************************************/
+/**********************************************************************************************
+ * This function returns the index of first bit cleared in a bitmap, starting from bit 0.
+ **********************************************************************************************
+ * @ bitmap  : pointer on the bitmap
+ * @ size    : number of bits to alalyse in bitmap
+ * @ returns index if found / returns 0xFFFFFFFF if bit not found
+ *********************************************************************************************/
 extern uint32_t bitmap_ffc( bitmap_t * bitmap, 
                             uint32_t   size );
 
-extern uint32_t bitmap_remote_ffc( xptr_t   bitmap_xp, 
-                                   uint32_t size );
-
-/*********************************************************************************************
- * This function takes a positive integer <val> as input argument, and returns the smallest
- * integer <order> such as : 1<<order >= val. 
- * In other words, <order> is the min number of bits to encode the <val> values.
- *********************************************************************************************
+/**********************************************************************************************
+ * This function returns the number of bits to code a non-zero unsigned integer value.
+ **********************************************************************************************
+ * @ val   : value to analyse
+ * @ returns number of bits
+ *********************************************************************************************/
+static inline uint32_t bits_nr( uint32_t val )
+{
+	register uint32_t i;
+
+	for( i=0 ; val > 0 ; i++ ) 
+		val = val >> 1;
+
+	return i;
+}
+
+/**********************************************************************************************
+ * This function takes an unsigned integer value as input argument, and returns another
+ * unsigned integer, that is the (base 2) logarithm of the smallest power of 2 contained 
+ * in the input value.
+ **********************************************************************************************
  * @ val   : value to analyse
  * @ returns logarithm value
- ********************************************************************************************/
+ *********************************************************************************************/
 static inline uint32_t bits_log2( uint32_t val )
 {
-    uint32_t i;
-
-    if( val > 0 )
-    {
-        val--;
-        for( i=0 ; val > 0 ; i++ ) val = val >> 1;
-        return i;
-    }
-    return 0;
+	return (val == 0) ? 1 : bits_nr( val ) - 1;
 }
 
