Index: trunk/kernel/libk/bits.c
===================================================================
--- trunk/kernel/libk/bits.c	(revision 1)
+++ trunk/kernel/libk/bits.c	(revision 11)
@@ -81,5 +81,5 @@
 		}
 	}
-}
+}  // bitmap_set_range()
 
 ///////////////////////////////////////////
@@ -108,5 +108,5 @@
 		}
 	}
-}
+}  // bitmap_clear_range()
 
 ///////////////////////////////////////
@@ -115,28 +115,36 @@
                       uint32_t   size )
 {
+    uint32_t max_word;
 	uint32_t word = index / 32;  
 	uint32_t bit  = index % 32; 
  
-	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;
-}
+    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()
 
 ///////////////////////////////////////
@@ -145,28 +153,36 @@
                       uint32_t   size )
 {
+    uint32_t max_word;
 	uint32_t word = index / 32;
 	uint32_t bit  = index % 32;
- 
-	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;
-}
+    
+    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()
 
 //////////////////////////////////////
@@ -174,38 +190,54 @@
                      uint32_t   size )
 {
+    uint32_t max_word;
+	uint32_t word;
+	uint32_t bit;
+
+    if( size )
+    {
+        max_word = ( (size-1) >>5 ) + 1;
+
+        for( word = 0 ; 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_ffs()
+
+//////////////////////////////////////
+uint32_t bitmap_ffc( bitmap_t * bitmap, 
+                     uint32_t   size )
+{
+    uint32_t max_word;
 	uint32_t word;
 	uint32_t bit;
  
-	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;
-}
-
-//////////////////////////////////////
-uint32_t bitmap_ffc( bitmap_t * bitmap, 
-                     uint32_t   size )
-{
-	uint32_t word;
-	uint32_t bit;
- 
-	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;
-}
-
+    if( size )
+    {
+        max_word = ( (size-1) >>5 ) + 1;
+
+        for( word = 0 ; 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_ffc()
+
