ArchiSys/TP/TP1/rendu/mmap.c

181 lines
4.5 KiB
C
Raw Normal View History

2024-03-26 17:06:15 +01:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
2024-03-26 17:40:15 +01:00
#define CHECK_FERROR(f) { if (ferror(f)) {fprintf(stderr, "IO Error"); exit(1);} }
2024-03-31 18:54:56 +02:00
#include "config.h"
2024-03-26 17:06:15 +01:00
#include "mmap.h"
2024-03-29 22:46:30 +01:00
MMap* mmap_init(){
MMap *mmap = malloc(sizeof(MMap));
2024-03-31 18:54:56 +02:00
mmap->size = PAGE_SIZE*PAGE_COUNT;
2024-03-26 17:45:20 +01:00
mmap->memory = malloc(mmap->size);
mmap->count = 0;
2024-03-29 22:46:30 +01:00
mmap->support = malloc(sizeof(PageInfo));
2024-03-31 18:54:56 +02:00
mmap->support->data = malloc(PAGE_SIZE);
2024-03-26 17:45:20 +01:00
mmap->free_list = createLinkedList();
2024-03-31 18:54:56 +02:00
// insert PAGE_COUNT pages in the free list
for (int i=0; i < PAGE_COUNT; i++) {
2024-03-29 22:46:30 +01:00
PageInfo *page = malloc(sizeof(PageInfo));
2024-03-31 18:54:56 +02:00
page->data = mmap->memory + i*PAGE_SIZE;
2024-03-26 17:45:20 +01:00
page->key = i;
insertFirst(mmap->free_list, i, page);
}
mmap->alloc_list = createLinkedList();
mmap->swap = fopen("/tmp/myswap", "wb+");
int tmp = -1;
2024-03-31 18:54:56 +02:00
for (int i=0; i < PAGE_COUNT; i++) {
2024-03-27 11:07:57 +01:00
fwrite(&tmp, sizeof(int), 1, mmap->swap);
2024-03-26 17:45:20 +01:00
}
char tmp2 = '\0';
2024-03-31 18:54:56 +02:00
for (int i=0; i < PAGE_COUNT*PAGE_SIZE; i++) {
2024-03-26 17:45:20 +01:00
fwrite(&tmp2, 1, 1, mmap->swap);
}
return mmap;
2024-03-26 17:06:15 +01:00
}
2024-03-29 22:46:30 +01:00
void mmap_clean(MMap *mmap) {
2024-03-26 17:45:20 +01:00
free(mmap->memory);
fclose(mmap->swap);
free(mmap);
remove("/tmp/myswap");
2024-03-26 17:06:15 +01:00
}
2024-03-29 22:46:30 +01:00
PageInfo* page_alloc(MMap *mmap) {
PageInfo *page = NULL;
2024-03-26 17:45:20 +01:00
page = page_free(mmap);
2024-03-27 11:07:57 +01:00
if (page) {
2024-03-26 17:45:20 +01:00
insertFirst(mmap->alloc_list, page->key, page);
mmap->count++;
}
return page;
2024-03-26 17:06:15 +01:00
}
2024-03-29 22:46:30 +01:00
PageInfo* page_free(MMap *mmap) {
PageInfo *page = NULL;
node *node = deleteFirst(mmap->free_list);
2024-03-27 11:07:57 +01:00
if (node) {
2024-03-26 17:45:20 +01:00
page = node->data;
}
return page;
2024-03-26 17:06:15 +01:00
}
2024-03-29 22:46:30 +01:00
int check_page_free_list(MMap *mmap) {
2024-03-26 17:45:20 +01:00
return length(mmap->free_list);
2024-03-26 17:06:15 +01:00
}
2024-03-29 22:46:30 +01:00
int check_page_alloc(MMap *mmap) {
2024-03-26 17:45:20 +01:00
return length(mmap->alloc_list);
2024-03-26 17:06:15 +01:00
}
2024-03-29 22:46:30 +01:00
int move_to_swap(MMap *mmap, PageInfo *page) {
2024-03-26 17:45:20 +01:00
int key = -1;
fseek(mmap->swap, 0, SEEK_SET);
2024-03-26 17:40:15 +01:00
CHECK_FERROR(mmap->swap);
2024-03-31 18:54:56 +02:00
for (int i=0; i < PAGE_COUNT; i++) {
2024-03-26 17:45:20 +01:00
fread(&key, sizeof(int), 1, mmap->swap);
CHECK_FERROR(mmap->swap);
if (key == -1) {
2024-03-27 11:07:57 +01:00
fseek(mmap->swap, i*sizeof(int), SEEK_SET);
2024-03-26 17:45:20 +01:00
CHECK_FERROR(mmap->swap);
2024-03-26 17:40:15 +01:00
2024-03-26 17:45:20 +01:00
fwrite(&page->key, sizeof(int), 1, mmap->swap);
CHECK_FERROR(mmap->swap);
2024-03-26 17:40:15 +01:00
2024-03-31 18:54:56 +02:00
fseek(mmap->swap, PAGE_COUNT*sizeof(int) + i*PAGE_SIZE, SEEK_SET);
2024-03-26 17:45:20 +01:00
CHECK_FERROR(mmap->swap);
2024-03-26 17:40:15 +01:00
2024-03-31 18:54:56 +02:00
fwrite(page->data, PAGE_SIZE, 1, mmap->swap);
2024-03-26 17:45:20 +01:00
CHECK_FERROR(mmap->swap);
return 0;
}
2024-03-26 17:06:15 +01:00
}
2024-03-26 17:45:20 +01:00
return -1;
2024-03-26 17:06:15 +01:00
}
2024-03-29 22:46:30 +01:00
PageInfo* read_from_swap(MMap* mmap, PageInfo *page) {
2024-03-26 17:45:20 +01:00
int key = -1;
fseek(mmap->swap, 0, SEEK_SET);
2024-03-26 17:40:15 +01:00
CHECK_FERROR(mmap->swap);
2024-03-31 18:54:56 +02:00
for (int i=0; i < PAGE_COUNT; i++) {
(void)fread(&key, sizeof(int), 1, mmap->swap);
2024-03-27 11:07:57 +01:00
CHECK_FERROR(mmap->swap);
2024-03-26 17:40:15 +01:00
2024-03-26 17:45:20 +01:00
if (key != page->key)
continue;
2024-03-26 17:40:15 +01:00
2024-03-31 18:54:56 +02:00
fseek(mmap->swap, PAGE_COUNT*sizeof(int) + i*PAGE_SIZE, SEEK_SET);
2024-03-26 17:45:20 +01:00
CHECK_FERROR(mmap->swap);
2024-03-26 17:40:15 +01:00
2024-03-31 18:54:56 +02:00
fread(page->data, PAGE_SIZE, 1, mmap->swap);
2024-03-26 17:45:20 +01:00
CHECK_FERROR(mmap->swap);
2024-03-27 11:07:57 +01:00
fseek(mmap->swap, i*sizeof(int), SEEK_SET);
2024-03-26 17:45:20 +01:00
CHECK_FERROR(mmap->swap);
int tmp = -1;
fwrite(&tmp, sizeof(int), 1, mmap->swap);
CHECK_FERROR(mmap->swap);
return page;
}
return NULL;
2024-03-26 17:06:15 +01:00
}
2024-03-29 22:46:30 +01:00
PageInfo* page_lookup(MMap* mmap, int key) {
node* data = find(mmap->alloc_list, key);
2024-03-27 11:07:57 +01:00
if (data) {
return data->data;
}
mmap->support->key = key;
2024-03-29 22:46:30 +01:00
PageInfo* page = read_from_swap(mmap, mmap->support);
2024-03-27 11:07:57 +01:00
if (page) { // Move one elem from mem to swap
2024-03-29 22:46:30 +01:00
PageInfo* swapped_page = deleteFirst(mmap->alloc_list)->data;
2024-03-27 11:07:57 +01:00
move_to_swap(mmap, swapped_page);
2024-03-31 18:54:56 +02:00
memcpy(swapped_page->data, mmap->support->data, PAGE_SIZE);
2024-03-27 11:07:57 +01:00
swapped_page->key = key;
insertFirst(mmap->alloc_list, key, swapped_page);
return page;
}
return NULL;
}
2024-03-29 22:46:30 +01:00
void page_remove(MMap *mmap, int key) {
PageInfo* page = page_lookup(mmap, key);
2024-03-27 11:07:57 +01:00
if (!page)
return;
deleteElement(mmap->alloc_list, key);
mmap->count--;
}
2024-03-29 22:46:30 +01:00
PageInfo *page_create(MMap *mmap, bool *moved_to_swap) {
2024-03-31 18:54:56 +02:00
if (mmap->count >= 2*PAGE_COUNT)
2024-03-27 11:07:57 +01:00
return NULL;
2024-03-29 22:46:30 +01:00
PageInfo* page = page_alloc(mmap);
2024-03-27 11:07:57 +01:00
*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;
}