diff --git a/TP/TP1/rendu/Makefile b/TP/TP1/rendu/Makefile new file mode 100644 index 0000000..ea42c7d --- /dev/null +++ b/TP/TP1/rendu/Makefile @@ -0,0 +1,16 @@ +all: build/test + +build/test: test.c build/vmap.o build/mmap.o build/linked_list.o + gcc -o test test.c vmap.o mmap.o linked_list.o -w + +build/mmap.o: mmap.c mmap.h linked_list.c linked_list.h + gcc -c mmap.c + +build/vmap.o: vmap.c vmap.h + gcc -c vmap.c + +build/linked_list.o: linked_list.c linked_list.h + gcc -c linked_list.c + +clean: + rm -f build/* diff --git a/TP/TP1/rendu/linked_list.c b/TP/TP1/rendu/linked_list.c new file mode 100644 index 0000000..669b5f8 --- /dev/null +++ b/TP/TP1/rendu/linked_list.c @@ -0,0 +1,125 @@ +// linked list +#include +#include +#include + +#include "linked_list.h" + + +struct linkedList* createLinkedList() { + struct linkedList *list = (struct linkedList*) malloc(sizeof(struct linkedList)); + list->head = NULL; + return list; +} + +//insert link at the first location +void insertFirst(struct linkedList *list, int key, void *data) { + //create a link + struct node *link = (struct node*) malloc(sizeof(struct node)); + + link->key = key; + link->data = data; + + //point it to old first node + link->next = list->head; + + //point first to new first node + list->head = link; +} + +//delete first item +struct node* deleteFirst(struct linkedList *list) { + if (list->head == NULL) { + return NULL; + } + + //save reference to first link + struct node *tempLink = list->head; + + //mark next to first link as first + list->head = list->head->next; + + //return the deleted link + return tempLink; +} + +//find a link with given key +struct node* find(struct linkedList *list, int key) { + + //start from the first link + struct node* current = list->head; + + //if list is empty + if(list->head == NULL) { + return NULL; + } + + //navigate through list + while(current->key != key) { + + //if it is last node + if(current->next == NULL) { + return NULL; + } else { + //go to next link + current = current->next; + } + } + + //if data found, return the current Link + return current; +} + +//delete a link with given key +struct node* deleteElement(struct linkedList *list, int key) { + + //start from the first link + struct node* current = list->head; + struct node* previous = NULL; + + //if list is empty + if(list->head == NULL) { + return NULL; + } + + //navigate through list + while(current->key != key) { + + //if it is last node + if(current->next == NULL) { + return NULL; + } else { + //store reference to current link + previous = current; + //move to next link + current = current->next; + } + } + + //found a match, update the link + if(current == list->head) { + //change first to point to next link + list->head = list->head->next; + } else { + //bypass the current link + previous->next = current->next; + } + + return current; +} + +//is list empty +bool isEmpty(struct linkedList *list) { + return list->head == NULL; +} + +int length(struct linkedList *list) { + int length = 0; + struct node *current; + + for(current = list->head; current != NULL; current = current->next) { + length++; + } + + return length; +} \ No newline at end of file diff --git a/TP/TP1/rendu/linked_list.h b/TP/TP1/rendu/linked_list.h new file mode 100644 index 0000000..c47140a --- /dev/null +++ b/TP/TP1/rendu/linked_list.h @@ -0,0 +1,19 @@ +#include + +struct node { + void *data; + int key; + struct node *next; +}; + +struct linkedList { + struct node *head; +}; + +struct linkedList* createLinkedList(); +void insertFirst(struct linkedList *list, int key, void *data); +struct node* deleteFirst(struct linkedList *list); +struct node* find(struct linkedList *list, int key); +struct node* deleteElement(struct linkedList *list, int key); +bool isEmpty(struct linkedList *list); +int length(struct linkedList *list); \ No newline at end of file diff --git a/TP/TP1/rendu/mmap.c b/TP/TP1/rendu/mmap.c new file mode 100644 index 0000000..6714698 --- /dev/null +++ b/TP/TP1/rendu/mmap.c @@ -0,0 +1,140 @@ +#include +#include +#include +#include + +#include "mmap.h" + +struct MMap* mmap_init(){ + struct MMap *mmap = malloc(sizeof(struct MMap)); + mmap->size = 4096*100; + mmap->memory = malloc(mmap->size); + mmap->count = 0; + mmap->support = malloc(sizeof(struct PageInfo)); + mmap->support->data = malloc(4096); + mmap->free_list = createLinkedList(); + // insert 100 pages in the free list + for (int i = 0; i < 100; i++) { + struct PageInfo *page = malloc(sizeof(struct PageInfo)); + page->data = mmap->memory + i*4096; + 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 < 100*sizeof(int); i+=sizeof(int)) { + fwrite(&tmp, sizeof(int), 1, mmap->swap); + } + char tmp2 = '\0'; + for (int i = 0; i < 100*4096; i++) { + fwrite(&tmp2, 1, 1, mmap->swap); + } + return mmap; +} + +void mmap_clean(struct MMap *mmap) { + free(mmap->memory); + fclose(mmap->swap); + free(mmap); + remove("/tmp/myswap"); +} + +struct PageInfo* page_alloc(struct MMap *mmap) { + struct PageInfo *page = NULL; + page = page_free(mmap); + if(page!=NULL) { + insertFirst(mmap->alloc_list, page->key, page); + mmap->count++; + } + return page; +} + +struct PageInfo* page_free(struct MMap *mmap) { + struct PageInfo *page = NULL; + struct node *node = deleteFirst(mmap->free_list); + if (node != NULL) { + page = node->data; + } + return page; +} + +int check_page_free_list(struct MMap *mmap) { + return length(mmap->free_list); +} + +int check_page_alloc(struct MMap *mmap) { + return length(mmap->alloc_list); +} + +int move_to_swap(struct MMap *mmap, struct PageInfo *page) { + int key = -1; + fseek(mmap->swap, 0, SEEK_SET); + if( ferror(mmap->swap) ) { + return -1; + } + for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) { + fread(&key, sizeof(int), 1, mmap->swap); + if( ferror(mmap->swap) ) { + return -1; + } + if (key == -1) { + fseek(mmap->swap, i, SEEK_SET); + if( ferror(mmap->swap) ) { + return -1; + } + fwrite(&page->key, sizeof(int), 1, mmap->swap); + if( ferror(mmap->swap) ) { + return -1; + } + fseek(mmap->swap, 100*sizeof(int) + (i/sizeof(int))*4096, SEEK_SET); + if( ferror(mmap->swap) ) { + return -1; + } + fwrite(page->data, 4096, 1, mmap->swap); + if( ferror(mmap->swap) ) { + return -1; + } + return 0; + } + } + return -1; +} + +struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page) { + int key = -1; + fseek(mmap->swap, 0, SEEK_SET); + if( ferror(mmap->swap) ) { + return NULL; + } + for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) { + int ret = fread(&key, sizeof(int), 1, mmap->swap); + if( ferror(mmap->swap) ) { + return NULL; + } + if (key == page->key) { + fseek(mmap->swap, 100*sizeof(int) + (i/sizeof(int))*4096, SEEK_SET); + if( ferror(mmap->swap) ) { + return NULL; + } + fread(page->data, 4096, 1, mmap->swap); + if( ferror(mmap->swap) ) { + return NULL; + } + fseek(mmap->swap, i, SEEK_SET); + if( ferror(mmap->swap) ) { + return NULL; + } + int tmp = -1; + fwrite(&tmp, sizeof(int), 1, mmap->swap); + if( ferror(mmap->swap) ) { + return NULL; + } + return page; + } else { + return NULL; + } + } + return NULL; +} + diff --git a/TP/TP1/rendu/mmap.h b/TP/TP1/rendu/mmap.h new file mode 100644 index 0000000..33e886f --- /dev/null +++ b/TP/TP1/rendu/mmap.h @@ -0,0 +1,27 @@ +#include + +#include "linked_list.h" + +struct MMap { + void *memory; + int size; + int count; + struct PageInfo *support; + struct linkedList *free_list; + struct linkedList *alloc_list; + FILE *swap; +}; + +struct PageInfo { + int key; + void *data; +}; + +struct MMap* mmap_init(); +void mmap_clean(struct MMap *mmap); +struct PageInfo* page_alloc(struct MMap *mmap); +struct PageInfo* page_free(struct MMap *mmap); +int check_page_free_list(struct MMap *mmap); +int check_page_alloc(struct MMap *mmap); +int move_to_swap(struct MMap *mmap, struct PageInfo *page); +struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page); diff --git a/TP/TP1/rendu/test.c b/TP/TP1/rendu/test.c new file mode 100644 index 0000000..a42795f --- /dev/null +++ b/TP/TP1/rendu/test.c @@ -0,0 +1,8 @@ +#include + +#include "mmap.h" + +int main(int argc, char **argv) { + // Some really cool tests! + return 0; +} diff --git a/TP/TP1/sujet.pdf b/TP/TP1/sujet.pdf new file mode 100644 index 0000000..03c429e Binary files /dev/null and b/TP/TP1/sujet.pdf differ