X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flib%2Fkernel%2Fbitmap.c;h=88a5e7e584828188be569b5b701253ab81df51a9;hb=4d02c49e21ba884fd5d3dc092b3834641e3b742a;hp=3690a3a37c71500efeaa031fdaa2aeff2971c5dc;hpb=7106fafb92091fe9f85fbe051ed5bed5a9ff805a;p=pintos-anon diff --git a/src/lib/kernel/bitmap.c b/src/lib/kernel/bitmap.c index 3690a3a..88a5e7e 100644 --- a/src/lib/kernel/bitmap.c +++ b/src/lib/kernel/bitmap.c @@ -46,18 +46,18 @@ bit_mask (size_t bit_idx) return (elem_type) 1 << (bit_idx % ELEM_BITS); } -/* Returns the number of elements required for B's bits. */ +/* Returns the number of elements required for BIT_CNT bits. */ static inline size_t -elem_cnt (const struct bitmap *b) +elem_cnt (size_t bit_cnt) { - return DIV_ROUND_UP (b->bit_cnt, ELEM_BITS); + return DIV_ROUND_UP (bit_cnt, ELEM_BITS); } -/* Returns the number of bytes required by B's elements. */ +/* Returns the number of bytes required for BIT_CNT bits. */ static inline size_t -byte_cnt (const struct bitmap *b) +byte_cnt (size_t bit_cnt) { - return sizeof (elem_type) * elem_cnt (b); + return sizeof (elem_type) * elem_cnt (bit_cnt); } /* Returns a bit mask in which the bits actually used in the last @@ -69,8 +69,9 @@ last_mask (const struct bitmap *b) return last_bits ? ((elem_type) 1 << last_bits) - 1 : (elem_type) -1; } -/* Initializes B to be a bitmap of BIT_CNT bits. - Returns true if successfalse, false if memory allocation +/* Initializes B to be a bitmap of BIT_CNT bits + and sets all of its bits to false. + Returns true if success, false if memory allocation failed. */ struct bitmap * bitmap_create (size_t bit_cnt) @@ -79,7 +80,7 @@ bitmap_create (size_t bit_cnt) if (b != NULL) { b->bit_cnt = bit_cnt; - b->bits = malloc (byte_cnt (b)); + b->bits = malloc (byte_cnt (bit_cnt)); if (b->bits != NULL || bit_cnt == 0) { bitmap_set_all (b, false); @@ -90,7 +91,9 @@ bitmap_create (size_t bit_cnt) return NULL; } -/* Destroys bitmap B, freeing its storage. */ +/* Destroys bitmap B, freeing its storage. + Not for use on bitmaps created by + bitmap_create_preallocated(). */ void bitmap_destroy (struct bitmap *b) { @@ -108,57 +111,80 @@ bitmap_size (const struct bitmap *b) return b->bit_cnt; } -/* Sets the bit numbered IDX in B to VALUE. */ +/* Atomically 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); + bitmap_mark (b, idx); else - b->bits[elem_idx (idx)] &= ~bit_mask (idx); + bitmap_reset (b, idx); } /* Sets all bits in B to VALUE. */ void bitmap_set_all (struct bitmap *b, bool value) +{ + ASSERT (b != NULL); + + bitmap_set_multiple (b, 0, bitmap_size (b), value); +} + +/* Sets the CNT bits starting at START in B to VALUE. */ +void +bitmap_set_multiple (struct bitmap *b, size_t start, size_t cnt, bool value) { size_t i; ASSERT (b != NULL); + ASSERT (start <= b->bit_cnt); + ASSERT (start + cnt <= b->bit_cnt); - if (b->bit_cnt > 0) - { - for (i = 0; i < elem_cnt (b); i++) - b->bits[i] = value ? (elem_type) -1 : 0; - b->bits[elem_cnt (b) - 1] &= last_mask (b); - } + for (i = 0; i < cnt; i++) + bitmap_set (b, start + i, value); } -/* Sets the bit numbered IDX in B to true. */ +/* Atomically sets the bit numbered BIT_IDX in B to true. */ void -bitmap_mark (struct bitmap *b, size_t idx) +bitmap_mark (struct bitmap *b, size_t bit_idx) { - bitmap_set (b, idx, true); + size_t idx = elem_idx (bit_idx); + elem_type mask = bit_mask (bit_idx); + + /* This is equivalent to `b->bits[idx] |= mask' except that it + is guaranteed to be atomic on a uniprocessor machine. See + the description of the OR instruction in [IA32-v2b]. */ + asm ("orl %1, %0" : "=m" (b->bits[idx]) : "r" (mask) : "cc"); } -/* Sets the bit numbered IDX in B to false. */ +/* Atomically sets the bit numbered BIT_IDX in B to false. */ void -bitmap_reset (struct bitmap *b, size_t idx) +bitmap_reset (struct bitmap *b, size_t bit_idx) { - bitmap_set (b, idx, false); + size_t idx = elem_idx (bit_idx); + elem_type mask = bit_mask (idx); + + /* This is equivalent to `b->bits[idx] &= ~mask' except that it + is guaranteed to be atomic on a uniprocessor machine. See + the description of the AND instruction in [IA32-v2a]. */ + asm ("andl %1, %0" : "=m" (b->bits[idx]) : "r" (~mask) : "cc"); } -/* Toggles the bit numbered IDX in B; +/* Atomically 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) +bitmap_flip (struct bitmap *b, size_t bit_idx) { - ASSERT (b != NULL); - ASSERT (idx < b->bit_cnt); - b->bits[elem_idx (idx)] ^= bit_mask (idx); + size_t idx = elem_idx (bit_idx); + elem_type mask = bit_mask (idx); + + /* This is equivalent to `b->bits[idx] ^= mask' except that it + is guaranteed to be atomic on a uniprocessor machine. See + the description of the XOR instruction in [IA32-v2b]. */ + asm ("xorl %1, %0" : "=m" (b->bits[idx]) : "r" (mask) : "cc"); } /* Returns the value of the bit numbered IDX in B. */ @@ -170,126 +196,100 @@ bitmap_test (const struct bitmap *b, size_t idx) 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) +/* Returns true if any of the CNT bits starting at START is set + to VALUE. */ +static bool +contains (const struct bitmap *b, size_t start, size_t cnt, bool value) { - elem_type ignore = value ? 0 : (elem_type) -1; - size_t idx; + size_t i; ASSERT (b != NULL); - for (idx = 0; idx < elem_cnt (b); idx++) - { - elem_type e = b->bits[idx]; - if (e != ignore) - { - idx *= ELEM_BITS; - - while ((e & 1) != value) - { - e >>= 1; - idx++; - } + ASSERT (start <= b->bit_cnt); + ASSERT (start + cnt <= b->bit_cnt); - return idx < b->bit_cnt ? idx : BITMAP_ERROR; - } - } - return BITMAP_ERROR; + for (i = 0; i < cnt; i++) + if (bitmap_test (b, start + i) == value) + return true; + return false; } -/* 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. */ +/* Finds and returns the starting index of the first group of CNT + consecutive bits in B at or after START that are all set to + VALUE. + If there is no such group, returns BITMAP_ERROR. */ size_t -bitmap_find_and_set (struct bitmap *b) +bitmap_scan (const struct bitmap *b, size_t start, size_t cnt, bool value) { - size_t idx = bitmap_scan (b, false); - if (idx != BITMAP_ERROR) - bitmap_mark (b, idx); - return idx; + ASSERT (b != NULL); + ASSERT (start <= b->bit_cnt); + + if (cnt <= b->bit_cnt) + { + size_t last = b->bit_cnt - cnt; + size_t i; + for (i = start; i <= last; i++) + if (!contains (b, i, cnt, !value)) + return i; + } + return BITMAP_ERROR; } -/* 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. */ +/* Finds the first group of CNT consecutive bits in B at or after + START that are all set to VALUE, flips them all to !VALUE, + and returns the index of the first bit in the group. + If there is no such group, returns BITMAP_ERROR. + If CNT is zero, returns 0. + Bits are set atomically, but testing bits is not atomic with + setting them. */ size_t -bitmap_find_and_clear (struct bitmap *b) +bitmap_scan_and_flip (struct bitmap *b, size_t start, size_t cnt, bool value) { - size_t idx = bitmap_scan (b, true); + size_t idx = bitmap_scan (b, start, cnt, value); if (idx != BITMAP_ERROR) - bitmap_reset (b, idx); + bitmap_set_multiple (b, idx, cnt, !value); return idx; } -/* Returns the number of bits in B set to true. */ +/* Returns the number of bits in B between START and START + CNT, + exclusive, that are set to VALUE. */ size_t -bitmap_set_cnt (const struct bitmap *b) -{ - size_t cnt; - size_t i; - - ASSERT (b != NULL); - cnt = 0; - for (i = 0; i < elem_cnt (b); i++) - { - elem_type e = b->bits[i]; - while (e != 0) - { - cnt++; - e &= e - 1; - } - } - return cnt; -} - -/* Returns true if any bits in B are set to true, - and false otherwise.*/ -bool -bitmap_any (const struct bitmap *b) +bitmap_count (const struct bitmap *b, size_t start, size_t cnt, bool value) { - size_t i; + size_t i, value_cnt; ASSERT (b != NULL); - for (i = 0; i < elem_cnt (b); i++) - if (b->bits[i]) - return true; - return false; + ASSERT (start <= b->bit_cnt); + ASSERT (start + cnt <= b->bit_cnt); + + value_cnt = 0; + for (i = 0; i < cnt; i++) + if (bitmap_test (b, start + i) == value) + value_cnt++; + return value_cnt; } -/* Returns the number of bits in B set to false. */ +/* Returns true if any bits in B between START and START + CNT, + exclusive, are set to true, and false otherwise.*/ bool -bitmap_clear_cnt (const struct bitmap *b) +bitmap_any (const struct bitmap *b, size_t start, size_t cnt) { - return b->bit_cnt - bitmap_set_cnt (b); + return contains (b, start, cnt, true); } -/* Returns true if no bits in B are set to true, - and false otherwise.*/ +/* Returns true if no bits in B between START and START + CNT, + exclusive, are set to true, and false otherwise.*/ bool -bitmap_none (const struct bitmap *b) +bitmap_none (const struct bitmap *b, size_t start, size_t cnt) { - return !bitmap_any (b); + return !contains (b, start, cnt, true); } -/* Returns true if every bit in B is set to true, - and false otherwise. */ +/* Returns true if every bit in B between START and START + CNT, + exclusive, is set to true, and false otherwise. */ bool -bitmap_all (const struct bitmap *b) +bitmap_all (const struct bitmap *b, size_t start, size_t cnt) { - size_t i; - - ASSERT (b != NULL); - - if (b->bit_cnt == 0) - return true; - - for (i = 0; i < elem_cnt (b) - 1; i++) - if (b->bits[i] != (elem_type) -1) - return false; - return b->bits[i] == last_mask (b); + return !contains (b, start, cnt, false); } #ifdef FILESYS @@ -297,7 +297,7 @@ bitmap_all (const struct bitmap *b) size_t bitmap_file_size (const struct bitmap *b) { - return byte_cnt (b); + return byte_cnt (b->bit_cnt); } /* Reads FILE into B, ignoring errors. */ @@ -306,8 +306,8 @@ bitmap_read (struct bitmap *b, struct file *file) { if (b->bit_cnt > 0) { - file_read_at (file, b->bits, byte_cnt (b), 0); - b->bits[elem_cnt (b) - 1] &= last_mask (b); + file_read_at (file, b->bits, byte_cnt (b->bit_cnt), 0); + b->bits[elem_cnt (b->bit_cnt) - 1] &= last_mask (b); } } @@ -315,7 +315,7 @@ bitmap_read (struct bitmap *b, struct file *file) void bitmap_write (const struct bitmap *b, struct file *file) { - file_write_at (file, b->bits, byte_cnt (b), 0); + file_write_at (file, b->bits, byte_cnt (b->bit_cnt), 0); } #endif /* FILESYS */ @@ -323,5 +323,30 @@ bitmap_write (const struct bitmap *b, struct file *file) void bitmap_dump (const struct bitmap *b) { - hex_dump (b->bits, byte_cnt (b), false); + hex_dump (0, b->bits, byte_cnt (b->bit_cnt), false); +} + +/* Returns the number of bytes required to accomodate a bitmap + with BIT_CNT bits. */ +size_t +bitmap_needed_bytes (size_t bit_cnt) +{ + return sizeof (struct bitmap) + byte_cnt (bit_cnt); +} + +/* Creates and returns a bitmap with BIT_CNT bits in the + BLOCK_SIZE bytes of storage preallocated at BLOCK. + BLOCK_SIZE must be at least bitmap_needed_bytes(BIT_CNT). */ +struct bitmap * +bitmap_create_preallocated (size_t bit_cnt, void *block, size_t block_size) +{ + struct bitmap *b = block; + + ASSERT (block_size >= bitmap_needed_bytes (bit_cnt)); + + b->bit_cnt = bit_cnt; + b->bits = (elem_type *) (b + 1); + bitmap_set_all (b, false); + return b; } +