00001 // bitmap.c 00002 // Routines to manage a bitmap -- an array of bits each of which 00003 // can be either on or off. Represented as an array of integers. 00004 // 00005 // Copyright (c) 1992-1993 The Regents of the University of California. 00006 // All rights reserved. See copyright.h for copyright notice and limitation 00007 // of liability and disclaimer of warranty provisions. 00008 00009 #include "copyright.h" 00010 #include "bitmap.h" 00011 00012 //---------------------------------------------------------------------- 00013 // BitMap::BitMap 00014 // Initialize a bitmap with "nitems" bits, so that every bit is clear. 00015 // it can be added somewhere on a list. 00016 // 00017 // "nitems" is the number of bits in the bitmap. 00018 //---------------------------------------------------------------------- 00019 00020 BitMap::BitMap(int nitems) 00021 { 00022 numBits = nitems; 00023 numWords = divRoundUp(numBits, BitsInWord); 00024 map = new unsigned int[numWords]; 00025 for (int i = 0; i < numBits; i++) 00026 Clear(i); 00027 } 00028 00029 //---------------------------------------------------------------------- 00030 // BitMap::~BitMap 00031 // De-allocate a bitmap. 00032 //---------------------------------------------------------------------- 00033 00034 BitMap::~BitMap() 00035 { 00036 delete map; 00037 } 00038 00039 //---------------------------------------------------------------------- 00040 // BitMap::Set 00041 // Set the "nth" bit in a bitmap. 00042 // 00043 // "which" is the number of the bit to be set. 00044 //---------------------------------------------------------------------- 00045 00046 void 00047 BitMap::Mark(int which) 00048 { 00049 ASSERT(which >= 0 && which < numBits); 00050 map[which / BitsInWord] |= 1 << (which % BitsInWord); 00051 } 00052 00053 //---------------------------------------------------------------------- 00054 // BitMap::Clear 00055 // Clear the "nth" bit in a bitmap. 00056 // 00057 // "which" is the number of the bit to be cleared. 00058 //---------------------------------------------------------------------- 00059 00060 void 00061 BitMap::Clear(int which) 00062 { 00063 ASSERT(which >= 0 && which < numBits); 00064 map[which / BitsInWord] &= ~(1 << (which % BitsInWord)); 00065 } 00066 00067 //---------------------------------------------------------------------- 00068 // BitMap::Test 00069 // Return TRUE if the "nth" bit is set. 00070 // 00071 // "which" is the number of the bit to be tested. 00072 //---------------------------------------------------------------------- 00073 00074 bool 00075 BitMap::Test(int which) 00076 { 00077 ASSERT(which >= 0 && which < numBits); 00078 00079 if (map[which / BitsInWord] & (1 << (which % BitsInWord))) 00080 return TRUE; 00081 else 00082 return FALSE; 00083 } 00084 00085 //---------------------------------------------------------------------- 00086 // BitMap::Find 00087 // Return the number of the first bit which is clear. 00088 // As a side effect, set the bit (mark it as in use). 00089 // (In other words, find and allocate a bit.) 00090 // 00091 // If no bits are clear, return -1. 00092 //---------------------------------------------------------------------- 00093 00094 int 00095 BitMap::Find() 00096 { 00097 for (int i = 0; i < numBits; i++) 00098 if (!Test(i)) { 00099 Mark(i); 00100 return i; 00101 } 00102 return -1; 00103 } 00104 00105 //---------------------------------------------------------------------- 00106 // BitMap::NumClear 00107 // Return the number of clear bits in the bitmap. 00108 // (In other words, how many bits are unallocated?) 00109 //---------------------------------------------------------------------- 00110 00111 int 00112 BitMap::NumClear() 00113 { 00114 int count = 0; 00115 00116 for (int i = 0; i < numBits; i++) 00117 if (!Test(i)) count++; 00118 return count; 00119 } 00120 00121 //---------------------------------------------------------------------- 00122 // BitMap::Print 00123 // Print the contents of the bitmap, for debugging. 00124 // 00125 // Could be done in a number of ways, but we just print the #'s of 00126 // all the bits that are set in the bitmap. 00127 //---------------------------------------------------------------------- 00128 00129 void 00130 BitMap::Print() 00131 { 00132 printf("Bitmap set:\n"); 00133 for (int i = 0; i < numBits; i++) 00134 if (Test(i)) 00135 printf("%d, ", i); 00136 printf("\n"); 00137 } 00138 00139 // These aren't needed until the FILESYS assignment 00140 00141 //---------------------------------------------------------------------- 00142 // BitMap::FetchFromFile 00143 // Initialize the contents of a bitmap from a Nachos file. 00144 // 00145 // "file" is the place to read the bitmap from 00146 //---------------------------------------------------------------------- 00147 00148 void 00149 BitMap::FetchFrom(OpenFile *file) 00150 { 00151 file->ReadAt((char *)map, numWords * sizeof(unsigned), 0); 00152 } 00153 00154 //---------------------------------------------------------------------- 00155 // BitMap::WriteBack 00156 // Store the contents of a bitmap to a Nachos file. 00157 // 00158 // "file" is the place to write the bitmap to 00159 //---------------------------------------------------------------------- 00160 00161 void 00162 BitMap::WriteBack(OpenFile *file) 00163 { 00164 file->WriteAt((char *)map, numWords * sizeof(unsigned), 0); 00165 }
1.2.14 written by Dimitri van Heesch,
© 1997-2002