181 lines
4.5 KiB
C
181 lines
4.5 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
|
|
#define CHECK_FERROR(f) { if (ferror(f)) {fprintf(stderr, "IO Error"); exit(1);} }
|
|
|
|
#include "config.h"
|
|
#include "mmap.h"
|
|
|
|
MMap* mmap_init(){
|
|
MMap *mmap = malloc(sizeof(MMap));
|
|
mmap->size = PAGE_SIZE*PAGE_COUNT;
|
|
mmap->memory = malloc(mmap->size);
|
|
mmap->count = 0;
|
|
mmap->support = malloc(sizeof(PageInfo));
|
|
mmap->support->data = malloc(PAGE_SIZE);
|
|
mmap->free_list = createLinkedList();
|
|
// insert PAGE_COUNT pages in the free list
|
|
for (int i=0; i < PAGE_COUNT; i++) {
|
|
PageInfo *page = malloc(sizeof(PageInfo));
|
|
page->data = mmap->memory + i*PAGE_SIZE;
|
|
page->key = i;
|
|
insertFirst(mmap->free_list, i, page);
|
|
}
|
|
mmap->alloc_list = createLinkedList();
|
|
mmap->swap = fopen("/tmp/myswap", "wb+");
|
|
int tmp = -1;
|
|
for (int i=0; i < PAGE_COUNT; i++) {
|
|
fwrite(&tmp, sizeof(int), 1, mmap->swap);
|
|
}
|
|
char tmp2 = '\0';
|
|
for (int i=0; i < PAGE_COUNT*PAGE_SIZE; i++) {
|
|
fwrite(&tmp2, 1, 1, mmap->swap);
|
|
}
|
|
return mmap;
|
|
}
|
|
|
|
void mmap_clean(MMap *mmap) {
|
|
free(mmap->memory);
|
|
fclose(mmap->swap);
|
|
free(mmap);
|
|
remove("/tmp/myswap");
|
|
}
|
|
|
|
PageInfo* page_alloc(MMap *mmap) {
|
|
PageInfo *page = NULL;
|
|
page = page_free(mmap);
|
|
if (page) {
|
|
insertFirst(mmap->alloc_list, page->key, page);
|
|
mmap->count++;
|
|
}
|
|
return page;
|
|
}
|
|
|
|
PageInfo* page_free(MMap *mmap) {
|
|
PageInfo *page = NULL;
|
|
node *node = deleteFirst(mmap->free_list);
|
|
if (node) {
|
|
page = node->data;
|
|
}
|
|
return page;
|
|
}
|
|
|
|
int check_page_free_list(MMap *mmap) {
|
|
return length(mmap->free_list);
|
|
}
|
|
|
|
int check_page_alloc(MMap *mmap) {
|
|
return length(mmap->alloc_list);
|
|
}
|
|
|
|
int move_to_swap(MMap *mmap, PageInfo *page) {
|
|
int key = -1;
|
|
fseek(mmap->swap, 0, SEEK_SET);
|
|
CHECK_FERROR(mmap->swap);
|
|
|
|
for (int i=0; i < PAGE_COUNT; i++) {
|
|
fread(&key, sizeof(int), 1, mmap->swap);
|
|
CHECK_FERROR(mmap->swap);
|
|
|
|
if (key == -1) {
|
|
fseek(mmap->swap, i*sizeof(int), SEEK_SET);
|
|
CHECK_FERROR(mmap->swap);
|
|
|
|
fwrite(&page->key, sizeof(int), 1, mmap->swap);
|
|
CHECK_FERROR(mmap->swap);
|
|
|
|
fseek(mmap->swap, PAGE_COUNT*sizeof(int) + i*PAGE_SIZE, SEEK_SET);
|
|
CHECK_FERROR(mmap->swap);
|
|
|
|
fwrite(page->data, PAGE_SIZE, 1, mmap->swap);
|
|
CHECK_FERROR(mmap->swap);
|
|
|
|
return 0;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
PageInfo* read_from_swap(MMap* mmap, PageInfo *page) {
|
|
int key = -1;
|
|
fseek(mmap->swap, 0, SEEK_SET);
|
|
CHECK_FERROR(mmap->swap);
|
|
|
|
for (int i=0; i < PAGE_COUNT; i++) {
|
|
(void)fread(&key, sizeof(int), 1, mmap->swap);
|
|
CHECK_FERROR(mmap->swap);
|
|
|
|
if (key != page->key)
|
|
continue;
|
|
|
|
fseek(mmap->swap, PAGE_COUNT*sizeof(int) + i*PAGE_SIZE, SEEK_SET);
|
|
CHECK_FERROR(mmap->swap);
|
|
|
|
fread(page->data, PAGE_SIZE, 1, mmap->swap);
|
|
CHECK_FERROR(mmap->swap);
|
|
|
|
fseek(mmap->swap, i*sizeof(int), SEEK_SET);
|
|
CHECK_FERROR(mmap->swap);
|
|
|
|
int tmp = -1;
|
|
fwrite(&tmp, sizeof(int), 1, mmap->swap);
|
|
CHECK_FERROR(mmap->swap);
|
|
|
|
return page;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
PageInfo* page_lookup(MMap* mmap, int key) {
|
|
node* data = find(mmap->alloc_list, key);
|
|
if (data) {
|
|
return data->data;
|
|
}
|
|
|
|
mmap->support->key = key;
|
|
PageInfo* page = read_from_swap(mmap, mmap->support);
|
|
if (page) { // Move one elem from mem to swap
|
|
PageInfo* swapped_page = deleteFirst(mmap->alloc_list)->data;
|
|
move_to_swap(mmap, swapped_page);
|
|
|
|
memcpy(swapped_page->data, mmap->support->data, PAGE_SIZE);
|
|
swapped_page->key = key;
|
|
insertFirst(mmap->alloc_list, key, swapped_page);
|
|
return page;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
void page_remove(MMap *mmap, int key) {
|
|
PageInfo* page = page_lookup(mmap, key);
|
|
if (!page)
|
|
return;
|
|
|
|
deleteElement(mmap->alloc_list, key);
|
|
mmap->count--;
|
|
}
|
|
|
|
PageInfo *page_create(MMap *mmap, bool *moved_to_swap) {
|
|
if (mmap->count >= 2*PAGE_COUNT)
|
|
return NULL;
|
|
|
|
PageInfo* page = page_alloc(mmap);
|
|
*moved_to_swap = false;
|
|
|
|
if (page)
|
|
return page;
|
|
|
|
// Another page has been moved to swap
|
|
*moved_to_swap = true;
|
|
|
|
page = deleteFirst(mmap->alloc_list)->data;
|
|
move_to_swap(mmap, page);
|
|
// Reuse swapped page for the new one
|
|
page->key = mmap->count;
|
|
mmap->count++;
|
|
insertFirst(mmap->alloc_list, page->key, page);
|
|
|
|
return page;
|
|
} |