Example #1
0
//--------------------------------------------------------------------
// BufMgr::FreePage
//
// Input    : pid     - page id of a particular page 
// Output   : None
// Purpose  : Free the memory allocated for the page with 
//            page id = pid  
// Condition: Either the page is already in the buffer and is pinned
//            no more than once, or the page is not in the buffer.
// PostCond : The page is unpinned, and the frame where it resides in
//            the buffer pool is freed.  Also the page is deallocated
//            from the database. 
// Return   : OK if operation is successful.  FAIL otherwise.
// Note     : You can call MINIBASE_DB->DeallocatePage(pid) to
//            deallocate a page.
//--------------------------------------------------------------------
Status BufMgr::FreePage(PageID pid)
{
	//std::cout << "Free PageID " << pid << std::endl;
	////std::cout << "Free page:  " << pid << std::endl;

	Frame* targetFrame;
	int frameIndex = FindFrame(pid);
	if (frameIndex != INVALID_FRAME) {
		targetFrame = &frames[frameIndex];

		if (targetFrame->GetPinCount() > 1) return FAIL;
		
		UnpinPage(pid, true);
		FlushPage(pid);
	}
	
	return MINIBASE_DB->DeallocatePage(pid);
}