1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| class BMgr { public: BMgr(DSMgr& dsmgr) { memset(ftop, -1, sizeof(ftop)); memset(ptof, NULL, sizeof(ptof)); lru_head = NULL; lru_tail = NULL; ds = &dsmgr; hit_cnt = 0; } int FixPage(int page_id, int cmd) { int frame_id = -1; int hash_id = Hash(page_id); BCB* bcb = ptof[hash_id]; while (bcb != NULL) { if (bcb->page_id == page_id) { frame_id = bcb->frame_id; break; } bcb = bcb->nextBCB; } if (frame_id == -1) { frame_id = SelectVictim(); if (ftop[frame_id] != -1) { BCB tmp = RemoveBCB(ftop[frame_id]); if (tmp.dirty == 1) { ds->WritePage(tmp.page_id, buf[tmp.frame_id]); } } buf[frame_id] = ds->ReadPage(page_id); } else { RemoveBCB(page_id); hit_cnt++; } BCB* newbcb = new BCB(page_id, frame_id, 0, 0, ptof[hash_id], lru_head, NULL); newbcb->count++; lru_head = newbcb; if (lru_tail == NULL) { lru_tail = newbcb; } if (cmd == 1) { newbcb->dirty = 1; } ptof[hash_id] = newbcb; ftop[frame_id] = page_id; return frame_id; } Page FixNewPage() { Page page; for (int i = 0; i < MAXPAGES; i++) { if (!ds->GetUse(i)) { ds->SetUse(i, 1); page.page_id = i; break; } } int page_id = page.page_id; int hash_id = Hash(page_id); int frame_id = SelectVictim(); if (ftop[frame_id] != -1) { BCB tmp = RemoveBCB(ftop[frame_id]); if (tmp.dirty == 1) { ds->WritePage(tmp.page_id, buf[tmp.frame_id]); } } page.frame_id = frame_id; BCB* newbcb = new BCB(page_id, frame_id, 0, 0, NULL, lru_head, NULL); lru_head = newbcb; if (lru_tail == NULL) { lru_tail = newbcb; } ptof[hash_id] = newbcb; ftop[frame_id] = page_id; return page; } int UnfixPage(int page_id) { int hash_id = Hash(page_id); BCB* bcb = ptof[hash_id]; while (bcb != NULL) { if (bcb->page_id == page_id) { break; } bcb = bcb->nextBCB; } bcb->count--; return bcb->count; } int NumFreeFrames() { int cnt = 0; for (int i = 0; i < DEFBUFSIZE; i++) { if (ftop[i] == -1) { cnt++; } } return cnt; } int SelectVictim() { int frame_id = -1; if (NumFreeFrames() > 0) { for (int i = 0; i < DEFBUFSIZE; i++) { if (ftop[i] == -1) { frame_id = i; break; } } } else { frame_id = SelectLRUVictim(); } return frame_id; } int SelectLRUVictim() { BCB* tmp = lru_tail; while (tmp->count > 0) { tmp = tmp->prevLRU; } return tmp->frame_id; } int Hash(int page_id) { return page_id % DEFBUFSIZE; } BCB RemoveBCB(int page_id) { int hash_id = Hash(page_id); BCB* tmp = new BCB(); BCB* bcb = ptof[hash_id]; if (bcb->page_id == page_id) { tmp = bcb; bcb = bcb->nextBCB; } else { while (bcb->nextBCB != NULL) { if (bcb->nextBCB->page_id == page_id) { tmp = bcb->nextBCB; bcb->nextBCB = tmp->nextBCB; break; } bcb = bcb->nextBCB; } } if (tmp->nextLRU != NULL) { tmp->nextLRU->prevLRU = tmp->prevLRU; } else { lru_tail = tmp->prevLRU; } if (tmp->prevLRU != NULL) { tmp->prevLRU->nextLRU = tmp->nextLRU; } else { lru_head = tmp->nextLRU; } ptof[hash_id] = bcb; BCB ret = *tmp; delete tmp; return ret; } void SetDirty(int frame_id) { int page_id = ftop[frame_id]; BCB *bcb = ptof[Hash(page_id)]; while (bcb != NULL) { if (bcb->page_id == page_id) { bcb->dirty = 1; break; } bcb = bcb->nextBCB; } } void UnsetDirty(int frame_id) { int page_id = ftop[frame_id]; BCB *bcb = ptof[Hash(page_id)]; while (bcb != NULL) { if (bcb->page_id == page_id) { bcb->dirty = 0; break; } bcb = bcb->nextBCB; } } void WriteDirtys() { BCB* bcb = NULL; for (int i = 0; i < DEFBUFSIZE; i++) { bcb = ptof[i]; while (bcb != NULL) { if (bcb->dirty == 1) { ds->WritePage(bcb->page_id, buf[bcb->frame_id]); } bcb = bcb->nextBCB; } } } void WriteBuffer(int frame_id, bFrame& frm) { buf[frame_id] = frm; } void PrintFrame(int frame_id) { cout<<buf[frame_id].field<<endl; } public: int hit_cnt; private: int ftop[DEFBUFSIZE]; BCB* ptof[DEFBUFSIZE]; DSMgr* ds; bFrame buf[DEFBUFSIZE]; BCB* lru_head; BCB* lru_tail; };
|