Coherent indentation
This commit is contained in:
parent
ea10ad5df1
commit
c06df0b0b7
@ -7,119 +7,119 @@
|
|||||||
|
|
||||||
|
|
||||||
struct linkedList* createLinkedList() {
|
struct linkedList* createLinkedList() {
|
||||||
struct linkedList *list = (struct linkedList*) malloc(sizeof(struct linkedList));
|
struct linkedList *list = (struct linkedList*)malloc(sizeof(struct 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(struct linkedList *list, int key, void *data) {
|
||||||
//create a link
|
//create a link
|
||||||
struct node *link = (struct node*) malloc(sizeof(struct node));
|
struct node *link = (struct node*) malloc(sizeof(struct node));
|
||||||
|
|
||||||
link->key = key;
|
link->key = key;
|
||||||
link->data = data;
|
link->data = data;
|
||||||
|
|
||||||
//point it to old first node
|
//point it to old first node
|
||||||
link->next = list->head;
|
link->next = list->head;
|
||||||
|
|
||||||
//point first to new first node
|
//point first to new first node
|
||||||
list->head = link;
|
list->head = link;
|
||||||
}
|
}
|
||||||
|
|
||||||
//delete first item
|
//delete first item
|
||||||
struct node* deleteFirst(struct linkedList *list) {
|
struct node* deleteFirst(struct 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;
|
struct 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;
|
||||||
|
|
||||||
//return the deleted link
|
//return the deleted link
|
||||||
return tempLink;
|
return tempLink;
|
||||||
}
|
}
|
||||||
|
|
||||||
//find a link with given key
|
//find a link with given key
|
||||||
struct node* find(struct linkedList *list, int key) {
|
struct node* find(struct linkedList *list, int key) {
|
||||||
|
|
||||||
//start from the first link
|
//start from the first link
|
||||||
struct node* current = list->head;
|
struct node* current = list->head;
|
||||||
|
|
||||||
//if list is empty
|
//if list is empty
|
||||||
if(list->head == NULL) {
|
if(list->head == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
//navigate through list
|
//navigate through list
|
||||||
while(current->key != key) {
|
while(current->key != key) {
|
||||||
|
|
||||||
//if it is last node
|
//if it is last node
|
||||||
if(current->next == NULL) {
|
if(current->next == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
} else {
|
} else {
|
||||||
//go to next link
|
//go to next link
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//if data found, return the current Link
|
//if data found, return the current Link
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
//delete a link with given key
|
//delete a link with given key
|
||||||
struct node* deleteElement(struct linkedList *list, int key) {
|
struct node* deleteElement(struct linkedList *list, int key) {
|
||||||
|
|
||||||
//start from the first link
|
//start from the first link
|
||||||
struct node* current = list->head;
|
struct node* current = list->head;
|
||||||
struct node* previous = NULL;
|
struct node* previous = NULL;
|
||||||
|
|
||||||
//if list is empty
|
//if list is empty
|
||||||
if(list->head == NULL) {
|
if(list->head == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
//navigate through list
|
//navigate through list
|
||||||
while(current->key != key) {
|
while(current->key != key) {
|
||||||
|
|
||||||
//if it is last node
|
//if it is last node
|
||||||
if(current->next == NULL) {
|
if(current->next == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
} else {
|
} else {
|
||||||
//store reference to current link
|
//store reference to current link
|
||||||
previous = current;
|
previous = current;
|
||||||
//move to next link
|
//move to next link
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//found a match, update the link
|
//found a match, update the link
|
||||||
if(current == list->head) {
|
if(current == list->head) {
|
||||||
//change first to point to next link
|
//change first to point to next link
|
||||||
list->head = list->head->next;
|
list->head = list->head->next;
|
||||||
} else {
|
} else {
|
||||||
//bypass the current link
|
//bypass the current link
|
||||||
previous->next = current->next;
|
previous->next = current->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
//is list empty
|
//is list empty
|
||||||
bool isEmpty(struct linkedList *list) {
|
bool isEmpty(struct linkedList *list) {
|
||||||
return list->head == NULL;
|
return list->head == NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int length(struct linkedList *list) {
|
int length(struct linkedList *list) {
|
||||||
int length = 0;
|
int length = 0;
|
||||||
struct node *current;
|
struct node *current;
|
||||||
|
|
||||||
for(current = list->head; current != NULL; current = current->next) {
|
for(current = list->head; current != NULL; current = current->next) {
|
||||||
length++;
|
length++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
@ -1,14 +1,14 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
struct node {
|
struct node {
|
||||||
void *data;
|
void *data;
|
||||||
int key;
|
int key;
|
||||||
struct node *next;
|
struct node *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct linkedList {
|
struct linkedList {
|
||||||
struct node *head;
|
struct node *head;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct linkedList* createLinkedList();
|
struct linkedList* createLinkedList();
|
||||||
void insertFirst(struct linkedList *list, int key, void *data);
|
void insertFirst(struct linkedList *list, int key, void *data);
|
||||||
|
@ -8,122 +8,122 @@
|
|||||||
#include "mmap.h"
|
#include "mmap.h"
|
||||||
|
|
||||||
struct MMap* mmap_init(){
|
struct MMap* mmap_init(){
|
||||||
struct MMap *mmap = malloc(sizeof(struct MMap));
|
struct MMap *mmap = malloc(sizeof(struct 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(struct 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));
|
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;
|
||||||
insertFirst(mmap->free_list, i, page);
|
insertFirst(mmap->free_list, i, page);
|
||||||
}
|
}
|
||||||
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*sizeof(int); i+=sizeof(int)) {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
void mmap_clean(struct MMap *mmap) {
|
void mmap_clean(struct 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) {
|
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!=NULL) {
|
||||||
insertFirst(mmap->alloc_list, page->key, page);
|
insertFirst(mmap->alloc_list, page->key, page);
|
||||||
mmap->count++;
|
mmap->count++;
|
||||||
}
|
}
|
||||||
return page;
|
return page;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 != NULL) {
|
||||||
page = node->data;
|
page = node->data;
|
||||||
}
|
}
|
||||||
return page;
|
return page;
|
||||||
}
|
}
|
||||||
|
|
||||||
int check_page_free_list(struct MMap *mmap) {
|
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) {
|
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 move_to_swap(struct MMap *mmap, struct PageInfo *page) {
|
||||||
int key = -1;
|
int key = -1;
|
||||||
fseek(mmap->swap, 0, SEEK_SET);
|
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);
|
|
||||||
CHECK_FERROR(mmap->swap);
|
CHECK_FERROR(mmap->swap);
|
||||||
|
|
||||||
if (key == -1) {
|
for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) {
|
||||||
fseek(mmap->swap, i, SEEK_SET);
|
fread(&key, sizeof(int), 1, mmap->swap);
|
||||||
CHECK_FERROR(mmap->swap);
|
CHECK_FERROR(mmap->swap);
|
||||||
|
|
||||||
fwrite(&page->key, sizeof(int), 1, mmap->swap);
|
if (key == -1) {
|
||||||
CHECK_FERROR(mmap->swap);
|
fseek(mmap->swap, i, SEEK_SET);
|
||||||
|
CHECK_FERROR(mmap->swap);
|
||||||
fseek(mmap->swap, 100*sizeof(int) + (i/sizeof(int))*4096, SEEK_SET);
|
|
||||||
CHECK_FERROR(mmap->swap);
|
|
||||||
|
|
||||||
fwrite(page->data, 4096, 1, mmap->swap);
|
fwrite(&page->key, sizeof(int), 1, mmap->swap);
|
||||||
CHECK_FERROR(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) {
|
struct PageInfo* read_from_swap(struct MMap* mmap, struct PageInfo *page) {
|
||||||
int key = -1;
|
int key = -1;
|
||||||
fseek(mmap->swap, 0, SEEK_SET);
|
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);
|
|
||||||
CHECK_FERROR(mmap->swap);
|
CHECK_FERROR(mmap->swap);
|
||||||
|
|
||||||
fread(page->data, 4096, 1, mmap->swap);
|
for (int i = 0; i < 100*sizeof(int); i+=sizeof(int)) {
|
||||||
CHECK_FERROR(mmap->swap);
|
int ret = fread(&key, sizeof(int), 1, mmap->swap);
|
||||||
|
CHECK_FERROR(mmap->swap);
|
||||||
|
|
||||||
fseek(mmap->swap, i, SEEK_SET);
|
if (key != page->key)
|
||||||
CHECK_FERROR(mmap->swap);
|
continue;
|
||||||
|
|
||||||
int tmp = -1;
|
fseek(mmap->swap, 100*sizeof(int) + (i/sizeof(int))*4096, SEEK_SET);
|
||||||
fwrite(&tmp, sizeof(int), 1, mmap->swap);
|
CHECK_FERROR(mmap->swap);
|
||||||
CHECK_FERROR(mmap->swap);
|
|
||||||
|
|
||||||
return page;
|
fread(page->data, 4096, 1, mmap->swap);
|
||||||
}
|
CHECK_FERROR(mmap->swap);
|
||||||
return NULL;
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,18 +3,18 @@
|
|||||||
#include "linked_list.h"
|
#include "linked_list.h"
|
||||||
|
|
||||||
struct MMap {
|
struct MMap {
|
||||||
void *memory;
|
void *memory;
|
||||||
int size;
|
int size;
|
||||||
int count;
|
int count;
|
||||||
struct PageInfo *support;
|
struct PageInfo *support;
|
||||||
struct linkedList *free_list;
|
struct linkedList *free_list;
|
||||||
struct linkedList *alloc_list;
|
struct linkedList *alloc_list;
|
||||||
FILE *swap;
|
FILE *swap;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PageInfo {
|
struct PageInfo {
|
||||||
int key;
|
int key;
|
||||||
void *data;
|
void *data;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct MMap* mmap_init();
|
struct MMap* mmap_init();
|
||||||
@ -25,3 +25,6 @@ int check_page_free_list(struct MMap *mmap);
|
|||||||
int check_page_alloc(struct MMap *mmap);
|
int check_page_alloc(struct MMap *mmap);
|
||||||
int move_to_swap(struct MMap *mmap, struct PageInfo *page);
|
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);
|
||||||
|
void page_remove(struct MMap *mmap, int key);
|
||||||
|
struct PageInfo *page_create(struct MMap *mmap, int *moved_to_swap);
|
@ -3,6 +3,6 @@
|
|||||||
#include "mmap.h"
|
#include "mmap.h"
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
// Some really cool tests!
|
// Some really cool tests!
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
3
TP/TP1/rendu/vmap.h
Normal file
3
TP/TP1/rendu/vmap.h
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
void *my_malloc(int size);
|
||||||
|
void my_free(void *data);
|
||||||
|
void my_copy(void *mem, void *data, int size);
|
Loading…
Reference in New Issue
Block a user