Esempio n. 1
0
int FileSystem::deleteFile(fs_fileno fileHandle) {
	bool mustReleaseLock = getLock();

	fs_pageno firstPage = getFirstPage(fileHandle);
	assert(firstPage >= 0);

	// remove the file from the file->page mappings
	setFirstPage(fileHandle, UNUSED_PAGE);
	setPageCount(fileHandle, UNUSED_PAGE);

	// mark all pages occupied by the file as free
	fs_pageno page = firstPage;
	while (page > 0) {
		fs_pageno nextPage = getPageStatus(page);
		setPageStatus(page, UNUSED_PAGE);
		page = nextPage;
	}

	// check if it is appropriate to reduce the size of the file->page table
	if ((freeFileNumbers[fileMappingSize - 1] == doubleIntsPerPage) &&
			(freeFileNumbers[fileMappingSize - 2] == doubleIntsPerPage))
		decreaseFileMappingSize();

	if (mustReleaseLock)
		releaseLock();
	return FILESYSTEM_SUCCESS;
} // end of deleteFile(fs_fileno)
Esempio n. 2
0
void FileSystem::initializeFreeSpaceArrays() {
	bool mustReleaseLock = getLock();

	if (freePages == NULL) {
		freePages = (int16_t*)malloc(pageLayoutSize * sizeof(int16_t));
		for (int j = 0; j < pageLayoutSize; j++) {
			freePages[j] = 0;
			for (int k = 0; k < intsPerPage; k++)
				if (getPageStatus(j * intsPerPage + k) == UNUSED_PAGE)
					freePages[j]++;
		} // end for (int j = 0; j < pageLayoutSize; j++)
	}

	if (freeFileNumbers == NULL) {
		freeFileNumbers = (int16_t*)malloc(fileMappingSize * sizeof(int16_t));
		for (int j = 0; j < fileMappingSize; j++) {
			freeFileNumbers[j] = false;
			for (int k = 0; k < doubleIntsPerPage; k++)
				if (getFirstPage(j * doubleIntsPerPage + k) == UNUSED_PAGE)
					freeFileNumbers[j]++;
		} // end for (int j = 0; j < pageLayoutSize; j++)
	}

	if (mustReleaseLock)
		releaseLock();
} // end of initializeFreeSpaceArrays()
Esempio n. 3
0
ChatLogWin::ChatLogWin(QWidget *parent) :
    QWidget(parent),
    ui_(new Ui::ChatLogWin),
    chat_log_(NULL)
{
    ui_->setupUi(this);

    move((QApplication::desktop()->width() - this->width()) /2, (QApplication::desktop()->height() - this->height()) /2);
    ui_->msgbrowse->setReadOnly(true);

    connect(ui_->btn_first, SIGNAL(clicked()), this, SLOT(getFirstPage()));
    connect(ui_->btn_last, SIGNAL(clicked()), this, SLOT(getLastPage()));
    connect(ui_->btn_next, SIGNAL(clicked()), this, SLOT(getNextPage()));
    connect(ui_->btn_prep, SIGNAL(clicked()), this, SLOT(getPrePage()));
}
Esempio n. 4
0
int FileSystem::createFile(fs_fileno fileHandle) {
	bool mustReleaseLock = getLock();
	int result = FILESYSTEM_ERROR;

	// Claim free page (first of file), then claim file number.
	fs_pageno firstPage = claimFreePage(-1, -1);
	assert(firstPage >= 0);

	while (fileHandle >= doubleIntsPerPage * fileMappingSize)
		increaseFileMappingSize();

	if (fileHandle >= 0) {
		// create a file with a specific file number
		if (getFirstPage(fileHandle) >= 0) {
			setPageStatus(firstPage, UNUSED_PAGE);
			goto endOfCreateFile;
		}
	}
	else {
		// create a file with an arbitrary file number
		fileHandle = claimFreeFileNumber();
		assert(fileHandle >= 0);
		setPageStatus(firstPage, UNUSED_PAGE);
	}

	// Set length to zero. Write first page to the file mappings table.
	setPageStatus(firstPage, 0);
	setFirstPage(fileHandle, firstPage);
	setPageCount(fileHandle, 1);

	result = fileHandle;

endOfCreateFile:
	if (mustReleaseLock)
		releaseLock();
	return result;
} // end of createFile(fs_fileno)
Esempio n. 5
0
int FileSystem::defrag() {
	printf("defrag called\n");
	exit(1);
	int32_t nextFreePage = 1;
	int32_t *newPosition = (int32_t*)malloc(pageCount * sizeof(int32_t));
	// page 0 must not be moved around
	newPosition[0] = 0;
	for (int i = 1; i < pageCount; i++)
		newPosition[i] = -1;

	// first, perform a DFS to assign new page positions
	int upperFileLimit = doubleIntsPerPage * fileMappingSize;
	for (int file = 0; file < upperFileLimit; file++) {
		int32_t page = getFirstPage(file);
		while (page > 0) {
			assert(newPosition[page] < 0);
			newPosition[page] = nextFreePage++;
			page = getPageStatus(page);
		}
	} // end for (int file = 0; file < upperFileLimit; file++)

	// then, assign new page numbers to the remaining (free) pages
	for (int i = 1; i < pageCount; i++)
		if (getPageStatus(i) == UNUSED_PAGE)
			newPosition[i] = nextFreePage++;

	// make sure that every page has been assigned a new location
	assert(nextFreePage == pageCount);

	// correct data in the page layout table
	int32_t *oldPageLayout = (int32_t*)malloc(pageLayoutSize * intsPerPage * sizeof(int32_t));
	int32_t *newPageLayout = (int32_t*)malloc(pageLayoutSize * intsPerPage * sizeof(int32_t));
	for (int i = 0; i < pageLayoutSize; i++)
		readPage(pageCount + i, &oldPageLayout[intsPerPage * i]);
	for (int page = 0; page < pageCount; page++) {
		if (oldPageLayout[page] <= 0)
			newPageLayout[newPosition[page]] = oldPageLayout[page];
		else
			newPageLayout[newPosition[page]] = newPosition[oldPageLayout[page]];
	}
	for (int i = 0; i < pageLayoutSize; i++)
		writePage(pageCount + i, &newPageLayout[intsPerPage * i]);	
	free(newPageLayout);
	free(oldPageLayout);

	// correct page numbers in the file->page table
	for (int file = 0; file < upperFileLimit; file++) {
		int32_t page = getFirstPage(file);
		if (page >= 0)
			setFirstPage(file, newPosition[page]);
	} // end for (int file = 0; file < upperFileLimit; file++)

	byte *buffer1 = (byte*)malloc(pageSize);
	byte *buffer2 = (byte*)malloc(pageSize);
	// happy reading/writing while moving all pages to their new positions
	for (int page = 0; page < pageCount; page++) {
		int currentPage = page;
		while (newPosition[currentPage] != currentPage) {
			assert(newPosition[currentPage] >= page);
			int32_t newPos = newPosition[currentPage];

			// swap data in pages "currentPage" and "newPos"
			if (readPage(currentPage, buffer1) == FILESYSTEM_ERROR)
				return FILESYSTEM_ERROR;
			if (readPage(newPos, buffer2) == FILESYSTEM_ERROR)
				return FILESYSTEM_ERROR;
			if (writePage(newPos, buffer1) == FILESYSTEM_ERROR)
				return FILESYSTEM_ERROR;
			if (writePage(currentPage, buffer2) == FILESYSTEM_ERROR)
				return FILESYSTEM_ERROR;

			// update permutation table
			newPosition[currentPage] = newPosition[newPos];
			newPosition[newPos] = newPos;
		}
	} // end for (int page = 0; page < pageCount; page++)
	free(buffer2);
	free(buffer1);

	free(newPosition);
	return FILESYSTEM_SUCCESS;
} // end of defrag()