Add comments.
[pintos-anon] / src / lib / bitmap.c
index d614c38fabef38e631d68716fca01e09462ce022..c5ca38ab43d182ce75a32d1d42bbf3725143dba4 100644 (file)
@@ -7,30 +7,52 @@
 #ifdef FILESYS
 #include "file.h"
 #endif
-
+\f
+/* Number of bits in an element. */
 #define ELEM_BITS (sizeof (elem_type) * CHAR_BIT)
-#define ELEM_IDX(BIT_IDX) ((BIT_IDX) / ELEM_BITS)
-#define BIT_MASK(BIT_IDX) ((elem_type) 1 << ((BIT_IDX) % ELEM_BITS))
 
+/* Returns the index of the element that contains the bit
+   numbered BIT_IDX. */
+static inline size_t
+elem_idx (size_t bit_idx) 
+{
+  return bit_idx / ELEM_BITS;
+}
+
+/* Returns an elem_type where only the bit corresponding to
+   BIT_IDX is turned on. */
+static inline elem_type
+bit_mask (size_t bit_idx) 
+{
+  return (elem_type) 1 << (bit_idx % ELEM_BITS);
+}
+
+/* Returns the number of elements required for B's bits. */
 static inline size_t
 elem_cnt (const struct bitmap *b) 
 {
   return DIV_ROUND_UP (b->bit_cnt, ELEM_BITS);
 }
 
+/* Returns the number of bytes required by B's elements. */
 static inline size_t
 byte_cnt (const struct bitmap *b) 
 {
   return sizeof (elem_type) * elem_cnt (b);
 }
 
+/* Returns a bit mask in which the bits actually used in the last
+   element of B's bits are set to 1 and the rest are set to 0. */
 static inline elem_type
 last_mask (const struct bitmap *b) 
 {
   int last_bits = b->bit_cnt % ELEM_BITS;
   return last_bits ? ((elem_type) 1 << last_bits) - 1 : (elem_type) -1;
 }
-
+\f
+/* Initializes B to be a bitmap of BIT_CNT bits.
+   Returns true if successfalse, false if memory allocation
+   failed. */
 bool
 bitmap_init (struct bitmap *b, size_t bit_cnt) 
 {
@@ -43,12 +65,7 @@ bitmap_init (struct bitmap *b, size_t bit_cnt)
   return true;
 }
 
-size_t
-bitmap_storage_size (const struct bitmap *b) 
-{
-  return byte_cnt (b);
-}
-
+/* Destroys bitmap B, freeing its storage. */
 void
 bitmap_destroy (struct bitmap *b) 
 {
@@ -57,23 +74,26 @@ bitmap_destroy (struct bitmap *b)
   free (b->bits);
 }
 
+/* Returns the number of bits in B. */
 size_t
 bitmap_size (const struct bitmap *b)
 {
   return b->bit_cnt;
 }
 
+/* Sets the bit numbered IDX in B to VALUE. */
 void
 bitmap_set (struct bitmap *b, size_t idx, bool value) 
 {
   ASSERT (b != NULL);
   ASSERT (idx < b->bit_cnt);
   if (value)
-    b->bits[ELEM_IDX (idx)] |= BIT_MASK (idx);
+    b->bits[elem_idx (idx)] |= bit_mask (idx);
   else
-    b->bits[ELEM_IDX (idx)] &= ~BIT_MASK (idx);
+    b->bits[elem_idx (idx)] &= ~bit_mask (idx);
 }
 
+/* Sets all bits in B to VALUE. */
 void
 bitmap_set_all (struct bitmap *b, bool value) 
 {
@@ -86,34 +106,42 @@ bitmap_set_all (struct bitmap *b, bool value)
   b->bits[elem_cnt (b) - 1] &= last_mask (b);
 }
 
+/* Sets the bit numbered IDX in B to true. */
 void
 bitmap_mark (struct bitmap *b, size_t idx) 
 {
   bitmap_set (b, idx, true);
 }
 
+/* Sets the bit numbered IDX in B to false. */
 void
 bitmap_reset (struct bitmap *b, size_t idx) 
 {
   bitmap_set (b, idx, false);
 }
 
+/* Toggles the bit numbered IDX in B;
+   that is, if it is true, makes it false,
+   and if it is false, makes it true. */
 void
 bitmap_flip (struct bitmap *b, size_t idx) 
 {
   ASSERT (b != NULL);
   ASSERT (idx < b->bit_cnt);
-  b->bits[ELEM_IDX (idx)] ^= BIT_MASK (idx);
+  b->bits[elem_idx (idx)] ^= bit_mask (idx);
 }
 
+/* Returns the value of the bit numbered IDX in B. */
 bool
 bitmap_test (const struct bitmap *b, size_t idx) 
 {
   ASSERT (b != NULL);
   ASSERT (idx < b->bit_cnt);
-  return (b->bits[ELEM_IDX (idx)] & BIT_MASK (idx)) != 0;
+  return (b->bits[elem_idx (idx)] & bit_mask (idx)) != 0;
 }
 
+/* Returns the smallest index of a bit set to VALUE in B.
+   If no bits in B are set to VALUE, returns BITMAP_ERROR. */
 size_t
 bitmap_scan (const struct bitmap *b, bool value) 
 {
@@ -140,6 +168,10 @@ bitmap_scan (const struct bitmap *b, bool value)
   return BITMAP_ERROR;
 }
 
+/* Finds the smallest index of a bit set to false in B,
+   sets it to true, and returns the index.
+   If no bits in B are set to false, changes no bits and
+   returns BITMAP_ERROR. */
 size_t
 bitmap_find_and_set (struct bitmap *b) 
 {
@@ -149,6 +181,10 @@ bitmap_find_and_set (struct bitmap *b)
   return idx;
 }
 
+/* Finds the smallest index of a bit set to true in B,
+   sets it to false, and returns the index.
+   If no bits in B are set to true, changes no bits and
+   returns BITMAP_ERROR. */
 size_t
 bitmap_find_and_clear (struct bitmap *b) 
 {
@@ -158,6 +194,7 @@ bitmap_find_and_clear (struct bitmap *b)
   return idx;
 }
 
+/* Returns the number of bits in B set to true. */
 size_t
 bitmap_set_cnt (const struct bitmap *b) 
 {
@@ -178,6 +215,8 @@ bitmap_set_cnt (const struct bitmap *b)
   return cnt;
 }
 
+/* Returns true if any bits in B are set to true,
+   and false otherwise.*/
 bool
 bitmap_any (const struct bitmap *b) 
 {
@@ -190,18 +229,23 @@ bitmap_any (const struct bitmap *b)
   return false;
 }
 
+/* Returns the number of bits in B set to false. */
 bool
 bitmap_clear_cnt (const struct bitmap *b) 
 {
   return b->bit_cnt - bitmap_set_cnt (b);
 }
 
+/* Returns true if no bits in B are set to true,
+   and false otherwise.*/
 bool
 bitmap_none (const struct bitmap *b) 
 {
   return !bitmap_any (b); 
 }
 
+/* Returns true if every bit in B is set to true,
+   and false otherwise. */
 bool
 bitmap_all (const struct bitmap *b) 
 {
@@ -219,6 +263,14 @@ bitmap_all (const struct bitmap *b)
 }
 
 #ifdef FILESYS
+/* Returns the number of bytes needed to store B in a file. */
+size_t
+bitmap_file_size (const struct bitmap *b) 
+{
+  return byte_cnt (b);
+}
+
+/* Reads FILE into B, ignoring errors. */
 void
 bitmap_read (struct bitmap *b, struct file *file) 
 {
@@ -226,6 +278,7 @@ bitmap_read (struct bitmap *b, struct file *file)
   b->bits[elem_cnt (b) - 1] &= last_mask (b);
 }
 
+/* Writes FILE to B, ignoring errors. */
 void
 bitmap_write (const struct bitmap *b, struct file *file)
 {
@@ -233,6 +286,7 @@ bitmap_write (const struct bitmap *b, struct file *file)
 }
 #endif /* FILESYS */
 
+/* Dumps the contents of B to the console as hexadecimal. */
 void
 bitmap_dump (const struct bitmap *b) 
 {