From: Ben Pfaff Date: Mon, 30 Aug 2004 18:07:59 +0000 (+0000) Subject: Fix bitmap usage. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=18c42969655f3a3de4ed601d0f6ce865ecb4a1bf;p=pintos-anon Fix bitmap usage. --- diff --git a/src/filesys/filesys.c b/src/filesys/filesys.c index 0b96c27..d0e5c49 100644 --- a/src/filesys/filesys.c +++ b/src/filesys/filesys.c @@ -21,7 +21,8 @@ do_format (void) /* Create the initial bitmap and reserve sectors for the free map and root directory file headers. */ - bitmap_init (&free_map, disk_size (disk)); + if (!bitmap_init (&free_map, disk_size (disk))) + panic ("bitmap creation failed"); bitmap_mark (&free_map, FREE_MAP_SECTOR); bitmap_mark (&free_map, ROOT_DIR_SECTOR); @@ -85,7 +86,8 @@ filesys_create (const char *name, off_t initial_size) goto exit1; /* Allocate a block for the file header. */ - bitmap_init (&free_map, disk_size (disk)); + if (!bitmap_init (&free_map, disk_size (disk))) + goto exit1; bitmapio_read (&free_map, &free_map_file); hdr_sector = bitmap_find_and_set (&free_map); if (hdr_sector == BITMAP_ERROR) @@ -129,13 +131,15 @@ filesys_remove (const char *name) dir_init (&dir, NUM_DIR_ENTRIES); dir_read (&dir, &root_dir_file); if (!dir_lookup (&dir, name, &hdr_sector)) - goto exit; + goto exit1; /* Read the file header. */ - filehdr_read (&filehdr, hdr_sector); + if (!filehdr_read (&filehdr, hdr_sector)) + goto exit1; /* Allocate a block for the file header. */ - bitmap_init (&free_map, disk_size (disk)); + if (!bitmap_init (&free_map, disk_size (disk))) + goto exit2; bitmapio_read (&free_map, &free_map_file); /* Deallocate. */ @@ -150,9 +154,10 @@ filesys_remove (const char *name) success = true; /* Clean up. */ - filehdr_destroy (&filehdr); bitmap_destroy (&free_map); - exit: + exit2: + filehdr_destroy (&filehdr); + exit1: dir_destroy (&dir); return success; diff --git a/src/filesys/filesys.h b/src/filesys/filesys.h index 4f31b7c..3de5f85 100644 --- a/src/filesys/filesys.h +++ b/src/filesys/filesys.h @@ -2,6 +2,7 @@ #define HEADER_FILESYS_H 1 #include +#include #ifndef OFF_T #define OFF_T diff --git a/src/lib/bitmap.c b/src/lib/bitmap.c index 616056f..46235b2 100644 --- a/src/lib/bitmap.c +++ b/src/lib/bitmap.c @@ -5,7 +5,6 @@ #include "lib.h" #include "malloc.h" -typedef unsigned long elem_type; #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)) @@ -22,16 +21,16 @@ byte_cnt (const struct bitmap *b) return sizeof (elem_type) * elem_cnt (b); } -void +bool bitmap_init (struct bitmap *b, size_t bit_cnt) { b->bit_cnt = bit_cnt; b->bits = malloc (byte_cnt (b)); if (b->bits == NULL && bit_cnt > 0) - return NULL; + return false; bitmap_set_all (b, false); - return b; + return true; } size_t diff --git a/src/lib/bitmap.h b/src/lib/bitmap.h index ba4465e..896a1e8 100644 --- a/src/lib/bitmap.h +++ b/src/lib/bitmap.h @@ -4,16 +4,19 @@ #include #include +typedef unsigned long elem_type; + struct bitmap { size_t bit_cnt; elem_type *bits; }; -void bitmap_init (struct bitmap *, size_t bit_cnt); +bool bitmap_init (struct bitmap *, size_t bit_cnt); void bitmap_destroy (struct bitmap *); size_t bitmap_size (const struct bitmap *); +size_t bitmap_storage_size (const struct bitmap *); void bitmap_set (struct bitmap *, size_t idx, bool); void bitmap_set_all (struct bitmap *, bool);