Finish mmap

This commit is contained in:
augustin64 2024-03-27 11:07:57 +01:00
parent c06df0b0b7
commit 678b8c2808
2 changed files with 66 additions and 14 deletions

View File

@ -16,7 +16,7 @@ struct MMap* mmap_init(){
mmap->support->data = malloc(4096); mmap->support->data = malloc(4096);
mmap->free_list = createLinkedList(); mmap->free_list = createLinkedList();
// insert 100 pages in the free list // insert 100 pages in the free list
for (int i = 0; i < 100; i++) { for (int i=0; i < 100; i++) {
struct PageInfo *page = malloc(sizeof(struct PageInfo)); struct PageInfo *page = malloc(sizeof(struct PageInfo));
page->data = mmap->memory + i*4096; page->data = mmap->memory + i*4096;
page->key = i; page->key = i;
@ -25,11 +25,11 @@ struct MMap* mmap_init(){
mmap->alloc_list = createLinkedList(); mmap->alloc_list = createLinkedList();
mmap->swap = fopen("/tmp/myswap", "wb+"); mmap->swap = fopen("/tmp/myswap", "wb+");
int tmp = -1; int tmp = -1;
for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) { for (int i=0; i < 100; i++) {
fwrite(&tmp, sizeof(int), 1, mmap->swap); fwrite(&tmp, sizeof(int), 1, mmap->swap);
} }
char tmp2 = '\0'; char tmp2 = '\0';
for (int i = 0; i < 100*4096; i++) { for (int i=0; i < 100*4096; i++) {
fwrite(&tmp2, 1, 1, mmap->swap); fwrite(&tmp2, 1, 1, mmap->swap);
} }
return mmap; return mmap;
@ -45,7 +45,7 @@ void mmap_clean(struct MMap *mmap) {
struct PageInfo* page_alloc(struct MMap *mmap) { struct PageInfo* page_alloc(struct MMap *mmap) {
struct PageInfo *page = NULL; struct PageInfo *page = NULL;
page = page_free(mmap); page = page_free(mmap);
if(page!=NULL) { if (page) {
insertFirst(mmap->alloc_list, page->key, page); insertFirst(mmap->alloc_list, page->key, page);
mmap->count++; mmap->count++;
} }
@ -55,7 +55,7 @@ struct PageInfo* page_alloc(struct MMap *mmap) {
struct PageInfo* page_free(struct MMap *mmap) { struct PageInfo* page_free(struct MMap *mmap) {
struct PageInfo *page = NULL; struct PageInfo *page = NULL;
struct node *node = deleteFirst(mmap->free_list); struct node *node = deleteFirst(mmap->free_list);
if (node != NULL) { if (node) {
page = node->data; page = node->data;
} }
return page; return page;
@ -74,18 +74,18 @@ int move_to_swap(struct MMap *mmap, struct PageInfo *page) {
fseek(mmap->swap, 0, SEEK_SET); fseek(mmap->swap, 0, SEEK_SET);
CHECK_FERROR(mmap->swap); CHECK_FERROR(mmap->swap);
for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) { for (int i=0; i < 100; i++) {
fread(&key, sizeof(int), 1, mmap->swap); fread(&key, sizeof(int), 1, mmap->swap);
CHECK_FERROR(mmap->swap); CHECK_FERROR(mmap->swap);
if (key == -1) { if (key == -1) {
fseek(mmap->swap, i, SEEK_SET); fseek(mmap->swap, i*sizeof(int), SEEK_SET);
CHECK_FERROR(mmap->swap); CHECK_FERROR(mmap->swap);
fwrite(&page->key, sizeof(int), 1, mmap->swap); fwrite(&page->key, sizeof(int), 1, mmap->swap);
CHECK_FERROR(mmap->swap); CHECK_FERROR(mmap->swap);
fseek(mmap->swap, 100*sizeof(int) + (i/sizeof(int))*4096, SEEK_SET); fseek(mmap->swap, 100*sizeof(int) + i*4096, SEEK_SET);
CHECK_FERROR(mmap->swap); CHECK_FERROR(mmap->swap);
fwrite(page->data, 4096, 1, mmap->swap); fwrite(page->data, 4096, 1, mmap->swap);
@ -102,20 +102,20 @@ struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page) {
fseek(mmap->swap, 0, SEEK_SET); fseek(mmap->swap, 0, SEEK_SET);
CHECK_FERROR(mmap->swap); CHECK_FERROR(mmap->swap);
for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) { for (int i=0; i < 100; i++) {
int ret = fread(&key, sizeof(int), 1, mmap->swap); int ret = fread(&key, sizeof(int), 1, mmap->swap);
CHECK_FERROR(mmap->swap); CHECK_FERROR(mmap->swap);
if (key != page->key) if (key != page->key)
continue; continue;
fseek(mmap->swap, 100*sizeof(int) + (i/sizeof(int))*4096, SEEK_SET); fseek(mmap->swap, 100*sizeof(int) + i*4096, SEEK_SET);
CHECK_FERROR(mmap->swap); CHECK_FERROR(mmap->swap);
fread(page->data, 4096, 1, mmap->swap); fread(page->data, 4096, 1, mmap->swap);
CHECK_FERROR(mmap->swap); CHECK_FERROR(mmap->swap);
fseek(mmap->swap, i, SEEK_SET); fseek(mmap->swap, i*sizeof(int), SEEK_SET);
CHECK_FERROR(mmap->swap); CHECK_FERROR(mmap->swap);
int tmp = -1; int tmp = -1;
@ -127,3 +127,54 @@ struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page) {
return NULL; return NULL;
} }
struct PageInfo* page_lookup(struct MMap* mmap, int key) {
struct node* data = find(mmap->alloc_list, key);
if (data) {
return data->data;
}
mmap->support->key = key;
struct PageInfo* page = read_from_swap(mmap, mmap->support);
if (page) { // Move one elem from mem to swap
struct PageInfo* swapped_page = deleteFirst(mmap->alloc_list)->data;
move_to_swap(mmap, swapped_page);
memcpy(swapped_page->data, mmap->support->data, 4096);
swapped_page->key = key;
insertFirst(mmap->alloc_list, key, swapped_page);
return page;
}
return NULL;
}
void page_remove(struct MMap *mmap, int key) {
struct PageInfo* page = page_lookup(mmap, key);
if (!page)
return;
deleteElement(mmap->alloc_list, key);
mmap->count--;
}
struct PageInfo *page_create(struct MMap *mmap, bool *moved_to_swap) {
if (mmap->count >= 200)
return NULL;
struct 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;
}

View File

@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
#include "linked_list.h" #include "linked_list.h"
@ -27,4 +28,4 @@ int move_to_swap(struct MMap *mmap, struct PageInfo *page);
struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page); struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page);
struct PageInfo *page_lookup(struct MMap *mmap, int key); struct PageInfo *page_lookup(struct MMap *mmap, int key);
void page_remove(struct MMap *mmap, int key); void page_remove(struct MMap *mmap, int key);
struct PageInfo *page_create(struct MMap *mmap, int *moved_to_swap); struct PageInfo *page_create(struct MMap *mmap, bool *moved_to_swap);