diff --git a/TP/TP1/rendu/linked_list.c b/TP/TP1/rendu/linked_list.c index 669b5f8..f772e8e 100644 --- a/TP/TP1/rendu/linked_list.c +++ b/TP/TP1/rendu/linked_list.c @@ -7,119 +7,119 @@ struct linkedList* createLinkedList() { - struct linkedList *list = (struct linkedList*) malloc(sizeof(struct linkedList)); - list->head = NULL; - return list; + 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)); + //create a link + struct node *link = (struct node*) malloc(sizeof(struct node)); - link->key = key; - link->data = data; + link->key = key; + link->data = data; - //point it to old first node - link->next = list->head; + //point it to old first node + link->next = list->head; - //point first to new first node - list->head = link; + //point first to new first node + list->head = link; } //delete first item struct node* deleteFirst(struct linkedList *list) { - if (list->head == NULL) { - return NULL; - } + if (list->head == NULL) { + return NULL; + } - //save reference to first link - struct node *tempLink = list->head; + //save reference to first link + struct node *tempLink = list->head; - //mark next to first link as first - list->head = list->head->next; + //mark next to first link as first + list->head = list->head->next; - //return the deleted link - return tempLink; + //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; + //start from the first link + struct node* current = list->head; - //if list is empty - if(list->head == NULL) { - return NULL; - } + //if list is empty + if(list->head == NULL) { + return NULL; + } - //navigate through list - while(current->key != key) { + //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 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; + //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; + //start from the first link + struct node* current = list->head; + struct node* previous = NULL; - //if list is empty - if(list->head == NULL) { - return NULL; - } + //if list is empty + if(list->head == NULL) { + return NULL; + } - //navigate through list - while(current->key != key) { + //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; - } - } + //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; - } + //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; + return current; } //is list empty bool isEmpty(struct linkedList *list) { - return list->head == NULL; + return list->head == NULL; } int length(struct linkedList *list) { - int length = 0; - struct node *current; + int length = 0; + struct node *current; - for(current = list->head; current != NULL; current = current->next) { - length++; - } + for(current = list->head; current != NULL; current = current->next) { + length++; + } - return 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 index c47140a..55a7c00 100644 --- a/TP/TP1/rendu/linked_list.h +++ b/TP/TP1/rendu/linked_list.h @@ -1,14 +1,14 @@ #include struct node { - void *data; - int key; - struct node *next; + void *data; + int key; + struct node *next; }; struct linkedList { - struct node *head; -}; + struct node *head; +}; struct linkedList* createLinkedList(); void insertFirst(struct linkedList *list, int key, void *data); diff --git a/TP/TP1/rendu/mmap.c b/TP/TP1/rendu/mmap.c index 421270f..523e5ee 100644 --- a/TP/TP1/rendu/mmap.c +++ b/TP/TP1/rendu/mmap.c @@ -8,122 +8,122 @@ #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; + 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"); + 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 = 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; + 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); + return length(mmap->free_list); } int check_page_alloc(struct MMap *mmap) { - return length(mmap->alloc_list); + return length(mmap->alloc_list); } int move_to_swap(struct MMap *mmap, struct PageInfo *page) { - int key = -1; - fseek(mmap->swap, 0, SEEK_SET); - CHECK_FERROR(mmap->swap); - - for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) { - fread(&key, sizeof(int), 1, mmap->swap); + int key = -1; + fseek(mmap->swap, 0, SEEK_SET); CHECK_FERROR(mmap->swap); - if (key == -1) { - fseek(mmap->swap, i, SEEK_SET); - CHECK_FERROR(mmap->swap); + for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) { + fread(&key, sizeof(int), 1, mmap->swap); + CHECK_FERROR(mmap->swap); - fwrite(&page->key, sizeof(int), 1, mmap->swap); - CHECK_FERROR(mmap->swap); - - fseek(mmap->swap, 100*sizeof(int) + (i/sizeof(int))*4096, SEEK_SET); - CHECK_FERROR(mmap->swap); + if (key == -1) { + fseek(mmap->swap, i, SEEK_SET); + CHECK_FERROR(mmap->swap); - fwrite(page->data, 4096, 1, mmap->swap); - CHECK_FERROR(mmap->swap); + fwrite(&page->key, sizeof(int), 1, mmap->swap); + CHECK_FERROR(mmap->swap); - return 0; + fseek(mmap->swap, 100*sizeof(int) + (i/sizeof(int))*4096, SEEK_SET); + CHECK_FERROR(mmap->swap); + + fwrite(page->data, 4096, 1, mmap->swap); + CHECK_FERROR(mmap->swap); + + return 0; + } } - } - return -1; + return -1; } struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page) { - int key = -1; - fseek(mmap->swap, 0, SEEK_SET); - CHECK_FERROR(mmap->swap); - - for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) { - int ret = fread(&key, sizeof(int), 1, mmap->swap); - CHECK_FERROR(mmap->swap); - - if (key != page->key) - continue; - - fseek(mmap->swap, 100*sizeof(int) + (i/sizeof(int))*4096, SEEK_SET); + int key = -1; + fseek(mmap->swap, 0, SEEK_SET); CHECK_FERROR(mmap->swap); - fread(page->data, 4096, 1, mmap->swap); - CHECK_FERROR(mmap->swap); + for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) { + int ret = fread(&key, sizeof(int), 1, mmap->swap); + CHECK_FERROR(mmap->swap); - fseek(mmap->swap, i, SEEK_SET); - CHECK_FERROR(mmap->swap); + if (key != page->key) + continue; - int tmp = -1; - fwrite(&tmp, sizeof(int), 1, mmap->swap); - CHECK_FERROR(mmap->swap); + fseek(mmap->swap, 100*sizeof(int) + (i/sizeof(int))*4096, SEEK_SET); + CHECK_FERROR(mmap->swap); - return page; - } - return NULL; + fread(page->data, 4096, 1, mmap->swap); + CHECK_FERROR(mmap->swap); + + fseek(mmap->swap, i, SEEK_SET); + CHECK_FERROR(mmap->swap); + + int tmp = -1; + fwrite(&tmp, sizeof(int), 1, mmap->swap); + CHECK_FERROR(mmap->swap); + + return page; + } + return NULL; } diff --git a/TP/TP1/rendu/mmap.h b/TP/TP1/rendu/mmap.h index 33e886f..495c6d8 100644 --- a/TP/TP1/rendu/mmap.h +++ b/TP/TP1/rendu/mmap.h @@ -3,18 +3,18 @@ #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; + 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; + int key; + void *data; }; struct MMap* mmap_init(); @@ -25,3 +25,6 @@ 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); +struct PageInfo *page_lookup(struct MMap *mmap, int key); +void page_remove(struct MMap *mmap, int key); +struct PageInfo *page_create(struct MMap *mmap, int *moved_to_swap); \ No newline at end of file diff --git a/TP/TP1/rendu/test.c b/TP/TP1/rendu/test.c index a42795f..5855607 100644 --- a/TP/TP1/rendu/test.c +++ b/TP/TP1/rendu/test.c @@ -3,6 +3,6 @@ #include "mmap.h" int main(int argc, char **argv) { - // Some really cool tests! - return 0; + // Some really cool tests! + return 0; } diff --git a/TP/TP1/rendu/vmap.h b/TP/TP1/rendu/vmap.h new file mode 100644 index 0000000..fb30ca8 --- /dev/null +++ b/TP/TP1/rendu/vmap.h @@ -0,0 +1,3 @@ +void *my_malloc(int size); +void my_free(void *data); +void my_copy(void *mem, void *data, int size); \ No newline at end of file