X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flib%2Fkernel%2Fbitmap.c;h=3690a3a37c71500efeaa031fdaa2aeff2971c5dc;hb=7106fafb92091fe9f85fbe051ed5bed5a9ff805a;hp=7bbc92868ca9fdf908798c919c168c7b3b44732b;hpb=fbc28c24bfd95208ba4394830ced0c1fbfab4a42;p=pintos-anon diff --git a/src/lib/kernel/bitmap.c b/src/lib/kernel/bitmap.c index 7bbc928..3690a3a 100644 --- a/src/lib/kernel/bitmap.c +++ b/src/lib/kernel/bitmap.c @@ -8,9 +8,28 @@ #include "filesys/file.h" #endif +/* Element type. + + This must be an unsigned integer type at least as wide as int. + + Each bit represents one bit in the bitmap. + If bit 0 in an element represents bit K in the bitmap, + then bit 1 in the element represents bit K+1 in the bitmap, + and so on. */ +typedef unsigned long elem_type; + /* Number of bits in an element. */ #define ELEM_BITS (sizeof (elem_type) * CHAR_BIT) +/* From the outside, a bitmap is an array of bits. From the + inside, it's an array of elem_type (defined above) that + simulates an array of bits. */ +struct bitmap + { + size_t bit_cnt; /* Number of bits. */ + elem_type *bits; /* Elements that represent bits. */ + }; + /* Returns the index of the element that contains the bit numbered BIT_IDX. */ static inline size_t @@ -53,25 +72,33 @@ last_mask (const struct bitmap *b) /* 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) +struct bitmap * +bitmap_create (size_t bit_cnt) { - b->bit_cnt = bit_cnt; - b->bits = malloc (byte_cnt (b)); - if (b->bits == NULL && bit_cnt > 0) - return false; - - bitmap_set_all (b, false); - return true; + struct bitmap *b = malloc (sizeof *b); + if (b != NULL) + { + b->bit_cnt = bit_cnt; + b->bits = malloc (byte_cnt (b)); + if (b->bits != NULL || bit_cnt == 0) + { + bitmap_set_all (b, false); + return b; + } + free (b); + } + return NULL; } /* Destroys bitmap B, freeing its storage. */ void bitmap_destroy (struct bitmap *b) { - ASSERT (b); - - free (b->bits); + if (b != NULL) + { + free (b->bits); + free (b); + } } /* Returns the number of bits in B. */