From 7cfd4eb53675a3a3be508d7658daf34be02bb1dd Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 19 Jun 2017 09:34:49 -0500 Subject: [PATCH] I lied, now the constructor is finished. --- MP2/MP2_Sources/cont_frame_pool.C | 92 ++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 26 deletions(-) diff --git a/MP2/MP2_Sources/cont_frame_pool.C b/MP2/MP2_Sources/cont_frame_pool.C index aeb5ec4..44ec1c0 100644 --- a/MP2/MP2_Sources/cont_frame_pool.C +++ b/MP2/MP2_Sources/cont_frame_pool.C @@ -137,7 +137,8 @@ ContFramePool::ContFramePool(unsigned long _base_frame_no, // since as stated in the instructions it is already big enough to // hold for 128MB of memory with one bit per frame, so should still // be enough for 64MB of memory with 2 bits per frame. - assert(_n_frames <= FRAME_SIZE * 8); + // Assertion changed to match max size of frames allowed. + assert(_n_frames <= FRAME_SIZE * 4); base_frame_no = _base_frame_no; nframes = _n_frames; @@ -147,28 +148,14 @@ ContFramePool::ContFramePool(unsigned long _base_frame_no, // If _info_frame_no is zero then we keep management info in the first // frame(s), else we use the provided frame(s) to keep management info - // NOTE: need a second check for the number of info frames specified, if any. + // NOTE: bitmap needs to be allocated with n_info_frames if specified. if(info_frame_no == 0) { - if(n_info_frames == 0) - { - bitmap = (unsigned char *) (base_frame_no * FRAME_SIZE); - } - else - { - bitmap = (unsigned char *) (base_frame_no * FRAME_SIZE * n_info_frames); - } + bitmap = (unsigned char *) (base_frame_no * FRAME_SIZE); } else { - if(n_info_frames == 0) - { - bitmap = (unsigned char *) (info_frame_no * FRAME_SIZE); - } - else - { - bitmap = (unsigned char *) (info_frame_no * FRAME_SIZE * n_info_frames); - } + bitmap = (unsigned char *) (info_frame_no * FRAME_SIZE * n_info_frames); } // Number of frames must "fill" the bitmap! @@ -176,19 +163,39 @@ ContFramePool::ContFramePool(unsigned long _base_frame_no, // Everything ok. Proceed to mark all bits in the bitmap - // NOTE: I don't think this needs to be changed... - for(int i=0; i*8 < _n_frames; i++) + // NOTE: changed to reflect that I need 2 bits per frame now + for(int i=0; i*4 < _n_frames; i++) { bitmap[i] = 0xFF; } // Mark the first frame as being used if it is being used // NOTE: need to mark multiple frames if needed. - unsigned long i = info_frame_no; - while(i < (info_frame_no + n_info_frames)) + if(info_frame_no == 0) { - bitmap[i*8] = 0x7F; - nFreeFrames--; + bitmap[0] = 0x3F; + nFreeFrames--; + } + else + { + unsigned int i = info_frame_no / 4; + unsigned int r = info_frame_no % 4; + unsigned char mask = 0xC0; + mask = mask >> r*2; + + unsigned int c = 0; + while(c < n_info_frames) + { + bitmap[i] = bitmap[i] ^ mask; + if(mask == 0x00) + { + i++; + mask = 0xC0; + } + c++; + } + + nFreeFrames--; } Console::puts("Frame Pool initialized\n"); @@ -196,8 +203,41 @@ ContFramePool::ContFramePool(unsigned long _base_frame_no, unsigned long ContFramePool::get_frames(unsigned int _n_frames) { - // TODO: IMPLEMENTATION NEEEDED! - assert(false); + // Are there enough frames left to allocate? + assert(nFreeFrames > _n_frames); + + // Find a frame that is not being used and return its frame index. + // Mark that frame as being used in the bitmap. + // NOTE: Must be updated to find a sequence of contiguous frames + // that are not being used and update the index of the head. + unsigned int frame_no = base_frame_no; + + unsigned int i = 0; + unsigned int c = 0; + while (true) { + if(bitmap[i] != 0x0) + { + while(c < _n_frames) + { + + } + } + i++; + } + + frame_no += i * 8; + + unsigned char mask = 0x80; + while ((mask & bitmap[i]) == 0) { + mask = mask >> 1; + frame_no++; + } + nFreeFrames--; + + // Update bitmap + bitmap[i] = bitmap[i] ^ mask; + + return (frame_no); } void ContFramePool::mark_inaccessible(unsigned long _base_frame_no,