This commit is contained in:
augustin64 2024-03-29 22:46:30 +01:00
parent 678b8c2808
commit 1edff7956e
4 changed files with 67 additions and 67 deletions

View File

@ -6,16 +6,16 @@
#include "linked_list.h" #include "linked_list.h"
struct linkedList* createLinkedList() { linkedList* createLinkedList() {
struct linkedList *list = (struct linkedList*)malloc(sizeof(struct linkedList)); linkedList *list = (linkedList*)malloc(sizeof(linkedList));
list->head = NULL; list->head = NULL;
return list; return list;
} }
//insert link at the first location //insert link at the first location
void insertFirst(struct linkedList *list, int key, void *data) { void insertFirst(linkedList *list, int key, void *data) {
//create a link //create a link
struct node *link = (struct node*) malloc(sizeof(struct node)); node *link = (node*) malloc(sizeof(node));
link->key = key; link->key = key;
link->data = data; link->data = data;
@ -28,13 +28,13 @@ void insertFirst(struct linkedList *list, int key, void *data) {
} }
//delete first item //delete first item
struct node* deleteFirst(struct linkedList *list) { node* deleteFirst(linkedList *list) {
if (list->head == NULL) { if (list->head == NULL) {
return NULL; return NULL;
} }
//save reference to first link //save reference to first link
struct node *tempLink = list->head; node *tempLink = list->head;
//mark next to first link as first //mark next to first link as first
list->head = list->head->next; list->head = list->head->next;
@ -44,10 +44,10 @@ struct node* deleteFirst(struct linkedList *list) {
} }
//find a link with given key //find a link with given key
struct node* find(struct linkedList *list, int key) { node* find(linkedList *list, int key) {
//start from the first link //start from the first link
struct node* current = list->head; node* current = list->head;
//if list is empty //if list is empty
if(list->head == NULL) { if(list->head == NULL) {
@ -71,11 +71,11 @@ struct node* find(struct linkedList *list, int key) {
} }
//delete a link with given key //delete a link with given key
struct node* deleteElement(struct linkedList *list, int key) { node* deleteElement(linkedList *list, int key) {
//start from the first link //start from the first link
struct node* current = list->head; node* current = list->head;
struct node* previous = NULL; node* previous = NULL;
//if list is empty //if list is empty
if(list->head == NULL) { if(list->head == NULL) {
@ -109,13 +109,13 @@ struct node* deleteElement(struct linkedList *list, int key) {
} }
//is list empty //is list empty
bool isEmpty(struct linkedList *list) { bool isEmpty(linkedList *list) {
return list->head == NULL; return list->head == NULL;
} }
int length(struct linkedList *list) { int length(linkedList *list) {
int length = 0; int length = 0;
struct node *current; node *current;
for(current = list->head; current != NULL; current = current->next) { for(current = list->head; current != NULL; current = current->next) {
length++; length++;

View File

@ -1,19 +1,19 @@
#include <stdbool.h> #include <stdbool.h>
struct node { typedef struct node {
void *data; void *data;
int key; int key;
struct node *next; node *next;
}; } node;
struct linkedList { typedef struct linkedList {
struct node *head; node *head;
}; } linkedList;
struct linkedList* createLinkedList(); linkedList* createLinkedList();
void insertFirst(struct linkedList *list, int key, void *data); void insertFirst(linkedList *list, int key, void *data);
struct node* deleteFirst(struct linkedList *list); node* deleteFirst(linkedList *list);
struct node* find(struct linkedList *list, int key); node* find(linkedList *list, int key);
struct node* deleteElement(struct linkedList *list, int key); node* deleteElement(linkedList *list, int key);
bool isEmpty(struct linkedList *list); bool isEmpty(linkedList *list);
int length(struct linkedList *list); int length(linkedList *list);

View File

@ -7,17 +7,17 @@
#include "mmap.h" #include "mmap.h"
struct MMap* mmap_init(){ MMap* mmap_init(){
struct MMap *mmap = malloc(sizeof(struct MMap)); MMap *mmap = malloc(sizeof(MMap));
mmap->size = 4096*100; mmap->size = 4096*100;
mmap->memory = malloc(mmap->size); mmap->memory = malloc(mmap->size);
mmap->count = 0; mmap->count = 0;
mmap->support = malloc(sizeof(struct PageInfo)); mmap->support = malloc(sizeof(PageInfo));
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)); PageInfo *page = malloc(sizeof(PageInfo));
page->data = mmap->memory + i*4096; page->data = mmap->memory + i*4096;
page->key = i; page->key = i;
insertFirst(mmap->free_list, i, page); insertFirst(mmap->free_list, i, page);
@ -35,15 +35,15 @@ struct MMap* mmap_init(){
return mmap; return mmap;
} }
void mmap_clean(struct MMap *mmap) { void mmap_clean(MMap *mmap) {
free(mmap->memory); free(mmap->memory);
fclose(mmap->swap); fclose(mmap->swap);
free(mmap); free(mmap);
remove("/tmp/myswap"); remove("/tmp/myswap");
} }
struct PageInfo* page_alloc(struct MMap *mmap) { PageInfo* page_alloc(MMap *mmap) {
struct PageInfo *page = NULL; PageInfo *page = NULL;
page = page_free(mmap); page = page_free(mmap);
if (page) { if (page) {
insertFirst(mmap->alloc_list, page->key, page); insertFirst(mmap->alloc_list, page->key, page);
@ -52,24 +52,24 @@ struct PageInfo* page_alloc(struct MMap *mmap) {
return page; return page;
} }
struct PageInfo* page_free(struct MMap *mmap) { PageInfo* page_free(MMap *mmap) {
struct PageInfo *page = NULL; PageInfo *page = NULL;
struct node *node = deleteFirst(mmap->free_list); node *node = deleteFirst(mmap->free_list);
if (node) { if (node) {
page = node->data; page = node->data;
} }
return page; return page;
} }
int check_page_free_list(struct MMap *mmap) { int check_page_free_list(MMap *mmap) {
return length(mmap->free_list); return length(mmap->free_list);
} }
int check_page_alloc(struct MMap *mmap) { int check_page_alloc(MMap *mmap) {
return length(mmap->alloc_list); return length(mmap->alloc_list);
} }
int move_to_swap(struct MMap *mmap, struct PageInfo *page) { int move_to_swap(MMap *mmap, PageInfo *page) {
int key = -1; int key = -1;
fseek(mmap->swap, 0, SEEK_SET); fseek(mmap->swap, 0, SEEK_SET);
CHECK_FERROR(mmap->swap); CHECK_FERROR(mmap->swap);
@ -97,7 +97,7 @@ int move_to_swap(struct MMap *mmap, struct PageInfo *page) {
return -1; return -1;
} }
struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page) { PageInfo* read_from_swap(MMap* mmap, PageInfo *page) {
int key = -1; int key = -1;
fseek(mmap->swap, 0, SEEK_SET); fseek(mmap->swap, 0, SEEK_SET);
CHECK_FERROR(mmap->swap); CHECK_FERROR(mmap->swap);
@ -127,16 +127,16 @@ struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page) {
return NULL; return NULL;
} }
struct PageInfo* page_lookup(struct MMap* mmap, int key) { PageInfo* page_lookup(MMap* mmap, int key) {
struct node* data = find(mmap->alloc_list, key); node* data = find(mmap->alloc_list, key);
if (data) { if (data) {
return data->data; return data->data;
} }
mmap->support->key = key; mmap->support->key = key;
struct PageInfo* page = read_from_swap(mmap, mmap->support); PageInfo* page = read_from_swap(mmap, mmap->support);
if (page) { // Move one elem from mem to swap if (page) { // Move one elem from mem to swap
struct PageInfo* swapped_page = deleteFirst(mmap->alloc_list)->data; PageInfo* swapped_page = deleteFirst(mmap->alloc_list)->data;
move_to_swap(mmap, swapped_page); move_to_swap(mmap, swapped_page);
memcpy(swapped_page->data, mmap->support->data, 4096); memcpy(swapped_page->data, mmap->support->data, 4096);
@ -147,8 +147,8 @@ struct PageInfo* page_lookup(struct MMap* mmap, int key) {
return NULL; return NULL;
} }
void page_remove(struct MMap *mmap, int key) { void page_remove(MMap *mmap, int key) {
struct PageInfo* page = page_lookup(mmap, key); PageInfo* page = page_lookup(mmap, key);
if (!page) if (!page)
return; return;
@ -156,11 +156,11 @@ void page_remove(struct MMap *mmap, int key) {
mmap->count--; mmap->count--;
} }
struct PageInfo *page_create(struct MMap *mmap, bool *moved_to_swap) { PageInfo *page_create(MMap *mmap, bool *moved_to_swap) {
if (mmap->count >= 200) if (mmap->count >= 200)
return NULL; return NULL;
struct PageInfo* page = page_alloc(mmap); PageInfo* page = page_alloc(mmap);
*moved_to_swap = false; *moved_to_swap = false;
if (page) if (page)

View File

@ -3,29 +3,29 @@
#include "linked_list.h" #include "linked_list.h"
struct MMap { typedef struct MMap {
void *memory; void *memory;
int size; int size;
int count; int count;
struct PageInfo *support; PageInfo *support;
struct linkedList *free_list; linkedList *free_list;
struct linkedList *alloc_list; linkedList *alloc_list;
FILE *swap; FILE *swap;
}; } MMap;
struct PageInfo { typedef struct PageInfo {
int key; int key;
void *data; void *data;
}; } PageInfo;
struct MMap* mmap_init(); MMap* mmap_init();
void mmap_clean(struct MMap *mmap); void mmap_clean(MMap *mmap);
struct PageInfo* page_alloc(struct MMap *mmap); PageInfo* page_alloc(MMap *mmap);
struct PageInfo* page_free(struct MMap *mmap); PageInfo* page_free(MMap *mmap);
int check_page_free_list(struct MMap *mmap); int check_page_free_list(MMap *mmap);
int check_page_alloc(struct MMap *mmap); int check_page_alloc(MMap *mmap);
int move_to_swap(struct MMap *mmap, struct PageInfo *page); int move_to_swap(MMap *mmap, PageInfo *page);
struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page); PageInfo* read_from_swap(MMap* mmap, PageInfo *page);
struct PageInfo *page_lookup(struct MMap *mmap, int key); PageInfo *page_lookup(MMap *mmap, int key);
void page_remove(struct MMap *mmap, int key); void page_remove(MMap *mmap, int key);
struct PageInfo *page_create(struct MMap *mmap, bool *moved_to_swap); PageInfo *page_create(MMap *mmap, bool *moved_to_swap);